blob: a06b0084aa03dfbc1f559bef79943c23acaa46e9 [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
234 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000235 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000236 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000237 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 for (int i = 0; i < numFormats; ++i) {
239 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
240 fCaps.f8BitPaletteSupport = true;
241 break;
242 }
243 }
244
245 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000246 // we could also look for GL_ATI_separate_stencil extension or
247 // GL_EXT_stencil_two_side but they use different function signatures
248 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000249 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000251 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 this->hasExtension("GL_EXT_stencil_wrap");
253 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000254 // ES 2 has two sided stencil and stencil wrap
255 fCaps.fTwoSidedStencilSupport = true;
256 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000257 }
258
259 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000260 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
261 // extension includes glMapBuffer.
262 } else {
263 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
264 }
265
266 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000267 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000268 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
269 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000270 } else {
271 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 }
273 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000274 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000275 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000276 }
277
278 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
279
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000280 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
281 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000282 // Our render targets are always created with textures as the color
283 // attachment, hence this min:
284 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
285
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000286 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000287}
288
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000289void GrGpuGL::fillInConfigRenderableTable() {
290
291 // OpenGL < 3.0
292 // no support for render targets unless the GL_ARB_framebuffer_object
293 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
294 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
295 // probably don't get R8 in this case.
296
297 // OpenGL 3.0
298 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
299 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
300
301 // >= OpenGL 3.1
302 // base color renderable: RED, RG, RGB, and RGBA
303 // sized derivatives: R8, RGBA4, RGBA8
304 // if the GL_ARB_compatibility extension is supported then we get back
305 // support for GL_ALPHA and ALPHA8
306
307 // GL_EXT_bgra adds BGRA render targets to any version
308
309 // ES 2.0
310 // color renderable: RGBA4, RGB5_A1, RGB565
311 // GL_EXT_texture_rg adds support for R8 as a color render target
312 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
313 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
314 // added BGRA support
315
316 if (kDesktop_GrGLBinding == this->glBinding()) {
317 // Post 3.0 we will get R8
318 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
319 if (this->glVersion() >= GR_GL_VER(3,0) ||
320 this->hasExtension("GL_ARB_framebuffer_object")) {
321 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
322 }
323 } else {
324 // On ES we can only hope for R8
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000325 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
326 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000327 }
328
329 if (kDesktop_GrGLBinding != this->glBinding()) {
330 // only available in ES
331 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
332 }
333
334 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
335 // GL_EXT_framebuffer_object for FBO support. Both of these
336 // allow RGBA4 render targets so this is always supported.
337 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
338
339 if (this->glCaps().rgba8RenderbufferSupport()) {
340 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig] = true;
341 }
342
343 if (this->glCaps().bgraFormatSupport()) {
344 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig] = true;
345 }
346
347 // the un-premultiplied formats just inherit the premultiplied setting
348 fConfigRenderSupport[kRGBA_8888_UPM_GrPixelConfig] =
349 fConfigRenderSupport[kRGBA_8888_PM_GrPixelConfig];
350 fConfigRenderSupport[kBGRA_8888_UPM_GrPixelConfig] =
351 fConfigRenderSupport[kBGRA_8888_PM_GrPixelConfig];
352}
353
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000354bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
355 if (kUnknown_CanPreserveUnpremulRoundtrip ==
356 fCanPreserveUnpremulRoundtrip) {
357
358 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
359 uint32_t* srcData = data.get();
360 uint32_t* firstRead = data.get() + 256 * 256;
361 uint32_t* secondRead = data.get() + 2 * 256 * 256;
362
363 for (int y = 0; y < 256; ++y) {
364 for (int x = 0; x < 256; ++x) {
365 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
366 color[3] = y;
367 color[2] = x;
368 color[1] = x;
369 color[0] = x;
370 }
371 }
372
373 // We have broader support for read/write pixels on render targets
374 // than on textures.
375 GrTextureDesc dstDesc;
376 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
377 kNoStencil_GrTextureFlagBit;
378 dstDesc.fWidth = 256;
379 dstDesc.fHeight = 256;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000380 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000381 dstDesc.fSampleCnt = 0;
382
383 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
384 if (!dstTex.get()) {
385 return false;
386 }
387 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
388 GrAssert(NULL != rt);
389
390 bool failed = true;
391 static const UnpremulConversion gMethods[] = {
392 kUpOnWrite_DownOnRead_UnpremulConversion,
393 kDownOnWrite_UpOnRead_UnpremulConversion,
394 };
395
396 // pretend that we can do the roundtrip to avoid recursive calls to
397 // this function
398 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
399 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
400 fUnpremulConversion = gMethods[i];
401 rt->writePixels(0, 0,
402 256, 256,
403 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
404 rt->readPixels(0, 0,
405 256, 256,
406 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
407 rt->writePixels(0, 0,
408 256, 256,
409 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
410 rt->readPixels(0, 0,
411 256, 256,
412 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
413 failed = false;
414 for (int j = 0; j < 256 * 256; ++j) {
415 if (firstRead[j] != secondRead[j]) {
416 failed = true;
417 break;
418 }
419 }
420 }
421 fCanPreserveUnpremulRoundtrip = failed ?
422 kNo_CanPreserveUnpremulRoundtrip :
423 kYes_CanPreserveUnpremulRoundtrip;
424 }
425
426 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
427 return true;
428 } else {
429 return false;
430 }
431}
432
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000433GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000434 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
435 return GrPixelConfigSwapRAndB(config);
436 } else {
437 return config;
438 }
439}
440
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000441GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000442 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000443 return GrPixelConfigSwapRAndB(config);
444 } else {
445 return config;
446 }
447}
448
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000449bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
450 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
451}
452
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000453void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000454 if (gPrintStartupSpew && !fPrintedCaps) {
455 fPrintedCaps = true;
456 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000457 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000458 }
459
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000460 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000462 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000464 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 GL_CALL(Disable(GR_GL_DEPTH_TEST));
466 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000467
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000468 GL_CALL(Disable(GR_GL_CULL_FACE));
469 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000470 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000472 GL_CALL(Disable(GR_GL_DITHER));
473 if (kDesktop_GrGLBinding == this->glBinding()) {
474 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
475 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
476 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000477 fHWAAState.fMSAAEnabled = false;
478 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000479 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000481 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000482 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000483
reed@google.comac10a2d2010-12-22 21:39:39 +0000484 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000485 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000487 // invalid
488 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000491 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000492
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000493 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000494 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000495
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000496 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000497
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000498 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000499
tomhudson@google.com93813632011-10-27 20:21:16 +0000500 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000501 fHWDrawState.setTexture(s, NULL);
502 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
503 -GR_ScalarMax,
504 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000505 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000506 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000507 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000508
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000509 fHWBounds.fScissorRect.invalidate();
bsalomon@google.comb72f2032012-04-17 19:29:51 +0000510 // set to true to force disableScissor to make a GL call.
511 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000512 this->disableScissor();
513
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000514 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000515
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000516 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 fHWStencilClip = false;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000518
519 // TODO: I believe this should actually go in GrGpu::onResetContext
520 // rather than here
521 fClipMaskManager.resetMask();
reed@google.comac10a2d2010-12-22 21:39:39 +0000522
523 fHWGeometryState.fIndexBuffer = NULL;
524 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000525
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000526 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000527
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000528 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000529 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000530
531 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000532 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000533 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
534 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000535 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000536 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
537 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000538 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000539 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
540 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000541 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000542 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
543 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000544}
545
bsalomon@google.come269f212011-11-07 13:29:52 +0000546GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000547 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000548 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000549 return NULL;
550 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000551
bsalomon@google.com99621082011-11-15 16:47:16 +0000552 glTexDesc.fWidth = desc.fWidth;
553 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000554 glTexDesc.fConfig = desc.fConfig;
555 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
556 glTexDesc.fOwnsID = false;
557 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
558
559 GrGLTexture* texture = NULL;
560 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
561 GrGLRenderTarget::Desc glRTDesc;
562 glRTDesc.fRTFBOID = 0;
563 glRTDesc.fTexFBOID = 0;
564 glRTDesc.fMSColorRenderbufferID = 0;
565 glRTDesc.fOwnIDs = true;
566 glRTDesc.fConfig = desc.fConfig;
567 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000568 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
569 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000570 glTexDesc.fTextureID,
571 &glRTDesc)) {
572 return NULL;
573 }
574 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
575 } else {
576 texture = new GrGLTexture(this, glTexDesc);
577 }
578 if (NULL == texture) {
579 return NULL;
580 }
581
582 this->setSpareTextureUnit();
583 return texture;
584}
585
586GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
587 GrGLRenderTarget::Desc glDesc;
588 glDesc.fConfig = desc.fConfig;
589 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
590 glDesc.fMSColorRenderbufferID = 0;
591 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
592 glDesc.fSampleCnt = desc.fSampleCnt;
593 glDesc.fOwnIDs = false;
594 GrGLIRect viewport;
595 viewport.fLeft = 0;
596 viewport.fBottom = 0;
597 viewport.fWidth = desc.fWidth;
598 viewport.fHeight = desc.fHeight;
599
600 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
601 if (desc.fStencilBits) {
602 GrGLStencilBuffer::Format format;
603 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
604 format.fPacked = false;
605 format.fStencilBits = desc.fStencilBits;
606 format.fTotalBits = desc.fStencilBits;
607 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
608 0,
609 desc.fWidth,
610 desc.fHeight,
611 desc.fSampleCnt,
612 format);
613 tgt->setStencilBuffer(sb);
614 sb->unref();
615 }
616 return tgt;
617}
618
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000619////////////////////////////////////////////////////////////////////////////////
620
bsalomon@google.com6f379512011-11-16 20:36:03 +0000621void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
622 int left, int top, int width, int height,
623 GrPixelConfig config, const void* buffer,
624 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000625 if (NULL == buffer) {
626 return;
627 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000628 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
629
bsalomon@google.com6f379512011-11-16 20:36:03 +0000630 this->setSpareTextureUnit();
631 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
632 GrGLTexture::Desc desc;
633 desc.fConfig = glTex->config();
634 desc.fWidth = glTex->width();
635 desc.fHeight = glTex->height();
636 desc.fOrientation = glTex->orientation();
637 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000638
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000639 this->uploadTexData(desc, false,
640 left, top, width, height,
641 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000642}
643
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000644namespace {
645bool adjust_pixel_ops_params(int surfaceWidth,
646 int surfaceHeight,
647 size_t bpp,
648 int* left, int* top, int* width, int* height,
649 const void** data,
650 size_t* rowBytes) {
651 if (!*rowBytes) {
652 *rowBytes = *width * bpp;
653 }
654
655 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
656 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
657
658 if (!subRect.intersect(bounds)) {
659 return false;
660 }
661 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
662 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
663
664 *left = subRect.fLeft;
665 *top = subRect.fTop;
666 *width = subRect.width();
667 *height = subRect.height();
668 return true;
669}
670}
671
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000672bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
673 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000674 int left, int top, int width, int height,
675 GrPixelConfig dataConfig,
676 const void* data,
677 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000678 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000679
680 size_t bpp = GrBytesPerPixel(dataConfig);
681 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
682 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000683 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000684 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000685 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000686
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000687 // in case we need a temporary, trimmed copy of the src pixels
688 SkAutoSMalloc<128 * 128> tempStorage;
689
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000690 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000691 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000692 if (useTexStorage) {
693 if (kDesktop_GrGLBinding == this->glBinding()) {
694 // 565 is not a sized internal format on desktop GL. So on desktop
695 // with 565 we always use an unsized internal format to let the
696 // system pick the best sized format to convert the 565 data to.
697 // Since glTexStorage only allows sized internal formats we will
698 // instead fallback to glTexImage2D.
699 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
700 } else {
701 // ES doesn't allow paletted textures to be used with tex storage
702 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
703 }
704 }
705
706 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000707 GrGLenum externalFormat;
708 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000709 // glTexStorage requires sized internal formats on both desktop and ES. ES
710 // glTexImage requires an unsized format.
711 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
712 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000713 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000714 }
715
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000716 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000717 // paletted textures cannot be updated
718 return false;
719 }
720
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000721 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000722 * check whether to allocate a temporary buffer for flipping y or
723 * because our srcData has extra bytes past each row. If so, we need
724 * to trim those off here, since GL ES may not let us specify
725 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000726 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000727 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000728 bool swFlipY = false;
729 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000730 if (NULL != data) {
731 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000732 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000733 glFlipY = true;
734 } else {
735 swFlipY = true;
736 }
737 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000738 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000739 // can't use this for flipping, only non-neg values allowed. :(
740 if (rowBytes != trimRowBytes) {
741 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
742 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
743 restoreGLRowLength = true;
744 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000745 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000746 if (trimRowBytes != rowBytes || swFlipY) {
747 // copy data into our new storage, skipping the trailing bytes
748 size_t trimSize = height * trimRowBytes;
749 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000750 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000751 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000753 char* dst = (char*)tempStorage.reset(trimSize);
754 for (int y = 0; y < height; y++) {
755 memcpy(dst, src, trimRowBytes);
756 if (swFlipY) {
757 src -= rowBytes;
758 } else {
759 src += rowBytes;
760 }
761 dst += trimRowBytes;
762 }
763 // now point data to our copied version
764 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000765 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000766 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000767 if (glFlipY) {
768 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
769 }
770 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000771 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000772 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000773 if (isNewTexture &&
774 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000775 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000776 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000777 if (useTexStorage) {
778 // We never resize or change formats of textures. We don't use
779 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000780 GL_ALLOC_CALL(this->glInterface(),
781 TexStorage2D(GR_GL_TEXTURE_2D,
782 1, // levels
783 internalFormat,
784 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000785 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000786 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
787 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
788 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000789 GL_ALLOC_CALL(this->glInterface(),
790 CompressedTexImage2D(GR_GL_TEXTURE_2D,
791 0, // level
792 internalFormat,
793 desc.fWidth, desc.fHeight,
794 0, // border
795 imageSize,
796 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000797 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000798 GL_ALLOC_CALL(this->glInterface(),
799 TexImage2D(GR_GL_TEXTURE_2D,
800 0, // level
801 internalFormat,
802 desc.fWidth, desc.fHeight,
803 0, // border
804 externalFormat, externalType,
805 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000806 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000807 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000808 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000809 if (error != GR_GL_NO_ERROR) {
810 succeeded = false;
811 } else {
812 // if we have data and we used TexStorage to create the texture, we
813 // now upload with TexSubImage.
814 if (NULL != data && useTexStorage) {
815 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
816 0, // level
817 left, top,
818 width, height,
819 externalFormat, externalType,
820 data));
821 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000822 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000823 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000824 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000825 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000826 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000827 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
828 0, // level
829 left, top,
830 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000831 externalFormat, externalType, data));
832 }
833
834 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000835 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000836 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000837 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000838 if (glFlipY) {
839 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
840 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000841 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000842}
843
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000844namespace {
845bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
846 int sampleCount,
847 GrGLenum format,
848 int width, int height) {
849 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
850 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
851 bool created = false;
852 if (GrGLCaps::kNVDesktop_CoverageAAType ==
853 ctxInfo.caps().coverageAAType()) {
854 const GrGLCaps::MSAACoverageMode& mode =
855 ctxInfo.caps().getMSAACoverageMode(sampleCount);
856 GL_ALLOC_CALL(ctxInfo.interface(),
857 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
858 mode.fCoverageSampleCnt,
859 mode.fColorSampleCnt,
860 format,
861 width, height));
862 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
863 }
864 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000865 // glRBMS will fail if requested samples is > max samples.
866 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000867 GL_ALLOC_CALL(ctxInfo.interface(),
868 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
869 sampleCount,
870 format,
871 width, height));
872 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
873 }
874 return created;
875}
876}
877
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878bool GrGpuGL::createRenderTargetObjects(int width, int height,
879 GrGLuint texID,
880 GrGLRenderTarget::Desc* desc) {
881 desc->fMSColorRenderbufferID = 0;
882 desc->fRTFBOID = 0;
883 desc->fTexFBOID = 0;
884 desc->fOwnIDs = true;
885
886 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887
bsalomon@google.comab15d612011-08-09 12:57:56 +0000888 GrGLenum msColorFormat = 0; // suppress warning
889
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000890 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000891 if (!desc->fTexFBOID) {
892 goto FAILED;
893 }
894
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000895
896 // If we are using multisampling we will create two FBOS. We render
897 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000898 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000899 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000900 goto FAILED;
901 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
903 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000904 if (!desc->fRTFBOID ||
905 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000906 !this->configToGLFormats(desc->fConfig,
907 // GLES requires sized internal formats
908 kES2_GrGLBinding == this->glBinding(),
909 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000910 goto FAILED;
911 }
912 } else {
913 desc->fRTFBOID = desc->fTexFBOID;
914 }
915
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000916 // below here we may bind the FBO
917 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918 if (desc->fRTFBOID != desc->fTexFBOID) {
919 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000922 if (!renderbuffer_storage_msaa(fGLContextInfo,
923 desc->fSampleCnt,
924 msColorFormat,
925 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000926 goto FAILED;
927 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
929 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000930 GR_GL_COLOR_ATTACHMENT0,
931 GR_GL_RENDERBUFFER,
932 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000933 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000934 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
935 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
936 goto FAILED;
937 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000938 fGLContextInfo.caps().markConfigAsValidColorAttachment(
939 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000940 }
941 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000942 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000943
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000944 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
945 GR_GL_COLOR_ATTACHMENT0,
946 GR_GL_TEXTURE_2D,
947 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000948 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000949 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
950 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
951 goto FAILED;
952 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000953 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000954 }
955
956 return true;
957
958FAILED:
959 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000960 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000961 }
962 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000963 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000964 }
965 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000966 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000967 }
968 return false;
969}
970
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000971// good to set a break-point here to know when createTexture fails
972static GrTexture* return_null_texture() {
973// GrAssert(!"null texture");
974 return NULL;
975}
976
977#if GR_DEBUG
978static size_t as_size_t(int x) {
979 return x;
980}
981#endif
982
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000983GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000984 const void* srcData,
985 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000986
987#if GR_COLLECT_STATS
988 ++fStats.fTextureCreateCnt;
989#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000990
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000991 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000992 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000993
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000994 // Attempt to catch un- or wrongly initialized sample counts;
995 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
996
bsalomon@google.com99621082011-11-15 16:47:16 +0000997 glTexDesc.fWidth = desc.fWidth;
998 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000999 glTexDesc.fConfig = desc.fConfig;
1000 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001001
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001002 glRTDesc.fMSColorRenderbufferID = 0;
1003 glRTDesc.fRTFBOID = 0;
1004 glRTDesc.fTexFBOID = 0;
1005 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001006 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001007
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001008 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001010 const Caps& caps = this->getCaps();
1011
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001012 // We keep GrRenderTargets in GL's normal orientation so that they
1013 // can be drawn to by the outside world without the client having
1014 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001015 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001017
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001018 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001019 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001020 desc.fSampleCnt) {
1021 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 }
1023
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +00001025 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
1026 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001027 return return_null_texture();
1028 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
1030
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001031 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001032 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +00001033 // provides a hint about how this texture will be used
1034 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1035 GR_GL_TEXTURE_USAGE,
1036 GR_GL_FRAMEBUFFER_ATTACHMENT));
1037 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +00001038 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001039 return return_null_texture();
1040 }
1041
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001042 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001043 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001044
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001045 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1046 // drivers have a bug where an FBO won't be complete if it includes a
1047 // texture that is not mipmap complete (considering the filter in use).
1048 GrGLTexture::TexParams initialTexParams;
1049 // we only set a subset here so invalidate first
1050 initialTexParams.invalidate();
1051 initialTexParams.fFilter = GR_GL_NEAREST;
1052 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1053 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1055 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001056 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1058 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001059 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001060 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1061 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001062 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001063 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1064 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001065 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001066 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1067 glTexDesc.fWidth, glTexDesc.fHeight,
1068 desc.fConfig, srcData, rowBytes)) {
1069 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1070 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001071 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001072
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001073 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 if (renderTarget) {
1075#if GR_COLLECT_STATS
1076 ++fStats.fRenderTargetCreateCnt;
1077#endif
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001078 // unbind the texture from the texture unit before binding it to the frame buffer
1079 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1080
bsalomon@google.com99621082011-11-15 16:47:16 +00001081 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1082 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001083 glTexDesc.fTextureID,
1084 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001085 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001086 return return_null_texture();
1087 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001088 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001090 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001092 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001093#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001094 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1095 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001096#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097 return tex;
1098}
1099
1100namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001101
1102const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1103
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1105 GrGLuint rb,
1106 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001107 // we shouldn't ever know one size and not the other
1108 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1109 (kUnknownBitCount == format->fTotalBits));
1110 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1113 (GrGLint*)&format->fStencilBits);
1114 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1117 (GrGLint*)&format->fTotalBits);
1118 format->fTotalBits += format->fStencilBits;
1119 } else {
1120 format->fTotalBits = format->fStencilBits;
1121 }
1122 }
1123}
1124}
1125
1126bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1127 int width, int height) {
1128
1129 // All internally created RTs are also textures. We don't create
1130 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1131 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001132 GrAssert(width >= rt->width());
1133 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134
1135 int samples = rt->numSamples();
1136 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 if (!sbID) {
1139 return false;
1140 }
1141
1142 GrGLStencilBuffer* sb = NULL;
1143
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001144 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 // we start with the last stencil format that succeeded in hopes
1148 // that we won't go through this loop more than once after the
1149 // first (painful) stencil creation.
1150 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001151 const GrGLCaps::StencilFormat& sFmt =
1152 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001153 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001154 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001156 bool created;
1157 if (samples > 0) {
1158 created = renderbuffer_storage_msaa(fGLContextInfo,
1159 samples,
1160 sFmt.fInternalFormat,
1161 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001163 GL_ALLOC_CALL(this->glInterface(),
1164 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001165 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001166 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001167 created =
1168 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001170 if (created) {
1171 // After sized formats we attempt an unsized format and take
1172 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001173 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001175 sb = new GrGLStencilBuffer(this, sbID, width, height,
1176 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1178 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001179 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 return true;
1182 }
1183 sb->abandon(); // otherwise we lose sbID
1184 sb->unref();
1185 }
1186 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001187 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001188 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189}
1190
1191bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1192 GrRenderTarget* rt) {
1193 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1194
1195 GrGLuint fbo = glrt->renderFBOID();
1196
1197 if (NULL == sb) {
1198 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 GR_GL_STENCIL_ATTACHMENT,
1201 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 GR_GL_DEPTH_ATTACHMENT,
1204 GR_GL_RENDERBUFFER, 0));
1205#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001206 GrGLenum status;
1207 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1209#endif
1210 }
1211 return true;
1212 } else {
1213 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1214 GrGLuint rb = glsb->renderbufferID();
1215
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001216 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1218 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 GR_GL_STENCIL_ATTACHMENT,
1220 GR_GL_RENDERBUFFER, rb));
1221 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 GR_GL_DEPTH_ATTACHMENT,
1224 GR_GL_RENDERBUFFER, rb));
1225 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 GR_GL_DEPTH_ATTACHMENT,
1228 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001231 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001232 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001233 glsb->format())) {
1234 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1235 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001237 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001239 if (glsb->format().fPacked) {
1240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
1243 }
1244 return false;
1245 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001246 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001247 rt->config(),
1248 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001253}
1254
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001255////////////////////////////////////////////////////////////////////////////////
1256
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001257GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001258 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001261 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001262 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001263 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001265 GL_ALLOC_CALL(this->glInterface(),
1266 BufferData(GR_GL_ARRAY_BUFFER,
1267 size,
1268 NULL, // data ptr
1269 dynamic ? GR_GL_DYNAMIC_DRAW :
1270 GR_GL_STATIC_DRAW));
1271 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 // deleting bound buffer does implicit bind to 0
1274 fHWGeometryState.fVertexBuffer = NULL;
1275 return NULL;
1276 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001277 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 size, dynamic);
1279 fHWGeometryState.fVertexBuffer = vertexBuffer;
1280 return vertexBuffer;
1281 }
1282 return NULL;
1283}
1284
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001285GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001286 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001287 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001290 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001292 GL_ALLOC_CALL(this->glInterface(),
1293 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1294 size,
1295 NULL, // data ptr
1296 dynamic ? GR_GL_DYNAMIC_DRAW :
1297 GR_GL_STATIC_DRAW));
1298 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 // deleting bound buffer does implicit bind to 0
1301 fHWGeometryState.fIndexBuffer = NULL;
1302 return NULL;
1303 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001304 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001305 size, dynamic);
1306 fHWGeometryState.fIndexBuffer = indexBuffer;
1307 return indexBuffer;
1308 }
1309 return NULL;
1310}
1311
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001312void GrGpuGL::enableScissoring(const GrIRect& rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001313 const GrDrawState& drawState = this->getDrawState();
1314 const GrGLRenderTarget* rt =
1315 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1316
1317 GrAssert(NULL != rt);
1318 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001319
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001320 GrGLIRect scissor;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001321 scissor.setRelativeTo(vp, rect.fLeft, rect.fTop,
1322 rect.width(), rect.height());
1323 if (scissor.contains(vp)) {
1324 disableScissor();
1325 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 }
1327
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001328 if (fHWBounds.fScissorRect != scissor) {
1329 scissor.pushToGLScissor(this->glInterface());
1330 fHWBounds.fScissorRect = scissor;
1331 }
1332 if (!fHWBounds.fScissorEnabled) {
1333 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1334 fHWBounds.fScissorEnabled = true;
1335 }
1336}
1337
1338void GrGpuGL::disableScissor() {
1339 if (fHWBounds.fScissorEnabled) {
1340 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1341 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 }
1343}
1344
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001345void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001346 const GrDrawState& drawState = this->getDrawState();
1347 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001348 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001349 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001350
bsalomon@google.com74b98712011-11-11 19:46:16 +00001351 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001352 if (NULL != rect) {
1353 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001354 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001355 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001356 if (clippedRect.intersect(rtRect)) {
1357 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001358 } else {
1359 return;
1360 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001362 this->flushRenderTarget(rect);
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001363 if (NULL != rect)
1364 this->enableScissoring(*rect);
1365 else
1366 this->disableScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001367
1368 GrGLfloat r, g, b, a;
1369 static const GrGLfloat scale255 = 1.f / 255.f;
1370 a = GrColorUnpackA(color) * scale255;
1371 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001372 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001373 scaleRGB *= a;
1374 }
1375 r = GrColorUnpackR(color) * scaleRGB;
1376 g = GrColorUnpackG(color) * scaleRGB;
1377 b = GrColorUnpackB(color) * scaleRGB;
1378
1379 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001380 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001381 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001382 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001385void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001387 return;
1388 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001389
1390 this->flushRenderTarget(&GrIRect::EmptyIRect());
1391
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001392 this->disableScissor();
1393
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001394 GL_CALL(StencilMask(0xffffffff));
1395 GL_CALL(ClearStencil(0));
1396 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001397 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001398}
1399
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001400void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001401 const GrDrawState& drawState = this->getDrawState();
1402 const GrRenderTarget* rt = drawState.getRenderTarget();
1403 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404
1405 // this should only be called internally when we know we have a
1406 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001407 GrAssert(NULL != rt->getStencilBuffer());
1408 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001409#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001411 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001412#else
1413 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001414 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001415 // turned into draws. Our contract on GrDrawTarget says that
1416 // changing the clip between stencil passes may or may not
1417 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001418 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001419#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001420 GrGLint value;
1421 if (insideClip) {
1422 value = (1 << (stencilBitCount - 1));
1423 } else {
1424 value = 0;
1425 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001426 this->flushRenderTarget(&GrIRect::EmptyIRect());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001427 this->enableScissoring(rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001428 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001429 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001430 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001432}
1433
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001434void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001435 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001436}
1437
bsalomon@google.comc4364992011-11-07 15:54:49 +00001438bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1439 int left, int top,
1440 int width, int height,
1441 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001442 size_t rowBytes) const {
1443 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001444 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001445 return false;
1446 }
1447
1448 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001449 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001450 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001451 return true;
1452 }
1453 // If we have to do memcpys to handle rowBytes then y-flip is free
1454 // Note the rowBytes might be tight to the passed in data, but if data
1455 // gets clipped in x to the target the rowBytes will no longer be tight.
1456 if (left >= 0 && (left + width) < renderTarget->width()) {
1457 return 0 == rowBytes ||
1458 GrBytesPerPixel(config) * width == rowBytes;
1459 } else {
1460 return false;
1461 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001462}
1463
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001464bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001465 int left, int top,
1466 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001467 GrPixelConfig config,
1468 void* buffer,
1469 size_t rowBytes,
1470 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001471 GrGLenum format;
1472 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001473 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001474 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001476 size_t bpp = GrBytesPerPixel(config);
1477 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1478 &left, &top, &width, &height,
1479 const_cast<const void**>(&buffer),
1480 &rowBytes)) {
1481 return false;
1482 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001483
bsalomon@google.comc6980972011-11-02 19:57:21 +00001484 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001485 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001486 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001487 switch (tgt->getResolveType()) {
1488 case GrGLRenderTarget::kCantResolve_ResolveType:
1489 return false;
1490 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001491 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001492 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001493 break;
1494 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001495 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001496 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001497 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1498 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001499 break;
1500 default:
1501 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 }
1503
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001504 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001505
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001506 // the read rect is viewport-relative
1507 GrGLIRect readRect;
1508 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001509
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001510 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001511 if (0 == rowBytes) {
1512 rowBytes = tightRowBytes;
1513 }
1514 size_t readDstRowBytes = tightRowBytes;
1515 void* readDst = buffer;
1516
1517 // determine if GL can read using the passed rowBytes or if we need
1518 // a scratch buffer.
1519 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1520 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001521 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 GrAssert(!(rowBytes % sizeof(GrColor)));
1523 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1524 readDstRowBytes = rowBytes;
1525 } else {
1526 scratch.reset(tightRowBytes * height);
1527 readDst = scratch.get();
1528 }
1529 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001530 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001531 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1532 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001533 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1534 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001535 format, type, readDst));
1536 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001537 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001538 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1539 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001540 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001541 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1542 invertY = true;
1543 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001544
1545 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001546 // API presents top-to-bottom. We must preserve the padding contents. Note
1547 // that the above readPixels did not overwrite the padding.
1548 if (readDst == buffer) {
1549 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001550 if (!invertY) {
1551 scratch.reset(tightRowBytes);
1552 void* tmpRow = scratch.get();
1553 // flip y in-place by rows
1554 const int halfY = height >> 1;
1555 char* top = reinterpret_cast<char*>(buffer);
1556 char* bottom = top + (height - 1) * rowBytes;
1557 for (int y = 0; y < halfY; y++) {
1558 memcpy(tmpRow, top, tightRowBytes);
1559 memcpy(top, bottom, tightRowBytes);
1560 memcpy(bottom, tmpRow, tightRowBytes);
1561 top += rowBytes;
1562 bottom -= rowBytes;
1563 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001564 }
1565 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001566 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 // copy from readDst to buffer while flipping y
1568 const int halfY = height >> 1;
1569 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570 char* dst = reinterpret_cast<char*>(buffer);
1571 if (!invertY) {
1572 dst += (height-1) * rowBytes;
1573 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 for (int y = 0; y < height; y++) {
1575 memcpy(dst, src, tightRowBytes);
1576 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001577 if (invertY) {
1578 dst += rowBytes;
1579 } else {
1580 dst -= rowBytes;
1581 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001582 }
1583 }
1584 return true;
1585}
1586
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001587void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001588
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001589 GrGLRenderTarget* rt =
1590 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1591 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001592
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001593 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001594 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 #if GR_COLLECT_STATS
1596 ++fStats.fRenderTargetChngCnt;
1597 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001598 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001599 GrGLenum status;
1600 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001601 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001602 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 }
1604 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001605 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001606 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001607 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001608 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001609 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 fHWBounds.fViewportRect = vp;
1611 }
1612 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001613 if (NULL == bound || !bound->isEmpty()) {
1614 rt->flagAsNeedingResolve(bound);
1615 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001616}
1617
twiz@google.com0f31ca72011-03-18 17:38:11 +00001618GrGLenum gPrimitiveType2GLMode[] = {
1619 GR_GL_TRIANGLES,
1620 GR_GL_TRIANGLE_STRIP,
1621 GR_GL_TRIANGLE_FAN,
1622 GR_GL_POINTS,
1623 GR_GL_LINES,
1624 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001625};
1626
bsalomon@google.comd302f142011-03-03 13:54:13 +00001627#define SWAP_PER_DRAW 0
1628
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001629#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001630 #if GR_MAC_BUILD
1631 #include <AGL/agl.h>
1632 #elif GR_WIN32_BUILD
1633 void SwapBuf() {
1634 DWORD procID = GetCurrentProcessId();
1635 HWND hwnd = GetTopWindow(GetDesktopWindow());
1636 while(hwnd) {
1637 DWORD wndProcID = 0;
1638 GetWindowThreadProcessId(hwnd, &wndProcID);
1639 if(wndProcID == procID) {
1640 SwapBuffers(GetDC(hwnd));
1641 }
1642 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1643 }
1644 }
1645 #endif
1646#endif
1647
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001648void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1649 uint32_t startVertex,
1650 uint32_t startIndex,
1651 uint32_t vertexCount,
1652 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1654
twiz@google.com0f31ca72011-03-18 17:38:11 +00001655 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001656
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001657 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1658 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1659
1660 // our setupGeometry better have adjusted this to zero since
1661 // DrawElements always draws from the begining of the arrays for idx 0.
1662 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001663
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001664 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1665 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001666#if SWAP_PER_DRAW
1667 glFlush();
1668 #if GR_MAC_BUILD
1669 aglSwapBuffers(aglGetCurrentContext());
1670 int set_a_break_pt_here = 9;
1671 aglSwapBuffers(aglGetCurrentContext());
1672 #elif GR_WIN32_BUILD
1673 SwapBuf();
1674 int set_a_break_pt_here = 9;
1675 SwapBuf();
1676 #endif
1677#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001678}
1679
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001680void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1681 uint32_t startVertex,
1682 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1684
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001685 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1686
1687 // our setupGeometry better have adjusted this to zero.
1688 // DrawElements doesn't take an offset so we always adjus the startVertex.
1689 GrAssert(0 == startVertex);
1690
1691 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1692 // account for startVertex in the DrawElements case. So we always
1693 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001694 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001695#if SWAP_PER_DRAW
1696 glFlush();
1697 #if GR_MAC_BUILD
1698 aglSwapBuffers(aglGetCurrentContext());
1699 int set_a_break_pt_here = 9;
1700 aglSwapBuffers(aglGetCurrentContext());
1701 #elif GR_WIN32_BUILD
1702 SwapBuf();
1703 int set_a_break_pt_here = 9;
1704 SwapBuf();
1705 #endif
1706#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001707}
1708
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001709void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1710
1711 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001712
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001713 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001714 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001715 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001716 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1717 rt->renderFBOID()));
1718 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1719 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001720 #if GR_COLLECT_STATS
1721 ++fStats.fRenderTargetChngCnt;
1722 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001723 // make sure we go through flushRenderTarget() since we've modified
1724 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001725 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001726 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001727 const GrIRect dirtyRect = rt->getResolveRect();
1728 GrGLIRect r;
1729 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1730 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001731
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001732 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001733 // Apple's extension uses the scissor as the blit bounds.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001734#if 1
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001735 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1736 GL_CALL(Scissor(r.fLeft, r.fBottom,
1737 r.fWidth, r.fHeight));
1738 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001739 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 fHWBounds.fScissorEnabled = true;
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001741#else
1742 this->enableScissoring(dirtyRect);
1743 GL_CALL(ResolveMultisampleFramebuffer());
1744#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001746 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001747 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001748 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1749 this->glCaps().msFBOType());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001750 this->disableScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001751 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001752 int right = r.fLeft + r.fWidth;
1753 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001754 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1755 r.fLeft, r.fBottom, right, top,
1756 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001758 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 }
1760}
1761
twiz@google.com0f31ca72011-03-18 17:38:11 +00001762static const GrGLenum grToGLStencilFunc[] = {
1763 GR_GL_ALWAYS, // kAlways_StencilFunc
1764 GR_GL_NEVER, // kNever_StencilFunc
1765 GR_GL_GREATER, // kGreater_StencilFunc
1766 GR_GL_GEQUAL, // kGEqual_StencilFunc
1767 GR_GL_LESS, // kLess_StencilFunc
1768 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1769 GR_GL_EQUAL, // kEqual_StencilFunc,
1770 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001771};
1772GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1773GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1774GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1775GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1776GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1777GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1778GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1779GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1780GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1781
twiz@google.com0f31ca72011-03-18 17:38:11 +00001782static const GrGLenum grToGLStencilOp[] = {
1783 GR_GL_KEEP, // kKeep_StencilOp
1784 GR_GL_REPLACE, // kReplace_StencilOp
1785 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1786 GR_GL_INCR, // kIncClamp_StencilOp
1787 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1788 GR_GL_DECR, // kDecClamp_StencilOp
1789 GR_GL_ZERO, // kZero_StencilOp
1790 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791};
1792GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1793GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1794GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1795GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1796GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1797GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1798GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1799GR_STATIC_ASSERT(6 == kZero_StencilOp);
1800GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1801
reed@google.comac10a2d2010-12-22 21:39:39 +00001802void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001803 const GrDrawState& drawState = this->getDrawState();
1804
1805 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001806
1807 // use stencil for clipping if clipping is enabled and the clip
1808 // has been written into the stencil.
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001809 bool stencilClip = fClipMaskManager.isClipInStencil() && drawState.isClipState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001810 bool drawClipToStencil =
1811 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001812 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1813 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001814 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1815 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001816
1817 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001818
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001819 // we can't simultaneously perform stencil-clipping and
1820 // modify the stencil clip
1821 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001822
bsalomon@google.comd302f142011-03-03 13:54:13 +00001823 if (settings->isDisabled()) {
1824 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001825 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001826 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001828
1829 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001830 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001831 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001832 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001834 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001835 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1836 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1837 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1838 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1839 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1840 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1841 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1842 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001843 }
1844 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001845 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001846 GrStencilBuffer* stencilBuffer =
1847 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001848 if (NULL != stencilBuffer) {
1849 stencilBits = stencilBuffer->bits();
1850 }
1851 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001852 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001853
1854 GrGLuint clipStencilMask = 0;
1855 GrGLuint userStencilMask = ~0;
1856 if (stencilBits > 0) {
1857 clipStencilMask = 1 << (stencilBits - 1);
1858 userStencilMask = clipStencilMask - 1;
1859 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001860
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001861 unsigned int frontRef = settings->frontFuncRef();
1862 unsigned int frontMask = settings->frontFuncMask();
1863 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001864 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001865
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001866 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001867 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1868 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001870 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001871 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001872
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001873 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001874 stencilClip,
1875 clipStencilMask,
1876 userStencilMask,
1877 &frontRef,
1878 &frontMask);
1879 frontWriteMask &= userStencilMask;
1880 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001881 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001882 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001883 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001884 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001885 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001886 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001887 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001888 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001889 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001890 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001892 unsigned int backRef = settings->backFuncRef();
1893 unsigned int backMask = settings->backFuncMask();
1894 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001895
1896
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001897 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001898 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1899 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001900 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001901 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001902 stencilClip, settings->backFunc())];
1903 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001904 stencilClip,
1905 clipStencilMask,
1906 userStencilMask,
1907 &backRef,
1908 &backMask);
1909 backWriteMask &= userStencilMask;
1910 }
1911
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001912 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1913 frontRef, frontMask));
1914 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1915 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1916 backRef, backMask));
1917 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1918 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001919 grToGLStencilOp[settings->frontFailOp()],
1920 grToGLStencilOp[settings->frontPassOp()],
1921 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001922
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001923 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001924 grToGLStencilOp[settings->backFailOp()],
1925 grToGLStencilOp[settings->backPassOp()],
1926 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001927 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001928 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1929 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001930 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1931 grToGLStencilOp[settings->frontPassOp()],
1932 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001933 }
1934 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001935 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001936 fHWStencilClip = stencilClip;
1937 }
1938}
1939
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001941 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001942 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001943 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1944 // smooth lines.
1945
1946 // we prefer smooth lines over multisampled lines
1947 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001948 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001949 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001950 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001951 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001952 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001953 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001955 fHWAAState.fSmoothLineEnabled = false;
1956 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001957 if (rt->isMultisampled() &&
1958 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001959 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001960 fHWAAState.fMSAAEnabled = false;
1961 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001962 } else if (rt->isMultisampled() &&
1963 this->getDrawState().isHWAntialiasState() !=
1964 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001965 if (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 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001969 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001970 fHWAAState.fMSAAEnabled = true;
1971 }
1972 }
1973 }
1974}
1975
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001976void GrGpuGL::flushBlend(GrPrimitiveType type,
1977 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001978 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001979 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001981 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 fHWBlendDisabled = false;
1983 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001984 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1985 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1987 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001988 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 }
1990 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001991 // any optimization to disable blending should
1992 // have already been applied and tweaked the coeffs
1993 // to (1, 0).
1994 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1995 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996 if (fHWBlendDisabled != blendOff) {
1997 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001999 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00002001 }
2002 fHWBlendDisabled = blendOff;
2003 }
2004 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002005 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
2006 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002007 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2008 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002009 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002010 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002011 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002012 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2013 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002014 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00002015
2016 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002017 GrColorUnpackR(blendConst) / 255.f,
2018 GrColorUnpackG(blendConst) / 255.f,
2019 GrColorUnpackB(blendConst) / 255.f,
2020 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00002021 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002022 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002023 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002024 }
2025 }
2026 }
2027}
2028
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002029namespace {
2030
2031unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002032 switch (filter) {
2033 case GrSamplerState::kBilinear_Filter:
2034 case GrSamplerState::k4x4Downsample_Filter:
2035 return GR_GL_LINEAR;
2036 case GrSamplerState::kNearest_Filter:
2037 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002038 case GrSamplerState::kErode_Filter:
2039 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00002040 return GR_GL_NEAREST;
2041 default:
2042 GrAssert(!"Unknown filter type");
2043 return GR_GL_LINEAR;
2044 }
2045}
2046
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002047// get_swizzle is only called from this .cpp so it is OK to inline it here
2048inline const GrGLenum* get_swizzle(GrPixelConfig config,
2049 const GrSamplerState& sampler,
2050 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002051 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002052 if (glCaps.textureRedSupport()) {
2053 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2054 GR_GL_RED, GR_GL_RED };
2055 return gRedSmear;
2056 } else {
2057 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2058 GR_GL_ALPHA, GR_GL_ALPHA };
2059 return gAlphaSmear;
2060 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002061 } else if (sampler.swapsRAndB()) {
2062 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
2063 GR_GL_RED, GR_GL_ALPHA };
2064 return gRedBlueSwap;
2065 } else {
2066 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2067 GR_GL_BLUE, GR_GL_ALPHA };
2068 return gStraight;
2069 }
2070}
2071
2072void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
2073 // should add texparameteri to interface to make 1 instead of 4 calls here
2074 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2075 GR_GL_TEXTURE_SWIZZLE_R,
2076 swizzle[0]));
2077 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2078 GR_GL_TEXTURE_SWIZZLE_G,
2079 swizzle[1]));
2080 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_SWIZZLE_B,
2082 swizzle[2]));
2083 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
2084 GR_GL_TEXTURE_SWIZZLE_A,
2085 swizzle[3]));
2086}
2087}
2088
bsalomon@google.comffca4002011-02-22 20:34:01 +00002089bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002090
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002091 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002092 // GrGpu::setupClipAndFlushState should have already checked this
2093 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002094 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00002095
tomhudson@google.com93813632011-10-27 20:21:16 +00002096 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002097 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002098 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002099 GrGLTexture* nextTexture =
2100 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002101
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002102 // true for now, but maybe not with GrEffect.
2103 GrAssert(NULL != nextTexture);
2104 // if we created a rt/tex and rendered to it without using a
robertphillips@google.com1942c052012-05-03 17:58:27 +00002105 // texture and now we're texturing from the rt it will still be
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002106 // the last bound texture, but it needs resolving. So keep this
2107 // out of the "last != next" check.
2108 GrGLRenderTarget* texRT =
2109 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2110 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002111 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002113
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002114 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002115 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002117 #if GR_COLLECT_STATS
2118 ++fStats.fTextureChngCnt;
2119 #endif
2120 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002121 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002122 // The texture matrix has to compensate for texture width/height
2123 // and NPOT-embedded-in-POT
2124 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002125 }
2126
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002127 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002128 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002129 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002130 nextTexture->getCachedTexParams(&timestamp);
2131 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002132 GrGLTexture::TexParams newTexParams;
2133
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002134 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002135
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002136 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002137 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2138 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002139 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002140 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002141 sizeof(newTexParams.fSwizzleRGBA));
2142 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002143 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002145 GR_GL_TEXTURE_MAG_FILTER,
2146 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002147 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002148 GR_GL_TEXTURE_MIN_FILTER,
2149 newTexParams.fFilter));
2150 }
2151 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2152 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002154 GR_GL_TEXTURE_WRAP_S,
2155 newTexParams.fWrapS));
2156 }
2157 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2158 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002160 GR_GL_TEXTURE_WRAP_T,
2161 newTexParams.fWrapT));
2162 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002163 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002164 (setAll ||
2165 memcmp(newTexParams.fSwizzleRGBA,
2166 oldTexParams.fSwizzleRGBA,
2167 sizeof(newTexParams.fSwizzleRGBA)))) {
2168 setTextureUnit(s);
2169 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2170 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002171 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002172 nextTexture->setCachedTexParams(newTexParams,
2173 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002174 }
2175 }
2176
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002177 GrIRect* rect = NULL;
2178 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002179 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002180 fClip.hasConservativeBounds()) {
2181 fClip.getConservativeBounds().roundOut(&clipBounds);
2182 rect = &clipBounds;
2183 }
2184 this->flushRenderTarget(rect);
2185 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002186
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002187 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2188 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002189 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002190 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002191 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 }
2193 }
2194
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002195 if (drawState->isColorWriteDisabled() !=
2196 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002197 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002198 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002199 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002200 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002201 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002202 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002203 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002204 }
2205
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002206 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002207 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002208 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(Enable(GR_GL_CULL_FACE));
2210 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002211 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002212 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002213 GL_CALL(Enable(GR_GL_CULL_FACE));
2214 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002215 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002216 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002218 break;
2219 default:
2220 GrCrash("Unknown draw face.");
2221 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002222 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002223 }
2224
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002225#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002227 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002228 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002229 NULL == drawState->getRenderTarget() ||
2230 NULL == drawState->getTexture(s) ||
2231 drawState->getTexture(s)->asRenderTarget() !=
2232 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002233 }
2234#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002235
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002236 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002237
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002238 // This copy must happen after flushStencil() is called. flushStencil()
2239 // relies on detecting when the kModifyStencilClip_StateBit state has
2240 // changed since the last draw.
2241 fHWDrawState.copyStateFlags(*drawState);
robertphillips@google.com1942c052012-05-03 17:58:27 +00002242 // only GrInOrderDrawBuffer ever needs to ref/unref the textures
2243 fHWDrawState.disableState(GrDrawState::kTexturesNeedRef_StateBit);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002244 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002245}
2246
2247void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002248 if (fHWGeometryState.fVertexBuffer != buffer) {
2249 fHWGeometryState.fArrayPtrsDirty = true;
2250 fHWGeometryState.fVertexBuffer = buffer;
2251 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
2254void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 if (fHWGeometryState.fVertexBuffer == buffer) {
2256 // deleting bound buffer does implied bind to 0
2257 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002258 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 }
2260}
2261
2262void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002263 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264}
2265
2266void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002267 if (fHWGeometryState.fIndexBuffer == buffer) {
2268 // deleting bound buffer does implied bind to 0
2269 fHWGeometryState.fIndexBuffer = NULL;
2270 }
2271}
2272
reed@google.comac10a2d2010-12-22 21:39:39 +00002273void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2274 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002275 GrDrawState* drawState = this->drawState();
2276 if (drawState->getRenderTarget() == renderTarget) {
2277 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002279 if (fHWDrawState.getRenderTarget() == renderTarget) {
2280 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002282}
2283
2284void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002285 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002286 GrDrawState* drawState = this->drawState();
2287 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002288 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002289 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002290 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002292 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002294 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002295}
2296
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002297bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2298 bool getSizedInternalFormat,
2299 GrGLenum* internalFormat,
2300 GrGLenum* externalFormat,
2301 GrGLenum* externalType) {
2302 GrGLenum dontCare;
2303 if (NULL == internalFormat) {
2304 internalFormat = &dontCare;
2305 }
2306 if (NULL == externalFormat) {
2307 externalFormat = &dontCare;
2308 }
2309 if (NULL == externalType) {
2310 externalType = &dontCare;
2311 }
2312
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002314 case kRGBA_8888_PM_GrPixelConfig:
2315 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002316 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002317 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002318 if (getSizedInternalFormat) {
2319 *internalFormat = GR_GL_RGBA8;
2320 } else {
2321 *internalFormat = GR_GL_RGBA;
2322 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002323 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002324 break;
2325 case kBGRA_8888_PM_GrPixelConfig:
2326 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002327 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002328 return false;
2329 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002330 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002331 if (getSizedInternalFormat) {
2332 *internalFormat = GR_GL_BGRA8;
2333 } else {
2334 *internalFormat = GR_GL_BGRA;
2335 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002336 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002337 if (getSizedInternalFormat) {
2338 *internalFormat = GR_GL_RGBA8;
2339 } else {
2340 *internalFormat = GR_GL_RGBA;
2341 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002342 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002343 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002344 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002345 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002346 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002347 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002348 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002349 if (getSizedInternalFormat) {
2350 if (this->glBinding() == kDesktop_GrGLBinding) {
2351 return false;
2352 } else {
2353 *internalFormat = GR_GL_RGB565;
2354 }
2355 } else {
2356 *internalFormat = GR_GL_RGB;
2357 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002358 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002360 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002361 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002362 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002363 if (getSizedInternalFormat) {
2364 *internalFormat = GR_GL_RGBA4;
2365 } else {
2366 *internalFormat = GR_GL_RGBA;
2367 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002368 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002369 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002370 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002371 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002372 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002373 // glCompressedTexImage doesn't take external params
2374 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002375 // no sized/unsized internal format distinction here
2376 *internalFormat = GR_GL_PALETTE8_RGBA8;
2377 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002378 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002379 } else {
2380 return false;
2381 }
2382 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002383 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002384 if (this->glCaps().textureRedSupport()) {
2385 *internalFormat = GR_GL_RED;
2386 *externalFormat = GR_GL_RED;
2387 if (getSizedInternalFormat) {
2388 *internalFormat = GR_GL_R8;
2389 } else {
2390 *internalFormat = GR_GL_RED;
2391 }
2392 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002393 } else {
2394 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002395 *externalFormat = GR_GL_ALPHA;
2396 if (getSizedInternalFormat) {
2397 *internalFormat = GR_GL_ALPHA8;
2398 } else {
2399 *internalFormat = GR_GL_ALPHA;
2400 }
2401 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002402 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002403 break;
2404 default:
2405 return false;
2406 }
2407 return true;
2408}
2409
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002410void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002411 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002412 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002413 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002414 fActiveTextureUnitIdx = unit;
2415 }
2416}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002417
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002418void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002419 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002420 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002421 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2422 }
2423}
2424
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002425void GrGpuGL::resetDirtyFlags() {
2426 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2427}
2428
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002429void GrGpuGL::setBuffers(bool indexed,
2430 int* extraVertexOffset,
2431 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002432
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002433 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002434
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002435 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2436
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002437 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002438 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002439 case kBuffer_GeometrySrcType:
2440 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002441 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002442 break;
2443 case kArray_GeometrySrcType:
2444 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002445 this->finalizeReservedVertices();
2446 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2447 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002448 break;
2449 default:
2450 vbuf = NULL; // suppress warning
2451 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002452 }
2453
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002454 GrAssert(NULL != vbuf);
2455 GrAssert(!vbuf->isLocked());
2456 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002457 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002458 fHWGeometryState.fArrayPtrsDirty = true;
2459 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002460 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002461
2462 if (indexed) {
2463 GrAssert(NULL != extraIndexOffset);
2464
2465 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002466 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002467 case kBuffer_GeometrySrcType:
2468 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002469 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002470 break;
2471 case kArray_GeometrySrcType:
2472 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002473 this->finalizeReservedIndices();
2474 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2475 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002476 break;
2477 default:
2478 ibuf = NULL; // suppress warning
2479 GrCrash("Unknown geometry src type!");
2480 }
2481
2482 GrAssert(NULL != ibuf);
2483 GrAssert(!ibuf->isLocked());
2484 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002485 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002486 fHWGeometryState.fIndexBuffer = ibuf;
2487 }
2488 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002489}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002490
2491int GrGpuGL::getMaxEdges() const {
2492 // FIXME: This is a pessimistic estimate based on how many other things
2493 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002494 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002495 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002496}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002497