blob: 0a99258633b03f4a622eb472bfc52e22c9957192 [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.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000018static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000086 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
87 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000088
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
90 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
91 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
92 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
93 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
94 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
96 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
97 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
98 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
100 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
102 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000103
bsalomon@google.com47059542012-06-06 20:51:20 +0000104 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
105 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
107 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000108
109 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000110 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
111 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000112}
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114///////////////////////////////////////////////////////////////////////////////
115
rileya@google.come38160c2012-07-03 18:03:04 +0000116static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000117
robertphillips@google.com6177e692013-02-28 20:16:25 +0000118GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000120 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000121
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000122 SkASSERT(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000123
bsalomon@google.combcce8922013-03-25 15:38:39 +0000124 fCaps.reset(SkRef(ctx.info().caps()));
125
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000126 fHWBoundTextures.reset(ctx.info().caps()->maxFragmentTextureUnits());
127
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000128 fillInConfigRenderableTable();
129
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000130
robertphillips@google.com6177e692013-02-28 20:16:25 +0000131 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000134 const GrGLubyte* vendor;
135 const GrGLubyte* renderer;
136 const GrGLubyte* version;
137 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
138 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
139 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
141 this);
142 GrPrintf("------ VENDOR %s\n", vendor);
143 GrPrintf("------ RENDERER %s\n", renderer);
144 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000145 GrPrintf("------ EXTENSIONS\n");
146 ctx.info().extensions().print();
147 GrPrintf("\n");
bsalomon@google.combcce8922013-03-25 15:38:39 +0000148 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000149 }
150
robertphillips@google.com6177e692013-02-28 20:16:25 +0000151 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContext()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000152
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000154
bsalomon@google.comfe676522011-06-17 18:12:21 +0000155 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000156 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157}
158
159GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000160 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000161 // detach the current program so there is no confusion on OpenGL's part
162 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164 GL_CALL(UseProgram(0));
165 }
166
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000167 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000168
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000169 // This must be called by before the GrDrawTarget destructor
170 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000171 // This subclass must do this before the base class destructor runs
172 // since we will unref the GrGLInterface.
173 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000174}
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176///////////////////////////////////////////////////////////////////////////////
177
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000178void GrGpuGL::fillInConfigRenderableTable() {
179
180 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181 // no support for render targets unless the GL_ARB_framebuffer_object
182 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
183 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 // probably don't get R8 in this case.
185
186 // OpenGL 3.0
187 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
188 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
189
190 // >= OpenGL 3.1
191 // base color renderable: RED, RG, RGB, and RGBA
192 // sized derivatives: R8, RGBA4, RGBA8
193 // if the GL_ARB_compatibility extension is supported then we get back
194 // support for GL_ALPHA and ALPHA8
195
196 // GL_EXT_bgra adds BGRA render targets to any version
197
198 // ES 2.0
199 // color renderable: RGBA4, RGB5_A1, RGB565
200 // GL_EXT_texture_rg adds support for R8 as a color render target
201 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000202 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000203
commit-bot@chromium.orgc5dffe42013-08-20 15:25:21 +0000204 // ES 3.0
205 // Same as ES 2.0 except R8 and RGBA8 are supported without extensions (the functions called
206 // below already account for this).
207
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000208 if (kDesktop_GrGLBinding == this->glBinding()) {
209 // Post 3.0 we will get R8
210 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
211 if (this->glVersion() >= GR_GL_VER(3,0) ||
212 this->hasExtension("GL_ARB_framebuffer_object")) {
213 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
214 }
215 } else {
216 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000218 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000219 }
220
221 if (kDesktop_GrGLBinding != this->glBinding()) {
222 // only available in ES
223 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
224 }
225
reed@google.com6ba45722013-06-21 18:30:53 +0000226 // we no longer support 444 as a render target
227 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = false;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000228
229 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000230 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000231 }
232
233 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000234 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000235 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000236}
237
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000238GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
239 GrPixelConfig surfaceConfig) const {
240 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000241 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000242 } else if (fGLContext.info().isMesa() &&
243 GrBytesPerPixel(readConfig) == 4 &&
244 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000245 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
246 // Perhaps this should be guarded by some compiletime or runtime check.
247 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000248 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
249 !this->glCaps().readPixelsSupported(this->glInterface(),
250 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
251 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000252 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000253 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000254 }
255}
256
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000257GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
258 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000259 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
260 return kBGRA_8888_GrPixelConfig;
261 } else {
262 return writeConfig;
263 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000264}
265
266bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
267 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
268 return false;
269 }
bsalomon@google.com791816a2013-08-15 18:54:39 +0000270 if (srcConfig != texture->config() && kES_GrGLBinding == this->glBinding()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000271 // In general ES2 requires the internal format of the texture and the format of the src
272 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
273 // texture. It depends upon which extension added BGRA. The Apple extension allows it
274 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
275 // internal format).
276 if (this->glCaps().bgraFormatSupport() &&
277 !this->glCaps().bgraIsInternalFormat() &&
278 kBGRA_8888_GrPixelConfig == srcConfig &&
279 kRGBA_8888_GrPixelConfig == texture->config()) {
280 return true;
281 } else {
282 return false;
283 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000284 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000285 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000286 }
287}
288
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000289bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
290 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
291}
292
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000293void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000294 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 if (resetBits & kMisc_GrGLBackendState) {
296 GL_CALL(Disable(GR_GL_DEPTH_TEST));
297 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000298
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000299 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
300 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000301
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000302 if (kDesktop_GrGLBinding == this->glBinding()) {
303 // Desktop-only state that we never change
304 if (!this->glCaps().isCoreProfile()) {
305 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
306 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
307 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
308 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
309 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
310 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
311 }
312 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
313 // core profile. This seems like a bug since the core spec removes any mention of
314 // GL_ARB_imaging.
315 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
316 GL_CALL(Disable(GR_GL_COLOR_TABLE));
317 }
318 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
319 // Since ES doesn't support glPointSize at all we always use the VS to
320 // set the point size
321 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
322
323 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
324 // currently part of our gl interface. There are probably others as
325 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000326 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 fHWWriteToColor = kUnknown_TriState;
328 // we only ever use lines in hairline mode
329 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000330 }
edisonn@google.comba669992013-06-28 16:03:21 +0000331
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000332 if (resetBits & kAA_GrGLBackendState) {
333 fHWAAState.invalidate();
334 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000335
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000336 // invalid
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000337 if (resetBits & kTextureBinding_GrGLBackendState) {
338 fHWActiveTextureUnitIdx = -1;
339 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
340 fHWBoundTextures[s] = NULL;
341 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000342 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000343
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000344 if (resetBits & kBlend_GrGLBackendState) {
345 fHWBlendState.invalidate();
346 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000347
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000348 if (resetBits & kView_GrGLBackendState) {
349 fHWScissorSettings.invalidate();
350 fHWViewport.invalidate();
351 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000353 if (resetBits & kStencil_GrGLBackendState) {
354 fHWStencilSettings.invalidate();
355 fHWStencilTestEnabled = kUnknown_TriState;
356 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000357
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000358 // Vertex
359 if (resetBits & kVertex_GrGLBackendState) {
360 fHWGeometryState.invalidate();
361 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000363 if (resetBits & kRenderTarget_GrGLBackendState) {
364 fHWBoundRenderTarget = NULL;
365 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000366
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000367 if (resetBits & kPathStencil_GrGLBackendState) {
368 fHWPathStencilMatrixState.invalidate();
369 if (this->caps()->pathStencilingSupport()) {
370 // we don't use the model view matrix.
371 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
372 GL_CALL(LoadIdentity());
373 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000374 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000375
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000376 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000377 if (resetBits & kPixelStore_GrGLBackendState) {
378 if (this->glCaps().unpackRowLengthSupport()) {
379 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
380 }
381 if (this->glCaps().packRowLengthSupport()) {
382 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
383 }
384 if (this->glCaps().unpackFlipYSupport()) {
385 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
386 }
387 if (this->glCaps().packFlipYSupport()) {
388 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
389 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000390 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000391
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000392 if (resetBits & kProgram_GrGLBackendState) {
393 fHWProgramID = 0;
394 fSharedGLProgramState.invalidate();
395 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000396}
397
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000398namespace {
399
400GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
401 // By default, GrRenderTargets are GL's normal orientation so that they
402 // can be drawn to by the outside world without the client having
403 // to render upside down.
404 if (kDefault_GrSurfaceOrigin == origin) {
405 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
406 } else {
407 return origin;
408 }
409}
410
411}
412
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000413GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000414 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 return NULL;
416 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000417
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000418 if (0 == desc.fTextureHandle) {
419 return NULL;
420 }
421
bsalomon@google.combcce8922013-03-25 15:38:39 +0000422 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000423 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
424 return NULL;
425 }
426
427 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000428 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000429 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000430 glTexDesc.fWidth = desc.fWidth;
431 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000432 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000433 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000434 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000435 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000436 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000437 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
438 // assuming the old behaviour, which is that backend textures are always
439 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
440 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
441 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
442 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
443 } else {
444 glTexDesc.fOrigin = desc.fOrigin;
445 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000446
447 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000448 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000449 GrGLRenderTarget::Desc glRTDesc;
450 glRTDesc.fRTFBOID = 0;
451 glRTDesc.fTexFBOID = 0;
452 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000453 glRTDesc.fConfig = desc.fConfig;
454 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000455 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000456 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000457 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
458 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000459 glTexDesc.fTextureID,
460 &glRTDesc)) {
461 return NULL;
462 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000463 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000464 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000465 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000466 }
467 if (NULL == texture) {
468 return NULL;
469 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000470
bsalomon@google.come269f212011-11-07 13:29:52 +0000471 return texture;
472}
473
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000474GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000475 GrGLRenderTarget::Desc glDesc;
476 glDesc.fConfig = desc.fConfig;
477 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
478 glDesc.fMSColorRenderbufferID = 0;
479 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
480 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000481 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000482 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000483
484 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000485 GrGLIRect viewport;
486 viewport.fLeft = 0;
487 viewport.fBottom = 0;
488 viewport.fWidth = desc.fWidth;
489 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000490
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000491 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
492 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000493 if (desc.fStencilBits) {
494 GrGLStencilBuffer::Format format;
495 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
496 format.fPacked = false;
497 format.fStencilBits = desc.fStencilBits;
498 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000499 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000500 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
501 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000502 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000503 0,
504 desc.fWidth,
505 desc.fHeight,
506 desc.fSampleCnt,
507 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000508 tgt->setStencilBuffer(sb);
509 sb->unref();
510 }
511 return tgt;
512}
513
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000514////////////////////////////////////////////////////////////////////////////////
515
bsalomon@google.com9c680582013-02-06 18:17:50 +0000516bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000517 int left, int top, int width, int height,
518 GrPixelConfig config, const void* buffer,
519 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000520 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000521 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000522 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000523 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
524
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000525 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000526 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
527 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000528 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529 desc.fWidth = glTex->width();
530 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000531 desc.fConfig = glTex->config();
532 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000534 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000535
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000536 if (this->uploadTexData(desc, false,
537 left, top, width, height,
538 config, buffer, rowBytes)) {
539 texture->dirtyMipMaps(true);
540 return true;
541 } else {
542 return false;
543 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544}
545
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000546namespace {
547bool adjust_pixel_ops_params(int surfaceWidth,
548 int surfaceHeight,
549 size_t bpp,
550 int* left, int* top, int* width, int* height,
551 const void** data,
552 size_t* rowBytes) {
553 if (!*rowBytes) {
554 *rowBytes = *width * bpp;
555 }
556
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000557 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
558 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000559
560 if (!subRect.intersect(bounds)) {
561 return false;
562 }
563 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
564 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
565
566 *left = subRect.fLeft;
567 *top = subRect.fTop;
568 *width = subRect.width();
569 *height = subRect.height();
570 return true;
571}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000572
573GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
574 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
575 return GR_GL_GET_ERROR(interface);
576 } else {
577 return CHECK_ALLOC_ERROR(interface);
578 }
579}
580
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000581}
582
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000583bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
584 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000585 int left, int top, int width, int height,
586 GrPixelConfig dataConfig,
587 const void* data,
588 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000589 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000590
591 size_t bpp = GrBytesPerPixel(dataConfig);
592 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
593 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000594 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000595 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000596 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000597
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000598 // in case we need a temporary, trimmed copy of the src pixels
599 SkAutoSMalloc<128 * 128> tempStorage;
600
bsalomon@google.com313f0192012-07-10 17:21:02 +0000601 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000602 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000603 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000604 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000605
bsalomon@google.com313f0192012-07-10 17:21:02 +0000606 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
607 // 565 is not a sized internal format on desktop GL. So on desktop with
608 // 565 we always use an unsized internal format to let the system pick
609 // the best sized format to convert the 565 data to. Since TexStorage
610 // only allows sized internal formats we will instead use TexImage2D.
611 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000612 }
613
614 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000615 GrGLenum externalFormat;
616 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000617 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
618 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
619 // size of the internal format whenever possible and so only use a sized internal format when
620 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000621 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000622 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000623 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000624 }
625
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000626 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000627 // paletted textures cannot be updated
628 return false;
629 }
630
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000631 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000632 * check whether to allocate a temporary buffer for flipping y or
633 * because our srcData has extra bytes past each row. If so, we need
634 * to trim those off here, since GL ES may not let us specify
635 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000636 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000637 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000638 bool swFlipY = false;
639 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000640 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000641 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000642 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000643 glFlipY = true;
644 } else {
645 swFlipY = true;
646 }
647 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000648 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000649 // can't use this for flipping, only non-neg values allowed. :(
650 if (rowBytes != trimRowBytes) {
651 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
652 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
653 restoreGLRowLength = true;
654 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000655 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000656 if (trimRowBytes != rowBytes || swFlipY) {
657 // copy data into our new storage, skipping the trailing bytes
658 size_t trimSize = height * trimRowBytes;
659 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000660 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000661 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000662 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000663 char* dst = (char*)tempStorage.reset(trimSize);
664 for (int y = 0; y < height; y++) {
665 memcpy(dst, src, trimRowBytes);
666 if (swFlipY) {
667 src -= rowBytes;
668 } else {
669 src += rowBytes;
670 }
671 dst += trimRowBytes;
672 }
673 // now point data to our copied version
674 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000675 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000677 if (glFlipY) {
678 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
679 }
680 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000681 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000682 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000683 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000684 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000685 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000686 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000687 if (useTexStorage) {
688 // We never resize or change formats of textures. We don't use
689 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000690 GL_ALLOC_CALL(this->glInterface(),
691 TexStorage2D(GR_GL_TEXTURE_2D,
692 1, // levels
693 internalFormat,
694 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000695 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000696 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
697 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
698 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000699 GL_ALLOC_CALL(this->glInterface(),
700 CompressedTexImage2D(GR_GL_TEXTURE_2D,
701 0, // level
702 internalFormat,
703 desc.fWidth, desc.fHeight,
704 0, // border
705 imageSize,
706 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000707 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000708 GL_ALLOC_CALL(this->glInterface(),
709 TexImage2D(GR_GL_TEXTURE_2D,
710 0, // level
711 internalFormat,
712 desc.fWidth, desc.fHeight,
713 0, // border
714 externalFormat, externalType,
715 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000716 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000717 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000718 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000719 if (error != GR_GL_NO_ERROR) {
720 succeeded = false;
721 } else {
722 // if we have data and we used TexStorage to create the texture, we
723 // now upload with TexSubImage.
724 if (NULL != data && useTexStorage) {
725 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
726 0, // level
727 left, top,
728 width, height,
729 externalFormat, externalType,
730 data));
731 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000732 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000733 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000734 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000735 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000736 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000737 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
738 0, // level
739 left, top,
740 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000741 externalFormat, externalType, data));
742 }
743
744 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000745 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000746 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000747 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000748 if (glFlipY) {
749 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
750 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000751 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000752}
753
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000754namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000755bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000756 int sampleCount,
757 GrGLenum format,
758 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000759 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000760 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000761 bool created = false;
762 if (GrGLCaps::kNVDesktop_CoverageAAType ==
bsalomon@google.combcce8922013-03-25 15:38:39 +0000763 ctx.info().caps()->coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000764 const GrGLCaps::MSAACoverageMode& mode =
bsalomon@google.combcce8922013-03-25 15:38:39 +0000765 ctx.info().caps()->getMSAACoverageMode(sampleCount);
robertphillips@google.com6177e692013-02-28 20:16:25 +0000766 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000767 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
768 mode.fCoverageSampleCnt,
769 mode.fColorSampleCnt,
770 format,
771 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000772 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000773 }
774 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000775 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000776 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
777 sampleCount,
778 format,
779 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000780 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000781 }
782 return created;
783}
784}
785
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000786bool GrGpuGL::createRenderTargetObjects(int width, int height,
787 GrGLuint texID,
788 GrGLRenderTarget::Desc* desc) {
789 desc->fMSColorRenderbufferID = 0;
790 desc->fRTFBOID = 0;
791 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000792 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000793
794 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000795
bsalomon@google.comab15d612011-08-09 12:57:56 +0000796 GrGLenum msColorFormat = 0; // suppress warning
797
bsalomon@google.com347c3822013-05-01 20:10:01 +0000798 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
799 goto FAILED;
800 }
801
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000802 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000803 if (!desc->fTexFBOID) {
804 goto FAILED;
805 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000806
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000807
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000808 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
809 // the texture bound to the other. The exception is the IMG multisample extension. With this
810 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
811 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000812 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000813 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
814 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000815 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000816 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000817 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000818 // ES2 and ES3 require sized internal formats for rb storage.
bsalomon@google.com791816a2013-08-15 18:54:39 +0000819 kES_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000820 &msColorFormat,
821 NULL,
822 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000823 goto FAILED;
824 }
825 } else {
826 desc->fRTFBOID = desc->fTexFBOID;
827 }
828
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000829 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000830 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000832 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000833 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000834 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000835 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000836 desc->fSampleCnt,
837 msColorFormat,
838 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000839 goto FAILED;
840 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000841 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000842 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000843 GR_GL_COLOR_ATTACHMENT0,
844 GR_GL_RENDERBUFFER,
845 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000846 if (desc->fCheckAllocation ||
847 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000848 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
849 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
850 goto FAILED;
851 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000852 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000853 }
854 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000855 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000856
bsalomon@google.com347c3822013-05-01 20:10:01 +0000857 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000858 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
859 GR_GL_COLOR_ATTACHMENT0,
860 GR_GL_TEXTURE_2D,
861 texID, 0, desc->fSampleCnt));
862 } else {
863 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
864 GR_GL_COLOR_ATTACHMENT0,
865 GR_GL_TEXTURE_2D,
866 texID, 0));
867 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000868 if (desc->fCheckAllocation ||
869 !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.combcce8922013-03-25 15:38:39 +0000874 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000875 }
876
877 return true;
878
879FAILED:
880 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000881 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000882 }
883 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000884 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000885 }
886 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000887 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 }
889 return false;
890}
891
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000892// good to set a break-point here to know when createTexture fails
893static GrTexture* return_null_texture() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000894// SkASSERT(!"null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000895 return NULL;
896}
897
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000898#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000899static size_t as_size_t(int x) {
900 return x;
901}
902#endif
903
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000904GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000905 const void* srcData,
906 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000907
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000908 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000909 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000910
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000911 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000912 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000913 // We fail if the MSAA was requested and is not available.
914 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
915 //GrPrintf("MSAA RT requested but not supported on this platform.");
916 return return_null_texture();
917 }
918 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000919 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000920
robertphillips@google.com32716282012-06-04 12:48:45 +0000921 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000922 glTexDesc.fWidth = desc.fWidth;
923 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000924 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000925 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000926
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000927 glRTDesc.fMSColorRenderbufferID = 0;
928 glRTDesc.fRTFBOID = 0;
929 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000930 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000931 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000932 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000933
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000934 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000935
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000936 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
937 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000938
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000939 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000940 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000941 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000942 //GrPrintf("MSAA RT requested but not supported on this platform.");
943 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000944 }
945
reed@google.comac10a2d2010-12-22 21:39:39 +0000946 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000947 int maxRTSize = this->caps()->maxRenderTargetSize();
948 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000949 return return_null_texture();
950 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000951 } else {
952 int maxSize = this->caps()->maxTextureSize();
953 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
954 return return_null_texture();
955 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000956 }
957
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000958 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000959 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000960 // provides a hint about how this texture will be used
961 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
962 GR_GL_TEXTURE_USAGE,
963 GR_GL_FRAMEBUFFER_ATTACHMENT));
964 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000965 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000966 return return_null_texture();
967 }
968
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000969 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000970 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000971
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000972 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
973 // drivers have a bug where an FBO won't be complete if it includes a
974 // texture that is not mipmap complete (considering the filter in use).
975 GrGLTexture::TexParams initialTexParams;
976 // we only set a subset here so invalidate first
977 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000978 initialTexParams.fMinFilter = GR_GL_NEAREST;
979 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000980 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
981 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
983 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000984 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000985 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
986 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000987 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000988 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
989 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000990 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000991 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
992 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000993 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000994 if (!this->uploadTexData(glTexDesc, true, 0, 0,
995 glTexDesc.fWidth, glTexDesc.fHeight,
996 desc.fConfig, srcData, rowBytes)) {
997 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
998 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000999 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001000
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001001 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001002 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001003 // unbind the texture from the texture unit before binding it to the frame buffer
1004 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1005
bsalomon@google.com99621082011-11-15 16:47:16 +00001006 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1007 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 glTexDesc.fTextureID,
1009 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001011 return return_null_texture();
1012 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001013 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001014 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001015 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001017 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001018#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001019 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1020 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001021#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 return tex;
1023}
1024
1025namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001026
1027const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1028
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001031
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001032 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001033 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001034 (kUnknownBitCount == format->fTotalBits));
1035 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001036 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001037 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1038 (GrGLint*)&format->fStencilBits);
1039 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001040 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001041 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1042 (GrGLint*)&format->fTotalBits);
1043 format->fTotalBits += format->fStencilBits;
1044 } else {
1045 format->fTotalBits = format->fStencilBits;
1046 }
1047 }
1048}
1049}
1050
1051bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1052 int width, int height) {
1053
1054 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001055 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001056 SkASSERT(rt->asTexture());
1057 SkASSERT(width >= rt->width());
1058 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001059
1060 int samples = rt->numSamples();
1061 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001062 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001063 if (!sbID) {
1064 return false;
1065 }
1066
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001067 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001069 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001070 // we start with the last stencil format that succeeded in hopes
1071 // that we won't go through this loop more than once after the
1072 // first (painful) stencil creation.
1073 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001074 const GrGLCaps::StencilFormat& sFmt =
1075 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001076 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001077 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001079 bool created;
1080 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001081 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001082 samples,
1083 sFmt.fInternalFormat,
1084 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001085 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001086 GL_ALLOC_CALL(this->glInterface(),
1087 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001088 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001089 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001090 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001091 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001093 if (created) {
1094 // After sized formats we attempt an unsized format and take
1095 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001096 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001097 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001098 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001099 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001100 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001101 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1103 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001104 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001105 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 return true;
1107 }
1108 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 }
1110 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001112 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113}
1114
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001115bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1117
1118 GrGLuint fbo = glrt->renderFBOID();
1119
1120 if (NULL == sb) {
1121 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001122 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001123 GR_GL_STENCIL_ATTACHMENT,
1124 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001125 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001126 GR_GL_DEPTH_ATTACHMENT,
1127 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001129 GrGLenum status;
1130 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001131 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132#endif
1133 }
1134 return true;
1135 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001136 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 GrGLuint rb = glsb->renderbufferID();
1138
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001139 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1141 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001142 GR_GL_STENCIL_ATTACHMENT,
1143 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001146 GR_GL_DEPTH_ATTACHMENT,
1147 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001149 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001150 GR_GL_DEPTH_ATTACHMENT,
1151 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001154 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001155 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001156 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1157 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001158 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001159 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001161 if (glsb->format().fPacked) {
1162 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1163 GR_GL_DEPTH_ATTACHMENT,
1164 GR_GL_RENDERBUFFER, 0));
1165 }
1166 return false;
1167 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001168 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001169 rt->config(),
1170 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001173 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001174 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001175}
1176
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001177////////////////////////////////////////////////////////////////////////////////
1178
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001179GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001180 GrGLVertexBuffer::Desc desc;
1181 desc.fDynamic = dynamic;
1182 desc.fSizeInBytes = size;
1183 desc.fIsWrapped = false;
1184
bsalomon@google.com96966a52013-02-21 16:34:21 +00001185 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001186 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001187 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001188 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001189 } else {
1190 GL_CALL(GenBuffers(1, &desc.fID));
1191 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001192 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001193 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1194 // make sure driver can allocate memory for this buffer
1195 GL_ALLOC_CALL(this->glInterface(),
1196 BufferData(GR_GL_ARRAY_BUFFER,
1197 desc.fSizeInBytes,
1198 NULL, // data ptr
1199 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1200 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1201 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001202 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001203 return NULL;
1204 }
1205 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1206 return vertexBuffer;
1207 }
1208 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001210}
1211
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001212GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001213 GrGLIndexBuffer::Desc desc;
1214 desc.fDynamic = dynamic;
1215 desc.fSizeInBytes = size;
1216 desc.fIsWrapped = false;
1217
bsalomon@google.com96966a52013-02-21 16:34:21 +00001218 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001219 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001220 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001222 } else {
1223 GL_CALL(GenBuffers(1, &desc.fID));
1224 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001225 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001226 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1227 // make sure driver can allocate memory for this buffer
1228 GL_ALLOC_CALL(this->glInterface(),
1229 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1230 desc.fSizeInBytes,
1231 NULL, // data ptr
1232 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1233 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1234 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001235 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001236 return NULL;
1237 }
1238 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1239 return indexBuffer;
1240 }
1241 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001243}
1244
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001245GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001246 SkASSERT(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001247 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001248}
1249
bsalomon@google.coma3201942012-06-21 19:58:20 +00001250void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001251 const GrDrawState& drawState = this->getDrawState();
1252 const GrGLRenderTarget* rt =
1253 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1254
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001255 SkASSERT(NULL != rt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001256 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001257
bsalomon@google.coma3201942012-06-21 19:58:20 +00001258 if (fScissorState.fEnabled) {
1259 GrGLIRect scissor;
1260 scissor.setRelativeTo(vp,
1261 fScissorState.fRect.fLeft,
1262 fScissorState.fRect.fTop,
1263 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001264 fScissorState.fRect.height(),
1265 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001266 // 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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001287void GrGpuGL::onClear(const SkIRect* 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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001291 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001292
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001293 SkIRect 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;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001297 SkIRect 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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001331 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001332
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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001343void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001344 const GrDrawState& drawState = this->getDrawState();
1345 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001346 SkASSERT(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.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001350 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001351 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001352#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001353 SkASSERT(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 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001369 this->flushRenderTarget(&SkIRect::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() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001383 this->flushRenderTarget(&SkIRect::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 {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001391 // If this rendertarget is aready TopLeft, we don't need to flip.
1392 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1393 return false;
1394 }
1395
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001396 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001397 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001398 return false;
1399 }
1400
1401 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001402 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001403 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001404 return true;
1405 }
1406 // If we have to do memcpys to handle rowBytes then y-flip is free
1407 // Note the rowBytes might be tight to the passed in data, but if data
1408 // gets clipped in x to the target the rowBytes will no longer be tight.
1409 if (left >= 0 && (left + width) < renderTarget->width()) {
1410 return 0 == rowBytes ||
1411 GrBytesPerPixel(config) * width == rowBytes;
1412 } else {
1413 return false;
1414 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001415}
1416
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001417bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001418 int left, int top,
1419 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001420 GrPixelConfig config,
1421 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001422 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001423 GrGLenum format;
1424 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001425 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001426 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001428 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001429 size_t bpp = GrBytesPerPixel(config);
1430 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1431 &left, &top, &width, &height,
1432 const_cast<const void**>(&buffer),
1433 &rowBytes)) {
1434 return false;
1435 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001436
bsalomon@google.comc6980972011-11-02 19:57:21 +00001437 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001438 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001439 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001440 switch (tgt->getResolveType()) {
1441 case GrGLRenderTarget::kCantResolve_ResolveType:
1442 return false;
1443 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001444 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001445 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001446 break;
1447 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001448 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001449 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001450 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1451 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001452 break;
1453 default:
1454 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 }
1456
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001457 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001458
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001459 // the read rect is viewport-relative
1460 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001461 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001462
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001463 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 if (0 == rowBytes) {
1465 rowBytes = tightRowBytes;
1466 }
1467 size_t readDstRowBytes = tightRowBytes;
1468 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001469
bsalomon@google.comc6980972011-11-02 19:57:21 +00001470 // determine if GL can read using the passed rowBytes or if we need
1471 // a scratch buffer.
1472 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1473 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001474 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001475 SkASSERT(!(rowBytes % sizeof(GrColor)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001476 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1477 readDstRowBytes = rowBytes;
1478 } else {
1479 scratch.reset(tightRowBytes * height);
1480 readDst = scratch.get();
1481 }
1482 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001483 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001484 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1485 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001486 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1487 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001488 format, type, readDst));
1489 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001490 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001491 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1492 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001493 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001494 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001495 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001496 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001497
1498 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001499 // API presents top-to-bottom. We must preserve the padding contents. Note
1500 // that the above readPixels did not overwrite the padding.
1501 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001502 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001503 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001504 scratch.reset(tightRowBytes);
1505 void* tmpRow = scratch.get();
1506 // flip y in-place by rows
1507 const int halfY = height >> 1;
1508 char* top = reinterpret_cast<char*>(buffer);
1509 char* bottom = top + (height - 1) * rowBytes;
1510 for (int y = 0; y < halfY; y++) {
1511 memcpy(tmpRow, top, tightRowBytes);
1512 memcpy(top, bottom, tightRowBytes);
1513 memcpy(bottom, tmpRow, tightRowBytes);
1514 top += rowBytes;
1515 bottom -= rowBytes;
1516 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 }
1518 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001519 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001520 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001521 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001522 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001523 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001524 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001525 dst += (height-1) * rowBytes;
1526 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001527 for (int y = 0; y < height; y++) {
1528 memcpy(dst, src, tightRowBytes);
1529 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001530 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001531 dst += rowBytes;
1532 } else {
1533 dst -= rowBytes;
1534 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 }
1536 }
1537 return true;
1538}
1539
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001540void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001541
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001542 GrGLRenderTarget* rt =
1543 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001544 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001545
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001546 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001547 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001548#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001549 GrGLenum status;
1550 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001551 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001552 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001553 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001554#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001555 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001556 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001557 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001558 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001559 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 }
1561 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001562 if (NULL == bound || !bound->isEmpty()) {
1563 rt->flagAsNeedingResolve(bound);
1564 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001565
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001566 GrTexture *texture = rt->asTexture();
1567 if (texture) {
1568 texture->dirtyMipMaps(true);
1569 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001570}
1571
twiz@google.com0f31ca72011-03-18 17:38:11 +00001572GrGLenum gPrimitiveType2GLMode[] = {
1573 GR_GL_TRIANGLES,
1574 GR_GL_TRIANGLE_STRIP,
1575 GR_GL_TRIANGLE_FAN,
1576 GR_GL_POINTS,
1577 GR_GL_LINES,
1578 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001579};
1580
bsalomon@google.comd302f142011-03-03 13:54:13 +00001581#define SWAP_PER_DRAW 0
1582
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001583#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001584 #if GR_MAC_BUILD
1585 #include <AGL/agl.h>
1586 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001587 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001588 void SwapBuf() {
1589 DWORD procID = GetCurrentProcessId();
1590 HWND hwnd = GetTopWindow(GetDesktopWindow());
1591 while(hwnd) {
1592 DWORD wndProcID = 0;
1593 GetWindowThreadProcessId(hwnd, &wndProcID);
1594 if(wndProcID == procID) {
1595 SwapBuffers(GetDC(hwnd));
1596 }
1597 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1598 }
1599 }
1600 #endif
1601#endif
1602
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001603void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001604 size_t indexOffsetInBytes;
1605 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001606
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001607 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001608
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001609 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001610 GrGLvoid* indices =
1611 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001612 // info.startVertex() was accounted for by setupGeometry.
1613 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1614 info.indexCount(),
1615 GR_GL_UNSIGNED_SHORT,
1616 indices));
1617 } else {
1618 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1619 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1620 // accounted for startVertex.
1621 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1622 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001623#if SWAP_PER_DRAW
1624 glFlush();
1625 #if GR_MAC_BUILD
1626 aglSwapBuffers(aglGetCurrentContext());
1627 int set_a_break_pt_here = 9;
1628 aglSwapBuffers(aglGetCurrentContext());
1629 #elif GR_WIN32_BUILD
1630 SwapBuf();
1631 int set_a_break_pt_here = 9;
1632 SwapBuf();
1633 #endif
1634#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001635}
1636
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001637namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001638
1639static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001640const GrStencilSettings& winding_nv_path_stencil_settings() {
1641 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1642 kIncClamp_StencilOp,
1643 kIncClamp_StencilOp,
1644 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001645 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001646 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1647}
1648const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1649 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1650 kInvert_StencilOp,
1651 kInvert_StencilOp,
1652 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001653 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001654 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1655}
1656}
1657
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001658void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001659 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001660 GrStencilSettings* settings) {
1661 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001662 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001663 *settings = even_odd_nv_path_stencil_settings();
1664 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001665 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001666 *settings = winding_nv_path_stencil_settings();
1667 return;
1668 default:
1669 GrCrash("Unexpected path fill.");
1670 }
1671}
1672
sugoi@google.com12b4e272012-12-06 20:13:11 +00001673void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001674 SkASSERT(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001675
1676 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1677 GrDrawState* drawState = this->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001678 SkASSERT(NULL != drawState->getRenderTarget());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001679 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1680 return;
1681 }
1682
1683 // Decide how to manipulate the stencil buffer based on the fill rule.
1684 // Also, assert that the stencil settings we set in setStencilPathSettings
1685 // are present.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001686 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001687 GrGLenum fillMode;
1688 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001689 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001690 fillMode = GR_GL_COUNT_UP;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001691 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001692 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001693 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001694 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1695 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001696 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001697 fillMode = GR_GL_INVERT;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001698 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001699 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001700 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001701 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1702 break;
1703 default:
1704 // Only the above two fill rules are allowed.
1705 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001706 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001707 }
1708 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1709 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001710}
1711
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001712void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001713 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001714 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001715 // Some extensions automatically resolves the texture when it is read.
1716 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001717 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001718 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1719 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1720 // make sure we go through flushRenderTarget() since we've modified
1721 // the bound DRAW FBO ID.
1722 fHWBoundRenderTarget = NULL;
1723 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001724 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001725 GrGLIRect r;
1726 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1727 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001728
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001729 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001730 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001731 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001732 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001733 fScissorState.fEnabled = true;
1734 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001735 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001736 GL_CALL(ResolveMultisampleFramebuffer());
1737 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001738 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001739 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001740 asr.reset(&fScissorState);
1741 fScissorState.fEnabled = false;
1742 this->flushScissor();
1743 }
1744 int right = r.fLeft + r.fWidth;
1745 int top = r.fBottom + r.fHeight;
1746 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1747 r.fLeft, r.fBottom, right, top,
1748 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001749 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001750 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001751 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 }
1753}
1754
bsalomon@google.com411dad02012-06-05 20:24:20 +00001755namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756
bsalomon@google.com411dad02012-06-05 20:24:20 +00001757GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1758 static const GrGLenum gTable[] = {
1759 GR_GL_ALWAYS, // kAlways_StencilFunc
1760 GR_GL_NEVER, // kNever_StencilFunc
1761 GR_GL_GREATER, // kGreater_StencilFunc
1762 GR_GL_GEQUAL, // kGEqual_StencilFunc
1763 GR_GL_LESS, // kLess_StencilFunc
1764 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1765 GR_GL_EQUAL, // kEqual_StencilFunc,
1766 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1767 };
1768 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1769 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1770 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1771 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1772 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1773 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1774 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1775 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1776 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001777 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001778
1779 return gTable[basicFunc];
1780}
1781
1782GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1783 static const GrGLenum gTable[] = {
1784 GR_GL_KEEP, // kKeep_StencilOp
1785 GR_GL_REPLACE, // kReplace_StencilOp
1786 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1787 GR_GL_INCR, // kIncClamp_StencilOp
1788 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1789 GR_GL_DECR, // kDecClamp_StencilOp
1790 GR_GL_ZERO, // kZero_StencilOp
1791 GR_GL_INVERT, // kInvert_StencilOp
1792 };
1793 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1794 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1795 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1796 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1797 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1798 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1799 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1800 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1801 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001802 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001803 return gTable[op];
1804}
1805
1806void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001807 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001808 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001809 GrStencilSettings::Face grFace) {
1810 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1811 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1812 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1813
1814 GrGLint ref = settings.funcRef(grFace);
1815 GrGLint mask = settings.funcMask(grFace);
1816 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001817
1818 if (GR_GL_FRONT_AND_BACK == glFace) {
1819 // we call the combined func just in case separate stencil is not
1820 // supported.
1821 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1822 GR_GL_CALL(gl, StencilMask(writeMask));
1823 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1824 } else {
1825 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1826 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1827 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1828 }
1829}
1830}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001831
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001832void GrGpuGL::flushStencil(DrawType type) {
1833 if (kStencilPath_DrawType == type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001834 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001835 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1836 // that draws the path to the SB (glStencilFillPath)
1837 GrGLenum func =
1838 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1839 GL_CALL(PathStencilFunc(func,
1840 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1841 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1842 } else if (fHWStencilSettings != fStencilSettings) {
1843 if (fStencilSettings.isDisabled()) {
1844 if (kNo_TriState != fHWStencilTestEnabled) {
1845 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1846 fHWStencilTestEnabled = kNo_TriState;
1847 }
1848 } else {
1849 if (kYes_TriState != fHWStencilTestEnabled) {
1850 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1851 fHWStencilTestEnabled = kYes_TriState;
1852 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001853 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001854 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001855 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001856 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001857 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001858 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001859 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001860 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001861 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001862 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001863 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001865 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001866 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001867 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001868 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001869 }
1870 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001871 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001872 }
1873}
1874
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001875void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001876// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1877// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1878#if 0
1879 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1880 #define RT_HAS_MSAA rt->isMultisampled()
1881#else
1882 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1883#endif
1884
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001885 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001886 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001887 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1888 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001889 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001890 bool smoothLines = false;
1891
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001892 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001893 smoothLines = this->willUseHWAALines();
1894 if (smoothLines) {
1895 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1896 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1897 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1898 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001899 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001900 kNo_TriState != fHWAAState.fMSAAEnabled) {
1901 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1902 fHWAAState.fMSAAEnabled = kNo_TriState;
1903 }
1904 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001905 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001906 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1907 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1908 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1909 }
1910 }
1911 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001912 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001913 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1914 // convex hulls of each segment appear to get filled.
1915 bool enableMSAA = kStencilPath_DrawType == type ||
1916 this->getDrawState().isHWAntialiasState();
1917 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001918 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1919 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1920 fHWAAState.fMSAAEnabled = kYes_TriState;
1921 }
1922 } else {
1923 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1924 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1925 fHWAAState.fMSAAEnabled = kNo_TriState;
1926 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001927 }
1928 }
1929 }
1930}
1931
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001932void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001933 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001934 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001935 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001936 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001938 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001939 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001940 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1941 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1942 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1943 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1944 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1945 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001946 }
1947 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001948 // any optimization to disable blending should
1949 // have already been applied and tweaked the coeffs
1950 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001951 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1952 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001953 if (blendOff) {
1954 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001956 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001957 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001958 } else {
1959 if (kYes_TriState != fHWBlendState.fEnabled) {
1960 GL_CALL(Enable(GR_GL_BLEND));
1961 fHWBlendState.fEnabled = kYes_TriState;
1962 }
1963 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1964 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001965 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1966 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001967 fHWBlendState.fSrcCoeff = srcCoeff;
1968 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001969 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001970 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001971 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1972 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001973 (!fHWBlendState.fConstColorValid ||
1974 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001975 GrGLfloat c[4];
1976 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001977 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001978 fHWBlendState.fConstColor = blendConst;
1979 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 }
1981 }
1982 }
1983}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001984namespace {
1985
bsalomon@google.com6d003d12012-09-11 15:45:20 +00001986inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00001987 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
1988 GR_GL_TEXTURE_SWIZZLE_RGBA,
1989 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001990}
bsalomon@google.comb8670992012-07-25 21:27:09 +00001991
bsalomon@google.com6c76d242012-09-07 16:52:24 +00001992inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00001993 static const GrGLenum gWrapModes[] = {
1994 GR_GL_CLAMP_TO_EDGE,
1995 GR_GL_REPEAT,
1996 GR_GL_MIRRORED_REPEAT
1997 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00001998 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00001999 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2000 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2001 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2002 return gWrapModes[tm];
2003}
2004
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002005}
2006
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002007void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002008 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002009
bsalomon@google.comb8670992012-07-25 21:27:09 +00002010 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2011 // 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 +00002012 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002013 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002014 if (NULL != texRT) {
2015 this->onResolveRenderTarget(texRT);
2016 }
2017
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002018 if (fHWBoundTextures[unitIdx] != texture) {
2019 this->setTextureUnit(unitIdx);
2020 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2021 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002022 }
2023
bsalomon@google.com4c883782012-06-04 19:05:11 +00002024 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002025 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002026 bool setAll = timestamp < this->getResetTimestamp();
2027 GrGLTexture::TexParams newTexParams;
2028
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002029 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002030 GR_GL_NEAREST,
2031 GR_GL_LINEAR,
2032 GR_GL_LINEAR_MIPMAP_LINEAR
2033 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002034 static GrGLenum glMagFilterModes[] = {
2035 GR_GL_NEAREST,
2036 GR_GL_LINEAR,
2037 GR_GL_LINEAR
2038 };
2039 newTexParams.fMinFilter = glMinFilterModes[params.filterMode()];
2040 newTexParams.fMagFilter = glMagFilterModes[params.filterMode()];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002041
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002042#ifndef SKIA_IGNORE_GPU_MIPMAPS
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002043 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002044 texture->mipMapsAreDirty()) {
2045// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2046 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2047 texture->dirtyMipMaps(false);
2048 }
2049#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002050
bsalomon@google.comb8670992012-07-25 21:27:09 +00002051 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2052 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002053 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002054 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002055 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002056 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002057 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002058 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2059 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002060 newTexParams.fMagFilter));
2061 }
2062 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2063 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002064 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2065 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002066 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 }
2068 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002069 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_WRAP_S,
2072 newTexParams.fWrapS));
2073 }
2074 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002075 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_WRAP_T,
2078 newTexParams.fWrapT));
2079 }
2080 if (this->glCaps().textureSwizzleSupport() &&
2081 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2082 oldTexParams.fSwizzleRGBA,
2083 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002084 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002085 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2086 this->glInterface());
2087 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002088 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002089}
2090
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002091void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002092
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002093 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002094
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002095 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002096 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002097 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002098 fHWDitherEnabled = kYes_TriState;
2099 }
2100 } else {
2101 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002102 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002103 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002104 }
2105 }
2106
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002107 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002108 if (kNo_TriState != fHWWriteToColor) {
2109 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2110 GR_GL_FALSE, GR_GL_FALSE));
2111 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002112 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002113 } else {
2114 if (kYes_TriState != fHWWriteToColor) {
2115 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2116 fHWWriteToColor = kYes_TriState;
2117 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002118 }
2119
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002120 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002121 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002122 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002123 GL_CALL(Enable(GR_GL_CULL_FACE));
2124 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002125 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002126 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002127 GL_CALL(Enable(GR_GL_CULL_FACE));
2128 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002129 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002130 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002131 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002132 break;
2133 default:
2134 GrCrash("Unknown draw face.");
2135 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002136 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002137 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002138}
2139
reed@google.comac10a2d2010-12-22 21:39:39 +00002140void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002141 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002142 if (fHWBoundRenderTarget == renderTarget) {
2143 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002144 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002145}
2146
2147void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002148 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002149 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002150 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002151 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002152 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002156bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2157 bool getSizedInternalFormat,
2158 GrGLenum* internalFormat,
2159 GrGLenum* externalFormat,
2160 GrGLenum* externalType) {
2161 GrGLenum dontCare;
2162 if (NULL == internalFormat) {
2163 internalFormat = &dontCare;
2164 }
2165 if (NULL == externalFormat) {
2166 externalFormat = &dontCare;
2167 }
2168 if (NULL == externalType) {
2169 externalType = &dontCare;
2170 }
2171
reed@google.comac10a2d2010-12-22 21:39:39 +00002172 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002173 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002174 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002175 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002176 if (getSizedInternalFormat) {
2177 *internalFormat = GR_GL_RGBA8;
2178 } else {
2179 *internalFormat = GR_GL_RGBA;
2180 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002181 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002182 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002183 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002184 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002185 return false;
2186 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002187 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002188 if (getSizedInternalFormat) {
2189 *internalFormat = GR_GL_BGRA8;
2190 } else {
2191 *internalFormat = GR_GL_BGRA;
2192 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002193 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002194 if (getSizedInternalFormat) {
2195 *internalFormat = GR_GL_RGBA8;
2196 } else {
2197 *internalFormat = GR_GL_RGBA;
2198 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002199 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002200 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002201 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002202 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002203 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002204 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002205 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002206 if (getSizedInternalFormat) {
2207 if (this->glBinding() == kDesktop_GrGLBinding) {
2208 return false;
2209 } else {
2210 *internalFormat = GR_GL_RGB565;
2211 }
2212 } else {
2213 *internalFormat = GR_GL_RGB;
2214 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002215 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002216 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002217 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002218 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002219 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002220 if (getSizedInternalFormat) {
2221 *internalFormat = GR_GL_RGBA4;
2222 } else {
2223 *internalFormat = GR_GL_RGBA;
2224 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002225 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002227 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002228 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002229 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002230 // glCompressedTexImage doesn't take external params
2231 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002232 // no sized/unsized internal format distinction here
2233 *internalFormat = GR_GL_PALETTE8_RGBA8;
2234 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002235 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002236 } else {
2237 return false;
2238 }
2239 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002240 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002241 if (this->glCaps().textureRedSupport()) {
2242 *internalFormat = GR_GL_RED;
2243 *externalFormat = GR_GL_RED;
2244 if (getSizedInternalFormat) {
2245 *internalFormat = GR_GL_R8;
2246 } else {
2247 *internalFormat = GR_GL_RED;
2248 }
2249 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002250 } else {
2251 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002252 *externalFormat = GR_GL_ALPHA;
2253 if (getSizedInternalFormat) {
2254 *internalFormat = GR_GL_ALPHA8;
2255 } else {
2256 *internalFormat = GR_GL_ALPHA;
2257 }
2258 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002259 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002260 break;
2261 default:
2262 return false;
2263 }
2264 return true;
2265}
2266
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002267void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002268 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002269 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002270 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002271 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002272 }
2273}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002274
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002275void GrGpuGL::setScratchTextureUnit() {
2276 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2277 int lastUnitIdx = fHWBoundTextures.count() - 1;
2278 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2279 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2280 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002281 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002282 // clear out the this field so that if a program does use this unit it will rebind the correct
2283 // texture.
2284 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002285}
2286
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002287namespace {
2288// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002289inline bool can_blit_framebuffer(const GrSurface* dst,
2290 const GrSurface* src,
2291 const GrGpuGL* gpu,
2292 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002293 if (gpu->isConfigRenderable(dst->config()) &&
2294 gpu->isConfigRenderable(src->config()) &&
2295 gpu->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002296 if (NULL != wouldNeedTempFBO) {
2297 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2298 }
2299 return true;
2300 } else {
2301 return false;
2302 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002303}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002304
2305inline bool can_copy_texsubimage(const GrSurface* dst,
2306 const GrSurface* src,
2307 const GrGpuGL* gpu,
2308 bool* wouldNeedTempFBO = NULL) {
2309 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2310 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2311 // many drivers would allow it to work, but ANGLE does not.
bsalomon@google.com791816a2013-08-15 18:54:39 +00002312 if (kES_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002313 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2314 return false;
2315 }
2316 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2317 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2318 // then we don't want to copy to the texture but to the MSAA buffer.
2319 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2320 return false;
2321 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002322 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2323 // If the src is multisampled (and uses an extension where there is a separate MSAA
2324 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2325 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2326 return false;
2327 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002328 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2329 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2330 if (NULL != wouldNeedTempFBO) {
2331 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2332 }
2333 return true;
2334 } else {
2335 return false;
2336 }
2337}
2338
2339// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2340// relative to is output.
2341inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2342 GrSurface* surface,
2343 GrGLenum fboTarget,
2344 GrGLIRect* viewport) {
2345 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2346 GrGLuint tempFBOID;
2347 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002348 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002349 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2350 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2351 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2352 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2353 GR_GL_COLOR_ATTACHMENT0,
2354 GR_GL_TEXTURE_2D,
2355 texID,
2356 0));
2357 viewport->fLeft = 0;
2358 viewport->fBottom = 0;
2359 viewport->fWidth = surface->width();
2360 viewport->fHeight = surface->height();
2361 } else {
2362 tempFBOID = 0;
2363 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2364 *viewport = rt->getViewport();
2365 }
2366 return tempFBOID;
2367}
2368
2369}
2370
2371void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2372 // Check for format issues with glCopyTexSubImage2D
bsalomon@google.com791816a2013-08-15 18:54:39 +00002373 if (kES_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002374 kBGRA_8888_GrPixelConfig == src->config()) {
2375 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002376 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002377 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002378 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002379 } else if (NULL == src->asRenderTarget()) {
2380 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2381 // class handle it by rendering.
2382 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002383 return;
2384 }
2385
2386 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2387 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2388 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2389 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002390 } else {
2391 desc->fConfig = src->config();
2392 desc->fOrigin = src->origin();
2393 desc->fFlags = kNone_GrTextureFlags;
2394 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002395}
2396
2397bool GrGpuGL::onCopySurface(GrSurface* dst,
2398 GrSurface* src,
2399 const SkIRect& srcRect,
2400 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002401 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002402 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002403 bool wouldNeedTempFBO = false;
2404 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2405 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2406 GrGLuint srcFBO;
2407 GrGLIRect srcVP;
2408 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2409 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002410 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002411 // We modified the bound FBO
2412 fHWBoundRenderTarget = NULL;
2413 GrGLIRect srcGLRect;
2414 srcGLRect.setRelativeTo(srcVP,
2415 srcRect.fLeft,
2416 srcRect.fTop,
2417 srcRect.width(),
2418 srcRect.height(),
2419 src->origin());
2420
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002421 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002422 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2423 GrGLint dstY;
2424 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2425 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2426 } else {
2427 dstY = dstPoint.fY;
2428 }
2429 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2430 dstPoint.fX, dstY,
2431 srcGLRect.fLeft, srcGLRect.fBottom,
2432 srcGLRect.fWidth, srcGLRect.fHeight));
2433 copied = true;
2434 if (srcFBO) {
2435 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2436 }
2437 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2438 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002439 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2440 srcRect.width(), srcRect.height());
2441 bool selfOverlap = false;
2442 if (dst->isSameAs(src)) {
2443 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2444 }
2445
2446 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002447 GrGLuint dstFBO;
2448 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002449 GrGLIRect dstVP;
2450 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002451 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2452 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2453 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002454 fHWBoundRenderTarget = NULL;
2455 GrGLIRect srcGLRect;
2456 GrGLIRect dstGLRect;
2457 srcGLRect.setRelativeTo(srcVP,
2458 srcRect.fLeft,
2459 srcRect.fTop,
2460 srcRect.width(),
2461 srcRect.height(),
2462 src->origin());
2463 dstGLRect.setRelativeTo(dstVP,
2464 dstRect.fLeft,
2465 dstRect.fTop,
2466 dstRect.width(),
2467 dstRect.height(),
2468 dst->origin());
2469
2470 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002471 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002472 // The EXT version applies the scissor during the blit, so disable it.
2473 asr.reset(&fScissorState);
2474 fScissorState.fEnabled = false;
2475 this->flushScissor();
2476 }
2477 GrGLint srcY0;
2478 GrGLint srcY1;
2479 // Does the blit need to y-mirror or not?
2480 if (src->origin() == dst->origin()) {
2481 srcY0 = srcGLRect.fBottom;
2482 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2483 } else {
2484 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2485 srcY1 = srcGLRect.fBottom;
2486 }
2487 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2488 srcY0,
2489 srcGLRect.fLeft + srcGLRect.fWidth,
2490 srcY1,
2491 dstGLRect.fLeft,
2492 dstGLRect.fBottom,
2493 dstGLRect.fLeft + dstGLRect.fWidth,
2494 dstGLRect.fBottom + dstGLRect.fHeight,
2495 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2496 if (dstFBO) {
2497 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2498 }
2499 if (srcFBO) {
2500 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2501 }
2502 copied = true;
2503 }
2504 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002505 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002506 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002507 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002508 }
2509 return copied;
2510}
2511
2512bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2513 GrSurface* src,
2514 const SkIRect& srcRect,
2515 const SkIPoint& dstPoint) {
2516 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002517 if (can_copy_texsubimage(dst, src, this)) {
2518 return true;
2519 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002520 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002521 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002522 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2523 srcRect.width(), srcRect.height());
2524 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2525 return true;
2526 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002527 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002528 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002529 }
2530 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002531 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002532}
2533
2534
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002535///////////////////////////////////////////////////////////////////////////////
2536
bsalomon@google.com6918d482013-03-07 19:09:11 +00002537GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2538 GrGpuGL* gpu,
2539 const GrGLVertexBuffer* vbuffer,
2540 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002541 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002542 GrGLAttribArrayState* attribState;
2543
bsalomon@google.com6918d482013-03-07 19:09:11 +00002544 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2545 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2546 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2547 SkSafeUnref(fVBOVertexArray);
2548 GrGLuint arrayID;
2549 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2550 int attrCount = gpu->glCaps().maxVertexAttributes();
2551 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002552 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002553 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2554 } else {
2555 if (NULL != ibuffer) {
2556 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2557 } else {
2558 this->setVertexArrayID(gpu, 0);
2559 }
2560 int attrCount = gpu->glCaps().maxVertexAttributes();
2561 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2562 fDefaultVertexArrayAttribState.resize(attrCount);
2563 }
2564 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002565 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002566 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002567}