blob: 149025aa0d9606fc718176ca48f722394712b542 [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
robertphillips@google.com6177e692013-02-28 20:16:25 +0000122 GrAssert(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
jvanverth@google.com054ae992013-04-01 20:06:51 +0000153 GrAssert(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
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000163 GrAssert(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
204 if (kDesktop_GrGLBinding == this->glBinding()) {
205 // Post 3.0 we will get R8
206 // Prior to 3.0 we will get ALPHA8 (with GL_ARB_framebuffer_object)
207 if (this->glVersion() >= GR_GL_VER(3,0) ||
208 this->hasExtension("GL_ARB_framebuffer_object")) {
209 fConfigRenderSupport[kAlpha_8_GrPixelConfig] = true;
210 }
211 } else {
212 // On ES we can only hope for R8
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213 fConfigRenderSupport[kAlpha_8_GrPixelConfig] =
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000214 this->glCaps().textureRedSupport();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000215 }
216
217 if (kDesktop_GrGLBinding != this->glBinding()) {
218 // only available in ES
219 fConfigRenderSupport[kRGB_565_GrPixelConfig] = true;
220 }
221
reed@google.com6ba45722013-06-21 18:30:53 +0000222 // we no longer support 444 as a render target
223 fConfigRenderSupport[kRGBA_4444_GrPixelConfig] = false;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000224
225 if (this->glCaps().rgba8RenderbufferSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000226 fConfigRenderSupport[kRGBA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000227 }
228
229 if (this->glCaps().bgraFormatSupport()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000230 fConfigRenderSupport[kBGRA_8888_GrPixelConfig] = true;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000231 }
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000232}
233
bsalomon@google.com9c680582013-02-06 18:17:50 +0000234namespace {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000235GrPixelConfig preferred_pixel_ops_config(GrPixelConfig cpuConfig, GrPixelConfig surfaceConfig) {
236 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == cpuConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000237 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000238 } else if (GrBytesPerPixel(cpuConfig) == 4 &&
239 GrPixelConfigSwapRAndB(cpuConfig) == surfaceConfig) {
240 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
241 // Perhaps this should be guarded by some compiletime or runtime check.
242 return surfaceConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000243 } else {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000244 return cpuConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000245 }
246}
bsalomon@google.com9c680582013-02-06 18:17:50 +0000247}
248
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000249GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
250 GrPixelConfig surfaceConfig) const {
251 return preferred_pixel_ops_config(readConfig, surfaceConfig);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000252}
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000253
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000254GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
255 GrPixelConfig surfaceConfig) const {
256 return preferred_pixel_ops_config(writeConfig, surfaceConfig);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000257}
258
259bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
260 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
261 return false;
262 }
263 if (srcConfig != texture->config() && kES2_GrGLBinding == this->glBinding()) {
264 // In general ES2 requires the internal format of the texture and the format of the src
265 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
266 // texture. It depends upon which extension added BGRA. The Apple extension allows it
267 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
268 // internal format).
269 if (this->glCaps().bgraFormatSupport() &&
270 !this->glCaps().bgraIsInternalFormat() &&
271 kBGRA_8888_GrPixelConfig == srcConfig &&
272 kRGBA_8888_GrPixelConfig == texture->config()) {
273 return true;
274 } else {
275 return false;
276 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000277 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000278 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000279 }
280}
281
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000282bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
283 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
284}
285
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000286void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000287 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000288 if (resetBits & kMisc_GrGLBackendState) {
289 GL_CALL(Disable(GR_GL_DEPTH_TEST));
290 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000291
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000292 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
293 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000294
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 if (kDesktop_GrGLBinding == this->glBinding()) {
296 // Desktop-only state that we never change
297 if (!this->glCaps().isCoreProfile()) {
298 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
299 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
300 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
301 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
302 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
303 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
304 }
305 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
306 // core profile. This seems like a bug since the core spec removes any mention of
307 // GL_ARB_imaging.
308 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
309 GL_CALL(Disable(GR_GL_COLOR_TABLE));
310 }
311 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
312 // Since ES doesn't support glPointSize at all we always use the VS to
313 // set the point size
314 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
315
316 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
317 // currently part of our gl interface. There are probably others as
318 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000319 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000320 fHWWriteToColor = kUnknown_TriState;
321 // we only ever use lines in hairline mode
322 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000323 }
edisonn@google.comba669992013-06-28 16:03:21 +0000324
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000325 if (resetBits & kAA_GrGLBackendState) {
326 fHWAAState.invalidate();
327 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000329 // invalid
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000330 if (resetBits & kTextureBinding_GrGLBackendState) {
331 fHWActiveTextureUnitIdx = -1;
332 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
333 fHWBoundTextures[s] = NULL;
334 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000335 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000336
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000337 if (resetBits & kBlend_GrGLBackendState) {
338 fHWBlendState.invalidate();
339 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000340
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000341 if (resetBits & kView_GrGLBackendState) {
342 fHWScissorSettings.invalidate();
343 fHWViewport.invalidate();
344 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000346 if (resetBits & kStencil_GrGLBackendState) {
347 fHWStencilSettings.invalidate();
348 fHWStencilTestEnabled = kUnknown_TriState;
349 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000350
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000351 // Vertex
352 if (resetBits & kVertex_GrGLBackendState) {
353 fHWGeometryState.invalidate();
354 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000356 if (resetBits & kRenderTarget_GrGLBackendState) {
357 fHWBoundRenderTarget = NULL;
358 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000359
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000360 if (resetBits & kPathStencil_GrGLBackendState) {
361 fHWPathStencilMatrixState.invalidate();
362 if (this->caps()->pathStencilingSupport()) {
363 // we don't use the model view matrix.
364 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
365 GL_CALL(LoadIdentity());
366 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000367 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000368
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000369 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000370 if (resetBits & kPixelStore_GrGLBackendState) {
371 if (this->glCaps().unpackRowLengthSupport()) {
372 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
373 }
374 if (this->glCaps().packRowLengthSupport()) {
375 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
376 }
377 if (this->glCaps().unpackFlipYSupport()) {
378 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
379 }
380 if (this->glCaps().packFlipYSupport()) {
381 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
382 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000383 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000384
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000385 if (resetBits & kProgram_GrGLBackendState) {
386 fHWProgramID = 0;
387 fSharedGLProgramState.invalidate();
388 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000389}
390
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000391namespace {
392
393GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
394 // By default, GrRenderTargets are GL's normal orientation so that they
395 // can be drawn to by the outside world without the client having
396 // to render upside down.
397 if (kDefault_GrSurfaceOrigin == origin) {
398 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
399 } else {
400 return origin;
401 }
402}
403
404}
405
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000406GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000407 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000408 return NULL;
409 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000410
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000411 if (0 == desc.fTextureHandle) {
412 return NULL;
413 }
414
bsalomon@google.combcce8922013-03-25 15:38:39 +0000415 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000416 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
417 return NULL;
418 }
419
420 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000421 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000422 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000423 glTexDesc.fWidth = desc.fWidth;
424 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000425 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000426 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000427 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000428 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000429 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000430 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
431 // assuming the old behaviour, which is that backend textures are always
432 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
433 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
434 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
435 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
436 } else {
437 glTexDesc.fOrigin = desc.fOrigin;
438 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000439
440 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000441 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000442 GrGLRenderTarget::Desc glRTDesc;
443 glRTDesc.fRTFBOID = 0;
444 glRTDesc.fTexFBOID = 0;
445 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000446 glRTDesc.fConfig = desc.fConfig;
447 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000448 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000449 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000450 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
451 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000452 glTexDesc.fTextureID,
453 &glRTDesc)) {
454 return NULL;
455 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000456 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000457 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000458 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000459 }
460 if (NULL == texture) {
461 return NULL;
462 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000463
bsalomon@google.come269f212011-11-07 13:29:52 +0000464 return texture;
465}
466
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000467GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000468 GrGLRenderTarget::Desc glDesc;
469 glDesc.fConfig = desc.fConfig;
470 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
471 glDesc.fMSColorRenderbufferID = 0;
472 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
473 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000474 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000475 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000476
477 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000478 GrGLIRect viewport;
479 viewport.fLeft = 0;
480 viewport.fBottom = 0;
481 viewport.fWidth = desc.fWidth;
482 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000483
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000484 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
485 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000486 if (desc.fStencilBits) {
487 GrGLStencilBuffer::Format format;
488 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
489 format.fPacked = false;
490 format.fStencilBits = desc.fStencilBits;
491 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000492 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000493 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
494 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000495 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000496 0,
497 desc.fWidth,
498 desc.fHeight,
499 desc.fSampleCnt,
500 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000501 tgt->setStencilBuffer(sb);
502 sb->unref();
503 }
504 return tgt;
505}
506
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000507////////////////////////////////////////////////////////////////////////////////
508
bsalomon@google.com9c680582013-02-06 18:17:50 +0000509bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000510 int left, int top, int width, int height,
511 GrPixelConfig config, const void* buffer,
512 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000513 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000514 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000515 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000516 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
517
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000518 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000519 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
520 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000521 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000522 desc.fWidth = glTex->width();
523 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000524 desc.fConfig = glTex->config();
525 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000526 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000527 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000528
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000529 if (this->uploadTexData(desc, false,
530 left, top, width, height,
531 config, buffer, rowBytes)) {
532 texture->dirtyMipMaps(true);
533 return true;
534 } else {
535 return false;
536 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000537}
538
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000539namespace {
540bool adjust_pixel_ops_params(int surfaceWidth,
541 int surfaceHeight,
542 size_t bpp,
543 int* left, int* top, int* width, int* height,
544 const void** data,
545 size_t* rowBytes) {
546 if (!*rowBytes) {
547 *rowBytes = *width * bpp;
548 }
549
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000550 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
551 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000552
553 if (!subRect.intersect(bounds)) {
554 return false;
555 }
556 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
557 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
558
559 *left = subRect.fLeft;
560 *top = subRect.fTop;
561 *width = subRect.width();
562 *height = subRect.height();
563 return true;
564}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000565
566GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
567 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
568 return GR_GL_GET_ERROR(interface);
569 } else {
570 return CHECK_ALLOC_ERROR(interface);
571 }
572}
573
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000574}
575
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000576bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
577 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000578 int left, int top, int width, int height,
579 GrPixelConfig dataConfig,
580 const void* data,
581 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000582 GrAssert(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000583
584 size_t bpp = GrBytesPerPixel(dataConfig);
585 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
586 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000587 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000588 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000589 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000590
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000591 // in case we need a temporary, trimmed copy of the src pixels
592 SkAutoSMalloc<128 * 128> tempStorage;
593
bsalomon@google.com313f0192012-07-10 17:21:02 +0000594 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000595 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000596 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000597 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000598
bsalomon@google.com313f0192012-07-10 17:21:02 +0000599 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
600 // 565 is not a sized internal format on desktop GL. So on desktop with
601 // 565 we always use an unsized internal format to let the system pick
602 // the best sized format to convert the 565 data to. Since TexStorage
603 // only allows sized internal formats we will instead use TexImage2D.
604 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000605 }
606
607 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000608 GrGLenum externalFormat;
609 GrGLenum externalType;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000610 // glTexStorage requires sized internal formats on both desktop and ES. ES
611 // glTexImage requires an unsized format.
612 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
613 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000614 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000615 }
616
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000617 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000618 // paletted textures cannot be updated
619 return false;
620 }
621
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000622 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623 * check whether to allocate a temporary buffer for flipping y or
624 * because our srcData has extra bytes past each row. If so, we need
625 * to trim those off here, since GL ES may not let us specify
626 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000627 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000628 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000629 bool swFlipY = false;
630 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000631 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000632 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000633 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 glFlipY = true;
635 } else {
636 swFlipY = true;
637 }
638 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000639 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000640 // can't use this for flipping, only non-neg values allowed. :(
641 if (rowBytes != trimRowBytes) {
642 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
643 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
644 restoreGLRowLength = true;
645 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000646 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000647 if (trimRowBytes != rowBytes || swFlipY) {
648 // copy data into our new storage, skipping the trailing bytes
649 size_t trimSize = height * trimRowBytes;
650 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000651 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000652 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000653 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000654 char* dst = (char*)tempStorage.reset(trimSize);
655 for (int y = 0; y < height; y++) {
656 memcpy(dst, src, trimRowBytes);
657 if (swFlipY) {
658 src -= rowBytes;
659 } else {
660 src += rowBytes;
661 }
662 dst += trimRowBytes;
663 }
664 // now point data to our copied version
665 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000666 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000667 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000668 if (glFlipY) {
669 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
670 }
671 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000672 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000673 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000675 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000676 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000677 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000678 if (useTexStorage) {
679 // We never resize or change formats of textures. We don't use
680 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000681 GL_ALLOC_CALL(this->glInterface(),
682 TexStorage2D(GR_GL_TEXTURE_2D,
683 1, // levels
684 internalFormat,
685 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000686 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000687 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
688 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
689 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000690 GL_ALLOC_CALL(this->glInterface(),
691 CompressedTexImage2D(GR_GL_TEXTURE_2D,
692 0, // level
693 internalFormat,
694 desc.fWidth, desc.fHeight,
695 0, // border
696 imageSize,
697 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000698 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000699 GL_ALLOC_CALL(this->glInterface(),
700 TexImage2D(GR_GL_TEXTURE_2D,
701 0, // level
702 internalFormat,
703 desc.fWidth, desc.fHeight,
704 0, // border
705 externalFormat, externalType,
706 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000707 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000708 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000709 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000710 if (error != GR_GL_NO_ERROR) {
711 succeeded = false;
712 } else {
713 // if we have data and we used TexStorage to create the texture, we
714 // now upload with TexSubImage.
715 if (NULL != data && useTexStorage) {
716 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
717 0, // level
718 left, top,
719 width, height,
720 externalFormat, externalType,
721 data));
722 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000723 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000724 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000725 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000726 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000727 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000728 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
729 0, // level
730 left, top,
731 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000732 externalFormat, externalType, data));
733 }
734
735 if (restoreGLRowLength) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000736 GrAssert(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000737 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000738 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000739 if (glFlipY) {
740 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
741 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000742 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000743}
744
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000745namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000746bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000747 int sampleCount,
748 GrGLenum format,
749 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000750 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000751 GrAssert(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000752 bool created = false;
753 if (GrGLCaps::kNVDesktop_CoverageAAType ==
bsalomon@google.combcce8922013-03-25 15:38:39 +0000754 ctx.info().caps()->coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000755 const GrGLCaps::MSAACoverageMode& mode =
bsalomon@google.combcce8922013-03-25 15:38:39 +0000756 ctx.info().caps()->getMSAACoverageMode(sampleCount);
robertphillips@google.com6177e692013-02-28 20:16:25 +0000757 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000758 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
759 mode.fCoverageSampleCnt,
760 mode.fColorSampleCnt,
761 format,
762 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000763 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000764 }
765 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000766 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000767 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
768 sampleCount,
769 format,
770 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000771 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000772 }
773 return created;
774}
775}
776
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000777bool GrGpuGL::createRenderTargetObjects(int width, int height,
778 GrGLuint texID,
779 GrGLRenderTarget::Desc* desc) {
780 desc->fMSColorRenderbufferID = 0;
781 desc->fRTFBOID = 0;
782 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000783 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000784
785 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000786
bsalomon@google.comab15d612011-08-09 12:57:56 +0000787 GrGLenum msColorFormat = 0; // suppress warning
788
bsalomon@google.com347c3822013-05-01 20:10:01 +0000789 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
790 goto FAILED;
791 }
792
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000793 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000794 if (!desc->fTexFBOID) {
795 goto FAILED;
796 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000797
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000798
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000799 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
800 // the texture bound to the other. The exception is the IMG multisample extension. With this
801 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
802 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000803 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000804 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
805 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000807 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000808 !this->configToGLFormats(desc->fConfig,
809 // GLES requires sized internal formats
810 kES2_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000811 &msColorFormat,
812 NULL,
813 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814 goto FAILED;
815 }
816 } else {
817 desc->fRTFBOID = desc->fTexFBOID;
818 }
819
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000820 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000821 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000822 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com13ac3a32013-01-09 23:29:39 +0000823 GrAssert(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000826 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000827 desc->fSampleCnt,
828 msColorFormat,
829 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000830 goto FAILED;
831 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000832 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000833 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000834 GR_GL_COLOR_ATTACHMENT0,
835 GR_GL_RENDERBUFFER,
836 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000837 if (desc->fCheckAllocation ||
838 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000839 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
840 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
841 goto FAILED;
842 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000843 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000844 }
845 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000846 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000847
bsalomon@google.com347c3822013-05-01 20:10:01 +0000848 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000849 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
850 GR_GL_COLOR_ATTACHMENT0,
851 GR_GL_TEXTURE_2D,
852 texID, 0, desc->fSampleCnt));
853 } else {
854 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
855 GR_GL_COLOR_ATTACHMENT0,
856 GR_GL_TEXTURE_2D,
857 texID, 0));
858 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000859 if (desc->fCheckAllocation ||
860 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000861 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
862 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
863 goto FAILED;
864 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000865 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 }
867
868 return true;
869
870FAILED:
871 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000872 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873 }
874 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000875 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 }
877 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000878 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879 }
880 return false;
881}
882
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000883// good to set a break-point here to know when createTexture fails
884static GrTexture* return_null_texture() {
885// GrAssert(!"null texture");
886 return NULL;
887}
888
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000889#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000890static size_t as_size_t(int x) {
891 return x;
892}
893#endif
894
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000895GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000896 const void* srcData,
897 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000898
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000899 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000900 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000901
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000902 // Attempt to catch un- or wrongly initialized sample counts;
903 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000904 // We fail if the MSAA was requested and is not available.
905 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
906 //GrPrintf("MSAA RT requested but not supported on this platform.");
907 return return_null_texture();
908 }
909 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000910 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000911
robertphillips@google.com32716282012-06-04 12:48:45 +0000912 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000913 glTexDesc.fWidth = desc.fWidth;
914 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000915 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000916 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000917
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000918 glRTDesc.fMSColorRenderbufferID = 0;
919 glRTDesc.fRTFBOID = 0;
920 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000921 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000922 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000923 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000924
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000925 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000926
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000927 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
928 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000929
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000930 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000931 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000932 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000933 //GrPrintf("MSAA RT requested but not supported on this platform.");
934 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000935 }
936
reed@google.comac10a2d2010-12-22 21:39:39 +0000937 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000938 int maxRTSize = this->caps()->maxRenderTargetSize();
939 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000940 return return_null_texture();
941 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000942 } else {
943 int maxSize = this->caps()->maxTextureSize();
944 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
945 return return_null_texture();
946 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000947 }
948
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000949 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000950 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000951 // provides a hint about how this texture will be used
952 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
953 GR_GL_TEXTURE_USAGE,
954 GR_GL_FRAMEBUFFER_ATTACHMENT));
955 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000956 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000957 return return_null_texture();
958 }
959
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000960 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000961 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000962
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000963 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
964 // drivers have a bug where an FBO won't be complete if it includes a
965 // texture that is not mipmap complete (considering the filter in use).
966 GrGLTexture::TexParams initialTexParams;
967 // we only set a subset here so invalidate first
968 initialTexParams.invalidate();
969 initialTexParams.fFilter = GR_GL_NEAREST;
970 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
971 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000972 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
973 GR_GL_TEXTURE_MAG_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000974 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000975 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
976 GR_GL_TEXTURE_MIN_FILTER,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000977 initialTexParams.fFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000978 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
979 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000980 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
982 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000983 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000984 if (!this->uploadTexData(glTexDesc, true, 0, 0,
985 glTexDesc.fWidth, glTexDesc.fHeight,
986 desc.fConfig, srcData, rowBytes)) {
987 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
988 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000989 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000990
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000991 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000992 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000993 // unbind the texture from the texture unit before binding it to the frame buffer
994 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
995
bsalomon@google.com99621082011-11-15 16:47:16 +0000996 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
997 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000998 glTexDesc.fTextureID,
999 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001000 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 return return_null_texture();
1002 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001003 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001004 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001005 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001007 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001008#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001009 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1010 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001011#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001012 return tex;
1013}
1014
1015namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001016
1017const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1018
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001019void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001021
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 // we shouldn't ever know one size and not the other
1023 GrAssert((kUnknownBitCount == format->fStencilBits) ==
1024 (kUnknownBitCount == format->fTotalBits));
1025 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001027 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1028 (GrGLint*)&format->fStencilBits);
1029 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001030 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1032 (GrGLint*)&format->fTotalBits);
1033 format->fTotalBits += format->fStencilBits;
1034 } else {
1035 format->fTotalBits = format->fStencilBits;
1036 }
1037 }
1038}
1039}
1040
1041bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1042 int width, int height) {
1043
1044 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001045 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001046 GrAssert(rt->asTexture());
bsalomon@google.com99621082011-11-15 16:47:16 +00001047 GrAssert(width >= rt->width());
1048 GrAssert(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001049
1050 int samples = rt->numSamples();
1051 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001052 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001053 if (!sbID) {
1054 return false;
1055 }
1056
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001057 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001059 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 // we start with the last stencil format that succeeded in hopes
1061 // that we won't go through this loop more than once after the
1062 // first (painful) stencil creation.
1063 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001064 const GrGLCaps::StencilFormat& sFmt =
1065 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001066 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001067 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001068 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001069 bool created;
1070 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001071 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001072 samples,
1073 sFmt.fInternalFormat,
1074 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001075 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001076 GL_ALLOC_CALL(this->glInterface(),
1077 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001078 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001079 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001080 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001081 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001082 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001083 if (created) {
1084 // After sized formats we attempt an unsized format and take
1085 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001086 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001087 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001088 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001089 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001090 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001091 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001092 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1093 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001094 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001095 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001096 return true;
1097 }
1098 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001099 }
1100 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001101 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001102 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103}
1104
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001105bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001106 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1107
1108 GrGLuint fbo = glrt->renderFBOID();
1109
1110 if (NULL == sb) {
1111 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001112 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001113 GR_GL_STENCIL_ATTACHMENT,
1114 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001116 GR_GL_DEPTH_ATTACHMENT,
1117 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001119 GrGLenum status;
1120 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 GrAssert(GR_GL_FRAMEBUFFER_COMPLETE == status);
1122#endif
1123 }
1124 return true;
1125 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001126 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127 GrGLuint rb = glsb->renderbufferID();
1128
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001129 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1131 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001132 GR_GL_STENCIL_ATTACHMENT,
1133 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001135 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001136 GR_GL_DEPTH_ATTACHMENT,
1137 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001140 GR_GL_DEPTH_ATTACHMENT,
1141 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001144 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001145 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001146 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1147 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001149 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001151 if (glsb->format().fPacked) {
1152 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1153 GR_GL_DEPTH_ATTACHMENT,
1154 GR_GL_RENDERBUFFER, 0));
1155 }
1156 return false;
1157 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001158 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001159 rt->config(),
1160 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001163 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001164 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001165}
1166
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001167////////////////////////////////////////////////////////////////////////////////
1168
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001169GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001170 GrGLVertexBuffer::Desc desc;
1171 desc.fDynamic = dynamic;
1172 desc.fSizeInBytes = size;
1173 desc.fIsWrapped = false;
1174
bsalomon@google.com96966a52013-02-21 16:34:21 +00001175 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001176 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001177 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001179 } else {
1180 GL_CALL(GenBuffers(1, &desc.fID));
1181 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001182 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001183 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1184 // make sure driver can allocate memory for this buffer
1185 GL_ALLOC_CALL(this->glInterface(),
1186 BufferData(GR_GL_ARRAY_BUFFER,
1187 desc.fSizeInBytes,
1188 NULL, // data ptr
1189 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1190 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1191 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001192 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001193 return NULL;
1194 }
1195 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1196 return vertexBuffer;
1197 }
1198 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001200}
1201
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001202GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001203 GrGLIndexBuffer::Desc desc;
1204 desc.fDynamic = dynamic;
1205 desc.fSizeInBytes = size;
1206 desc.fIsWrapped = false;
1207
bsalomon@google.com96966a52013-02-21 16:34:21 +00001208 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001209 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001210 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001212 } else {
1213 GL_CALL(GenBuffers(1, &desc.fID));
1214 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001215 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001216 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1217 // make sure driver can allocate memory for this buffer
1218 GL_ALLOC_CALL(this->glInterface(),
1219 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1220 desc.fSizeInBytes,
1221 NULL, // data ptr
1222 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1223 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1224 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001225 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001226 return NULL;
1227 }
1228 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1229 return indexBuffer;
1230 }
1231 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001233}
1234
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001235GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001236 GrAssert(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001237 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001238}
1239
bsalomon@google.coma3201942012-06-21 19:58:20 +00001240void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001241 const GrDrawState& drawState = this->getDrawState();
1242 const GrGLRenderTarget* rt =
1243 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1244
1245 GrAssert(NULL != rt);
1246 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001247
bsalomon@google.coma3201942012-06-21 19:58:20 +00001248 if (fScissorState.fEnabled) {
1249 GrGLIRect scissor;
1250 scissor.setRelativeTo(vp,
1251 fScissorState.fRect.fLeft,
1252 fScissorState.fRect.fTop,
1253 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001254 fScissorState.fRect.height(),
1255 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001256 // if the scissor fully contains the viewport then we fall through and
1257 // disable the scissor test.
1258 if (!scissor.contains(vp)) {
1259 if (fHWScissorSettings.fRect != scissor) {
1260 scissor.pushToGLScissor(this->glInterface());
1261 fHWScissorSettings.fRect = scissor;
1262 }
1263 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1264 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1265 fHWScissorSettings.fEnabled = kYes_TriState;
1266 }
1267 return;
1268 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001269 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001270 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001271 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001272 fHWScissorSettings.fEnabled = kNo_TriState;
1273 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 }
1275}
1276
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001277void GrGpuGL::onClear(const SkIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001278 const GrDrawState& drawState = this->getDrawState();
1279 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001280 // parent class should never let us get here with no RT
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001281 GrAssert(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001282
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001283 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001284 if (NULL != rect) {
1285 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001286 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001287 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001288 if (clippedRect.intersect(rtRect)) {
1289 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001290 } else {
1291 return;
1292 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001294 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001295 GrAutoTRestore<ScissorState> asr(&fScissorState);
1296 fScissorState.fEnabled = (NULL != rect);
1297 if (fScissorState.fEnabled) {
1298 fScissorState.fRect = *rect;
1299 }
1300 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001301
1302 GrGLfloat r, g, b, a;
1303 static const GrGLfloat scale255 = 1.f / 255.f;
1304 a = GrColorUnpackA(color) * scale255;
1305 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001306 r = GrColorUnpackR(color) * scaleRGB;
1307 g = GrColorUnpackG(color) * scaleRGB;
1308 b = GrColorUnpackB(color) * scaleRGB;
1309
1310 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001311 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001312 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001313 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001314}
1315
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001316void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001317 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001318 return;
1319 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001320
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001321 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001322
bsalomon@google.coma3201942012-06-21 19:58:20 +00001323 GrAutoTRestore<ScissorState> asr(&fScissorState);
1324 fScissorState.fEnabled = false;
1325 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001326
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001327 GL_CALL(StencilMask(0xffffffff));
1328 GL_CALL(ClearStencil(0));
1329 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001330 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001331}
1332
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001333void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001334 const GrDrawState& drawState = this->getDrawState();
1335 const GrRenderTarget* rt = drawState.getRenderTarget();
1336 GrAssert(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001337
1338 // this should only be called internally when we know we have a
1339 // stencil buffer.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001340 GrAssert(NULL != rt->getStencilBuffer());
1341 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001342#if 0
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 GrAssert(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001344 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001345#else
1346 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001347 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001348 // turned into draws. Our contract on GrDrawTarget says that
1349 // changing the clip between stencil passes may or may not
1350 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001351 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001352#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001353 GrGLint value;
1354 if (insideClip) {
1355 value = (1 << (stencilBitCount - 1));
1356 } else {
1357 value = 0;
1358 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001359 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001360
1361 GrAutoTRestore<ScissorState> asr(&fScissorState);
1362 fScissorState.fEnabled = true;
1363 fScissorState.fRect = rect;
1364 this->flushScissor();
1365
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001366 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001367 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001368 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001369 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001370}
1371
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001372void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001373 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001374}
1375
bsalomon@google.comc4364992011-11-07 15:54:49 +00001376bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1377 int left, int top,
1378 int width, int height,
1379 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001380 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001381 // If this rendertarget is aready TopLeft, we don't need to flip.
1382 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1383 return false;
1384 }
1385
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001386 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001387 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001388 return false;
1389 }
1390
1391 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001392 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001393 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001394 return true;
1395 }
1396 // If we have to do memcpys to handle rowBytes then y-flip is free
1397 // Note the rowBytes might be tight to the passed in data, but if data
1398 // gets clipped in x to the target the rowBytes will no longer be tight.
1399 if (left >= 0 && (left + width) < renderTarget->width()) {
1400 return 0 == rowBytes ||
1401 GrBytesPerPixel(config) * width == rowBytes;
1402 } else {
1403 return false;
1404 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001405}
1406
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001407bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001408 int left, int top,
1409 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001410 GrPixelConfig config,
1411 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001412 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001413 GrGLenum format;
1414 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001415 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001416 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001418 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001419 size_t bpp = GrBytesPerPixel(config);
1420 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1421 &left, &top, &width, &height,
1422 const_cast<const void**>(&buffer),
1423 &rowBytes)) {
1424 return false;
1425 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001426
bsalomon@google.comc6980972011-11-02 19:57:21 +00001427 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001428 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001429 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001430 switch (tgt->getResolveType()) {
1431 case GrGLRenderTarget::kCantResolve_ResolveType:
1432 return false;
1433 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001435 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001436 break;
1437 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001438 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001439 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001440 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1441 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001442 break;
1443 default:
1444 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 }
1446
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001447 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001448
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001449 // the read rect is viewport-relative
1450 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001451 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001452
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001453 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001454 if (0 == rowBytes) {
1455 rowBytes = tightRowBytes;
1456 }
1457 size_t readDstRowBytes = tightRowBytes;
1458 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001459
bsalomon@google.comc6980972011-11-02 19:57:21 +00001460 // determine if GL can read using the passed rowBytes or if we need
1461 // a scratch buffer.
1462 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1463 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001464 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.comc6980972011-11-02 19:57:21 +00001465 GrAssert(!(rowBytes % sizeof(GrColor)));
1466 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1467 readDstRowBytes = rowBytes;
1468 } else {
1469 scratch.reset(tightRowBytes * height);
1470 readDst = scratch.get();
1471 }
1472 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001473 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001474 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1475 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001476 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1477 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001478 format, type, readDst));
1479 if (readDstRowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001480 GrAssert(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001481 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
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, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001485 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001486 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001487
1488 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001489 // API presents top-to-bottom. We must preserve the padding contents. Note
1490 // that the above readPixels did not overwrite the padding.
1491 if (readDst == buffer) {
1492 GrAssert(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001493 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001494 scratch.reset(tightRowBytes);
1495 void* tmpRow = scratch.get();
1496 // flip y in-place by rows
1497 const int halfY = height >> 1;
1498 char* top = reinterpret_cast<char*>(buffer);
1499 char* bottom = top + (height - 1) * rowBytes;
1500 for (int y = 0; y < halfY; y++) {
1501 memcpy(tmpRow, top, tightRowBytes);
1502 memcpy(top, bottom, tightRowBytes);
1503 memcpy(bottom, tmpRow, tightRowBytes);
1504 top += rowBytes;
1505 bottom -= rowBytes;
1506 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001507 }
1508 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001509 GrAssert(readDst != buffer); GrAssert(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001510 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001511 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001512 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001513 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001514 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001515 dst += (height-1) * rowBytes;
1516 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001517 for (int y = 0; y < height; y++) {
1518 memcpy(dst, src, tightRowBytes);
1519 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001520 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001521 dst += rowBytes;
1522 } else {
1523 dst -= rowBytes;
1524 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001525 }
1526 }
1527 return true;
1528}
1529
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001530void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001531
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001532 GrGLRenderTarget* rt =
1533 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
1534 GrAssert(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001535
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001536 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001537 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001538#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001539 GrGLenum status;
1540 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001541 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001542 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001543 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001544#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001545 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001546 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001547 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001548 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001549 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001550 }
1551 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001552 if (NULL == bound || !bound->isEmpty()) {
1553 rt->flagAsNeedingResolve(bound);
1554 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001555
1556 GrTexture *texture = rt->asTexture();
1557 if (texture) {
1558 texture->dirtyMipMaps(true);
1559 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001560}
1561
twiz@google.com0f31ca72011-03-18 17:38:11 +00001562GrGLenum gPrimitiveType2GLMode[] = {
1563 GR_GL_TRIANGLES,
1564 GR_GL_TRIANGLE_STRIP,
1565 GR_GL_TRIANGLE_FAN,
1566 GR_GL_POINTS,
1567 GR_GL_LINES,
1568 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001569};
1570
bsalomon@google.comd302f142011-03-03 13:54:13 +00001571#define SWAP_PER_DRAW 0
1572
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001573#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001574 #if GR_MAC_BUILD
1575 #include <AGL/agl.h>
1576 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001577 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001578 void SwapBuf() {
1579 DWORD procID = GetCurrentProcessId();
1580 HWND hwnd = GetTopWindow(GetDesktopWindow());
1581 while(hwnd) {
1582 DWORD wndProcID = 0;
1583 GetWindowThreadProcessId(hwnd, &wndProcID);
1584 if(wndProcID == procID) {
1585 SwapBuffers(GetDC(hwnd));
1586 }
1587 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1588 }
1589 }
1590 #endif
1591#endif
1592
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001593void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001594 size_t indexOffsetInBytes;
1595 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001596
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001597 GrAssert((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001598
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001599 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001600 GrGLvoid* indices =
1601 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001602 // info.startVertex() was accounted for by setupGeometry.
1603 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1604 info.indexCount(),
1605 GR_GL_UNSIGNED_SHORT,
1606 indices));
1607 } else {
1608 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1609 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1610 // accounted for startVertex.
1611 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1612 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001613#if SWAP_PER_DRAW
1614 glFlush();
1615 #if GR_MAC_BUILD
1616 aglSwapBuffers(aglGetCurrentContext());
1617 int set_a_break_pt_here = 9;
1618 aglSwapBuffers(aglGetCurrentContext());
1619 #elif GR_WIN32_BUILD
1620 SwapBuf();
1621 int set_a_break_pt_here = 9;
1622 SwapBuf();
1623 #endif
1624#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001625}
1626
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001627namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001628
1629static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001630const GrStencilSettings& winding_nv_path_stencil_settings() {
1631 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1632 kIncClamp_StencilOp,
1633 kIncClamp_StencilOp,
1634 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001635 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001636 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1637}
1638const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1639 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1640 kInvert_StencilOp,
1641 kInvert_StencilOp,
1642 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001643 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001644 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1645}
1646}
1647
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001648void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001649 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001650 GrStencilSettings* settings) {
1651 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001652 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001653 *settings = even_odd_nv_path_stencil_settings();
1654 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001655 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001656 *settings = winding_nv_path_stencil_settings();
1657 return;
1658 default:
1659 GrCrash("Unexpected path fill.");
1660 }
1661}
1662
sugoi@google.com12b4e272012-12-06 20:13:11 +00001663void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001664 GrAssert(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001665
1666 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1667 GrDrawState* drawState = this->drawState();
1668 GrAssert(NULL != drawState->getRenderTarget());
1669 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1670 return;
1671 }
1672
1673 // Decide how to manipulate the stencil buffer based on the fill rule.
1674 // Also, assert that the stencil settings we set in setStencilPathSettings
1675 // are present.
1676 GrAssert(!fStencilSettings.isTwoSided());
1677 GrGLenum fillMode;
1678 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001679 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001680 fillMode = GR_GL_COUNT_UP;
1681 GrAssert(kIncClamp_StencilOp ==
1682 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1683 GrAssert(kIncClamp_StencilOp ==
1684 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1685 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001686 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001687 fillMode = GR_GL_INVERT;
1688 GrAssert(kInvert_StencilOp ==
1689 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
1690 GrAssert(kInvert_StencilOp ==
1691 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1692 break;
1693 default:
1694 // Only the above two fill rules are allowed.
1695 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001696 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001697 }
1698 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1699 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001700}
1701
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001702void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001703 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001704 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001705 // Some extensions automatically resolves the texture when it is read.
1706 if (this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001707 GrAssert(rt->textureFBOID() != rt->renderFBOID());
1708 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1709 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1710 // make sure we go through flushRenderTarget() since we've modified
1711 // the bound DRAW FBO ID.
1712 fHWBoundRenderTarget = NULL;
1713 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001714 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001715 GrGLIRect r;
1716 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1717 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001718
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001719 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001720 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001721 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001722 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001723 fScissorState.fEnabled = true;
1724 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001725 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001726 GL_CALL(ResolveMultisampleFramebuffer());
1727 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001728 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001729 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001730 asr.reset(&fScissorState);
1731 fScissorState.fEnabled = false;
1732 this->flushScissor();
1733 }
1734 int right = r.fLeft + r.fWidth;
1735 int top = r.fBottom + r.fHeight;
1736 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1737 r.fLeft, r.fBottom, right, top,
1738 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001739 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001741 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
1743}
1744
bsalomon@google.com411dad02012-06-05 20:24:20 +00001745namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001746
bsalomon@google.com411dad02012-06-05 20:24:20 +00001747GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1748 static const GrGLenum gTable[] = {
1749 GR_GL_ALWAYS, // kAlways_StencilFunc
1750 GR_GL_NEVER, // kNever_StencilFunc
1751 GR_GL_GREATER, // kGreater_StencilFunc
1752 GR_GL_GEQUAL, // kGEqual_StencilFunc
1753 GR_GL_LESS, // kLess_StencilFunc
1754 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1755 GR_GL_EQUAL, // kEqual_StencilFunc,
1756 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1757 };
1758 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1759 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1760 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1761 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1762 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1763 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1764 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1765 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1766 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
1767 GrAssert((unsigned) basicFunc < kBasicStencilFuncCount);
1768
1769 return gTable[basicFunc];
1770}
1771
1772GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1773 static const GrGLenum gTable[] = {
1774 GR_GL_KEEP, // kKeep_StencilOp
1775 GR_GL_REPLACE, // kReplace_StencilOp
1776 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1777 GR_GL_INCR, // kIncClamp_StencilOp
1778 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1779 GR_GL_DECR, // kDecClamp_StencilOp
1780 GR_GL_ZERO, // kZero_StencilOp
1781 GR_GL_INVERT, // kInvert_StencilOp
1782 };
1783 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1784 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1785 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1786 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1787 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1788 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1789 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1790 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1791 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
1792 GrAssert((unsigned) op < kStencilOpCount);
1793 return gTable[op];
1794}
1795
1796void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001797 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001798 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001799 GrStencilSettings::Face grFace) {
1800 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1801 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1802 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1803
1804 GrGLint ref = settings.funcRef(grFace);
1805 GrGLint mask = settings.funcMask(grFace);
1806 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001807
1808 if (GR_GL_FRONT_AND_BACK == glFace) {
1809 // we call the combined func just in case separate stencil is not
1810 // supported.
1811 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1812 GR_GL_CALL(gl, StencilMask(writeMask));
1813 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1814 } else {
1815 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1816 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1817 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1818 }
1819}
1820}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001821
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001822void GrGpuGL::flushStencil(DrawType type) {
1823 if (kStencilPath_DrawType == type) {
1824 GrAssert(!fStencilSettings.isTwoSided());
1825 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1826 // that draws the path to the SB (glStencilFillPath)
1827 GrGLenum func =
1828 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1829 GL_CALL(PathStencilFunc(func,
1830 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1831 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1832 } else if (fHWStencilSettings != fStencilSettings) {
1833 if (fStencilSettings.isDisabled()) {
1834 if (kNo_TriState != fHWStencilTestEnabled) {
1835 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1836 fHWStencilTestEnabled = kNo_TriState;
1837 }
1838 } else {
1839 if (kYes_TriState != fHWStencilTestEnabled) {
1840 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1841 fHWStencilTestEnabled = kYes_TriState;
1842 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001843 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001844 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001845 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001846 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001847 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001848 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001849 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001850 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001851 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001852 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001853 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001854 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001855 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001856 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001857 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001858 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859 }
1860 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001861 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 }
1863}
1864
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001865void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001866// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1867// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1868#if 0
1869 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1870 #define RT_HAS_MSAA rt->isMultisampled()
1871#else
1872 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1873#endif
1874
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001875 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001876 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001877 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1878 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001879 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001880 bool smoothLines = false;
1881
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001882 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001883 smoothLines = this->willUseHWAALines();
1884 if (smoothLines) {
1885 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1886 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1887 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1888 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001889 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001890 kNo_TriState != fHWAAState.fMSAAEnabled) {
1891 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1892 fHWAAState.fMSAAEnabled = kNo_TriState;
1893 }
1894 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001895 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001896 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1897 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1898 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1899 }
1900 }
1901 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001902 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001903 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1904 // convex hulls of each segment appear to get filled.
1905 bool enableMSAA = kStencilPath_DrawType == type ||
1906 this->getDrawState().isHWAntialiasState();
1907 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001908 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1909 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1910 fHWAAState.fMSAAEnabled = kYes_TriState;
1911 }
1912 } else {
1913 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1914 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1915 fHWAAState.fMSAAEnabled = kNo_TriState;
1916 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001917 }
1918 }
1919 }
1920}
1921
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001922void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001923 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001924 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001925 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001926 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001927 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001928 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001929 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001930 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1931 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1932 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1933 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1934 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1935 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001936 }
1937 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001938 // any optimization to disable blending should
1939 // have already been applied and tweaked the coeffs
1940 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001941 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1942 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001943 if (blendOff) {
1944 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001945 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001946 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001947 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001948 } else {
1949 if (kYes_TriState != fHWBlendState.fEnabled) {
1950 GL_CALL(Enable(GR_GL_BLEND));
1951 fHWBlendState.fEnabled = kYes_TriState;
1952 }
1953 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1954 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1956 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001957 fHWBlendState.fSrcCoeff = srcCoeff;
1958 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001959 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001960 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001961 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1962 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001963 (!fHWBlendState.fConstColorValid ||
1964 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001965 GrGLfloat c[4];
1966 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001967 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001968 fHWBlendState.fConstColor = blendConst;
1969 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001970 }
1971 }
1972 }
1973}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001974namespace {
1975
bsalomon@google.com6d003d12012-09-11 15:45:20 +00001976inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00001977 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
1978 GR_GL_TEXTURE_SWIZZLE_RGBA,
1979 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001980}
bsalomon@google.comb8670992012-07-25 21:27:09 +00001981
bsalomon@google.com6c76d242012-09-07 16:52:24 +00001982inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00001983 static const GrGLenum gWrapModes[] = {
1984 GR_GL_CLAMP_TO_EDGE,
1985 GR_GL_REPEAT,
1986 GR_GL_MIRRORED_REPEAT
1987 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00001988 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00001989 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
1990 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
1991 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
1992 return gWrapModes[tm];
1993}
1994
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001995}
1996
bsalomon@google.com34cccde2013-01-04 18:34:30 +00001997void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
1998 GrAssert(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001999
bsalomon@google.comb8670992012-07-25 21:27:09 +00002000 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2001 // 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 +00002002 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002003 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002004 if (NULL != texRT) {
2005 this->onResolveRenderTarget(texRT);
2006 }
2007
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002008 if (fHWBoundTextures[unitIdx] != texture) {
2009 this->setTextureUnit(unitIdx);
2010 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2011 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002012 }
2013
bsalomon@google.com4c883782012-06-04 19:05:11 +00002014 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002015 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002016 bool setAll = timestamp < this->getResetTimestamp();
2017 GrGLTexture::TexParams newTexParams;
2018
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002019 static GrGLenum glFilterModes[] = {
2020 GR_GL_NEAREST,
2021 GR_GL_LINEAR,
2022 GR_GL_LINEAR_MIPMAP_LINEAR
2023 };
2024 newTexParams.fFilter = glFilterModes[params.filterMode()];
2025
2026#ifndef SKIA_IGNORE_GPU_MIPMAPS
2027 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
2028 texture->mipMapsAreDirty()) {
2029// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2030 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2031 texture->dirtyMipMaps(false);
2032 }
2033#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002034
bsalomon@google.comb8670992012-07-25 21:27:09 +00002035 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2036 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002037 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002038 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002039 sizeof(newTexParams.fSwizzleRGBA));
2040 if (setAll || newTexParams.fFilter != oldTexParams.fFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002041 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002042 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2043 GR_GL_TEXTURE_MAG_FILTER,
2044 newTexParams.fFilter));
2045 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2046 GR_GL_TEXTURE_MIN_FILTER,
2047 newTexParams.fFilter));
2048 }
2049 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002050 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002051 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2052 GR_GL_TEXTURE_WRAP_S,
2053 newTexParams.fWrapS));
2054 }
2055 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002056 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002057 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2058 GR_GL_TEXTURE_WRAP_T,
2059 newTexParams.fWrapT));
2060 }
2061 if (this->glCaps().textureSwizzleSupport() &&
2062 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2063 oldTexParams.fSwizzleRGBA,
2064 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002065 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002066 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2067 this->glInterface());
2068 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002069 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002070}
2071
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002072void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002073
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002074 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002075
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002076 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002077 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002078 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002079 fHWDitherEnabled = kYes_TriState;
2080 }
2081 } else {
2082 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002083 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002084 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002085 }
2086 }
2087
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002088 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002089 if (kNo_TriState != fHWWriteToColor) {
2090 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2091 GR_GL_FALSE, GR_GL_FALSE));
2092 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002093 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002094 } else {
2095 if (kYes_TriState != fHWWriteToColor) {
2096 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2097 fHWWriteToColor = kYes_TriState;
2098 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002099 }
2100
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002101 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002102 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002103 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002104 GL_CALL(Enable(GR_GL_CULL_FACE));
2105 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002106 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002107 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002108 GL_CALL(Enable(GR_GL_CULL_FACE));
2109 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002110 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002111 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002112 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002113 break;
2114 default:
2115 GrCrash("Unknown draw face.");
2116 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002117 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002118 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002119}
2120
reed@google.comac10a2d2010-12-22 21:39:39 +00002121void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
2122 GrAssert(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002123 if (fHWBoundRenderTarget == renderTarget) {
2124 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002125 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002126}
2127
2128void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002129 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002130 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002131 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002132 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002133 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002135}
2136
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002137bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2138 bool getSizedInternalFormat,
2139 GrGLenum* internalFormat,
2140 GrGLenum* externalFormat,
2141 GrGLenum* externalType) {
2142 GrGLenum dontCare;
2143 if (NULL == internalFormat) {
2144 internalFormat = &dontCare;
2145 }
2146 if (NULL == externalFormat) {
2147 externalFormat = &dontCare;
2148 }
2149 if (NULL == externalType) {
2150 externalType = &dontCare;
2151 }
2152
reed@google.comac10a2d2010-12-22 21:39:39 +00002153 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002154 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002155 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002156 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002157 if (getSizedInternalFormat) {
2158 *internalFormat = GR_GL_RGBA8;
2159 } else {
2160 *internalFormat = GR_GL_RGBA;
2161 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002162 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002163 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002164 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002165 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002166 return false;
2167 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002168 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002169 if (getSizedInternalFormat) {
2170 *internalFormat = GR_GL_BGRA8;
2171 } else {
2172 *internalFormat = GR_GL_BGRA;
2173 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002174 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002175 if (getSizedInternalFormat) {
2176 *internalFormat = GR_GL_RGBA8;
2177 } else {
2178 *internalFormat = GR_GL_RGBA;
2179 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002180 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002181 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002182 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002183 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002184 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002185 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002186 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002187 if (getSizedInternalFormat) {
2188 if (this->glBinding() == kDesktop_GrGLBinding) {
2189 return false;
2190 } else {
2191 *internalFormat = GR_GL_RGB565;
2192 }
2193 } else {
2194 *internalFormat = GR_GL_RGB;
2195 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002196 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002198 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002199 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002200 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002201 if (getSizedInternalFormat) {
2202 *internalFormat = GR_GL_RGBA4;
2203 } else {
2204 *internalFormat = GR_GL_RGBA;
2205 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002206 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002207 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002208 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002209 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002210 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002211 // glCompressedTexImage doesn't take external params
2212 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002213 // no sized/unsized internal format distinction here
2214 *internalFormat = GR_GL_PALETTE8_RGBA8;
2215 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002216 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002217 } else {
2218 return false;
2219 }
2220 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002221 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002222 if (this->glCaps().textureRedSupport()) {
2223 *internalFormat = GR_GL_RED;
2224 *externalFormat = GR_GL_RED;
2225 if (getSizedInternalFormat) {
2226 *internalFormat = GR_GL_R8;
2227 } else {
2228 *internalFormat = GR_GL_RED;
2229 }
2230 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002231 } else {
2232 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002233 *externalFormat = GR_GL_ALPHA;
2234 if (getSizedInternalFormat) {
2235 *internalFormat = GR_GL_ALPHA8;
2236 } else {
2237 *internalFormat = GR_GL_ALPHA;
2238 }
2239 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002240 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002241 break;
2242 default:
2243 return false;
2244 }
2245 return true;
2246}
2247
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002248void GrGpuGL::setTextureUnit(int unit) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002249 GrAssert(unit >= 0 && unit < fHWBoundTextures.count());
2250 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002251 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002252 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002253 }
2254}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002255
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002256void GrGpuGL::setScratchTextureUnit() {
2257 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2258 int lastUnitIdx = fHWBoundTextures.count() - 1;
2259 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2260 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2261 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002262 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002263 // clear out the this field so that if a program does use this unit it will rebind the correct
2264 // texture.
2265 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002266}
2267
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002268namespace {
2269// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002270inline bool can_blit_framebuffer(const GrSurface* dst,
2271 const GrSurface* src,
2272 const GrGpuGL* gpu,
2273 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002274 if (gpu->isConfigRenderable(dst->config()) &&
2275 gpu->isConfigRenderable(src->config()) &&
2276 gpu->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002277 if (NULL != wouldNeedTempFBO) {
2278 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2279 }
2280 return true;
2281 } else {
2282 return false;
2283 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002284}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002285
2286inline bool can_copy_texsubimage(const GrSurface* dst,
2287 const GrSurface* src,
2288 const GrGpuGL* gpu,
2289 bool* wouldNeedTempFBO = NULL) {
2290 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2291 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2292 // many drivers would allow it to work, but ANGLE does not.
2293 if (kES2_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
2294 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2295 return false;
2296 }
2297 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2298 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2299 // then we don't want to copy to the texture but to the MSAA buffer.
2300 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2301 return false;
2302 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002303 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2304 // If the src is multisampled (and uses an extension where there is a separate MSAA
2305 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2306 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2307 return false;
2308 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002309 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2310 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2311 if (NULL != wouldNeedTempFBO) {
2312 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2313 }
2314 return true;
2315 } else {
2316 return false;
2317 }
2318}
2319
2320// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2321// relative to is output.
2322inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2323 GrSurface* surface,
2324 GrGLenum fboTarget,
2325 GrGLIRect* viewport) {
2326 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2327 GrGLuint tempFBOID;
2328 if (NULL == rt) {
2329 GrAssert(NULL != surface->asTexture());
2330 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2331 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2332 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2333 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2334 GR_GL_COLOR_ATTACHMENT0,
2335 GR_GL_TEXTURE_2D,
2336 texID,
2337 0));
2338 viewport->fLeft = 0;
2339 viewport->fBottom = 0;
2340 viewport->fWidth = surface->width();
2341 viewport->fHeight = surface->height();
2342 } else {
2343 tempFBOID = 0;
2344 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2345 *viewport = rt->getViewport();
2346 }
2347 return tempFBOID;
2348}
2349
2350}
2351
2352void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2353 // Check for format issues with glCopyTexSubImage2D
2354 if (kES2_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
2355 kBGRA_8888_GrPixelConfig == src->config()) {
2356 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002357 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002358 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002359 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002360 } else if (NULL == src->asRenderTarget()) {
2361 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2362 // class handle it by rendering.
2363 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002364 return;
2365 }
2366
2367 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2368 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2369 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2370 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002371 } else {
2372 desc->fConfig = src->config();
2373 desc->fOrigin = src->origin();
2374 desc->fFlags = kNone_GrTextureFlags;
2375 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002376}
2377
2378bool GrGpuGL::onCopySurface(GrSurface* dst,
2379 GrSurface* src,
2380 const SkIRect& srcRect,
2381 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002382 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002383 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002384 bool wouldNeedTempFBO = false;
2385 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2386 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2387 GrGLuint srcFBO;
2388 GrGLIRect srcVP;
2389 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2390 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
2391 GrAssert(NULL != dstTex);
2392 // We modified the bound FBO
2393 fHWBoundRenderTarget = NULL;
2394 GrGLIRect srcGLRect;
2395 srcGLRect.setRelativeTo(srcVP,
2396 srcRect.fLeft,
2397 srcRect.fTop,
2398 srcRect.width(),
2399 srcRect.height(),
2400 src->origin());
2401
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002402 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002403 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2404 GrGLint dstY;
2405 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2406 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2407 } else {
2408 dstY = dstPoint.fY;
2409 }
2410 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2411 dstPoint.fX, dstY,
2412 srcGLRect.fLeft, srcGLRect.fBottom,
2413 srcGLRect.fWidth, srcGLRect.fHeight));
2414 copied = true;
2415 if (srcFBO) {
2416 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2417 }
2418 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2419 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002420 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2421 srcRect.width(), srcRect.height());
2422 bool selfOverlap = false;
2423 if (dst->isSameAs(src)) {
2424 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2425 }
2426
2427 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002428 GrGLuint dstFBO;
2429 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002430 GrGLIRect dstVP;
2431 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002432 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2433 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2434 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002435 fHWBoundRenderTarget = NULL;
2436 GrGLIRect srcGLRect;
2437 GrGLIRect dstGLRect;
2438 srcGLRect.setRelativeTo(srcVP,
2439 srcRect.fLeft,
2440 srcRect.fTop,
2441 srcRect.width(),
2442 srcRect.height(),
2443 src->origin());
2444 dstGLRect.setRelativeTo(dstVP,
2445 dstRect.fLeft,
2446 dstRect.fTop,
2447 dstRect.width(),
2448 dstRect.height(),
2449 dst->origin());
2450
2451 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002452 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002453 // The EXT version applies the scissor during the blit, so disable it.
2454 asr.reset(&fScissorState);
2455 fScissorState.fEnabled = false;
2456 this->flushScissor();
2457 }
2458 GrGLint srcY0;
2459 GrGLint srcY1;
2460 // Does the blit need to y-mirror or not?
2461 if (src->origin() == dst->origin()) {
2462 srcY0 = srcGLRect.fBottom;
2463 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2464 } else {
2465 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2466 srcY1 = srcGLRect.fBottom;
2467 }
2468 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2469 srcY0,
2470 srcGLRect.fLeft + srcGLRect.fWidth,
2471 srcY1,
2472 dstGLRect.fLeft,
2473 dstGLRect.fBottom,
2474 dstGLRect.fLeft + dstGLRect.fWidth,
2475 dstGLRect.fBottom + dstGLRect.fHeight,
2476 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2477 if (dstFBO) {
2478 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2479 }
2480 if (srcFBO) {
2481 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2482 }
2483 copied = true;
2484 }
2485 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002486 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002487 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002488 GrAssert(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002489 }
2490 return copied;
2491}
2492
2493bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2494 GrSurface* src,
2495 const SkIRect& srcRect,
2496 const SkIPoint& dstPoint) {
2497 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002498 if (can_copy_texsubimage(dst, src, this)) {
2499 return true;
2500 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002501 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002502 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002503 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2504 srcRect.width(), srcRect.height());
2505 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2506 return true;
2507 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002508 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002509 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002510 }
2511 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002512 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002513}
2514
2515
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002516///////////////////////////////////////////////////////////////////////////////
2517
bsalomon@google.com6918d482013-03-07 19:09:11 +00002518GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2519 GrGpuGL* gpu,
2520 const GrGLVertexBuffer* vbuffer,
2521 const GrGLIndexBuffer* ibuffer) {
2522 GrAssert(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002523 GrGLAttribArrayState* attribState;
2524
bsalomon@google.com6918d482013-03-07 19:09:11 +00002525 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2526 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2527 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2528 SkSafeUnref(fVBOVertexArray);
2529 GrGLuint arrayID;
2530 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2531 int attrCount = gpu->glCaps().maxVertexAttributes();
2532 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002533 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002534 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2535 } else {
2536 if (NULL != ibuffer) {
2537 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2538 } else {
2539 this->setVertexArrayID(gpu, 0);
2540 }
2541 int attrCount = gpu->glCaps().maxVertexAttributes();
2542 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2543 fDefaultVertexArrayAttribState.resize(attrCount);
2544 }
2545 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002546 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002547 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002548}