blob: c10df6c7e8416bf840282bcdf86ba98c87d46d5e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpuGL.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000011#include "GrGLPath.h"
bsalomon@google.com6d003d12012-09-11 15:45:20 +000012#include "GrGLShaderBuilder.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000014#include "GrTypes.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
caryclark@google.comcf6285b2012-06-06 12:09:01 +000017static const GrGLuint GR_MAX_GLUINT = ~0U;
twiz@google.com0f31ca72011-03-18 17:38:11 +000018static const GrGLint GR_INVAL_GLINT = ~0;
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000021#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022
bsalomon@google.com8531c1c2011-01-13 19:52:45 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
twiz@google.com0f31ca72011-03-18 17:38:11 +000039static const GrGLenum gXfermodeCoeff2Blend[] = {
40 GR_GL_ZERO,
41 GR_GL_ONE,
42 GR_GL_SRC_COLOR,
43 GR_GL_ONE_MINUS_SRC_COLOR,
44 GR_GL_DST_COLOR,
45 GR_GL_ONE_MINUS_DST_COLOR,
46 GR_GL_SRC_ALPHA,
47 GR_GL_ONE_MINUS_SRC_ALPHA,
48 GR_GL_DST_ALPHA,
49 GR_GL_ONE_MINUS_DST_ALPHA,
50 GR_GL_CONSTANT_COLOR,
51 GR_GL_ONE_MINUS_CONSTANT_COLOR,
52 GR_GL_CONSTANT_ALPHA,
53 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054
55 // extended blend coeffs
56 GR_GL_SRC1_COLOR,
57 GR_GL_ONE_MINUS_SRC1_COLOR,
58 GR_GL_SRC1_ALPHA,
59 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000060};
61
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000063 static const bool gCoeffReferencesBlendConst[] = {
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 true,
75 true,
76 true,
77 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000078
79 // extended blend coeffs
80 false,
81 false,
82 false,
83 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000084 };
85 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000086 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
87 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000088
bsalomon@google.com47059542012-06-06 20:51:20 +000089 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
90 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
91 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
92 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
93 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
94 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
96 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
97 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
98 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
100 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
102 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000103
bsalomon@google.com47059542012-06-06 20:51:20 +0000104 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
105 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
107 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000108
109 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000110 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
111 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000112}
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114///////////////////////////////////////////////////////////////////////////////
115
rileya@google.come38160c2012-07-03 18:03:04 +0000116static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000117
robertphillips@google.com6177e692013-02-28 20:16:25 +0000118GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000120 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000121
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000122 SkASSERT(ctx.isInitialized());
tomhudson@google.com747bf292011-06-14 18:16:52 +0000123
bsalomon@google.combcce8922013-03-25 15:38:39 +0000124 fCaps.reset(SkRef(ctx.info().caps()));
125
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000126 fHWBoundTextures.reset(ctx.info().caps()->maxFragmentTextureUnits());
127
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000128 fillInConfigRenderableTable();
129
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000130
robertphillips@google.com6177e692013-02-28 20:16:25 +0000131 GrGLClearErr(fGLContext.interface());
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000134 const GrGLubyte* vendor;
135 const GrGLubyte* renderer;
136 const GrGLubyte* version;
137 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
138 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
139 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
141 this);
142 GrPrintf("------ VENDOR %s\n", vendor);
143 GrPrintf("------ RENDERER %s\n", renderer);
144 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000145 GrPrintf("------ EXTENSIONS\n");
146 ctx.info().extensions().print();
147 GrPrintf("\n");
bsalomon@google.combcce8922013-03-25 15:38:39 +0000148 ctx.info().caps()->print();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000149 }
150
robertphillips@google.com6177e692013-02-28 20:16:25 +0000151 fProgramCache = SkNEW_ARGS(ProgramCache, (this->glContext()));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000152
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000154
bsalomon@google.comfe676522011-06-17 18:12:21 +0000155 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000156 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157}
158
159GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000160 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000161 // detach the current program so there is no confusion on OpenGL's part
162 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000163 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164 GL_CALL(UseProgram(0));
165 }
166
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000167 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000168
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000169 // This must be called by before the GrDrawTarget destructor
170 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000171 // This subclass must do this before the base class destructor runs
172 // since we will unref the GrGLInterface.
173 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000174}
175
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000176///////////////////////////////////////////////////////////////////////////////
177
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000178void GrGpuGL::fillInConfigRenderableTable() {
179
180 // OpenGL < 3.0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000181 // no support for render targets unless the GL_ARB_framebuffer_object
182 // extension is supported (in which case we get ALPHA, RED, RG, RGB,
183 // RGBA (ALPHA8, RGBA4, RGBA8) for OpenGL > 1.1). Note that we
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 // probably don't get R8 in this case.
185
186 // OpenGL 3.0
187 // base color renderable: ALPHA, RED, RG, RGB, and RGBA
188 // sized derivatives: ALPHA8, R8, RGBA4, RGBA8
189
190 // >= OpenGL 3.1
191 // base color renderable: RED, RG, RGB, and RGBA
192 // sized derivatives: R8, RGBA4, RGBA8
193 // if the GL_ARB_compatibility extension is supported then we get back
194 // support for GL_ALPHA and ALPHA8
195
196 // GL_EXT_bgra adds BGRA render targets to any version
197
198 // ES 2.0
199 // color renderable: RGBA4, RGB5_A1, RGB565
200 // GL_EXT_texture_rg adds support for R8 as a color render target
201 // GL_OES_rgb8_rgba8 and/or GL_ARM_rgba8 adds support for RGBA8
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000202 // GL_EXT_texture_format_BGRA8888 and/or GL_APPLE_texture_format_BGRA8888 added BGRA support
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000203
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
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000234GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
235 GrPixelConfig surfaceConfig) const {
236 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000237 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000238 } else if (fGLContext.info().isMesa() &&
239 GrBytesPerPixel(readConfig) == 4 &&
240 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000241 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
242 // Perhaps this should be guarded by some compiletime or runtime check.
243 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000244 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
245 !this->glCaps().readPixelsSupported(this->glInterface(),
246 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
247 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000248 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000249 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000250 }
251}
252
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000253GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
254 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000255 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
256 return kBGRA_8888_GrPixelConfig;
257 } else {
258 return writeConfig;
259 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000260}
261
262bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
263 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
264 return false;
265 }
bsalomon@google.com791816a2013-08-15 18:54:39 +0000266 if (srcConfig != texture->config() && kES_GrGLBinding == this->glBinding()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000267 // In general ES2 requires the internal format of the texture and the format of the src
268 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
269 // texture. It depends upon which extension added BGRA. The Apple extension allows it
270 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
271 // internal format).
272 if (this->glCaps().bgraFormatSupport() &&
273 !this->glCaps().bgraIsInternalFormat() &&
274 kBGRA_8888_GrPixelConfig == srcConfig &&
275 kRGBA_8888_GrPixelConfig == texture->config()) {
276 return true;
277 } else {
278 return false;
279 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000280 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000281 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000282 }
283}
284
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000285bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
286 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
287}
288
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000289void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000290 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000291 if (resetBits & kMisc_GrGLBackendState) {
292 GL_CALL(Disable(GR_GL_DEPTH_TEST));
293 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000294
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
296 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000297
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000298 if (kDesktop_GrGLBinding == this->glBinding()) {
299 // Desktop-only state that we never change
300 if (!this->glCaps().isCoreProfile()) {
301 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
302 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
303 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
304 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
305 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
306 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
307 }
308 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
309 // core profile. This seems like a bug since the core spec removes any mention of
310 // GL_ARB_imaging.
311 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
312 GL_CALL(Disable(GR_GL_COLOR_TABLE));
313 }
314 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
315 // Since ES doesn't support glPointSize at all we always use the VS to
316 // set the point size
317 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
318
319 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
320 // currently part of our gl interface. There are probably others as
321 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000322 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 fHWWriteToColor = kUnknown_TriState;
324 // we only ever use lines in hairline mode
325 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000326 }
edisonn@google.comba669992013-06-28 16:03:21 +0000327
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000328 if (resetBits & kAA_GrGLBackendState) {
329 fHWAAState.invalidate();
330 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000331
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000332 // invalid
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000333 if (resetBits & kTextureBinding_GrGLBackendState) {
334 fHWActiveTextureUnitIdx = -1;
335 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
336 fHWBoundTextures[s] = NULL;
337 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000338 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000339
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000340 if (resetBits & kBlend_GrGLBackendState) {
341 fHWBlendState.invalidate();
342 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000343
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000344 if (resetBits & kView_GrGLBackendState) {
345 fHWScissorSettings.invalidate();
346 fHWViewport.invalidate();
347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000349 if (resetBits & kStencil_GrGLBackendState) {
350 fHWStencilSettings.invalidate();
351 fHWStencilTestEnabled = kUnknown_TriState;
352 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000353
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000354 // Vertex
355 if (resetBits & kVertex_GrGLBackendState) {
356 fHWGeometryState.invalidate();
357 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000359 if (resetBits & kRenderTarget_GrGLBackendState) {
360 fHWBoundRenderTarget = NULL;
361 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000362
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000363 if (resetBits & kPathStencil_GrGLBackendState) {
364 fHWPathStencilMatrixState.invalidate();
365 if (this->caps()->pathStencilingSupport()) {
366 // we don't use the model view matrix.
367 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
368 GL_CALL(LoadIdentity());
369 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000370 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000371
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000372 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000373 if (resetBits & kPixelStore_GrGLBackendState) {
374 if (this->glCaps().unpackRowLengthSupport()) {
375 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
376 }
377 if (this->glCaps().packRowLengthSupport()) {
378 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
379 }
380 if (this->glCaps().unpackFlipYSupport()) {
381 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
382 }
383 if (this->glCaps().packFlipYSupport()) {
384 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
385 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000386 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000387
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000388 if (resetBits & kProgram_GrGLBackendState) {
389 fHWProgramID = 0;
390 fSharedGLProgramState.invalidate();
391 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000392}
393
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000394namespace {
395
396GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
397 // By default, GrRenderTargets are GL's normal orientation so that they
398 // can be drawn to by the outside world without the client having
399 // to render upside down.
400 if (kDefault_GrSurfaceOrigin == origin) {
401 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
402 } else {
403 return origin;
404 }
405}
406
407}
408
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000409GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000410 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000411 return NULL;
412 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000413
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000414 if (0 == desc.fTextureHandle) {
415 return NULL;
416 }
417
bsalomon@google.combcce8922013-03-25 15:38:39 +0000418 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000419 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
420 return NULL;
421 }
422
423 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000424 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000425 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000426 glTexDesc.fWidth = desc.fWidth;
427 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000428 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000429 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000430 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000431 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000432 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000433 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
434 // assuming the old behaviour, which is that backend textures are always
435 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
436 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
437 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
438 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
439 } else {
440 glTexDesc.fOrigin = desc.fOrigin;
441 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000442
443 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000444 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000445 GrGLRenderTarget::Desc glRTDesc;
446 glRTDesc.fRTFBOID = 0;
447 glRTDesc.fTexFBOID = 0;
448 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000449 glRTDesc.fConfig = desc.fConfig;
450 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000451 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000452 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000453 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
454 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000455 glTexDesc.fTextureID,
456 &glRTDesc)) {
457 return NULL;
458 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000459 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000460 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000461 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000462 }
463 if (NULL == texture) {
464 return NULL;
465 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000466
bsalomon@google.come269f212011-11-07 13:29:52 +0000467 return texture;
468}
469
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000470GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000471 GrGLRenderTarget::Desc glDesc;
472 glDesc.fConfig = desc.fConfig;
473 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
474 glDesc.fMSColorRenderbufferID = 0;
475 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
476 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000477 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000478 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000479
480 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000481 GrGLIRect viewport;
482 viewport.fLeft = 0;
483 viewport.fBottom = 0;
484 viewport.fWidth = desc.fWidth;
485 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000486
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000487 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
488 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000489 if (desc.fStencilBits) {
490 GrGLStencilBuffer::Format format;
491 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
492 format.fPacked = false;
493 format.fStencilBits = desc.fStencilBits;
494 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000495 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000496 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
497 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000498 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000499 0,
500 desc.fWidth,
501 desc.fHeight,
502 desc.fSampleCnt,
503 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000504 tgt->setStencilBuffer(sb);
505 sb->unref();
506 }
507 return tgt;
508}
509
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000510////////////////////////////////////////////////////////////////////////////////
511
bsalomon@google.com9c680582013-02-06 18:17:50 +0000512bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000513 int left, int top, int width, int height,
514 GrPixelConfig config, const void* buffer,
515 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000516 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000517 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000518 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000519 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
520
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000521 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000522 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
523 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000524 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000525 desc.fWidth = glTex->width();
526 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000527 desc.fConfig = glTex->config();
528 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000530 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000531
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000532 if (this->uploadTexData(desc, false,
533 left, top, width, height,
534 config, buffer, rowBytes)) {
535 texture->dirtyMipMaps(true);
536 return true;
537 } else {
538 return false;
539 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000540}
541
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000542namespace {
543bool adjust_pixel_ops_params(int surfaceWidth,
544 int surfaceHeight,
545 size_t bpp,
546 int* left, int* top, int* width, int* height,
547 const void** data,
548 size_t* rowBytes) {
549 if (!*rowBytes) {
550 *rowBytes = *width * bpp;
551 }
552
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000553 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
554 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000555
556 if (!subRect.intersect(bounds)) {
557 return false;
558 }
559 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
560 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
561
562 *left = subRect.fLeft;
563 *top = subRect.fTop;
564 *width = subRect.width();
565 *height = subRect.height();
566 return true;
567}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000568
569GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
570 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
571 return GR_GL_GET_ERROR(interface);
572 } else {
573 return CHECK_ALLOC_ERROR(interface);
574 }
575}
576
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000577}
578
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000579bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
580 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000581 int left, int top, int width, int height,
582 GrPixelConfig dataConfig,
583 const void* data,
584 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000585 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000586
587 size_t bpp = GrBytesPerPixel(dataConfig);
588 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
589 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000590 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000591 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000592 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000593
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000594 // in case we need a temporary, trimmed copy of the src pixels
595 SkAutoSMalloc<128 * 128> tempStorage;
596
bsalomon@google.com313f0192012-07-10 17:21:02 +0000597 // paletted textures cannot be partially updated
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000598 bool useTexStorage = isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000599 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000600 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000601
bsalomon@google.com313f0192012-07-10 17:21:02 +0000602 if (useTexStorage && kDesktop_GrGLBinding == this->glBinding()) {
603 // 565 is not a sized internal format on desktop GL. So on desktop with
604 // 565 we always use an unsized internal format to let the system pick
605 // the best sized format to convert the 565 data to. Since TexStorage
606 // only allows sized internal formats we will instead use TexImage2D.
607 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000608 }
609
610 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000611 GrGLenum externalFormat;
612 GrGLenum externalType;
commit-bot@chromium.orgc0b9b202013-08-20 12:22:48 +0000613 // glTexStorage requires sized internal formats on both desktop and ES. ES2
614 // requires an unsized format for glTexImage. ES3 has relaxed this restriction.
615 // Qualcomm Adreno drivers seem to have issues with the relaxed ES3 rules.
616 bool useSizedInternalFormat = useTexStorage ||
617 kDesktop_GrGLBinding == this->glBinding() ||
618 (this->glVersion() >= GR_GL_VER(3,0) &&
619 kQualcomm_GrGLVendor != this->glContext().info().vendor());
620 if (!this->configToGLFormats(dataConfig, useSizedInternalFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000621 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000622 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000623 }
624
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000625 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000626 // paletted textures cannot be updated
627 return false;
628 }
629
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000630 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000631 * check whether to allocate a temporary buffer for flipping y or
632 * because our srcData has extra bytes past each row. If so, we need
633 * to trim those off here, since GL ES may not let us specify
634 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000635 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000636 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000637 bool swFlipY = false;
638 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000639 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000640 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000641 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000642 glFlipY = true;
643 } else {
644 swFlipY = true;
645 }
646 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000647 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000648 // can't use this for flipping, only non-neg values allowed. :(
649 if (rowBytes != trimRowBytes) {
650 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
651 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
652 restoreGLRowLength = true;
653 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000654 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000655 if (trimRowBytes != rowBytes || swFlipY) {
656 // copy data into our new storage, skipping the trailing bytes
657 size_t trimSize = height * trimRowBytes;
658 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000659 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000660 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000661 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000662 char* dst = (char*)tempStorage.reset(trimSize);
663 for (int y = 0; y < height; y++) {
664 memcpy(dst, src, trimRowBytes);
665 if (swFlipY) {
666 src -= rowBytes;
667 } else {
668 src += rowBytes;
669 }
670 dst += trimRowBytes;
671 }
672 // now point data to our copied version
673 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000674 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000675 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000676 if (glFlipY) {
677 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
678 }
679 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000680 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000681 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000683 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000684 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000685 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000686 if (useTexStorage) {
687 // We never resize or change formats of textures. We don't use
688 // mipmaps currently.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000689 GL_ALLOC_CALL(this->glInterface(),
690 TexStorage2D(GR_GL_TEXTURE_2D,
691 1, // levels
692 internalFormat,
693 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000694 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000695 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
696 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
697 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000698 GL_ALLOC_CALL(this->glInterface(),
699 CompressedTexImage2D(GR_GL_TEXTURE_2D,
700 0, // level
701 internalFormat,
702 desc.fWidth, desc.fHeight,
703 0, // border
704 imageSize,
705 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000706 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000707 GL_ALLOC_CALL(this->glInterface(),
708 TexImage2D(GR_GL_TEXTURE_2D,
709 0, // level
710 internalFormat,
711 desc.fWidth, desc.fHeight,
712 0, // border
713 externalFormat, externalType,
714 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000715 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000716 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000717 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000718 if (error != GR_GL_NO_ERROR) {
719 succeeded = false;
720 } else {
721 // if we have data and we used TexStorage to create the texture, we
722 // now upload with TexSubImage.
723 if (NULL != data && useTexStorage) {
724 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
725 0, // level
726 left, top,
727 width, height,
728 externalFormat, externalType,
729 data));
730 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000731 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000732 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000733 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000734 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000735 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000736 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
737 0, // level
738 left, top,
739 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000740 externalFormat, externalType, data));
741 }
742
743 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000744 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000745 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000746 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000747 if (glFlipY) {
748 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
749 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000750 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000751}
752
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000753namespace {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000754bool renderbuffer_storage_msaa(GrGLContext& ctx,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000755 int sampleCount,
756 GrGLenum format,
757 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000758 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000759 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.info().caps()->msFBOType());
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000760 bool created = false;
761 if (GrGLCaps::kNVDesktop_CoverageAAType ==
bsalomon@google.combcce8922013-03-25 15:38:39 +0000762 ctx.info().caps()->coverageAAType()) {
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000763 const GrGLCaps::MSAACoverageMode& mode =
bsalomon@google.combcce8922013-03-25 15:38:39 +0000764 ctx.info().caps()->getMSAACoverageMode(sampleCount);
robertphillips@google.com6177e692013-02-28 20:16:25 +0000765 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000766 RenderbufferStorageMultisampleCoverage(GR_GL_RENDERBUFFER,
767 mode.fCoverageSampleCnt,
768 mode.fColorSampleCnt,
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 if (!created) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000774 GL_ALLOC_CALL(ctx.interface(),
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000775 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
776 sampleCount,
777 format,
778 width, height));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000779 created = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000780 }
781 return created;
782}
783}
784
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000785bool GrGpuGL::createRenderTargetObjects(int width, int height,
786 GrGLuint texID,
787 GrGLRenderTarget::Desc* desc) {
788 desc->fMSColorRenderbufferID = 0;
789 desc->fRTFBOID = 0;
790 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000791 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000792
793 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000794
bsalomon@google.comab15d612011-08-09 12:57:56 +0000795 GrGLenum msColorFormat = 0; // suppress warning
796
bsalomon@google.com347c3822013-05-01 20:10:01 +0000797 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
798 goto FAILED;
799 }
800
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000801 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000802 if (!desc->fTexFBOID) {
803 goto FAILED;
804 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000805
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000807 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
808 // the texture bound to the other. The exception is the IMG multisample extension. With this
809 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
810 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000811 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000812 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
813 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000814 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000815 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000816 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.orgc0b9b202013-08-20 12:22:48 +0000817 // ES2 and ES3 require sized internal formats for rb storage.
bsalomon@google.com791816a2013-08-15 18:54:39 +0000818 kES_GrGLBinding == this->glBinding(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000819 &msColorFormat,
820 NULL,
821 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000822 goto FAILED;
823 }
824 } else {
825 desc->fRTFBOID = desc->fTexFBOID;
826 }
827
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000828 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000829 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000830 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000831 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000832 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000833 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000834 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000835 desc->fSampleCnt,
836 msColorFormat,
837 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 goto FAILED;
839 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000840 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000841 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000842 GR_GL_COLOR_ATTACHMENT0,
843 GR_GL_RENDERBUFFER,
844 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000845 if (desc->fCheckAllocation ||
846 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000847 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
848 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
849 goto FAILED;
850 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000851 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000852 }
853 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000854 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000855
bsalomon@google.com347c3822013-05-01 20:10:01 +0000856 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000857 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
858 GR_GL_COLOR_ATTACHMENT0,
859 GR_GL_TEXTURE_2D,
860 texID, 0, desc->fSampleCnt));
861 } else {
862 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
863 GR_GL_COLOR_ATTACHMENT0,
864 GR_GL_TEXTURE_2D,
865 texID, 0));
866 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000867 if (desc->fCheckAllocation ||
868 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000869 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
870 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
871 goto FAILED;
872 }
bsalomon@google.combcce8922013-03-25 15:38:39 +0000873 fGLContext.info().caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 }
875
876 return true;
877
878FAILED:
879 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000880 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000881 }
882 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000883 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000884 }
885 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000886 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000887 }
888 return false;
889}
890
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000891// good to set a break-point here to know when createTexture fails
892static GrTexture* return_null_texture() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000893// SkASSERT(!"null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000894 return NULL;
895}
896
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000897#if 0 && GR_DEBUG
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000898static size_t as_size_t(int x) {
899 return x;
900}
901#endif
902
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000903GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000904 const void* srcData,
905 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000906
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000907 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000908 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000909
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000910 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000911 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000912 // We fail if the MSAA was requested and is not available.
913 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
914 //GrPrintf("MSAA RT requested but not supported on this platform.");
915 return return_null_texture();
916 }
917 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000918 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000919
robertphillips@google.com32716282012-06-04 12:48:45 +0000920 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000921 glTexDesc.fWidth = desc.fWidth;
922 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000923 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000924 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000925
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000926 glRTDesc.fMSColorRenderbufferID = 0;
927 glRTDesc.fRTFBOID = 0;
928 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000929 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000930 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000931 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000932
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000933 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000934
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000935 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
936 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000937
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000938 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000939 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000940 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000941 //GrPrintf("MSAA RT requested but not supported on this platform.");
942 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000943 }
944
reed@google.comac10a2d2010-12-22 21:39:39 +0000945 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000946 int maxRTSize = this->caps()->maxRenderTargetSize();
947 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000948 return return_null_texture();
949 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000950 } else {
951 int maxSize = this->caps()->maxTextureSize();
952 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
953 return return_null_texture();
954 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000955 }
956
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000958 if (renderTarget && this->glCaps().textureUsageSupport()) {
bsalomon@google.com07dd2bf2011-12-09 19:40:36 +0000959 // provides a hint about how this texture will be used
960 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
961 GR_GL_TEXTURE_USAGE,
962 GR_GL_FRAMEBUFFER_ATTACHMENT));
963 }
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000964 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000965 return return_null_texture();
966 }
967
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000968 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000969 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000970
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000971 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
972 // drivers have a bug where an FBO won't be complete if it includes a
973 // texture that is not mipmap complete (considering the filter in use).
974 GrGLTexture::TexParams initialTexParams;
975 // we only set a subset here so invalidate first
976 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000977 initialTexParams.fMinFilter = GR_GL_NEAREST;
978 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000979 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
980 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000981 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
982 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000983 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000984 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
985 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000986 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000987 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
988 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000989 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000990 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
991 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000992 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000993 if (!this->uploadTexData(glTexDesc, true, 0, 0,
994 glTexDesc.fWidth, glTexDesc.fHeight,
995 desc.fConfig, srcData, rowBytes)) {
996 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
997 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000998 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000999
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001000 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001001 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001002 // unbind the texture from the texture unit before binding it to the frame buffer
1003 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1004
bsalomon@google.com99621082011-11-15 16:47:16 +00001005 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1006 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001007 glTexDesc.fTextureID,
1008 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001009 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 return return_null_texture();
1011 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001012 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001014 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001016 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001017#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001018 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1019 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001020#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001021 return tex;
1022}
1023
1024namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001025
1026const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1027
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001028void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001030
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001032 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 (kUnknownBitCount == format->fTotalBits));
1034 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001035 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001036 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1037 (GrGLint*)&format->fStencilBits);
1038 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001039 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001040 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1041 (GrGLint*)&format->fTotalBits);
1042 format->fTotalBits += format->fStencilBits;
1043 } else {
1044 format->fTotalBits = format->fStencilBits;
1045 }
1046 }
1047}
1048}
1049
1050bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1051 int width, int height) {
1052
1053 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001054 // SBs for a client's standalone RT (that is a RT that isn't also a texture).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001055 SkASSERT(rt->asTexture());
1056 SkASSERT(width >= rt->width());
1057 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058
1059 int samples = rt->numSamples();
1060 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001061 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001062 if (!sbID) {
1063 return false;
1064 }
1065
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001066 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001068 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001069 // we start with the last stencil format that succeeded in hopes
1070 // that we won't go through this loop more than once after the
1071 // first (painful) stencil creation.
1072 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001073 const GrGLCaps::StencilFormat& sFmt =
1074 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001075 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001076 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001078 bool created;
1079 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001080 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001081 samples,
1082 sFmt.fInternalFormat,
1083 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001084 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001085 GL_ALLOC_CALL(this->glInterface(),
1086 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001087 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001088 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001089 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001090 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001091 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001092 if (created) {
1093 // After sized formats we attempt an unsized format and take
1094 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001095 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001096 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001097 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001098 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001099 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001100 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001101 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1102 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001103 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001104 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 return true;
1106 }
1107 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001108 }
1109 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001111 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112}
1113
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001114bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1116
1117 GrGLuint fbo = glrt->renderFBOID();
1118
1119 if (NULL == sb) {
1120 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001121 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001122 GR_GL_STENCIL_ATTACHMENT,
1123 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001125 GR_GL_DEPTH_ATTACHMENT,
1126 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001128 GrGLenum status;
1129 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001130 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131#endif
1132 }
1133 return true;
1134 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001135 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 GrGLuint rb = glsb->renderbufferID();
1137
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001138 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1140 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001141 GR_GL_STENCIL_ATTACHMENT,
1142 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001145 GR_GL_DEPTH_ATTACHMENT,
1146 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001149 GR_GL_DEPTH_ATTACHMENT,
1150 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001153 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001154 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001155 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1156 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001158 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001160 if (glsb->format().fPacked) {
1161 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1162 GR_GL_DEPTH_ATTACHMENT,
1163 GR_GL_RENDERBUFFER, 0));
1164 }
1165 return false;
1166 } else {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001167 fGLContext.info().caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001168 rt->config(),
1169 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001172 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001174}
1175
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001176////////////////////////////////////////////////////////////////////////////////
1177
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001178GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001179 GrGLVertexBuffer::Desc desc;
1180 desc.fDynamic = dynamic;
1181 desc.fSizeInBytes = size;
1182 desc.fIsWrapped = false;
1183
bsalomon@google.com96966a52013-02-21 16:34:21 +00001184 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001185 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001186 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001188 } else {
1189 GL_CALL(GenBuffers(1, &desc.fID));
1190 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001191 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001192 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1193 // make sure driver can allocate memory for this buffer
1194 GL_ALLOC_CALL(this->glInterface(),
1195 BufferData(GR_GL_ARRAY_BUFFER,
1196 desc.fSizeInBytes,
1197 NULL, // data ptr
1198 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1199 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1200 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001201 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001202 return NULL;
1203 }
1204 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1205 return vertexBuffer;
1206 }
1207 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001209}
1210
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001211GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(uint32_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001212 GrGLIndexBuffer::Desc desc;
1213 desc.fDynamic = dynamic;
1214 desc.fSizeInBytes = size;
1215 desc.fIsWrapped = false;
1216
bsalomon@google.com96966a52013-02-21 16:34:21 +00001217 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001218 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001219 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001221 } else {
1222 GL_CALL(GenBuffers(1, &desc.fID));
1223 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001224 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001225 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1226 // make sure driver can allocate memory for this buffer
1227 GL_ALLOC_CALL(this->glInterface(),
1228 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
1229 desc.fSizeInBytes,
1230 NULL, // data ptr
1231 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1232 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1233 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001234 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001235 return NULL;
1236 }
1237 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1238 return indexBuffer;
1239 }
1240 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001242}
1243
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001244GrPath* GrGpuGL::onCreatePath(const SkPath& inPath) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001245 SkASSERT(this->caps()->pathStencilingSupport());
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001246 return SkNEW_ARGS(GrGLPath, (this, inPath));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001247}
1248
bsalomon@google.coma3201942012-06-21 19:58:20 +00001249void GrGpuGL::flushScissor() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001250 const GrDrawState& drawState = this->getDrawState();
1251 const GrGLRenderTarget* rt =
1252 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1253
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001254 SkASSERT(NULL != rt);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001255 const GrGLIRect& vp = rt->getViewport();
reed@google.comac10a2d2010-12-22 21:39:39 +00001256
bsalomon@google.coma3201942012-06-21 19:58:20 +00001257 if (fScissorState.fEnabled) {
1258 GrGLIRect scissor;
1259 scissor.setRelativeTo(vp,
1260 fScissorState.fRect.fLeft,
1261 fScissorState.fRect.fTop,
1262 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001263 fScissorState.fRect.height(),
1264 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001265 // if the scissor fully contains the viewport then we fall through and
1266 // disable the scissor test.
1267 if (!scissor.contains(vp)) {
1268 if (fHWScissorSettings.fRect != scissor) {
1269 scissor.pushToGLScissor(this->glInterface());
1270 fHWScissorSettings.fRect = scissor;
1271 }
1272 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1273 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1274 fHWScissorSettings.fEnabled = kYes_TriState;
1275 }
1276 return;
1277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001279 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001280 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001281 fHWScissorSettings.fEnabled = kNo_TriState;
1282 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 }
1284}
1285
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001286void GrGpuGL::onClear(const SkIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001287 const GrDrawState& drawState = this->getDrawState();
1288 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001289 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001290 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001291
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001292 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001293 if (NULL != rect) {
1294 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001295 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001296 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001297 if (clippedRect.intersect(rtRect)) {
1298 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001299 } else {
1300 return;
1301 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001303 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001304 GrAutoTRestore<ScissorState> asr(&fScissorState);
1305 fScissorState.fEnabled = (NULL != rect);
1306 if (fScissorState.fEnabled) {
1307 fScissorState.fRect = *rect;
1308 }
1309 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001310
1311 GrGLfloat r, g, b, a;
1312 static const GrGLfloat scale255 = 1.f / 255.f;
1313 a = GrColorUnpackA(color) * scale255;
1314 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001315 r = GrColorUnpackR(color) * scaleRGB;
1316 g = GrColorUnpackG(color) * scaleRGB;
1317 b = GrColorUnpackB(color) * scaleRGB;
1318
1319 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001320 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001321 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001323}
1324
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001325void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001326 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001327 return;
1328 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001329
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001330 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001331
bsalomon@google.coma3201942012-06-21 19:58:20 +00001332 GrAutoTRestore<ScissorState> asr(&fScissorState);
1333 fScissorState.fEnabled = false;
1334 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001335
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001336 GL_CALL(StencilMask(0xffffffff));
1337 GL_CALL(ClearStencil(0));
1338 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001339 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001340}
1341
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001342void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001343 const GrDrawState& drawState = this->getDrawState();
1344 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001345 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001346
1347 // this should only be called internally when we know we have a
1348 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001349 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001350 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001351#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001352 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001353 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001354#else
1355 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001356 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001357 // turned into draws. Our contract on GrDrawTarget says that
1358 // changing the clip between stencil passes may or may not
1359 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001360 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001361#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001362 GrGLint value;
1363 if (insideClip) {
1364 value = (1 << (stencilBitCount - 1));
1365 } else {
1366 value = 0;
1367 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001368 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001369
1370 GrAutoTRestore<ScissorState> asr(&fScissorState);
1371 fScissorState.fEnabled = true;
1372 fScissorState.fRect = rect;
1373 this->flushScissor();
1374
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001375 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001376 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001377 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001378 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001379}
1380
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001381void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001382 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
bsalomon@google.comc4364992011-11-07 15:54:49 +00001385bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1386 int left, int top,
1387 int width, int height,
1388 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001389 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001390 // If this rendertarget is aready TopLeft, we don't need to flip.
1391 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1392 return false;
1393 }
1394
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001395 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001396 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001397 return false;
1398 }
1399
1400 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001401 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001402 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001403 return true;
1404 }
1405 // If we have to do memcpys to handle rowBytes then y-flip is free
1406 // Note the rowBytes might be tight to the passed in data, but if data
1407 // gets clipped in x to the target the rowBytes will no longer be tight.
1408 if (left >= 0 && (left + width) < renderTarget->width()) {
1409 return 0 == rowBytes ||
1410 GrBytesPerPixel(config) * width == rowBytes;
1411 } else {
1412 return false;
1413 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001414}
1415
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001416bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001417 int left, int top,
1418 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001419 GrPixelConfig config,
1420 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001421 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001422 GrGLenum format;
1423 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001424 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001425 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001427 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001428 size_t bpp = GrBytesPerPixel(config);
1429 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1430 &left, &top, &width, &height,
1431 const_cast<const void**>(&buffer),
1432 &rowBytes)) {
1433 return false;
1434 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001435
bsalomon@google.comc6980972011-11-02 19:57:21 +00001436 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001437 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001438 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001439 switch (tgt->getResolveType()) {
1440 case GrGLRenderTarget::kCantResolve_ResolveType:
1441 return false;
1442 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001443 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001444 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001445 break;
1446 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001447 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001448 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001449 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1450 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001451 break;
1452 default:
1453 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001454 }
1455
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001456 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001457
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001458 // the read rect is viewport-relative
1459 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001460 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001461
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001462 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001463 if (0 == rowBytes) {
1464 rowBytes = tightRowBytes;
1465 }
1466 size_t readDstRowBytes = tightRowBytes;
1467 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001468
bsalomon@google.comc6980972011-11-02 19:57:21 +00001469 // determine if GL can read using the passed rowBytes or if we need
1470 // a scratch buffer.
1471 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1472 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001473 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001474 SkASSERT(!(rowBytes % sizeof(GrColor)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001475 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowBytes / sizeof(GrColor)));
1476 readDstRowBytes = rowBytes;
1477 } else {
1478 scratch.reset(tightRowBytes * height);
1479 readDst = scratch.get();
1480 }
1481 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001482 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001483 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1484 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001485 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1486 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001487 format, type, readDst));
1488 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001489 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001490 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1491 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001492 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001493 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001494 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001495 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001496
1497 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001498 // API presents top-to-bottom. We must preserve the padding contents. Note
1499 // that the above readPixels did not overwrite the padding.
1500 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001501 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001502 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001503 scratch.reset(tightRowBytes);
1504 void* tmpRow = scratch.get();
1505 // flip y in-place by rows
1506 const int halfY = height >> 1;
1507 char* top = reinterpret_cast<char*>(buffer);
1508 char* bottom = top + (height - 1) * rowBytes;
1509 for (int y = 0; y < halfY; y++) {
1510 memcpy(tmpRow, top, tightRowBytes);
1511 memcpy(top, bottom, tightRowBytes);
1512 memcpy(bottom, tmpRow, tightRowBytes);
1513 top += rowBytes;
1514 bottom -= rowBytes;
1515 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001516 }
1517 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001518 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001519 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001520 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001521 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001522 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001523 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524 dst += (height-1) * rowBytes;
1525 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001526 for (int y = 0; y < height; y++) {
1527 memcpy(dst, src, tightRowBytes);
1528 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001529 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001530 dst += rowBytes;
1531 } else {
1532 dst -= rowBytes;
1533 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001534 }
1535 }
1536 return true;
1537}
1538
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001539void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001540
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001541 GrGLRenderTarget* rt =
1542 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001543 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001544
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001545 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001546 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001547#if GR_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001548 GrGLenum status;
1549 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.comc312bf92011-03-21 21:10:33 +00001550 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
reed@google.comb9255d52011-06-13 18:54:59 +00001551 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
reed@google.comac10a2d2010-12-22 21:39:39 +00001552 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001553#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001554 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001555 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001556 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001557 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001558 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 }
1560 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001561 if (NULL == bound || !bound->isEmpty()) {
1562 rt->flagAsNeedingResolve(bound);
1563 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001564
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001565 GrTexture *texture = rt->asTexture();
1566 if (texture) {
1567 texture->dirtyMipMaps(true);
1568 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001569}
1570
twiz@google.com0f31ca72011-03-18 17:38:11 +00001571GrGLenum gPrimitiveType2GLMode[] = {
1572 GR_GL_TRIANGLES,
1573 GR_GL_TRIANGLE_STRIP,
1574 GR_GL_TRIANGLE_FAN,
1575 GR_GL_POINTS,
1576 GR_GL_LINES,
1577 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001578};
1579
bsalomon@google.comd302f142011-03-03 13:54:13 +00001580#define SWAP_PER_DRAW 0
1581
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001582#if SWAP_PER_DRAW
bsalomon@google.comd302f142011-03-03 13:54:13 +00001583 #if GR_MAC_BUILD
1584 #include <AGL/agl.h>
1585 #elif GR_WIN32_BUILD
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001586 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001587 void SwapBuf() {
1588 DWORD procID = GetCurrentProcessId();
1589 HWND hwnd = GetTopWindow(GetDesktopWindow());
1590 while(hwnd) {
1591 DWORD wndProcID = 0;
1592 GetWindowThreadProcessId(hwnd, &wndProcID);
1593 if(wndProcID == procID) {
1594 SwapBuffers(GetDC(hwnd));
1595 }
1596 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1597 }
1598 }
1599 #endif
1600#endif
1601
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001602void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001603 size_t indexOffsetInBytes;
1604 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001605
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001606 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001607
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001608 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001609 GrGLvoid* indices =
1610 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001611 // info.startVertex() was accounted for by setupGeometry.
1612 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1613 info.indexCount(),
1614 GR_GL_UNSIGNED_SHORT,
1615 indices));
1616 } else {
1617 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1618 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1619 // accounted for startVertex.
1620 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1621 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001622#if SWAP_PER_DRAW
1623 glFlush();
1624 #if GR_MAC_BUILD
1625 aglSwapBuffers(aglGetCurrentContext());
1626 int set_a_break_pt_here = 9;
1627 aglSwapBuffers(aglGetCurrentContext());
1628 #elif GR_WIN32_BUILD
1629 SwapBuf();
1630 int set_a_break_pt_here = 9;
1631 SwapBuf();
1632 #endif
1633#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001634}
1635
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001636namespace {
bsalomon@google.com100abf42012-09-05 17:40:04 +00001637
1638static const uint16_t kOnes16 = static_cast<uint16_t>(~0);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001639const GrStencilSettings& winding_nv_path_stencil_settings() {
1640 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1641 kIncClamp_StencilOp,
1642 kIncClamp_StencilOp,
1643 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001644 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001645 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1646}
1647const GrStencilSettings& even_odd_nv_path_stencil_settings() {
1648 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
1649 kInvert_StencilOp,
1650 kInvert_StencilOp,
1651 kAlwaysIfInClip_StencilFunc,
bsalomon@google.com100abf42012-09-05 17:40:04 +00001652 kOnes16, kOnes16, kOnes16);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001653 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
1654}
1655}
1656
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001657void GrGpuGL::setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001658 SkPath::FillType fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001659 GrStencilSettings* settings) {
1660 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001661 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001662 *settings = even_odd_nv_path_stencil_settings();
1663 return;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001664 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001665 *settings = winding_nv_path_stencil_settings();
1666 return;
1667 default:
1668 GrCrash("Unexpected path fill.");
1669 }
1670}
1671
sugoi@google.com12b4e272012-12-06 20:13:11 +00001672void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001673 SkASSERT(this->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001674
1675 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1676 GrDrawState* drawState = this->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001677 SkASSERT(NULL != drawState->getRenderTarget());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001678 if (NULL == drawState->getRenderTarget()->getStencilBuffer()) {
1679 return;
1680 }
1681
1682 // Decide how to manipulate the stencil buffer based on the fill rule.
1683 // Also, assert that the stencil settings we set in setStencilPathSettings
1684 // are present.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001685 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001686 GrGLenum fillMode;
1687 switch (fill) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001688 case SkPath::kWinding_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001689 fillMode = GR_GL_COUNT_UP;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001690 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001691 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001692 SkASSERT(kIncClamp_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001693 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1694 break;
sugoi@google.com12b4e272012-12-06 20:13:11 +00001695 case SkPath::kEvenOdd_FillType:
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001696 fillMode = GR_GL_INVERT;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001697 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001698 fStencilSettings.passOp(GrStencilSettings::kFront_Face));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001699 SkASSERT(kInvert_StencilOp ==
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001700 fStencilSettings.failOp(GrStencilSettings::kFront_Face));
1701 break;
1702 default:
1703 // Only the above two fill rules are allowed.
1704 GrCrash("Unexpected path fill.");
bsalomon@google.com548a4332012-07-11 19:45:22 +00001705 return; // suppress unused var warning.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001706 }
1707 GrGLint writeMask = fStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1708 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001709}
1710
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001711void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001712 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001713 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001714 // Some extensions automatically resolves the texture when it is read.
1715 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001716 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001717 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1718 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1719 // make sure we go through flushRenderTarget() since we've modified
1720 // the bound DRAW FBO ID.
1721 fHWBoundRenderTarget = NULL;
1722 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001723 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001724 GrGLIRect r;
1725 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1726 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001727
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001728 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001729 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001730 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001731 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001732 fScissorState.fEnabled = true;
1733 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001734 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001735 GL_CALL(ResolveMultisampleFramebuffer());
1736 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001737 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001738 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001739 asr.reset(&fScissorState);
1740 fScissorState.fEnabled = false;
1741 this->flushScissor();
1742 }
1743 int right = r.fLeft + r.fWidth;
1744 int top = r.fBottom + r.fHeight;
1745 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1746 r.fLeft, r.fBottom, right, top,
1747 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001748 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001750 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 }
1752}
1753
bsalomon@google.com411dad02012-06-05 20:24:20 +00001754namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001755
bsalomon@google.com411dad02012-06-05 20:24:20 +00001756GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1757 static const GrGLenum gTable[] = {
1758 GR_GL_ALWAYS, // kAlways_StencilFunc
1759 GR_GL_NEVER, // kNever_StencilFunc
1760 GR_GL_GREATER, // kGreater_StencilFunc
1761 GR_GL_GEQUAL, // kGEqual_StencilFunc
1762 GR_GL_LESS, // kLess_StencilFunc
1763 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1764 GR_GL_EQUAL, // kEqual_StencilFunc,
1765 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1766 };
1767 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1768 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1769 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1770 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1771 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1772 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1773 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1774 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1775 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001776 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001777
1778 return gTable[basicFunc];
1779}
1780
1781GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1782 static const GrGLenum gTable[] = {
1783 GR_GL_KEEP, // kKeep_StencilOp
1784 GR_GL_REPLACE, // kReplace_StencilOp
1785 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1786 GR_GL_INCR, // kIncClamp_StencilOp
1787 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1788 GR_GL_DECR, // kDecClamp_StencilOp
1789 GR_GL_ZERO, // kZero_StencilOp
1790 GR_GL_INVERT, // kInvert_StencilOp
1791 };
1792 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1793 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1794 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1795 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1796 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1797 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1798 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1799 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1800 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001801 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001802 return gTable[op];
1803}
1804
1805void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001806 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001807 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001808 GrStencilSettings::Face grFace) {
1809 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1810 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1811 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1812
1813 GrGLint ref = settings.funcRef(grFace);
1814 GrGLint mask = settings.funcMask(grFace);
1815 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001816
1817 if (GR_GL_FRONT_AND_BACK == glFace) {
1818 // we call the combined func just in case separate stencil is not
1819 // supported.
1820 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1821 GR_GL_CALL(gl, StencilMask(writeMask));
1822 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1823 } else {
1824 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1825 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1826 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1827 }
1828}
1829}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001830
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001831void GrGpuGL::flushStencil(DrawType type) {
1832 if (kStencilPath_DrawType == type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001833 SkASSERT(!fStencilSettings.isTwoSided());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001834 // Just the func, ref, and mask is set here. The op and write mask are params to the call
1835 // that draws the path to the SB (glStencilFillPath)
1836 GrGLenum func =
1837 gr_to_gl_stencil_func(fStencilSettings.func(GrStencilSettings::kFront_Face));
1838 GL_CALL(PathStencilFunc(func,
1839 fStencilSettings.funcRef(GrStencilSettings::kFront_Face),
1840 fStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
1841 } else if (fHWStencilSettings != fStencilSettings) {
1842 if (fStencilSettings.isDisabled()) {
1843 if (kNo_TriState != fHWStencilTestEnabled) {
1844 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1845 fHWStencilTestEnabled = kNo_TriState;
1846 }
1847 } else {
1848 if (kYes_TriState != fHWStencilTestEnabled) {
1849 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1850 fHWStencilTestEnabled = kYes_TriState;
1851 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001852 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001853 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001854 if (this->caps()->twoSidedStencilSupport()) {
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,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001858 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001859 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001860 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001861 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001862 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001863 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001864 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001865 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001866 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001867 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 }
1869 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001870 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001871 }
1872}
1873
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001874void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001875// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1876// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1877#if 0
1878 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1879 #define RT_HAS_MSAA rt->isMultisampled()
1880#else
1881 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1882#endif
1883
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001884 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001885 if (kDesktop_GrGLBinding == this->glBinding()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001886 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1887 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001888 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001889 bool smoothLines = false;
1890
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001891 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001892 smoothLines = this->willUseHWAALines();
1893 if (smoothLines) {
1894 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1895 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1896 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1897 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001898 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001899 kNo_TriState != fHWAAState.fMSAAEnabled) {
1900 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1901 fHWAAState.fMSAAEnabled = kNo_TriState;
1902 }
1903 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001904 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001905 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1906 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1907 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1908 }
1909 }
1910 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001911 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001912 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1913 // convex hulls of each segment appear to get filled.
1914 bool enableMSAA = kStencilPath_DrawType == type ||
1915 this->getDrawState().isHWAntialiasState();
1916 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001917 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1918 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1919 fHWAAState.fMSAAEnabled = kYes_TriState;
1920 }
1921 } else {
1922 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1923 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1924 fHWAAState.fMSAAEnabled = kNo_TriState;
1925 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001926 }
1927 }
1928 }
1929}
1930
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001931void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001932 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001933 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001934 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001935 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001937 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001938 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001939 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
1940 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
1941 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
1942 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
1943 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
1944 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001945 }
1946 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00001947 // any optimization to disable blending should
1948 // have already been applied and tweaked the coeffs
1949 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00001950 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
1951 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001952 if (blendOff) {
1953 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001954 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001955 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001956 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001957 } else {
1958 if (kYes_TriState != fHWBlendState.fEnabled) {
1959 GL_CALL(Enable(GR_GL_BLEND));
1960 fHWBlendState.fEnabled = kYes_TriState;
1961 }
1962 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1963 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001964 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1965 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001966 fHWBlendState.fSrcCoeff = srcCoeff;
1967 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001968 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00001969 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001970 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1971 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001972 (!fHWBlendState.fConstColorValid ||
1973 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00001974 GrGLfloat c[4];
1975 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001976 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001977 fHWBlendState.fConstColor = blendConst;
1978 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001979 }
1980 }
1981 }
1982}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001983namespace {
1984
bsalomon@google.com6d003d12012-09-11 15:45:20 +00001985inline void set_tex_swizzle(GrGLenum swizzle[4], const GrGLInterface* gl) {
bsalomon@google.com4d063de2012-05-31 17:59:23 +00001986 GR_GL_CALL(gl, TexParameteriv(GR_GL_TEXTURE_2D,
1987 GR_GL_TEXTURE_SWIZZLE_RGBA,
1988 reinterpret_cast<const GrGLint*>(swizzle)));
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001989}
bsalomon@google.comb8670992012-07-25 21:27:09 +00001990
bsalomon@google.com6c76d242012-09-07 16:52:24 +00001991inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00001992 static const GrGLenum gWrapModes[] = {
1993 GR_GL_CLAMP_TO_EDGE,
1994 GR_GL_REPEAT,
1995 GR_GL_MIRRORED_REPEAT
1996 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00001997 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00001998 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
1999 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2000 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2001 return gWrapModes[tm];
2002}
2003
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002004}
2005
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002006void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002007 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002008
bsalomon@google.comb8670992012-07-25 21:27:09 +00002009 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2010 // 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 +00002011 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002012 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002013 if (NULL != texRT) {
2014 this->onResolveRenderTarget(texRT);
2015 }
2016
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002017 if (fHWBoundTextures[unitIdx] != texture) {
2018 this->setTextureUnit(unitIdx);
2019 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2020 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002021 }
2022
bsalomon@google.com4c883782012-06-04 19:05:11 +00002023 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002024 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002025 bool setAll = timestamp < this->getResetTimestamp();
2026 GrGLTexture::TexParams newTexParams;
2027
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002028 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002029 GR_GL_NEAREST,
2030 GR_GL_LINEAR,
2031 GR_GL_LINEAR_MIPMAP_LINEAR
2032 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002033 static GrGLenum glMagFilterModes[] = {
2034 GR_GL_NEAREST,
2035 GR_GL_LINEAR,
2036 GR_GL_LINEAR
2037 };
2038 newTexParams.fMinFilter = glMinFilterModes[params.filterMode()];
2039 newTexParams.fMagFilter = glMagFilterModes[params.filterMode()];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002040
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002041#ifndef SKIA_IGNORE_GPU_MIPMAPS
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002042 if (params.filterMode() == GrTextureParams::kMipMap_FilterMode &&
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002043 texture->mipMapsAreDirty()) {
2044// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2045 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2046 texture->dirtyMipMaps(false);
2047 }
2048#endif
bsalomon@google.com4c883782012-06-04 19:05:11 +00002049
bsalomon@google.comb8670992012-07-25 21:27:09 +00002050 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2051 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002052 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002053 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002054 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002055 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
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_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002059 newTexParams.fMagFilter));
2060 }
2061 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2062 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002063 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2064 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002065 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002066 }
2067 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002068 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002069 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2070 GR_GL_TEXTURE_WRAP_S,
2071 newTexParams.fWrapS));
2072 }
2073 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002074 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002075 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2076 GR_GL_TEXTURE_WRAP_T,
2077 newTexParams.fWrapT));
2078 }
2079 if (this->glCaps().textureSwizzleSupport() &&
2080 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2081 oldTexParams.fSwizzleRGBA,
2082 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002083 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002084 set_tex_swizzle(newTexParams.fSwizzleRGBA,
2085 this->glInterface());
2086 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002087 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002088}
2089
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002090void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002091
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002092 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002093
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002094 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002095 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002096 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002097 fHWDitherEnabled = kYes_TriState;
2098 }
2099 } else {
2100 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002101 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002102 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002103 }
2104 }
2105
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002106 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002107 if (kNo_TriState != fHWWriteToColor) {
2108 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2109 GR_GL_FALSE, GR_GL_FALSE));
2110 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002111 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002112 } else {
2113 if (kYes_TriState != fHWWriteToColor) {
2114 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2115 fHWWriteToColor = kYes_TriState;
2116 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002117 }
2118
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002119 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002120 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002121 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002122 GL_CALL(Enable(GR_GL_CULL_FACE));
2123 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002124 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002125 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002126 GL_CALL(Enable(GR_GL_CULL_FACE));
2127 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002128 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002129 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002130 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002131 break;
2132 default:
2133 GrCrash("Unknown draw face.");
2134 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002135 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002136 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002137}
2138
reed@google.comac10a2d2010-12-22 21:39:39 +00002139void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002140 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002141 if (fHWBoundRenderTarget == renderTarget) {
2142 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002143 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002144}
2145
2146void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002147 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002148 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002149 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002150 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002151 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002152 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002153}
2154
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002155bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2156 bool getSizedInternalFormat,
2157 GrGLenum* internalFormat,
2158 GrGLenum* externalFormat,
2159 GrGLenum* externalType) {
2160 GrGLenum dontCare;
2161 if (NULL == internalFormat) {
2162 internalFormat = &dontCare;
2163 }
2164 if (NULL == externalFormat) {
2165 externalFormat = &dontCare;
2166 }
2167 if (NULL == externalType) {
2168 externalType = &dontCare;
2169 }
2170
reed@google.comac10a2d2010-12-22 21:39:39 +00002171 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002172 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002173 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002174 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002175 if (getSizedInternalFormat) {
2176 *internalFormat = GR_GL_RGBA8;
2177 } else {
2178 *internalFormat = GR_GL_RGBA;
2179 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002180 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002181 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002182 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002183 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002184 return false;
2185 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002186 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002187 if (getSizedInternalFormat) {
2188 *internalFormat = GR_GL_BGRA8;
2189 } else {
2190 *internalFormat = GR_GL_BGRA;
2191 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002192 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002193 if (getSizedInternalFormat) {
2194 *internalFormat = GR_GL_RGBA8;
2195 } else {
2196 *internalFormat = GR_GL_RGBA;
2197 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002198 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002199 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002200 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002201 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002202 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002203 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002204 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002205 if (getSizedInternalFormat) {
2206 if (this->glBinding() == kDesktop_GrGLBinding) {
2207 return false;
2208 } else {
2209 *internalFormat = GR_GL_RGB565;
2210 }
2211 } else {
2212 *internalFormat = GR_GL_RGB;
2213 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002214 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002215 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002216 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002217 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002218 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002219 if (getSizedInternalFormat) {
2220 *internalFormat = GR_GL_RGBA4;
2221 } else {
2222 *internalFormat = GR_GL_RGBA;
2223 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002224 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002226 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002227 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002228 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002229 // glCompressedTexImage doesn't take external params
2230 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002231 // no sized/unsized internal format distinction here
2232 *internalFormat = GR_GL_PALETTE8_RGBA8;
2233 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002234 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002235 } else {
2236 return false;
2237 }
2238 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002239 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002240 if (this->glCaps().textureRedSupport()) {
2241 *internalFormat = GR_GL_RED;
2242 *externalFormat = GR_GL_RED;
2243 if (getSizedInternalFormat) {
2244 *internalFormat = GR_GL_R8;
2245 } else {
2246 *internalFormat = GR_GL_RED;
2247 }
2248 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002249 } else {
2250 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002251 *externalFormat = GR_GL_ALPHA;
2252 if (getSizedInternalFormat) {
2253 *internalFormat = GR_GL_ALPHA8;
2254 } else {
2255 *internalFormat = GR_GL_ALPHA;
2256 }
2257 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002258 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002259 break;
2260 default:
2261 return false;
2262 }
2263 return true;
2264}
2265
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002266void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002267 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002268 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002269 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002270 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002271 }
2272}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002273
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002274void GrGpuGL::setScratchTextureUnit() {
2275 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2276 int lastUnitIdx = fHWBoundTextures.count() - 1;
2277 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2278 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2279 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002280 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002281 // clear out the this field so that if a program does use this unit it will rebind the correct
2282 // texture.
2283 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002284}
2285
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002286namespace {
2287// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002288inline bool can_blit_framebuffer(const GrSurface* dst,
2289 const GrSurface* src,
2290 const GrGpuGL* gpu,
2291 bool* wouldNeedTempFBO = NULL) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002292 if (gpu->isConfigRenderable(dst->config()) &&
2293 gpu->isConfigRenderable(src->config()) &&
2294 gpu->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002295 if (NULL != wouldNeedTempFBO) {
2296 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2297 }
2298 return true;
2299 } else {
2300 return false;
2301 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002302}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002303
2304inline bool can_copy_texsubimage(const GrSurface* dst,
2305 const GrSurface* src,
2306 const GrGpuGL* gpu,
2307 bool* wouldNeedTempFBO = NULL) {
2308 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2309 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2310 // many drivers would allow it to work, but ANGLE does not.
bsalomon@google.com791816a2013-08-15 18:54:39 +00002311 if (kES_GrGLBinding == gpu->glBinding() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002312 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2313 return false;
2314 }
2315 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2316 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2317 // then we don't want to copy to the texture but to the MSAA buffer.
2318 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2319 return false;
2320 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002321 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2322 // If the src is multisampled (and uses an extension where there is a separate MSAA
2323 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2324 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2325 return false;
2326 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002327 if (gpu->isConfigRenderable(src->config()) && NULL != dst->asTexture() &&
2328 dst->origin() == src->origin() && kIndex_8_GrPixelConfig != src->config()) {
2329 if (NULL != wouldNeedTempFBO) {
2330 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2331 }
2332 return true;
2333 } else {
2334 return false;
2335 }
2336}
2337
2338// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2339// relative to is output.
2340inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2341 GrSurface* surface,
2342 GrGLenum fboTarget,
2343 GrGLIRect* viewport) {
2344 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2345 GrGLuint tempFBOID;
2346 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002347 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002348 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2349 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2350 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2351 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2352 GR_GL_COLOR_ATTACHMENT0,
2353 GR_GL_TEXTURE_2D,
2354 texID,
2355 0));
2356 viewport->fLeft = 0;
2357 viewport->fBottom = 0;
2358 viewport->fWidth = surface->width();
2359 viewport->fHeight = surface->height();
2360 } else {
2361 tempFBOID = 0;
2362 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2363 *viewport = rt->getViewport();
2364 }
2365 return tempFBOID;
2366}
2367
2368}
2369
2370void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2371 // Check for format issues with glCopyTexSubImage2D
bsalomon@google.com791816a2013-08-15 18:54:39 +00002372 if (kES_GrGLBinding == this->glBinding() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002373 kBGRA_8888_GrPixelConfig == src->config()) {
2374 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002375 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002376 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002377 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002378 } else if (NULL == src->asRenderTarget()) {
2379 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2380 // class handle it by rendering.
2381 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002382 return;
2383 }
2384
2385 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2386 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2387 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2388 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002389 } else {
2390 desc->fConfig = src->config();
2391 desc->fOrigin = src->origin();
2392 desc->fFlags = kNone_GrTextureFlags;
2393 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002394}
2395
2396bool GrGpuGL::onCopySurface(GrSurface* dst,
2397 GrSurface* src,
2398 const SkIRect& srcRect,
2399 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002400 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002401 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002402 bool wouldNeedTempFBO = false;
2403 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2404 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2405 GrGLuint srcFBO;
2406 GrGLIRect srcVP;
2407 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2408 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002409 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002410 // We modified the bound FBO
2411 fHWBoundRenderTarget = NULL;
2412 GrGLIRect srcGLRect;
2413 srcGLRect.setRelativeTo(srcVP,
2414 srcRect.fLeft,
2415 srcRect.fTop,
2416 srcRect.width(),
2417 srcRect.height(),
2418 src->origin());
2419
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002420 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002421 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2422 GrGLint dstY;
2423 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2424 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2425 } else {
2426 dstY = dstPoint.fY;
2427 }
2428 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2429 dstPoint.fX, dstY,
2430 srcGLRect.fLeft, srcGLRect.fBottom,
2431 srcGLRect.fWidth, srcGLRect.fHeight));
2432 copied = true;
2433 if (srcFBO) {
2434 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2435 }
2436 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2437 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002438 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2439 srcRect.width(), srcRect.height());
2440 bool selfOverlap = false;
2441 if (dst->isSameAs(src)) {
2442 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2443 }
2444
2445 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002446 GrGLuint dstFBO;
2447 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002448 GrGLIRect dstVP;
2449 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002450 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2451 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2452 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002453 fHWBoundRenderTarget = NULL;
2454 GrGLIRect srcGLRect;
2455 GrGLIRect dstGLRect;
2456 srcGLRect.setRelativeTo(srcVP,
2457 srcRect.fLeft,
2458 srcRect.fTop,
2459 srcRect.width(),
2460 srcRect.height(),
2461 src->origin());
2462 dstGLRect.setRelativeTo(dstVP,
2463 dstRect.fLeft,
2464 dstRect.fTop,
2465 dstRect.width(),
2466 dstRect.height(),
2467 dst->origin());
2468
2469 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002470 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002471 // The EXT version applies the scissor during the blit, so disable it.
2472 asr.reset(&fScissorState);
2473 fScissorState.fEnabled = false;
2474 this->flushScissor();
2475 }
2476 GrGLint srcY0;
2477 GrGLint srcY1;
2478 // Does the blit need to y-mirror or not?
2479 if (src->origin() == dst->origin()) {
2480 srcY0 = srcGLRect.fBottom;
2481 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2482 } else {
2483 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2484 srcY1 = srcGLRect.fBottom;
2485 }
2486 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2487 srcY0,
2488 srcGLRect.fLeft + srcGLRect.fWidth,
2489 srcY1,
2490 dstGLRect.fLeft,
2491 dstGLRect.fBottom,
2492 dstGLRect.fLeft + dstGLRect.fWidth,
2493 dstGLRect.fBottom + dstGLRect.fHeight,
2494 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2495 if (dstFBO) {
2496 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2497 }
2498 if (srcFBO) {
2499 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2500 }
2501 copied = true;
2502 }
2503 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002504 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002505 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002506 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002507 }
2508 return copied;
2509}
2510
2511bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2512 GrSurface* src,
2513 const SkIRect& srcRect,
2514 const SkIPoint& dstPoint) {
2515 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002516 if (can_copy_texsubimage(dst, src, this)) {
2517 return true;
2518 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002519 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002520 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002521 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2522 srcRect.width(), srcRect.height());
2523 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2524 return true;
2525 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002526 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002527 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002528 }
2529 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002530 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002531}
2532
2533
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002534///////////////////////////////////////////////////////////////////////////////
2535
bsalomon@google.com6918d482013-03-07 19:09:11 +00002536GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2537 GrGpuGL* gpu,
2538 const GrGLVertexBuffer* vbuffer,
2539 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002540 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002541 GrGLAttribArrayState* attribState;
2542
bsalomon@google.com6918d482013-03-07 19:09:11 +00002543 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2544 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2545 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2546 SkSafeUnref(fVBOVertexArray);
2547 GrGLuint arrayID;
2548 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2549 int attrCount = gpu->glCaps().maxVertexAttributes();
2550 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002551 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002552 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2553 } else {
2554 if (NULL != ibuffer) {
2555 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2556 } else {
2557 this->setVertexArrayID(gpu, 0);
2558 }
2559 int attrCount = gpu->glCaps().maxVertexAttributes();
2560 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2561 fDefaultVertexArrayAttribState.resize(attrCount);
2562 }
2563 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002564 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002565 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002566}