blob: 7568ba32cf195099ce54d005d2567fece1b1462d [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 ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000084 SK_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 ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000108 SK_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());
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000123 fHWPathTexGenSettings.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 {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000203 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config() ||
204 GrPixelConfigIsCompressed(srcConfig) || GrPixelConfigIsCompressed(texture->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000205 return false;
206 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000207 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000208 // In general ES2 requires the internal format of the texture and the format of the src
209 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
210 // texture. It depends upon which extension added BGRA. The Apple extension allows it
211 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
212 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000213 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000214 !this->glCaps().bgraIsInternalFormat() &&
215 kBGRA_8888_GrPixelConfig == srcConfig &&
216 kRGBA_8888_GrPixelConfig == texture->config()) {
217 return true;
218 } else {
219 return false;
220 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000221 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000222 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000223 }
224}
225
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000226bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
227 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
228}
229
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000230void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000231 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000232 if (resetBits & kMisc_GrGLBackendState) {
233 GL_CALL(Disable(GR_GL_DEPTH_TEST));
234 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000235
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000236 fHWDrawFace = GrDrawState::kInvalid_DrawFace;
237 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000239 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000240 // Desktop-only state that we never change
241 if (!this->glCaps().isCoreProfile()) {
242 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
243 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
244 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
245 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
246 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
247 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
248 }
249 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
250 // core profile. This seems like a bug since the core spec removes any mention of
251 // GL_ARB_imaging.
252 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
253 GL_CALL(Disable(GR_GL_COLOR_TABLE));
254 }
255 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
256 // Since ES doesn't support glPointSize at all we always use the VS to
257 // set the point size
258 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
259
260 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
261 // currently part of our gl interface. There are probably others as
262 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000263 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000264 fHWWriteToColor = kUnknown_TriState;
265 // we only ever use lines in hairline mode
266 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000267 }
edisonn@google.comba669992013-06-28 16:03:21 +0000268
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000269 if (resetBits & kAA_GrGLBackendState) {
270 fHWAAState.invalidate();
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000273 fHWActiveTextureUnitIdx = -1; // invalid
274
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000275 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000276 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
277 fHWBoundTextures[s] = NULL;
278 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000279 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000280
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000281 if (resetBits & kBlend_GrGLBackendState) {
282 fHWBlendState.invalidate();
283 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000284
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000285 if (resetBits & kView_GrGLBackendState) {
286 fHWScissorSettings.invalidate();
287 fHWViewport.invalidate();
288 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000290 if (resetBits & kStencil_GrGLBackendState) {
291 fHWStencilSettings.invalidate();
292 fHWStencilTestEnabled = kUnknown_TriState;
293 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000294
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000295 // Vertex
296 if (resetBits & kVertex_GrGLBackendState) {
297 fHWGeometryState.invalidate();
298 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 if (resetBits & kRenderTarget_GrGLBackendState) {
301 fHWBoundRenderTarget = NULL;
302 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000303
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000304 if (resetBits & kPathRendering_GrGLBackendState) {
305 if (this->caps()->pathRenderingSupport()) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000306 fHWProjectionMatrixState.invalidate();
307 // we don't use the model view matrix.
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +0000308 GL_CALL(MatrixLoadIdentity(GR_GL_MODELVIEW));
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) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000311 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
312 fHWPathTexGenSettings[i].fMode = GR_GL_NONE;
313 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000314 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000315 fHWActivePathTexGenSets = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000316 }
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000317 fHWPathStencilSettings.invalidate();
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000318 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000319
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000320 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000321 if (resetBits & kPixelStore_GrGLBackendState) {
322 if (this->glCaps().unpackRowLengthSupport()) {
323 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
324 }
325 if (this->glCaps().packRowLengthSupport()) {
326 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
327 }
328 if (this->glCaps().unpackFlipYSupport()) {
329 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
330 }
331 if (this->glCaps().packFlipYSupport()) {
332 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
333 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000334 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000335
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000336 if (resetBits & kProgram_GrGLBackendState) {
337 fHWProgramID = 0;
338 fSharedGLProgramState.invalidate();
339 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000340}
341
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000342namespace {
343
344GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
345 // By default, GrRenderTargets are GL's normal orientation so that they
346 // can be drawn to by the outside world without the client having
347 // to render upside down.
348 if (kDefault_GrSurfaceOrigin == origin) {
349 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
350 } else {
351 return origin;
352 }
353}
354
355}
356
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000357GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000358 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000359 return NULL;
360 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000361
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000362 if (0 == desc.fTextureHandle) {
363 return NULL;
364 }
365
bsalomon@google.combcce8922013-03-25 15:38:39 +0000366 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000367 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
368 return NULL;
369 }
370
371 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000372 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000373 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000374 glTexDesc.fWidth = desc.fWidth;
375 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000376 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000377 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000378 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000379 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000380 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000381 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
382 // assuming the old behaviour, which is that backend textures are always
383 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
384 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
385 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
386 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
387 } else {
388 glTexDesc.fOrigin = desc.fOrigin;
389 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000390
391 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000392 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000393 GrGLRenderTarget::Desc glRTDesc;
394 glRTDesc.fRTFBOID = 0;
395 glRTDesc.fTexFBOID = 0;
396 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000397 glRTDesc.fConfig = desc.fConfig;
398 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000399 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000400 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000401 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
402 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000403 glTexDesc.fTextureID,
404 &glRTDesc)) {
405 return NULL;
406 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000407 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000408 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000409 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000410 }
411 if (NULL == texture) {
412 return NULL;
413 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 return texture;
416}
417
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000418GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000419 GrGLRenderTarget::Desc glDesc;
420 glDesc.fConfig = desc.fConfig;
421 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
422 glDesc.fMSColorRenderbufferID = 0;
423 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
424 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000425 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000426 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000427
428 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000429 GrGLIRect viewport;
430 viewport.fLeft = 0;
431 viewport.fBottom = 0;
432 viewport.fWidth = desc.fWidth;
433 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000435 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
436 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000437 if (desc.fStencilBits) {
438 GrGLStencilBuffer::Format format;
439 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
440 format.fPacked = false;
441 format.fStencilBits = desc.fStencilBits;
442 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000443 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000444 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
445 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000446 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000447 0,
448 desc.fWidth,
449 desc.fHeight,
450 desc.fSampleCnt,
451 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000452 tgt->setStencilBuffer(sb);
453 sb->unref();
454 }
455 return tgt;
456}
457
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000458////////////////////////////////////////////////////////////////////////////////
459
bsalomon@google.com9c680582013-02-06 18:17:50 +0000460bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000461 int left, int top, int width, int height,
462 GrPixelConfig config, const void* buffer,
463 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000464 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000465 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000466 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000467 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
468
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000469 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000470 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
471 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000472 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000473 desc.fWidth = glTex->width();
474 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000475 desc.fConfig = glTex->config();
476 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000477 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000478 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000479
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000480 if (this->uploadTexData(desc, false,
481 left, top, width, height,
482 config, buffer, rowBytes)) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000483 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000484 return true;
485 } else {
486 return false;
487 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000488}
489
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000490namespace {
491bool adjust_pixel_ops_params(int surfaceWidth,
492 int surfaceHeight,
493 size_t bpp,
494 int* left, int* top, int* width, int* height,
495 const void** data,
496 size_t* rowBytes) {
497 if (!*rowBytes) {
498 *rowBytes = *width * bpp;
499 }
500
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000501 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
502 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000503
504 if (!subRect.intersect(bounds)) {
505 return false;
506 }
507 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
508 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
509
510 *left = subRect.fLeft;
511 *top = subRect.fTop;
512 *width = subRect.width();
513 *height = subRect.height();
514 return true;
515}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000516
517GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
518 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
519 return GR_GL_GET_ERROR(interface);
520 } else {
521 return CHECK_ALLOC_ERROR(interface);
522 }
523}
524
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000525}
526
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000527bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
528 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529 int left, int top, int width, int height,
530 GrPixelConfig dataConfig,
531 const void* data,
532 size_t rowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000533 SkASSERT(NULL != data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000534
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000535 // If we're uploading compressed data then we should be using uploadCompressedTexData
536 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
537
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000538 size_t bpp = GrBytesPerPixel(dataConfig);
539 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
540 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000541 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000542 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000543 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000545 // in case we need a temporary, trimmed copy of the src pixels
546 SkAutoSMalloc<128 * 128> tempStorage;
547
bsalomon@google.com313f0192012-07-10 17:21:02 +0000548 // paletted textures cannot be partially updated
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000549 // We currently lazily create MIPMAPs when the we see a draw with
550 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
551 // MIP levels are all created when the texture is created. So for now we don't use
552 // texture storage.
553 bool useTexStorage = false &&
554 isNewTexture &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000555 kIndex_8_GrPixelConfig != desc.fConfig &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000556 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000557
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000558 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000559 // 565 is not a sized internal format on desktop GL. So on desktop with
560 // 565 we always use an unsized internal format to let the system pick
561 // the best sized format to convert the 565 data to. Since TexStorage
562 // only allows sized internal formats we will instead use TexImage2D.
563 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000564 }
565
566 GrGLenum internalFormat;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000567 GrGLenum externalFormat;
568 GrGLenum externalType;
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000569 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
570 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
571 // size of the internal format whenever possible and so only use a sized internal format when
572 // using texture storage.
bsalomon@google.com3323c4d2013-08-20 13:48:33 +0000573 if (!this->configToGLFormats(dataConfig, useTexStorage, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000574 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000575 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000576 }
577
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000578 if (!isNewTexture && GR_GL_PALETTE8_RGBA8 == internalFormat) {
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000579 // paletted textures cannot be updated
580 return false;
581 }
582
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000583 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000584 * check whether to allocate a temporary buffer for flipping y or
585 * because our srcData has extra bytes past each row. If so, we need
586 * to trim those off here, since GL ES may not let us specify
587 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000588 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000589 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000590 bool swFlipY = false;
591 bool glFlipY = false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000592 if (NULL != data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000593 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000594 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000595 glFlipY = true;
596 } else {
597 swFlipY = true;
598 }
599 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000600 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000601 // can't use this for flipping, only non-neg values allowed. :(
602 if (rowBytes != trimRowBytes) {
603 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
604 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
605 restoreGLRowLength = true;
606 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000607 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000608 if (trimRowBytes != rowBytes || swFlipY) {
609 // copy data into our new storage, skipping the trailing bytes
610 size_t trimSize = height * trimRowBytes;
611 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000612 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000613 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000614 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 char* dst = (char*)tempStorage.reset(trimSize);
616 for (int y = 0; y < height; y++) {
617 memcpy(dst, src, trimRowBytes);
618 if (swFlipY) {
619 src -= rowBytes;
620 } else {
621 src += rowBytes;
622 }
623 dst += trimRowBytes;
624 }
625 // now point data to our copied version
626 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000627 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 if (glFlipY) {
630 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
631 }
632 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, static_cast<GrGLint>(bpp)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000633 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000635 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000636 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000637 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000638 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000639 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000640 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000641 GL_ALLOC_CALL(this->glInterface(),
642 TexStorage2D(GR_GL_TEXTURE_2D,
643 1, // levels
644 internalFormat,
645 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000646 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000647 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
648 GrGLsizei imageSize = desc.fWidth * desc.fHeight +
649 kGrColorTableSize;
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000650 GL_ALLOC_CALL(this->glInterface(),
651 CompressedTexImage2D(GR_GL_TEXTURE_2D,
652 0, // level
653 internalFormat,
654 desc.fWidth, desc.fHeight,
655 0, // border
656 imageSize,
657 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000658 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000659 GL_ALLOC_CALL(this->glInterface(),
660 TexImage2D(GR_GL_TEXTURE_2D,
661 0, // level
662 internalFormat,
663 desc.fWidth, desc.fHeight,
664 0, // border
665 externalFormat, externalType,
666 data));
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000667 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000668 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000669 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000670 if (error != GR_GL_NO_ERROR) {
671 succeeded = false;
672 } else {
673 // if we have data and we used TexStorage to create the texture, we
674 // now upload with TexSubImage.
675 if (NULL != data && useTexStorage) {
676 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
677 0, // level
678 left, top,
679 width, height,
680 externalFormat, externalType,
681 data));
682 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000683 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000684 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000685 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000686 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000687 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000688 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
689 0, // level
690 left, top,
691 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000692 externalFormat, externalType, data));
693 }
694
695 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000696 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000697 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000698 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000699 if (glFlipY) {
700 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
701 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000702 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000703}
704
krajcevski9c0e6292014-06-02 07:38:14 -0700705bool GrGpuGL::uploadCompressedTexData(const GrGLTexture::Desc& desc,
706 const void* data) {
707 SkASSERT(NULL != data);
708
709 // No support for software flip y, yet...
710 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
711
712 // Make sure that the width and height that we pass to OpenGL
713 // is a multiple of the block size.
714 int dataSize = GrCompressedFormatDataSize(desc.fConfig, desc.fWidth, desc.fHeight);
715
716 // We only need the internal format for compressed 2D textures.
717 GrGLenum internalFormat = 0;
718 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
719 return false;
720 }
721
722 bool succeeded = true;
723 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
724 GL_ALLOC_CALL(this->glInterface(),
725 CompressedTexImage2D(GR_GL_TEXTURE_2D,
726 0, // level
727 internalFormat,
728 desc.fWidth, desc.fHeight,
729 0, // border
730 dataSize,
731 data));
732
733 GrGLenum error = check_alloc_error(desc, this->glInterface());
734 if (error != GR_GL_NO_ERROR) {
735 succeeded = false;
736 }
737 return succeeded;
738}
739
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000740static bool renderbuffer_storage_msaa(GrGLContext& ctx,
741 int sampleCount,
742 GrGLenum format,
743 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000744 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000745 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000746 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000747 case GrGLCaps::kDesktop_ARB_MSFBOType:
748 case GrGLCaps::kDesktop_EXT_MSFBOType:
749 case GrGLCaps::kES_3_0_MSFBOType:
750 GL_ALLOC_CALL(ctx.interface(),
751 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
752 sampleCount,
753 format,
754 width, height));
755 break;
756 case GrGLCaps::kES_Apple_MSFBOType:
757 GL_ALLOC_CALL(ctx.interface(),
758 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
759 sampleCount,
760 format,
761 width, height));
762 break;
763 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
764 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
765 GL_ALLOC_CALL(ctx.interface(),
766 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
767 sampleCount,
768 format,
769 width, height));
770 break;
771 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000772 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000773 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000774 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000775 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000776}
777
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000778bool GrGpuGL::createRenderTargetObjects(int width, int height,
779 GrGLuint texID,
780 GrGLRenderTarget::Desc* desc) {
781 desc->fMSColorRenderbufferID = 0;
782 desc->fRTFBOID = 0;
783 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000784 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000785
786 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000787
bsalomon@google.comab15d612011-08-09 12:57:56 +0000788 GrGLenum msColorFormat = 0; // suppress warning
789
bsalomon@google.com347c3822013-05-01 20:10:01 +0000790 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
791 goto FAILED;
792 }
793
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000794 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000795 if (!desc->fTexFBOID) {
796 goto FAILED;
797 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000798
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000799
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000800 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
801 // the texture bound to the other. The exception is the IMG multisample extension. With this
802 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
803 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000804 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000805 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
806 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000807 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000808 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000809 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000810 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000811 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000812 &msColorFormat,
813 NULL,
814 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000815 goto FAILED;
816 }
817 } else {
818 desc->fRTFBOID = desc->fTexFBOID;
819 }
820
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000821 // below here we may bind the FBO
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000822 fHWBoundRenderTarget = NULL;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000823 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000824 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000825 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000826 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000827 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000828 desc->fSampleCnt,
829 msColorFormat,
830 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000831 goto FAILED;
832 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000833 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000834 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000835 GR_GL_COLOR_ATTACHMENT0,
836 GR_GL_RENDERBUFFER,
837 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000838 if (desc->fCheckAllocation ||
839 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000840 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
841 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
842 goto FAILED;
843 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000844 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000845 }
846 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000847 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000848
bsalomon@google.com347c3822013-05-01 20:10:01 +0000849 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000850 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
851 GR_GL_COLOR_ATTACHMENT0,
852 GR_GL_TEXTURE_2D,
853 texID, 0, desc->fSampleCnt));
854 } else {
855 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
856 GR_GL_COLOR_ATTACHMENT0,
857 GR_GL_TEXTURE_2D,
858 texID, 0));
859 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000860 if (desc->fCheckAllocation ||
861 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000862 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
863 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
864 goto FAILED;
865 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000866 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000867 }
868
869 return true;
870
871FAILED:
872 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000873 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000874 }
875 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000877 }
878 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000879 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880 }
881 return false;
882}
883
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000884// good to set a break-point here to know when createTexture fails
885static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000886// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000887 return NULL;
888}
889
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000890#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000891static size_t as_size_t(int x) {
892 return x;
893}
894#endif
895
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000896GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000897 const void* srcData,
898 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000899
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000900 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000901 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000902
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000903 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000904 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000905 // We fail if the MSAA was requested and is not available.
906 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
907 //GrPrintf("MSAA RT requested but not supported on this platform.");
908 return return_null_texture();
909 }
910 // If the sample count exceeds the max then we clamp it.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000911 glTexDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000912
robertphillips@google.com32716282012-06-04 12:48:45 +0000913 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000914 glTexDesc.fWidth = desc.fWidth;
915 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000916 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000917 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000918
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000919 glRTDesc.fMSColorRenderbufferID = 0;
920 glRTDesc.fRTFBOID = 0;
921 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000922 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000923 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000924 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000925
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000926 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000927
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000928 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
929 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000930
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000931 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000932 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000933 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000934 //GrPrintf("MSAA RT requested but not supported on this platform.");
935 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000936 }
937
reed@google.comac10a2d2010-12-22 21:39:39 +0000938 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000939 int maxRTSize = this->caps()->maxRenderTargetSize();
940 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000941 return return_null_texture();
942 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000943 } else {
944 int maxSize = this->caps()->maxTextureSize();
945 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
946 return return_null_texture();
947 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000948 }
949
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000950 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000951
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000952 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000953 return return_null_texture();
954 }
955
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000956 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000957 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000958
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000959 if (renderTarget && this->glCaps().textureUsageSupport()) {
960 // provides a hint about how this texture will be used
961 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
962 GR_GL_TEXTURE_USAGE,
963 GR_GL_FRAMEBUFFER_ATTACHMENT));
964 }
965
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000966 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
967 // drivers have a bug where an FBO won't be complete if it includes a
968 // texture that is not mipmap complete (considering the filter in use).
969 GrGLTexture::TexParams initialTexParams;
970 // we only set a subset here so invalidate first
971 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000972 initialTexParams.fMinFilter = GR_GL_NEAREST;
973 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000974 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
975 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000976 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
977 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000978 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000979 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
980 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000981 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
983 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000984 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000985 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
986 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000987 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000988 if (!this->uploadTexData(glTexDesc, true, 0, 0,
989 glTexDesc.fWidth, glTexDesc.fHeight,
990 desc.fConfig, srcData, rowBytes)) {
991 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
992 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000993 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000994
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000995 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000996 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +0000997 // unbind the texture from the texture unit before binding it to the frame buffer
998 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
999
bsalomon@google.com99621082011-11-15 16:47:16 +00001000 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1001 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001002 glTexDesc.fTextureID,
1003 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001004 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001005 return return_null_texture();
1006 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001007 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001009 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001011 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001012#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001013 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1014 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001015#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001016 return tex;
1017}
1018
krajcevski9c0e6292014-06-02 07:38:14 -07001019GrTexture* GrGpuGL::onCreateCompressedTexture(const GrTextureDesc& desc,
1020 const void* srcData) {
1021
1022 if(SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
1023 return return_null_texture();
1024 }
1025
1026 // Make sure that we're not flipping Y.
1027 GrSurfaceOrigin texOrigin = resolve_origin(desc.fOrigin, false);
1028 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1029 return return_null_texture();
1030 }
1031
1032 GrGLTexture::Desc glTexDesc;
1033
1034 glTexDesc.fFlags = desc.fFlags;
1035 glTexDesc.fWidth = desc.fWidth;
1036 glTexDesc.fHeight = desc.fHeight;
1037 glTexDesc.fConfig = desc.fConfig;
1038 glTexDesc.fIsWrapped = false;
1039 glTexDesc.fOrigin = texOrigin;
1040
1041 int maxSize = this->caps()->maxTextureSize();
1042 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1043 return return_null_texture();
1044 }
1045
1046 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
1047
1048 if (!glTexDesc.fTextureID) {
1049 return return_null_texture();
1050 }
1051
1052 this->setScratchTextureUnit();
1053 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1054
1055 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1056 // drivers have a bug where an FBO won't be complete if it includes a
1057 // texture that is not mipmap complete (considering the filter in use).
1058 GrGLTexture::TexParams initialTexParams;
1059 // we only set a subset here so invalidate first
1060 initialTexParams.invalidate();
1061 initialTexParams.fMinFilter = GR_GL_NEAREST;
1062 initialTexParams.fMagFilter = GR_GL_NEAREST;
1063 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1064 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1065 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1066 GR_GL_TEXTURE_MAG_FILTER,
1067 initialTexParams.fMagFilter));
1068 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1069 GR_GL_TEXTURE_MIN_FILTER,
1070 initialTexParams.fMinFilter));
1071 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1072 GR_GL_TEXTURE_WRAP_S,
1073 initialTexParams.fWrapS));
1074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1075 GR_GL_TEXTURE_WRAP_T,
1076 initialTexParams.fWrapT));
1077
1078 if (!this->uploadCompressedTexData(glTexDesc, srcData)) {
1079 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1080 return return_null_texture();
1081 }
1082
1083 GrGLTexture* tex;
1084 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
1085 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1086#ifdef TRACE_TEXTURE_CREATION
1087 GrPrintf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
1088 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1089#endif
1090 return tex;
1091}
1092
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001093namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001094
1095const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1096
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001097void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001098 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001099
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001101 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001102 (kUnknownBitCount == format->fTotalBits));
1103 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001104 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001105 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1106 (GrGLint*)&format->fStencilBits);
1107 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1110 (GrGLint*)&format->fTotalBits);
1111 format->fTotalBits += format->fStencilBits;
1112 } else {
1113 format->fTotalBits = format->fStencilBits;
1114 }
1115 }
1116}
1117}
1118
1119bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1120 int width, int height) {
1121
1122 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001123 // 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 +00001124 SkASSERT(rt->asTexture());
1125 SkASSERT(width >= rt->width());
1126 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001127
1128 int samples = rt->numSamples();
1129 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001130 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 if (!sbID) {
1132 return false;
1133 }
1134
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001135 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001137 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138 // we start with the last stencil format that succeeded in hopes
1139 // that we won't go through this loop more than once after the
1140 // first (painful) stencil creation.
1141 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001142 const GrGLCaps::StencilFormat& sFmt =
1143 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001144 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001145 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001147 bool created;
1148 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001149 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001150 samples,
1151 sFmt.fInternalFormat,
1152 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001154 GL_ALLOC_CALL(this->glInterface(),
1155 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001156 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001157 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001158 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001159 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001160 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 if (created) {
1162 // After sized formats we attempt an unsized format and take
1163 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001164 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001165 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001166 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001167 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001168 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001169 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1171 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001172 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001173 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001174 return true;
1175 }
1176 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 }
1178 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001180 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181}
1182
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001183bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1185
1186 GrGLuint fbo = glrt->renderFBOID();
1187
1188 if (NULL == sb) {
1189 if (NULL != rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001190 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001191 GR_GL_STENCIL_ATTACHMENT,
1192 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001193 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001194 GR_GL_DEPTH_ATTACHMENT,
1195 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001196#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001197 GrGLenum status;
1198 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001199 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200#endif
1201 }
1202 return true;
1203 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001204 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 GrGLuint rb = glsb->renderbufferID();
1206
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001207 fHWBoundRenderTarget = NULL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001208 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001210 GR_GL_STENCIL_ATTACHMENT,
1211 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001214 GR_GL_DEPTH_ATTACHMENT,
1215 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001217 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001218 GR_GL_DEPTH_ATTACHMENT,
1219 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001220 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001222 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001223 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001224 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1225 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001227 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001229 if (glsb->format().fPacked) {
1230 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1231 GR_GL_DEPTH_ATTACHMENT,
1232 GR_GL_RENDERBUFFER, 0));
1233 }
1234 return false;
1235 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001236 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001237 rt->config(),
1238 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001241 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001243}
1244
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001245////////////////////////////////////////////////////////////////////////////////
1246
robertphillips@google.comadacc702013-10-14 21:53:24 +00001247GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001248 GrGLVertexBuffer::Desc desc;
1249 desc.fDynamic = dynamic;
1250 desc.fSizeInBytes = size;
1251 desc.fIsWrapped = false;
1252
bsalomon@google.com96966a52013-02-21 16:34:21 +00001253 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001254 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001255 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001257 } else {
1258 GL_CALL(GenBuffers(1, &desc.fID));
1259 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001260 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001261 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1262 // make sure driver can allocate memory for this buffer
1263 GL_ALLOC_CALL(this->glInterface(),
1264 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001265 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001266 NULL, // data ptr
1267 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1268 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1269 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001270 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001271 return NULL;
1272 }
1273 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1274 return vertexBuffer;
1275 }
1276 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001278}
1279
robertphillips@google.comadacc702013-10-14 21:53:24 +00001280GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001281 GrGLIndexBuffer::Desc desc;
1282 desc.fDynamic = dynamic;
1283 desc.fSizeInBytes = size;
1284 desc.fIsWrapped = false;
1285
bsalomon@google.com96966a52013-02-21 16:34:21 +00001286 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001287 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001288 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001290 } else {
1291 GL_CALL(GenBuffers(1, &desc.fID));
1292 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001293 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001294 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1295 // make sure driver can allocate memory for this buffer
1296 GL_ALLOC_CALL(this->glInterface(),
1297 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001298 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001299 NULL, // data ptr
1300 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1301 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1302 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001303 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001304 return NULL;
1305 }
1306 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1307 return indexBuffer;
1308 }
1309 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001311}
1312
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001313GrPath* GrGpuGL::onCreatePath(const SkPath& inPath, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001314 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001315 return SkNEW_ARGS(GrGLPath, (this, inPath, stroke));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001316}
1317
bsalomon@google.coma3201942012-06-21 19:58:20 +00001318void GrGpuGL::flushScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001319 if (fScissorState.fEnabled) {
commit-bot@chromium.org5c363642013-10-04 15:28:18 +00001320 // Only access the RT if scissoring is being enabled. We can call this before performing
1321 // a glBitframebuffer for a surface->surface copy, which requires no RT to be bound to the
1322 // GrDrawState.
1323 const GrDrawState& drawState = this->getDrawState();
1324 const GrGLRenderTarget* rt =
1325 static_cast<const GrGLRenderTarget*>(drawState.getRenderTarget());
1326
1327 SkASSERT(NULL != rt);
1328 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001329 GrGLIRect scissor;
1330 scissor.setRelativeTo(vp,
1331 fScissorState.fRect.fLeft,
1332 fScissorState.fRect.fTop,
1333 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001334 fScissorState.fRect.height(),
1335 rt->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001336 // if the scissor fully contains the viewport then we fall through and
1337 // disable the scissor test.
1338 if (!scissor.contains(vp)) {
1339 if (fHWScissorSettings.fRect != scissor) {
1340 scissor.pushToGLScissor(this->glInterface());
1341 fHWScissorSettings.fRect = scissor;
1342 }
1343 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1344 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1345 fHWScissorSettings.fEnabled = kYes_TriState;
1346 }
1347 return;
1348 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001350 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001351 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001352 fHWScissorSettings.fEnabled = kNo_TriState;
1353 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 }
1355}
1356
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001357void GrGpuGL::onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001358 const GrDrawState& drawState = this->getDrawState();
1359 const GrRenderTarget* rt = drawState.getRenderTarget();
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001360 // parent class should never let us get here with no RT
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001361 SkASSERT(NULL != rt);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001362
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001363 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1364 rect = NULL;
1365 }
1366
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001367 SkIRect clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001368 if (NULL != rect) {
1369 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001370 clippedRect = *rect;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001371 SkIRect rtRect = SkIRect::MakeWH(rt->width(), rt->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001372 if (clippedRect.intersect(rtRect)) {
1373 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001374 } else {
1375 return;
1376 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001377 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001378
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001379 this->flushRenderTarget(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001380 GrAutoTRestore<ScissorState> asr(&fScissorState);
1381 fScissorState.fEnabled = (NULL != rect);
1382 if (fScissorState.fEnabled) {
1383 fScissorState.fRect = *rect;
1384 }
1385 this->flushScissor();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001386
1387 GrGLfloat r, g, b, a;
1388 static const GrGLfloat scale255 = 1.f / 255.f;
1389 a = GrColorUnpackA(color) * scale255;
1390 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001391 r = GrColorUnpackR(color) * scaleRGB;
1392 g = GrColorUnpackG(color) * scaleRGB;
1393 b = GrColorUnpackB(color) * scaleRGB;
1394
1395 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001396 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001397 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001398 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001399}
1400
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001401void GrGpuGL::discard(GrRenderTarget* renderTarget) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001402 if (!this->caps()->discardRenderTargetSupport()) {
1403 return;
1404 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001405 if (NULL == renderTarget) {
1406 renderTarget = this->drawState()->getRenderTarget();
1407 if (NULL == renderTarget) {
1408 return;
1409 }
1410 }
1411
1412 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
1413 if (renderTarget != fHWBoundRenderTarget) {
1414 fHWBoundRenderTarget = NULL;
1415 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1416 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001417 switch (this->glCaps().invalidateFBType()) {
1418 case GrGLCaps::kNone_FBFetchType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001419 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001420 break;
1421 case GrGLCaps::kInvalidate_InvalidateFBType:
1422 if (0 == glRT->renderFBOID()) {
1423 // When rendering to the default framebuffer the legal values for attachments
1424 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1425 // types.
1426 static const GrGLenum attachments[] = { GR_GL_COLOR };
1427 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1428 attachments));
1429 } else {
1430 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1431 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1432 attachments));
1433 }
1434 break;
1435 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001436 if (0 == glRT->renderFBOID()) {
1437 // When rendering to the default framebuffer the legal values for attachments
1438 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1439 // types. See glDiscardFramebuffer() spec.
1440 static const GrGLenum attachments[] = { GR_GL_COLOR };
1441 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1442 attachments));
1443 } else {
1444 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1445 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1446 attachments));
1447 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001448 break;
1449 }
1450 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001451 renderTarget->flagAsResolved();
1452}
1453
1454
bsalomon@google.comedc177d2011-08-05 15:46:40 +00001455void GrGpuGL::clearStencil() {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001456 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001457 return;
1458 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001459
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001460 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001461
bsalomon@google.coma3201942012-06-21 19:58:20 +00001462 GrAutoTRestore<ScissorState> asr(&fScissorState);
1463 fScissorState.fEnabled = false;
1464 this->flushScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001465
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001466 GL_CALL(StencilMask(0xffffffff));
1467 GL_CALL(ClearStencil(0));
1468 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001469 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001470}
1471
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001472void GrGpuGL::clearStencilClip(const SkIRect& rect, bool insideClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001473 const GrDrawState& drawState = this->getDrawState();
1474 const GrRenderTarget* rt = drawState.getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001475 SkASSERT(NULL != rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001476
1477 // this should only be called internally when we know we have a
1478 // stencil buffer.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001479 SkASSERT(NULL != rt->getStencilBuffer());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 GrGLint stencilBitCount = rt->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001481#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001482 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001483 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001484#else
1485 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001486 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001487 // turned into draws. Our contract on GrDrawTarget says that
1488 // changing the clip between stencil passes may or may not
1489 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001490 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001491#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001492 GrGLint value;
1493 if (insideClip) {
1494 value = (1 << (stencilBitCount - 1));
1495 } else {
1496 value = 0;
1497 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001498 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001499
1500 GrAutoTRestore<ScissorState> asr(&fScissorState);
1501 fScissorState.fEnabled = true;
1502 fScissorState.fRect = rect;
1503 this->flushScissor();
1504
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001505 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001506 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001507 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001508 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001509}
1510
bsalomon@google.comc4364992011-11-07 15:54:49 +00001511bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1512 int left, int top,
1513 int width, int height,
1514 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001515 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001516 // If this rendertarget is aready TopLeft, we don't need to flip.
1517 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1518 return false;
1519 }
1520
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001521 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001522 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001523 return false;
1524 }
1525
1526 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001527 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001528 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001529 return true;
1530 }
1531 // If we have to do memcpys to handle rowBytes then y-flip is free
1532 // Note the rowBytes might be tight to the passed in data, but if data
1533 // gets clipped in x to the target the rowBytes will no longer be tight.
1534 if (left >= 0 && (left + width) < renderTarget->width()) {
1535 return 0 == rowBytes ||
1536 GrBytesPerPixel(config) * width == rowBytes;
1537 } else {
1538 return false;
1539 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001540}
1541
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001542bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001543 int left, int top,
1544 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001545 GrPixelConfig config,
1546 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001547 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001548 // We cannot read pixels into a compressed buffer
1549 if (GrPixelConfigIsCompressed(config)) {
1550 return false;
1551 }
1552
1553 GrGLenum format = 0;
1554 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001555 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001556 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001557 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001558 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001559 size_t bpp = GrBytesPerPixel(config);
1560 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1561 &left, &top, &width, &height,
1562 const_cast<const void**>(&buffer),
1563 &rowBytes)) {
1564 return false;
1565 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001566
bsalomon@google.comc6980972011-11-02 19:57:21 +00001567 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001568 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001569 GrDrawState::AutoRenderTargetRestore artr;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001570 switch (tgt->getResolveType()) {
1571 case GrGLRenderTarget::kCantResolve_ResolveType:
1572 return false;
1573 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001574 artr.set(this->drawState(), target);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001575 this->flushRenderTarget(&SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001576 break;
1577 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001578 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001579 // we don't track the state of the READ FBO ID.
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001580 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1581 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001582 break;
1583 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001584 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001585 }
1586
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001587 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001588
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001589 // the read rect is viewport-relative
1590 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001591 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001592
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001593 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001594 if (0 == rowBytes) {
1595 rowBytes = tightRowBytes;
1596 }
1597 size_t readDstRowBytes = tightRowBytes;
1598 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001599
bsalomon@google.comc6980972011-11-02 19:57:21 +00001600 // determine if GL can read using the passed rowBytes or if we need
1601 // a scratch buffer.
1602 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
1603 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001604 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001605 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001606 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001607 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001608 readDstRowBytes = rowBytes;
1609 } else {
1610 scratch.reset(tightRowBytes * height);
1611 readDst = scratch.get();
1612 }
1613 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001614 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001615 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1616 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001617 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1618 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001619 format, type, readDst));
1620 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001621 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001622 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1623 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001624 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001625 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001626 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001627 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001628
1629 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001630 // API presents top-to-bottom. We must preserve the padding contents. Note
1631 // that the above readPixels did not overwrite the padding.
1632 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001633 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001634 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001635 scratch.reset(tightRowBytes);
1636 void* tmpRow = scratch.get();
1637 // flip y in-place by rows
1638 const int halfY = height >> 1;
1639 char* top = reinterpret_cast<char*>(buffer);
1640 char* bottom = top + (height - 1) * rowBytes;
1641 for (int y = 0; y < halfY; y++) {
1642 memcpy(tmpRow, top, tightRowBytes);
1643 memcpy(top, bottom, tightRowBytes);
1644 memcpy(bottom, tmpRow, tightRowBytes);
1645 top += rowBytes;
1646 bottom -= rowBytes;
1647 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001648 }
1649 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001650 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001651 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001652 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001653 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001654 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001655 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001656 dst += (height-1) * rowBytes;
1657 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001658 for (int y = 0; y < height; y++) {
1659 memcpy(dst, src, tightRowBytes);
1660 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001661 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001662 dst += rowBytes;
1663 } else {
1664 dst -= rowBytes;
1665 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001666 }
1667 }
1668 return true;
1669}
1670
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001671void GrGpuGL::flushRenderTarget(const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001672
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001673 GrGLRenderTarget* rt =
1674 static_cast<GrGLRenderTarget*>(this->drawState()->getRenderTarget());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001675 SkASSERT(NULL != rt);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001676
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001677 if (fHWBoundRenderTarget != rt) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001678 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, rt->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001679#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001680 // don't do this check in Chromium -- this is causing
1681 // lots of repeated command buffer flushes when the compositor is
1682 // rendering with Ganesh, which is really slow; even too slow for
1683 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001684 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001685 GrGLenum status;
1686 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1687 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1688 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1689 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001691#endif
bsalomon@google.comc811ea32012-05-21 15:33:09 +00001692 fHWBoundRenderTarget = rt;
bsalomon@google.comd302f142011-03-03 13:54:13 +00001693 const GrGLIRect& vp = rt->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001694 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001695 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001696 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 }
1698 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001699 if (NULL == bound || !bound->isEmpty()) {
1700 rt->flagAsNeedingResolve(bound);
1701 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001702
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001703 GrTexture *texture = rt->asTexture();
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +00001704 if (NULL != texture) {
1705 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001706 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001707}
1708
twiz@google.com0f31ca72011-03-18 17:38:11 +00001709GrGLenum gPrimitiveType2GLMode[] = {
1710 GR_GL_TRIANGLES,
1711 GR_GL_TRIANGLE_STRIP,
1712 GR_GL_TRIANGLE_FAN,
1713 GR_GL_POINTS,
1714 GR_GL_LINES,
1715 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001716};
1717
bsalomon@google.comd302f142011-03-03 13:54:13 +00001718#define SWAP_PER_DRAW 0
1719
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001720#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001721 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001722 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001723 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001724 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001725 void SwapBuf() {
1726 DWORD procID = GetCurrentProcessId();
1727 HWND hwnd = GetTopWindow(GetDesktopWindow());
1728 while(hwnd) {
1729 DWORD wndProcID = 0;
1730 GetWindowThreadProcessId(hwnd, &wndProcID);
1731 if(wndProcID == procID) {
1732 SwapBuffers(GetDC(hwnd));
1733 }
1734 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1735 }
1736 }
1737 #endif
1738#endif
1739
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001740void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001741 size_t indexOffsetInBytes;
1742 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001743
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001744 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001745
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001746 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001747 GrGLvoid* indices =
1748 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001749 // info.startVertex() was accounted for by setupGeometry.
1750 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1751 info.indexCount(),
1752 GR_GL_UNSIGNED_SHORT,
1753 indices));
1754 } else {
1755 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1756 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1757 // accounted for startVertex.
1758 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1759 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001760#if SWAP_PER_DRAW
1761 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001762 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001763 aglSwapBuffers(aglGetCurrentContext());
1764 int set_a_break_pt_here = 9;
1765 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001766 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001767 SwapBuf();
1768 int set_a_break_pt_here = 9;
1769 SwapBuf();
1770 #endif
1771#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001772}
1773
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001774static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
1775 switch (op) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001776 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001777 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001778 /* fallthrough */;
1779 case kIncClamp_StencilOp:
1780 return GR_GL_COUNT_UP;
1781 case kInvert_StencilOp:
1782 return GR_GL_INVERT;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001783 }
1784}
1785
sugoi@google.com12b4e272012-12-06 20:13:11 +00001786void GrGpuGL::onGpuStencilPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001787 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001788
1789 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001790 SkASSERT(NULL != this->drawState()->getRenderTarget());
1791 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1792
1793 flushPathStencilSettings(fill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001794
1795 // Decide how to manipulate the stencil buffer based on the fill rule.
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001796 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1797
1798 GrGLenum fillMode =
1799 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1800 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001801 GL_CALL(StencilFillPath(id, fillMode, writeMask));
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00001802}
1803
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001804void GrGpuGL::onGpuDrawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001805 SkASSERT(this->caps()->pathRenderingSupport());
1806
1807 GrGLuint id = static_cast<const GrGLPath*>(path)->pathID();
1808 SkASSERT(NULL != this->drawState()->getRenderTarget());
1809 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1810 SkASSERT(!fCurrentProgram->hasVertexShader());
1811
1812 flushPathStencilSettings(fill);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001813 const SkStrokeRec& stroke = path->getStroke();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001814
1815 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(fill);
1816 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1817 GrGLenum fillMode =
1818 gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1819 GrGLint writeMask = fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001820
1821 if (stroke.isFillStyle() || SkStrokeRec::kStrokeAndFill_Style == stroke.getStyle()) {
1822 GL_CALL(StencilFillPath(id, fillMode, writeMask));
1823 }
1824 if (stroke.needToApply()) {
1825 GL_CALL(StencilStrokePath(id, 0xffff, writeMask));
1826 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001827
1828 if (nonInvertedFill == fill) {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +00001829 if (stroke.needToApply()) {
1830 GL_CALL(CoverStrokePath(id, GR_GL_BOUNDING_BOX));
1831 } else {
1832 GL_CALL(CoverFillPath(id, GR_GL_BOUNDING_BOX));
1833 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001834 } else {
1835 GrDrawState* drawState = this->drawState();
1836 GrDrawState::AutoViewMatrixRestore avmr;
1837 SkRect bounds = SkRect::MakeLTRB(0, 0,
1838 SkIntToScalar(drawState->getRenderTarget()->width()),
1839 SkIntToScalar(drawState->getRenderTarget()->height()));
1840 SkMatrix vmi;
1841 // mapRect through persp matrix may not be correct
1842 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1843 vmi.mapRect(&bounds);
1844 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1845 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001846 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001847 bounds.outset(bloat, bloat);
1848 } else {
1849 avmr.setIdentity(drawState);
1850 }
1851
1852 this->drawSimpleRect(bounds, NULL);
1853 }
1854}
1855
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +00001856void GrGpuGL::onGpuDrawPaths(int pathCount, const GrPath** paths,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001857 const SkMatrix* transforms,
1858 SkPath::FillType fill,
1859 SkStrokeRec::Style stroke) {
1860 SkASSERT(this->caps()->pathRenderingSupport());
1861 SkASSERT(NULL != this->drawState()->getRenderTarget());
1862 SkASSERT(NULL != this->drawState()->getRenderTarget()->getStencilBuffer());
1863 SkASSERT(!fCurrentProgram->hasVertexShader());
1864 SkASSERT(stroke != SkStrokeRec::kHairline_Style);
1865
1866 SkAutoMalloc pathData(pathCount * sizeof(GrGLuint));
1867 SkAutoMalloc transformData(pathCount * sizeof(GrGLfloat) * 6);
1868 GrGLfloat* transformValues =
1869 reinterpret_cast<GrGLfloat*>(transformData.get());
1870 GrGLuint* pathIDs = reinterpret_cast<GrGLuint*>(pathData.get());
1871
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +00001872 for (int i = 0; i < pathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001873 SkASSERT(transforms[i].asAffine(NULL));
1874 const SkMatrix& m = transforms[i];
1875 transformValues[i * 6] = m.getScaleX();
1876 transformValues[i * 6 + 1] = m.getSkewY();
1877 transformValues[i * 6 + 2] = m.getSkewX();
1878 transformValues[i * 6 + 3] = m.getScaleY();
1879 transformValues[i * 6 + 4] = m.getTranslateX();
1880 transformValues[i * 6 + 5] = m.getTranslateY();
1881 pathIDs[i] = static_cast<const GrGLPath*>(paths[i])->pathID();
1882 }
1883
1884 flushPathStencilSettings(fill);
1885
1886 SkPath::FillType nonInvertedFill =
1887 SkPath::ConvertToNonInverseFillType(fill);
1888
1889 SkASSERT(!fHWPathStencilSettings.isTwoSided());
1890 GrGLenum fillMode =
1891 gr_stencil_op_to_gl_path_rendering_fill_mode(
1892 fHWPathStencilSettings.passOp(GrStencilSettings::kFront_Face));
1893 GrGLint writeMask =
1894 fHWPathStencilSettings.writeMask(GrStencilSettings::kFront_Face);
1895
1896 bool doFill = stroke == SkStrokeRec::kFill_Style
1897 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1898 bool doStroke = stroke == SkStrokeRec::kStroke_Style
1899 || stroke == SkStrokeRec::kStrokeAndFill_Style;
1900
1901 if (doFill) {
1902 GL_CALL(StencilFillPathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1903 pathIDs, 0,
1904 fillMode, writeMask,
1905 GR_GL_AFFINE_2D, transformValues));
1906 }
1907 if (doStroke) {
1908 GL_CALL(StencilStrokePathInstanced(pathCount, GR_GL_UNSIGNED_INT,
1909 pathIDs, 0,
1910 0xffff, writeMask,
1911 GR_GL_AFFINE_2D, transformValues));
1912 }
1913
1914 if (nonInvertedFill == fill) {
1915 if (doStroke) {
1916 GL_CALL(CoverStrokePathInstanced(
1917 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1918 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1919 GR_GL_AFFINE_2D, transformValues));
1920 } else {
1921 GL_CALL(CoverFillPathInstanced(
1922 pathCount, GR_GL_UNSIGNED_INT, pathIDs, 0,
1923 GR_GL_BOUNDING_BOX_OF_BOUNDING_BOXES,
1924 GR_GL_AFFINE_2D, transformValues));
1925
1926 }
1927 } else {
1928 GrDrawState* drawState = this->drawState();
1929 GrDrawState::AutoViewMatrixRestore avmr;
1930 SkRect bounds = SkRect::MakeLTRB(0, 0,
1931 SkIntToScalar(drawState->getRenderTarget()->width()),
1932 SkIntToScalar(drawState->getRenderTarget()->height()));
1933 SkMatrix vmi;
1934 // mapRect through persp matrix may not be correct
1935 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
1936 vmi.mapRect(&bounds);
1937 // theoretically could set bloat = 0, instead leave it because of matrix inversion
1938 // precision.
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001939 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +00001940 bounds.outset(bloat, bloat);
1941 } else {
1942 avmr.setIdentity(drawState);
1943 }
1944
1945 this->drawSimpleRect(bounds, NULL);
1946 }
1947}
1948
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001949void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001950 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001951 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001952 // Some extensions automatically resolves the texture when it is read.
1953 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001954 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001955 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1956 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1957 // make sure we go through flushRenderTarget() since we've modified
1958 // the bound DRAW FBO ID.
1959 fHWBoundRenderTarget = NULL;
1960 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001961 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001962 GrGLIRect r;
1963 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1964 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001965
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001966 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001967 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001968 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001969 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001970 fScissorState.fEnabled = true;
1971 fScissorState.fRect = dirtyRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001972 this->flushScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001973 GL_CALL(ResolveMultisampleFramebuffer());
1974 } else {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001975 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001976 // this respects the scissor during the blit, so disable it.
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001977 asr.reset(&fScissorState);
1978 fScissorState.fEnabled = false;
1979 this->flushScissor();
1980 }
1981 int right = r.fLeft + r.fWidth;
1982 int top = r.fBottom + r.fHeight;
1983 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1984 r.fLeft, r.fBottom, right, top,
1985 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001986 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001987 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001988 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001989 }
1990}
1991
bsalomon@google.com411dad02012-06-05 20:24:20 +00001992namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001993
bsalomon@google.com411dad02012-06-05 20:24:20 +00001994GrGLenum gr_to_gl_stencil_func(GrStencilFunc basicFunc) {
1995 static const GrGLenum gTable[] = {
1996 GR_GL_ALWAYS, // kAlways_StencilFunc
1997 GR_GL_NEVER, // kNever_StencilFunc
1998 GR_GL_GREATER, // kGreater_StencilFunc
1999 GR_GL_GEQUAL, // kGEqual_StencilFunc
2000 GR_GL_LESS, // kLess_StencilFunc
2001 GR_GL_LEQUAL, // kLEqual_StencilFunc,
2002 GR_GL_EQUAL, // kEqual_StencilFunc,
2003 GR_GL_NOTEQUAL, // kNotEqual_StencilFunc,
2004 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002005 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002006 GR_STATIC_ASSERT(0 == kAlways_StencilFunc);
2007 GR_STATIC_ASSERT(1 == kNever_StencilFunc);
2008 GR_STATIC_ASSERT(2 == kGreater_StencilFunc);
2009 GR_STATIC_ASSERT(3 == kGEqual_StencilFunc);
2010 GR_STATIC_ASSERT(4 == kLess_StencilFunc);
2011 GR_STATIC_ASSERT(5 == kLEqual_StencilFunc);
2012 GR_STATIC_ASSERT(6 == kEqual_StencilFunc);
2013 GR_STATIC_ASSERT(7 == kNotEqual_StencilFunc);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002014 SkASSERT((unsigned) basicFunc < kBasicStencilFuncCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002015
2016 return gTable[basicFunc];
2017}
2018
2019GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2020 static const GrGLenum gTable[] = {
2021 GR_GL_KEEP, // kKeep_StencilOp
2022 GR_GL_REPLACE, // kReplace_StencilOp
2023 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2024 GR_GL_INCR, // kIncClamp_StencilOp
2025 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2026 GR_GL_DECR, // kDecClamp_StencilOp
2027 GR_GL_ZERO, // kZero_StencilOp
2028 GR_GL_INVERT, // kInvert_StencilOp
2029 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002030 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002031 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2032 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2033 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2034 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2035 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2036 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2037 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2038 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002039 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002040 return gTable[op];
2041}
2042
2043void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002044 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002045 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002046 GrStencilSettings::Face grFace) {
2047 GrGLenum glFunc = gr_to_gl_stencil_func(settings.func(grFace));
2048 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2049 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2050
2051 GrGLint ref = settings.funcRef(grFace);
2052 GrGLint mask = settings.funcMask(grFace);
2053 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002054
2055 if (GR_GL_FRONT_AND_BACK == glFace) {
2056 // we call the combined func just in case separate stencil is not
2057 // supported.
2058 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2059 GR_GL_CALL(gl, StencilMask(writeMask));
2060 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
2061 } else {
2062 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2063 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2064 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2065 }
2066}
2067}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002068
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002069void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002070 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002071 if (fStencilSettings.isDisabled()) {
2072 if (kNo_TriState != fHWStencilTestEnabled) {
2073 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2074 fHWStencilTestEnabled = kNo_TriState;
2075 }
2076 } else {
2077 if (kYes_TriState != fHWStencilTestEnabled) {
2078 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2079 fHWStencilTestEnabled = kYes_TriState;
2080 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002081 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002082 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002083 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002084 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002085 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002086 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002087 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002088 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002089 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002090 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002091 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002092 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002093 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00002094 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002095 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002096 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002097 }
2098 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002099 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002100 }
2101}
2102
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002103void GrGpuGL::flushAAState(DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002104// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2105// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2106#if 0
2107 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2108 #define RT_HAS_MSAA rt->isMultisampled()
2109#else
2110 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
2111#endif
2112
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00002113 const GrRenderTarget* rt = this->getDrawState().getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002114 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002115 // ES doesn't support toggling GL_MULTISAMPLE and doesn't have
2116 // smooth lines.
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002117 // we prefer smooth lines over multisampled lines
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002118 bool smoothLines = false;
2119
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002120 if (kDrawLines_DrawType == type) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002121 smoothLines = this->willUseHWAALines();
2122 if (smoothLines) {
2123 if (kYes_TriState != fHWAAState.fSmoothLineEnabled) {
2124 GL_CALL(Enable(GR_GL_LINE_SMOOTH));
2125 fHWAAState.fSmoothLineEnabled = kYes_TriState;
2126 // must disable msaa to use line smoothing
bsalomon@google.com202d1392013-03-19 18:58:08 +00002127 if (RT_HAS_MSAA &&
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002128 kNo_TriState != fHWAAState.fMSAAEnabled) {
2129 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2130 fHWAAState.fMSAAEnabled = kNo_TriState;
2131 }
2132 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002133 } else {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002134 if (kNo_TriState != fHWAAState.fSmoothLineEnabled) {
2135 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
2136 fHWAAState.fSmoothLineEnabled = kNo_TriState;
2137 }
2138 }
2139 }
bsalomon@google.com202d1392013-03-19 18:58:08 +00002140 if (!smoothLines && RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002141 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
2142 // convex hulls of each segment appear to get filled.
2143 bool enableMSAA = kStencilPath_DrawType == type ||
2144 this->getDrawState().isHWAntialiasState();
2145 if (enableMSAA) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002146 if (kYes_TriState != fHWAAState.fMSAAEnabled) {
2147 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2148 fHWAAState.fMSAAEnabled = kYes_TriState;
2149 }
2150 } else {
2151 if (kNo_TriState != fHWAAState.fMSAAEnabled) {
2152 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2153 fHWAAState.fMSAAEnabled = kNo_TriState;
2154 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002155 }
2156 }
2157 }
2158}
2159
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00002160void GrGpuGL::flushPathStencilSettings(SkPath::FillType fill) {
2161 GrStencilSettings pathStencilSettings;
2162 this->getPathStencilSettingsForFillType(fill, &pathStencilSettings);
2163 if (fHWPathStencilSettings != pathStencilSettings) {
2164 // Just the func, ref, and mask is set here. The op and write mask are params to the call
2165 // that draws the path to the SB (glStencilFillPath)
2166 GrGLenum func =
2167 gr_to_gl_stencil_func(pathStencilSettings.func(GrStencilSettings::kFront_Face));
2168 GL_CALL(PathStencilFunc(func,
2169 pathStencilSettings.funcRef(GrStencilSettings::kFront_Face),
2170 pathStencilSettings.funcMask(GrStencilSettings::kFront_Face)));
2171
2172 fHWPathStencilSettings = pathStencilSettings;
2173 }
2174}
2175
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002176void GrGpuGL::flushBlend(bool isLines,
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002177 GrBlendCoeff srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002178 GrBlendCoeff dstCoeff) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +00002179 if (isLines && this->willUseHWAALines()) {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002180 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002181 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002182 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002183 }
bsalomon@google.com47059542012-06-06 20:51:20 +00002184 if (kSA_GrBlendCoeff != fHWBlendState.fSrcCoeff ||
2185 kISA_GrBlendCoeff != fHWBlendState.fDstCoeff) {
2186 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[kSA_GrBlendCoeff],
2187 gXfermodeCoeff2Blend[kISA_GrBlendCoeff]));
2188 fHWBlendState.fSrcCoeff = kSA_GrBlendCoeff;
2189 fHWBlendState.fDstCoeff = kISA_GrBlendCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002190 }
2191 } else {
bsalomon@google.com86c1f712011-10-12 14:54:26 +00002192 // any optimization to disable blending should
2193 // have already been applied and tweaked the coeffs
2194 // to (1, 0).
bsalomon@google.com47059542012-06-06 20:51:20 +00002195 bool blendOff = kOne_GrBlendCoeff == srcCoeff &&
2196 kZero_GrBlendCoeff == dstCoeff;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002197 if (blendOff) {
2198 if (kNo_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002199 GL_CALL(Disable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002200 fHWBlendState.fEnabled = kNo_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002201 }
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002202 } else {
2203 if (kYes_TriState != fHWBlendState.fEnabled) {
2204 GL_CALL(Enable(GR_GL_BLEND));
2205 fHWBlendState.fEnabled = kYes_TriState;
2206 }
2207 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2208 fHWBlendState.fDstCoeff != dstCoeff) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002209 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2210 gXfermodeCoeff2Blend[dstCoeff]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002211 fHWBlendState.fSrcCoeff = srcCoeff;
2212 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002213 }
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002214 GrColor blendConst = this->getDrawState().getBlendConstant();
bsalomon@google.com271cffc2011-05-20 14:13:56 +00002215 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2216 BlendCoeffReferencesConstant(dstCoeff)) &&
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002217 (!fHWBlendState.fConstColorValid ||
2218 fHWBlendState.fConstColor != blendConst)) {
bsalomon@google.com75347472012-09-17 17:23:21 +00002219 GrGLfloat c[4];
2220 GrColorToRGBAFloat(blendConst, c);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002221 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002222 fHWBlendState.fConstColor = blendConst;
2223 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002224 }
2225 }
2226 }
2227}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002228
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002229static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002230 static const GrGLenum gWrapModes[] = {
2231 GR_GL_CLAMP_TO_EDGE,
2232 GR_GL_REPEAT,
2233 GR_GL_MIRRORED_REPEAT
2234 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002235 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002236 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2237 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2238 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2239 return gWrapModes[tm];
2240}
2241
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002242void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002243 SkASSERT(NULL != texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002244
bsalomon@google.comb8670992012-07-25 21:27:09 +00002245 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2246 // 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 +00002247 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002248 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002249 if (NULL != texRT) {
2250 this->onResolveRenderTarget(texRT);
2251 }
2252
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002253 if (fHWBoundTextures[unitIdx] != texture) {
2254 this->setTextureUnit(unitIdx);
2255 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
2256 fHWBoundTextures[unitIdx] = texture;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002257 }
2258
bsalomon@google.com4c883782012-06-04 19:05:11 +00002259 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002260 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002261 bool setAll = timestamp < this->getResetTimestamp();
2262 GrGLTexture::TexParams newTexParams;
2263
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002264 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002265 GR_GL_NEAREST,
2266 GR_GL_LINEAR,
2267 GR_GL_LINEAR_MIPMAP_LINEAR
2268 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002269 static GrGLenum glMagFilterModes[] = {
2270 GR_GL_NEAREST,
2271 GR_GL_LINEAR,
2272 GR_GL_LINEAR
2273 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002274 GrTextureParams::FilterMode filterMode = params.filterMode();
2275 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2276 filterMode = GrTextureParams::kBilerp_FilterMode;
2277 }
2278 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2279 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002280
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002281 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
2282 texture->mipMapsAreDirty() && !GrPixelConfigIsCompressed(texture->config())) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002283 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
2284 texture->dirtyMipMaps(false);
2285 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002286
bsalomon@google.comb8670992012-07-25 21:27:09 +00002287 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2288 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002289 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002290 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002291 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002292 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002293 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002294 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2295 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002296 newTexParams.fMagFilter));
2297 }
2298 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2299 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002300 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2301 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002302 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002303 }
2304 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002305 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002306 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2307 GR_GL_TEXTURE_WRAP_S,
2308 newTexParams.fWrapS));
2309 }
2310 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002311 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002312 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2313 GR_GL_TEXTURE_WRAP_T,
2314 newTexParams.fWrapT));
2315 }
2316 if (this->glCaps().textureSwizzleSupport() &&
2317 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2318 oldTexParams.fSwizzleRGBA,
2319 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002320 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002321 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002322 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2323 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2324 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2325 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2326 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2327 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2328 } else {
2329 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2330 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2331 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2332 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002333 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002334 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002335}
2336
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002337void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix,
2338 const SkISize& renderTargetSize,
2339 GrSurfaceOrigin renderTargetOrigin) {
2340
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002341 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002342
2343 if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
2344 renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
2345 matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
2346 return;
2347 }
2348
2349 fHWProjectionMatrixState.fViewMatrix = matrix;
2350 fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
2351 fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
2352
2353 GrGLfloat glMatrix[4 * 4];
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +00002354 fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix);
commit-bot@chromium.orgf6696722014-04-25 06:21:30 +00002355 GL_CALL(MatrixLoadf(GR_GL_PROJECTION, glMatrix));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002356}
2357
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002358void GrGpuGL::enablePathTexGen(int unitIdx,
2359 PathTexGenComponents components,
2360 const GrGLfloat* coefficients) {
2361 SkASSERT(this->glCaps().pathRenderingSupport());
2362 SkASSERT(components >= kS_PathTexGenComponents &&
2363 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002364 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= unitIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002365
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002366 if (GR_GL_OBJECT_LINEAR == fHWPathTexGenSettings[unitIdx].fMode &&
2367 components == fHWPathTexGenSettings[unitIdx].fNumComponents &&
2368 !memcmp(coefficients, fHWPathTexGenSettings[unitIdx].fCoefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002369 3 * components * sizeof(GrGLfloat))) {
2370 return;
2371 }
2372
2373 this->setTextureUnit(unitIdx);
2374
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002375 fHWPathTexGenSettings[unitIdx].fNumComponents = components;
2376 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + unitIdx,
2377 GR_GL_OBJECT_LINEAR,
2378 components,
2379 coefficients));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002380
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002381 memcpy(fHWPathTexGenSettings[unitIdx].fCoefficients, coefficients,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002382 3 * components * sizeof(GrGLfloat));
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002383}
2384
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002385void GrGpuGL::enablePathTexGen(int unitIdx, PathTexGenComponents components,
2386 const SkMatrix& matrix) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002387 GrGLfloat coefficients[3 * 3];
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002388 SkASSERT(this->glCaps().pathRenderingSupport());
2389 SkASSERT(components >= kS_PathTexGenComponents &&
2390 components <= kSTR_PathTexGenComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002391
2392 coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
2393 coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
2394 coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
2395
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002396 if (components >= kST_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002397 coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
2398 coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
2399 coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
2400 }
2401
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002402 if (components >= kSTR_PathTexGenComponents) {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002403 coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
2404 coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
2405 coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
2406 }
2407
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002408 enablePathTexGen(unitIdx, components, coefficients);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002409}
2410
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002411void GrGpuGL::flushPathTexGenSettings(int numUsedTexCoordSets) {
2412 SkASSERT(this->glCaps().pathRenderingSupport());
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +00002413 SkASSERT(this->glCaps().maxFixedFunctionTextureCoords() >= numUsedTexCoordSets);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002414
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002415 // Only write the inactive path tex gens, since active path tex gens were
2416 // written when they were enabled.
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002417
2418 SkDEBUGCODE(
2419 for (int i = 0; i < numUsedTexCoordSets; i++) {
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002420 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002421 }
commit-bot@chromium.org20807222013-11-01 11:54:54 +00002422 );
2423
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002424 for (int i = numUsedTexCoordSets; i < fHWActivePathTexGenSets; i++) {
2425 SkASSERT(0 != fHWPathTexGenSettings[i].fNumComponents);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002426
2427 this->setTextureUnit(i);
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002428 GL_CALL(PathTexGen(GR_GL_TEXTURE0 + i, GR_GL_NONE, 0, NULL));
2429 fHWPathTexGenSettings[i].fNumComponents = 0;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002430 }
2431
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +00002432 fHWActivePathTexGenSets = numUsedTexCoordSets;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +00002433}
2434
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002435void GrGpuGL::flushMiscFixedFunctionState() {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00002436
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002437 const GrDrawState& drawState = this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +00002438
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002439 if (drawState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002440 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002441 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002442 fHWDitherEnabled = kYes_TriState;
2443 }
2444 } else {
2445 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002446 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002447 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002448 }
2449 }
2450
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002451 if (drawState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002452 if (kNo_TriState != fHWWriteToColor) {
2453 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2454 GR_GL_FALSE, GR_GL_FALSE));
2455 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002456 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002457 } else {
2458 if (kYes_TriState != fHWWriteToColor) {
2459 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2460 fHWWriteToColor = kYes_TriState;
2461 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002462 }
2463
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002464 if (fHWDrawFace != drawState.getDrawFace()) {
bsalomon@google.coma5d056a2012-03-27 15:59:58 +00002465 switch (this->getDrawState().getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002466 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002467 GL_CALL(Enable(GR_GL_CULL_FACE));
2468 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002469 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002470 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002471 GL_CALL(Enable(GR_GL_CULL_FACE));
2472 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002473 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002474 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002475 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002476 break;
2477 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002478 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002479 }
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +00002480 fHWDrawFace = drawState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002481 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002482}
2483
reed@google.comac10a2d2010-12-22 21:39:39 +00002484void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002485 SkASSERT(NULL != renderTarget);
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002486 if (fHWBoundRenderTarget == renderTarget) {
2487 fHWBoundRenderTarget = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00002488 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002489}
2490
2491void GrGpuGL::notifyTextureDelete(GrGLTexture* texture) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002492 for (int s = 0; s < fHWBoundTextures.count(); ++s) {
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002493 if (fHWBoundTextures[s] == texture) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002494 // deleting bound texture does implied bind to 0
bsalomon@google.comc811ea32012-05-21 15:33:09 +00002495 fHWBoundTextures[s] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002496 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002497 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002498}
2499
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002500bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2501 bool getSizedInternalFormat,
2502 GrGLenum* internalFormat,
2503 GrGLenum* externalFormat,
2504 GrGLenum* externalType) {
2505 GrGLenum dontCare;
2506 if (NULL == internalFormat) {
2507 internalFormat = &dontCare;
2508 }
2509 if (NULL == externalFormat) {
2510 externalFormat = &dontCare;
2511 }
2512 if (NULL == externalType) {
2513 externalType = &dontCare;
2514 }
2515
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002516 if(!this->glCaps().isConfigTexturable(config)) {
2517 return false;
2518 }
2519
reed@google.comac10a2d2010-12-22 21:39:39 +00002520 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002521 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002522 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002523 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002524 if (getSizedInternalFormat) {
2525 *internalFormat = GR_GL_RGBA8;
2526 } else {
2527 *internalFormat = GR_GL_RGBA;
2528 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002529 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002530 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002531 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002532 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002533 if (getSizedInternalFormat) {
2534 *internalFormat = GR_GL_BGRA8;
2535 } else {
2536 *internalFormat = GR_GL_BGRA;
2537 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002538 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002539 if (getSizedInternalFormat) {
2540 *internalFormat = GR_GL_RGBA8;
2541 } else {
2542 *internalFormat = GR_GL_RGBA;
2543 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002544 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002545 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002546 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002547 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002548 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002549 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002550 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002551 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002552 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002553 return false;
2554 } else {
2555 *internalFormat = GR_GL_RGB565;
2556 }
2557 } else {
2558 *internalFormat = GR_GL_RGB;
2559 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002560 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002561 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002562 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002563 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002564 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002565 if (getSizedInternalFormat) {
2566 *internalFormat = GR_GL_RGBA4;
2567 } else {
2568 *internalFormat = GR_GL_RGBA;
2569 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002570 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002571 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002572 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002573 // glCompressedTexImage doesn't take external params
2574 *externalFormat = GR_GL_PALETTE8_RGBA8;
2575 // no sized/unsized internal format distinction here
2576 *internalFormat = GR_GL_PALETTE8_RGBA8;
2577 // unused with CompressedTexImage
2578 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002579 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002580 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002581 if (this->glCaps().textureRedSupport()) {
2582 *internalFormat = GR_GL_RED;
2583 *externalFormat = GR_GL_RED;
2584 if (getSizedInternalFormat) {
2585 *internalFormat = GR_GL_R8;
2586 } else {
2587 *internalFormat = GR_GL_RED;
2588 }
2589 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002590 } else {
2591 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002592 *externalFormat = GR_GL_ALPHA;
2593 if (getSizedInternalFormat) {
2594 *internalFormat = GR_GL_ALPHA8;
2595 } else {
2596 *internalFormat = GR_GL_ALPHA;
2597 }
2598 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002599 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002600 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002601 case kETC1_GrPixelConfig:
2602 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2603 break;
2604 case kLATC_GrPixelConfig:
2605 switch(this->glCaps().latcAlias()) {
2606 case GrGLCaps::kLATC_LATCAlias:
2607 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2608 break;
2609 case GrGLCaps::kRGTC_LATCAlias:
2610 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2611 break;
2612 case GrGLCaps::k3DC_LATCAlias:
2613 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2614 break;
2615 }
2616 break;
reed@google.comac10a2d2010-12-22 21:39:39 +00002617 default:
2618 return false;
2619 }
2620 return true;
2621}
2622
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002623void GrGpuGL::setTextureUnit(int unit) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002624 SkASSERT(unit >= 0 && unit < fHWBoundTextures.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002625 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002626 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002627 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002628 }
2629}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002630
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002631void GrGpuGL::setScratchTextureUnit() {
2632 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
2633 int lastUnitIdx = fHWBoundTextures.count() - 1;
2634 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2635 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2636 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002637 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002638 // clear out the this field so that if a program does use this unit it will rebind the correct
2639 // texture.
2640 fHWBoundTextures[lastUnitIdx] = NULL;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002641}
2642
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002643namespace {
2644// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002645inline bool can_blit_framebuffer(const GrSurface* dst,
2646 const GrSurface* src,
2647 const GrGpuGL* gpu,
2648 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002649 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2650 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002651 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002652 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2653 // or the rects are not the same (not just the same size but have the same edges).
2654 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2655 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2656 return false;
2657 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002658 if (NULL != wouldNeedTempFBO) {
2659 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2660 }
2661 return true;
2662 } else {
2663 return false;
2664 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002665}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002666
2667inline bool can_copy_texsubimage(const GrSurface* dst,
2668 const GrSurface* src,
2669 const GrGpuGL* gpu,
2670 bool* wouldNeedTempFBO = NULL) {
2671 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2672 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2673 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002674 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002675 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2676 return false;
2677 }
2678 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2679 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2680 // then we don't want to copy to the texture but to the MSAA buffer.
2681 if (NULL != dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
2682 return false;
2683 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002684 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2685 // If the src is multisampled (and uses an extension where there is a separate MSAA
2686 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
2687 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2688 return false;
2689 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002690 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
2691 NULL != dst->asTexture() &&
2692 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002693 kIndex_8_GrPixelConfig != src->config() &&
2694 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002695 if (NULL != wouldNeedTempFBO) {
2696 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2697 }
2698 return true;
2699 } else {
2700 return false;
2701 }
2702}
2703
2704// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2705// relative to is output.
2706inline GrGLuint bind_surface_as_fbo(const GrGLInterface* gl,
2707 GrSurface* surface,
2708 GrGLenum fboTarget,
2709 GrGLIRect* viewport) {
2710 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2711 GrGLuint tempFBOID;
2712 if (NULL == rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002713 SkASSERT(NULL != surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002714 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
2715 GR_GL_CALL(gl, GenFramebuffers(1, &tempFBOID));
2716 GR_GL_CALL(gl, BindFramebuffer(fboTarget, tempFBOID));
2717 GR_GL_CALL(gl, FramebufferTexture2D(fboTarget,
2718 GR_GL_COLOR_ATTACHMENT0,
2719 GR_GL_TEXTURE_2D,
2720 texID,
2721 0));
2722 viewport->fLeft = 0;
2723 viewport->fBottom = 0;
2724 viewport->fWidth = surface->width();
2725 viewport->fHeight = surface->height();
2726 } else {
2727 tempFBOID = 0;
2728 GR_GL_CALL(gl, BindFramebuffer(fboTarget, rt->renderFBOID()));
2729 *viewport = rt->getViewport();
2730 }
2731 return tempFBOID;
2732}
2733
2734}
2735
2736void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2737 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002738 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002739 kBGRA_8888_GrPixelConfig == src->config()) {
2740 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002741 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002742 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002743 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002744 } else if (NULL == src->asRenderTarget()) {
2745 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2746 // class handle it by rendering.
2747 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002748 return;
2749 }
2750
2751 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2752 if (NULL != srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
2753 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2754 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002755 } else {
2756 desc->fConfig = src->config();
2757 desc->fOrigin = src->origin();
2758 desc->fFlags = kNone_GrTextureFlags;
2759 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002760}
2761
2762bool GrGpuGL::onCopySurface(GrSurface* dst,
2763 GrSurface* src,
2764 const SkIRect& srcRect,
2765 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002766 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002767 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002768 bool wouldNeedTempFBO = false;
2769 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2770 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2771 GrGLuint srcFBO;
2772 GrGLIRect srcVP;
2773 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_FRAMEBUFFER, &srcVP);
2774 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002775 SkASSERT(NULL != dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002776 // We modified the bound FBO
2777 fHWBoundRenderTarget = NULL;
2778 GrGLIRect srcGLRect;
2779 srcGLRect.setRelativeTo(srcVP,
2780 srcRect.fLeft,
2781 srcRect.fTop,
2782 srcRect.width(),
2783 srcRect.height(),
2784 src->origin());
2785
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002786 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002787 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2788 GrGLint dstY;
2789 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2790 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2791 } else {
2792 dstY = dstPoint.fY;
2793 }
2794 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2795 dstPoint.fX, dstY,
2796 srcGLRect.fLeft, srcGLRect.fBottom,
2797 srcGLRect.fWidth, srcGLRect.fHeight));
2798 copied = true;
2799 if (srcFBO) {
2800 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2801 }
2802 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2803 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002804 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2805 srcRect.width(), srcRect.height());
2806 bool selfOverlap = false;
2807 if (dst->isSameAs(src)) {
2808 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2809 }
2810
2811 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002812 GrGLuint dstFBO;
2813 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002814 GrGLIRect dstVP;
2815 GrGLIRect srcVP;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002816 dstFBO = bind_surface_as_fbo(this->glInterface(), dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2817 srcFBO = bind_surface_as_fbo(this->glInterface(), src, GR_GL_READ_FRAMEBUFFER, &srcVP);
2818 // We modified the bound FBO
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002819 fHWBoundRenderTarget = NULL;
2820 GrGLIRect srcGLRect;
2821 GrGLIRect dstGLRect;
2822 srcGLRect.setRelativeTo(srcVP,
2823 srcRect.fLeft,
2824 srcRect.fTop,
2825 srcRect.width(),
2826 srcRect.height(),
2827 src->origin());
2828 dstGLRect.setRelativeTo(dstVP,
2829 dstRect.fLeft,
2830 dstRect.fTop,
2831 dstRect.width(),
2832 dstRect.height(),
2833 dst->origin());
2834
2835 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002836 if (GrGLCaps::kDesktop_EXT_MSFBOType == this->glCaps().msFBOType()) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002837 // The EXT version applies the scissor during the blit, so disable it.
2838 asr.reset(&fScissorState);
2839 fScissorState.fEnabled = false;
2840 this->flushScissor();
2841 }
2842 GrGLint srcY0;
2843 GrGLint srcY1;
2844 // Does the blit need to y-mirror or not?
2845 if (src->origin() == dst->origin()) {
2846 srcY0 = srcGLRect.fBottom;
2847 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2848 } else {
2849 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2850 srcY1 = srcGLRect.fBottom;
2851 }
2852 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2853 srcY0,
2854 srcGLRect.fLeft + srcGLRect.fWidth,
2855 srcY1,
2856 dstGLRect.fLeft,
2857 dstGLRect.fBottom,
2858 dstGLRect.fLeft + dstGLRect.fWidth,
2859 dstGLRect.fBottom + dstGLRect.fHeight,
2860 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2861 if (dstFBO) {
2862 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2863 }
2864 if (srcFBO) {
2865 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2866 }
2867 copied = true;
2868 }
2869 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002870 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002871 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002872 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002873 }
2874 return copied;
2875}
2876
2877bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2878 GrSurface* src,
2879 const SkIRect& srcRect,
2880 const SkIPoint& dstPoint) {
2881 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002882 if (can_copy_texsubimage(dst, src, this)) {
2883 return true;
2884 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002885 if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002886 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002887 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2888 srcRect.width(), srcRect.height());
2889 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2890 return true;
2891 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002892 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002893 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002894 }
2895 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002896 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002897}
2898
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002899void GrGpuGL::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002900 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002901 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
2902 SkString markerString = markerArray.toString();
2903 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002904 }
2905}
2906
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002907void GrGpuGL::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002908 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002909 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002910 }
2911}
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002912///////////////////////////////////////////////////////////////////////////////
2913
bsalomon@google.com6918d482013-03-07 19:09:11 +00002914GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2915 GrGpuGL* gpu,
2916 const GrGLVertexBuffer* vbuffer,
2917 const GrGLIndexBuffer* ibuffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002918 SkASSERT(NULL != vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002919 GrGLAttribArrayState* attribState;
2920
bsalomon@google.com6918d482013-03-07 19:09:11 +00002921 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2922 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002923 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002924 SkSafeUnref(fVBOVertexArray);
2925 GrGLuint arrayID;
2926 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2927 int attrCount = gpu->glCaps().maxVertexAttributes();
2928 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002929 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002930 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2931 } else {
2932 if (NULL != ibuffer) {
2933 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2934 } else {
2935 this->setVertexArrayID(gpu, 0);
2936 }
2937 int attrCount = gpu->glCaps().maxVertexAttributes();
2938 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2939 fDefaultVertexArrayAttribState.resize(attrCount);
2940 }
2941 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002942 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002943 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002944}