blob: 09ddab3b169fc0c46d49c30aaa13038d23dced3d [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();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000519 // set to true to force disableScissor to make a GL call.
520 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000521 this->disableScissor();
522
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000523 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000525 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000527
528 // TODO: I believe this should actually go in GrGpu::onResetContext
529 // rather than here
530 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
532 fHWGeometryState.fIndexBuffer = NULL;
533 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000534
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000535 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000537 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000538 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000539
540 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000541 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000542 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
543 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000544 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
546 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000547 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000548 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
549 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000550 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000551 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
552 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
bsalomon@google.come269f212011-11-07 13:29:52 +0000555GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000556 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000557 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000558 return NULL;
559 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000560
bsalomon@google.com99621082011-11-15 16:47:16 +0000561 glTexDesc.fWidth = desc.fWidth;
562 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000563 glTexDesc.fConfig = desc.fConfig;
564 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
565 glTexDesc.fOwnsID = false;
566 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
567
568 GrGLTexture* texture = NULL;
569 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
570 GrGLRenderTarget::Desc glRTDesc;
571 glRTDesc.fRTFBOID = 0;
572 glRTDesc.fTexFBOID = 0;
573 glRTDesc.fMSColorRenderbufferID = 0;
574 glRTDesc.fOwnIDs = true;
575 glRTDesc.fConfig = desc.fConfig;
576 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000577 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
578 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000579 glTexDesc.fTextureID,
580 &glRTDesc)) {
581 return NULL;
582 }
583 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
584 } else {
585 texture = new GrGLTexture(this, glTexDesc);
586 }
587 if (NULL == texture) {
588 return NULL;
589 }
590
591 this->setSpareTextureUnit();
592 return texture;
593}
594
595GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
596 GrGLRenderTarget::Desc glDesc;
597 glDesc.fConfig = desc.fConfig;
598 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
599 glDesc.fMSColorRenderbufferID = 0;
600 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
601 glDesc.fSampleCnt = desc.fSampleCnt;
602 glDesc.fOwnIDs = false;
603 GrGLIRect viewport;
604 viewport.fLeft = 0;
605 viewport.fBottom = 0;
606 viewport.fWidth = desc.fWidth;
607 viewport.fHeight = desc.fHeight;
608
609 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
610 if (desc.fStencilBits) {
611 GrGLStencilBuffer::Format format;
612 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
613 format.fPacked = false;
614 format.fStencilBits = desc.fStencilBits;
615 format.fTotalBits = desc.fStencilBits;
616 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
617 0,
618 desc.fWidth,
619 desc.fHeight,
620 desc.fSampleCnt,
621 format);
622 tgt->setStencilBuffer(sb);
623 sb->unref();
624 }
625 return tgt;
626}
627
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628////////////////////////////////////////////////////////////////////////////////
629
bsalomon@google.com6f379512011-11-16 20:36:03 +0000630void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
631 int left, int top, int width, int height,
632 GrPixelConfig config, const void* buffer,
633 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 if (NULL == buffer) {
635 return;
636 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000637 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
638
bsalomon@google.com6f379512011-11-16 20:36:03 +0000639 this->setSpareTextureUnit();
640 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
641 GrGLTexture::Desc desc;
642 desc.fConfig = glTex->config();
643 desc.fWidth = glTex->width();
644 desc.fHeight = glTex->height();
645 desc.fOrientation = glTex->orientation();
646 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000647
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000648 this->uploadTexData(desc, false,
649 left, top, width, height,
650 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000651}
652
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000653namespace {
654bool adjust_pixel_ops_params(int surfaceWidth,
655 int surfaceHeight,
656 size_t bpp,
657 int* left, int* top, int* width, int* height,
658 const void** data,
659 size_t* rowBytes) {
660 if (!*rowBytes) {
661 *rowBytes = *width * bpp;
662 }
663
664 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
665 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
666
667 if (!subRect.intersect(bounds)) {
668 return false;
669 }
670 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
671 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
672
673 *left = subRect.fLeft;
674 *top = subRect.fTop;
675 *width = subRect.width();
676 *height = subRect.height();
677 return true;
678}
679}
680
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000681bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
682 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000683 int left, int top, int width, int height,
684 GrPixelConfig dataConfig,
685 const void* data,
686 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000687 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000688
689 size_t bpp = GrBytesPerPixel(dataConfig);
690 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
691 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000692 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000694 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000695
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000696 // in case we need a temporary, trimmed copy of the src pixels
697 SkAutoSMalloc<128 * 128> tempStorage;
698
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000699 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000700 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000701 if (useTexStorage) {
702 if (kDesktop_GrGLBinding == this->glBinding()) {
703 // 565 is not a sized internal format on desktop GL. So on desktop
704 // with 565 we always use an unsized internal format to let the
705 // system pick the best sized format to convert the 565 data to.
706 // Since glTexStorage only allows sized internal formats we will
707 // instead fallback to glTexImage2D.
708 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
709 } else {
710 // ES doesn't allow paletted textures to be used with tex storage
711 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
712 }
713 }
714
715 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 GrGLenum externalFormat;
717 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000718 // glTexStorage requires sized internal formats on both desktop and ES. ES
719 // glTexImage requires an unsized format.
720 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
721 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000722 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000723 }
724
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000725 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000726 // paletted textures cannot be updated
727 return false;
728 }
729
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000730 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000731 * check whether to allocate a temporary buffer for flipping y or
732 * because our srcData has extra bytes past each row. If so, we need
733 * to trim those off here, since GL ES may not let us specify
734 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000737 bool swFlipY = false;
738 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000739 if (NULL != data) {
740 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000741 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 glFlipY = true;
743 } else {
744 swFlipY = true;
745 }
746 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000747 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000748 // can't use this for flipping, only non-neg values allowed. :(
749 if (rowBytes != trimRowBytes) {
750 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
751 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
752 restoreGLRowLength = true;
753 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000754 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000755 if (trimRowBytes != rowBytes || swFlipY) {
756 // copy data into our new storage, skipping the trailing bytes
757 size_t trimSize = height * trimRowBytes;
758 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000759 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000760 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000761 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000762 char* dst = (char*)tempStorage.reset(trimSize);
763 for (int y = 0; y < height; y++) {
764 memcpy(dst, src, trimRowBytes);
765 if (swFlipY) {
766 src -= rowBytes;
767 } else {
768 src += rowBytes;
769 }
770 dst += trimRowBytes;
771 }
772 // now point data to our copied version
773 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000776 if (glFlipY) {
777 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
778 }
779 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000780 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000781 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000782 if (isNewTexture &&
783 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000784 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000785 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 if (useTexStorage) {
787 // We never resize or change formats of textures. We don't use
788 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000789 GL_ALLOC_CALL(this->glInterface(),
790 TexStorage2D(GR_GL_TEXTURE_2D,
791 1, // levels
792 internalFormat,
793 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000794 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000795 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
796 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
797 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000798 GL_ALLOC_CALL(this->glInterface(),
799 CompressedTexImage2D(GR_GL_TEXTURE_2D,
800 0, // level
801 internalFormat,
802 desc.fWidth, desc.fHeight,
803 0, // border
804 imageSize,
805 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000806 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000807 GL_ALLOC_CALL(this->glInterface(),
808 TexImage2D(GR_GL_TEXTURE_2D,
809 0, // level
810 internalFormat,
811 desc.fWidth, desc.fHeight,
812 0, // border
813 externalFormat, externalType,
814 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000815 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000816 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000817 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000818 if (error != GR_GL_NO_ERROR) {
819 succeeded = false;
820 } else {
821 // if we have data and we used TexStorage to create the texture, we
822 // now upload with TexSubImage.
823 if (NULL != data && useTexStorage) {
824 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
825 0, // level
826 left, top,
827 width, height,
828 externalFormat, externalType,
829 data));
830 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000831 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000832 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000833 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000834 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000836 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
837 0, // level
838 left, top,
839 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000840 externalFormat, externalType, data));
841 }
842
843 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000844 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000845 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000846 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000847 if (glFlipY) {
848 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
849 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000850 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000851}
852
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000853namespace {
854bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
855 int sampleCount,
856 GrGLenum format,
857 int width, int height) {
858 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
859 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
860 bool created = false;
861 if (GrGLCaps::kNVDesktop_CoverageAAType ==
862 ctxInfo.caps().coverageAAType()) {
863 const GrGLCaps::MSAACoverageMode& mode =
864 ctxInfo.caps().getMSAACoverageMode(sampleCount);
865 GL_ALLOC_CALL(ctxInfo.interface(),
866 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
867 mode.fCoverageSampleCnt,
868 mode.fColorSampleCnt,
869 format,
870 width, height));
871 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
872 }
873 if (!created) {
874 GL_ALLOC_CALL(ctxInfo.interface(),
875 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
876 sampleCount,
877 format,
878 width, height));
879 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
880 }
881 return created;
882}
883}
884
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885bool GrGpuGL::createRenderTargetObjects(int width, int height,
886 GrGLuint texID,
887 GrGLRenderTarget::Desc* desc) {
888 desc->fMSColorRenderbufferID = 0;
889 desc->fRTFBOID = 0;
890 desc->fTexFBOID = 0;
891 desc->fOwnIDs = true;
892
893 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000894
bsalomon@google.comab15d612011-08-09 12:57:56 +0000895 GrGLenum msColorFormat = 0; // suppress warning
896
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000897 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 if (!desc->fTexFBOID) {
899 goto FAILED;
900 }
901
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000902
903 // If we are using multisampling we will create two FBOS. We render
904 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000905 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000906 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000907 goto FAILED;
908 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000909 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
910 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 if (!desc->fRTFBOID ||
912 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000913 !this->configToGLFormats(desc->fConfig,
914 // GLES requires sized internal formats
915 kES2_GrGLBinding == this->glBinding(),
916 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000917 goto FAILED;
918 }
919 } else {
920 desc->fRTFBOID = desc->fTexFBOID;
921 }
922
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000923 // below here we may bind the FBO
924 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000925 if (desc->fRTFBOID != desc->fTexFBOID) {
926 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000927 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000928 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000929 if (!renderbuffer_storage_msaa(fGLContextInfo,
930 desc->fSampleCnt,
931 msColorFormat,
932 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000933 goto FAILED;
934 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000935 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
936 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 GR_GL_COLOR_ATTACHMENT0,
938 GR_GL_RENDERBUFFER,
939 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000940 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000941 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
942 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
943 goto FAILED;
944 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000945 fGLContextInfo.caps().markConfigAsValidColorAttachment(
946 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000947 }
948 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000950
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
952 GR_GL_COLOR_ATTACHMENT0,
953 GR_GL_TEXTURE_2D,
954 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000955 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000956 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
957 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
958 goto FAILED;
959 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000960 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961 }
962
963 return true;
964
965FAILED:
966 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000968 }
969 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000971 }
972 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000973 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000974 }
975 return false;
976}
977
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000978// good to set a break-point here to know when createTexture fails
979static GrTexture* return_null_texture() {
980// GrAssert(!"null texture");
981 return NULL;
982}
983
984#if GR_DEBUG
985static size_t as_size_t(int x) {
986 return x;
987}
988#endif
989
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000990GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000991 const void* srcData,
992 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000993
994#if GR_COLLECT_STATS
995 ++fStats.fTextureCreateCnt;
996#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000997
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000998 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000999 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001001 // Attempt to catch un- or wrongly initialized sample counts;
1002 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
1003
bsalomon@google.com99621082011-11-15 16:47:16 +00001004 glTexDesc.fWidth = desc.fWidth;
1005 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001006 glTexDesc.fConfig = desc.fConfig;
1007 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009 glRTDesc.fMSColorRenderbufferID = 0;
1010 glRTDesc.fRTFBOID = 0;
1011 glRTDesc.fTexFBOID = 0;
1012 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001013 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001014
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001015 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001016
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001017 const Caps& caps = this->getCaps();
1018
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001019 // We keep GrRenderTargets in GL's normal orientation so that they
1020 // can be drawn to by the outside world without the client having
1021 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001022 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001023 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001024
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001025 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001026 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001027 desc.fSampleCnt) {
1028 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
1030
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001032 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1033 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001034 return return_null_texture();
1035 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 }
1037
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001038 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001039 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001040 // provides a hint about how this texture will be used
1041 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1042 GR_GL_TEXTURE_USAGE,
1043 GR_GL_FRAMEBUFFER_ATTACHMENT));
1044 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001045 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001046 return return_null_texture();
1047 }
1048
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001049 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001050 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001051
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001052 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1053 // drivers have a bug where an FBO won't be complete if it includes a
1054 // texture that is not mipmap complete (considering the filter in use).
1055 GrGLTexture::TexParams initialTexParams;
1056 // we only set a subset here so invalidate first
1057 initialTexParams.invalidate();
1058 initialTexParams.fFilter = GR_GL_NEAREST;
1059 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1060 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001061 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1062 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001063 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001064 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1065 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001066 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001067 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1068 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001069 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1071 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001072 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001073 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1074 glTexDesc.fWidth, glTexDesc.fHeight,
1075 desc.fConfig, srcData, rowBytes)) {
1076 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1077 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001078 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001079
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081 if (renderTarget) {
1082#if GR_COLLECT_STATS
1083 ++fStats.fRenderTargetCreateCnt;
1084#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001085 // unbind the texture from the texture unit before binding it to the frame buffer
1086 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1087
bsalomon@google.com99621082011-11-15 16:47:16 +00001088 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1089 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001090 glTexDesc.fTextureID,
1091 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001092 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 return return_null_texture();
1094 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001095 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001097 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001099 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001100#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001101 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1102 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001103#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104 return tex;
1105}
1106
1107namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001108
1109const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1110
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1112 GrGLuint rb,
1113 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001114 // we shouldn't ever know one size and not the other
1115 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1116 (kUnknownBitCount == format->fTotalBits));
1117 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1120 (GrGLint*)&format->fStencilBits);
1121 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1124 (GrGLint*)&format->fTotalBits);
1125 format->fTotalBits += format->fStencilBits;
1126 } else {
1127 format->fTotalBits = format->fStencilBits;
1128 }
1129 }
1130}
1131}
1132
1133bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1134 int width, int height) {
1135
1136 // All internally created RTs are also textures. We don't create
1137 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1138 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001139 GrAssert(width >= rt->width());
1140 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141
1142 int samples = rt->numSamples();
1143 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 if (!sbID) {
1146 return false;
1147 }
1148
1149 GrGLStencilBuffer* sb = NULL;
1150
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001151 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001153 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 // we start with the last stencil format that succeeded in hopes
1155 // that we won't go through this loop more than once after the
1156 // first (painful) stencil creation.
1157 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001158 const GrGLCaps::StencilFormat& sFmt =
1159 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001160 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001161 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001163 bool created;
1164 if (samples > 0) {
1165 created = renderbuffer_storage_msaa(fGLContextInfo,
1166 samples,
1167 sFmt.fInternalFormat,
1168 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001170 GL_ALLOC_CALL(this->glInterface(),
1171 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001172 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001173 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001174 created =
1175 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001177 if (created) {
1178 // After sized formats we attempt an unsized format and take
1179 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001180 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001182 sb = new GrGLStencilBuffer(this, sbID, width, height,
1183 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1185 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001186 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 return true;
1189 }
1190 sb->abandon(); // otherwise we lose sbID
1191 sb->unref();
1192 }
1193 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001195 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196}
1197
1198bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1199 GrRenderTarget* rt) {
1200 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1201
1202 GrGLuint fbo = glrt->renderFBOID();
1203
1204 if (NULL == sb) {
1205 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 GR_GL_STENCIL_ATTACHMENT,
1208 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 GR_GL_DEPTH_ATTACHMENT,
1211 GR_GL_RENDERBUFFER, 0));
1212#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001213 GrGLenum status;
1214 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1216#endif
1217 }
1218 return true;
1219 } else {
1220 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1221 GrGLuint rb = glsb->renderbufferID();
1222
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001223 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1225 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 GR_GL_STENCIL_ATTACHMENT,
1227 GR_GL_RENDERBUFFER, rb));
1228 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 GR_GL_DEPTH_ATTACHMENT,
1231 GR_GL_RENDERBUFFER, rb));
1232 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 GR_GL_DEPTH_ATTACHMENT,
1235 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001238 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001239 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001240 glsb->format())) {
1241 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1242 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001244 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001246 if (glsb->format().fPacked) {
1247 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1248 GR_GL_DEPTH_ATTACHMENT,
1249 GR_GL_RENDERBUFFER, 0));
1250 }
1251 return false;
1252 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001253 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001254 rt->config(),
1255 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001258 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001260}
1261
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001262////////////////////////////////////////////////////////////////////////////////
1263
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001264GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001265 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001266 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001269 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001270 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001272 GL_ALLOC_CALL(this->glInterface(),
1273 BufferData(GR_GL_ARRAY_BUFFER,
1274 size,
1275 NULL, // data ptr
1276 dynamic ? GR_GL_DYNAMIC_DRAW :
1277 GR_GL_STATIC_DRAW));
1278 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // deleting bound buffer does implicit bind to 0
1281 fHWGeometryState.fVertexBuffer = NULL;
1282 return NULL;
1283 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001284 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 size, dynamic);
1286 fHWGeometryState.fVertexBuffer = vertexBuffer;
1287 return vertexBuffer;
1288 }
1289 return NULL;
1290}
1291
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001292GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001293 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001294 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001297 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001299 GL_ALLOC_CALL(this->glInterface(),
1300 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1301 size,
1302 NULL, // data ptr
1303 dynamic ? GR_GL_DYNAMIC_DRAW :
1304 GR_GL_STATIC_DRAW));
1305 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001306 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 // deleting bound buffer does implicit bind to 0
1308 fHWGeometryState.fIndexBuffer = NULL;
1309 return NULL;
1310 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001311 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 size, dynamic);
1313 fHWGeometryState.fIndexBuffer = indexBuffer;
1314 return indexBuffer;
1315 }
1316 return NULL;
1317}
1318
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001319void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001320 const GrDrawState& drawState = this->getDrawState();
1321 const GrGLRenderTarget* rt =
1322 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1323
1324 GrAssert(NULL != rt);
1325 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001326
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001327 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001328 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1329 rect.width(), rect.height());
1330 if (scissor.contains(vp)) {
1331 disableScissor();
1332 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 }
1334
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001335 if (fHWBounds.fScissorRect != scissor) {
1336 scissor.pushToGLScissor(this->glInterface());
1337 fHWBounds.fScissorRect = scissor;
1338 }
1339 if (!fHWBounds.fScissorEnabled) {
1340 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1341 fHWBounds.fScissorEnabled = true;
1342 }
1343}
1344
1345void GrGpuGL::disableScissor() {
1346 if (fHWBounds.fScissorEnabled) {
1347 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1348 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 }
1350}
1351
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001352void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001353 const GrDrawState& drawState = this->getDrawState();
1354 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001355 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001356 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001357
bsalomon@google.com74b98712011-11-11 19:46:16 +00001358 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001359 if (NULL != rect) {
1360 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001361 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001362 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001363 if (clippedRect.intersect(rtRect)) {
1364 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001365 } else {
1366 return;
1367 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001368 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001369 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001370 if (NULL != rect)
1371 this->enableScissoring(*rect);
1372 else
1373 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001374
1375 GrGLfloat r, g, b, a;
1376 static const GrGLfloat scale255 = 1.f / 255.f;
1377 a = GrColorUnpackA(color) * scale255;
1378 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001379 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001380 scaleRGB *= a;
1381 }
1382 r = GrColorUnpackR(color) * scaleRGB;
1383 g = GrColorUnpackG(color) * scaleRGB;
1384 b = GrColorUnpackB(color) * scaleRGB;
1385
1386 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001387 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001388 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001389 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001390}
1391
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001392void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001393 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001394 return;
1395 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001396
1397 this->flushRenderTarget(&GrIRect::EmptyIRect());
1398
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001399 this->disableScissor();
1400
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001401 GL_CALL(StencilMask(0xffffffff));
1402 GL_CALL(ClearStencil(0));
1403 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001404 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001405}
1406
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001407void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001408 const GrDrawState& drawState = this->getDrawState();
1409 const GrRenderTarget* rt = drawState.getRenderTarget();
1410 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001411
1412 // this should only be called internally when we know we have a
1413 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 GrAssert(NULL != rt->getStencilBuffer());
1415 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001416#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001418 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001419#else
1420 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001421 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001422 // turned into draws. Our contract on GrDrawTarget says that
1423 // changing the clip between stencil passes may or may not
1424 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001425 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001426#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001427 GrGLint value;
1428 if (insideClip) {
1429 value = (1 << (stencilBitCount - 1));
1430 } else {
1431 value = 0;
1432 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001433 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001434 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001435 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001436 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001437 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001438 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001439}
1440
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001441void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001442 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
bsalomon@google.comc4364992011-11-07 15:54:49 +00001445bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1446 int left, int top,
1447 int width, int height,
1448 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001449 size_t rowBytes) const {
1450 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001451 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001452 return false;
1453 }
1454
1455 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001456 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001457 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001458 return true;
1459 }
1460 // If we have to do memcpys to handle rowBytes then y-flip is free
1461 // Note the rowBytes might be tight to the passed in data, but if data
1462 // gets clipped in x to the target the rowBytes will no longer be tight.
1463 if (left >= 0 && (left + width) < renderTarget->width()) {
1464 return 0 == rowBytes ||
1465 GrBytesPerPixel(config) * width == rowBytes;
1466 } else {
1467 return false;
1468 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001469}
1470
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001471bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001472 int left, int top,
1473 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001474 GrPixelConfig config,
1475 void* buffer,
1476 size_t rowBytes,
1477 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001478 GrGLenum format;
1479 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001480 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001481 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001482 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001483 size_t bpp = GrBytesPerPixel(config);
1484 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1485 &left, &top, &width, &height,
1486 const_cast<const void**>(&buffer),
1487 &rowBytes)) {
1488 return false;
1489 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001490
bsalomon@google.comc6980972011-11-02 19:57:21 +00001491 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001492 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001493 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001494 switch (tgt->getResolveType()) {
1495 case GrGLRenderTarget::kCantResolve_ResolveType:
1496 return false;
1497 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001498 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001499 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001500 break;
1501 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001502 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001503 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001504 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1505 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001506 break;
1507 default:
1508 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 }
1510
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001511 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001512
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001513 // the read rect is viewport-relative
1514 GrGLIRect readRect;
1515 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001517 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001518 if (0 == rowBytes) {
1519 rowBytes = tightRowBytes;
1520 }
1521 size_t readDstRowBytes = tightRowBytes;
1522 void* readDst = buffer;
1523
1524 // determine if GL can read using the passed rowBytes or if we need
1525 // a scratch buffer.
1526 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1527 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001528 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001529 GrAssert(!(rowBytes % sizeof(GrColor)));
1530 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1531 readDstRowBytes = rowBytes;
1532 } else {
1533 scratch.reset(tightRowBytes * height);
1534 readDst = scratch.get();
1535 }
1536 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001537 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001538 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1539 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001540 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1541 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001542 format, type, readDst));
1543 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001544 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001545 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1546 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001547 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001548 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1549 invertY = true;
1550 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001551
1552 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001553 // API presents top-to-bottom. We must preserve the padding contents. Note
1554 // that the above readPixels did not overwrite the padding.
1555 if (readDst == buffer) {
1556 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001557 if (!invertY) {
1558 scratch.reset(tightRowBytes);
1559 void* tmpRow = scratch.get();
1560 // flip y in-place by rows
1561 const int halfY = height >> 1;
1562 char* top = reinterpret_cast<char*>(buffer);
1563 char* bottom = top + (height - 1) * rowBytes;
1564 for (int y = 0; y < halfY; y++) {
1565 memcpy(tmpRow, top, tightRowBytes);
1566 memcpy(top, bottom, tightRowBytes);
1567 memcpy(bottom, tmpRow, tightRowBytes);
1568 top += rowBytes;
1569 bottom -= rowBytes;
1570 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001571 }
1572 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001573 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 // copy from readDst to buffer while flipping y
1575 const int halfY = height >> 1;
1576 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001577 char* dst = reinterpret_cast<char*>(buffer);
1578 if (!invertY) {
1579 dst += (height-1) * rowBytes;
1580 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001581 for (int y = 0; y < height; y++) {
1582 memcpy(dst, src, tightRowBytes);
1583 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001584 if (invertY) {
1585 dst += rowBytes;
1586 } else {
1587 dst -= rowBytes;
1588 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 }
1590 }
1591 return true;
1592}
1593
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001594void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001595
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001596 GrGLRenderTarget* rt =
1597 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1598 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001599
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001600 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001601 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 #if GR_COLLECT_STATS
1603 ++fStats.fRenderTargetChngCnt;
1604 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001606 GrGLenum status;
1607 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001608 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001609 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 }
1611 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001612 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001613 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001614 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001615 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001616 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 fHWBounds.fViewportRect = vp;
1618 }
1619 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001620 if (NULL == bound || !bound->isEmpty()) {
1621 rt->flagAsNeedingResolve(bound);
1622 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001623}
1624
twiz@google.com0f31ca72011-03-18 17:38:11 +00001625GrGLenum gPrimitiveType2GLMode[] = {
1626 GR_GL_TRIANGLES,
1627 GR_GL_TRIANGLE_STRIP,
1628 GR_GL_TRIANGLE_FAN,
1629 GR_GL_POINTS,
1630 GR_GL_LINES,
1631 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001632};
1633
bsalomon@google.comd302f142011-03-03 13:54:13 +00001634#define SWAP_PER_DRAW 0
1635
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001636#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001637 #if GR_MAC_BUILD
1638 #include <AGL/agl.h>
1639 #elif GR_WIN32_BUILD
1640 void SwapBuf() {
1641 DWORD procID = GetCurrentProcessId();
1642 HWND hwnd = GetTopWindow(GetDesktopWindow());
1643 while(hwnd) {
1644 DWORD wndProcID = 0;
1645 GetWindowThreadProcessId(hwnd, &wndProcID);
1646 if(wndProcID == procID) {
1647 SwapBuffers(GetDC(hwnd));
1648 }
1649 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1650 }
1651 }
1652 #endif
1653#endif
1654
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001655void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1656 uint32_t startVertex,
1657 uint32_t startIndex,
1658 uint32_t vertexCount,
1659 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001660 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1661
twiz@google.com0f31ca72011-03-18 17:38:11 +00001662 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001663
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001664 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1665 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1666
1667 // our setupGeometry better have adjusted this to zero since
1668 // DrawElements always draws from the begining of the arrays for idx 0.
1669 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001670
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001671 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1672 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001673#if SWAP_PER_DRAW
1674 glFlush();
1675 #if GR_MAC_BUILD
1676 aglSwapBuffers(aglGetCurrentContext());
1677 int set_a_break_pt_here = 9;
1678 aglSwapBuffers(aglGetCurrentContext());
1679 #elif GR_WIN32_BUILD
1680 SwapBuf();
1681 int set_a_break_pt_here = 9;
1682 SwapBuf();
1683 #endif
1684#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001685}
1686
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001687void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1688 uint32_t startVertex,
1689 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1691
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001692 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1693
1694 // our setupGeometry better have adjusted this to zero.
1695 // DrawElements doesn't take an offset so we always adjus the startVertex.
1696 GrAssert(0 == startVertex);
1697
1698 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1699 // account for startVertex in the DrawElements case. So we always
1700 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001701 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001702#if SWAP_PER_DRAW
1703 glFlush();
1704 #if GR_MAC_BUILD
1705 aglSwapBuffers(aglGetCurrentContext());
1706 int set_a_break_pt_here = 9;
1707 aglSwapBuffers(aglGetCurrentContext());
1708 #elif GR_WIN32_BUILD
1709 SwapBuf();
1710 int set_a_break_pt_here = 9;
1711 SwapBuf();
1712 #endif
1713#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001714}
1715
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001716void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1717
1718 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001719
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001720 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001721 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001723 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1724 rt->renderFBOID()));
1725 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1726 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 #if GR_COLLECT_STATS
1728 ++fStats.fRenderTargetChngCnt;
1729 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001730 // make sure we go through flushRenderTarget() since we've modified
1731 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001732 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001733 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001734 const GrIRect dirtyRect = rt->getResolveRect();
1735 GrGLIRect r;
1736 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1737 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001738
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001739 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001740 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001741#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001742 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1743 GL_CALL(Scissor(r.fLeft, r.fBottom,
1744 r.fWidth, r.fHeight));
1745 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001746 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001748#else
1749 this->enableScissoring(dirtyRect);
1750 GL_CALL(ResolveMultisampleFramebuffer());
1751#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001753 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001754 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001755 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1756 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001757 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001758 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001759 int right = r.fLeft + r.fWidth;
1760 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001761 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1762 r.fLeft, r.fBottom, right, top,
1763 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001765 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 }
1767}
1768
twiz@google.com0f31ca72011-03-18 17:38:11 +00001769static const GrGLenum grToGLStencilFunc[] = {
1770 GR_GL_ALWAYS, // kAlways_StencilFunc
1771 GR_GL_NEVER, // kNever_StencilFunc
1772 GR_GL_GREATER, // kGreater_StencilFunc
1773 GR_GL_GEQUAL, // kGEqual_StencilFunc
1774 GR_GL_LESS, // kLess_StencilFunc
1775 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1776 GR_GL_EQUAL, // kEqual_StencilFunc,
1777 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001778};
1779GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1780GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1781GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1782GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1783GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1784GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1785GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1786GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1787GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1788
twiz@google.com0f31ca72011-03-18 17:38:11 +00001789static const GrGLenum grToGLStencilOp[] = {
1790 GR_GL_KEEP, // kKeep_StencilOp
1791 GR_GL_REPLACE, // kReplace_StencilOp
1792 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1793 GR_GL_INCR, // kIncClamp_StencilOp
1794 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1795 GR_GL_DECR, // kDecClamp_StencilOp
1796 GR_GL_ZERO, // kZero_StencilOp
1797 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798};
1799GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1800GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1801GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1802GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1803GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1804GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1805GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1806GR_STATIC_ASSERT(6 == kZero_StencilOp);
1807GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1808
reed@google.comac10a2d2010-12-22 21:39:39 +00001809void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001810 const GrDrawState& drawState = this->getDrawState();
1811
1812 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001813
1814 // use stencil for clipping if clipping is enabled and the clip
1815 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001816 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001817 bool drawClipToStencil =
1818 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001819 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1820 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001821 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1822 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001823
1824 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001825
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001826 // we can't simultaneously perform stencil-clipping and
1827 // modify the stencil clip
1828 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001829
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 if (settings->isDisabled()) {
1831 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001832 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835
1836 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001837 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001838 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001839 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001840 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001841 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001842 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1843 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1844 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1845 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1846 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1847 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1848 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1849 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850 }
1851 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001852 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001853 GrStencilBuffer* stencilBuffer =
1854 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001855 if (NULL != stencilBuffer) {
1856 stencilBits = stencilBuffer->bits();
1857 }
1858 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001859 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001860
1861 GrGLuint clipStencilMask = 0;
1862 GrGLuint userStencilMask = ~0;
1863 if (stencilBits > 0) {
1864 clipStencilMask = 1 << (stencilBits - 1);
1865 userStencilMask = clipStencilMask - 1;
1866 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001867
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001868 unsigned int frontRef = settings->frontFuncRef();
1869 unsigned int frontMask = settings->frontFuncMask();
1870 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001871 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001873 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001874 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1875 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001876 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001877 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001878 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001879
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001880 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001881 stencilClip,
1882 clipStencilMask,
1883 userStencilMask,
1884 &frontRef,
1885 &frontMask);
1886 frontWriteMask &= userStencilMask;
1887 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001888 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001889 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001890 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001891 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001892 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001893 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001894 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001895 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001896 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001897 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001898
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001899 unsigned int backRef = settings->backFuncRef();
1900 unsigned int backMask = settings->backFuncMask();
1901 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001902
1903
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001904 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001905 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1906 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001907 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001908 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001909 stencilClip, settings->backFunc())];
1910 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001911 stencilClip,
1912 clipStencilMask,
1913 userStencilMask,
1914 &backRef,
1915 &backMask);
1916 backWriteMask &= userStencilMask;
1917 }
1918
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001919 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1920 frontRef, frontMask));
1921 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1922 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1923 backRef, backMask));
1924 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1925 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001926 grToGLStencilOp[settings->frontFailOp()],
1927 grToGLStencilOp[settings->frontPassOp()],
1928 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001929
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001930 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001931 grToGLStencilOp[settings->backFailOp()],
1932 grToGLStencilOp[settings->backPassOp()],
1933 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001934 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001935 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1936 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001937 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1938 grToGLStencilOp[settings->frontPassOp()],
1939 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001940 }
1941 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001942 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001943 fHWStencilClip = stencilClip;
1944 }
1945}
1946
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001947void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001948 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001949 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001950 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1951 // smooth lines.
1952
1953 // we prefer smooth lines over multisampled lines
1954 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001955 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001956 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001958 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001960 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001961 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001962 fHWAAState.fSmoothLineEnabled = false;
1963 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001964 if (rt->isMultisampled() &&
1965 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001966 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001967 fHWAAState.fMSAAEnabled = false;
1968 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001969 } else if (rt->isMultisampled() &&
1970 this->getDrawState().isHWAntialiasState() !=
1971 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001972 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001973 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001974 fHWAAState.fMSAAEnabled = false;
1975 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001977 fHWAAState.fMSAAEnabled = true;
1978 }
1979 }
1980 }
1981}
1982
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001983void GrGpuGL::flushBlend(GrPrimitiveType type,
1984 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001985 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001986 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001987 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001988 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 fHWBlendDisabled = false;
1990 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001991 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1992 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1994 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001995 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 }
1997 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001998 // any optimization to disable blending should
1999 // have already been applied and tweaked the coeffs
2000 // to (1, 0).
2001 bool blendOff = kOne_BlendCoeff == srcCoeff &&
2002 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002003 if (fHWBlendDisabled != blendOff) {
2004 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002006 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002007 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002008 }
2009 fHWBlendDisabled = blendOff;
2010 }
2011 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002012 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2013 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2015 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002016 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002017 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002018 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002019 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2020 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002021 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002022
2023 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002024 GrColorUnpackR(blendConst) / 255.f,
2025 GrColorUnpackG(blendConst) / 255.f,
2026 GrColorUnpackB(blendConst) / 255.f,
2027 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002028 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002030 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002031 }
2032 }
2033 }
2034}
2035
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002036namespace {
2037
2038unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002039 switch (filter) {
2040 case GrSamplerState::kBilinear_Filter:
2041 case GrSamplerState::k4x4Downsample_Filter:
2042 return GR_GL_LINEAR;
2043 case GrSamplerState::kNearest_Filter:
2044 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002045 case GrSamplerState::kErode_Filter:
2046 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002047 return GR_GL_NEAREST;
2048 default:
2049 GrAssert(!"Unknown filter type");
2050 return GR_GL_LINEAR;
2051 }
2052}
2053
robertphillips@google.com69950682012-04-06 18:06:10 +00002054// get_swizzle is only called from this .cpp so it is OK to inline it here
2055inline const GrGLenum* get_swizzle(GrPixelConfig config,
2056 const GrSamplerState& sampler,
2057 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002058 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00002059 if (glCaps.textureRedSupport()) {
2060 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2061 GR_GL_RED, GR_GL_RED };
2062 return gRedSmear;
2063 } else {
2064 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2065 GR_GL_ALPHA, GR_GL_ALPHA };
2066 return gAlphaSmear;
2067 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002068 } else if (sampler.swapsRAndB()) {
2069 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2070 GR_GL_RED, GR_GL_ALPHA };
2071 return gRedBlueSwap;
2072 } else {
2073 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2074 GR_GL_BLUE, GR_GL_ALPHA };
2075 return gStraight;
2076 }
2077}
2078
2079void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2080 // should add texparameteri to interface to make 1 instead of 4 calls here
2081 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2082 GR_GL_TEXTURE_SWIZZLE_R,
2083 swizzle[0]));
2084 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2085 GR_GL_TEXTURE_SWIZZLE_G,
2086 swizzle[1]));
2087 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2088 GR_GL_TEXTURE_SWIZZLE_B,
2089 swizzle[2]));
2090 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2091 GR_GL_TEXTURE_SWIZZLE_A,
2092 swizzle[3]));
2093}
2094}
2095
bsalomon@google.comffca4002011-02-22 20:34:01 +00002096bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002097
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002099 // GrGpu::setupClipAndFlushState should have already checked this
2100 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002101 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002102
tomhudson@google.com93813632011-10-27 20:21:16 +00002103 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002104 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002105 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002106 GrGLTexture* nextTexture =
2107 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002108
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002109 // true for now, but maybe not with GrEffect.
2110 GrAssert(NULL != nextTexture);
2111 // if we created a rt/tex and rendered to it without using a
2112 // texture and now we're texuring from the rt it will still be
2113 // the last bound texture, but it needs resolving. So keep this
2114 // out of the "last != next" check.
2115 GrGLRenderTarget* texRT =
2116 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2117 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002118 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002119 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002121 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002122 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002124 #if GR_COLLECT_STATS
2125 ++fStats.fTextureChngCnt;
2126 #endif
2127 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002128 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002129 // The texture matrix has to compensate for texture width/height
2130 // and NPOT-embedded-in-POT
2131 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002132 }
2133
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002134 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002135 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002136 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002137 nextTexture->getCachedTexParams(&timestamp);
2138 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002139 GrGLTexture::TexParams newTexParams;
2140
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002142
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002143 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2145 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002146 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002147 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002148 sizeof(newTexParams.fSwizzleRGBA));
2149 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002150 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002151 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002152 GR_GL_TEXTURE_MAG_FILTER,
2153 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002155 GR_GL_TEXTURE_MIN_FILTER,
2156 newTexParams.fFilter));
2157 }
2158 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2159 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002160 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002161 GR_GL_TEXTURE_WRAP_S,
2162 newTexParams.fWrapS));
2163 }
2164 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2165 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002166 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002167 GR_GL_TEXTURE_WRAP_T,
2168 newTexParams.fWrapT));
2169 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002170 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002171 (setAll ||
2172 memcmp(newTexParams.fSwizzleRGBA,
2173 oldTexParams.fSwizzleRGBA,
2174 sizeof(newTexParams.fSwizzleRGBA)))) {
2175 setTextureUnit(s);
2176 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2177 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002178 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002179 nextTexture->setCachedTexParams(newTexParams,
2180 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002181 }
2182 }
2183
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002184 GrIRect* rect = NULL;
2185 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002186 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002187 fClip.hasConservativeBounds()) {
2188 fClip.getConservativeBounds().roundOut(&clipBounds);
2189 rect = &clipBounds;
2190 }
2191 this->flushRenderTarget(rect);
2192 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002193
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002194 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2195 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002196 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002198 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 }
2200 }
2201
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002202 if (drawState->isColorWriteDisabled() !=
2203 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002204 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002205 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002206 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002207 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002208 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002209 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 }
2212
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002213 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002214 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002215 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002216 GL_CALL(Enable(GR_GL_CULL_FACE));
2217 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002218 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002219 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002220 GL_CALL(Enable(GR_GL_CULL_FACE));
2221 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002222 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002223 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002224 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002225 break;
2226 default:
2227 GrCrash("Unknown draw face.");
2228 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002229 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002230 }
2231
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002232#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002233 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002234 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002235 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002236 NULL == drawState->getRenderTarget() ||
2237 NULL == drawState->getTexture(s) ||
2238 drawState->getTexture(s)->asRenderTarget() !=
2239 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002240 }
2241#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002242
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002243 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002244
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002245 // This copy must happen after flushStencil() is called. flushStencil()
2246 // relies on detecting when the kModifyStencilClip_StateBit state has
2247 // changed since the last draw.
2248 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002249 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002250}
2251
2252void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002253 if (fHWGeometryState.fVertexBuffer != buffer) {
2254 fHWGeometryState.fArrayPtrsDirty = true;
2255 fHWGeometryState.fVertexBuffer = buffer;
2256 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002257}
2258
2259void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 if (fHWGeometryState.fVertexBuffer == buffer) {
2261 // deleting bound buffer does implied bind to 0
2262 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002263 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 }
2265}
2266
2267void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002268 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269}
2270
2271void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002272 if (fHWGeometryState.fIndexBuffer == buffer) {
2273 // deleting bound buffer does implied bind to 0
2274 fHWGeometryState.fIndexBuffer = NULL;
2275 }
2276}
2277
reed@google.comac10a2d2010-12-22 21:39:39 +00002278void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2279 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002280 GrDrawState* drawState = this->drawState();
2281 if (drawState->getRenderTarget() == renderTarget) {
2282 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002283 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002284 if (fHWDrawState.getRenderTarget() == renderTarget) {
2285 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002287}
2288
2289void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002290 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002291 GrDrawState* drawState = this->drawState();
2292 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002293 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002294 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002295 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002296 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002297 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002298 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002299 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002300}
2301
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002302bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2303 bool getSizedInternalFormat,
2304 GrGLenum* internalFormat,
2305 GrGLenum* externalFormat,
2306 GrGLenum* externalType) {
2307 GrGLenum dontCare;
2308 if (NULL == internalFormat) {
2309 internalFormat = &dontCare;
2310 }
2311 if (NULL == externalFormat) {
2312 externalFormat = &dontCare;
2313 }
2314 if (NULL == externalType) {
2315 externalType = &dontCare;
2316 }
2317
reed@google.comac10a2d2010-12-22 21:39:39 +00002318 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002319 case kRGBA_8888_PM_GrPixelConfig:
2320 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002321 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002322 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002323 if (getSizedInternalFormat) {
2324 *internalFormat = GR_GL_RGBA8;
2325 } else {
2326 *internalFormat = GR_GL_RGBA;
2327 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002328 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002329 break;
2330 case kBGRA_8888_PM_GrPixelConfig:
2331 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002332 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002333 return false;
2334 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002335 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002336 if (getSizedInternalFormat) {
2337 *internalFormat = GR_GL_BGRA8;
2338 } else {
2339 *internalFormat = GR_GL_BGRA;
2340 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002341 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002342 if (getSizedInternalFormat) {
2343 *internalFormat = GR_GL_RGBA8;
2344 } else {
2345 *internalFormat = GR_GL_RGBA;
2346 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002347 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002348 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002349 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002350 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002351 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002352 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002353 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002354 if (getSizedInternalFormat) {
2355 if (this->glBinding() == kDesktop_GrGLBinding) {
2356 return false;
2357 } else {
2358 *internalFormat = GR_GL_RGB565;
2359 }
2360 } else {
2361 *internalFormat = GR_GL_RGB;
2362 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002363 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002364 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002365 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002366 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002367 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002368 if (getSizedInternalFormat) {
2369 *internalFormat = GR_GL_RGBA4;
2370 } else {
2371 *internalFormat = GR_GL_RGBA;
2372 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002373 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002374 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002375 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002376 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002377 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002378 // glCompressedTexImage doesn't take external params
2379 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002380 // no sized/unsized internal format distinction here
2381 *internalFormat = GR_GL_PALETTE8_RGBA8;
2382 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002383 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002384 } else {
2385 return false;
2386 }
2387 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002388 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002389 if (this->glCaps().textureRedSupport()) {
2390 *internalFormat = GR_GL_RED;
2391 *externalFormat = GR_GL_RED;
2392 if (getSizedInternalFormat) {
2393 *internalFormat = GR_GL_R8;
2394 } else {
2395 *internalFormat = GR_GL_RED;
2396 }
2397 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002398 } else {
2399 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002400 *externalFormat = GR_GL_ALPHA;
2401 if (getSizedInternalFormat) {
2402 *internalFormat = GR_GL_ALPHA8;
2403 } else {
2404 *internalFormat = GR_GL_ALPHA;
2405 }
2406 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002407 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 break;
2409 default:
2410 return false;
2411 }
2412 return true;
2413}
2414
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002415void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002416 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002417 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002418 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002419 fActiveTextureUnitIdx = unit;
2420 }
2421}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002422
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002423void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002424 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002425 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002426 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2427 }
2428}
2429
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002430void GrGpuGL::resetDirtyFlags() {
2431 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2432}
2433
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002434void GrGpuGL::setBuffers(bool indexed,
2435 int* extraVertexOffset,
2436 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002437
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002438 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002439
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002440 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2441
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002443 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002444 case kBuffer_GeometrySrcType:
2445 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002446 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002447 break;
2448 case kArray_GeometrySrcType:
2449 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002450 this->finalizeReservedVertices();
2451 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2452 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002453 break;
2454 default:
2455 vbuf = NULL; // suppress warning
2456 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002457 }
2458
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002459 GrAssert(NULL != vbuf);
2460 GrAssert(!vbuf->isLocked());
2461 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002462 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002463 fHWGeometryState.fArrayPtrsDirty = true;
2464 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002465 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002466
2467 if (indexed) {
2468 GrAssert(NULL != extraIndexOffset);
2469
2470 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002471 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002472 case kBuffer_GeometrySrcType:
2473 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002474 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002475 break;
2476 case kArray_GeometrySrcType:
2477 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002478 this->finalizeReservedIndices();
2479 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2480 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002481 break;
2482 default:
2483 ibuf = NULL; // suppress warning
2484 GrCrash("Unknown geometry src type!");
2485 }
2486
2487 GrAssert(NULL != ibuf);
2488 GrAssert(!ibuf->isLocked());
2489 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002490 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002491 fHWGeometryState.fIndexBuffer = ibuf;
2492 }
2493 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002494}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002495
2496int GrGpuGL::getMaxEdges() const {
2497 // FIXME: This is a pessimistic estimate based on how many other things
2498 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002499 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002500 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002501}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002502