blob: 0aa072cd8463c8972090bba2c2dcb15133434096 [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"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000015#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000016#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000019#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021#define SKIP_CACHE_CHECK true
22
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000023#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
24 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
25 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
26 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000028 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
31#endif
32
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000033
34///////////////////////////////////////////////////////////////////////////////
35
twiz@google.com0f31ca72011-03-18 17:38:11 +000036static const GrGLenum gXfermodeCoeff2Blend[] = {
37 GR_GL_ZERO,
38 GR_GL_ONE,
39 GR_GL_SRC_COLOR,
40 GR_GL_ONE_MINUS_SRC_COLOR,
41 GR_GL_DST_COLOR,
42 GR_GL_ONE_MINUS_DST_COLOR,
43 GR_GL_SRC_ALPHA,
44 GR_GL_ONE_MINUS_SRC_ALPHA,
45 GR_GL_DST_ALPHA,
46 GR_GL_ONE_MINUS_DST_ALPHA,
47 GR_GL_CONSTANT_COLOR,
48 GR_GL_ONE_MINUS_CONSTANT_COLOR,
49 GR_GL_CONSTANT_ALPHA,
50 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000051
52 // extended blend coeffs
53 GR_GL_SRC1_COLOR,
54 GR_GL_ONE_MINUS_SRC1_COLOR,
55 GR_GL_SRC1_ALPHA,
56 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000057};
58
bsalomon@google.com271cffc2011-05-20 14:13:56 +000059bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000060 static const bool gCoeffReferencesBlendConst[] = {
61 false,
62 false,
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 true,
72 true,
73 true,
74 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000075
76 // extended blend coeffs
77 false,
78 false,
79 false,
80 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000081 };
82 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000083 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
84 GR_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000085
bsalomon@google.com47059542012-06-06 20:51:20 +000086 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
87 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
88 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
89 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
90 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
91 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
92 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
93 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
94 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
95 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
96 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
97 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
98 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
99 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000100
bsalomon@google.com47059542012-06-06 20:51:20 +0000101 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
102 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
103 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
104 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
106 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000107 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
108 GR_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000109}
110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111///////////////////////////////////////////////////////////////////////////////
112
rileya@google.come38160c2012-07-03 18:03:04 +0000113static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000114
robertphillips@google.com6177e692013-02-28 20:16:25 +0000115GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000116 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000117 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000118
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000119 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000120 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000121
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000122 fHWBoundTextures.reset(this->glCaps().maxFragmentTextureUnits());
123 fHWTexGenSettings.reset(this->glCaps().maxFixedFunctionTextureCoords());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000124
robertphillips@google.com6177e692013-02-28 20:16:25 +0000125 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000126 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000127 const GrGLubyte* vendor;
128 const GrGLubyte* renderer;
129 const GrGLubyte* version;
130 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
131 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
132 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000133 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
134 this);
135 GrPrintf("------ VENDOR %s\n", vendor);
136 GrPrintf("------ RENDERER %s\n", renderer);
137 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000138 GrPrintf("------ EXTENSIONS\n");
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +0000139#if 0 // TODO: Reenable this after GrGLInterface's extensions can be accessed safely.
140 ctx.extensions().print();
141#endif
bsalomon@google.com00142c42013-05-02 19:42:54 +0000142 GrPrintf("\n");
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000143 GrPrintf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000144 }
145
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000146 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000147
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000148 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000149
bsalomon@google.comfe676522011-06-17 18:12:21 +0000150 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000151 fHWProgramID = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000152}
153
154GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000155 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000156 // detach the current program so there is no confusion on OpenGL's part
157 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000158 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000159 GL_CALL(UseProgram(0));
160 }
161
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000162 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000163
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000164 // This must be called by before the GrDrawTarget destructor
165 this->releaseGeometry();
bsalomon@google.com15b11df2011-09-16 21:18:29 +0000166 // This subclass must do this before the base class destructor runs
167 // since we will unref the GrGLInterface.
168 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +0000169}
170
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000171///////////////////////////////////////////////////////////////////////////////
172
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000173
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000174GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
175 GrPixelConfig surfaceConfig) const {
176 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000177 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000178 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000179 GrBytesPerPixel(readConfig) == 4 &&
180 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000181 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
182 // Perhaps this should be guarded by some compiletime or runtime check.
183 return surfaceConfig;
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000184 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
185 !this->glCaps().readPixelsSupported(this->glInterface(),
186 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
187 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000188 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000189 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000190 }
191}
192
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000193GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
194 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000195 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
196 return kBGRA_8888_GrPixelConfig;
197 } else {
198 return writeConfig;
199 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000200}
201
202bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
203 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
204 return false;
205 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000206 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000207 // In general ES2 requires the internal format of the texture and the format of the src
208 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
209 // texture. It depends upon which extension added BGRA. The Apple extension allows it
210 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
211 // internal format).
212 if (this->glCaps().bgraFormatSupport() &&
213 !this->glCaps().bgraIsInternalFormat() &&
214 kBGRA_8888_GrPixelConfig == srcConfig &&
215 kRGBA_8888_GrPixelConfig == texture->config()) {
216 return true;
217 } else {
218 return false;
219 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000220 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000221 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000222 }
223}
224
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000225bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
226 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
227}
228
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000229void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000230 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000231 if (resetBits & kMisc_GrGLBackendState) {
232 GL_CALL(Disable(GR_GL_DEPTH_TEST));
233 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000234
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000235 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
236 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000238 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000239 // Desktop-only state that we never change
240 if (!this->glCaps().isCoreProfile()) {
241 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
242 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
243 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
244 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
245 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
246 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
247 }
248 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
249 // core profile. This seems like a bug since the core spec removes any mention of
250 // GL_ARB_imaging.
251 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
252 GL_CALL(Disable(GR_GL_COLOR_TABLE));
253 }
254 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
255 // Since ES doesn't support glPointSize at all we always use the VS to
256 // set the point size
257 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
258
259 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
260 // currently part of our gl interface. There are probably others as
261 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000262 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000263 fHWWriteToColor = kUnknown_TriState;
264 // we only ever use lines in hairline mode
265 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000266 }
edisonn@google.comba669992013-06-28 16:03:21 +0000267
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000268 if (resetBits & kAA_GrGLBackendState) {
269 fHWAAState.invalidate();
270 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000272 fHWActiveTextureUnitIdx = -1; // invalid
273
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000274 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000275 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
276 fHWBoundTextures[s] = NULL;
277 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000278 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000279
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000280 if (resetBits & kBlend_GrGLBackendState) {
281 fHWBlendState.invalidate();
282 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000283
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000284 if (resetBits & kView_GrGLBackendState) {
285 fHWScissorSettings.invalidate();
286 fHWViewport.invalidate();
287 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000288
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000289 if (resetBits & kStencil_GrGLBackendState) {
290 fHWStencilSettings.invalidate();
291 fHWStencilTestEnabled = kUnknown_TriState;
292 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000293
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000294 // Vertex
295 if (resetBits & kVertex_GrGLBackendState) {
296 fHWGeometryState.invalidate();
297 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000298
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000299 if (resetBits & kRenderTarget_GrGLBackendState) {
300 fHWBoundRenderTarget = NULL;
301 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000302
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000303 if (resetBits & (kFixedFunction_GrGLBackendState | kPathRendering_GrGLBackendState)) {
304 if (this->glCaps().fixedFunctionSupport()) {
305 fHWProjectionMatrixState.invalidate();
306 // we don't use the model view matrix.
307 GL_CALL(MatrixMode(GR_GL_MODELVIEW));
308 GL_CALL(LoadIdentity());
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000309
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000310 for (int i = 0; i < this->glCaps().maxFixedFunctionTextureCoords(); ++i) {
311 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + i));
312 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S));
313 GL_CALL(Disable(GR_GL_TEXTURE_GEN_T));
314 GL_CALL(Disable(GR_GL_TEXTURE_GEN_Q));
315 GL_CALL(Disable(GR_GL_TEXTURE_GEN_R));
316 if (this->caps()->pathRenderingSupport()) {
317 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
318 }
319 fHWTexGenSettings[i].fMode = GR_GL_NONE;
320 fHWTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000321 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000322 fHWActiveTexGenSets = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000324 if (this->caps()->pathRenderingSupport()) {
325 fHWPathStencilSettings.invalidate();
326 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000327 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000328
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000329 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000330 if (resetBits & kPixelStore_GrGLBackendState) {
331 if (this->glCaps().unpackRowLengthSupport()) {
332 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
333 }
334 if (this->glCaps().packRowLengthSupport()) {
335 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
336 }
337 if (this->glCaps().unpackFlipYSupport()) {
338 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
339 }
340 if (this->glCaps().packFlipYSupport()) {
341 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
342 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000343 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000344
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000345 if (resetBits & kProgram_GrGLBackendState) {
346 fHWProgramID = 0;
347 fSharedGLProgramState.invalidate();
348 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000351namespace {
352
353GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
354 // By default, GrRenderTargets are GL's normal orientation so that they
355 // can be drawn to by the outside world without the client having
356 // to render upside down.
357 if (kDefault_GrSurfaceOrigin == origin) {
358 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
359 } else {
360 return origin;
361 }
362}
363
364}
365
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000366GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000367 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000368 return NULL;
369 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000370
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000371 if (0 == desc.fTextureHandle) {
372 return NULL;
373 }
374
bsalomon@google.combcce8922013-03-25 15:38:39 +0000375 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000376 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
377 return NULL;
378 }
379
380 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000381 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000382 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000383 glTexDesc.fWidth = desc.fWidth;
384 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000385 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000386 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000387 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000388 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000389 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000390 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
391 // assuming the old behaviour, which is that backend textures are always
392 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
393 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
394 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
395 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
396 } else {
397 glTexDesc.fOrigin = desc.fOrigin;
398 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000399
400 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000401 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000402 GrGLRenderTarget::Desc glRTDesc;
403 glRTDesc.fRTFBOID = 0;
404 glRTDesc.fTexFBOID = 0;
405 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000406 glRTDesc.fConfig = desc.fConfig;
407 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000408 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000409 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000410 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
411 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000412 glTexDesc.fTextureID,
413 &glRTDesc)) {
414 return NULL;
415 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000416 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000417 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000418 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000419 }
420 if (NULL == texture) {
421 return NULL;
422 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000423
bsalomon@google.come269f212011-11-07 13:29:52 +0000424 return texture;
425}
426
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000427GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000428 GrGLRenderTarget::Desc glDesc;
429 glDesc.fConfig = desc.fConfig;
430 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
431 glDesc.fMSColorRenderbufferID = 0;
432 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
433 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000434 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000435 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000436
437 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000438 GrGLIRect viewport;
439 viewport.fLeft = 0;
440 viewport.fBottom = 0;
441 viewport.fWidth = desc.fWidth;
442 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000443
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000444 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
445 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000446 if (desc.fStencilBits) {
447 GrGLStencilBuffer::Format format;
448 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
449 format.fPacked = false;
450 format.fStencilBits = desc.fStencilBits;
451 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000452 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000453 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
454 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000455 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000456 0,
457 desc.fWidth,
458 desc.fHeight,
459 desc.fSampleCnt,
460 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000461 tgt->setStencilBuffer(sb);
462 sb->unref();
463 }
464 return tgt;
465}
466
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000467////////////////////////////////////////////////////////////////////////////////
468
bsalomon@google.com9c680582013-02-06 18:17:50 +0000469bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000470 int left, int top, int width, int height,
471 GrPixelConfig config, const void* buffer,
472 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000473 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000474 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000475 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000476 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
477
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000478 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000479 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
480 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000481 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000482 desc.fWidth = glTex->width();
483 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000484 desc.fConfig = glTex->config();
485 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000486 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000487 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000488
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000489 if (this->uploadTexData(desc, false,
490 left, top, width, height,
491 config, buffer, rowBytes)) {
492 texture->dirtyMipMaps(true);
493 return true;
494 } else {
495 return false;
496 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000497}
498
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000499namespace {
500bool adjust_pixel_ops_params(int surfaceWidth,
501 int surfaceHeight,
502 size_t bpp,
503 int* left, int* top, int* width, int* height,
504 const void** data,
505 size_t* rowBytes) {
506 if (!*rowBytes) {
507 *rowBytes = *width * bpp;
508 }
509
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000510 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
511 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000512
513 if (!subRect.intersect(bounds)) {
514 return false;
515 }
516 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
517 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
518
519 *left = subRect.fLeft;
520 *top = subRect.fTop;
521 *width = subRect.width();
522 *height = subRect.height();
523 return true;
524}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000525
526GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
527 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
528 return GR_GL_GET_ERROR(interface);
529 } else {
530 return CHECK_ALLOC_ERROR(interface);
531 }
532}
533
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000534}
535
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000536bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
537 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000538 int left, int top, int width, int height,
539 GrPixelConfig dataConfig,
540 const void* data,
541 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000542 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000543
544 size_t bpp = GrBytesPerPixel(dataConfig);
545 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
546 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000547 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000548 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000549 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000550
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000551 // in case we need a temporary, trimmed copy of the src pixels
552 SkAutoSMalloc<128 * 128> tempStorage;
553
bsalomon@google.com313f0192012-07-10 17:21:02 +0000554 // paletted textures cannot be partially updated
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000555 // We currently lazily create MIPMAPs when the we see a draw with
556 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
557 // MIP levels are all created when the texture is created. So for now we don't use
558 // texture storage.
559 bool useTexStorage = false &&
560 isNewTexture &&
bsalomon@google.com313f0192012-07-10 17:21:02 +0000561 desc.fConfig != kIndex_8_GrPixelConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000562 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000563
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000564 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000565 // 565 is not a sized internal format on desktop GL. So on desktop with
566 // 565 we always use an unsized internal format to let the system pick
567 // the best sized format to convert the 565 data to. Since TexStorage
568 // only allows sized internal formats we will instead use TexImage2D.
569 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000570 }
571
572 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000573 GrGLenum externalFormat;
574 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000575 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
576 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
577 // size of the internal format whenever possible and so only use a sized internal format when
578 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000579 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000580 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000581 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000582 }
583
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000584 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000585 // paletted textures cannot be updated
586 return false;
587 }
588
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000589 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000590 * check whether to allocate a temporary buffer for flipping y or
591 * because our srcData has extra bytes past each row. If so, we need
592 * to trim those off here, since GL ES may not let us specify
593 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000594 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000595 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000596 bool swFlipY = false;
597 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000598 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000599 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000600 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000601 glFlipY = true;
602 } else {
603 swFlipY = true;
604 }
605 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000606 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000607 // can't use this for flipping, only non-neg values allowed. :(
608 if (rowBytes != trimRowBytes) {
609 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
610 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
611 restoreGLRowLength = true;
612 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000613 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000614 if (trimRowBytes != rowBytes || swFlipY) {
615 // copy data into our new storage, skipping the trailing bytes
616 size_t trimSize = height * trimRowBytes;
617 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000618 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000619 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000620 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000621 char* dst = (char*)tempStorage.reset(trimSize);
622 for (int y = 0; y < height; y++) {
623 memcpy(dst, src, trimRowBytes);
624 if (swFlipY) {
625 src -= rowBytes;
626 } else {
627 src += rowBytes;
628 }
629 dst += trimRowBytes;
630 }
631 // now point data to our copied version
632 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000633 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000634 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000635 if (glFlipY) {
636 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
637 }
638 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000639 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000640 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000641 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000642 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000643 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000644 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000645 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000646 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000647 GL_ALLOC_CALL(this->glInterface(),
648 TexStorage2D(GR_GL_TEXTURE_2D,
649 1, // levels
650 internalFormat,
651 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000652 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000653 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
654 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
655 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000656 GL_ALLOC_CALL(this->glInterface(),
657 CompressedTexImage2D(GR_GL_TEXTURE_2D,
658 0, // level
659 internalFormat,
660 desc.fWidth, desc.fHeight,
661 0, // border
662 imageSize,
663 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000664 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000665 GL_ALLOC_CALL(this->glInterface(),
666 TexImage2D(GR_GL_TEXTURE_2D,
667 0, // level
668 internalFormat,
669 desc.fWidth, desc.fHeight,
670 0, // border
671 externalFormat, externalType,
672 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000673 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000674 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000675 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000676 if (error != GR_GL_NO_ERROR) {
677 succeeded = false;
678 } else {
679 // if we have data and we used TexStorage to create the texture, we
680 // now upload with TexSubImage.
681 if (NULL != data && useTexStorage) {
682 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
683 0, // level
684 left, top,
685 width, height,
686 externalFormat, externalType,
687 data));
688 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000689 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000690 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000691 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000693 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000694 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
695 0, // level
696 left, top,
697 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000698 externalFormat, externalType, data));
699 }
700
701 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000702 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000703 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000704 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000705 if (glFlipY) {
706 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
707 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000708 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000709}
710
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000711static bool renderbuffer_storage_msaa(GrGLContext& ctx,
712 int sampleCount,
713 GrGLenum format,
714 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000715 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000716 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000717 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000718 case GrGLCaps::kDesktop_ARB_MSFBOType:
719 case GrGLCaps::kDesktop_EXT_MSFBOType:
720 case GrGLCaps::kES_3_0_MSFBOType:
721 GL_ALLOC_CALL(ctx.interface(),
722 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
723 sampleCount,
724 format,
725 width, height));
726 break;
727 case GrGLCaps::kES_Apple_MSFBOType:
728 GL_ALLOC_CALL(ctx.interface(),
729 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
730 sampleCount,
731 format,
732 width, height));
733 break;
734 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
735 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
736 GL_ALLOC_CALL(ctx.interface(),
737 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
738 sampleCount,
739 format,
740 width, height));
741 break;
742 case GrGLCaps::kNone_MSFBOType:
743 GrCrash("Shouldn't be here if we don't support multisampled renderbuffers.");
744 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000745 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000746 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000747}
748
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000749bool GrGpuGL::createRenderTargetObjects(int width, int height,
750 GrGLuint texID,
751 GrGLRenderTarget::Desc* desc) {
752 desc->fMSColorRenderbufferID = 0;
753 desc->fRTFBOID = 0;
754 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000755 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000756
757 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000758
bsalomon@google.comab15d612011-08-09 12:57:56 +0000759 GrGLenum msColorFormat = 0; // suppress warning
760
bsalomon@google.com347c3822013-05-01 20:10:01 +0000761 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
762 goto FAILED;
763 }
764
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000765 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000766 if (!desc->fTexFBOID) {
767 goto FAILED;
768 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000769
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000770
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000771 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
772 // the texture bound to the other. The exception is the IMG multisample extension. With this
773 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
774 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000775 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000776 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
777 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000778 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000779 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000780 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000781 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000782 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000783 &msColorFormat,
784 NULL,
785 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000786 goto FAILED;
787 }
788 } else {
789 desc->fRTFBOID = desc->fTexFBOID;
790 }
791
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000792 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000793 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000794 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000795 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000796 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000797 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000798 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000799 desc->fSampleCnt,
800 msColorFormat,
801 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000802 goto FAILED;
803 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000804 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000805 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000806 GR_GL_COLOR_ATTACHMENT0,
807 GR_GL_RENDERBUFFER,
808 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000809 if (desc->fCheckAllocation ||
810 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000811 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
812 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
813 goto FAILED;
814 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000815 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000816 }
817 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000818 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000819
bsalomon@google.com347c3822013-05-01 20:10:01 +0000820 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000821 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
822 GR_GL_COLOR_ATTACHMENT0,
823 GR_GL_TEXTURE_2D,
824 texID, 0, desc->fSampleCnt));
825 } else {
826 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
827 GR_GL_COLOR_ATTACHMENT0,
828 GR_GL_TEXTURE_2D,
829 texID, 0));
830 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000831 if (desc->fCheckAllocation ||
832 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000833 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
834 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
835 goto FAILED;
836 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000837 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000838 }
839
840 return true;
841
842FAILED:
843 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000844 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000845 }
846 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000847 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000848 }
849 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000850 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000851 }
852 return false;
853}
854
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000855// good to set a break-point here to know when createTexture fails
856static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000857// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000858 return NULL;
859}
860
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000861#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000862static size_t as_size_t(int x) {
863 return x;
864}
865#endif
866
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000867GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000868 const void* srcData,
869 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000870
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000871 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000872 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000873
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000874 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000875 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000876 // We fail if the MSAA was requested and is not available.
877 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
878 //GrPrintf("MSAA RT requested but not supported on this platform.");
879 return return_null_texture();
880 }
881 // If the sample count exceeds the max then we clamp it.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000882 glTexDesc.fSampleCnt = GrMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000883
robertphillips@google.com32716282012-06-04 12:48:45 +0000884 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000885 glTexDesc.fWidth = desc.fWidth;
886 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000887 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000888 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000889
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000890 glRTDesc.fMSColorRenderbufferID = 0;
891 glRTDesc.fRTFBOID = 0;
892 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000893 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000894 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000895 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000896
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000897 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000898
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000899 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
900 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000901
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000902 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000903 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000904 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000905 //GrPrintf("MSAA RT requested but not supported on this platform.");
906 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000907 }
908
reed@google.comac10a2d2010-12-22 21:39:39 +0000909 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000910 int maxRTSize = this->caps()->maxRenderTargetSize();
911 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000912 return return_null_texture();
913 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000914 } else {
915 int maxSize = this->caps()->maxTextureSize();
916 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
917 return return_null_texture();
918 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000919 }
920
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000922
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000923 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000924 return return_null_texture();
925 }
926
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000927 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000928 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000929
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000930 if (renderTarget && this->glCaps().textureUsageSupport()) {
931 // provides a hint about how this texture will be used
932 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
933 GR_GL_TEXTURE_USAGE,
934 GR_GL_FRAMEBUFFER_ATTACHMENT));
935 }
936
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000937 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
938 // drivers have a bug where an FBO won't be complete if it includes a
939 // texture that is not mipmap complete (considering the filter in use).
940 GrGLTexture::TexParams initialTexParams;
941 // we only set a subset here so invalidate first
942 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000943 initialTexParams.fMinFilter = GR_GL_NEAREST;
944 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000945 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
946 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000947 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
948 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000949 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
951 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000952 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000953 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
954 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000955 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000956 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
957 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000958 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000959 if (!this->uploadTexData(glTexDesc, true, 0, 0,
960 glTexDesc.fWidth, glTexDesc.fHeight,
961 desc.fConfig, srcData, rowBytes)) {
962 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
963 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000964 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000965
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000966 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000967 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000968 // unbind the texture from the texture unit before binding it to the frame buffer
969 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
970
bsalomon@google.com99621082011-11-15 16:47:16 +0000971 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
972 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000973 glTexDesc.fTextureID,
974 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000975 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 return return_null_texture();
977 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000978 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000979 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000980 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +0000981 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000982 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +0000983#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000984 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
985 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +0000986#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987 return tex;
988}
989
990namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000991
992const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
993
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000995 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000996
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000997 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000998 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000999 (kUnknownBitCount == format->fTotalBits));
1000 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1003 (GrGLint*)&format->fStencilBits);
1004 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001005 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001006 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1007 (GrGLint*)&format->fTotalBits);
1008 format->fTotalBits += format->fStencilBits;
1009 } else {
1010 format->fTotalBits = format->fStencilBits;
1011 }
1012 }
1013}
1014}
1015
1016bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1017 int width, int height) {
1018
1019 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001020 // 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 +00001021 SkASSERT(rt->asTexture());
1022 SkASSERT(width >= rt->width());
1023 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001024
1025 int samples = rt->numSamples();
1026 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001028 if (!sbID) {
1029 return false;
1030 }
1031
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001032 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001033 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001034 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001035 // we start with the last stencil format that succeeded in hopes
1036 // that we won't go through this loop more than once after the
1037 // first (painful) stencil creation.
1038 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001039 const GrGLCaps::StencilFormat& sFmt =
1040 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001041 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001042 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001043 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001044 bool created;
1045 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001046 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001047 samples,
1048 sFmt.fInternalFormat,
1049 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001050 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001051 GL_ALLOC_CALL(this->glInterface(),
1052 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001053 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001054 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001055 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001056 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001057 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001058 if (created) {
1059 // After sized formats we attempt an unsized format and take
1060 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001061 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001062 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001063 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001064 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001065 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001066 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001067 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1068 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001069 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001070 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 return true;
1072 }
1073 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 }
1075 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001077 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001078}
1079
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001080bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001081 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1082
1083 GrGLuint fbo = glrt->renderFBOID();
1084
1085 if (NULL == sb) {
1086 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001087 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001088 GR_GL_STENCIL_ATTACHMENT,
1089 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001090 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001091 GR_GL_DEPTH_ATTACHMENT,
1092 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001093#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001094 GrGLenum status;
1095 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001096 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001097#endif
1098 }
1099 return true;
1100 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001101 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 GrGLuint rb = glsb->renderbufferID();
1103
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001104 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001105 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1106 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001107 GR_GL_STENCIL_ATTACHMENT,
1108 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001110 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001111 GR_GL_DEPTH_ATTACHMENT,
1112 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001115 GR_GL_DEPTH_ATTACHMENT,
1116 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001117 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001118
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001119 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001120 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001121 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1122 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001124 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001125 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001126 if (glsb->format().fPacked) {
1127 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1128 GR_GL_DEPTH_ATTACHMENT,
1129 GR_GL_RENDERBUFFER, 0));
1130 }
1131 return false;
1132 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001133 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001134 rt->config(),
1135 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001138 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001140}
1141
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001142////////////////////////////////////////////////////////////////////////////////
1143
robertphillips@google.comadacc702013-10-14 21:53:24 +00001144GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001145 GrGLVertexBuffer::Desc desc;
1146 desc.fDynamic = dynamic;
1147 desc.fSizeInBytes = size;
1148 desc.fIsWrapped = false;
1149
bsalomon@google.com96966a52013-02-21 16:34:21 +00001150 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001151 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001152 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001154 } else {
1155 GL_CALL(GenBuffers(1, &desc.fID));
1156 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001157 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001158 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1159 // make sure driver can allocate memory for this buffer
1160 GL_ALLOC_CALL(this->glInterface(),
1161 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001162 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001163 NULL, // data ptr
1164 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1165 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1166 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001167 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001168 return NULL;
1169 }
1170 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1171 return vertexBuffer;
1172 }
1173 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001174 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001175}
1176
robertphillips@google.comadacc702013-10-14 21:53:24 +00001177GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001178 GrGLIndexBuffer::Desc desc;
1179 desc.fDynamic = dynamic;
1180 desc.fSizeInBytes = size;
1181 desc.fIsWrapped = false;
1182
bsalomon@google.com96966a52013-02-21 16:34:21 +00001183 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001184 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001185 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001186 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001187 } else {
1188 GL_CALL(GenBuffers(1, &desc.fID));
1189 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001190 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001191 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1192 // make sure driver can allocate memory for this buffer
1193 GL_ALLOC_CALL(this->glInterface(),
1194 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001195 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001196 NULL, // data ptr
1197 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1198 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1199 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001200 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001201 return NULL;
1202 }
1203 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1204 return indexBuffer;
1205 }
1206 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001208}
1209
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001210GrPath* GrGpuGL::onCreatePath(const SkPath& inPath, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001211 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001212 return SkNEW_ARGS(GrGLPath, (this, inPath, stroke));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001213}
1214
bsalomon@google.coma3201942012-06-21 19:58:20 +00001215void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001216 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001217 // Only access the RT if scissoring is being enabled. We can call this before performing
1218 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1219 // GrDrawState.
1220 const GrDrawState& drawState = this->getDrawState();
1221 const GrGLRenderTarget* rt =
1222 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1223
1224 SkASSERT(NULL != rt);
1225 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001226 GrGLIRect scissor;
1227 scissor.setRelativeTo(vp,
1228 fScissorState.fRect.fLeft,
1229 fScissorState.fRect.fTop,
1230 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001231 fScissorState.fRect.height(),
1232 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001233 // if the scissor fully contains the viewport then we fall through and
1234 // disable the scissor test.
1235 if (!scissor.contains(vp)) {
1236 if (fHWScissorSettings.fRect != scissor) {
1237 scissor.pushToGLScissor(this->glInterface());
1238 fHWScissorSettings.fRect = scissor;
1239 }
1240 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1241 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1242 fHWScissorSettings.fEnabled = kYes_TriState;
1243 }
1244 return;
1245 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001247 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001248 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001249 fHWScissorSettings.fEnabled = kNo_TriState;
1250 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 }
1252}
1253
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001254void GrGpuGL::onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001255 const GrDrawState& drawState = this->getDrawState();
1256 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001257 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001258 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001259
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001260 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1261 rect = NULL;
1262 }
1263
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001264 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001265 if (NULL != rect) {
1266 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001267 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001268 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001269 if (clippedRect.intersect(rtRect)) {
1270 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001271 } else {
1272 return;
1273 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001275
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001276 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001277 GrAutoTRestore<ScissorState> asr(&fScissorState);
1278 fScissorState.fEnabled = (NULL != rect);
1279 if (fScissorState.fEnabled) {
1280 fScissorState.fRect = *rect;
1281 }
1282 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001283
1284 GrGLfloat r, g, b, a;
1285 static const GrGLfloat scale255 = 1.f / 255.f;
1286 a = GrColorUnpackA(color) * scale255;
1287 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001288 r = GrColorUnpackR(color) * scaleRGB;
1289 g = GrColorUnpackG(color) * scaleRGB;
1290 b = GrColorUnpackB(color) * scaleRGB;
1291
1292 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001293 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001294 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001295 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296}
1297
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001298void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001299 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001300 return;
1301 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001302
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001303 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001304
bsalomon@google.coma3201942012-06-21 19:58:20 +00001305 GrAutoTRestore<ScissorState> asr(&fScissorState);
1306 fScissorState.fEnabled = false;
1307 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001308
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001309 GL_CALL(StencilMask(0xffffffff));
1310 GL_CALL(ClearStencil(0));
1311 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001312 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001313}
1314
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001315void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001316 const GrDrawState& drawState = this->getDrawState();
1317 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001318 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001319
1320 // this should only be called internally when we know we have a
1321 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001322 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001323 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001324#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001325 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001326 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001327#else
1328 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001329 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001330 // turned into draws. Our contract on GrDrawTarget says that
1331 // changing the clip between stencil passes may or may not
1332 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001333 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001334#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001335 GrGLint value;
1336 if (insideClip) {
1337 value = (1 << (stencilBitCount - 1));
1338 } else {
1339 value = 0;
1340 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001341 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001342
1343 GrAutoTRestore<ScissorState> asr(&fScissorState);
1344 fScissorState.fEnabled = true;
1345 fScissorState.fRect = rect;
1346 this->flushScissor();
1347
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001348 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001349 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001350 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001351 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001352}
1353
bsalomon@google.combcdbbe62011-04-12 15:40:00 +00001354void GrGpuGL::onForceRenderTargetFlush() {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001355 this->flushRenderTarget(&SkIRect::EmptyIRect());
reed@google.comac10a2d2010-12-22 21:39:39 +00001356}
1357
bsalomon@google.comc4364992011-11-07 15:54:49 +00001358bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1359 int left, int top,
1360 int width, int height,
1361 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001362 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001363 // If this rendertarget is aready TopLeft, we don't need to flip.
1364 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1365 return false;
1366 }
1367
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001368 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001369 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001370 return false;
1371 }
1372
1373 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001374 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001375 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001376 return true;
1377 }
1378 // If we have to do memcpys to handle rowBytes then y-flip is free
1379 // Note the rowBytes might be tight to the passed in data, but if data
1380 // gets clipped in x to the target the rowBytes will no longer be tight.
1381 if (left >= 0 && (left + width) < renderTarget->width()) {
1382 return 0 == rowBytes ||
1383 GrBytesPerPixel(config) * width == rowBytes;
1384 } else {
1385 return false;
1386 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001387}
1388
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001389bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001390 int left, int top,
1391 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392 GrPixelConfig config,
1393 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001394 size_t rowBytes) {
twiz@google.com0f31ca72011-03-18 17:38:11 +00001395 GrGLenum format;
1396 GrGLenum type;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001397 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001398 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001399 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001400 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001401 size_t bpp = GrBytesPerPixel(config);
1402 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1403 &left, &top, &width, &height,
1404 const_cast<const void**>(&buffer),
1405 &rowBytes)) {
1406 return false;
1407 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001408
bsalomon@google.comc6980972011-11-02 19:57:21 +00001409 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001410 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001411 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001412 switch (tgt->getResolveType()) {
1413 case GrGLRenderTarget::kCantResolve_ResolveType:
1414 return false;
1415 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001417 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001418 break;
1419 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001420 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001421 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001422 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1423 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001424 break;
1425 default:
1426 GrCrash("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 }
1428
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001429 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001430
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001431 // the read rect is viewport-relative
1432 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001433 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001434
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001435 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001436 if (0 == rowBytes) {
1437 rowBytes = tightRowBytes;
1438 }
1439 size_t readDstRowBytes = tightRowBytes;
1440 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001441
bsalomon@google.comc6980972011-11-02 19:57:21 +00001442 // determine if GL can read using the passed rowBytes or if we need
1443 // a scratch buffer.
1444 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1445 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001446 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001447 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001448 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001449 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001450 readDstRowBytes = rowBytes;
1451 } else {
1452 scratch.reset(tightRowBytes * height);
1453 readDst = scratch.get();
1454 }
1455 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001456 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001457 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1458 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001459 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1460 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001461 format, type, readDst));
1462 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001463 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001464 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1465 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001466 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001467 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001468 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001469 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001470
1471 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001472 // API presents top-to-bottom. We must preserve the padding contents. Note
1473 // that the above readPixels did not overwrite the padding.
1474 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001475 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001476 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001477 scratch.reset(tightRowBytes);
1478 void* tmpRow = scratch.get();
1479 // flip y in-place by rows
1480 const int halfY = height >> 1;
1481 char* top = reinterpret_cast<char*>(buffer);
1482 char* bottom = top + (height - 1) * rowBytes;
1483 for (int y = 0; y < halfY; y++) {
1484 memcpy(tmpRow, top, tightRowBytes);
1485 memcpy(top, bottom, tightRowBytes);
1486 memcpy(bottom, tmpRow, tightRowBytes);
1487 top += rowBytes;
1488 bottom -= rowBytes;
1489 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001490 }
1491 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001492 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001493 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001494 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001495 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001496 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001497 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001498 dst += (height-1) * rowBytes;
1499 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001500 for (int y = 0; y < height; y++) {
1501 memcpy(dst, src, tightRowBytes);
1502 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001503 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001504 dst += rowBytes;
1505 } else {
1506 dst -= rowBytes;
1507 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 }
1509 }
1510 return true;
1511}
1512
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001513void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001514
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001515 GrGLRenderTarget* rt =
1516 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001517 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001518
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001519 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001520 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001521#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001522 // don't do this check in Chromium -- this is causing
1523 // lots of repeated command buffer flushes when the compositor is
1524 // rendering with Ganesh, which is really slow; even too slow for
1525 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001526 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001527 GrGLenum status;
1528 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1529 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1530 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1531 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001532 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001533#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001534 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001535 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001536 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001537 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001538 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001539 }
1540 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001541 if (NULL == bound || !bound->isEmpty()) {
1542 rt->flagAsNeedingResolve(bound);
1543 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001544
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001545 GrTexture *texture = rt->asTexture();
1546 if (texture) {
1547 texture->dirtyMipMaps(true);
1548 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001549}
1550
twiz@google.com0f31ca72011-03-18 17:38:11 +00001551GrGLenum gPrimitiveType2GLMode[] = {
1552 GR_GL_TRIANGLES,
1553 GR_GL_TRIANGLE_STRIP,
1554 GR_GL_TRIANGLE_FAN,
1555 GR_GL_POINTS,
1556 GR_GL_LINES,
1557 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001558};
1559
bsalomon@google.comd302f142011-03-03 13:54:13 +00001560#define SWAP_PER_DRAW 0
1561
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001562#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001563 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001564 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001565 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001566 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001567 void SwapBuf() {
1568 DWORD procID = GetCurrentProcessId();
1569 HWND hwnd = GetTopWindow(GetDesktopWindow());
1570 while(hwnd) {
1571 DWORD wndProcID = 0;
1572 GetWindowThreadProcessId(hwnd, &wndProcID);
1573 if(wndProcID == procID) {
1574 SwapBuffers(GetDC(hwnd));
1575 }
1576 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1577 }
1578 }
1579 #endif
1580#endif
1581
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001582void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001583 size_t indexOffsetInBytes;
1584 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001585
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001586 SkASSERT((size_t)info.primitiveType() < GR_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001587
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001588 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001589 GrGLvoid* indices =
1590 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001591 // info.startVertex() was accounted for by setupGeometry.
1592 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1593 info.indexCount(),
1594 GR_GL_UNSIGNED_SHORT,
1595 indices));
1596 } else {
1597 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1598 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1599 // accounted for startVertex.
1600 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1601 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001602#if SWAP_PER_DRAW
1603 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001604 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001605 aglSwapBuffers(aglGetCurrentContext());
1606 int set_a_break_pt_here = 9;
1607 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001608 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001609 SwapBuf();
1610 int set_a_break_pt_here = 9;
1611 SwapBuf();
1612 #endif
1613#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001614}
1615
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001616static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
1617 switch (op) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001618 default:
1619 GrCrash("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001620 /* fallthrough */;
1621 case kIncClamp_StencilOp:
1622 return GR_GL_COUNT_UP;
1623 case kInvert_StencilOp:
1624 return GR_GL_INVERT;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001625 }
1626}
1627
sugoi@google.com12b4e272012-12-06 20:13:11 +00001628void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001629 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001630
1631 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001632 SkASSERT(NULL != this->drawState()->getRenderTarget());
1633 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1634
1635 flushPathStencilSettings(fill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001636
1637 // Decide how to manipulate the stencil buffer based on the fill rule.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001638 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1639
1640 GrGLenum fillMode =
1641 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1642 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001643 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001644}
1645
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001646void GrGpuGL::onGpuDrawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001647 SkASSERT(this->caps()->pathRenderingSupport());
1648
1649 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1650 SkASSERT(NULL != this->drawState()->getRenderTarget());
1651 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1652 SkASSERT(!fCurrentProgram->hasVertexShader());
1653
1654 flushPathStencilSettings(fill);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001655 const SkStrokeRec& stroke = path->getStroke();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001656
1657 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill);
1658 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1659 GrGLenum fillMode =
1660 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1661 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001662
1663 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
1664 GL_CALL(StencilFillPath(id, fillMode, writeMask));
1665 }
1666 if (stroke.needToApply()) {
1667 GL_CALL(StencilStrokePath(id, 0xffff, writeMask));
1668 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001669
1670 if (nonInvertedFill == fill) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001671 if (stroke.needToApply()) {
1672 GL_CALL(CoverStrokePath(id, GR_GL_BOUNDING_BOX));
1673 } else {
1674 GL_CALL(CoverFillPath(id, GR_GL_BOUNDING_BOX));
1675 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001676 } else {
1677 GrDrawState* drawState = this->drawState();
1678 GrDrawState::AutoViewMatrixRestore avmr;
1679 SkRect bounds = SkRect::MakeLTRB(0, 0,
1680 SkIntToScalar(drawState->getRenderTarget()->width()),
1681 SkIntToScalar(drawState->getRenderTarget()->height()));
1682 SkMatrix vmi;
1683 // mapRect through persp matrix may not be correct
1684 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1685 vmi.mapRect(&bounds);
1686 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1687 // precision.
1688 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
1689 bounds.outset(bloat, bloat);
1690 } else {
1691 avmr.setIdentity(drawState);
1692 }
1693
1694 this->drawSimpleRect(bounds, NULL);
1695 }
1696}
1697
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001698void GrGpuGL::onGpuDrawPaths(size_t pathCount, const GrPath** paths,
1699 const SkMatrix* transforms,
1700 SkPath::FillType fill,
1701 SkStrokeRec::Style stroke) {
1702 SkASSERT(this->caps()->pathRenderingSupport());
1703 SkASSERT(NULL != this->drawState()->getRenderTarget());
1704 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1705 SkASSERT(!fCurrentProgram->hasVertexShader());
1706 SkASSERT(stroke != SkStrokeRec::kHairline_Style);
1707
1708 SkAutoMalloc pathData(pathCount * sizeof(GrGLuint));
1709 SkAutoMalloc transformData(pathCount * sizeof(GrGLfloat) * 6);
1710 GrGLfloat* transformValues =
1711 reinterpret_cast<GrGLfloat*>(transformData.get());
1712 GrGLuint* pathIDs = reinterpret_cast<GrGLuint*>(pathData.get());
1713
1714 for (size_t i = 0; i < pathCount; ++i) {
1715 SkASSERT(transforms[i].asAffine(NULL));
1716 const SkMatrix& m = transforms[i];
1717 transformValues[i * 6] = m.getScaleX();
1718 transformValues[i * 6 + 1] = m.getSkewY();
1719 transformValues[i * 6 + 2] = m.getSkewX();
1720 transformValues[i * 6 + 3] = m.getScaleY();
1721 transformValues[i * 6 + 4] = m.getTranslateX();
1722 transformValues[i * 6 + 5] = m.getTranslateY();
1723 pathIDs[i] = static_cast<const GrGLPath*>(paths[i])->pathID();
1724 }
1725
1726 flushPathStencilSettings(fill);
1727
1728 SkPath::FillType nonInvertedFill =
1729 SkPath::ConvertToNonInverseFillType(fill);
1730
1731 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1732 GrGLenum fillMode =
1733 gr_stencil_op_to_gl_path_rendering_fill_mode(
1734 fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1735 GrGLint writeMask =
1736 fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1737
1738 bool doFill = stroke == SkStrokeRec::kFill_Style
1739 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1740 bool doStroke = stroke == SkStrokeRec::kStroke_Style
1741 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1742
1743 if (doFill) {
1744 GL_CALL(StencilFillPathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1745 pathIDs, 0,
1746 fillMode, writeMask,
1747 GR_GL_AFFINE_2D, transformValues));
1748 }
1749 if (doStroke) {
1750 GL_CALL(StencilStrokePathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1751 pathIDs, 0,
1752 0xffff, writeMask,
1753 GR_GL_AFFINE_2D, transformValues));
1754 }
1755
1756 if (nonInvertedFill == fill) {
1757 if (doStroke) {
1758 GL_CALL(CoverStrokePathInstanced(
1759 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1760 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1761 GR_GL_AFFINE_2D, transformValues));
1762 } else {
1763 GL_CALL(CoverFillPathInstanced(
1764 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1765 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1766 GR_GL_AFFINE_2D, transformValues));
1767
1768 }
1769 } else {
1770 GrDrawState* drawState = this->drawState();
1771 GrDrawState::AutoViewMatrixRestore avmr;
1772 SkRect bounds = SkRect::MakeLTRB(0, 0,
1773 SkIntToScalar(drawState->getRenderTarget()->width()),
1774 SkIntToScalar(drawState->getRenderTarget()->height()));
1775 SkMatrix vmi;
1776 // mapRect through persp matrix may not be correct
1777 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1778 vmi.mapRect(&bounds);
1779 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1780 // precision.
1781 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
1782 bounds.outset(bloat, bloat);
1783 } else {
1784 avmr.setIdentity(drawState);
1785 }
1786
1787 this->drawSimpleRect(bounds, NULL);
1788 }
1789}
1790
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001791void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001792 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001793 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001794 // Some extensions automatically resolves the texture when it is read.
1795 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001796 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001797 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1798 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1799 // make sure we go through flushRenderTarget() since we've modified
1800 // the bound DRAW FBO ID.
1801 fHWBoundRenderTarget = NULL;
1802 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001803 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001804 GrGLIRect r;
1805 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1806 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001807
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001808 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001809 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001810 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001811 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001812 fScissorState.fEnabled = true;
1813 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001814 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001815 GL_CALL(ResolveMultisampleFramebuffer());
1816 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001817 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001818 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001819 asr.reset(&fScissorState);
1820 fScissorState.fEnabled = false;
1821 this->flushScissor();
1822 }
1823 int right = r.fLeft + r.fWidth;
1824 int top = r.fBottom + r.fHeight;
1825 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1826 r.fLeft, r.fBottom, right, top,
1827 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001828 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001829 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001830 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001831 }
1832}
1833
bsalomon@google.com411dad02012-06-05 20:24:20 +00001834namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001835
bsalomon@google.com411dad02012-06-05 20:24:20 +00001836GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1837 static const GrGLenum gTable[] = {
1838 GR_GL_ALWAYS, // kAlways_StencilFunc
1839 GR_GL_NEVER, // kNever_StencilFunc
1840 GR_GL_GREATER, // kGreater_StencilFunc
1841 GR_GL_GEQUAL, // kGEqual_StencilFunc
1842 GR_GL_LESS, // kLess_StencilFunc
1843 GR_GL_LEQUAL, // kLEqual_StencilFunc,
1844 GR_GL_EQUAL, // kEqual_StencilFunc,
1845 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
1846 };
1847 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
1848 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
1849 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
1850 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
1851 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
1852 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
1853 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
1854 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
1855 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001856 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001857
1858 return gTable[basicFunc];
1859}
1860
1861GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1862 static const GrGLenum gTable[] = {
1863 GR_GL_KEEP, // kKeep_StencilOp
1864 GR_GL_REPLACE, // kReplace_StencilOp
1865 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1866 GR_GL_INCR, // kIncClamp_StencilOp
1867 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1868 GR_GL_DECR, // kDecClamp_StencilOp
1869 GR_GL_ZERO, // kZero_StencilOp
1870 GR_GL_INVERT, // kInvert_StencilOp
1871 };
1872 GR_STATIC_ASSERT(GR_ARRAY_COUNT(gTable) == kStencilOpCount);
1873 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1874 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1875 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1876 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1877 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1878 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1879 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1880 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001881 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001882 return gTable[op];
1883}
1884
1885void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001886 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001887 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001888 GrStencilSettings::Face grFace) {
1889 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
1890 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1891 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1892
1893 GrGLint ref = settings.funcRef(grFace);
1894 GrGLint mask = settings.funcMask(grFace);
1895 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001896
1897 if (GR_GL_FRONT_AND_BACK == glFace) {
1898 // we call the combined func just in case separate stencil is not
1899 // supported.
1900 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1901 GR_GL_CALL(gl, StencilMask(writeMask));
1902 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1903 } else {
1904 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1905 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1906 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1907 }
1908}
1909}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001910
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001911void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001912 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001913 if (fStencilSettings.isDisabled()) {
1914 if (kNo_TriState != fHWStencilTestEnabled) {
1915 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1916 fHWStencilTestEnabled = kNo_TriState;
1917 }
1918 } else {
1919 if (kYes_TriState != fHWStencilTestEnabled) {
1920 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1921 fHWStencilTestEnabled = kYes_TriState;
1922 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001923 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001924 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001925 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001926 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001927 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001928 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001930 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001931 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001932 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001933 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001934 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001935 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001936 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001937 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001938 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001939 }
1940 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001941 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001942 }
1943}
1944
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001945void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001946// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1947// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1948#if 0
1949 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1950 #define RT_HAS_MSAA rt->isMultisampled()
1951#else
1952 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1953#endif
1954
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001955 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00001956 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001957 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
1958 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001959 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001960 bool smoothLines = false;
1961
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001962 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001963 smoothLines = this->willUseHWAALines();
1964 if (smoothLines) {
1965 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
1966 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
1967 fHWAAState.fSmoothLineEnabled = kYes_TriState;
1968 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00001969 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001970 kNo_TriState != fHWAAState.fMSAAEnabled) {
1971 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1972 fHWAAState.fMSAAEnabled = kNo_TriState;
1973 }
1974 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001975 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001976 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
1977 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
1978 fHWAAState.fSmoothLineEnabled = kNo_TriState;
1979 }
1980 }
1981 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00001982 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001983 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1984 // convex hulls of each segment appear to get filled.
1985 bool enableMSAA = kStencilPath_DrawType == type ||
1986 this->getDrawState().isHWAntialiasState();
1987 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001988 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
1989 GL_CALL(Enable(GR_GL_MULTISAMPLE));
1990 fHWAAState.fMSAAEnabled = kYes_TriState;
1991 }
1992 } else {
1993 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
1994 GL_CALL(Disable(GR_GL_MULTISAMPLE));
1995 fHWAAState.fMSAAEnabled = kNo_TriState;
1996 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001997 }
1998 }
1999 }
2000}
2001
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002002void GrGpuGL::flushPathStencilSettings(SkPath::FillType fill) {
2003 GrStencilSettings pathStencilSettings;
2004 this->getPathStencilSettingsForFillType(fill, &pathStencilSettings);
2005 if (fHWPathStencilSettings != pathStencilSettings) {
2006 // Just the func, ref, and mask is set here. The op and write mask are params to the call
2007 // that draws the path to the SB (glStencilFillPath)
2008 GrGLenum func =
2009 gr_to_gl_stencil_func(pathStencilSettings.func(GrStencilSettings::kFront_Face));
2010 GL_CALL(PathStencilFunc(func,
2011 pathStencilSettings.funcRef(GrStencilSettings::kFront_Face),
2012 pathStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
2013
2014 fHWPathStencilSettings = pathStencilSettings;
2015 }
2016}
2017
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002018void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002019 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002020 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002021 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002022 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002023 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002024 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002025 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002026 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2027 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2028 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2029 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2030 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2031 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002032 }
2033 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002034 // any optimization to disable blending should
2035 // have already been applied and tweaked the coeffs
2036 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002037 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2038 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002039 if (blendOff) {
2040 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002041 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002042 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002043 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002044 } else {
2045 if (kYes_TriState != fHWBlendState.fEnabled) {
2046 GL_CALL(Enable(GR_GL_BLEND));
2047 fHWBlendState.fEnabled = kYes_TriState;
2048 }
2049 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2050 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002051 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2052 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002053 fHWBlendState.fSrcCoeff = srcCoeff;
2054 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002055 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002056 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002057 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2058 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002059 (!fHWBlendState.fConstColorValid ||
2060 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002061 GrGLfloat c[4];
2062 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002063 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002064 fHWBlendState.fConstColor = blendConst;
2065 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002066 }
2067 }
2068 }
2069}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002070
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002071static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002072 static const GrGLenum gWrapModes[] = {
2073 GR_GL_CLAMP_TO_EDGE,
2074 GR_GL_REPEAT,
2075 GR_GL_MIRRORED_REPEAT
2076 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002077 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002078 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2079 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2080 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2081 return gWrapModes[tm];
2082}
2083
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002084void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002085 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002086
bsalomon@google.comb8670992012-07-25 21:27:09 +00002087 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2088 // 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 +00002089 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002090 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002091 if (NULL != texRT) {
2092 this->onResolveRenderTarget(texRT);
2093 }
2094
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002095 if (fHWBoundTextures[unitIdx] != texture) {
2096 this->setTextureUnit(unitIdx);
2097 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2098 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002099 }
2100
bsalomon@google.com4c883782012-06-04 19:05:11 +00002101 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002102 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002103 bool setAll = timestamp < this->getResetTimestamp();
2104 GrGLTexture::TexParams newTexParams;
2105
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002106 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002107 GR_GL_NEAREST,
2108 GR_GL_LINEAR,
2109 GR_GL_LINEAR_MIPMAP_LINEAR
2110 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002111 static GrGLenum glMagFilterModes[] = {
2112 GR_GL_NEAREST,
2113 GR_GL_LINEAR,
2114 GR_GL_LINEAR
2115 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002116 GrTextureParams::FilterMode filterMode = params.filterMode();
2117 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2118 filterMode = GrTextureParams::kBilerp_FilterMode;
2119 }
2120 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2121 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002122
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002123 if (GrTextureParams::kMipMap_FilterMode == filterMode && texture->mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002124// GL_CALL(Hint(GR_GL_GENERATE_MIPMAP_HINT,GR_GL_NICEST));
2125 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2126 texture->dirtyMipMaps(false);
2127 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002128
bsalomon@google.comb8670992012-07-25 21:27:09 +00002129 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2130 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002131 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002132 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002133 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002134 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002135 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002136 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2137 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002138 newTexParams.fMagFilter));
2139 }
2140 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2141 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002142 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2143 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002144 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002145 }
2146 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002147 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002148 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2149 GR_GL_TEXTURE_WRAP_S,
2150 newTexParams.fWrapS));
2151 }
2152 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002153 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002154 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2155 GR_GL_TEXTURE_WRAP_T,
2156 newTexParams.fWrapT));
2157 }
2158 if (this->glCaps().textureSwizzleSupport() &&
2159 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2160 oldTexParams.fSwizzleRGBA,
2161 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002162 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002163 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002164 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2165 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2166 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2167 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2168 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2169 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2170 } else {
2171 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2172 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2173 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2174 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002175 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002176 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002177}
2178
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002179void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2180 const SkISize& renderTargetSize,
2181 GrSurfaceOrigin renderTargetOrigin) {
2182
2183 SkASSERT(this->glCaps().fixedFunctionSupport());
2184
2185 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2186 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2187 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2188 return;
2189 }
2190
2191 fHWProjectionMatrixState.fViewMatrix = matrix;
2192 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2193 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2194
2195 GrGLfloat glMatrix[4 * 4];
2196 fHWProjectionMatrixState.getGLMatrix<4>(glMatrix);
2197 GL_CALL(MatrixMode(GR_GL_PROJECTION));
2198 GL_CALL(LoadMatrixf(glMatrix));
2199}
2200
2201void GrGpuGL::enableTexGen(int unitIdx,
2202 TexGenComponents components,
2203 const GrGLfloat* coefficients) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002204 SkASSERT(this->glCaps().fixedFunctionSupport());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002205 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002206 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= unitIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002207
2208 if (GR_GL_OBJECT_LINEAR == fHWTexGenSettings[unitIdx].fMode &&
2209 components == fHWTexGenSettings[unitIdx].fNumComponents &&
2210 !memcmp(coefficients, fHWTexGenSettings[unitIdx].fCoefficients,
2211 3 * components * sizeof(GrGLfloat))) {
2212 return;
2213 }
2214
2215 this->setTextureUnit(unitIdx);
2216
2217 if (GR_GL_OBJECT_LINEAR != fHWTexGenSettings[unitIdx].fMode) {
2218 for (int i = 0; i < 4; i++) {
2219 GL_CALL(TexGeni(GR_GL_S + i, GR_GL_TEXTURE_GEN_MODE, GR_GL_OBJECT_LINEAR));
2220 }
2221 fHWTexGenSettings[unitIdx].fMode = GR_GL_OBJECT_LINEAR;
2222 }
2223
2224 for (int i = fHWTexGenSettings[unitIdx].fNumComponents; i < components; i++) {
2225 GL_CALL(Enable(GR_GL_TEXTURE_GEN_S + i));
2226 }
2227 for (int i = components; i < fHWTexGenSettings[unitIdx].fNumComponents; i++) {
2228 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + i));
2229 }
2230 fHWTexGenSettings[unitIdx].fNumComponents = components;
2231
2232 for (int i = 0; i < components; i++) {
2233 GrGLfloat plane[] = {coefficients[0 + 3 * i],
2234 coefficients[1 + 3 * i],
2235 0,
2236 coefficients[2 + 3 * i]};
2237 GL_CALL(TexGenfv(GR_GL_S + i, GR_GL_OBJECT_PLANE, plane));
2238 }
2239
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002240 if (this->caps()->pathRenderingSupport()) {
2241 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx,
2242 GR_GL_OBJECT_LINEAR,
2243 components,
2244 coefficients));
2245 }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002246
2247 memcpy(fHWTexGenSettings[unitIdx].fCoefficients, coefficients,
2248 3 * components * sizeof(GrGLfloat));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002249}
2250
2251void GrGpuGL::enableTexGen(int unitIdx, TexGenComponents components, const SkMatrix& matrix) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002252 GrGLfloat coefficients[3 * 3];
2253 SkASSERT(this->glCaps().fixedFunctionSupport());
2254 SkASSERT(components >= kS_TexGenComponents && components <= kSTR_TexGenComponents);
2255
2256 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2257 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2258 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2259
2260 if (components >= kST_TexGenComponents) {
2261 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2262 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2263 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2264 }
2265
2266 if (components >= kSTR_TexGenComponents) {
2267 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2268 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2269 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2270 }
2271
2272 enableTexGen(unitIdx, components, coefficients);
2273}
2274
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002275void GrGpuGL::flushTexGenSettings(int numUsedTexCoordSets) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002276 SkASSERT(this->glCaps().fixedFunctionSupport());
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002277 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= numUsedTexCoordSets);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002278
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002279 // Only write the inactive tex gens, since active tex gens were written
2280 // when they were enabled.
2281
2282 SkDEBUGCODE(
2283 for (int i = 0; i < numUsedTexCoordSets; i++) {
2284 SkASSERT(0 != fHWTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002285 }
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002286 );
2287
2288 for (int i = numUsedTexCoordSets; i < fHWActiveTexGenSets; i++) {
2289 SkASSERT(0 != fHWTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002290
2291 this->setTextureUnit(i);
2292 for (int j = 0; j < fHWTexGenSettings[i].fNumComponents; j++) {
2293 GL_CALL(Disable(GR_GL_TEXTURE_GEN_S + j));
2294 }
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002295
2296 if (this->caps()->pathRenderingSupport()) {
2297 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
2298 }
2299
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002300 fHWTexGenSettings[i].fNumComponents = 0;
2301 }
2302
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002303 fHWActiveTexGenSets = numUsedTexCoordSets;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002304}
2305
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002306void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002307
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002308 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002309
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002310 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002311 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002312 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002313 fHWDitherEnabled = kYes_TriState;
2314 }
2315 } else {
2316 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002317 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002318 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 }
2320 }
2321
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002322 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002323 if (kNo_TriState != fHWWriteToColor) {
2324 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2325 GR_GL_FALSE, GR_GL_FALSE));
2326 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002327 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002328 } else {
2329 if (kYes_TriState != fHWWriteToColor) {
2330 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2331 fHWWriteToColor = kYes_TriState;
2332 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002333 }
2334
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002335 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002336 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002337 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002338 GL_CALL(Enable(GR_GL_CULL_FACE));
2339 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002340 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002341 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002342 GL_CALL(Enable(GR_GL_CULL_FACE));
2343 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002344 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002345 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002346 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002347 break;
2348 default:
2349 GrCrash("Unknown draw face.");
2350 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002351 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002352 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002353}
2354
reed@google.comac10a2d2010-12-22 21:39:39 +00002355void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002356 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002357 if (fHWBoundRenderTarget == renderTarget) {
2358 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002360}
2361
2362void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002363 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002364 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002365 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002366 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002367 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002369}
2370
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002371bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2372 bool getSizedInternalFormat,
2373 GrGLenum* internalFormat,
2374 GrGLenum* externalFormat,
2375 GrGLenum* externalType) {
2376 GrGLenum dontCare;
2377 if (NULL == internalFormat) {
2378 internalFormat = &dontCare;
2379 }
2380 if (NULL == externalFormat) {
2381 externalFormat = &dontCare;
2382 }
2383 if (NULL == externalType) {
2384 externalType = &dontCare;
2385 }
2386
reed@google.comac10a2d2010-12-22 21:39:39 +00002387 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002388 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002389 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002390 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002391 if (getSizedInternalFormat) {
2392 *internalFormat = GR_GL_RGBA8;
2393 } else {
2394 *internalFormat = GR_GL_RGBA;
2395 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002396 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002397 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002398 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002399 if (!this->glCaps().bgraFormatSupport()) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002400 return false;
2401 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002402 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002403 if (getSizedInternalFormat) {
2404 *internalFormat = GR_GL_BGRA8;
2405 } else {
2406 *internalFormat = GR_GL_BGRA;
2407 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002408 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002409 if (getSizedInternalFormat) {
2410 *internalFormat = GR_GL_RGBA8;
2411 } else {
2412 *internalFormat = GR_GL_RGBA;
2413 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002414 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002415 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002416 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002417 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002418 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002419 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002420 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002421 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002422 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002423 return false;
2424 } else {
2425 *internalFormat = GR_GL_RGB565;
2426 }
2427 } else {
2428 *internalFormat = GR_GL_RGB;
2429 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002430 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002431 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002432 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002433 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002434 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002435 if (getSizedInternalFormat) {
2436 *internalFormat = GR_GL_RGBA4;
2437 } else {
2438 *internalFormat = GR_GL_RGBA;
2439 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002440 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002441 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002442 case kIndex_8_GrPixelConfig:
bsalomon@google.combcce8922013-03-25 15:38:39 +00002443 if (this->caps()->eightBitPaletteSupport()) {
bsalomon@google.comc312bf92011-03-21 21:10:33 +00002444 *internalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002445 // glCompressedTexImage doesn't take external params
2446 *externalFormat = GR_GL_PALETTE8_RGBA8;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002447 // no sized/unsized internal format distinction here
2448 *internalFormat = GR_GL_PALETTE8_RGBA8;
2449 // unused with CompressedTexImage
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002450 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002451 } else {
2452 return false;
2453 }
2454 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002455 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002456 if (this->glCaps().textureRedSupport()) {
2457 *internalFormat = GR_GL_RED;
2458 *externalFormat = GR_GL_RED;
2459 if (getSizedInternalFormat) {
2460 *internalFormat = GR_GL_R8;
2461 } else {
2462 *internalFormat = GR_GL_RED;
2463 }
2464 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002465 } else {
2466 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002467 *externalFormat = GR_GL_ALPHA;
2468 if (getSizedInternalFormat) {
2469 *internalFormat = GR_GL_ALPHA8;
2470 } else {
2471 *internalFormat = GR_GL_ALPHA;
2472 }
2473 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002474 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002475 break;
2476 default:
2477 return false;
2478 }
2479 return true;
2480}
2481
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002482void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002483 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002484 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002485 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002486 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002487 }
2488}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002489
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002490void GrGpuGL::setScratchTextureUnit() {
2491 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2492 int lastUnitIdx = fHWBoundTextures.count() - 1;
2493 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2494 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2495 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002496 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002497 // clear out the this field so that if a program does use this unit it will rebind the correct
2498 // texture.
2499 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002500}
2501
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002502namespace {
2503// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002504inline bool can_blit_framebuffer(const GrSurface* dst,
2505 const GrSurface* src,
2506 const GrGpuGL* gpu,
2507 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002508 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2509 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002510 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002511 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2512 // or the rects are not the same (not just the same size but have the same edges).
2513 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2514 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2515 return false;
2516 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002517 if (NULL != wouldNeedTempFBO) {
2518 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2519 }
2520 return true;
2521 } else {
2522 return false;
2523 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002524}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002525
2526inline bool can_copy_texsubimage(const GrSurface* dst,
2527 const GrSurface* src,
2528 const GrGpuGL* gpu,
2529 bool* wouldNeedTempFBO = NULL) {
2530 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2531 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2532 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002533 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002534 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2535 return false;
2536 }
2537 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2538 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2539 // then we don't want to copy to the texture but to the MSAA buffer.
2540 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2541 return false;
2542 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002543 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2544 // If the src is multisampled (and uses an extension where there is a separate MSAA
2545 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2546 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2547 return false;
2548 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002549 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
2550 NULL != dst->asTexture() &&
2551 dst->origin() == src->origin() &&
2552 kIndex_8_GrPixelConfig != src->config()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002553 if (NULL != wouldNeedTempFBO) {
2554 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2555 }
2556 return true;
2557 } else {
2558 return false;
2559 }
2560}
2561
2562// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2563// relative to is output.
2564inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2565 GrSurface* surface,
2566 GrGLenum fboTarget,
2567 GrGLIRect* viewport) {
2568 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2569 GrGLuint tempFBOID;
2570 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002571 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002572 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2573 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2574 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2575 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2576 GR_GL_COLOR_ATTACHMENT0,
2577 GR_GL_TEXTURE_2D,
2578 texID,
2579 0));
2580 viewport->fLeft = 0;
2581 viewport->fBottom = 0;
2582 viewport->fWidth = surface->width();
2583 viewport->fHeight = surface->height();
2584 } else {
2585 tempFBOID = 0;
2586 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2587 *viewport = rt->getViewport();
2588 }
2589 return tempFBOID;
2590}
2591
2592}
2593
2594void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2595 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002596 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002597 kBGRA_8888_GrPixelConfig == src->config()) {
2598 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002599 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002600 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002601 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002602 } else if (NULL == src->asRenderTarget()) {
2603 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2604 // class handle it by rendering.
2605 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002606 return;
2607 }
2608
2609 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2610 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2611 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2612 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002613 } else {
2614 desc->fConfig = src->config();
2615 desc->fOrigin = src->origin();
2616 desc->fFlags = kNone_GrTextureFlags;
2617 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002618}
2619
2620bool GrGpuGL::onCopySurface(GrSurface* dst,
2621 GrSurface* src,
2622 const SkIRect& srcRect,
2623 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002624 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002625 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002626 bool wouldNeedTempFBO = false;
2627 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2628 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2629 GrGLuint srcFBO;
2630 GrGLIRect srcVP;
2631 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2632 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002633 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002634 // We modified the bound FBO
2635 fHWBoundRenderTarget = NULL;
2636 GrGLIRect srcGLRect;
2637 srcGLRect.setRelativeTo(srcVP,
2638 srcRect.fLeft,
2639 srcRect.fTop,
2640 srcRect.width(),
2641 srcRect.height(),
2642 src->origin());
2643
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002644 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002645 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2646 GrGLint dstY;
2647 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2648 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2649 } else {
2650 dstY = dstPoint.fY;
2651 }
2652 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2653 dstPoint.fX, dstY,
2654 srcGLRect.fLeft, srcGLRect.fBottom,
2655 srcGLRect.fWidth, srcGLRect.fHeight));
2656 copied = true;
2657 if (srcFBO) {
2658 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2659 }
2660 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2661 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002662 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2663 srcRect.width(), srcRect.height());
2664 bool selfOverlap = false;
2665 if (dst->isSameAs(src)) {
2666 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2667 }
2668
2669 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002670 GrGLuint dstFBO;
2671 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002672 GrGLIRect dstVP;
2673 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002674 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2675 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2676 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002677 fHWBoundRenderTarget = NULL;
2678 GrGLIRect srcGLRect;
2679 GrGLIRect dstGLRect;
2680 srcGLRect.setRelativeTo(srcVP,
2681 srcRect.fLeft,
2682 srcRect.fTop,
2683 srcRect.width(),
2684 srcRect.height(),
2685 src->origin());
2686 dstGLRect.setRelativeTo(dstVP,
2687 dstRect.fLeft,
2688 dstRect.fTop,
2689 dstRect.width(),
2690 dstRect.height(),
2691 dst->origin());
2692
2693 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002694 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002695 // The EXT version applies the scissor during the blit, so disable it.
2696 asr.reset(&fScissorState);
2697 fScissorState.fEnabled = false;
2698 this->flushScissor();
2699 }
2700 GrGLint srcY0;
2701 GrGLint srcY1;
2702 // Does the blit need to y-mirror or not?
2703 if (src->origin() == dst->origin()) {
2704 srcY0 = srcGLRect.fBottom;
2705 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2706 } else {
2707 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2708 srcY1 = srcGLRect.fBottom;
2709 }
2710 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2711 srcY0,
2712 srcGLRect.fLeft + srcGLRect.fWidth,
2713 srcY1,
2714 dstGLRect.fLeft,
2715 dstGLRect.fBottom,
2716 dstGLRect.fLeft + dstGLRect.fWidth,
2717 dstGLRect.fBottom + dstGLRect.fHeight,
2718 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2719 if (dstFBO) {
2720 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2721 }
2722 if (srcFBO) {
2723 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2724 }
2725 copied = true;
2726 }
2727 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002728 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002729 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002730 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002731 }
2732 return copied;
2733}
2734
2735bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2736 GrSurface* src,
2737 const SkIRect& srcRect,
2738 const SkIPoint& dstPoint) {
2739 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002740 if (can_copy_texsubimage(dst, src, this)) {
2741 return true;
2742 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002743 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002744 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002745 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2746 srcRect.width(), srcRect.height());
2747 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2748 return true;
2749 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002750 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002751 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002752 }
2753 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002754 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002755}
2756
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002757void GrGpuGL::onInstantGpuTraceEvent(const char* marker) {
2758 if (this->caps()->gpuTracingSupport()) {
2759 // GL_CALL(InsertEventMarker(0, marker));
2760 }
2761}
2762
2763void GrGpuGL::onPushGpuTraceEvent(const char* marker) {
2764 if (this->caps()->gpuTracingSupport()) {
2765 // GL_CALL(PushGroupMarker(0, marker));
2766 }
2767}
2768
2769void GrGpuGL::onPopGpuTraceEvent() {
2770 if (this->caps()->gpuTracingSupport()) {
2771 // GL_CALL(PopGroupMarker());
2772 }
2773}
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002774
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002775///////////////////////////////////////////////////////////////////////////////
2776
bsalomon@google.com6918d482013-03-07 19:09:11 +00002777GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2778 GrGpuGL* gpu,
2779 const GrGLVertexBuffer* vbuffer,
2780 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002781 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002782 GrGLAttribArrayState* attribState;
2783
bsalomon@google.com6918d482013-03-07 19:09:11 +00002784 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2785 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
2786 if (NULL == fVBOVertexArray || !fVBOVertexArray->isValid()) {
2787 SkSafeUnref(fVBOVertexArray);
2788 GrGLuint arrayID;
2789 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2790 int attrCount = gpu->glCaps().maxVertexAttributes();
2791 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002792 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002793 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2794 } else {
2795 if (NULL != ibuffer) {
2796 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2797 } else {
2798 this->setVertexArrayID(gpu, 0);
2799 }
2800 int attrCount = gpu->glCaps().maxVertexAttributes();
2801 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2802 fDefaultVertexArrayAttribState.resize(attrCount);
2803 }
2804 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002805 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002806 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002807}