blob: 69880e5555aeeab3b45cab814aa83cae2e345600 [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
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000184 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000185
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000186 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000187
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000188 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000189 const GrGLubyte* ext;
190 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000191 const GrGLubyte* vendor;
192 const GrGLubyte* renderer;
193 const GrGLubyte* version;
194 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
195 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
196 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000197 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
198 this);
199 GrPrintf("------ VENDOR %s\n", vendor);
200 GrPrintf("------ RENDERER %s\n", renderer);
201 GrPrintf("------ VERSION %s\n", version);
202 GrPrintf("------ EXTENSIONS\n %s \n", ext);
203 }
204
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000205 this->resetDirtyFlags();
bsalomon@google.com316f99232011-01-13 21:28:12 +0000206
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000207 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000208
bsalomon@google.comfe676522011-06-17 18:12:21 +0000209 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000210 fCanPreserveUnpremulRoundtrip = kUnknown_CanPreserveUnpremulRoundtrip;
reed@google.comac10a2d2010-12-22 21:39:39 +0000211}
212
213GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000214 // This must be called by before the GrDrawTarget destructor
215 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000216 // This subclass must do this before the base class destructor runs
217 // since we will unref the GrGLInterface.
218 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000219}
220
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000221///////////////////////////////////////////////////////////////////////////////
222
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223void GrGpuGL::initCaps() {
224 GrGLint maxTextureUnits;
225 // check FS and fixed-function texture unit limits
226 // we only use textures in the fragment stage currently.
227 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000228 const GrGLInterface* gl = this->glInterface();
229 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000230 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000231 if (kES2_GrGLBinding != this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000232 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000233 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000235
236 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000237 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000238 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000239 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000240 for (int i = 0; i < numFormats; ++i) {
241 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
242 fCaps.f8BitPaletteSupport = true;
243 break;
244 }
245 }
246
247 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000248 // we could also look for GL_ATI_separate_stencil extension or
249 // GL_EXT_stencil_two_side but they use different function signatures
250 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000251 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000253 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 this->hasExtension("GL_EXT_stencil_wrap");
255 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000256 // ES 2 has two sided stencil and stencil wrap
257 fCaps.fTwoSidedStencilSupport = true;
258 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000259 }
260
261 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000262 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
263 // extension includes glMapBuffer.
264 } else {
265 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
266 }
267
268 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000269 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000270 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
271 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000272 } else {
273 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000274 }
275 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000276 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000277 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000278 }
279
280 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
281
282 ////////////////////////////////////////////////////////////////////////////
283 // Experiments to determine limitations that can't be queried.
284 // TODO: Make these a preprocess that generate some compile time constants.
285 // TODO: probe once at startup, rather than once per context creation.
286
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000287 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
288 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000289 // Our render targets are always created with textures as the color
290 // attachment, hence this min:
291 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
292
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000293 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000294}
295
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000296bool GrGpuGL::canPreserveReadWriteUnpremulPixels() {
297 if (kUnknown_CanPreserveUnpremulRoundtrip ==
298 fCanPreserveUnpremulRoundtrip) {
299
300 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
301 uint32_t* srcData = data.get();
302 uint32_t* firstRead = data.get() + 256 * 256;
303 uint32_t* secondRead = data.get() + 2 * 256 * 256;
304
305 for (int y = 0; y < 256; ++y) {
306 for (int x = 0; x < 256; ++x) {
307 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
308 color[3] = y;
309 color[2] = x;
310 color[1] = x;
311 color[0] = x;
312 }
313 }
314
315 // We have broader support for read/write pixels on render targets
316 // than on textures.
317 GrTextureDesc dstDesc;
318 dstDesc.fFlags = kRenderTarget_GrTextureFlagBit |
319 kNoStencil_GrTextureFlagBit;
320 dstDesc.fWidth = 256;
321 dstDesc.fHeight = 256;
322 dstDesc.fConfig = kRGBA_8888_GrPixelConfig;
323 dstDesc.fSampleCnt = 0;
324
325 SkAutoTUnref<GrTexture> dstTex(this->createTexture(dstDesc, NULL, 0));
326 if (!dstTex.get()) {
327 return false;
328 }
329 GrRenderTarget* rt = dstTex.get()->asRenderTarget();
330 GrAssert(NULL != rt);
331
332 bool failed = true;
333 static const UnpremulConversion gMethods[] = {
334 kUpOnWrite_DownOnRead_UnpremulConversion,
335 kDownOnWrite_UpOnRead_UnpremulConversion,
336 };
337
338 // pretend that we can do the roundtrip to avoid recursive calls to
339 // this function
340 fCanPreserveUnpremulRoundtrip = kYes_CanPreserveUnpremulRoundtrip;
341 for (size_t i = 0; i < GR_ARRAY_COUNT(gMethods) && failed; ++i) {
342 fUnpremulConversion = gMethods[i];
343 rt->writePixels(0, 0,
344 256, 256,
345 kRGBA_8888_UPM_GrPixelConfig, srcData, 0);
346 rt->readPixels(0, 0,
347 256, 256,
348 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
349 rt->writePixels(0, 0,
350 256, 256,
351 kRGBA_8888_UPM_GrPixelConfig, firstRead, 0);
352 rt->readPixels(0, 0,
353 256, 256,
354 kRGBA_8888_UPM_GrPixelConfig, secondRead, 0);
355 failed = false;
356 for (int j = 0; j < 256 * 256; ++j) {
357 if (firstRead[j] != secondRead[j]) {
358 failed = true;
359 break;
360 }
361 }
362 }
363 fCanPreserveUnpremulRoundtrip = failed ?
364 kNo_CanPreserveUnpremulRoundtrip :
365 kYes_CanPreserveUnpremulRoundtrip;
366 }
367
368 if (kYes_CanPreserveUnpremulRoundtrip == fCanPreserveUnpremulRoundtrip) {
369 return true;
370 } else {
371 return false;
372 }
373}
374
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000375GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000376 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
377 return GrPixelConfigSwapRAndB(config);
378 } else {
379 return config;
380 }
381}
382
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000383GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000384 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000385 return GrPixelConfigSwapRAndB(config);
386 } else {
387 return config;
388 }
389}
390
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000391bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
392 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
393}
394
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000395void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000396 if (gPrintStartupSpew && !fPrintedCaps) {
397 fPrintedCaps = true;
398 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000399 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000400 }
401
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000402 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000403 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000404 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000405
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000406 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000407 GL_CALL(Disable(GR_GL_DEPTH_TEST));
408 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000409
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000410 GL_CALL(Disable(GR_GL_CULL_FACE));
411 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000412 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000414 GL_CALL(Disable(GR_GL_DITHER));
415 if (kDesktop_GrGLBinding == this->glBinding()) {
416 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
417 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
418 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000419 fHWAAState.fMSAAEnabled = false;
420 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000421 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000423 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000424 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000425
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000427 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000428
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000429 // invalid
430 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000431
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000434
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000435 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000436 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000437
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000438 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000439
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000440 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000441
tomhudson@google.com93813632011-10-27 20:21:16 +0000442 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000443 fHWDrawState.setTexture(s, NULL);
444 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
445 -GR_ScalarMax,
446 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000447 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000448 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000449 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000450
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000451 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000453 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000454 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000456 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000458 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
460 fHWGeometryState.fIndexBuffer = NULL;
461 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000462
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000463 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000465 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000466 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000467
468 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000469 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000470 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
471 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000472 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000473 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
474 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000475 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000476 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
477 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000478 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000479 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
480 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000481}
482
bsalomon@google.come269f212011-11-07 13:29:52 +0000483GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000484 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000485 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000486 return NULL;
487 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000488
bsalomon@google.com99621082011-11-15 16:47:16 +0000489 glTexDesc.fWidth = desc.fWidth;
490 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000491 glTexDesc.fConfig = desc.fConfig;
492 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
493 glTexDesc.fOwnsID = false;
494 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
495
496 GrGLTexture* texture = NULL;
497 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
498 GrGLRenderTarget::Desc glRTDesc;
499 glRTDesc.fRTFBOID = 0;
500 glRTDesc.fTexFBOID = 0;
501 glRTDesc.fMSColorRenderbufferID = 0;
502 glRTDesc.fOwnIDs = true;
503 glRTDesc.fConfig = desc.fConfig;
504 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000505 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
506 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000507 glTexDesc.fTextureID,
508 &glRTDesc)) {
509 return NULL;
510 }
511 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
512 } else {
513 texture = new GrGLTexture(this, glTexDesc);
514 }
515 if (NULL == texture) {
516 return NULL;
517 }
518
519 this->setSpareTextureUnit();
520 return texture;
521}
522
523GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
524 GrGLRenderTarget::Desc glDesc;
525 glDesc.fConfig = desc.fConfig;
526 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
527 glDesc.fMSColorRenderbufferID = 0;
528 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
529 glDesc.fSampleCnt = desc.fSampleCnt;
530 glDesc.fOwnIDs = false;
531 GrGLIRect viewport;
532 viewport.fLeft = 0;
533 viewport.fBottom = 0;
534 viewport.fWidth = desc.fWidth;
535 viewport.fHeight = desc.fHeight;
536
537 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
538 if (desc.fStencilBits) {
539 GrGLStencilBuffer::Format format;
540 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
541 format.fPacked = false;
542 format.fStencilBits = desc.fStencilBits;
543 format.fTotalBits = desc.fStencilBits;
544 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
545 0,
546 desc.fWidth,
547 desc.fHeight,
548 desc.fSampleCnt,
549 format);
550 tgt->setStencilBuffer(sb);
551 sb->unref();
552 }
553 return tgt;
554}
555
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000556////////////////////////////////////////////////////////////////////////////////
557
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
559 int left, int top, int width, int height,
560 GrPixelConfig config, const void* buffer,
561 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000562 if (NULL == buffer) {
563 return;
564 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000565 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
566
bsalomon@google.com6f379512011-11-16 20:36:03 +0000567 this->setSpareTextureUnit();
568 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
569 GrGLTexture::Desc desc;
570 desc.fConfig = glTex->config();
571 desc.fWidth = glTex->width();
572 desc.fHeight = glTex->height();
573 desc.fOrientation = glTex->orientation();
574 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000575
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000576 this->uploadTexData(desc, false,
577 left, top, width, height,
578 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000579}
580
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000581namespace {
582bool adjust_pixel_ops_params(int surfaceWidth,
583 int surfaceHeight,
584 size_t bpp,
585 int* left, int* top, int* width, int* height,
586 const void** data,
587 size_t* rowBytes) {
588 if (!*rowBytes) {
589 *rowBytes = *width * bpp;
590 }
591
592 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
593 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
594
595 if (!subRect.intersect(bounds)) {
596 return false;
597 }
598 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
599 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
600
601 *left = subRect.fLeft;
602 *top = subRect.fTop;
603 *width = subRect.width();
604 *height = subRect.height();
605 return true;
606}
607}
608
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000609bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
610 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 int left, int top, int width, int height,
612 GrPixelConfig dataConfig,
613 const void* data,
614 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000616
617 size_t bpp = GrBytesPerPixel(dataConfig);
618 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
619 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000620 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000621 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000622 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000624 // in case we need a temporary, trimmed copy of the src pixels
625 SkAutoSMalloc<128 * 128> tempStorage;
626
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000627 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000628 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000629 if (useTexStorage) {
630 if (kDesktop_GrGLBinding == this->glBinding()) {
631 // 565 is not a sized internal format on desktop GL. So on desktop
632 // with 565 we always use an unsized internal format to let the
633 // system pick the best sized format to convert the 565 data to.
634 // Since glTexStorage only allows sized internal formats we will
635 // instead fallback to glTexImage2D.
636 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
637 } else {
638 // ES doesn't allow paletted textures to be used with tex storage
639 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
640 }
641 }
642
643 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000644 GrGLenum externalFormat;
645 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000646 // glTexStorage requires sized internal formats on both desktop and ES. ES
647 // glTexImage requires an unsized format.
648 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
649 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000650 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000651 }
652
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000653 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000654 // paletted textures cannot be updated
655 return false;
656 }
657
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000658 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000659 * check whether to allocate a temporary buffer for flipping y or
660 * because our srcData has extra bytes past each row. If so, we need
661 * to trim those off here, since GL ES may not let us specify
662 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000663 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000664 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000665 bool swFlipY = false;
666 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000667 if (NULL != data) {
668 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000669 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000670 glFlipY = true;
671 } else {
672 swFlipY = true;
673 }
674 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000675 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000676 // can't use this for flipping, only non-neg values allowed. :(
677 if (rowBytes != trimRowBytes) {
678 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
679 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
680 restoreGLRowLength = true;
681 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000682 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000683 if (trimRowBytes != rowBytes || swFlipY) {
684 // copy data into our new storage, skipping the trailing bytes
685 size_t trimSize = height * trimRowBytes;
686 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000687 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000688 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000689 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000690 char* dst = (char*)tempStorage.reset(trimSize);
691 for (int y = 0; y < height; y++) {
692 memcpy(dst, src, trimRowBytes);
693 if (swFlipY) {
694 src -= rowBytes;
695 } else {
696 src += rowBytes;
697 }
698 dst += trimRowBytes;
699 }
700 // now point data to our copied version
701 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000702 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000703 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000704 if (glFlipY) {
705 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
706 }
707 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000708 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000709 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000710 if (isNewTexture &&
711 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000712 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000713 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000714 if (useTexStorage) {
715 // We never resize or change formats of textures. We don't use
716 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000717 GL_ALLOC_CALL(this->glInterface(),
718 TexStorage2D(GR_GL_TEXTURE_2D,
719 1, // levels
720 internalFormat,
721 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000722 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000723 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
724 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
725 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000726 GL_ALLOC_CALL(this->glInterface(),
727 CompressedTexImage2D(GR_GL_TEXTURE_2D,
728 0, // level
729 internalFormat,
730 desc.fWidth, desc.fHeight,
731 0, // border
732 imageSize,
733 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000734 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000735 GL_ALLOC_CALL(this->glInterface(),
736 TexImage2D(GR_GL_TEXTURE_2D,
737 0, // level
738 internalFormat,
739 desc.fWidth, desc.fHeight,
740 0, // border
741 externalFormat, externalType,
742 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000743 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000744 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000745 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000746 if (error != GR_GL_NO_ERROR) {
747 succeeded = false;
748 } else {
749 // if we have data and we used TexStorage to create the texture, we
750 // now upload with TexSubImage.
751 if (NULL != data && useTexStorage) {
752 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
753 0, // level
754 left, top,
755 width, height,
756 externalFormat, externalType,
757 data));
758 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000759 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000761 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000762 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000763 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000764 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
765 0, // level
766 left, top,
767 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000768 externalFormat, externalType, data));
769 }
770
771 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000772 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000773 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000774 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000775 if (glFlipY) {
776 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
777 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000778 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000779}
780
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000781bool GrGpuGL::createRenderTargetObjects(int width, int height,
782 GrGLuint texID,
783 GrGLRenderTarget::Desc* desc) {
784 desc->fMSColorRenderbufferID = 0;
785 desc->fRTFBOID = 0;
786 desc->fTexFBOID = 0;
787 desc->fOwnIDs = true;
788
789 GrGLenum status;
790 GrGLint err;
791
bsalomon@google.comab15d612011-08-09 12:57:56 +0000792 GrGLenum msColorFormat = 0; // suppress warning
793
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000794 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000795 if (!desc->fTexFBOID) {
796 goto FAILED;
797 }
798
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000799
800 // If we are using multisampling we will create two FBOS. We render
801 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000802 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000803 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000804 goto FAILED;
805 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000806 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
807 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000808 if (!desc->fRTFBOID ||
809 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000810 !this->configToGLFormats(desc->fConfig,
811 // GLES requires sized internal formats
812 kES2_GrGLBinding == this->glBinding(),
813 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814 goto FAILED;
815 }
816 } else {
817 desc->fRTFBOID = desc->fTexFBOID;
818 }
819
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000820 // below here we may bind the FBO
821 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000822 if (desc->fRTFBOID != desc->fTexFBOID) {
823 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000826 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
827 GL_ALLOC_CALL(this->glInterface(),
828 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
829 desc->fSampleCnt,
830 msColorFormat,
831 width, height));
832 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000833 if (err != GR_GL_NO_ERROR) {
834 goto FAILED;
835 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000836 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
837 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 GR_GL_COLOR_ATTACHMENT0,
839 GR_GL_RENDERBUFFER,
840 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000841 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000842 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
843 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
844 goto FAILED;
845 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000846 fGLContextInfo.caps().markConfigAsValidColorAttachment(
847 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000848 }
849 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000850 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000852 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
853 GR_GL_COLOR_ATTACHMENT0,
854 GR_GL_TEXTURE_2D,
855 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000856 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000857 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
858 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
859 goto FAILED;
860 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000861 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000862 }
863
864 return true;
865
866FAILED:
867 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000868 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000869 }
870 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000872 }
873 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000875 }
876 return false;
877}
878
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000879// good to set a break-point here to know when createTexture fails
880static GrTexture* return_null_texture() {
881// GrAssert(!"null texture");
882 return NULL;
883}
884
885#if GR_DEBUG
886static size_t as_size_t(int x) {
887 return x;
888}
889#endif
890
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000891GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000892 const void* srcData,
893 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000894
895#if GR_COLLECT_STATS
896 ++fStats.fTextureCreateCnt;
897#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000898
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000899 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000900 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000901
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000902 // Attempt to catch un- or wrongly initialized sample counts;
903 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
904
bsalomon@google.com99621082011-11-15 16:47:16 +0000905 glTexDesc.fWidth = desc.fWidth;
906 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000907 glTexDesc.fConfig = desc.fConfig;
908 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000909
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000910 glRTDesc.fMSColorRenderbufferID = 0;
911 glRTDesc.fRTFBOID = 0;
912 glRTDesc.fTexFBOID = 0;
913 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000914 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000915
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000916 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000917
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000918 const Caps& caps = this->getCaps();
919
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000920 // We keep GrRenderTargets in GL's normal orientation so that they
921 // can be drawn to by the outside world without the client having
922 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000923 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000925
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000926 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000927 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000928 desc.fSampleCnt) {
929 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +0000930 }
931
reed@google.comac10a2d2010-12-22 21:39:39 +0000932 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000933 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
934 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000935 return return_null_texture();
936 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000937 }
938
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000939 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000940 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000941 // provides a hint about how this texture will be used
942 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
943 GR_GL_TEXTURE_USAGE,
944 GR_GL_FRAMEBUFFER_ATTACHMENT));
945 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000946 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000947 return return_null_texture();
948 }
949
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000950 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000951 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000952
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000953 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
954 // drivers have a bug where an FBO won't be complete if it includes a
955 // texture that is not mipmap complete (considering the filter in use).
956 GrGLTexture::TexParams initialTexParams;
957 // we only set a subset here so invalidate first
958 initialTexParams.invalidate();
959 initialTexParams.fFilter = GR_GL_NEAREST;
960 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
961 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
963 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000964 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
966 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000967 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000968 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
969 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000970 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000971 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
972 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000973 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000974 if (!this->uploadTexData(glTexDesc, true, 0, 0,
975 glTexDesc.fWidth, glTexDesc.fHeight,
976 desc.fConfig, srcData, rowBytes)) {
977 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
978 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000979 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000980
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000981 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 if (renderTarget) {
983#if GR_COLLECT_STATS
984 ++fStats.fRenderTargetCreateCnt;
985#endif
bsalomon@google.com99621082011-11-15 16:47:16 +0000986 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
987 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000988 glTexDesc.fTextureID,
989 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000991 return return_null_texture();
992 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000993 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000994 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000995 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000996 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000997 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000998#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000999 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1000 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001001#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 return tex;
1003}
1004
1005namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001006
1007const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1008
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1010 GrGLuint rb,
1011 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001012 // we shouldn't ever know one size and not the other
1013 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1014 (kUnknownBitCount == format->fTotalBits));
1015 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001017 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1018 (GrGLint*)&format->fStencilBits);
1019 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001021 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1022 (GrGLint*)&format->fTotalBits);
1023 format->fTotalBits += format->fStencilBits;
1024 } else {
1025 format->fTotalBits = format->fStencilBits;
1026 }
1027 }
1028}
1029}
1030
1031bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1032 int width, int height) {
1033
1034 // All internally created RTs are also textures. We don't create
1035 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1036 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001037 GrAssert(width >= rt->width());
1038 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001039
1040 int samples = rt->numSamples();
1041 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043 if (!sbID) {
1044 return false;
1045 }
1046
1047 GrGLStencilBuffer* sb = NULL;
1048
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001049 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001050 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001052 // we start with the last stencil format that succeeded in hopes
1053 // that we won't go through this loop more than once after the
1054 // first (painful) stencil creation.
1055 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001056 const GrGLCaps::StencilFormat& sFmt =
1057 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001058 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001059 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 // version on a GL that doesn't have an MSAA extension.
1061 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001062 GL_ALLOC_CALL(this->glInterface(),
1063 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1064 samples,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001065 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001066 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001068 GL_ALLOC_CALL(this->glInterface(),
1069 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001070 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001071 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001072 }
1073
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001074 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 if (err == GR_GL_NO_ERROR) {
1076 // After sized formats we attempt an unsized format and take whatever
1077 // sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001078 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001080 sb = new GrGLStencilBuffer(this, sbID, width, height,
1081 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1083 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001084 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001085 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001086 return true;
1087 }
1088 sb->abandon(); // otherwise we lose sbID
1089 sb->unref();
1090 }
1091 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001092 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001093 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001094}
1095
1096bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1097 GrRenderTarget* rt) {
1098 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1099
1100 GrGLuint fbo = glrt->renderFBOID();
1101
1102 if (NULL == sb) {
1103 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 GR_GL_STENCIL_ATTACHMENT,
1106 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 GR_GL_DEPTH_ATTACHMENT,
1109 GR_GL_RENDERBUFFER, 0));
1110#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001111 GrGLenum status;
1112 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1114#endif
1115 }
1116 return true;
1117 } else {
1118 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1119 GrGLuint rb = glsb->renderbufferID();
1120
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001121 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1123 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001124 GR_GL_STENCIL_ATTACHMENT,
1125 GR_GL_RENDERBUFFER, rb));
1126 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 GR_GL_DEPTH_ATTACHMENT,
1129 GR_GL_RENDERBUFFER, rb));
1130 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132 GR_GL_DEPTH_ATTACHMENT,
1133 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001136 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001137 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001138 glsb->format())) {
1139 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1140 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001142 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001144 if (glsb->format().fPacked) {
1145 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1146 GR_GL_DEPTH_ATTACHMENT,
1147 GR_GL_RENDERBUFFER, 0));
1148 }
1149 return false;
1150 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001151 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001152 rt->config(),
1153 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001156 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001158}
1159
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001160////////////////////////////////////////////////////////////////////////////////
1161
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001162GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001163 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001166 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001167 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001168 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001170 GL_ALLOC_CALL(this->glInterface(),
1171 BufferData(GR_GL_ARRAY_BUFFER,
1172 size,
1173 NULL, // data ptr
1174 dynamic ? GR_GL_DYNAMIC_DRAW :
1175 GR_GL_STATIC_DRAW));
1176 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 // deleting bound buffer does implicit bind to 0
1179 fHWGeometryState.fVertexBuffer = NULL;
1180 return NULL;
1181 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001182 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 size, dynamic);
1184 fHWGeometryState.fVertexBuffer = vertexBuffer;
1185 return vertexBuffer;
1186 }
1187 return NULL;
1188}
1189
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001190GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001191 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001195 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001197 GL_ALLOC_CALL(this->glInterface(),
1198 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1199 size,
1200 NULL, // data ptr
1201 dynamic ? GR_GL_DYNAMIC_DRAW :
1202 GR_GL_STATIC_DRAW));
1203 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001204 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 // deleting bound buffer does implicit bind to 0
1206 fHWGeometryState.fIndexBuffer = NULL;
1207 return NULL;
1208 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001209 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 size, dynamic);
1211 fHWGeometryState.fIndexBuffer = indexBuffer;
1212 return indexBuffer;
1213 }
1214 return NULL;
1215}
1216
reed@google.comac10a2d2010-12-22 21:39:39 +00001217void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001218 const GrDrawState& drawState = this->getDrawState();
1219 const GrGLRenderTarget* rt =
1220 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1221
1222 GrAssert(NULL != rt);
1223 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001224
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001225 GrGLIRect scissor;
1226 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001227 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001228 rect->width(), rect->height());
1229 if (scissor.contains(vp)) {
1230 rect = NULL;
1231 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 }
1233
1234 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001237 fHWBounds.fScissorRect = scissor;
1238 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 fHWBounds.fScissorEnabled = true;
1242 }
1243 } else {
1244 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001245 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 fHWBounds.fScissorEnabled = false;
1247 }
1248 }
1249}
1250
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001251void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001252 const GrDrawState& drawState = this->getDrawState();
1253 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001254 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001255 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001256
bsalomon@google.com74b98712011-11-11 19:46:16 +00001257 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001258 if (NULL != rect) {
1259 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001260 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001261 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001262 if (clippedRect.intersect(rtRect)) {
1263 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001264 } else {
1265 return;
1266 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001268 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001269 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001270
1271 GrGLfloat r, g, b, a;
1272 static const GrGLfloat scale255 = 1.f / 255.f;
1273 a = GrColorUnpackA(color) * scale255;
1274 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001275 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001276 scaleRGB *= a;
1277 }
1278 r = GrColorUnpackR(color) * scaleRGB;
1279 g = GrColorUnpackG(color) * scaleRGB;
1280 b = GrColorUnpackB(color) * scaleRGB;
1281
1282 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001283 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001284 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001285 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001286}
1287
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001288void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001289 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001290 return;
1291 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001292
1293 this->flushRenderTarget(&GrIRect::EmptyIRect());
1294
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001297 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(StencilMask(0xffffffff));
1300 GL_CALL(ClearStencil(0));
1301 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001302 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001303}
1304
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001305void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001306 const GrDrawState& drawState = this->getDrawState();
1307 const GrRenderTarget* rt = drawState.getRenderTarget();
1308 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001309
1310 // this should only be called internally when we know we have a
1311 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001312 GrAssert(NULL != rt->getStencilBuffer());
1313 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001314#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001316 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001317#else
1318 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001319 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001320 // turned into draws. Our contract on GrDrawTarget says that
1321 // changing the clip between stencil passes may or may not
1322 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001323 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001324#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001325 GrGLint value;
1326 if (insideClip) {
1327 value = (1 << (stencilBitCount - 1));
1328 } else {
1329 value = 0;
1330 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001331 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001332 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001333 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001334 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001335 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001336 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001337}
1338
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001339void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001340 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001341}
1342
bsalomon@google.comc4364992011-11-07 15:54:49 +00001343bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1344 int left, int top,
1345 int width, int height,
1346 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001347 size_t rowBytes) const {
1348 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001349 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001350 return false;
1351 }
1352
1353 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001354 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001355 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001356 return true;
1357 }
1358 // If we have to do memcpys to handle rowBytes then y-flip is free
1359 // Note the rowBytes might be tight to the passed in data, but if data
1360 // gets clipped in x to the target the rowBytes will no longer be tight.
1361 if (left >= 0 && (left + width) < renderTarget->width()) {
1362 return 0 == rowBytes ||
1363 GrBytesPerPixel(config) * width == rowBytes;
1364 } else {
1365 return false;
1366 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001367}
1368
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001369bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001370 int left, int top,
1371 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001372 GrPixelConfig config,
1373 void* buffer,
1374 size_t rowBytes,
1375 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001376 GrGLenum format;
1377 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001378 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001380 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001381 size_t bpp = GrBytesPerPixel(config);
1382 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1383 &left, &top, &width, &height,
1384 const_cast<const void**>(&buffer),
1385 &rowBytes)) {
1386 return false;
1387 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001388
bsalomon@google.comc6980972011-11-02 19:57:21 +00001389 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001390 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001391 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001392 switch (tgt->getResolveType()) {
1393 case GrGLRenderTarget::kCantResolve_ResolveType:
1394 return false;
1395 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001397 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001398 break;
1399 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001400 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001401 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001402 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1403 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001404 break;
1405 default:
1406 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001407 }
1408
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001409 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001410
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001411 // the read rect is viewport-relative
1412 GrGLIRect readRect;
1413 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001414
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001415 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001416 if (0 == rowBytes) {
1417 rowBytes = tightRowBytes;
1418 }
1419 size_t readDstRowBytes = tightRowBytes;
1420 void* readDst = buffer;
1421
1422 // determine if GL can read using the passed rowBytes or if we need
1423 // a scratch buffer.
1424 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1425 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001426 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001427 GrAssert(!(rowBytes % sizeof(GrColor)));
1428 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1429 readDstRowBytes = rowBytes;
1430 } else {
1431 scratch.reset(tightRowBytes * height);
1432 readDst = scratch.get();
1433 }
1434 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001435 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001436 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1437 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001438 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1439 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001440 format, type, readDst));
1441 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001442 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001443 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1444 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001445 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001446 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1447 invertY = true;
1448 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001449
1450 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001451 // API presents top-to-bottom. We must preserve the padding contents. Note
1452 // that the above readPixels did not overwrite the padding.
1453 if (readDst == buffer) {
1454 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001455 if (!invertY) {
1456 scratch.reset(tightRowBytes);
1457 void* tmpRow = scratch.get();
1458 // flip y in-place by rows
1459 const int halfY = height >> 1;
1460 char* top = reinterpret_cast<char*>(buffer);
1461 char* bottom = top + (height - 1) * rowBytes;
1462 for (int y = 0; y < halfY; y++) {
1463 memcpy(tmpRow, top, tightRowBytes);
1464 memcpy(top, bottom, tightRowBytes);
1465 memcpy(bottom, tmpRow, tightRowBytes);
1466 top += rowBytes;
1467 bottom -= rowBytes;
1468 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 }
1470 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001471 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001472 // copy from readDst to buffer while flipping y
1473 const int halfY = height >> 1;
1474 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001475 char* dst = reinterpret_cast<char*>(buffer);
1476 if (!invertY) {
1477 dst += (height-1) * rowBytes;
1478 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001479 for (int y = 0; y < height; y++) {
1480 memcpy(dst, src, tightRowBytes);
1481 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001482 if (invertY) {
1483 dst += rowBytes;
1484 } else {
1485 dst -= rowBytes;
1486 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 }
1488 }
1489 return true;
1490}
1491
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001492void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001493
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001494 GrGLRenderTarget* rt =
1495 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1496 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001497
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001498 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001499 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 #if GR_COLLECT_STATS
1501 ++fStats.fRenderTargetChngCnt;
1502 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001503 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001504 GrGLenum status;
1505 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001506 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001507 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 }
1509 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001510 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001511 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001512 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001513 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001514 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 fHWBounds.fViewportRect = vp;
1516 }
1517 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001518 if (NULL == bound || !bound->isEmpty()) {
1519 rt->flagAsNeedingResolve(bound);
1520 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001521}
1522
twiz@google.com0f31ca72011-03-18 17:38:11 +00001523GrGLenum gPrimitiveType2GLMode[] = {
1524 GR_GL_TRIANGLES,
1525 GR_GL_TRIANGLE_STRIP,
1526 GR_GL_TRIANGLE_FAN,
1527 GR_GL_POINTS,
1528 GR_GL_LINES,
1529 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001530};
1531
bsalomon@google.comd302f142011-03-03 13:54:13 +00001532#define SWAP_PER_DRAW 0
1533
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001534#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001535 #if GR_MAC_BUILD
1536 #include <AGL/agl.h>
1537 #elif GR_WIN32_BUILD
1538 void SwapBuf() {
1539 DWORD procID = GetCurrentProcessId();
1540 HWND hwnd = GetTopWindow(GetDesktopWindow());
1541 while(hwnd) {
1542 DWORD wndProcID = 0;
1543 GetWindowThreadProcessId(hwnd, &wndProcID);
1544 if(wndProcID == procID) {
1545 SwapBuffers(GetDC(hwnd));
1546 }
1547 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1548 }
1549 }
1550 #endif
1551#endif
1552
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001553void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1554 uint32_t startVertex,
1555 uint32_t startIndex,
1556 uint32_t vertexCount,
1557 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1559
twiz@google.com0f31ca72011-03-18 17:38:11 +00001560 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001561
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001562 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1563 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1564
1565 // our setupGeometry better have adjusted this to zero since
1566 // DrawElements always draws from the begining of the arrays for idx 0.
1567 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001568
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001569 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1570 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571#if SWAP_PER_DRAW
1572 glFlush();
1573 #if GR_MAC_BUILD
1574 aglSwapBuffers(aglGetCurrentContext());
1575 int set_a_break_pt_here = 9;
1576 aglSwapBuffers(aglGetCurrentContext());
1577 #elif GR_WIN32_BUILD
1578 SwapBuf();
1579 int set_a_break_pt_here = 9;
1580 SwapBuf();
1581 #endif
1582#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001583}
1584
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001585void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1586 uint32_t startVertex,
1587 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1589
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001590 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1591
1592 // our setupGeometry better have adjusted this to zero.
1593 // DrawElements doesn't take an offset so we always adjus the startVertex.
1594 GrAssert(0 == startVertex);
1595
1596 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1597 // account for startVertex in the DrawElements case. So we always
1598 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001599 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001600#if SWAP_PER_DRAW
1601 glFlush();
1602 #if GR_MAC_BUILD
1603 aglSwapBuffers(aglGetCurrentContext());
1604 int set_a_break_pt_here = 9;
1605 aglSwapBuffers(aglGetCurrentContext());
1606 #elif GR_WIN32_BUILD
1607 SwapBuf();
1608 int set_a_break_pt_here = 9;
1609 SwapBuf();
1610 #endif
1611#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001612}
1613
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001614void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1615
1616 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001617
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001618 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001619 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001621 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1622 rt->renderFBOID()));
1623 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1624 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 #if GR_COLLECT_STATS
1626 ++fStats.fRenderTargetChngCnt;
1627 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001628 // make sure we go through flushRenderTarget() since we've modified
1629 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001630 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001631 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001632 const GrIRect dirtyRect = rt->getResolveRect();
1633 GrGLIRect r;
1634 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1635 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001636
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001637 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001638 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001639 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1640 GL_CALL(Scissor(r.fLeft, r.fBottom,
1641 r.fWidth, r.fHeight));
1642 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001643 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 fHWBounds.fScissorEnabled = true;
1645 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001646 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001647 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001648 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1649 this->glCaps().msFBOType());
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001650 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001651 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001652 int right = r.fLeft + r.fWidth;
1653 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001654 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1655 r.fLeft, r.fBottom, right, top,
1656 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001658 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 }
1660}
1661
twiz@google.com0f31ca72011-03-18 17:38:11 +00001662static const GrGLenum grToGLStencilFunc[] = {
1663 GR_GL_ALWAYS, // kAlways_StencilFunc
1664 GR_GL_NEVER, // kNever_StencilFunc
1665 GR_GL_GREATER, // kGreater_StencilFunc
1666 GR_GL_GEQUAL, // kGEqual_StencilFunc
1667 GR_GL_LESS, // kLess_StencilFunc
1668 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1669 GR_GL_EQUAL, // kEqual_StencilFunc,
1670 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001671};
1672GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1673GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1674GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1675GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1676GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1677GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1678GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1679GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1680GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1681
twiz@google.com0f31ca72011-03-18 17:38:11 +00001682static const GrGLenum grToGLStencilOp[] = {
1683 GR_GL_KEEP, // kKeep_StencilOp
1684 GR_GL_REPLACE, // kReplace_StencilOp
1685 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1686 GR_GL_INCR, // kIncClamp_StencilOp
1687 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1688 GR_GL_DECR, // kDecClamp_StencilOp
1689 GR_GL_ZERO, // kZero_StencilOp
1690 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001691};
1692GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1693GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1694GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1695GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1696GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1697GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1698GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1699GR_STATIC_ASSERT(6 == kZero_StencilOp);
1700GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1701
reed@google.comac10a2d2010-12-22 21:39:39 +00001702void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001703 const GrDrawState& drawState = this->getDrawState();
1704
1705 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001706
1707 // use stencil for clipping if clipping is enabled and the clip
1708 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001709 bool stencilClip = fClipInStencil && drawState.isClipState();
1710 bool drawClipToStencil =
1711 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001712 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1713 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001714 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1715 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001716
1717 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001718
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001719 // we can't simultaneously perform stencil-clipping and
1720 // modify the stencil clip
1721 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001722
bsalomon@google.comd302f142011-03-03 13:54:13 +00001723 if (settings->isDisabled()) {
1724 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001725 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001726 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001728
1729 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001730 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001732 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001733 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001734 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001735 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1736 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1737 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1738 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1739 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1740 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1741 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1742 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001743 }
1744 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001745 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001746 GrStencilBuffer* stencilBuffer =
1747 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001748 if (NULL != stencilBuffer) {
1749 stencilBits = stencilBuffer->bits();
1750 }
1751 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001752 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001753
1754 GrGLuint clipStencilMask = 0;
1755 GrGLuint userStencilMask = ~0;
1756 if (stencilBits > 0) {
1757 clipStencilMask = 1 << (stencilBits - 1);
1758 userStencilMask = clipStencilMask - 1;
1759 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001761 unsigned int frontRef = settings->frontFuncRef();
1762 unsigned int frontMask = settings->frontFuncMask();
1763 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001764 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001765
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001766 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001767 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1768 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001769 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001770 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001771 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001772
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001773 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001774 stencilClip,
1775 clipStencilMask,
1776 userStencilMask,
1777 &frontRef,
1778 &frontMask);
1779 frontWriteMask &= userStencilMask;
1780 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001781 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001782 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001783 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001784 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001785 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001786 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001787 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001788 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001789 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001790 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001792 unsigned int backRef = settings->backFuncRef();
1793 unsigned int backMask = settings->backFuncMask();
1794 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001795
1796
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001797 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001798 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1799 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001800 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001801 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001802 stencilClip, settings->backFunc())];
1803 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001804 stencilClip,
1805 clipStencilMask,
1806 userStencilMask,
1807 &backRef,
1808 &backMask);
1809 backWriteMask &= userStencilMask;
1810 }
1811
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001812 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1813 frontRef, frontMask));
1814 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1815 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1816 backRef, backMask));
1817 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1818 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001819 grToGLStencilOp[settings->frontFailOp()],
1820 grToGLStencilOp[settings->frontPassOp()],
1821 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001823 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001824 grToGLStencilOp[settings->backFailOp()],
1825 grToGLStencilOp[settings->backPassOp()],
1826 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001827 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001828 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1829 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001830 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1831 grToGLStencilOp[settings->frontPassOp()],
1832 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001833 }
1834 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001835 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001836 fHWStencilClip = stencilClip;
1837 }
1838}
1839
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001840void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001841 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001842 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001843 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1844 // smooth lines.
1845
1846 // we prefer smooth lines over multisampled lines
1847 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001848 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001849 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001850 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001851 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001852 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001853 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001854 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001855 fHWAAState.fSmoothLineEnabled = false;
1856 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001857 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001858 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001859 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001860 fHWAAState.fMSAAEnabled = false;
1861 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001862 } else if (rt->isMultisampled() &&
1863 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001864 fHWAAState.fMSAAEnabled) {
1865 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001866 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001867 fHWAAState.fMSAAEnabled = false;
1868 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001869 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001870 fHWAAState.fMSAAEnabled = true;
1871 }
1872 }
1873 }
1874}
1875
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001876void GrGpuGL::flushBlend(GrPrimitiveType type,
1877 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001878 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001879 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001880 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001882 fHWBlendDisabled = false;
1883 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001884 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1885 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001886 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1887 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001888 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001889 }
1890 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001891 // any optimization to disable blending should
1892 // have already been applied and tweaked the coeffs
1893 // to (1, 0).
1894 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1895 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001896 if (fHWBlendDisabled != blendOff) {
1897 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001898 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001899 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001900 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001901 }
1902 fHWBlendDisabled = blendOff;
1903 }
1904 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001905 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1906 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001907 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1908 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001909 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001910 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001911 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001912 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1913 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001914 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001915
1916 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001917 GrColorUnpackR(blendConst) / 255.f,
1918 GrColorUnpackG(blendConst) / 255.f,
1919 GrColorUnpackB(blendConst) / 255.f,
1920 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001921 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001922 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001923 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001924 }
1925 }
1926 }
1927}
1928
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001929namespace {
1930
1931unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001932 switch (filter) {
1933 case GrSamplerState::kBilinear_Filter:
1934 case GrSamplerState::k4x4Downsample_Filter:
1935 return GR_GL_LINEAR;
1936 case GrSamplerState::kNearest_Filter:
1937 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001938 case GrSamplerState::kErode_Filter:
1939 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001940 return GR_GL_NEAREST;
1941 default:
1942 GrAssert(!"Unknown filter type");
1943 return GR_GL_LINEAR;
1944 }
1945}
1946
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001947const GrGLenum* get_swizzle(GrPixelConfig config,
1948 const GrSamplerState& sampler) {
1949 if (GrPixelConfigIsAlphaOnly(config)) {
1950 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
1951 GR_GL_ALPHA, GR_GL_ALPHA };
1952 return gAlphaSmear;
1953 } else if (sampler.swapsRAndB()) {
1954 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
1955 GR_GL_RED, GR_GL_ALPHA };
1956 return gRedBlueSwap;
1957 } else {
1958 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
1959 GR_GL_BLUE, GR_GL_ALPHA };
1960 return gStraight;
1961 }
1962}
1963
1964void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
1965 // should add texparameteri to interface to make 1 instead of 4 calls here
1966 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1967 GR_GL_TEXTURE_SWIZZLE_R,
1968 swizzle[0]));
1969 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1970 GR_GL_TEXTURE_SWIZZLE_G,
1971 swizzle[1]));
1972 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1973 GR_GL_TEXTURE_SWIZZLE_B,
1974 swizzle[2]));
1975 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1976 GR_GL_TEXTURE_SWIZZLE_A,
1977 swizzle[3]));
1978}
1979}
1980
bsalomon@google.comffca4002011-02-22 20:34:01 +00001981bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001982
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001983 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001984 // GrGpu::setupClipAndFlushState should have already checked this
1985 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001986 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00001987
tomhudson@google.com93813632011-10-27 20:21:16 +00001988 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001989 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001990 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001991 GrGLTexture* nextTexture =
1992 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00001993
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001994 // true for now, but maybe not with GrEffect.
1995 GrAssert(NULL != nextTexture);
1996 // if we created a rt/tex and rendered to it without using a
1997 // texture and now we're texuring from the rt it will still be
1998 // the last bound texture, but it needs resolving. So keep this
1999 // out of the "last != next" check.
2000 GrGLRenderTarget* texRT =
2001 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2002 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002003 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002004 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002005
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002006 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002007 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002008 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002009 #if GR_COLLECT_STATS
2010 ++fStats.fTextureChngCnt;
2011 #endif
2012 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002013 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002014 // The texture matrix has to compensate for texture width/height
2015 // and NPOT-embedded-in-POT
2016 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002017 }
2018
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002019 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002020 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002021 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002022 nextTexture->getCachedTexParams(&timestamp);
2023 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002024 GrGLTexture::TexParams newTexParams;
2025
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002026 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002027
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002028 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002029 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2030 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002031 memcpy(newTexParams.fSwizzleRGBA,
2032 get_swizzle(nextTexture->config(), sampler),
2033 sizeof(newTexParams.fSwizzleRGBA));
2034 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002035 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002036 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002037 GR_GL_TEXTURE_MAG_FILTER,
2038 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002039 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002040 GR_GL_TEXTURE_MIN_FILTER,
2041 newTexParams.fFilter));
2042 }
2043 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2044 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002046 GR_GL_TEXTURE_WRAP_S,
2047 newTexParams.fWrapS));
2048 }
2049 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2050 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002052 GR_GL_TEXTURE_WRAP_T,
2053 newTexParams.fWrapT));
2054 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002055 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002056 (setAll ||
2057 memcmp(newTexParams.fSwizzleRGBA,
2058 oldTexParams.fSwizzleRGBA,
2059 sizeof(newTexParams.fSwizzleRGBA)))) {
2060 setTextureUnit(s);
2061 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2062 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002063 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002064 nextTexture->setCachedTexParams(newTexParams,
2065 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002066 }
2067 }
2068
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002069 GrIRect* rect = NULL;
2070 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002071 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002072 fClip.hasConservativeBounds()) {
2073 fClip.getConservativeBounds().roundOut(&clipBounds);
2074 rect = &clipBounds;
2075 }
2076 this->flushRenderTarget(rect);
2077 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002078
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002079 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2080 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002081 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002082 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002083 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002084 }
2085 }
2086
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002087 if (drawState->isColorWriteDisabled() !=
2088 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002089 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002090 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002091 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002092 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002093 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002094 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002095 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002096 }
2097
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2099 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002100 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002101 GL_CALL(Enable(GR_GL_CULL_FACE));
2102 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002103 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002104 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002105 GL_CALL(Enable(GR_GL_CULL_FACE));
2106 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002107 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002108 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002109 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002110 break;
2111 default:
2112 GrCrash("Unknown draw face.");
2113 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002114 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002115 }
2116
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002117#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002119 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002120 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002121 NULL == drawState->getRenderTarget() ||
2122 NULL == drawState->getTexture(s) ||
2123 drawState->getTexture(s)->asRenderTarget() !=
2124 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002125 }
2126#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002127
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002128 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002129
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002130 // This copy must happen after flushStencil() is called. flushStencil()
2131 // relies on detecting when the kModifyStencilClip_StateBit state has
2132 // changed since the last draw.
2133 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002134 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002135}
2136
2137void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002138 if (fHWGeometryState.fVertexBuffer != buffer) {
2139 fHWGeometryState.fArrayPtrsDirty = true;
2140 fHWGeometryState.fVertexBuffer = buffer;
2141 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002142}
2143
2144void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002145 if (fHWGeometryState.fVertexBuffer == buffer) {
2146 // deleting bound buffer does implied bind to 0
2147 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002148 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002149 }
2150}
2151
2152void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002153 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
2156void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002157 if (fHWGeometryState.fIndexBuffer == buffer) {
2158 // deleting bound buffer does implied bind to 0
2159 fHWGeometryState.fIndexBuffer = NULL;
2160 }
2161}
2162
reed@google.comac10a2d2010-12-22 21:39:39 +00002163void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2164 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002165 GrDrawState* drawState = this->drawState();
2166 if (drawState->getRenderTarget() == renderTarget) {
2167 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002169 if (fHWDrawState.getRenderTarget() == renderTarget) {
2170 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002172}
2173
2174void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002175 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002176 GrDrawState* drawState = this->drawState();
2177 if (drawState->getTexture(s) == texture) {
2178 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002179 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002180 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002181 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002182 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002183 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002185}
2186
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002187bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2188 bool getSizedInternalFormat,
2189 GrGLenum* internalFormat,
2190 GrGLenum* externalFormat,
2191 GrGLenum* externalType) {
2192 GrGLenum dontCare;
2193 if (NULL == internalFormat) {
2194 internalFormat = &dontCare;
2195 }
2196 if (NULL == externalFormat) {
2197 externalFormat = &dontCare;
2198 }
2199 if (NULL == externalType) {
2200 externalType = &dontCare;
2201 }
2202
reed@google.comac10a2d2010-12-22 21:39:39 +00002203 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002204 case kRGBA_8888_PM_GrPixelConfig:
2205 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002206 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002207 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002208 if (getSizedInternalFormat) {
2209 *internalFormat = GR_GL_RGBA8;
2210 } else {
2211 *internalFormat = GR_GL_RGBA;
2212 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002213 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002214 break;
2215 case kBGRA_8888_PM_GrPixelConfig:
2216 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002217 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002218 return false;
2219 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002220 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002221 if (getSizedInternalFormat) {
2222 *internalFormat = GR_GL_BGRA8;
2223 } else {
2224 *internalFormat = GR_GL_BGRA;
2225 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002226 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002227 if (getSizedInternalFormat) {
2228 *internalFormat = GR_GL_RGBA8;
2229 } else {
2230 *internalFormat = GR_GL_RGBA;
2231 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002232 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002233 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002234 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002235 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002236 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002237 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002238 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002239 if (getSizedInternalFormat) {
2240 if (this->glBinding() == kDesktop_GrGLBinding) {
2241 return false;
2242 } else {
2243 *internalFormat = GR_GL_RGB565;
2244 }
2245 } else {
2246 *internalFormat = GR_GL_RGB;
2247 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002248 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002249 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002250 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002251 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002252 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002253 if (getSizedInternalFormat) {
2254 *internalFormat = GR_GL_RGBA4;
2255 } else {
2256 *internalFormat = GR_GL_RGBA;
2257 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002258 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002260 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002261 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002262 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002263 // glCompressedTexImage doesn't take external params
2264 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002265 // no sized/unsized internal format distinction here
2266 *internalFormat = GR_GL_PALETTE8_RGBA8;
2267 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002268 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002269 } else {
2270 return false;
2271 }
2272 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002273 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002274 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002275 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002276 if (getSizedInternalFormat) {
2277 *internalFormat = GR_GL_ALPHA8;
2278 } else {
2279 *internalFormat = GR_GL_ALPHA;
2280 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002281 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002282 break;
2283 default:
2284 return false;
2285 }
2286 return true;
2287}
2288
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002289void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002290 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002291 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002292 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002293 fActiveTextureUnitIdx = unit;
2294 }
2295}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002296
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002298 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002299 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002300 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2301 }
2302}
2303
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002304void GrGpuGL::resetDirtyFlags() {
2305 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2306}
2307
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002308void GrGpuGL::setBuffers(bool indexed,
2309 int* extraVertexOffset,
2310 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002311
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002312 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002313
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002314 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2315
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002316 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002317 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002318 case kBuffer_GeometrySrcType:
2319 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002320 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002321 break;
2322 case kArray_GeometrySrcType:
2323 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002324 this->finalizeReservedVertices();
2325 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2326 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002327 break;
2328 default:
2329 vbuf = NULL; // suppress warning
2330 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002331 }
2332
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002333 GrAssert(NULL != vbuf);
2334 GrAssert(!vbuf->isLocked());
2335 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002336 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002337 fHWGeometryState.fArrayPtrsDirty = true;
2338 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002339 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002340
2341 if (indexed) {
2342 GrAssert(NULL != extraIndexOffset);
2343
2344 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002345 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002346 case kBuffer_GeometrySrcType:
2347 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002348 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002349 break;
2350 case kArray_GeometrySrcType:
2351 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002352 this->finalizeReservedIndices();
2353 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2354 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002355 break;
2356 default:
2357 ibuf = NULL; // suppress warning
2358 GrCrash("Unknown geometry src type!");
2359 }
2360
2361 GrAssert(NULL != ibuf);
2362 GrAssert(!ibuf->isLocked());
2363 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002364 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002365 fHWGeometryState.fIndexBuffer = ibuf;
2366 }
2367 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002368}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002369
2370int GrGpuGL::getMaxEdges() const {
2371 // FIXME: This is a pessimistic estimate based on how many other things
2372 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002373 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002374 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002375}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002376