blob: 887ed7650ad49b4a7e3e7394b1e9a786c09f261a [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000012#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000013#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000014#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
caryclark@google.comcf6285b2012-06-06 12:09:01 +000016static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000017static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
bsalomon@google.com316f99232011-01-13 21:28:12 +000022// we use a spare texture unit to avoid
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023// mucking with the state of any of the stages.
tomhudson@google.com93813632011-10-27 20:21:16 +000024static const int SPARE_TEX_UNIT = GrDrawState::kNumStages;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026#define SKIP_CACHE_CHECK true
27
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000028#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
29 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000033 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
34 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
35 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
36#endif
37
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000038
39///////////////////////////////////////////////////////////////////////////////
40
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon@google.com271cffc2011-05-20 14:13:56 +000064bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
89 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
113 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000120static bool fbo_test(const GrGLInterface* gl, int w, int h) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000121
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000122 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000123
twiz@google.com0f31ca72011-03-18 17:38:11 +0000124 GrGLuint testFBO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 GR_GL_CALL(gl, GenFramebuffers(1, &testFBO));
126 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, testFBO));
twiz@google.com0f31ca72011-03-18 17:38:11 +0000127 GrGLuint testRTTex;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000128 GR_GL_CALL(gl, GenTextures(1, &testRTTex));
129 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, testRTTex));
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000130 // some implementations require texture to be mip-map complete before
131 // FBO with level 0 bound as color attachment will be framebuffer complete.
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000132 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 GR_GL_TEXTURE_MIN_FILTER,
134 GR_GL_NEAREST));
135 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, 0, GR_GL_RGBA, w, h,
136 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, NULL));
137 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
138 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER,
139 GR_GL_COLOR_ATTACHMENT0,
140 GR_GL_TEXTURE_2D, testRTTex, 0));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000141 GrGLenum status;
142 GR_GL_CALL_RET(gl, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 GR_GL_CALL(gl, DeleteFramebuffers(1, &testFBO));
144 GR_GL_CALL(gl, DeleteTextures(1, &testRTTex));
bsalomon@google.com316f99232011-01-13 21:28:12 +0000145
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000146 return status == GR_GL_FRAMEBUFFER_COMPLETE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147}
148
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000149GrGpuGL::GrGpuGL(const GrGLContextInfo& ctxInfo) : fGLContextInfo(ctxInfo) {
150
151 GrAssert(ctxInfo.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000152
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000153 fillInConfigRenderableTable();
154
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000155 fPrintedCaps = false;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000156
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000157 GrGLClearErr(fGLContextInfo.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000159 if (gPrintStartupSpew) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000160 const GrGLubyte* ext;
161 GL_CALL_RET(ext, GetString(GR_GL_EXTENSIONS));
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000162 const GrGLubyte* vendor;
163 const GrGLubyte* renderer;
164 const GrGLubyte* version;
165 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
166 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
167 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000168 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
169 this);
170 GrPrintf("------ VENDOR %s\n", vendor);
171 GrPrintf("------ RENDERER %s\n", renderer);
172 GrPrintf("------ VERSION %s\n", version);
173 GrPrintf("------ EXTENSIONS\n %s \n", ext);
174 }
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176 this->initCaps();
tomhudson@google.com747bf292011-06-14 18:16:52 +0000177
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000178 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContextInfo()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000179
bsalomon@google.comfe676522011-06-17 18:12:21 +0000180 fLastSuccessfulStencilFmtIdx = 0;
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000181 if (false) { // avoid bit rot, suppress warning
182 fbo_test(this->glInterface(), 0, 0);
183 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000184}
185
186GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000187 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000188 // detach the current program so there is no confusion on OpenGL's part
189 // that we want it to be deleted
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000190 GrAssert(fHWProgramID == fCurrentProgram->fProgramID);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000191 GL_CALL(UseProgram(0));
192 }
193
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000194 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000195
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000196 // This must be called by before the GrDrawTarget destructor
197 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000198 // This subclass must do this before the base class destructor runs
199 // since we will unref the GrGLInterface.
200 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000201}
202
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000203///////////////////////////////////////////////////////////////////////////////
204
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000205void GrGpuGL::initCaps() {
206 GrGLint maxTextureUnits;
207 // check FS and fixed-function texture unit limits
208 // we only use textures in the fragment stage currently.
209 // checks are > to make sure we have a spare unit.
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000210 const GrGLInterface* gl = this->glInterface();
211 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000212 GrAssert(maxTextureUnits > GrDrawState::kNumStages);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000213
bsalomon@google.comf6601872012-08-28 21:11:35 +0000214 CapsInternals* caps = this->capsInternals();
215
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000216 GrGLint numFormats;
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000217 GR_GL_GetIntegerv(gl, GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000218 SkAutoSTMalloc<10, GrGLint> formats(numFormats);
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000219 GR_GL_GetIntegerv(gl, GR_GL_COMPRESSED_TEXTURE_FORMATS, formats);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000220 for (int i = 0; i < numFormats; ++i) {
221 if (formats[i] == GR_GL_PALETTE8_RGBA8) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000222 caps->f8BitPaletteSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000223 break;
224 }
225 }
226
227 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000228 // we could also look for GL_ATI_separate_stencil extension or
229 // GL_EXT_stencil_two_side but they use different function signatures
230 // than GL2.0+ (and than each other).
bsalomon@google.comf6601872012-08-28 21:11:35 +0000231 caps->fTwoSidedStencilSupport = (this->glVersion() >= GR_GL_VER(2,0));
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000232 // supported on GL 1.4 and higher or by extension
bsalomon@google.comf6601872012-08-28 21:11:35 +0000233 caps->fStencilWrapOpsSupport = (this->glVersion() >= GR_GL_VER(1,4)) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000234 this->hasExtension("GL_EXT_stencil_wrap");
235 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000236 // ES 2 has two sided stencil and stencil wrap
bsalomon@google.comf6601872012-08-28 21:11:35 +0000237 caps->fTwoSidedStencilSupport = true;
238 caps->fStencilWrapOpsSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000239 }
240
241 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000242 caps->fBufferLockSupport = true; // we require VBO support and the desktop VBO
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000243 // extension includes glMapBuffer.
244 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000245 caps->fBufferLockSupport = this->hasExtension("GL_OES_mapbuffer");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000246 }
247
248 if (kDesktop_GrGLBinding == this->glBinding()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000249 if (this->glVersion() >= GR_GL_VER(2,0) ||
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 this->hasExtension("GL_ARB_texture_non_power_of_two")) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000251 caps->fNPOTTextureTileSupport = true;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000252 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000253 caps->fNPOTTextureTileSupport = false;
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000254 }
255 } else {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000256 // Unextended ES2 supports NPOT textures with clamp_to_edge and non-mip filters only
bsalomon@google.comf6601872012-08-28 21:11:35 +0000257 caps->fNPOTTextureTileSupport = this->hasExtension("GL_OES_texture_npot");
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000258 }
259
bsalomon@google.comf6601872012-08-28 21:11:35 +0000260 caps->fHWAALineSupport = (kDesktop_GrGLBinding == this->glBinding());
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261
bsalomon@google.comf6601872012-08-28 21:11:35 +0000262 GR_GL_GetIntegerv(gl, GR_GL_MAX_TEXTURE_SIZE, &caps->fMaxTextureSize);
263 GR_GL_GetIntegerv(gl, GR_GL_MAX_RENDERBUFFER_SIZE, &caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000264 // Our render targets are always created with textures as the color
265 // attachment, hence this min:
bsalomon@google.comf6601872012-08-28 21:11:35 +0000266 caps->fMaxRenderTargetSize = GrMin(caps->fMaxTextureSize, caps->fMaxRenderTargetSize);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000267
bsalomon@google.comf6601872012-08-28 21:11:35 +0000268 caps->fFSAASupport = GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType();
269 caps->fPathStencilingSupport = GR_GL_USE_NV_PATH_RENDERING &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000270 this->hasExtension("GL_NV_path_rendering");
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000271
272 // Enable supported shader-related caps
273 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000274 caps->fDualSourceBlendingSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000275 this->glVersion() >= GR_GL_VER(3,3) ||
276 this->hasExtension("GL_ARB_blend_func_extended");
bsalomon@google.comf6601872012-08-28 21:11:35 +0000277 caps->fShaderDerivativeSupport = true;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000278 // we don't support GL_ARB_geometry_shader4, just GL 3.2+ GS
bsalomon@google.comf6601872012-08-28 21:11:35 +0000279 caps->fGeometryShaderSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000280 this->glVersion() >= GR_GL_VER(3,2) &&
281 this->glslGeneration() >= k150_GrGLSLGeneration;
282 } else {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000283 caps->fShaderDerivativeSupport =
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000284 this->hasExtension("GL_OES_standard_derivatives");
285 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000286}
287
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000288void GrGpuGL::fillInConfigRenderableTable() {
289
290 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000291 // no support for render targets unless the GL_ARB_framebuffer_object
292 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
293 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000294 // probably don't get R8 in this case.
295
296 // OpenGL 3.0
297 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
298 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
299
300 // >= OpenGL 3.1
301 // base color renderable: RED, RG, RGB, and RGBA
302 // sized derivatives: R8, RGBA4, RGBA8
303 // if the GL_ARB_compatibility extension is supported then we get back
304 // support for GL_ALPHA and ALPHA8
305
306 // GL_EXT_bgra adds BGRA render targets to any version
307
308 // ES 2.0
309 // color renderable: RGBA4, RGB5_A1, RGB565
310 // GL_EXT_texture_rg adds support for R8 as a color render target
311 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
312 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888
313 // added BGRA support
314
315 if (kDesktop_GrGLBinding == this->glBinding()) {
316 // Post 3.0 we will get R8
317 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
318 if (this->glVersion() >= GR_GL_VER(3,0) ||
319 this->hasExtension("GL_ARB_framebuffer_object")) {
320 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
321 }
322 } else {
323 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000324 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000325 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000326 }
327
328 if (kDesktop_GrGLBinding != this->glBinding()) {
329 // only available in ES
330 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
331 }
332
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000333 // Pre 3.0, Ganesh relies on either GL_ARB_framebuffer_object or
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000334 // GL_EXT_framebuffer_object for FBO support. Both of these
335 // allow RGBA4 render targets so this is always supported.
336 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = true;
337
338 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000340 }
341
342 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000344 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000345}
346
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000347GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000348 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
349 return GrPixelConfigSwapRAndB(config);
350 } else {
351 return config;
352 }
353}
354
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000355GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig config) const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000356 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && GrPixelConfigIsRGBA8888(config)) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000357 return GrPixelConfigSwapRAndB(config);
358 } else {
359 return config;
360 }
361}
362
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000363bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
364 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
365}
366
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000367void GrGpuGL::onResetContext() {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000368 if (gPrintStartupSpew && !fPrintedCaps) {
369 fPrintedCaps = true;
370 this->getCaps().print();
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000371 this->glCaps().print();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000372 }
373
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000374 // we don't use the zb at all
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000375 GL_CALL(Disable(GR_GL_DEPTH_TEST));
376 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000377
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000378 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
379 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000380
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000381 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000382 // Desktop-only state that we never change
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000383 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000384 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
385 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
386 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
387 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
bsalomon@google.come76b7cc2012-06-18 12:47:06 +0000388 if (this->glCaps().imagingSupport()) {
389 GL_CALL(Disable(GR_GL_COLOR_TABLE));
390 }
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000391 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
392 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
393 // Since ES doesn't support glPointSize at all we always use the VS to
394 // set the point size
395 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
396
397 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000398 // currently part of our gl interface. There are probably others as
bsalomon@google.com469d0dd2012-05-21 20:14:29 +0000399 // well.
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000400 }
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000401 fHWAAState.invalidate();
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000402 fHWWriteToColor = kUnknown_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000403
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 // we only ever use lines in hairline mode
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000405 GL_CALL(LineWidth(1));
reed@google.comac10a2d2010-12-22 21:39:39 +0000406
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000407 // invalid
bsalomon@google.com49209392012-06-05 15:13:46 +0000408 fHWActiveTextureUnitIdx = -1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000410 fHWBlendState.invalidate();
bsalomon@google.com080773c2011-03-15 19:09:25 +0000411
tomhudson@google.com93813632011-10-27 20:21:16 +0000412 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000413 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000414 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000415
bsalomon@google.coma3201942012-06-21 19:58:20 +0000416 fHWScissorSettings.invalidate();
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000417
bsalomon@google.coma3201942012-06-21 19:58:20 +0000418 fHWViewport.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000419
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000420 fHWStencilSettings.invalidate();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000421 fHWStencilTestEnabled = kUnknown_TriState;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000422
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 fHWGeometryState.fIndexBuffer = NULL;
424 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000425
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000426 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000427
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000428 fHWBoundRenderTarget = NULL;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000429
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000430 fHWPathMatrixState.invalidate();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000431 if (fCaps.pathStencilingSupport()) {
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000432 // we don't use the model view matrix.
433 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
434 GL_CALL(LoadIdentity());
435 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000436
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000437 // we assume these values
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000438 if (this->glCaps().unpackRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000439 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
440 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000441 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000442 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
443 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000444 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000445 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
446 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000447 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000448 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
449 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000450
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000451 fHWGeometryState.fVertexOffset = ~0U;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000452
453 // Third party GL code may have left vertex attributes enabled. Some GL
454 // implementations (osmesa) may read vetex attributes that are not required
455 // by the current shader. Therefore, we have to ensure that only the
456 // attributes we require for the current draw are enabled or we may cause an
457 // invalid read.
458
459 // Disable all vertex layout bits so that next flush will assume all
460 // optional vertex attributes are disabled.
461 fHWGeometryState.fVertexLayout = 0;
462
463 // We always use the this attribute and assume it is always enabled.
464 int posAttrIdx = GrGLProgram::PositionAttributeIdx();
465 GL_CALL(EnableVertexAttribArray(posAttrIdx));
466 // Disable all other vertex attributes.
bsalomon@google.com60da4172012-06-01 19:25:00 +0000467 for (int va = 0; va < this->glCaps().maxVertexAttributes(); ++va) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000468 if (va != posAttrIdx) {
469 GL_CALL(DisableVertexAttribArray(va));
470 }
471 }
472
473 fHWProgramID = 0;
474 fHWConstAttribColor = GrColor_ILLEGAL;
475 fHWConstAttribCoverage = GrColor_ILLEGAL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000476}
477
bsalomon@google.come269f212011-11-07 13:29:52 +0000478GrTexture* GrGpuGL::onCreatePlatformTexture(const GrPlatformTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000479 GrGLTexture::Desc glTexDesc;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000480 if (!configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000481 return NULL;
482 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000483
robertphillips@google.com32716282012-06-04 12:48:45 +0000484 // next line relies on PlatformTextureDesc's flags matching GrTexture's
485 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000486 glTexDesc.fWidth = desc.fWidth;
487 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000488 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000489 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000490 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
491 glTexDesc.fOwnsID = false;
492 glTexDesc.fOrientation = GrGLTexture::kBottomUp_Orientation;
493
494 GrGLTexture* texture = NULL;
495 if (desc.fFlags & kRenderTarget_GrPlatformTextureFlag) {
496 GrGLRenderTarget::Desc glRTDesc;
497 glRTDesc.fRTFBOID = 0;
498 glRTDesc.fTexFBOID = 0;
499 glRTDesc.fMSColorRenderbufferID = 0;
500 glRTDesc.fOwnIDs = true;
501 glRTDesc.fConfig = desc.fConfig;
502 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com99621082011-11-15 16:47:16 +0000503 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
504 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000505 glTexDesc.fTextureID,
506 &glRTDesc)) {
507 return NULL;
508 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000509 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000510 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000511 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000512 }
513 if (NULL == texture) {
514 return NULL;
515 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000516
bsalomon@google.come269f212011-11-07 13:29:52 +0000517 this->setSpareTextureUnit();
518 return texture;
519}
520
521GrRenderTarget* GrGpuGL::onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
522 GrGLRenderTarget::Desc glDesc;
523 glDesc.fConfig = desc.fConfig;
524 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
525 glDesc.fMSColorRenderbufferID = 0;
526 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
527 glDesc.fSampleCnt = desc.fSampleCnt;
528 glDesc.fOwnIDs = false;
529 GrGLIRect viewport;
530 viewport.fLeft = 0;
531 viewport.fBottom = 0;
532 viewport.fWidth = desc.fWidth;
533 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000534
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000535 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
536 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000537 if (desc.fStencilBits) {
538 GrGLStencilBuffer::Format format;
539 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
540 format.fPacked = false;
541 format.fStencilBits = desc.fStencilBits;
542 format.fTotalBits = desc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000543 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
544 (this,
545 0,
546 desc.fWidth,
547 desc.fHeight,
548 desc.fSampleCnt,
549 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000550 tgt->setStencilBuffer(sb);
551 sb->unref();
552 }
553 return tgt;
554}
555
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000556////////////////////////////////////////////////////////////////////////////////
557
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558void GrGpuGL::onWriteTexturePixels(GrTexture* texture,
559 int left, int top, int width, int height,
560 GrPixelConfig config, const void* buffer,
561 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000562 if (NULL == buffer) {
563 return;
564 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000565 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
566
bsalomon@google.com6f379512011-11-16 20:36:03 +0000567 this->setSpareTextureUnit();
568 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
569 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000570 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000571 desc.fWidth = glTex->width();
572 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000573 desc.fConfig = glTex->config();
574 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000575 desc.fTextureID = glTex->textureID();
robertphillips@google.com32716282012-06-04 12:48:45 +0000576 desc.fOrientation = glTex->orientation();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000577
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000578 this->uploadTexData(desc, false,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000579 left, top, width, height,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000580 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000581}
582
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000583namespace {
584bool adjust_pixel_ops_params(int surfaceWidth,
585 int surfaceHeight,
586 size_t bpp,
587 int* left, int* top, int* width, int* height,
588 const void** data,
589 size_t* rowBytes) {
590 if (!*rowBytes) {
591 *rowBytes = *width * bpp;
592 }
593
594 GrIRect subRect = GrIRect::MakeXYWH(*left, *top, *width, *height);
595 GrIRect bounds = GrIRect::MakeWH(surfaceWidth, surfaceHeight);
596
597 if (!subRect.intersect(bounds)) {
598 return false;
599 }
600 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
601 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
602
603 *left = subRect.fLeft;
604 *top = subRect.fTop;
605 *width = subRect.width();
606 *height = subRect.height();
607 return true;
608}
609}
610
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000611bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
612 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000613 int left, int top, int width, int height,
614 GrPixelConfig dataConfig,
615 const void* data,
616 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000617 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000618
619 size_t bpp = GrBytesPerPixel(dataConfig);
620 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
621 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000622 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000623 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000624 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000625
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000626 // in case we need a temporary, trimmed copy of the src pixels
627 SkAutoSMalloc<128 * 128> tempStorage;
628
bsalomon@google.com313f0192012-07-10 17:21:02 +0000629 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000630 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000631 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000632 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000633
bsalomon@google.com313f0192012-07-10 17:21:02 +0000634 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
635 // 565 is not a sized internal format on desktop GL. So on desktop with
636 // 565 we always use an unsized internal format to let the system pick
637 // the best sized format to convert the 565 data to. Since TexStorage
638 // only allows sized internal formats we will instead use TexImage2D.
639 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000640 }
641
642 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000643 GrGLenum externalFormat;
644 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000645 // glTexStorage requires sized internal formats on both desktop and ES. ES
646 // glTexImage requires an unsized format.
647 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
648 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000649 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 }
651
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000652 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000653 // paletted textures cannot be updated
654 return false;
655 }
656
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000657 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000658 * check whether to allocate a temporary buffer for flipping y or
659 * because our srcData has extra bytes past each row. If so, we need
660 * to trim those off here, since GL ES may not let us specify
661 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000662 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000663 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000664 bool swFlipY = false;
665 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000666 if (NULL != data) {
667 if (GrGLTexture::kBottomUp_Orientation == desc.fOrientation) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000668 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000669 glFlipY = true;
670 } else {
671 swFlipY = true;
672 }
673 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000674 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000675 // can't use this for flipping, only non-neg values allowed. :(
676 if (rowBytes != trimRowBytes) {
677 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
678 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
679 restoreGLRowLength = true;
680 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000681 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000682 if (trimRowBytes != rowBytes || swFlipY) {
683 // copy data into our new storage, skipping the trailing bytes
684 size_t trimSize = height * trimRowBytes;
685 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000686 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000687 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000688 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000689 char* dst = (char*)tempStorage.reset(trimSize);
690 for (int y = 0; y < height; y++) {
691 memcpy(dst, src, trimRowBytes);
692 if (swFlipY) {
693 src -= rowBytes;
694 } else {
695 src += rowBytes;
696 }
697 dst += trimRowBytes;
698 }
699 // now point data to our copied version
700 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000701 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000702 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000703 if (glFlipY) {
704 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
705 }
706 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000708 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000709 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000710 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000711 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000712 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000713 if (useTexStorage) {
714 // We never resize or change formats of textures. We don't use
715 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000716 GL_ALLOC_CALL(this->glInterface(),
717 TexStorage2D(GR_GL_TEXTURE_2D,
718 1, // levels
719 internalFormat,
720 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000721 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000722 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
723 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
724 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000725 GL_ALLOC_CALL(this->glInterface(),
726 CompressedTexImage2D(GR_GL_TEXTURE_2D,
727 0, // level
728 internalFormat,
729 desc.fWidth, desc.fHeight,
730 0, // border
731 imageSize,
732 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000733 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000734 GL_ALLOC_CALL(this->glInterface(),
735 TexImage2D(GR_GL_TEXTURE_2D,
736 0, // level
737 internalFormat,
738 desc.fWidth, desc.fHeight,
739 0, // border
740 externalFormat, externalType,
741 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000742 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000743 }
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000744 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000745 if (error != GR_GL_NO_ERROR) {
746 succeeded = false;
747 } else {
748 // if we have data and we used TexStorage to create the texture, we
749 // now upload with TexSubImage.
750 if (NULL != data && useTexStorage) {
751 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
752 0, // level
753 left, top,
754 width, height,
755 externalFormat, externalType,
756 data));
757 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000758 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000759 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000760 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000761 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000762 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000763 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
764 0, // level
765 left, top,
766 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000767 externalFormat, externalType, data));
768 }
769
770 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000771 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000773 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000774 if (glFlipY) {
775 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
776 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000777 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000778}
779
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000780namespace {
781bool renderbuffer_storage_msaa(GrGLContextInfo& ctxInfo,
782 int sampleCount,
783 GrGLenum format,
784 int width, int height) {
785 CLEAR_ERROR_BEFORE_ALLOC(ctxInfo.interface());
786 GrAssert(GrGLCaps::kNone_MSFBOType != ctxInfo.caps().msFBOType());
787 bool created = false;
788 if (GrGLCaps::kNVDesktop_CoverageAAType ==
789 ctxInfo.caps().coverageAAType()) {
790 const GrGLCaps::MSAACoverageMode& mode =
791 ctxInfo.caps().getMSAACoverageMode(sampleCount);
792 GL_ALLOC_CALL(ctxInfo.interface(),
793 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
794 mode.fCoverageSampleCnt,
795 mode.fColorSampleCnt,
796 format,
797 width, height));
798 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
799 }
800 if (!created) {
bsalomon@google.comf6b070d2012-04-27 14:25:44 +0000801 // glRBMS will fail if requested samples is > max samples.
802 sampleCount = GrMin(sampleCount, ctxInfo.caps().maxSampleCount());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000803 GL_ALLOC_CALL(ctxInfo.interface(),
804 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
805 sampleCount,
806 format,
807 width, height));
808 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctxInfo.interface()));
809 }
810 return created;
811}
812}
813
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814bool GrGpuGL::createRenderTargetObjects(int width, int height,
815 GrGLuint texID,
816 GrGLRenderTarget::Desc* desc) {
817 desc->fMSColorRenderbufferID = 0;
818 desc->fRTFBOID = 0;
819 desc->fTexFBOID = 0;
820 desc->fOwnIDs = true;
821
822 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000823
bsalomon@google.comab15d612011-08-09 12:57:56 +0000824 GrGLenum msColorFormat = 0; // suppress warning
825
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000826 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827 if (!desc->fTexFBOID) {
828 goto FAILED;
829 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000830
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831
832 // If we are using multisampling we will create two FBOS. We render
833 // to one and then resolve to the texture bound to the other.
bsalomon@google.come269f212011-11-07 13:29:52 +0000834 if (desc->fSampleCnt > 0) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000835 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000836 goto FAILED;
837 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000838 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
839 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000840 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000841 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000842 !this->configToGLFormats(desc->fConfig,
843 // GLES requires sized internal formats
844 kES2_GrGLBinding == this->glBinding(),
845 &msColorFormat, NULL, NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000846 goto FAILED;
847 }
848 } else {
849 desc->fRTFBOID = desc->fTexFBOID;
850 }
851
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000852 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000853 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000854 if (desc->fRTFBOID != desc->fTexFBOID) {
855 GrAssert(desc->fSampleCnt > 1);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000856 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857 desc->fMSColorRenderbufferID));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000858 if (!renderbuffer_storage_msaa(fGLContextInfo,
859 desc->fSampleCnt,
860 msColorFormat,
861 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000862 goto FAILED;
863 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000864 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000865 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 GR_GL_COLOR_ATTACHMENT0,
867 GR_GL_RENDERBUFFER,
868 desc->fMSColorRenderbufferID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000869 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000870 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
871 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
872 goto FAILED;
873 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000874 fGLContextInfo.caps().markConfigAsValidColorAttachment(
875 desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 }
877 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
881 GR_GL_COLOR_ATTACHMENT0,
882 GR_GL_TEXTURE_2D,
883 texID, 0));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000884 if (!this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000885 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
886 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
887 goto FAILED;
888 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000889 fGLContextInfo.caps().markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890 }
891
892 return true;
893
894FAILED:
895 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000896 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000897 }
898 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000900 }
901 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000902 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000903 }
904 return false;
905}
906
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000907// good to set a break-point here to know when createTexture fails
908static GrTexture* return_null_texture() {
909// GrAssert(!"null texture");
910 return NULL;
911}
912
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000913#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000914static size_t as_size_t(int x) {
915 return x;
916}
917#endif
918
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000919GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000920 const void* srcData,
921 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000922
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000923 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000924 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000925
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000926 // Attempt to catch un- or wrongly initialized sample counts;
927 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
928
robertphillips@google.com32716282012-06-04 12:48:45 +0000929 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000930 glTexDesc.fWidth = desc.fWidth;
931 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000932 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000933 glTexDesc.fSampleCnt = desc.fSampleCnt;
934
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000935 glTexDesc.fOwnsID = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000936
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000937 glRTDesc.fMSColorRenderbufferID = 0;
938 glRTDesc.fRTFBOID = 0;
939 glRTDesc.fTexFBOID = 0;
940 glRTDesc.fOwnIDs = true;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000941 glRTDesc.fConfig = glTexDesc.fConfig;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000942
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000943 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000944
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000945 const Caps& caps = this->getCaps();
946
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000947 // We keep GrRenderTargets in GL's normal orientation so that they
948 // can be drawn to by the outside world without the client having
949 // to render upside down.
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000950 glTexDesc.fOrientation = renderTarget ? GrGLTexture::kBottomUp_Orientation :
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000951 GrGLTexture::kTopDown_Orientation;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000952
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000953 glRTDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000954 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000955 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000956 //GrPrintf("MSAA RT requested but not supported on this platform.");
957 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000958 }
959
reed@google.comac10a2d2010-12-22 21:39:39 +0000960 if (renderTarget) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000961 if (glTexDesc.fWidth > caps.maxRenderTargetSize() ||
962 glTexDesc.fHeight > caps.maxRenderTargetSize()) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000963 return return_null_texture();
964 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000965 }
966
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000967 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000968 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000969 // provides a hint about how this texture will be used
970 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
971 GR_GL_TEXTURE_USAGE,
972 GR_GL_FRAMEBUFFER_ATTACHMENT));
973 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000974 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000975 return return_null_texture();
976 }
977
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000978 this->setSpareTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000980
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000981 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
982 // drivers have a bug where an FBO won't be complete if it includes a
983 // texture that is not mipmap complete (considering the filter in use).
984 GrGLTexture::TexParams initialTexParams;
985 // we only set a subset here so invalidate first
986 initialTexParams.invalidate();
987 initialTexParams.fFilter = GR_GL_NEAREST;
988 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
989 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
991 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000992 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
994 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000995 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000996 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
997 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000998 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1000 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001001 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001002 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1003 glTexDesc.fWidth, glTexDesc.fHeight,
1004 desc.fConfig, srcData, rowBytes)) {
1005 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1006 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001007 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001008
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001009 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001011 // unbind the texture from the texture unit before binding it to the frame buffer
1012 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1013
bsalomon@google.com99621082011-11-15 16:47:16 +00001014 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1015 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 glTexDesc.fTextureID,
1017 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 return return_null_texture();
1020 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001021 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001023 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001025 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001026#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001027 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1028 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001029#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001030 return tex;
1031}
1032
1033namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001034
1035const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1036
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001037void inline get_stencil_rb_sizes(const GrGLInterface* gl,
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001038 GrGLuint rb,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GrGLStencilBuffer::Format* format) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001040 // we shouldn't ever know one size and not the other
1041 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1042 (kUnknownBitCount == format->fTotalBits));
1043 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001044 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001045 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1046 (GrGLint*)&format->fStencilBits);
1047 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001049 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1050 (GrGLint*)&format->fTotalBits);
1051 format->fTotalBits += format->fStencilBits;
1052 } else {
1053 format->fTotalBits = format->fStencilBits;
1054 }
1055 }
1056}
1057}
1058
1059bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1060 int width, int height) {
1061
1062 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001063 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001065 GrAssert(width >= rt->width());
1066 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067
1068 int samples = rt->numSamples();
1069 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001070 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 if (!sbID) {
1072 return false;
1073 }
1074
1075 GrGLStencilBuffer* sb = NULL;
1076
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001077 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001079 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001080 // we start with the last stencil format that succeeded in hopes
1081 // that we won't go through this loop more than once after the
1082 // first (painful) stencil creation.
1083 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001084 const GrGLCaps::StencilFormat& sFmt =
1085 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001086 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001087 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001088 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001089 bool created;
1090 if (samples > 0) {
1091 created = renderbuffer_storage_msaa(fGLContextInfo,
1092 samples,
1093 sFmt.fInternalFormat,
1094 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001095 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001096 GL_ALLOC_CALL(this->glInterface(),
1097 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001098 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001099 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001100 created =
1101 (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001103 if (created) {
1104 // After sized formats we attempt an unsized format and take
1105 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001106 GrGLStencilBuffer::Format format = sFmt;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 get_stencil_rb_sizes(this->glInterface(), sbID, &format);
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001108 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
1109 (this, sbID, width, height,
1110 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1112 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +00001113 sb->transferToCacheAndLock();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001114 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 return true;
1116 }
1117 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118 }
1119 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001120 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001121 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001122}
1123
1124bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
1125 GrRenderTarget* rt) {
1126 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1127
1128 GrGLuint fbo = glrt->renderFBOID();
1129
1130 if (NULL == sb) {
1131 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001132 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001133 GR_GL_STENCIL_ATTACHMENT,
1134 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 GR_GL_DEPTH_ATTACHMENT,
1137 GR_GL_RENDERBUFFER, 0));
1138#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001139 GrGLenum status;
1140 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1142#endif
1143 }
1144 return true;
1145 } else {
1146 GrGLStencilBuffer* glsb = (GrGLStencilBuffer*) sb;
1147 GrGLuint rb = glsb->renderbufferID();
1148
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001149 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1151 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 GR_GL_STENCIL_ATTACHMENT,
1153 GR_GL_RENDERBUFFER, rb));
1154 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001155 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 GR_GL_DEPTH_ATTACHMENT,
1157 GR_GL_RENDERBUFFER, rb));
1158 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001159 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 GR_GL_DEPTH_ATTACHMENT,
1161 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001162 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001164 GrGLenum status;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001165 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(),
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001166 glsb->format())) {
1167 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1168 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001170 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001172 if (glsb->format().fPacked) {
1173 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1174 GR_GL_DEPTH_ATTACHMENT,
1175 GR_GL_RENDERBUFFER, 0));
1176 }
1177 return false;
1178 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001179 fGLContextInfo.caps().markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001180 rt->config(),
1181 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001184 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001185 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001186}
1187
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001188////////////////////////////////////////////////////////////////////////////////
1189
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001190GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001191 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001192 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, id));
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001195 fHWGeometryState.fArrayPtrsDirty = true;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001196 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001198 GL_ALLOC_CALL(this->glInterface(),
1199 BufferData(GR_GL_ARRAY_BUFFER,
1200 size,
1201 NULL, // data ptr
1202 dynamic ? GR_GL_DYNAMIC_DRAW :
1203 GR_GL_STATIC_DRAW));
1204 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001205 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 // deleting bound buffer does implicit bind to 0
1207 fHWGeometryState.fVertexBuffer = NULL;
1208 return NULL;
1209 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001210 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer,
1211 (this, id,
1212 size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 fHWGeometryState.fVertexBuffer = vertexBuffer;
1214 return vertexBuffer;
1215 }
1216 return NULL;
1217}
1218
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001219GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001220 GrGLuint id;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(GenBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 if (id) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001224 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
reed@google.comac10a2d2010-12-22 21:39:39 +00001225 // make sure driver can allocate memory for this buffer
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001226 GL_ALLOC_CALL(this->glInterface(),
1227 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1228 size,
1229 NULL, // data ptr
1230 dynamic ? GR_GL_DYNAMIC_DRAW :
1231 GR_GL_STATIC_DRAW));
1232 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001233 GL_CALL(DeleteBuffers(1, &id));
reed@google.comac10a2d2010-12-22 21:39:39 +00001234 // deleting bound buffer does implicit bind to 0
1235 fHWGeometryState.fIndexBuffer = NULL;
1236 return NULL;
1237 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001238 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer,
1239 (this, id, size, dynamic));
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 fHWGeometryState.fIndexBuffer = indexBuffer;
1241 return indexBuffer;
1242 }
1243 return NULL;
1244}
1245
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001246GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001247 GrAssert(fCaps.pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001248 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001249}
1250
bsalomon@google.coma3201942012-06-21 19:58:20 +00001251void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001252 const GrDrawState& drawState = this->getDrawState();
1253 const GrGLRenderTarget* rt =
1254 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1255
1256 GrAssert(NULL != rt);
1257 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001258
bsalomon@google.coma3201942012-06-21 19:58:20 +00001259 if (fScissorState.fEnabled) {
1260 GrGLIRect scissor;
1261 scissor.setRelativeTo(vp,
1262 fScissorState.fRect.fLeft,
1263 fScissorState.fRect.fTop,
1264 fScissorState.fRect.width(),
1265 fScissorState.fRect.height());
1266 // if the scissor fully contains the viewport then we fall through and
1267 // disable the scissor test.
1268 if (!scissor.contains(vp)) {
1269 if (fHWScissorSettings.fRect != scissor) {
1270 scissor.pushToGLScissor(this->glInterface());
1271 fHWScissorSettings.fRect = scissor;
1272 }
1273 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1274 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1275 fHWScissorSettings.fEnabled = kYes_TriState;
1276 }
1277 return;
1278 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001280 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001281 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001282 fHWScissorSettings.fEnabled = kNo_TriState;
1283 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 }
1285}
1286
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001287void GrGpuGL::onClear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001288 const GrDrawState& drawState = this->getDrawState();
1289 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001290 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001291 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001292
bsalomon@google.com74b98712011-11-11 19:46:16 +00001293 GrIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001294 if (NULL != rect) {
1295 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001296 clippedRect = *rect;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001297 GrIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001298 if (clippedRect.intersect(rtRect)) {
1299 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001300 } else {
1301 return;
1302 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001304 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001305 GrAutoTRestore<ScissorState> asr(&fScissorState);
1306 fScissorState.fEnabled = (NULL != rect);
1307 if (fScissorState.fEnabled) {
1308 fScissorState.fRect = *rect;
1309 }
1310 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001311
1312 GrGLfloat r, g, b, a;
1313 static const GrGLfloat scale255 = 1.f / 255.f;
1314 a = GrColorUnpackA(color) * scale255;
1315 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001316 r = GrColorUnpackR(color) * scaleRGB;
1317 g = GrColorUnpackG(color) * scaleRGB;
1318 b = GrColorUnpackB(color) * scaleRGB;
1319
1320 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001321 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001322 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001323 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001324}
1325
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001326void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001327 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001328 return;
1329 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001330
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001331 this->flushRenderTarget(&GrIRect::EmptyIRect());
1332
bsalomon@google.coma3201942012-06-21 19:58:20 +00001333 GrAutoTRestore<ScissorState> asr(&fScissorState);
1334 fScissorState.fEnabled = false;
1335 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001336
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001337 GL_CALL(StencilMask(0xffffffff));
1338 GL_CALL(ClearStencil(0));
1339 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001340 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001341}
1342
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001343void GrGpuGL::clearStencilClip(const GrIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001344 const GrDrawState& drawState = this->getDrawState();
1345 const GrRenderTarget* rt = drawState.getRenderTarget();
1346 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001347
1348 // this should only be called internally when we know we have a
1349 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001350 GrAssert(NULL != rt->getStencilBuffer());
1351 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001352#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001354 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001355#else
1356 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001358 // turned into draws. Our contract on GrDrawTarget says that
1359 // changing the clip between stencil passes may or may not
1360 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001361 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001362#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001363 GrGLint value;
1364 if (insideClip) {
1365 value = (1 << (stencilBitCount - 1));
1366 } else {
1367 value = 0;
1368 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001369 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001370
1371 GrAutoTRestore<ScissorState> asr(&fScissorState);
1372 fScissorState.fEnabled = true;
1373 fScissorState.fRect = rect;
1374 this->flushScissor();
1375
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001376 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001377 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001378 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001379 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001380}
1381
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001382void GrGpuGL::onForceRenderTargetFlush() {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001383 this->flushRenderTarget(&GrIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001384}
1385
bsalomon@google.comc4364992011-11-07 15:54:49 +00001386bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1387 int left, int top,
1388 int width, int height,
1389 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001390 size_t rowBytes) const {
1391 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001392 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001393 return false;
1394 }
1395
1396 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001397 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001398 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001399 return true;
1400 }
1401 // If we have to do memcpys to handle rowBytes then y-flip is free
1402 // Note the rowBytes might be tight to the passed in data, but if data
1403 // gets clipped in x to the target the rowBytes will no longer be tight.
1404 if (left >= 0 && (left + width) < renderTarget->width()) {
1405 return 0 == rowBytes ||
1406 GrBytesPerPixel(config) * width == rowBytes;
1407 } else {
1408 return false;
1409 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001410}
1411
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001412bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001413 int left, int top,
1414 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001415 GrPixelConfig config,
1416 void* buffer,
1417 size_t rowBytes,
1418 bool invertY) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001419 GrGLenum format;
1420 GrGLenum type;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001421 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001422 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001423 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001424 size_t bpp = GrBytesPerPixel(config);
1425 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1426 &left, &top, &width, &height,
1427 const_cast<const void**>(&buffer),
1428 &rowBytes)) {
1429 return false;
1430 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431
bsalomon@google.comc6980972011-11-02 19:57:21 +00001432 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001433 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001435 switch (tgt->getResolveType()) {
1436 case GrGLRenderTarget::kCantResolve_ResolveType:
1437 return false;
1438 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001439 artr.set(this->drawState(), target);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001440 this->flushRenderTarget(&GrIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001441 break;
1442 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001443 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001444 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001445 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1446 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 break;
1448 default:
1449 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001450 }
1451
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001452 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001453
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001454 // the read rect is viewport-relative
1455 GrGLIRect readRect;
1456 readRect.setRelativeTo(glvp, left, top, width, height);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001457
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001458 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001459 if (0 == rowBytes) {
1460 rowBytes = tightRowBytes;
1461 }
1462 size_t readDstRowBytes = tightRowBytes;
1463 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001464
bsalomon@google.comc6980972011-11-02 19:57:21 +00001465 // determine if GL can read using the passed rowBytes or if we need
1466 // a scratch buffer.
1467 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1468 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001469 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001470 GrAssert(!(rowBytes % sizeof(GrColor)));
1471 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1472 readDstRowBytes = rowBytes;
1473 } else {
1474 scratch.reset(tightRowBytes * height);
1475 readDst = scratch.get();
1476 }
1477 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001478 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001479 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1480 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001481 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1482 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001483 format, type, readDst));
1484 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001485 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001486 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1487 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001488 if (!invertY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001489 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
1490 invertY = true;
1491 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001492
1493 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001494 // API presents top-to-bottom. We must preserve the padding contents. Note
1495 // that the above readPixels did not overwrite the padding.
1496 if (readDst == buffer) {
1497 GrAssert(rowBytes == readDstRowBytes);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001498 if (!invertY) {
1499 scratch.reset(tightRowBytes);
1500 void* tmpRow = scratch.get();
1501 // flip y in-place by rows
1502 const int halfY = height >> 1;
1503 char* top = reinterpret_cast<char*>(buffer);
1504 char* bottom = top + (height - 1) * rowBytes;
1505 for (int y = 0; y < halfY; y++) {
1506 memcpy(tmpRow, top, tightRowBytes);
1507 memcpy(top, bottom, tightRowBytes);
1508 memcpy(bottom, tmpRow, tightRowBytes);
1509 top += rowBytes;
1510 bottom -= rowBytes;
1511 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001512 }
1513 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001514 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001515 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001516 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001518 char* dst = reinterpret_cast<char*>(buffer);
1519 if (!invertY) {
1520 dst += (height-1) * rowBytes;
1521 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 for (int y = 0; y < height; y++) {
1523 memcpy(dst, src, tightRowBytes);
1524 src += readDstRowBytes;
bsalomon@google.comc4364992011-11-07 15:54:49 +00001525 if (invertY) {
1526 dst += rowBytes;
1527 } else {
1528 dst -= rowBytes;
1529 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 }
1531 }
1532 return true;
1533}
1534
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001535void GrGpuGL::flushRenderTarget(const GrIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001536
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001537 GrGLRenderTarget* rt =
1538 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1539 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001540
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001541 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001542 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 #if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001544 GrGLenum status;
1545 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001546 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001547 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001548 }
1549 #endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001550 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001551 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001552 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001553 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001554 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001555 }
1556 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001557 if (NULL == bound || !bound->isEmpty()) {
1558 rt->flagAsNeedingResolve(bound);
1559 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001560}
1561
twiz@google.com0f31ca72011-03-18 17:38:11 +00001562GrGLenum gPrimitiveType2GLMode[] = {
1563 GR_GL_TRIANGLES,
1564 GR_GL_TRIANGLE_STRIP,
1565 GR_GL_TRIANGLE_FAN,
1566 GR_GL_POINTS,
1567 GR_GL_LINES,
1568 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001569};
1570
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571#define SWAP_PER_DRAW 0
1572
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001573#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001574 #if GR_MAC_BUILD
1575 #include <AGL/agl.h>
1576 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001577 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001578 void SwapBuf() {
1579 DWORD procID = GetCurrentProcessId();
1580 HWND hwnd = GetTopWindow(GetDesktopWindow());
1581 while(hwnd) {
1582 DWORD wndProcID = 0;
1583 GetWindowThreadProcessId(hwnd, &wndProcID);
1584 if(wndProcID == procID) {
1585 SwapBuffers(GetDC(hwnd));
1586 }
1587 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1588 }
1589 }
1590 #endif
1591#endif
1592
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001593void GrGpuGL::onGpuDrawIndexed(GrPrimitiveType type,
1594 uint32_t startVertex,
1595 uint32_t startIndex,
1596 uint32_t vertexCount,
1597 uint32_t indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001598 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1599
twiz@google.com0f31ca72011-03-18 17:38:11 +00001600 GrGLvoid* indices = (GrGLvoid*)(sizeof(uint16_t) * startIndex);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00001601
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001602 GrAssert(NULL != fHWGeometryState.fIndexBuffer);
1603 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1604
1605 // our setupGeometry better have adjusted this to zero since
1606 // DrawElements always draws from the begining of the arrays for idx 0.
1607 GrAssert(0 == startVertex);
reed@google.comac10a2d2010-12-22 21:39:39 +00001608
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001609 GL_CALL(DrawElements(gPrimitiveType2GLMode[type], indexCount,
1610 GR_GL_UNSIGNED_SHORT, indices));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001611#if SWAP_PER_DRAW
1612 glFlush();
1613 #if GR_MAC_BUILD
1614 aglSwapBuffers(aglGetCurrentContext());
1615 int set_a_break_pt_here = 9;
1616 aglSwapBuffers(aglGetCurrentContext());
1617 #elif GR_WIN32_BUILD
1618 SwapBuf();
1619 int set_a_break_pt_here = 9;
1620 SwapBuf();
1621 #endif
1622#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001623}
1624
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00001625void GrGpuGL::onGpuDrawNonIndexed(GrPrimitiveType type,
1626 uint32_t startVertex,
1627 uint32_t vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 GrAssert((size_t)type < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
1629
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001630 GrAssert(NULL != fHWGeometryState.fVertexBuffer);
1631
1632 // our setupGeometry better have adjusted this to zero.
1633 // DrawElements doesn't take an offset so we always adjus the startVertex.
1634 GrAssert(0 == startVertex);
1635
1636 // pass 0 for parameter first. We have to adjust gl*Pointer() to
1637 // account for startVertex in the DrawElements case. So we always
1638 // rely on setupGeometry to have accounted for startVertex.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001639 GL_CALL(DrawArrays(gPrimitiveType2GLMode[type], 0, vertexCount));
bsalomon@google.comd302f142011-03-03 13:54:13 +00001640#if SWAP_PER_DRAW
1641 glFlush();
1642 #if GR_MAC_BUILD
1643 aglSwapBuffers(aglGetCurrentContext());
1644 int set_a_break_pt_here = 9;
1645 aglSwapBuffers(aglGetCurrentContext());
1646 #elif GR_WIN32_BUILD
1647 SwapBuf();
1648 int set_a_break_pt_here = 9;
1649 SwapBuf();
1650 #endif
1651#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001652}
1653
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001654namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001655
1656static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001657const GrStencilSettings& winding_nv_path_stencil_settings() {
1658 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1659 kIncClamp_StencilOp,
1660 kIncClamp_StencilOp,
1661 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001662 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001663 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1664}
1665const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1666 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1667 kInvert_StencilOp,
1668 kInvert_StencilOp,
1669 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001670 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001671 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1672}
1673}
1674
1675
1676void GrGpuGL::setStencilPathSettings(const GrPath&,
1677 GrPathFill fill,
1678 GrStencilSettings* settings) {
1679 switch (fill) {
1680 case kEvenOdd_GrPathFill:
1681 *settings = even_odd_nv_path_stencil_settings();
1682 return;
1683 case kWinding_GrPathFill:
1684 *settings = winding_nv_path_stencil_settings();
1685 return;
1686 default:
1687 GrCrash("Unexpected path fill.");
1688 }
1689}
1690
1691void GrGpuGL::onGpuStencilPath(const GrPath* path, GrPathFill fill) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001692 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001693
1694 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1695 GrDrawState* drawState = this->drawState();
1696 GrAssert(NULL != drawState->getRenderTarget());
1697 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1698 return;
1699 }
1700
1701 // Decide how to manipulate the stencil buffer based on the fill rule.
1702 // Also, assert that the stencil settings we set in setStencilPathSettings
1703 // are present.
1704 GrAssert(!fStencilSettings.isTwoSided());
1705 GrGLenum fillMode;
1706 switch (fill) {
1707 case kWinding_GrPathFill:
1708 fillMode = GR_GL_COUNT_UP;
1709 GrAssert(kIncClamp_StencilOp ==
1710 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1711 GrAssert(kIncClamp_StencilOp ==
1712 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1713 break;
1714 case kEvenOdd_GrPathFill:
1715 fillMode = GR_GL_INVERT;
1716 GrAssert(kInvert_StencilOp ==
1717 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1718 GrAssert(kInvert_StencilOp ==
1719 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1720 break;
1721 default:
1722 // Only the above two fill rules are allowed.
1723 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001724 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001725 }
1726 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1727 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001728}
1729
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001730void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
1731
1732 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001734 if (rt->needsResolve()) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001735 GrAssert(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType());
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 GrAssert(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001737 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1738 rt->renderFBOID()));
1739 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER,
1740 rt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 // make sure we go through flushRenderTarget() since we've modified
1742 // the bound DRAW FBO ID.
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001743 fHWBoundRenderTarget = NULL;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001744 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001745 const GrIRect dirtyRect = rt->getResolveRect();
1746 GrGLIRect r;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001747 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001748 dirtyRect.width(), dirtyRect.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001749
bsalomon@google.coma3201942012-06-21 19:58:20 +00001750 GrAutoTRestore<ScissorState> asr;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001751 if (GrGLCaps::kAppleES_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001752 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001753 asr.reset(&fScissorState);
1754 fScissorState.fEnabled = true;
1755 fScissorState.fRect = dirtyRect;
1756 this->flushScissor();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001757 GL_CALL(ResolveMultisampleFramebuffer());
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 } else {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001759 if (GrGLCaps::kDesktopARB_MSFBOType != this->glCaps().msFBOType()) {
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001760 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001761 GrAssert(GrGLCaps::kDesktopEXT_MSFBOType ==
1762 this->glCaps().msFBOType());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001763 asr.reset(&fScissorState);
1764 fScissorState.fEnabled = false;
1765 this->flushScissor();
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001766 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001767 int right = r.fLeft + r.fWidth;
1768 int top = r.fBottom + r.fHeight;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001769 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1770 r.fLeft, r.fBottom, right, top,
1771 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001773 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 }
1775}
1776
bsalomon@google.com411dad02012-06-05 20:24:20 +00001777namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001778
bsalomon@google.com411dad02012-06-05 20:24:20 +00001779GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1780 static const GrGLenum gTable[] = {
1781 GR_GL_ALWAYS, // kAlways_StencilFunc
1782 GR_GL_NEVER, // kNever_StencilFunc
1783 GR_GL_GREATER, // kGreater_StencilFunc
1784 GR_GL_GEQUAL, // kGEqual_StencilFunc
1785 GR_GL_LESS, // kLess_StencilFunc
1786 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1787 GR_GL_EQUAL, // kEqual_StencilFunc,
1788 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1789 };
1790 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1791 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1792 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1793 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1794 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1795 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1796 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1797 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1798 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1799 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1800
1801 return gTable[basicFunc];
1802}
1803
1804GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1805 static const GrGLenum gTable[] = {
1806 GR_GL_KEEP, // kKeep_StencilOp
1807 GR_GL_REPLACE, // kReplace_StencilOp
1808 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1809 GR_GL_INCR, // kIncClamp_StencilOp
1810 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1811 GR_GL_DECR, // kDecClamp_StencilOp
1812 GR_GL_ZERO, // kZero_StencilOp
1813 GR_GL_INVERT, // kInvert_StencilOp
1814 };
1815 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1816 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1817 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1818 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1819 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1820 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1821 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1822 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1823 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1824 GrAssert((unsigned) op < kStencilOpCount);
1825 return gTable[op];
1826}
1827
1828void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001829 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001830 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001831 GrStencilSettings::Face grFace) {
1832 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1833 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1834 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1835
1836 GrGLint ref = settings.funcRef(grFace);
1837 GrGLint mask = settings.funcMask(grFace);
1838 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001839
1840 if (GR_GL_FRONT_AND_BACK == glFace) {
1841 // we call the combined func just in case separate stencil is not
1842 // supported.
1843 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1844 GR_GL_CALL(gl, StencilMask(writeMask));
1845 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1846 } else {
1847 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1848 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1849 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1850 }
1851}
1852}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001853
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001854void GrGpuGL::flushStencil(DrawType type) {
1855 if (kStencilPath_DrawType == type) {
1856 GrAssert(!fStencilSettings.isTwoSided());
1857 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1858 // that draws the path to the SB (glStencilFillPath)
1859 GrGLenum func =
1860 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1861 GL_CALL(PathStencilFunc(func,
1862 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1863 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1864 } else if (fHWStencilSettings != fStencilSettings) {
1865 if (fStencilSettings.isDisabled()) {
1866 if (kNo_TriState != fHWStencilTestEnabled) {
1867 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1868 fHWStencilTestEnabled = kNo_TriState;
1869 }
1870 } else {
1871 if (kYes_TriState != fHWStencilTestEnabled) {
1872 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1873 fHWStencilTestEnabled = kYes_TriState;
1874 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001875 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001876 if (!fStencilSettings.isDisabled()) {
bsalomon@google.comf6601872012-08-28 21:11:35 +00001877 if (this->getCaps().twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001878 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001879 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001880 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001881 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001882 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001883 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001884 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001885 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001886 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001887 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001888 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001889 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001890 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001891 }
1892 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001893 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001894 }
1895}
1896
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001897void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001898 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001900 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1901 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001902 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001903 bool smoothLines = false;
1904
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001905 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001906 smoothLines = this->willUseHWAALines();
1907 if (smoothLines) {
1908 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1909 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1910 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1911 // must disable msaa to use line smoothing
1912 if (rt->isMultisampled() &&
1913 kNo_TriState != fHWAAState.fMSAAEnabled) {
1914 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1915 fHWAAState.fMSAAEnabled = kNo_TriState;
1916 }
1917 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001918 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001919 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1920 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1921 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1922 }
1923 }
1924 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001925 if (!smoothLines && rt->isMultisampled()) {
1926 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1927 // convex hulls of each segment appear to get filled.
1928 bool enableMSAA = kStencilPath_DrawType == type ||
1929 this->getDrawState().isHWAntialiasState();
1930 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001931 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1932 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1933 fHWAAState.fMSAAEnabled = kYes_TriState;
1934 }
1935 } else {
1936 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1937 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1938 fHWAAState.fMSAAEnabled = kNo_TriState;
1939 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001940 }
1941 }
1942 }
1943}
1944
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001945void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001946 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001947 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001948 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001949 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001950 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001951 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001952 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001953 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1954 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1955 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1956 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1957 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1958 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
1960 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001961 // any optimization to disable blending should
1962 // have already been applied and tweaked the coeffs
1963 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001964 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1965 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001966 if (blendOff) {
1967 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001968 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001969 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001971 } else {
1972 if (kYes_TriState != fHWBlendState.fEnabled) {
1973 GL_CALL(Enable(GR_GL_BLEND));
1974 fHWBlendState.fEnabled = kYes_TriState;
1975 }
1976 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1977 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001978 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1979 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001980 fHWBlendState.fSrcCoeff = srcCoeff;
1981 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001983 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001984 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1985 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001986 (!fHWBlendState.fConstColorValid ||
1987 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com0650e812011-04-08 18:07:53 +00001988
1989 float c[] = {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001990 GrColorUnpackR(blendConst) / 255.f,
1991 GrColorUnpackG(blendConst) / 255.f,
1992 GrColorUnpackB(blendConst) / 255.f,
1993 GrColorUnpackA(blendConst) / 255.f
bsalomon@google.com0650e812011-04-08 18:07:53 +00001994 };
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001995 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001996 fHWBlendState.fConstColor = blendConst;
1997 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
1999 }
2000 }
2001}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002002namespace {
2003
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002004// get_swizzle is only called from this .cpp so it is OK to inline it here
2005inline const GrGLenum* get_swizzle(GrPixelConfig config,
2006 const GrSamplerState& sampler,
2007 const GrGLCaps& glCaps) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002008 if (GrPixelConfigIsAlphaOnly(config)) {
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002009 if (glCaps.textureRedSupport()) {
2010 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED,
2011 GR_GL_RED, GR_GL_RED };
2012 return gRedSmear;
2013 } else {
2014 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
2015 GR_GL_ALPHA, GR_GL_ALPHA };
2016 return gAlphaSmear;
2017 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002018 } else {
2019 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN,
2020 GR_GL_BLUE, GR_GL_ALPHA };
2021 return gStraight;
2022 }
2023}
2024
2025void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00002026 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
2027 GR_GL_TEXTURE_SWIZZLE_RGBA,
2028 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002029}
bsalomon@google.comb8670992012-07-25 21:27:09 +00002030
bsalomon@google.com6c76d242012-09-07 16:52:24 +00002031inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002032 static const GrGLenum gWrapModes[] = {
2033 GR_GL_CLAMP_TO_EDGE,
2034 GR_GL_REPEAT,
2035 GR_GL_MIRRORED_REPEAT
2036 };
2037 GrAssert((unsigned) tm <= SK_ARRAY_COUNT(gWrapModes));
2038 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2039 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2040 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2041 return gWrapModes[tm];
2042}
2043
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002044}
2045
bsalomon@google.com4c883782012-06-04 19:05:11 +00002046void GrGpuGL::flushBoundTextureAndParams(int stage) {
2047 GrDrawState* drawState = this->drawState();
bsalomon@google.com0982d352012-07-31 15:33:25 +00002048 // FIXME: Assuming at most one texture per custom stage
bsalomon@google.comcddaf342012-07-30 13:09:05 +00002049 const GrCustomStage* customStage = drawState->sampler(stage)->getCustomStage();
2050 GrGLTexture* nextTexture = static_cast<GrGLTexture*>(customStage->texture(0));
bsalomon@google.com0982d352012-07-31 15:33:25 +00002051 if (NULL != nextTexture) {
2052 // Currently we always use the texture params from the GrSamplerState. Soon custom stages
2053 // will provide their own params.
2054 const GrTextureParams& texParams = drawState->getSampler(stage).getTextureParams();
2055 this->flushBoundTextureAndParams(stage, texParams, nextTexture);
2056 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002057}
2058
bsalomon@google.comb8670992012-07-25 21:27:09 +00002059void GrGpuGL::flushBoundTextureAndParams(int stage,
2060 const GrTextureParams& params,
2061 GrGLTexture* nextTexture) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002062 GrDrawState* drawState = this->drawState();
2063
bsalomon@google.com4c883782012-06-04 19:05:11 +00002064 // true for now, but maybe not with GrEffect.
2065 GrAssert(NULL != nextTexture);
bsalomon@google.comb8670992012-07-25 21:27:09 +00002066 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2067 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00002068 // out of the "last != next" check.
bsalomon@google.comb8670992012-07-25 21:27:09 +00002069 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(nextTexture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002070 if (NULL != texRT) {
2071 this->onResolveRenderTarget(texRT);
2072 }
2073
2074 if (fHWBoundTextures[stage] != nextTexture) {
2075 this->setTextureUnit(stage);
2076 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, nextTexture->textureID()));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002077 //GrPrintf("---- bindtexture %d\n", nextTexture->textureID());
2078 fHWBoundTextures[stage] = nextTexture;
2079 }
2080
bsalomon@google.com4c883782012-06-04 19:05:11 +00002081 ResetTimestamp timestamp;
2082 const GrGLTexture::TexParams& oldTexParams =
2083 nextTexture->getCachedTexParams(&timestamp);
2084 bool setAll = timestamp < this->getResetTimestamp();
2085 GrGLTexture::TexParams newTexParams;
2086
bsalomon@google.comb8670992012-07-25 21:27:09 +00002087 newTexParams.fFilter = params.isBilerp() ? GR_GL_LINEAR : GR_GL_NEAREST;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002088
bsalomon@google.comb8670992012-07-25 21:27:09 +00002089 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2090 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002091 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.comb8670992012-07-25 21:27:09 +00002092 get_swizzle(nextTexture->config(), drawState->getSampler(stage), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002093 sizeof(newTexParams.fSwizzleRGBA));
2094 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
2095 this->setTextureUnit(stage);
2096 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2097 GR_GL_TEXTURE_MAG_FILTER,
2098 newTexParams.fFilter));
2099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2100 GR_GL_TEXTURE_MIN_FILTER,
2101 newTexParams.fFilter));
2102 }
2103 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
2104 this->setTextureUnit(stage);
2105 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2106 GR_GL_TEXTURE_WRAP_S,
2107 newTexParams.fWrapS));
2108 }
2109 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
2110 this->setTextureUnit(stage);
2111 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2112 GR_GL_TEXTURE_WRAP_T,
2113 newTexParams.fWrapT));
2114 }
2115 if (this->glCaps().textureSwizzleSupport() &&
2116 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2117 oldTexParams.fSwizzleRGBA,
2118 sizeof(newTexParams.fSwizzleRGBA)))) {
2119 this->setTextureUnit(stage);
2120 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2121 this->glInterface());
2122 }
2123 nextTexture->setCachedTexParams(newTexParams,
2124 this->getResetTimestamp());
2125}
2126
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002127void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002128
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002129 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002130
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002131 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002132 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002133 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002134 fHWDitherEnabled = kYes_TriState;
2135 }
2136 } else {
2137 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002138 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002139 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002140 }
2141 }
2142
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002143 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002144 if (kNo_TriState != fHWWriteToColor) {
2145 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2146 GR_GL_FALSE, GR_GL_FALSE));
2147 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002148 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002149 } else {
2150 if (kYes_TriState != fHWWriteToColor) {
2151 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2152 fHWWriteToColor = kYes_TriState;
2153 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 }
2155
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002156 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002157 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002158 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002159 GL_CALL(Enable(GR_GL_CULL_FACE));
2160 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002161 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002162 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002163 GL_CALL(Enable(GR_GL_CULL_FACE));
2164 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002165 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002166 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002167 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002168 break;
2169 default:
2170 GrCrash("Unknown draw face.");
2171 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002172 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002174}
2175
2176void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002177 if (fHWGeometryState.fVertexBuffer != buffer) {
2178 fHWGeometryState.fArrayPtrsDirty = true;
2179 fHWGeometryState.fVertexBuffer = buffer;
2180 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002181}
2182
2183void GrGpuGL::notifyVertexBufferDelete(const GrGLVertexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002184 if (fHWGeometryState.fVertexBuffer == buffer) {
2185 // deleting bound buffer does implied bind to 0
2186 fHWGeometryState.fVertexBuffer = NULL;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002187 fHWGeometryState.fArrayPtrsDirty = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00002188 }
2189}
2190
2191void GrGpuGL::notifyIndexBufferBind(const GrGLIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002192 fHWGeometryState.fIndexBuffer = buffer;
reed@google.comac10a2d2010-12-22 21:39:39 +00002193}
2194
2195void GrGpuGL::notifyIndexBufferDelete(const GrGLIndexBuffer* buffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +00002196 if (fHWGeometryState.fIndexBuffer == buffer) {
2197 // deleting bound buffer does implied bind to 0
2198 fHWGeometryState.fIndexBuffer = NULL;
2199 }
2200}
2201
reed@google.comac10a2d2010-12-22 21:39:39 +00002202void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2203 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002204 if (fHWBoundRenderTarget == renderTarget) {
2205 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002206 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002207}
2208
2209void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002210 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002211 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002212 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002213 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002214 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002216}
2217
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002218bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2219 bool getSizedInternalFormat,
2220 GrGLenum* internalFormat,
2221 GrGLenum* externalFormat,
2222 GrGLenum* externalType) {
2223 GrGLenum dontCare;
2224 if (NULL == internalFormat) {
2225 internalFormat = &dontCare;
2226 }
2227 if (NULL == externalFormat) {
2228 externalFormat = &dontCare;
2229 }
2230 if (NULL == externalType) {
2231 externalType = &dontCare;
2232 }
2233
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002235 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002236 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002237 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002238 if (getSizedInternalFormat) {
2239 *internalFormat = GR_GL_RGBA8;
2240 } else {
2241 *internalFormat = GR_GL_RGBA;
2242 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002243 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002244 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002245 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002246 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002247 return false;
2248 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002249 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002250 if (getSizedInternalFormat) {
2251 *internalFormat = GR_GL_BGRA8;
2252 } else {
2253 *internalFormat = GR_GL_BGRA;
2254 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002255 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002256 if (getSizedInternalFormat) {
2257 *internalFormat = GR_GL_RGBA8;
2258 } else {
2259 *internalFormat = GR_GL_RGBA;
2260 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002261 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002262 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002263 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002264 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002265 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002266 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002267 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002268 if (getSizedInternalFormat) {
2269 if (this->glBinding() == kDesktop_GrGLBinding) {
2270 return false;
2271 } else {
2272 *internalFormat = GR_GL_RGB565;
2273 }
2274 } else {
2275 *internalFormat = GR_GL_RGB;
2276 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002277 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002278 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002279 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002280 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002281 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002282 if (getSizedInternalFormat) {
2283 *internalFormat = GR_GL_RGBA4;
2284 } else {
2285 *internalFormat = GR_GL_RGBA;
2286 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002287 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002289 case kIndex_8_GrPixelConfig:
bsalomon@google.comf6601872012-08-28 21:11:35 +00002290 if (this->getCaps().eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002291 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002292 // glCompressedTexImage doesn't take external params
2293 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002294 // no sized/unsized internal format distinction here
2295 *internalFormat = GR_GL_PALETTE8_RGBA8;
2296 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002297 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002298 } else {
2299 return false;
2300 }
2301 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002302 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002303 if (this->glCaps().textureRedSupport()) {
2304 *internalFormat = GR_GL_RED;
2305 *externalFormat = GR_GL_RED;
2306 if (getSizedInternalFormat) {
2307 *internalFormat = GR_GL_R8;
2308 } else {
2309 *internalFormat = GR_GL_RED;
2310 }
2311 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002312 } else {
2313 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002314 *externalFormat = GR_GL_ALPHA;
2315 if (getSizedInternalFormat) {
2316 *internalFormat = GR_GL_ALPHA8;
2317 } else {
2318 *internalFormat = GR_GL_ALPHA;
2319 }
2320 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002321 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002322 break;
2323 default:
2324 return false;
2325 }
2326 return true;
2327}
2328
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002329void GrGpuGL::setTextureUnit(int unit) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002330 GrAssert(unit >= 0 && unit < GrDrawState::kNumStages);
bsalomon@google.com49209392012-06-05 15:13:46 +00002331 if (fHWActiveTextureUnitIdx != unit) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002332 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002333 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002334 }
2335}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002336
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002337void GrGpuGL::setSpareTextureUnit() {
bsalomon@google.com49209392012-06-05 15:13:46 +00002338 if (fHWActiveTextureUnitIdx != (GR_GL_TEXTURE0 + SPARE_TEX_UNIT)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002339 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + SPARE_TEX_UNIT));
bsalomon@google.com49209392012-06-05 15:13:46 +00002340 fHWActiveTextureUnitIdx = SPARE_TEX_UNIT;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002341 }
2342}
2343
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002344void GrGpuGL::setBuffers(bool indexed,
2345 int* extraVertexOffset,
2346 int* extraIndexOffset) {
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002347
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002348 GrAssert(NULL != extraVertexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002349
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002350 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
2351
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002352 GrGLVertexBuffer* vbuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002353 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002354 case kBuffer_GeometrySrcType:
2355 *extraVertexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002356 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002357 break;
2358 case kArray_GeometrySrcType:
2359 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002360 this->finalizeReservedVertices();
2361 *extraVertexOffset = geoPoolState.fPoolStartVertex;
2362 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002363 break;
2364 default:
2365 vbuf = NULL; // suppress warning
2366 GrCrash("Unknown geometry src type!");
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002367 }
2368
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002369 GrAssert(NULL != vbuf);
2370 GrAssert(!vbuf->isLocked());
2371 if (fHWGeometryState.fVertexBuffer != vbuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002372 GL_CALL(BindBuffer(GR_GL_ARRAY_BUFFER, vbuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002373 fHWGeometryState.fArrayPtrsDirty = true;
2374 fHWGeometryState.fVertexBuffer = vbuf;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002375 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002376
2377 if (indexed) {
2378 GrAssert(NULL != extraIndexOffset);
2379
2380 GrGLIndexBuffer* ibuf;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002381 switch (this->getGeomSrc().fIndexSrc) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002382 case kBuffer_GeometrySrcType:
2383 *extraIndexOffset = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002384 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002385 break;
2386 case kArray_GeometrySrcType:
2387 case kReserved_GeometrySrcType:
bsalomon@google.com25fb21f2011-06-21 18:17:25 +00002388 this->finalizeReservedIndices();
2389 *extraIndexOffset = geoPoolState.fPoolStartIndex;
2390 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002391 break;
2392 default:
2393 ibuf = NULL; // suppress warning
2394 GrCrash("Unknown geometry src type!");
2395 }
2396
2397 GrAssert(NULL != ibuf);
2398 GrAssert(!ibuf->isLocked());
2399 if (fHWGeometryState.fIndexBuffer != ibuf) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002400 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibuf->bufferID()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002401 fHWGeometryState.fIndexBuffer = ibuf;
2402 }
2403 }
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002404}
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +00002405