blob: 8481b74ab489132599dd793e0a72a2209830730f [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;
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000322 dstDesc.fConfig = kRGBA_8888_PM_GrPixelConfig;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000323 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
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000986 // unbind the texture from the texture unit before binding it to the frame buffer
987 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
988
bsalomon@google.com99621082011-11-15 16:47:16 +0000989 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
990 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991 glTexDesc.fTextureID,
992 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000994 return return_null_texture();
995 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000996 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000998 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001000 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001001#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001002 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1003 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001004#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 return tex;
1006}
1007
1008namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001009
1010const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012void inline get_stencil_rb_sizes(const GrGLInterface* gl,
1013 GrGLuint rb,
1014 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 // we shouldn't ever know one size and not the other
1016 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1017 (kUnknownBitCount == format->fTotalBits));
1018 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001020 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1021 (GrGLint*)&format->fStencilBits);
1022 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001023 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1025 (GrGLint*)&format->fTotalBits);
1026 format->fTotalBits += format->fStencilBits;
1027 } else {
1028 format->fTotalBits = format->fStencilBits;
1029 }
1030 }
1031}
1032}
1033
1034bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1035 int width, int height) {
1036
1037 // All internally created RTs are also textures. We don't create
1038 // SBs for a client's standalone RT (that is RT that isnt also a texture).
1039 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001040 GrAssert(width >= rt->width());
1041 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001042
1043 int samples = rt->numSamples();
1044 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001045 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001046 if (!sbID) {
1047 return false;
1048 }
1049
1050 GrGLStencilBuffer* sb = NULL;
1051
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001052 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001054 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055 // we start with the last stencil format that succeeded in hopes
1056 // that we won't go through this loop more than once after the
1057 // first (painful) stencil creation.
1058 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001059 const GrGLCaps::StencilFormat& sFmt =
1060 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001061 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001062 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 // version on a GL that doesn't have an MSAA extension.
1064 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001065 GL_ALLOC_CALL(this->glInterface(),
1066 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1067 samples,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001068 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001069 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001070 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001071 GL_ALLOC_CALL(this->glInterface(),
1072 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001073 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001074 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 }
1076
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001077 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 if (err == GR_GL_NO_ERROR) {
1079 // After sized formats we attempt an unsized format and take whatever
1080 // sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001081 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001082 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001083 sb = new GrGLStencilBuffer(this, sbID, width, height,
1084 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001085 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1086 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001087 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001089 return true;
1090 }
1091 sb->abandon(); // otherwise we lose sbID
1092 sb->unref();
1093 }
1094 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001095 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001096 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097}
1098
1099bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1100 GrRenderTarget* rt) {
1101 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1102
1103 GrGLuint fbo = glrt->renderFBOID();
1104
1105 if (NULL == sb) {
1106 if (NULL != rt->getStencilBuffer()) {
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_STENCIL_ATTACHMENT,
1109 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 GR_GL_DEPTH_ATTACHMENT,
1112 GR_GL_RENDERBUFFER, 0));
1113#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001114 GrGLenum status;
1115 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1117#endif
1118 }
1119 return true;
1120 } else {
1121 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1122 GrGLuint rb = glsb->renderbufferID();
1123
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001124 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1126 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 GR_GL_STENCIL_ATTACHMENT,
1128 GR_GL_RENDERBUFFER, rb));
1129 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 GR_GL_DEPTH_ATTACHMENT,
1132 GR_GL_RENDERBUFFER, rb));
1133 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135 GR_GL_DEPTH_ATTACHMENT,
1136 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001139 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001140 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001141 glsb->format())) {
1142 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1143 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001145 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001147 if (glsb->format().fPacked) {
1148 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1149 GR_GL_DEPTH_ATTACHMENT,
1150 GR_GL_RENDERBUFFER, 0));
1151 }
1152 return false;
1153 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001154 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001155 rt->config(),
1156 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001159 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001160 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001161}
1162
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001163////////////////////////////////////////////////////////////////////////////////
1164
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001165GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001166 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001167 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001168 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001170 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001171 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001172 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001173 GL_ALLOC_CALL(this->glInterface(),
1174 BufferData(GR_GL_ARRAY_BUFFER,
1175 size,
1176 NULL, // data ptr
1177 dynamic ? GR_GL_DYNAMIC_DRAW :
1178 GR_GL_STATIC_DRAW));
1179 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 // deleting bound buffer does implicit bind to 0
1182 fHWGeometryState.fVertexBuffer = NULL;
1183 return NULL;
1184 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001185 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 size, dynamic);
1187 fHWGeometryState.fVertexBuffer = vertexBuffer;
1188 return vertexBuffer;
1189 }
1190 return NULL;
1191}
1192
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001193GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001194 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001197 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001198 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001200 GL_ALLOC_CALL(this->glInterface(),
1201 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1202 size,
1203 NULL, // data ptr
1204 dynamic ? GR_GL_DYNAMIC_DRAW :
1205 GR_GL_STATIC_DRAW));
1206 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001207 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 // deleting bound buffer does implicit bind to 0
1209 fHWGeometryState.fIndexBuffer = NULL;
1210 return NULL;
1211 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001212 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 size, dynamic);
1214 fHWGeometryState.fIndexBuffer = indexBuffer;
1215 return indexBuffer;
1216 }
1217 return NULL;
1218}
1219
reed@google.comac10a2d2010-12-22 21:39:39 +00001220void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001221 const GrDrawState& drawState = this->getDrawState();
1222 const GrGLRenderTarget* rt =
1223 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1224
1225 GrAssert(NULL != rt);
1226 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001227
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001228 GrGLIRect scissor;
1229 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001230 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001231 rect->width(), rect->height());
1232 if (scissor.contains(vp)) {
1233 rect = NULL;
1234 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 }
1236
1237 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001239 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 fHWBounds.fScissorRect = scissor;
1241 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001243 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 fHWBounds.fScissorEnabled = true;
1245 }
1246 } else {
1247 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001248 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 fHWBounds.fScissorEnabled = false;
1250 }
1251 }
1252}
1253
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001254void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001255 const GrDrawState& drawState = this->getDrawState();
1256 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001257 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001258 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001259
bsalomon@google.com74b98712011-11-11 19:46:16 +00001260 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001261 if (NULL != rect) {
1262 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001263 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001264 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001265 if (clippedRect.intersect(rtRect)) {
1266 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001267 } else {
1268 return;
1269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001271 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001272 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001273
1274 GrGLfloat r, g, b, a;
1275 static const GrGLfloat scale255 = 1.f / 255.f;
1276 a = GrColorUnpackA(color) * scale255;
1277 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001278 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001279 scaleRGB *= a;
1280 }
1281 r = GrColorUnpackR(color) * scaleRGB;
1282 g = GrColorUnpackG(color) * scaleRGB;
1283 b = GrColorUnpackB(color) * scaleRGB;
1284
1285 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001286 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001287 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289}
1290
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001291void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001292 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001293 return;
1294 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001295
1296 this->flushRenderTarget(&GrIRect::EmptyIRect());
1297
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001299 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001300 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GL_CALL(StencilMask(0xffffffff));
1303 GL_CALL(ClearStencil(0));
1304 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001305 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001306}
1307
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001308void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001309 const GrDrawState& drawState = this->getDrawState();
1310 const GrRenderTarget* rt = drawState.getRenderTarget();
1311 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001312
1313 // this should only be called internally when we know we have a
1314 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001315 GrAssert(NULL != rt->getStencilBuffer());
1316 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001317#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001319 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001320#else
1321 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001322 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001323 // turned into draws. Our contract on GrDrawTarget says that
1324 // changing the clip between stencil passes may or may not
1325 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001326 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001327#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001328 GrGLint value;
1329 if (insideClip) {
1330 value = (1 << (stencilBitCount - 1));
1331 } else {
1332 value = 0;
1333 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001334 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001335 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001337 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001338 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001339 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001342void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001343 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001344}
1345
bsalomon@google.comc4364992011-11-07 15:54:49 +00001346bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1347 int left, int top,
1348 int width, int height,
1349 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001350 size_t rowBytes) const {
1351 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001352 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001353 return false;
1354 }
1355
1356 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001357 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001358 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001359 return true;
1360 }
1361 // If we have to do memcpys to handle rowBytes then y-flip is free
1362 // Note the rowBytes might be tight to the passed in data, but if data
1363 // gets clipped in x to the target the rowBytes will no longer be tight.
1364 if (left >= 0 && (left + width) < renderTarget->width()) {
1365 return 0 == rowBytes ||
1366 GrBytesPerPixel(config) * width == rowBytes;
1367 } else {
1368 return false;
1369 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370}
1371
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001372bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001373 int left, int top,
1374 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001375 GrPixelConfig config,
1376 void* buffer,
1377 size_t rowBytes,
1378 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001379 GrGLenum format;
1380 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001381 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001383 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001384 size_t bpp = GrBytesPerPixel(config);
1385 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1386 &left, &top, &width, &height,
1387 const_cast<const void**>(&buffer),
1388 &rowBytes)) {
1389 return false;
1390 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001391
bsalomon@google.comc6980972011-11-02 19:57:21 +00001392 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001393 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001394 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001395 switch (tgt->getResolveType()) {
1396 case GrGLRenderTarget::kCantResolve_ResolveType:
1397 return false;
1398 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001399 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001400 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001401 break;
1402 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001403 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001404 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001405 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1406 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001407 break;
1408 default:
1409 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 }
1411
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001412 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001413
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001414 // the read rect is viewport-relative
1415 GrGLIRect readRect;
1416 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001417
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001418 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001419 if (0 == rowBytes) {
1420 rowBytes = tightRowBytes;
1421 }
1422 size_t readDstRowBytes = tightRowBytes;
1423 void* readDst = buffer;
1424
1425 // determine if GL can read using the passed rowBytes or if we need
1426 // a scratch buffer.
1427 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1428 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001429 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001430 GrAssert(!(rowBytes % sizeof(GrColor)));
1431 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1432 readDstRowBytes = rowBytes;
1433 } else {
1434 scratch.reset(tightRowBytes * height);
1435 readDst = scratch.get();
1436 }
1437 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001438 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001439 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1440 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001441 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1442 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001443 format, type, readDst));
1444 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001445 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001446 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1447 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001448 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001449 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1450 invertY = true;
1451 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001452
1453 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001454 // API presents top-to-bottom. We must preserve the padding contents. Note
1455 // that the above readPixels did not overwrite the padding.
1456 if (readDst == buffer) {
1457 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001458 if (!invertY) {
1459 scratch.reset(tightRowBytes);
1460 void* tmpRow = scratch.get();
1461 // flip y in-place by rows
1462 const int halfY = height >> 1;
1463 char* top = reinterpret_cast<char*>(buffer);
1464 char* bottom = top + (height - 1) * rowBytes;
1465 for (int y = 0; y < halfY; y++) {
1466 memcpy(tmpRow, top, tightRowBytes);
1467 memcpy(top, bottom, tightRowBytes);
1468 memcpy(bottom, tmpRow, tightRowBytes);
1469 top += rowBytes;
1470 bottom -= rowBytes;
1471 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001472 }
1473 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001474 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 // copy from readDst to buffer while flipping y
1476 const int halfY = height >> 1;
1477 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001478 char* dst = reinterpret_cast<char*>(buffer);
1479 if (!invertY) {
1480 dst += (height-1) * rowBytes;
1481 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001482 for (int y = 0; y < height; y++) {
1483 memcpy(dst, src, tightRowBytes);
1484 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001485 if (invertY) {
1486 dst += rowBytes;
1487 } else {
1488 dst -= rowBytes;
1489 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 }
1491 }
1492 return true;
1493}
1494
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001495void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001496
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001497 GrGLRenderTarget* rt =
1498 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1499 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001500
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001501 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001503 #if GR_COLLECT_STATS
1504 ++fStats.fRenderTargetChngCnt;
1505 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001506 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001507 GrGLenum status;
1508 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001509 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001510 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 }
1512 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001513 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001514 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001515 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001516 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001517 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 fHWBounds.fViewportRect = vp;
1519 }
1520 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001521 if (NULL == bound || !bound->isEmpty()) {
1522 rt->flagAsNeedingResolve(bound);
1523 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001524}
1525
twiz@google.com0f31ca72011-03-18 17:38:11 +00001526GrGLenum gPrimitiveType2GLMode[] = {
1527 GR_GL_TRIANGLES,
1528 GR_GL_TRIANGLE_STRIP,
1529 GR_GL_TRIANGLE_FAN,
1530 GR_GL_POINTS,
1531 GR_GL_LINES,
1532 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001533};
1534
bsalomon@google.comd302f142011-03-03 13:54:13 +00001535#define SWAP_PER_DRAW 0
1536
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001537#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001538 #if GR_MAC_BUILD
1539 #include <AGL/agl.h>
1540 #elif GR_WIN32_BUILD
1541 void SwapBuf() {
1542 DWORD procID = GetCurrentProcessId();
1543 HWND hwnd = GetTopWindow(GetDesktopWindow());
1544 while(hwnd) {
1545 DWORD wndProcID = 0;
1546 GetWindowThreadProcessId(hwnd, &wndProcID);
1547 if(wndProcID == procID) {
1548 SwapBuffers(GetDC(hwnd));
1549 }
1550 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1551 }
1552 }
1553 #endif
1554#endif
1555
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001556void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1557 uint32_t startVertex,
1558 uint32_t startIndex,
1559 uint32_t vertexCount,
1560 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001561 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1562
twiz@google.com0f31ca72011-03-18 17:38:11 +00001563 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001564
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001565 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1566 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1567
1568 // our setupGeometry better have adjusted this to zero since
1569 // DrawElements always draws from the begining of the arrays for idx 0.
1570 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001571
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001572 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1573 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001574#if SWAP_PER_DRAW
1575 glFlush();
1576 #if GR_MAC_BUILD
1577 aglSwapBuffers(aglGetCurrentContext());
1578 int set_a_break_pt_here = 9;
1579 aglSwapBuffers(aglGetCurrentContext());
1580 #elif GR_WIN32_BUILD
1581 SwapBuf();
1582 int set_a_break_pt_here = 9;
1583 SwapBuf();
1584 #endif
1585#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001586}
1587
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001588void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1589 uint32_t startVertex,
1590 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1592
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001593 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1594
1595 // our setupGeometry better have adjusted this to zero.
1596 // DrawElements doesn't take an offset so we always adjus the startVertex.
1597 GrAssert(0 == startVertex);
1598
1599 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1600 // account for startVertex in the DrawElements case. So we always
1601 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001602 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001603#if SWAP_PER_DRAW
1604 glFlush();
1605 #if GR_MAC_BUILD
1606 aglSwapBuffers(aglGetCurrentContext());
1607 int set_a_break_pt_here = 9;
1608 aglSwapBuffers(aglGetCurrentContext());
1609 #elif GR_WIN32_BUILD
1610 SwapBuf();
1611 int set_a_break_pt_here = 9;
1612 SwapBuf();
1613 #endif
1614#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001615}
1616
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001617void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1618
1619 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001620
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001621 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001622 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001624 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1625 rt->renderFBOID()));
1626 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1627 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 #if GR_COLLECT_STATS
1629 ++fStats.fRenderTargetChngCnt;
1630 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001631 // make sure we go through flushRenderTarget() since we've modified
1632 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001633 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001634 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001635 const GrIRect dirtyRect = rt->getResolveRect();
1636 GrGLIRect r;
1637 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1638 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001639
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001640 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001641 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001642 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1643 GL_CALL(Scissor(r.fLeft, r.fBottom,
1644 r.fWidth, r.fHeight));
1645 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001646 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 fHWBounds.fScissorEnabled = true;
1648 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001649 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001650 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001651 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1652 this->glCaps().msFBOType());
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001653 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001654 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001655 int right = r.fLeft + r.fWidth;
1656 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001657 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1658 r.fLeft, r.fBottom, right, top,
1659 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001660 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001661 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 }
1663}
1664
twiz@google.com0f31ca72011-03-18 17:38:11 +00001665static const GrGLenum grToGLStencilFunc[] = {
1666 GR_GL_ALWAYS, // kAlways_StencilFunc
1667 GR_GL_NEVER, // kNever_StencilFunc
1668 GR_GL_GREATER, // kGreater_StencilFunc
1669 GR_GL_GEQUAL, // kGEqual_StencilFunc
1670 GR_GL_LESS, // kLess_StencilFunc
1671 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1672 GR_GL_EQUAL, // kEqual_StencilFunc,
1673 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001674};
1675GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1676GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1677GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1678GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1679GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1680GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1681GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1682GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1683GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1684
twiz@google.com0f31ca72011-03-18 17:38:11 +00001685static const GrGLenum grToGLStencilOp[] = {
1686 GR_GL_KEEP, // kKeep_StencilOp
1687 GR_GL_REPLACE, // kReplace_StencilOp
1688 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1689 GR_GL_INCR, // kIncClamp_StencilOp
1690 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1691 GR_GL_DECR, // kDecClamp_StencilOp
1692 GR_GL_ZERO, // kZero_StencilOp
1693 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001694};
1695GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1696GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1697GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1698GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1699GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1700GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1701GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1702GR_STATIC_ASSERT(6 == kZero_StencilOp);
1703GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1704
reed@google.comac10a2d2010-12-22 21:39:39 +00001705void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001706 const GrDrawState& drawState = this->getDrawState();
1707
1708 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001709
1710 // use stencil for clipping if clipping is enabled and the clip
1711 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001712 bool stencilClip = fClipInStencil && drawState.isClipState();
1713 bool drawClipToStencil =
1714 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001715 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1716 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001717 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1718 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001719
1720 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001721
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001722 // we can't simultaneously perform stencil-clipping and
1723 // modify the stencil clip
1724 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001725
bsalomon@google.comd302f142011-03-03 13:54:13 +00001726 if (settings->isDisabled()) {
1727 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001728 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001729 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001731
1732 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001733 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001734 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001735 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001736 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001737 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001738 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1739 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1740 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1741 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1742 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1743 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1744 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1745 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001746 }
1747 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001748 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001749 GrStencilBuffer* stencilBuffer =
1750 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001751 if (NULL != stencilBuffer) {
1752 stencilBits = stencilBuffer->bits();
1753 }
1754 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001755 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001756
1757 GrGLuint clipStencilMask = 0;
1758 GrGLuint userStencilMask = ~0;
1759 if (stencilBits > 0) {
1760 clipStencilMask = 1 << (stencilBits - 1);
1761 userStencilMask = clipStencilMask - 1;
1762 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001764 unsigned int frontRef = settings->frontFuncRef();
1765 unsigned int frontMask = settings->frontFuncMask();
1766 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001767 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001768
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001769 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001770 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1771 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001772 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001773 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001774 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001775
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001776 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001777 stencilClip,
1778 clipStencilMask,
1779 userStencilMask,
1780 &frontRef,
1781 &frontMask);
1782 frontWriteMask &= userStencilMask;
1783 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001784 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001785 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001786 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001787 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001788 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001789 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001790 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001791 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001792 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001793 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001794
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001795 unsigned int backRef = settings->backFuncRef();
1796 unsigned int backMask = settings->backFuncMask();
1797 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798
1799
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001800 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001801 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1802 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001803 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001804 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001805 stencilClip, settings->backFunc())];
1806 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001807 stencilClip,
1808 clipStencilMask,
1809 userStencilMask,
1810 &backRef,
1811 &backMask);
1812 backWriteMask &= userStencilMask;
1813 }
1814
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001815 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1816 frontRef, frontMask));
1817 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1818 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1819 backRef, backMask));
1820 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1821 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001822 grToGLStencilOp[settings->frontFailOp()],
1823 grToGLStencilOp[settings->frontPassOp()],
1824 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001825
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001826 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001827 grToGLStencilOp[settings->backFailOp()],
1828 grToGLStencilOp[settings->backPassOp()],
1829 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001831 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1832 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001833 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1834 grToGLStencilOp[settings->frontPassOp()],
1835 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001836 }
1837 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001838 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001839 fHWStencilClip = stencilClip;
1840 }
1841}
1842
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001843void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001844 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001845 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001846 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1847 // smooth lines.
1848
1849 // we prefer smooth lines over multisampled lines
1850 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001851 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001852 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001853 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001854 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001855 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001856 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001857 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001858 fHWAAState.fSmoothLineEnabled = false;
1859 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001860 if (rt->isMultisampled() &&
1861 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001862 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001863 fHWAAState.fMSAAEnabled = false;
1864 }
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001865 } else if (rt->isMultisampled() &&
1866 this->getDrawState().isHWAntialiasState() !=
1867 fHWAAState.fMSAAEnabled) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001868 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001869 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001870 fHWAAState.fMSAAEnabled = false;
1871 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001872 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001873 fHWAAState.fMSAAEnabled = true;
1874 }
1875 }
1876 }
1877}
1878
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001879void GrGpuGL::flushBlend(GrPrimitiveType type,
1880 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001881 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001882 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001883 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001884 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001885 fHWBlendDisabled = false;
1886 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001887 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1888 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001889 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1890 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001891 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001892 }
1893 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001894 // any optimization to disable blending should
1895 // have already been applied and tweaked the coeffs
1896 // to (1, 0).
1897 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1898 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001899 if (fHWBlendDisabled != blendOff) {
1900 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001902 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001903 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001904 }
1905 fHWBlendDisabled = blendOff;
1906 }
1907 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001908 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1909 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001910 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1911 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001912 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001913 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001914 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001915 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1916 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001917 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001918
1919 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001920 GrColorUnpackR(blendConst) / 255.f,
1921 GrColorUnpackG(blendConst) / 255.f,
1922 GrColorUnpackB(blendConst) / 255.f,
1923 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001924 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001925 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001926 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001927 }
1928 }
1929 }
1930}
1931
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001932namespace {
1933
1934unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001935 switch (filter) {
1936 case GrSamplerState::kBilinear_Filter:
1937 case GrSamplerState::k4x4Downsample_Filter:
1938 return GR_GL_LINEAR;
1939 case GrSamplerState::kNearest_Filter:
1940 case GrSamplerState::kConvolution_Filter:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001941 case GrSamplerState::kErode_Filter:
1942 case GrSamplerState::kDilate_Filter:
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001943 return GR_GL_NEAREST;
1944 default:
1945 GrAssert(!"Unknown filter type");
1946 return GR_GL_LINEAR;
1947 }
1948}
1949
robertphillips@google.com69950682012-04-06 18:06:10 +00001950// get_swizzle is only called from this .cpp so it is OK to inline it here
1951inline const GrGLenum* get_swizzle(GrPixelConfig config,
1952 const GrSamplerState& sampler,
1953 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001954 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com69950682012-04-06 18:06:10 +00001955 if (glCaps.textureRedSupport()) {
1956 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
1957 GR_GL_RED, GR_GL_RED };
1958 return gRedSmear;
1959 } else {
1960 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
1961 GR_GL_ALPHA, GR_GL_ALPHA };
1962 return gAlphaSmear;
1963 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001964 } else if (sampler.swapsRAndB()) {
1965 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
1966 GR_GL_RED, GR_GL_ALPHA };
1967 return gRedBlueSwap;
1968 } else {
1969 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
1970 GR_GL_BLUE, GR_GL_ALPHA };
1971 return gStraight;
1972 }
1973}
1974
1975void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
1976 // should add texparameteri to interface to make 1 instead of 4 calls here
1977 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1978 GR_GL_TEXTURE_SWIZZLE_R,
1979 swizzle[0]));
1980 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1981 GR_GL_TEXTURE_SWIZZLE_G,
1982 swizzle[1]));
1983 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1984 GR_GL_TEXTURE_SWIZZLE_B,
1985 swizzle[2]));
1986 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1987 GR_GL_TEXTURE_SWIZZLE_A,
1988 swizzle[3]));
1989}
1990}
1991
bsalomon@google.comffca4002011-02-22 20:34:01 +00001992bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001993
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001994 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001995 // GrGpu::setupClipAndFlushState should have already checked this
1996 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001997 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00001998
tomhudson@google.com93813632011-10-27 20:21:16 +00001999 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002000 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002001 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002002 GrGLTexture* nextTexture =
2003 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00002004
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002005 // true for now, but maybe not with GrEffect.
2006 GrAssert(NULL != nextTexture);
2007 // if we created a rt/tex and rendered to it without using a
2008 // texture and now we're texuring from the rt it will still be
2009 // the last bound texture, but it needs resolving. So keep this
2010 // out of the "last != next" check.
2011 GrGLRenderTarget* texRT =
2012 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
2013 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002014 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00002015 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002016
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002017 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002018 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002020 #if GR_COLLECT_STATS
2021 ++fStats.fTextureChngCnt;
2022 #endif
2023 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002024 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00002025 // The texture matrix has to compensate for texture width/height
2026 // and NPOT-embedded-in-POT
2027 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002028 }
2029
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002030 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002031 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002032 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002033 nextTexture->getCachedTexParams(&timestamp);
2034 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002035 GrGLTexture::TexParams newTexParams;
2036
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002037 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00002038
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00002039 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002040 newTexParams.fWrapS = wraps[sampler.getWrapX()];
2041 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002042 memcpy(newTexParams.fSwizzleRGBA,
robertphillips@google.com69950682012-04-06 18:06:10 +00002043 get_swizzle(nextTexture->config(), sampler, this->glCaps()),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002044 sizeof(newTexParams.fSwizzleRGBA));
2045 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002046 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002047 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002048 GR_GL_TEXTURE_MAG_FILTER,
2049 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002050 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002051 GR_GL_TEXTURE_MIN_FILTER,
2052 newTexParams.fFilter));
2053 }
2054 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2055 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002056 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002057 GR_GL_TEXTURE_WRAP_S,
2058 newTexParams.fWrapS));
2059 }
2060 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2061 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002062 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002063 GR_GL_TEXTURE_WRAP_T,
2064 newTexParams.fWrapT));
2065 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002066 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002067 (setAll ||
2068 memcmp(newTexParams.fSwizzleRGBA,
2069 oldTexParams.fSwizzleRGBA,
2070 sizeof(newTexParams.fSwizzleRGBA)))) {
2071 setTextureUnit(s);
2072 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2073 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002074 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00002075 nextTexture->setCachedTexParams(newTexParams,
2076 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00002077 }
2078 }
2079
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002080 GrIRect* rect = NULL;
2081 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002082 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002083 fClip.hasConservativeBounds()) {
2084 fClip.getConservativeBounds().roundOut(&clipBounds);
2085 rect = &clipBounds;
2086 }
2087 this->flushRenderTarget(rect);
2088 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002089
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002090 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
2091 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002092 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002093 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002094 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002095 }
2096 }
2097
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 if (drawState->isColorWriteDisabled() !=
2099 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002100 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002101 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002102 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002103 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002104 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002105 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002106 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002107 }
2108
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002109 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002110 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002111 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002112 GL_CALL(Enable(GR_GL_CULL_FACE));
2113 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002114 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002115 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 GL_CALL(Enable(GR_GL_CULL_FACE));
2117 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002118 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002119 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002120 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002121 break;
2122 default:
2123 GrCrash("Unknown draw face.");
2124 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002125 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002126 }
2127
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002128#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002129 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002130 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002131 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002132 NULL == drawState->getRenderTarget() ||
2133 NULL == drawState->getTexture(s) ||
2134 drawState->getTexture(s)->asRenderTarget() !=
2135 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002136 }
2137#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002138
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002139 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002140
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002141 // This copy must happen after flushStencil() is called. flushStencil()
2142 // relies on detecting when the kModifyStencilClip_StateBit state has
2143 // changed since the last draw.
2144 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002145 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002146}
2147
2148void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002149 if (fHWGeometryState.fVertexBuffer != buffer) {
2150 fHWGeometryState.fArrayPtrsDirty = true;
2151 fHWGeometryState.fVertexBuffer = buffer;
2152 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002153}
2154
2155void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002156 if (fHWGeometryState.fVertexBuffer == buffer) {
2157 // deleting bound buffer does implied bind to 0
2158 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002159 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002160 }
2161}
2162
2163void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002164 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002165}
2166
2167void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002168 if (fHWGeometryState.fIndexBuffer == buffer) {
2169 // deleting bound buffer does implied bind to 0
2170 fHWGeometryState.fIndexBuffer = NULL;
2171 }
2172}
2173
reed@google.comac10a2d2010-12-22 21:39:39 +00002174void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2175 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002176 GrDrawState* drawState = this->drawState();
2177 if (drawState->getRenderTarget() == renderTarget) {
2178 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002180 if (fHWDrawState.getRenderTarget() == renderTarget) {
2181 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002182 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002183}
2184
2185void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002186 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002187 GrDrawState* drawState = this->drawState();
2188 if (drawState->getTexture(s) == texture) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002189 this->drawState()->setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002190 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002191 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002192 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002193 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002194 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002196}
2197
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002198bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2199 bool getSizedInternalFormat,
2200 GrGLenum* internalFormat,
2201 GrGLenum* externalFormat,
2202 GrGLenum* externalType) {
2203 GrGLenum dontCare;
2204 if (NULL == internalFormat) {
2205 internalFormat = &dontCare;
2206 }
2207 if (NULL == externalFormat) {
2208 externalFormat = &dontCare;
2209 }
2210 if (NULL == externalType) {
2211 externalType = &dontCare;
2212 }
2213
reed@google.comac10a2d2010-12-22 21:39:39 +00002214 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002215 case kRGBA_8888_PM_GrPixelConfig:
2216 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002217 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002218 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002219 if (getSizedInternalFormat) {
2220 *internalFormat = GR_GL_RGBA8;
2221 } else {
2222 *internalFormat = GR_GL_RGBA;
2223 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002224 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002225 break;
2226 case kBGRA_8888_PM_GrPixelConfig:
2227 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002228 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002229 return false;
2230 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002231 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002232 if (getSizedInternalFormat) {
2233 *internalFormat = GR_GL_BGRA8;
2234 } else {
2235 *internalFormat = GR_GL_BGRA;
2236 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002237 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002238 if (getSizedInternalFormat) {
2239 *internalFormat = GR_GL_RGBA8;
2240 } else {
2241 *internalFormat = GR_GL_RGBA;
2242 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002243 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002244 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002245 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002246 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002247 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002248 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002249 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002250 if (getSizedInternalFormat) {
2251 if (this->glBinding() == kDesktop_GrGLBinding) {
2252 return false;
2253 } else {
2254 *internalFormat = GR_GL_RGB565;
2255 }
2256 } else {
2257 *internalFormat = GR_GL_RGB;
2258 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002259 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002261 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002262 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002263 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002264 if (getSizedInternalFormat) {
2265 *internalFormat = GR_GL_RGBA4;
2266 } else {
2267 *internalFormat = GR_GL_RGBA;
2268 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002269 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002270 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002271 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002272 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002273 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002274 // glCompressedTexImage doesn't take external params
2275 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002276 // no sized/unsized internal format distinction here
2277 *internalFormat = GR_GL_PALETTE8_RGBA8;
2278 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002279 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002280 } else {
2281 return false;
2282 }
2283 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002284 case kAlpha_8_GrPixelConfig:
robertphillips@google.com69950682012-04-06 18:06:10 +00002285 if (this->glCaps().textureRedSupport()) {
2286 *internalFormat = GR_GL_RED;
2287 *externalFormat = GR_GL_RED;
2288 if (getSizedInternalFormat) {
2289 *internalFormat = GR_GL_R8;
2290 } else {
2291 *internalFormat = GR_GL_RED;
2292 }
2293 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294 } else {
2295 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com69950682012-04-06 18:06:10 +00002296 *externalFormat = GR_GL_ALPHA;
2297 if (getSizedInternalFormat) {
2298 *internalFormat = GR_GL_ALPHA8;
2299 } else {
2300 *internalFormat = GR_GL_ALPHA;
2301 }
2302 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002304 break;
2305 default:
2306 return false;
2307 }
2308 return true;
2309}
2310
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002311void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002312 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002313 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002314 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002315 fActiveTextureUnitIdx = unit;
2316 }
2317}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002318
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002319void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002320 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002321 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002322 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2323 }
2324}
2325
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002326void GrGpuGL::resetDirtyFlags() {
2327 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2328}
2329
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002330void GrGpuGL::setBuffers(bool indexed,
2331 int* extraVertexOffset,
2332 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002333
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002334 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002335
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002336 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2337
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002338 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002339 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002340 case kBuffer_GeometrySrcType:
2341 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002342 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002343 break;
2344 case kArray_GeometrySrcType:
2345 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002346 this->finalizeReservedVertices();
2347 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2348 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002349 break;
2350 default:
2351 vbuf = NULL; // suppress warning
2352 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002353 }
2354
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002355 GrAssert(NULL != vbuf);
2356 GrAssert(!vbuf->isLocked());
2357 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002358 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002359 fHWGeometryState.fArrayPtrsDirty = true;
2360 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002361 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002362
2363 if (indexed) {
2364 GrAssert(NULL != extraIndexOffset);
2365
2366 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002367 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002368 case kBuffer_GeometrySrcType:
2369 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002370 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002371 break;
2372 case kArray_GeometrySrcType:
2373 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002374 this->finalizeReservedIndices();
2375 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2376 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002377 break;
2378 default:
2379 ibuf = NULL; // suppress warning
2380 GrCrash("Unknown geometry src type!");
2381 }
2382
2383 GrAssert(NULL != ibuf);
2384 GrAssert(!ibuf->isLocked());
2385 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002386 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002387 fHWGeometryState.fIndexBuffer = ibuf;
2388 }
2389 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002390}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002391
2392int GrGpuGL::getMaxEdges() const {
2393 // FIXME: This is a pessimistic estimate based on how many other things
2394 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002395 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002396 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002397}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002398