blob: 68f33b11cacc090c6b5f65356e6512d3a1f892d5 [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;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210}
211
212GrGpuGL::~GrGpuGL() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000213 // This must be called by before the GrDrawTarget destructor
214 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000215 // This subclass must do this before the base class destructor runs
216 // since we will unref the GrGLInterface.
217 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218}
219
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220///////////////////////////////////////////////////////////////////////////////
221
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000222void GrGpuGL::initCaps() {
223 GrGLint maxTextureUnits;
224 // check FS and fixed-function texture unit limits
225 // we only use textures in the fragment stage currently.
226 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000227 const GrGLInterface* gl = this->glInterface();
228 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000229 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000230 if (kES2_GrGLBinding != this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000231 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
tomhudson@google.com93813632011-10-27 20:21:16 +0000232 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000233 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234
235 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000236 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000237 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000238 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239 for (int i = 0; i < numFormats; ++i) {
240 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
241 fCaps.f8BitPaletteSupport = true;
242 break;
243 }
244 }
245
246 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000247 // we could also look for GL_ATI_separate_stencil extension or
248 // GL_EXT_stencil_two_side but they use different function signatures
249 // than GL2.0+ (and than each other).
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000250 fCaps.fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000251 // supported on GL 1.4 and higher or by extension
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000252 fCaps.fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000253 this->hasExtension("GL_EXT_stencil_wrap");
254 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000255 // ES 2 has two sided stencil and stencil wrap
256 fCaps.fTwoSidedStencilSupport = true;
257 fCaps.fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000258 }
259
260 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 fCaps.fBufferLockSupport = true; // we require VBO support and the desktop VBO
262 // extension includes glMapBuffer.
263 } else {
264 fCaps.fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
265 }
266
267 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000268 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000269 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
270 fCaps.fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000271 } else {
272 fCaps.fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000273 }
274 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000275 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000276 fCaps.fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000277 }
278
279 fCaps.fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
280
281 ////////////////////////////////////////////////////////////////////////////
282 // Experiments to determine limitations that can't be queried.
283 // TODO: Make these a preprocess that generate some compile time constants.
284 // TODO: probe once at startup, rather than once per context creation.
285
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000286 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &fCaps.fMaxTextureSize);
287 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &fCaps.fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000288 // Our render targets are always created with textures as the color
289 // attachment, hence this min:
290 fCaps.fMaxRenderTargetSize = GrMin(fCaps.fMaxTextureSize, fCaps.fMaxRenderTargetSize);
291
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000292 fCaps.fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000293}
294
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000295GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000296 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
297 return GrPixelConfigSwapRAndB(config);
298 } else {
299 return config;
300 }
301}
302
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000303GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000304 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000305 return GrPixelConfigSwapRAndB(config);
306 } else {
307 return config;
308 }
309}
310
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000311bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
312 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
313}
314
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000315void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000316 if (gPrintStartupSpew && !fPrintedCaps) {
317 fPrintedCaps = true;
318 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000319 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320 }
321
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000322 // We detect cases when blending is effectively off
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 fHWBlendDisabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000324 GL_CALL(Enable(GR_GL_BLEND));
reed@google.comac10a2d2010-12-22 21:39:39 +0000325
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000326 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000327 GL_CALL(Disable(GR_GL_DEPTH_TEST));
328 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000329
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000330 GL_CALL(Disable(GR_GL_CULL_FACE));
331 GL_CALL(FrontFace(GR_GL_CCW));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000332 fHWDrawState.setDrawFace(GrDrawState::kBoth_DrawFace);
reed@google.comac10a2d2010-12-22 21:39:39 +0000333
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000334 GL_CALL(Disable(GR_GL_DITHER));
335 if (kDesktop_GrGLBinding == this->glBinding()) {
336 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
337 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
338 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000339 fHWAAState.fMSAAEnabled = false;
340 fHWAAState.fSmoothLineEnabled = false;
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000341 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000343 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000344 fHWDrawState.resetStateFlags();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345
reed@google.comac10a2d2010-12-22 21:39:39 +0000346 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000347 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000349 // invalid
350 fActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 // illegal values
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000353 fHWDrawState.setBlendFunc((GrBlendCoeff)0xFF, (GrBlendCoeff)0xFF);
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000354
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000355 fHWDrawState.setBlendConstant(0x00000000);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000356 GL_CALL(BlendColor(0,0,0,0));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000357
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000358 fHWDrawState.setColor(GrColor_ILLEGAL);
bsalomon@google.com316f99232011-01-13 21:28:12 +0000359
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000360 fHWDrawState.setViewMatrix(GrMatrix::InvalidMatrix());
bsalomon@google.com316f99232011-01-13 21:28:12 +0000361
tomhudson@google.com93813632011-10-27 20:21:16 +0000362 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000363 fHWDrawState.setTexture(s, NULL);
364 fHWDrawState.sampler(s)->setRadial2Params(-GR_ScalarMax,
365 -GR_ScalarMax,
366 true);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000367 *fHWDrawState.sampler(s)->matrix() = GrMatrix::InvalidMatrix();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000368 fHWDrawState.sampler(s)->setConvolutionParams(0, NULL, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000369 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000370
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000371 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 fHWBounds.fScissorEnabled = false;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000373 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000374 fHWBounds.fViewportRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000375
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000376 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 fHWStencilClip = false;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000378 fClipInStencil = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
380 fHWGeometryState.fIndexBuffer = NULL;
381 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000382
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000383 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000385 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000387
388 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000389 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000390 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
391 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000392 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000393 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
394 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000395 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000396 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
397 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000398 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000399 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
400 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000401}
402
bsalomon@google.come269f212011-11-07 13:29:52 +0000403GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000404 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000405 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000406 return NULL;
407 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000408
bsalomon@google.com99621082011-11-15 16:47:16 +0000409 glTexDesc.fWidth = desc.fWidth;
410 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000411 glTexDesc.fConfig = desc.fConfig;
412 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
413 glTexDesc.fOwnsID = false;
414 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
415
416 GrGLTexture* texture = NULL;
417 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
418 GrGLRenderTarget::Desc glRTDesc;
419 glRTDesc.fRTFBOID = 0;
420 glRTDesc.fTexFBOID = 0;
421 glRTDesc.fMSColorRenderbufferID = 0;
422 glRTDesc.fOwnIDs = true;
423 glRTDesc.fConfig = desc.fConfig;
424 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000425 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
426 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000427 glTexDesc.fTextureID,
428 &glRTDesc)) {
429 return NULL;
430 }
431 texture = new GrGLTexture(this, glTexDesc, glRTDesc);
432 } else {
433 texture = new GrGLTexture(this, glTexDesc);
434 }
435 if (NULL == texture) {
436 return NULL;
437 }
438
439 this->setSpareTextureUnit();
440 return texture;
441}
442
443GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
444 GrGLRenderTarget::Desc glDesc;
445 glDesc.fConfig = desc.fConfig;
446 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
447 glDesc.fMSColorRenderbufferID = 0;
448 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
449 glDesc.fSampleCnt = desc.fSampleCnt;
450 glDesc.fOwnIDs = false;
451 GrGLIRect viewport;
452 viewport.fLeft = 0;
453 viewport.fBottom = 0;
454 viewport.fWidth = desc.fWidth;
455 viewport.fHeight = desc.fHeight;
456
457 GrRenderTarget* tgt = new GrGLRenderTarget(this, glDesc, viewport);
458 if (desc.fStencilBits) {
459 GrGLStencilBuffer::Format format;
460 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
461 format.fPacked = false;
462 format.fStencilBits = desc.fStencilBits;
463 format.fTotalBits = desc.fStencilBits;
464 GrGLStencilBuffer* sb = new GrGLStencilBuffer(this,
465 0,
466 desc.fWidth,
467 desc.fHeight,
468 desc.fSampleCnt,
469 format);
470 tgt->setStencilBuffer(sb);
471 sb->unref();
472 }
473 return tgt;
474}
475
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000476////////////////////////////////////////////////////////////////////////////////
477
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
479 int left, int top, int width, int height,
480 GrPixelConfig config, const void* buffer,
481 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000482 if (NULL == buffer) {
483 return;
484 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000485 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
486
bsalomon@google.com6f379512011-11-16 20:36:03 +0000487 this->setSpareTextureUnit();
488 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
489 GrGLTexture::Desc desc;
490 desc.fConfig = glTex->config();
491 desc.fWidth = glTex->width();
492 desc.fHeight = glTex->height();
493 desc.fOrientation = glTex->orientation();
494 desc.fTextureID = glTex->textureID();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000495
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000496 this->uploadTexData(desc, false,
497 left, top, width, height,
498 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000499}
500
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000501namespace {
502bool adjust_pixel_ops_params(int surfaceWidth,
503 int surfaceHeight,
504 size_t bpp,
505 int* left, int* top, int* width, int* height,
506 const void** data,
507 size_t* rowBytes) {
508 if (!*rowBytes) {
509 *rowBytes = *width * bpp;
510 }
511
512 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
513 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
514
515 if (!subRect.intersect(bounds)) {
516 return false;
517 }
518 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
519 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
520
521 *left = subRect.fLeft;
522 *top = subRect.fTop;
523 *width = subRect.width();
524 *height = subRect.height();
525 return true;
526}
527}
528
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000529bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
530 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 int left, int top, int width, int height,
532 GrPixelConfig dataConfig,
533 const void* data,
534 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000535 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000536
537 size_t bpp = GrBytesPerPixel(dataConfig);
538 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
539 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000540 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000541 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000542 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000544 // in case we need a temporary, trimmed copy of the src pixels
545 SkAutoSMalloc<128 * 128> tempStorage;
546
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000547 bool useTexStorage = isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000548 this->glCaps().texStorageSupport();
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000549 if (useTexStorage) {
550 if (kDesktop_GrGLBinding == this->glBinding()) {
551 // 565 is not a sized internal format on desktop GL. So on desktop
552 // with 565 we always use an unsized internal format to let the
553 // system pick the best sized format to convert the 565 data to.
554 // Since glTexStorage only allows sized internal formats we will
555 // instead fallback to glTexImage2D.
556 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
557 } else {
558 // ES doesn't allow paletted textures to be used with tex storage
559 useTexStorage = desc.fConfig != kIndex_8_GrPixelConfig;
560 }
561 }
562
563 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000564 GrGLenum externalFormat;
565 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000566 // glTexStorage requires sized internal formats on both desktop and ES. ES
567 // glTexImage requires an unsized format.
568 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
569 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000570 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000571 }
572
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000573 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000574 // paletted textures cannot be updated
575 return false;
576 }
577
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000578 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000579 * check whether to allocate a temporary buffer for flipping y or
580 * because our srcData has extra bytes past each row. If so, we need
581 * to trim those off here, since GL ES may not let us specify
582 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000583 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000584 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000585 bool swFlipY = false;
586 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000587 if (NULL != data) {
588 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000589 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000590 glFlipY = true;
591 } else {
592 swFlipY = true;
593 }
594 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000595 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000596 // can't use this for flipping, only non-neg values allowed. :(
597 if (rowBytes != trimRowBytes) {
598 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
599 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
600 restoreGLRowLength = true;
601 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000602 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000603 if (trimRowBytes != rowBytes || swFlipY) {
604 // copy data into our new storage, skipping the trailing bytes
605 size_t trimSize = height * trimRowBytes;
606 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000607 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000608 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000609 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000610 char* dst = (char*)tempStorage.reset(trimSize);
611 for (int y = 0; y < height; y++) {
612 memcpy(dst, src, trimRowBytes);
613 if (swFlipY) {
614 src -= rowBytes;
615 } else {
616 src += rowBytes;
617 }
618 dst += trimRowBytes;
619 }
620 // now point data to our copied version
621 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000622 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000623 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000624 if (glFlipY) {
625 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
626 }
627 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 bool succeeded = true;
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000630 if (isNewTexture &&
631 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000632 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000633 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000634 if (useTexStorage) {
635 // We never resize or change formats of textures. We don't use
636 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000637 GL_ALLOC_CALL(this->glInterface(),
638 TexStorage2D(GR_GL_TEXTURE_2D,
639 1, // levels
640 internalFormat,
641 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000642 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000643 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
644 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
645 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000646 GL_ALLOC_CALL(this->glInterface(),
647 CompressedTexImage2D(GR_GL_TEXTURE_2D,
648 0, // level
649 internalFormat,
650 desc.fWidth, desc.fHeight,
651 0, // border
652 imageSize,
653 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000654 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000655 GL_ALLOC_CALL(this->glInterface(),
656 TexImage2D(GR_GL_TEXTURE_2D,
657 0, // level
658 internalFormat,
659 desc.fWidth, desc.fHeight,
660 0, // border
661 externalFormat, externalType,
662 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000663 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000664 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000665 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000666 if (error != GR_GL_NO_ERROR) {
667 succeeded = false;
668 } else {
669 // if we have data and we used TexStorage to create the texture, we
670 // now upload with TexSubImage.
671 if (NULL != data && useTexStorage) {
672 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
673 0, // level
674 left, top,
675 width, height,
676 externalFormat, externalType,
677 data));
678 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000679 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000680 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000681 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000682 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000683 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000684 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
685 0, // level
686 left, top,
687 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000688 externalFormat, externalType, data));
689 }
690
691 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000692 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000693 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000694 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000695 if (glFlipY) {
696 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
697 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000698 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000699}
700
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000701bool GrGpuGL::createRenderTargetObjects(int width, int height,
702 GrGLuint texID,
703 GrGLRenderTarget::Desc* desc) {
704 desc->fMSColorRenderbufferID = 0;
705 desc->fRTFBOID = 0;
706 desc->fTexFBOID = 0;
707 desc->fOwnIDs = true;
708
709 GrGLenum status;
710 GrGLint err;
711
bsalomon@google.comab15d612011-08-09 12:57:56 +0000712 GrGLenum msColorFormat = 0; // suppress warning
713
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000714 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000715 if (!desc->fTexFBOID) {
716 goto FAILED;
717 }
718
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000719
720 // If we are using multisampling we will create two FBOS. We render
721 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000722 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000723 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000724 goto FAILED;
725 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000726 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
727 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000728 if (!desc->fRTFBOID ||
729 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000730 !this->configToGLFormats(desc->fConfig,
731 // GLES requires sized internal formats
732 kES2_GrGLBinding == this->glBinding(),
733 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000734 goto FAILED;
735 }
736 } else {
737 desc->fRTFBOID = desc->fTexFBOID;
738 }
739
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000740 // below here we may bind the FBO
741 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000742 if (desc->fRTFBOID != desc->fTexFBOID) {
743 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000744 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000745 desc->fMSColorRenderbufferID));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000746 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
747 GL_ALLOC_CALL(this->glInterface(),
748 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
749 desc->fSampleCnt,
750 msColorFormat,
751 width, height));
752 err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000753 if (err != GR_GL_NO_ERROR) {
754 goto FAILED;
755 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000756 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
757 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000758 GR_GL_COLOR_ATTACHMENT0,
759 GR_GL_RENDERBUFFER,
760 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000761 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000762 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
763 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
764 goto FAILED;
765 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000766 fGLContextInfo.caps().markConfigAsValidColorAttachment(
767 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000768 }
769 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000770 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000771
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000772 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
773 GR_GL_COLOR_ATTACHMENT0,
774 GR_GL_TEXTURE_2D,
775 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000776 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000777 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
778 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
779 goto FAILED;
780 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000781 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000782 }
783
784 return true;
785
786FAILED:
787 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000788 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000789 }
790 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000791 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000792 }
793 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000794 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000795 }
796 return false;
797}
798
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000799// good to set a break-point here to know when createTexture fails
800static GrTexture* return_null_texture() {
801// GrAssert(!"null texture");
802 return NULL;
803}
804
805#if GR_DEBUG
806static size_t as_size_t(int x) {
807 return x;
808}
809#endif
810
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000811GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000812 const void* srcData,
813 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000814
815#if GR_COLLECT_STATS
816 ++fStats.fTextureCreateCnt;
817#endif
reed@google.com1fcd51e2011-01-05 15:50:27 +0000818
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000819 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000820 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000821
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000822 // Attempt to catch un- or wrongly initialized sample counts;
823 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
824
bsalomon@google.com99621082011-11-15 16:47:16 +0000825 glTexDesc.fWidth = desc.fWidth;
826 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000827 glTexDesc.fConfig = desc.fConfig;
828 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000829
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000830 glRTDesc.fMSColorRenderbufferID = 0;
831 glRTDesc.fRTFBOID = 0;
832 glRTDesc.fTexFBOID = 0;
833 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000834 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000835
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000836 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000837
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000838 const Caps& caps = this->getCaps();
839
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000840 // We keep GrRenderTargets in GL's normal orientation so that they
841 // can be drawn to by the outside world without the client having
842 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000843 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000844 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000845
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000846 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000847 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000848 desc.fSampleCnt) {
849 GrPrintf("MSAA RT requested but not supported on this platform.");
reed@google.comac10a2d2010-12-22 21:39:39 +0000850 }
851
reed@google.comac10a2d2010-12-22 21:39:39 +0000852 if (renderTarget) {
bsalomon@google.com99621082011-11-15 16:47:16 +0000853 if (glTexDesc.fWidth > caps.fMaxRenderTargetSize ||
854 glTexDesc.fHeight > caps.fMaxRenderTargetSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000855 return return_null_texture();
856 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000857 }
858
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000859 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000860 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000861 // provides a hint about how this texture will be used
862 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
863 GR_GL_TEXTURE_USAGE,
864 GR_GL_FRAMEBUFFER_ATTACHMENT));
865 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000866 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000867 return return_null_texture();
868 }
869
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000870 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000871 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000872
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000873 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
874 // drivers have a bug where an FBO won't be complete if it includes a
875 // texture that is not mipmap complete (considering the filter in use).
876 GrGLTexture::TexParams initialTexParams;
877 // we only set a subset here so invalidate first
878 initialTexParams.invalidate();
879 initialTexParams.fFilter = GR_GL_NEAREST;
880 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
881 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000882 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
883 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000884 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000885 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
886 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000887 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000888 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
889 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000890 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
892 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000893 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000894 if (!this->uploadTexData(glTexDesc, true, 0, 0,
895 glTexDesc.fWidth, glTexDesc.fHeight,
896 desc.fConfig, srcData, rowBytes)) {
897 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
898 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000899 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000900
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000901 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000902 if (renderTarget) {
903#if GR_COLLECT_STATS
904 ++fStats.fRenderTargetCreateCnt;
905#endif
bsalomon@google.com99621082011-11-15 16:47:16 +0000906 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
907 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 glTexDesc.fTextureID,
909 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000910 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000911 return return_null_texture();
912 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000913 tex = new GrGLTexture(this, glTexDesc, glRTDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000914 } else {
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000915 tex = new GrGLTexture(this, glTexDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000916 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000917 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000918#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000919 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
920 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000921#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 return tex;
923}
924
925namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000926
927const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
928
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000929void inline get_stencil_rb_sizes(const GrGLInterface* gl,
930 GrGLuint rb,
931 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000932 // we shouldn't ever know one size and not the other
933 GrAssert((kUnknownBitCount == format->fStencilBits) ==
934 (kUnknownBitCount == format->fTotalBits));
935 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000936 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000937 GR_GL_RENDERBUFFER_STENCIL_SIZE,
938 (GrGLint*)&format->fStencilBits);
939 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000940 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000941 GR_GL_RENDERBUFFER_DEPTH_SIZE,
942 (GrGLint*)&format->fTotalBits);
943 format->fTotalBits += format->fStencilBits;
944 } else {
945 format->fTotalBits = format->fStencilBits;
946 }
947 }
948}
949}
950
951bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
952 int width, int height) {
953
954 // All internally created RTs are also textures. We don't create
955 // SBs for a client's standalone RT (that is RT that isnt also a texture).
956 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +0000957 GrAssert(width >= rt->width());
958 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000959
960 int samples = rt->numSamples();
961 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000962 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000963 if (!sbID) {
964 return false;
965 }
966
967 GrGLStencilBuffer* sb = NULL;
968
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000969 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000970 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000971 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000972 // we start with the last stencil format that succeeded in hopes
973 // that we won't go through this loop more than once after the
974 // first (painful) stencil creation.
975 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000976 const GrGLCaps::StencilFormat& sFmt =
977 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000978 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000979 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000980 // version on a GL that doesn't have an MSAA extension.
981 if (samples > 1) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000982 GL_ALLOC_CALL(this->glInterface(),
983 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
984 samples,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000985 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000986 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000988 GL_ALLOC_CALL(this->glInterface(),
989 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000990 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000991 width, height));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000992 }
993
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000994 GrGLenum err = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 if (err == GR_GL_NO_ERROR) {
996 // After sized formats we attempt an unsized format and take whatever
997 // sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000998 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001000 sb = new GrGLStencilBuffer(this, sbID, width, height,
1001 samples, format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1003 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001004 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001005 sb->unref();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 return true;
1007 }
1008 sb->abandon(); // otherwise we lose sbID
1009 sb->unref();
1010 }
1011 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001012 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001013 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014}
1015
1016bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1017 GrRenderTarget* rt) {
1018 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1019
1020 GrGLuint fbo = glrt->renderFBOID();
1021
1022 if (NULL == sb) {
1023 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001025 GR_GL_STENCIL_ATTACHMENT,
1026 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 GR_GL_DEPTH_ATTACHMENT,
1029 GR_GL_RENDERBUFFER, 0));
1030#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001031 GrGLenum status;
1032 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1034#endif
1035 }
1036 return true;
1037 } else {
1038 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1039 GrGLuint rb = glsb->renderbufferID();
1040
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001041 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1043 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001044 GR_GL_STENCIL_ATTACHMENT,
1045 GR_GL_RENDERBUFFER, rb));
1046 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001047 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001048 GR_GL_DEPTH_ATTACHMENT,
1049 GR_GL_RENDERBUFFER, rb));
1050 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001051 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001052 GR_GL_DEPTH_ATTACHMENT,
1053 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001055
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001056 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001057 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001058 glsb->format())) {
1059 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1060 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001061 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001062 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001064 if (glsb->format().fPacked) {
1065 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1066 GR_GL_DEPTH_ATTACHMENT,
1067 GR_GL_RENDERBUFFER, 0));
1068 }
1069 return false;
1070 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001071 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001072 rt->config(),
1073 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001076 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001078}
1079
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001080////////////////////////////////////////////////////////////////////////////////
1081
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001082GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001083 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001086 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001087 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001088 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001090 GL_ALLOC_CALL(this->glInterface(),
1091 BufferData(GR_GL_ARRAY_BUFFER,
1092 size,
1093 NULL, // data ptr
1094 dynamic ? GR_GL_DYNAMIC_DRAW :
1095 GR_GL_STATIC_DRAW));
1096 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001097 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 // deleting bound buffer does implicit bind to 0
1099 fHWGeometryState.fVertexBuffer = NULL;
1100 return NULL;
1101 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001102 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 size, dynamic);
1104 fHWGeometryState.fVertexBuffer = vertexBuffer;
1105 return vertexBuffer;
1106 }
1107 return NULL;
1108}
1109
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001110GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001111 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001113 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001115 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001116 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001117 GL_ALLOC_CALL(this->glInterface(),
1118 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1119 size,
1120 NULL, // data ptr
1121 dynamic ? GR_GL_DYNAMIC_DRAW :
1122 GR_GL_STATIC_DRAW));
1123 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001125 // deleting bound buffer does implicit bind to 0
1126 fHWGeometryState.fIndexBuffer = NULL;
1127 return NULL;
1128 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001129 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, id,
reed@google.comac10a2d2010-12-22 21:39:39 +00001130 size, dynamic);
1131 fHWGeometryState.fIndexBuffer = indexBuffer;
1132 return indexBuffer;
1133 }
1134 return NULL;
1135}
1136
reed@google.comac10a2d2010-12-22 21:39:39 +00001137void GrGpuGL::flushScissor(const GrIRect* rect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001138 const GrDrawState& drawState = this->getDrawState();
1139 const GrGLRenderTarget* rt =
1140 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1141
1142 GrAssert(NULL != rt);
1143 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001144
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001145 GrGLIRect scissor;
1146 if (NULL != rect) {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001147 scissor.setRelativeTo(vp, rect->fLeft, rect->fTop,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001148 rect->width(), rect->height());
1149 if (scissor.contains(vp)) {
1150 rect = NULL;
1151 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 }
1153
1154 if (NULL != rect) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 if (fHWBounds.fScissorRect != scissor) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001156 scissor.pushToGLScissor(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001157 fHWBounds.fScissorRect = scissor;
1158 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001159 if (!fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 fHWBounds.fScissorEnabled = true;
1162 }
1163 } else {
1164 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001165 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001166 fHWBounds.fScissorEnabled = false;
1167 }
1168 }
1169}
1170
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001171void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001172 const GrDrawState& drawState = this->getDrawState();
1173 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001174 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001175 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001176
bsalomon@google.com74b98712011-11-11 19:46:16 +00001177 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001178 if (NULL != rect) {
1179 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001180 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001181 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001182 if (clippedRect.intersect(rtRect)) {
1183 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001184 } else {
1185 return;
1186 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001188 this->flushRenderTarget(rect);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001189 this->flushScissor(rect);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001190
1191 GrGLfloat r, g, b, a;
1192 static const GrGLfloat scale255 = 1.f / 255.f;
1193 a = GrColorUnpackA(color) * scale255;
1194 GrGLfloat scaleRGB = scale255;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001195 if (GrPixelConfigIsUnpremultiplied(rt->config())) {
bsalomon@google.com74b98712011-11-11 19:46:16 +00001196 scaleRGB *= a;
1197 }
1198 r = GrColorUnpackR(color) * scaleRGB;
1199 g = GrColorUnpackG(color) * scaleRGB;
1200 b = GrColorUnpackB(color) * scaleRGB;
1201
1202 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001203 fHWDrawState.disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001204 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001205 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001206}
1207
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001208void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001209 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001210 return;
1211 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001212
1213 this->flushRenderTarget(&GrIRect::EmptyIRect());
1214
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 if (fHWBounds.fScissorEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001217 fHWBounds.fScissorEnabled = false;
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(StencilMask(0xffffffff));
1220 GL_CALL(ClearStencil(0));
1221 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001222 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001223}
1224
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001225void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001226 const GrDrawState& drawState = this->getDrawState();
1227 const GrRenderTarget* rt = drawState.getRenderTarget();
1228 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229
1230 // this should only be called internally when we know we have a
1231 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001232 GrAssert(NULL != rt->getStencilBuffer());
1233 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001234#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001236 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001237#else
1238 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001240 // turned into draws. Our contract on GrDrawTarget says that
1241 // changing the clip between stencil passes may or may not
1242 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001243 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001244#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001245 GrGLint value;
1246 if (insideClip) {
1247 value = (1 << (stencilBitCount - 1));
1248 } else {
1249 value = 0;
1250 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001251 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 this->flushScissor(&rect);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(StencilMask(clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001254 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001255 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001256 fHWDrawState.stencil()->invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001257}
1258
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001259void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001260 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001261}
1262
bsalomon@google.comc4364992011-11-07 15:54:49 +00001263bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1264 int left, int top,
1265 int width, int height,
1266 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001267 size_t rowBytes) const {
1268 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001269 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001270 return false;
1271 }
1272
1273 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001274 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001275 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001276 return true;
1277 }
1278 // If we have to do memcpys to handle rowBytes then y-flip is free
1279 // Note the rowBytes might be tight to the passed in data, but if data
1280 // gets clipped in x to the target the rowBytes will no longer be tight.
1281 if (left >= 0 && (left + width) < renderTarget->width()) {
1282 return 0 == rowBytes ||
1283 GrBytesPerPixel(config) * width == rowBytes;
1284 } else {
1285 return false;
1286 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001287}
1288
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001289bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001290 int left, int top,
1291 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001292 GrPixelConfig config,
1293 void* buffer,
1294 size_t rowBytes,
1295 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001296 GrGLenum format;
1297 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001298 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001300 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001301 size_t bpp = GrBytesPerPixel(config);
1302 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1303 &left, &top, &width, &height,
1304 const_cast<const void**>(&buffer),
1305 &rowBytes)) {
1306 return false;
1307 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001308
bsalomon@google.comc6980972011-11-02 19:57:21 +00001309 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001310 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001311 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001312 switch (tgt->getResolveType()) {
1313 case GrGLRenderTarget::kCantResolve_ResolveType:
1314 return false;
1315 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001316 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001317 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001318 break;
1319 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001320 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001321 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1323 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001324 break;
1325 default:
1326 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 }
1328
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001329 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001330
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001331 // the read rect is viewport-relative
1332 GrGLIRect readRect;
1333 readRect.setRelativeTo(glvp, left, top, width, height);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001334
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001335 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001336 if (0 == rowBytes) {
1337 rowBytes = tightRowBytes;
1338 }
1339 size_t readDstRowBytes = tightRowBytes;
1340 void* readDst = buffer;
1341
1342 // determine if GL can read using the passed rowBytes or if we need
1343 // a scratch buffer.
1344 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1345 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001346 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001347 GrAssert(!(rowBytes % sizeof(GrColor)));
1348 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1349 readDstRowBytes = rowBytes;
1350 } else {
1351 scratch.reset(tightRowBytes * height);
1352 readDst = scratch.get();
1353 }
1354 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001355 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001356 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1357 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001358 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1359 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001360 format, type, readDst));
1361 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001362 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001363 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1364 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001365 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001366 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1367 invertY = true;
1368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001369
1370 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001371 // API presents top-to-bottom. We must preserve the padding contents. Note
1372 // that the above readPixels did not overwrite the padding.
1373 if (readDst == buffer) {
1374 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001375 if (!invertY) {
1376 scratch.reset(tightRowBytes);
1377 void* tmpRow = scratch.get();
1378 // flip y in-place by rows
1379 const int halfY = height >> 1;
1380 char* top = reinterpret_cast<char*>(buffer);
1381 char* bottom = top + (height - 1) * rowBytes;
1382 for (int y = 0; y < halfY; y++) {
1383 memcpy(tmpRow, top, tightRowBytes);
1384 memcpy(top, bottom, tightRowBytes);
1385 memcpy(bottom, tmpRow, tightRowBytes);
1386 top += rowBytes;
1387 bottom -= rowBytes;
1388 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001389 }
1390 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001391 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001392 // copy from readDst to buffer while flipping y
1393 const int halfY = height >> 1;
1394 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001395 char* dst = reinterpret_cast<char*>(buffer);
1396 if (!invertY) {
1397 dst += (height-1) * rowBytes;
1398 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001399 for (int y = 0; y < height; y++) {
1400 memcpy(dst, src, tightRowBytes);
1401 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001402 if (invertY) {
1403 dst += rowBytes;
1404 } else {
1405 dst -= rowBytes;
1406 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001407 }
1408 }
1409 return true;
1410}
1411
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001412void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001413
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001414 GrGLRenderTarget* rt =
1415 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1416 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001417
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001418 if (fHWDrawState.getRenderTarget() != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001419 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 #if GR_COLLECT_STATS
1421 ++fStats.fRenderTargetChngCnt;
1422 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001423 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001424 GrGLenum status;
1425 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001426 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001427 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 }
1429 #endif
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001430 fDirtyFlags.fRenderTargetChanged = true;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 fHWDrawState.setRenderTarget(rt);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001432 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com649a8622011-03-10 14:53:38 +00001433 if (fHWBounds.fViewportRect != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001434 vp.pushToGLViewport(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 fHWBounds.fViewportRect = vp;
1436 }
1437 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001438 if (NULL == bound || !bound->isEmpty()) {
1439 rt->flagAsNeedingResolve(bound);
1440 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001441}
1442
twiz@google.com0f31ca72011-03-18 17:38:11 +00001443GrGLenum gPrimitiveType2GLMode[] = {
1444 GR_GL_TRIANGLES,
1445 GR_GL_TRIANGLE_STRIP,
1446 GR_GL_TRIANGLE_FAN,
1447 GR_GL_POINTS,
1448 GR_GL_LINES,
1449 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001450};
1451
bsalomon@google.comd302f142011-03-03 13:54:13 +00001452#define SWAP_PER_DRAW 0
1453
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001454#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001455 #if GR_MAC_BUILD
1456 #include <AGL/agl.h>
1457 #elif GR_WIN32_BUILD
1458 void SwapBuf() {
1459 DWORD procID = GetCurrentProcessId();
1460 HWND hwnd = GetTopWindow(GetDesktopWindow());
1461 while(hwnd) {
1462 DWORD wndProcID = 0;
1463 GetWindowThreadProcessId(hwnd, &wndProcID);
1464 if(wndProcID == procID) {
1465 SwapBuffers(GetDC(hwnd));
1466 }
1467 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1468 }
1469 }
1470 #endif
1471#endif
1472
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001473void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1474 uint32_t startVertex,
1475 uint32_t startIndex,
1476 uint32_t vertexCount,
1477 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1479
twiz@google.com0f31ca72011-03-18 17:38:11 +00001480 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001481
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001482 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1483 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1484
1485 // our setupGeometry better have adjusted this to zero since
1486 // DrawElements always draws from the begining of the arrays for idx 0.
1487 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001488
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001489 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1490 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001491#if SWAP_PER_DRAW
1492 glFlush();
1493 #if GR_MAC_BUILD
1494 aglSwapBuffers(aglGetCurrentContext());
1495 int set_a_break_pt_here = 9;
1496 aglSwapBuffers(aglGetCurrentContext());
1497 #elif GR_WIN32_BUILD
1498 SwapBuf();
1499 int set_a_break_pt_here = 9;
1500 SwapBuf();
1501 #endif
1502#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001503}
1504
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001505void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1506 uint32_t startVertex,
1507 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1509
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001510 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1511
1512 // our setupGeometry better have adjusted this to zero.
1513 // DrawElements doesn't take an offset so we always adjus the startVertex.
1514 GrAssert(0 == startVertex);
1515
1516 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1517 // account for startVertex in the DrawElements case. So we always
1518 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001519 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001520#if SWAP_PER_DRAW
1521 glFlush();
1522 #if GR_MAC_BUILD
1523 aglSwapBuffers(aglGetCurrentContext());
1524 int set_a_break_pt_here = 9;
1525 aglSwapBuffers(aglGetCurrentContext());
1526 #elif GR_WIN32_BUILD
1527 SwapBuf();
1528 int set_a_break_pt_here = 9;
1529 SwapBuf();
1530 #endif
1531#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001532}
1533
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001534void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1535
1536 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001537
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001538 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001539 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001541 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1542 rt->renderFBOID()));
1543 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1544 rt->textureFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001545 #if GR_COLLECT_STATS
1546 ++fStats.fRenderTargetChngCnt;
1547 #endif
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001548 // make sure we go through flushRenderTarget() since we've modified
1549 // the bound DRAW FBO ID.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001550 fHWDrawState.setRenderTarget(NULL);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001551 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001552 const GrIRect dirtyRect = rt->getResolveRect();
1553 GrGLIRect r;
1554 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1555 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001556
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001557 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001558 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001559 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1560 GL_CALL(Scissor(r.fLeft, r.fBottom,
1561 r.fWidth, r.fHeight));
1562 GL_CALL(ResolveMultisampleFramebuffer());
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001563 fHWBounds.fScissorRect.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 fHWBounds.fScissorEnabled = true;
1565 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001566 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001567 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001568 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1569 this->glCaps().msFBOType());
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001570 this->flushScissor(NULL);
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001571 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001572 int right = r.fLeft + r.fWidth;
1573 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001574 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1575 r.fLeft, r.fBottom, right, top,
1576 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001577 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001578 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001579 }
1580}
1581
twiz@google.com0f31ca72011-03-18 17:38:11 +00001582static const GrGLenum grToGLStencilFunc[] = {
1583 GR_GL_ALWAYS, // kAlways_StencilFunc
1584 GR_GL_NEVER, // kNever_StencilFunc
1585 GR_GL_GREATER, // kGreater_StencilFunc
1586 GR_GL_GEQUAL, // kGEqual_StencilFunc
1587 GR_GL_LESS, // kLess_StencilFunc
1588 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1589 GR_GL_EQUAL, // kEqual_StencilFunc,
1590 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
bsalomon@google.comd302f142011-03-03 13:54:13 +00001591};
1592GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilFunc) == kBasicStencilFuncCount);
1593GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1594GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1595GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1596GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1597GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1598GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1599GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1600GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1601
twiz@google.com0f31ca72011-03-18 17:38:11 +00001602static const GrGLenum grToGLStencilOp[] = {
1603 GR_GL_KEEP, // kKeep_StencilOp
1604 GR_GL_REPLACE, // kReplace_StencilOp
1605 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1606 GR_GL_INCR, // kIncClamp_StencilOp
1607 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1608 GR_GL_DECR, // kDecClamp_StencilOp
1609 GR_GL_ZERO, // kZero_StencilOp
1610 GR_GL_INVERT, // kInvert_StencilOp
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611};
1612GR_STATIC_ASSERT(GR_ARRAY_COUNT(grToGLStencilOp) == kStencilOpCount);
1613GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1614GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1615GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1616GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1617GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1618GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1619GR_STATIC_ASSERT(6 == kZero_StencilOp);
1620GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1621
reed@google.comac10a2d2010-12-22 21:39:39 +00001622void GrGpuGL::flushStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001623 const GrDrawState& drawState = this->getDrawState();
1624
1625 const GrStencilSettings* settings = &drawState.getStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00001626
1627 // use stencil for clipping if clipping is enabled and the clip
1628 // has been written into the stencil.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001629 bool stencilClip = fClipInStencil && drawState.isClipState();
1630 bool drawClipToStencil =
1631 drawState.isStateFlagEnabled(kModifyStencilClip_StateBit);
bsalomon@google.com39dab772012-01-03 19:39:31 +00001632 bool stencilChange = (fHWDrawState.getStencil() != *settings) ||
1633 (fHWStencilClip != stencilClip) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001634 (fHWDrawState.isStateFlagEnabled(kModifyStencilClip_StateBit) !=
1635 drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001636
1637 if (stencilChange) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001638
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001639 // we can't simultaneously perform stencil-clipping and
1640 // modify the stencil clip
1641 GrAssert(!stencilClip || !drawClipToStencil);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642
bsalomon@google.comd302f142011-03-03 13:54:13 +00001643 if (settings->isDisabled()) {
1644 if (stencilClip) {
digit@google.com9b482c42012-02-16 22:03:26 +00001645 settings = GetClipStencilSettings();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001648
1649 if (settings->isDisabled()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001650 GL_CALL(Disable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001651 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001652 GL_CALL(Enable(GR_GL_STENCIL_TEST));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001653 #if GR_DEBUG
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001654 if (!this->getCaps().fStencilWrapOpsSupport) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001655 GrAssert(settings->frontPassOp() != kIncWrap_StencilOp);
1656 GrAssert(settings->frontPassOp() != kDecWrap_StencilOp);
1657 GrAssert(settings->frontFailOp() != kIncWrap_StencilOp);
1658 GrAssert(settings->backFailOp() != kDecWrap_StencilOp);
1659 GrAssert(settings->backPassOp() != kIncWrap_StencilOp);
1660 GrAssert(settings->backPassOp() != kDecWrap_StencilOp);
1661 GrAssert(settings->backFailOp() != kIncWrap_StencilOp);
1662 GrAssert(settings->frontFailOp() != kDecWrap_StencilOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001663 }
1664 #endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001665 int stencilBits = 0;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001666 GrStencilBuffer* stencilBuffer =
1667 drawState.getRenderTarget()->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001668 if (NULL != stencilBuffer) {
1669 stencilBits = stencilBuffer->bits();
1670 }
1671 // TODO: dynamically attach a stencil buffer
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001672 GrAssert(stencilBits || settings->isDisabled());
bsalomon@google.com2cdfade2011-11-23 16:53:42 +00001673
1674 GrGLuint clipStencilMask = 0;
1675 GrGLuint userStencilMask = ~0;
1676 if (stencilBits > 0) {
1677 clipStencilMask = 1 << (stencilBits - 1);
1678 userStencilMask = clipStencilMask - 1;
1679 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001680
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001681 unsigned int frontRef = settings->frontFuncRef();
1682 unsigned int frontMask = settings->frontFuncMask();
1683 unsigned int frontWriteMask = settings->frontWriteMask();
twiz@google.com0f31ca72011-03-18 17:38:11 +00001684 GrGLenum frontFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001685
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001686 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001687 GrAssert(settings->frontFunc() < kBasicStencilFuncCount);
1688 frontFunc = grToGLStencilFunc[settings->frontFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001689 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001690 frontFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001691 stencilClip, settings->frontFunc())];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001692
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001693 ConvertStencilFuncAndMask(settings->frontFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001694 stencilClip,
1695 clipStencilMask,
1696 userStencilMask,
1697 &frontRef,
1698 &frontMask);
1699 frontWriteMask &= userStencilMask;
1700 }
tomhudson@google.com62b09682011-11-09 16:39:17 +00001701 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001702 settings->frontFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001703 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001704 settings->frontPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001705 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001706 settings->backFailOp() < GR_ARRAY_COUNT(grToGLStencilOp));
tomhudson@google.com62b09682011-11-09 16:39:17 +00001707 GrAssert((size_t)
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001708 settings->backPassOp() < GR_ARRAY_COUNT(grToGLStencilOp));
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001709 if (this->getCaps().fTwoSidedStencilSupport) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001710 GrGLenum backFunc;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001711
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001712 unsigned int backRef = settings->backFuncRef();
1713 unsigned int backMask = settings->backFuncMask();
1714 unsigned int backWriteMask = settings->backWriteMask();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001715
1716
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001717 if (drawClipToStencil) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001718 GrAssert(settings->backFunc() < kBasicStencilFuncCount);
1719 backFunc = grToGLStencilFunc[settings->backFunc()];
bsalomon@google.comd302f142011-03-03 13:54:13 +00001720 } else {
tomhudson@google.com62b09682011-11-09 16:39:17 +00001721 backFunc = grToGLStencilFunc[ConvertStencilFunc(
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001722 stencilClip, settings->backFunc())];
1723 ConvertStencilFuncAndMask(settings->backFunc(),
bsalomon@google.comd302f142011-03-03 13:54:13 +00001724 stencilClip,
1725 clipStencilMask,
1726 userStencilMask,
1727 &backRef,
1728 &backMask);
1729 backWriteMask &= userStencilMask;
1730 }
1731
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001732 GL_CALL(StencilFuncSeparate(GR_GL_FRONT, frontFunc,
1733 frontRef, frontMask));
1734 GL_CALL(StencilMaskSeparate(GR_GL_FRONT, frontWriteMask));
1735 GL_CALL(StencilFuncSeparate(GR_GL_BACK, backFunc,
1736 backRef, backMask));
1737 GL_CALL(StencilMaskSeparate(GR_GL_BACK, backWriteMask));
1738 GL_CALL(StencilOpSeparate(GR_GL_FRONT,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001739 grToGLStencilOp[settings->frontFailOp()],
1740 grToGLStencilOp[settings->frontPassOp()],
1741 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001742
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001743 GL_CALL(StencilOpSeparate(GR_GL_BACK,
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001744 grToGLStencilOp[settings->backFailOp()],
1745 grToGLStencilOp[settings->backPassOp()],
1746 grToGLStencilOp[settings->backPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001748 GL_CALL(StencilFunc(frontFunc, frontRef, frontMask));
1749 GL_CALL(StencilMask(frontWriteMask));
bsalomon@google.com6b2445e2011-12-15 19:47:46 +00001750 GL_CALL(StencilOp(grToGLStencilOp[settings->frontFailOp()],
1751 grToGLStencilOp[settings->frontPassOp()],
1752 grToGLStencilOp[settings->frontPassOp()]));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001753 }
1754 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001755 *fHWDrawState.stencil() = *settings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 fHWStencilClip = stencilClip;
1757 }
1758}
1759
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001760void GrGpuGL::flushAAState(GrPrimitiveType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001761 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001762 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001763 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1764 // smooth lines.
1765
1766 // we prefer smooth lines over multisampled lines
1767 // msaa should be disabled if drawing smooth lines.
bsalomon@google.com0650e812011-04-08 18:07:53 +00001768 if (GrIsPrimTypeLines(type)) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001769 bool smooth = this->willUseHWAALines();
bsalomon@google.com0650e812011-04-08 18:07:53 +00001770 if (!fHWAAState.fSmoothLineEnabled && smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001771 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001772 fHWAAState.fSmoothLineEnabled = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001773 } else if (fHWAAState.fSmoothLineEnabled && !smooth) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001774 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001775 fHWAAState.fSmoothLineEnabled = false;
1776 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001777 if (rt->isMultisampled() &&
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001778 fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001779 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001780 fHWAAState.fMSAAEnabled = false;
1781 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001782 } else if (rt->isMultisampled() &&
1783 this->getDrawState().isHWAntialiasState() !=
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001784 fHWAAState.fMSAAEnabled) {
1785 if (fHWAAState.fMSAAEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001786 GL_CALL(Disable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001787 fHWAAState.fMSAAEnabled = false;
1788 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001789 GL_CALL(Enable(GR_GL_MULTISAMPLE));
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001790 fHWAAState.fMSAAEnabled = true;
1791 }
1792 }
1793 }
1794}
1795
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001796void GrGpuGL::flushBlend(GrPrimitiveType type,
1797 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001798 GrBlendCoeff dstCoeff) {
bsalomon@google.com471d4712011-08-23 15:45:25 +00001799 if (GrIsPrimTypeLines(type) && this->willUseHWAALines()) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001800 if (fHWBlendDisabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001801 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001802 fHWBlendDisabled = false;
1803 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001804 if (kSA_BlendCoeff != fHWDrawState.getSrcBlendCoeff() ||
1805 kISA_BlendCoeff != fHWDrawState.getDstBlendCoeff()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001806 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_BlendCoeff],
1807 gXfermodeCoeff2Blend[kISA_BlendCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001808 fHWDrawState.setBlendFunc(kSA_BlendCoeff, kISA_BlendCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001809 }
1810 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001811 // any optimization to disable blending should
1812 // have already been applied and tweaked the coeffs
1813 // to (1, 0).
1814 bool blendOff = kOne_BlendCoeff == srcCoeff &&
1815 kZero_BlendCoeff == dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001816 if (fHWBlendDisabled != blendOff) {
1817 if (blendOff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001818 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001819 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001820 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.com0650e812011-04-08 18:07:53 +00001821 }
1822 fHWBlendDisabled = blendOff;
1823 }
1824 if (!blendOff) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001825 if (fHWDrawState.getSrcBlendCoeff() != srcCoeff ||
1826 fHWDrawState.getDstBlendCoeff() != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001827 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1828 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001829 fHWDrawState.setBlendFunc(srcCoeff, dstCoeff);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001830 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001831 GrColor blendConst = fCurrDrawState.getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001832 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1833 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001834 fHWDrawState.getBlendConstant() != blendConst) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001835
1836 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001837 GrColorUnpackR(blendConst) / 255.f,
1838 GrColorUnpackG(blendConst) / 255.f,
1839 GrColorUnpackB(blendConst) / 255.f,
1840 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001841 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001842 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001843 fHWDrawState.setBlendConstant(blendConst);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001844 }
1845 }
1846 }
1847}
1848
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001849namespace {
1850
1851unsigned gr_to_gl_filter(GrSamplerState::Filter filter) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001852 switch (filter) {
1853 case GrSamplerState::kBilinear_Filter:
1854 case GrSamplerState::k4x4Downsample_Filter:
1855 return GR_GL_LINEAR;
1856 case GrSamplerState::kNearest_Filter:
1857 case GrSamplerState::kConvolution_Filter:
1858 return GR_GL_NEAREST;
1859 default:
1860 GrAssert(!"Unknown filter type");
1861 return GR_GL_LINEAR;
1862 }
1863}
1864
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001865const GrGLenum* get_swizzle(GrPixelConfig config,
1866 const GrSamplerState& sampler) {
1867 if (GrPixelConfigIsAlphaOnly(config)) {
1868 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
1869 GR_GL_ALPHA, GR_GL_ALPHA };
1870 return gAlphaSmear;
1871 } else if (sampler.swapsRAndB()) {
1872 static const GrGLenum gRedBlueSwap[] = { GR_GL_BLUE, GR_GL_GREEN,
1873 GR_GL_RED, GR_GL_ALPHA };
1874 return gRedBlueSwap;
1875 } else {
1876 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
1877 GR_GL_BLUE, GR_GL_ALPHA };
1878 return gStraight;
1879 }
1880}
1881
1882void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
1883 // should add texparameteri to interface to make 1 instead of 4 calls here
1884 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1885 GR_GL_TEXTURE_SWIZZLE_R,
1886 swizzle[0]));
1887 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1888 GR_GL_TEXTURE_SWIZZLE_G,
1889 swizzle[1]));
1890 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1891 GR_GL_TEXTURE_SWIZZLE_B,
1892 swizzle[2]));
1893 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
1894 GR_GL_TEXTURE_SWIZZLE_A,
1895 swizzle[3]));
1896}
1897}
1898
bsalomon@google.comffca4002011-02-22 20:34:01 +00001899bool GrGpuGL::flushGLStateCommon(GrPrimitiveType type) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001900
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001901 GrDrawState* drawState = this->drawState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001902 // GrGpu::setupClipAndFlushState should have already checked this
1903 // and bailed if not true.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001904 GrAssert(NULL != drawState->getRenderTarget());
reed@google.comac10a2d2010-12-22 21:39:39 +00001905
tomhudson@google.com93813632011-10-27 20:21:16 +00001906 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00001907 // bind texture and set sampler state
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001908 if (this->isStageEnabled(s)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001909 GrGLTexture* nextTexture =
1910 static_cast<GrGLTexture*>(drawState->getTexture(s));
reed@google.comac10a2d2010-12-22 21:39:39 +00001911
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001912 // true for now, but maybe not with GrEffect.
1913 GrAssert(NULL != nextTexture);
1914 // if we created a rt/tex and rendered to it without using a
1915 // texture and now we're texuring from the rt it will still be
1916 // the last bound texture, but it needs resolving. So keep this
1917 // out of the "last != next" check.
1918 GrGLRenderTarget* texRT =
1919 static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
1920 if (NULL != texRT) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001921 this->onResolveRenderTarget(texRT);
reed@google.comac10a2d2010-12-22 21:39:39 +00001922 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001923
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001924 if (fHWDrawState.getTexture(s) != nextTexture) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001925 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001926 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001927 #if GR_COLLECT_STATS
1928 ++fStats.fTextureChngCnt;
1929 #endif
1930 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001931 fHWDrawState.setTexture(s, nextTexture);
bsalomon@google.comcd19a5f2011-06-15 14:00:23 +00001932 // The texture matrix has to compensate for texture width/height
1933 // and NPOT-embedded-in-POT
1934 fDirtyFlags.fTextureChangedMask |= (1 << s);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001935 }
1936
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001937 const GrSamplerState& sampler = drawState->getSampler(s);
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001938 ResetTimestamp timestamp;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001939 const GrGLTexture::TexParams& oldTexParams =
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001940 nextTexture->getCachedTexParams(&timestamp);
1941 bool setAll = timestamp < this->getResetTimestamp();
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001942 GrGLTexture::TexParams newTexParams;
1943
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001944 newTexParams.fFilter = gr_to_gl_filter(sampler.getFilter());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001945
bsalomon@google.com1dcf5062011-11-14 19:29:53 +00001946 const GrGLenum* wraps = GrGLTexture::WrapMode2GLWrap();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001947 newTexParams.fWrapS = wraps[sampler.getWrapX()];
1948 newTexParams.fWrapT = wraps[sampler.getWrapY()];
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001949 memcpy(newTexParams.fSwizzleRGBA,
1950 get_swizzle(nextTexture->config(), sampler),
1951 sizeof(newTexParams.fSwizzleRGBA));
1952 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001953 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001955 GR_GL_TEXTURE_MAG_FILTER,
1956 newTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001957 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001958 GR_GL_TEXTURE_MIN_FILTER,
1959 newTexParams.fFilter));
1960 }
1961 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
1962 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001963 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001964 GR_GL_TEXTURE_WRAP_S,
1965 newTexParams.fWrapS));
1966 }
1967 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
1968 setTextureUnit(s);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001969 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001970 GR_GL_TEXTURE_WRAP_T,
1971 newTexParams.fWrapT));
1972 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001973 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001974 (setAll ||
1975 memcmp(newTexParams.fSwizzleRGBA,
1976 oldTexParams.fSwizzleRGBA,
1977 sizeof(newTexParams.fSwizzleRGBA)))) {
1978 setTextureUnit(s);
1979 set_tex_swizzle(newTexParams.fSwizzleRGBA,
1980 this->glInterface());
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001981 }
bsalomon@google.com80d09b92011-11-05 21:21:13 +00001982 nextTexture->setCachedTexParams(newTexParams,
1983 this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001984 }
1985 }
1986
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001987 GrIRect* rect = NULL;
1988 GrIRect clipBounds;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001989 if (drawState->isClipState() &&
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001990 fClip.hasConservativeBounds()) {
1991 fClip.getConservativeBounds().roundOut(&clipBounds);
1992 rect = &clipBounds;
1993 }
1994 this->flushRenderTarget(rect);
1995 this->flushAAState(type);
bsalomon@google.com0650e812011-04-08 18:07:53 +00001996
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001997 if (drawState->isDitherState() != fHWDrawState.isDitherState()) {
1998 if (drawState->isDitherState()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001999 GL_CALL(Enable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002000 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(Disable(GR_GL_DITHER));
reed@google.comac10a2d2010-12-22 21:39:39 +00002002 }
2003 }
2004
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002005 if (drawState->isColorWriteDisabled() !=
2006 fHWDrawState.isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002007 GrGLenum mask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002008 if (drawState->isColorWriteDisabled()) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002009 mask = GR_GL_FALSE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002010 } else {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002011 mask = GR_GL_TRUE;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002012 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002013 GL_CALL(ColorMask(mask, mask, mask, mask));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002014 }
2015
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002016 if (fHWDrawState.getDrawFace() != drawState->getDrawFace()) {
2017 switch (fCurrDrawState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002018 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002019 GL_CALL(Enable(GR_GL_CULL_FACE));
2020 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002021 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002022 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Enable(GR_GL_CULL_FACE));
2024 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002025 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002026 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002027 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002028 break;
2029 default:
2030 GrCrash("Unknown draw face.");
2031 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002032 fHWDrawState.setDrawFace(drawState->getDrawFace());
bsalomon@google.comd302f142011-03-03 13:54:13 +00002033 }
2034
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002035#if GR_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +00002036 // check for circular rendering
tomhudson@google.com93813632011-10-27 20:21:16 +00002037 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00002038 GrAssert(!this->isStageEnabled(s) ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002039 NULL == drawState->getRenderTarget() ||
2040 NULL == drawState->getTexture(s) ||
2041 drawState->getTexture(s)->asRenderTarget() !=
2042 drawState->getRenderTarget());
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002043 }
2044#endif
bsalomon@google.com316f99232011-01-13 21:28:12 +00002045
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002046 this->flushStencil();
reed@google.comac10a2d2010-12-22 21:39:39 +00002047
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002048 // This copy must happen after flushStencil() is called. flushStencil()
2049 // relies on detecting when the kModifyStencilClip_StateBit state has
2050 // changed since the last draw.
2051 fHWDrawState.copyStateFlags(*drawState);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002052 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002053}
2054
2055void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002056 if (fHWGeometryState.fVertexBuffer != buffer) {
2057 fHWGeometryState.fArrayPtrsDirty = true;
2058 fHWGeometryState.fVertexBuffer = buffer;
2059 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002060}
2061
2062void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002063 if (fHWGeometryState.fVertexBuffer == buffer) {
2064 // deleting bound buffer does implied bind to 0
2065 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002066 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002067 }
2068}
2069
2070void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002071 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002072}
2073
2074void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002075 if (fHWGeometryState.fIndexBuffer == buffer) {
2076 // deleting bound buffer does implied bind to 0
2077 fHWGeometryState.fIndexBuffer = NULL;
2078 }
2079}
2080
reed@google.comac10a2d2010-12-22 21:39:39 +00002081void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2082 GrAssert(NULL != renderTarget);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002083 GrDrawState* drawState = this->drawState();
2084 if (drawState->getRenderTarget() == renderTarget) {
2085 drawState->setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002086 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002087 if (fHWDrawState.getRenderTarget() == renderTarget) {
2088 fHWDrawState.setRenderTarget(NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +00002089 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002090}
2091
2092void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002093 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002094 GrDrawState* drawState = this->drawState();
2095 if (drawState->getTexture(s) == texture) {
2096 fCurrDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002097 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002098 if (fHWDrawState.getTexture(s) == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002099 // deleting bound texture does implied bind to 0
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002100 fHWDrawState.setTexture(s, NULL);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002101 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002102 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002103}
2104
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002105bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2106 bool getSizedInternalFormat,
2107 GrGLenum* internalFormat,
2108 GrGLenum* externalFormat,
2109 GrGLenum* externalType) {
2110 GrGLenum dontCare;
2111 if (NULL == internalFormat) {
2112 internalFormat = &dontCare;
2113 }
2114 if (NULL == externalFormat) {
2115 externalFormat = &dontCare;
2116 }
2117 if (NULL == externalType) {
2118 externalType = &dontCare;
2119 }
2120
reed@google.comac10a2d2010-12-22 21:39:39 +00002121 switch (config) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002122 case kRGBA_8888_PM_GrPixelConfig:
2123 case kRGBA_8888_UPM_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002124 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002125 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002126 if (getSizedInternalFormat) {
2127 *internalFormat = GR_GL_RGBA8;
2128 } else {
2129 *internalFormat = GR_GL_RGBA;
2130 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002131 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002132 break;
2133 case kBGRA_8888_PM_GrPixelConfig:
2134 case kBGRA_8888_UPM_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002135 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002136 return false;
2137 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002138 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002139 if (getSizedInternalFormat) {
2140 *internalFormat = GR_GL_BGRA8;
2141 } else {
2142 *internalFormat = GR_GL_BGRA;
2143 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002144 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002145 if (getSizedInternalFormat) {
2146 *internalFormat = GR_GL_RGBA8;
2147 } else {
2148 *internalFormat = GR_GL_RGBA;
2149 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002150 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002151 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002152 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002154 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002155 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002156 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002157 if (getSizedInternalFormat) {
2158 if (this->glBinding() == kDesktop_GrGLBinding) {
2159 return false;
2160 } else {
2161 *internalFormat = GR_GL_RGB565;
2162 }
2163 } else {
2164 *internalFormat = GR_GL_RGB;
2165 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002166 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002167 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002168 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002169 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002170 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002171 if (getSizedInternalFormat) {
2172 *internalFormat = GR_GL_RGBA4;
2173 } else {
2174 *internalFormat = GR_GL_RGBA;
2175 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002176 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002178 case kIndex_8_GrPixelConfig:
bsalomon@google.com18c9c192011-09-22 21:01:31 +00002179 if (this->getCaps().f8BitPaletteSupport) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002180 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002181 // glCompressedTexImage doesn't take external params
2182 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002183 // no sized/unsized internal format distinction here
2184 *internalFormat = GR_GL_PALETTE8_RGBA8;
2185 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002186 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002187 } else {
2188 return false;
2189 }
2190 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002191 case kAlpha_8_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002192 *internalFormat = GR_GL_ALPHA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002193 *externalFormat = GR_GL_ALPHA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002194 if (getSizedInternalFormat) {
2195 *internalFormat = GR_GL_ALPHA8;
2196 } else {
2197 *internalFormat = GR_GL_ALPHA;
2198 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002199 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002200 break;
2201 default:
2202 return false;
2203 }
2204 return true;
2205}
2206
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002207void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002208 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002209 if (fActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002210 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002211 fActiveTextureUnitIdx = unit;
2212 }
2213}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002214
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002215void GrGpuGL::setSpareTextureUnit() {
twiz@google.com0f31ca72011-03-18 17:38:11 +00002216 if (fActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002217 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002218 fActiveTextureUnitIdx = SPARE_TEX_UNIT;
2219 }
2220}
2221
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00002222void GrGpuGL::resetDirtyFlags() {
2223 Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags));
2224}
2225
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002226void GrGpuGL::setBuffers(bool indexed,
2227 int* extraVertexOffset,
2228 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002229
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002230 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002231
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002232 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2233
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002234 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002235 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002236 case kBuffer_GeometrySrcType:
2237 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002238 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002239 break;
2240 case kArray_GeometrySrcType:
2241 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002242 this->finalizeReservedVertices();
2243 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2244 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002245 break;
2246 default:
2247 vbuf = NULL; // suppress warning
2248 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002249 }
2250
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002251 GrAssert(NULL != vbuf);
2252 GrAssert(!vbuf->isLocked());
2253 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002254 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002255 fHWGeometryState.fArrayPtrsDirty = true;
2256 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002257 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002258
2259 if (indexed) {
2260 GrAssert(NULL != extraIndexOffset);
2261
2262 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002263 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002264 case kBuffer_GeometrySrcType:
2265 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002266 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002267 break;
2268 case kArray_GeometrySrcType:
2269 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002270 this->finalizeReservedIndices();
2271 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2272 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002273 break;
2274 default:
2275 ibuf = NULL; // suppress warning
2276 GrCrash("Unknown geometry src type!");
2277 }
2278
2279 GrAssert(NULL != ibuf);
2280 GrAssert(!ibuf->isLocked());
2281 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002282 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002283 fHWGeometryState.fIndexBuffer = ibuf;
2284 }
2285 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002286}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002287
2288int GrGpuGL::getMaxEdges() const {
2289 // FIXME: This is a pessimistic estimate based on how many other things
2290 // want to add uniforms. This should be centralized somewhere.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002291 return GR_CT_MIN(this->glCaps().maxFragmentUniformVectors() - 8,
tomhudson@google.com93813632011-10-27 20:21:16 +00002292 GrDrawState::kMaxEdges);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002293}
bsalomon@google.comfe676522011-06-17 18:12:21 +00002294