blob: be385110c9ab18999f044d750619f9f825df0d1c [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"
egdaniel170f90b2014-09-16 12:54:40 -070011#include "GrOptDrawState.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000012#include "GrTemplates.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000013#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000014#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000015#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000018#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
reed@google.comac10a2d2010-12-22 21:39:39 +000020#define SKIP_CACHE_CHECK true
21
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000022#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
23 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
24 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
25 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
30#endif
31
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000032
33///////////////////////////////////////////////////////////////////////////////
34
cdaltonb85a0aa2014-07-21 15:32:44 -070035
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
bsalomon1c63bf62014-07-22 13:09:46 -0700122 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000123
robertphillips@google.com6177e692013-02-28 20:16:25 +0000124 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000125 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000126 const GrGLubyte* vendor;
127 const GrGLubyte* renderer;
128 const GrGLubyte* version;
129 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
130 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
131 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000132 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
133 this);
134 GrPrintf("------ VENDOR %s\n", vendor);
135 GrPrintf("------ RENDERER %s\n", renderer);
136 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000137 GrPrintf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700138 ctx.extensions().print();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000139 GrPrintf("\n");
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000140 GrPrintf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000141 }
142
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000143 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000144
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000146
bsalomon@google.comfe676522011-06-17 18:12:21 +0000147 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000148 fHWProgramID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700149
150 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700151 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700152 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000153}
154
155GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000156 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000157 // detach the current program so there is no confusion on OpenGL's part
158 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000159 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000160 GL_CALL(UseProgram(0));
161 }
162
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000163 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000165 // This must be called by before the GrDrawTarget destructor
166 this->releaseGeometry();
bsalomonc8dc1f72014-08-21 13:02:13 -0700167}
168
robertphillipse3371302014-09-17 06:01:06 -0700169void GrGpuGL::contextAbandoned() {
170 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700171 fProgramCache->abandon();
172 fHWProgramID = 0;
173 if (this->glCaps().pathRenderingSupport()) {
174 this->glPathRendering()->abandonGpuResources();
175 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000176}
177
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000178///////////////////////////////////////////////////////////////////////////////
bungemanc7af8122014-07-16 09:10:41 -0700179
180
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000181GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
182 GrPixelConfig surfaceConfig) const {
183 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000184 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000185 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000186 GrBytesPerPixel(readConfig) == 4 &&
187 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000188 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
189 // Perhaps this should be guarded by some compiletime or runtime check.
190 return surfaceConfig;
bungemanc7af8122014-07-16 09:10:41 -0700191 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
192 !this->glCaps().readPixelsSupported(this->glInterface(),
193 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000194 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000195 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000196 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000197 }
198}
199
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000200GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
201 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000202 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
203 return kBGRA_8888_GrPixelConfig;
204 } else {
205 return writeConfig;
206 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000207}
208
209bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700210 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000211 return false;
212 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000213 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000214 // In general ES2 requires the internal format of the texture and the format of the src
215 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
216 // texture. It depends upon which extension added BGRA. The Apple extension allows it
217 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
218 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000219 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000220 !this->glCaps().bgraIsInternalFormat() &&
221 kBGRA_8888_GrPixelConfig == srcConfig &&
222 kRGBA_8888_GrPixelConfig == texture->config()) {
223 return true;
224 } else {
225 return false;
226 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000227 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000228 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000229 }
230}
231
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000232bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
233 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
234}
235
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000236void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000237 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000238 if (resetBits & kMisc_GrGLBackendState) {
239 GL_CALL(Disable(GR_GL_DEPTH_TEST));
240 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000241
egdaniel89af44a2014-09-26 06:15:04 -0700242 fHWDrawFace = GrOptDrawState::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000243 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000245 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000246 // Desktop-only state that we never change
247 if (!this->glCaps().isCoreProfile()) {
248 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
249 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
250 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
251 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
252 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
253 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
254 }
255 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
256 // core profile. This seems like a bug since the core spec removes any mention of
257 // GL_ARB_imaging.
258 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
259 GL_CALL(Disable(GR_GL_COLOR_TABLE));
260 }
261 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
262 // Since ES doesn't support glPointSize at all we always use the VS to
263 // set the point size
264 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
265
266 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
267 // currently part of our gl interface. There are probably others as
268 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000269 }
joshualitt58162332014-08-01 06:44:53 -0700270
271 if (kGLES_GrGLStandard == this->glStandard() &&
272 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
273 // The arm extension requires specifically enabling MSAA fetching per sample.
274 // On some devices this may have a perf hit. Also multiple render targets are disabled
275 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
276 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000277 fHWWriteToColor = kUnknown_TriState;
278 // we only ever use lines in hairline mode
279 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000280 }
edisonn@google.comba669992013-06-28 16:03:21 +0000281
egdanielb414f252014-07-29 13:15:47 -0700282 if (resetBits & kMSAAEnable_GrGLBackendState) {
283 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000284 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000285
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000286 fHWActiveTextureUnitIdx = -1; // invalid
287
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000288 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700289 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
290 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000291 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000292 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000293
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000294 if (resetBits & kBlend_GrGLBackendState) {
295 fHWBlendState.invalidate();
296 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000297
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000298 if (resetBits & kView_GrGLBackendState) {
299 fHWScissorSettings.invalidate();
300 fHWViewport.invalidate();
301 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000302
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000303 if (resetBits & kStencil_GrGLBackendState) {
304 fHWStencilSettings.invalidate();
305 fHWStencilTestEnabled = kUnknown_TriState;
306 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000307
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000308 // Vertex
309 if (resetBits & kVertex_GrGLBackendState) {
310 fHWGeometryState.invalidate();
311 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000313 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700314 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000315 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000316
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000317 if (resetBits & kPathRendering_GrGLBackendState) {
318 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700319 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000320 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000321 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000322
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000323 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000324 if (resetBits & kPixelStore_GrGLBackendState) {
325 if (this->glCaps().unpackRowLengthSupport()) {
326 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
327 }
328 if (this->glCaps().packRowLengthSupport()) {
329 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
330 }
331 if (this->glCaps().unpackFlipYSupport()) {
332 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
333 }
334 if (this->glCaps().packFlipYSupport()) {
335 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
336 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000337 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000338
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000339 if (resetBits & kProgram_GrGLBackendState) {
340 fHWProgramID = 0;
341 fSharedGLProgramState.invalidate();
342 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000343}
344
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000345namespace {
346
347GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
348 // By default, GrRenderTargets are GL's normal orientation so that they
349 // can be drawn to by the outside world without the client having
350 // to render upside down.
351 if (kDefault_GrSurfaceOrigin == origin) {
352 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
353 } else {
354 return origin;
355 }
356}
357
358}
359
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000360GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000361 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000362 return NULL;
363 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000364
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000365 if (0 == desc.fTextureHandle) {
366 return NULL;
367 }
368
bsalomon@google.combcce8922013-03-25 15:38:39 +0000369 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000370 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
371 return NULL;
372 }
373
374 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000375 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000376 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000377 glTexDesc.fWidth = desc.fWidth;
378 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000379 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000380 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000381 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000382 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000383 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000384 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
385 // assuming the old behaviour, which is that backend textures are always
386 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
387 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
388 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
389 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
390 } else {
391 glTexDesc.fOrigin = desc.fOrigin;
392 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000393
394 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000395 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000396 GrGLRenderTarget::Desc glRTDesc;
397 glRTDesc.fRTFBOID = 0;
398 glRTDesc.fTexFBOID = 0;
399 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000400 glRTDesc.fConfig = desc.fConfig;
401 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000402 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000403 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000404 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
405 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000406 glTexDesc.fTextureID,
407 &glRTDesc)) {
408 return NULL;
409 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000410 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000411 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000412 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000413 }
414 if (NULL == texture) {
415 return NULL;
416 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000417
bsalomon@google.come269f212011-11-07 13:29:52 +0000418 return texture;
419}
420
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000421GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000422 GrGLRenderTarget::Desc glDesc;
423 glDesc.fConfig = desc.fConfig;
424 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
425 glDesc.fMSColorRenderbufferID = 0;
426 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
427 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000428 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000429 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000430
431 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000432 GrGLIRect viewport;
433 viewport.fLeft = 0;
434 viewport.fBottom = 0;
435 viewport.fWidth = desc.fWidth;
436 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000438 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
439 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000440 if (desc.fStencilBits) {
441 GrGLStencilBuffer::Format format;
442 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
443 format.fPacked = false;
444 format.fStencilBits = desc.fStencilBits;
445 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000446 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000447 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
448 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000449 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000450 0,
451 desc.fWidth,
452 desc.fHeight,
453 desc.fSampleCnt,
454 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000455 tgt->setStencilBuffer(sb);
456 sb->unref();
457 }
458 return tgt;
459}
460
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000461////////////////////////////////////////////////////////////////////////////////
462
bsalomon@google.com9c680582013-02-06 18:17:50 +0000463bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000464 int left, int top, int width, int height,
465 GrPixelConfig config, const void* buffer,
466 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000467 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000468 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000469 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000470 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
471
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000472 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000473 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
474 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000475 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000476 desc.fWidth = glTex->width();
477 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000478 desc.fConfig = glTex->config();
479 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000480 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000481 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000482
krajcevski145d48c2014-06-11 16:07:50 -0700483 bool success = false;
484 if (GrPixelConfigIsCompressed(desc.fConfig)) {
485 // We check that config == desc.fConfig in GrGpuGL::canWriteTexturePixels()
486 SkASSERT(config == desc.fConfig);
487 success = this->uploadCompressedTexData(desc, buffer, false,
488 left, top, width, height);
489 } else {
490 success = this->uploadTexData(desc, false,
491 left, top, width, height,
492 config, buffer, rowBytes);
493 }
494
495 if (success) {
robertphillipsb06e5a22014-09-30 06:58:20 -0700496 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000497 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000498 }
krajcevski145d48c2014-06-11 16:07:50 -0700499
500 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000501}
502
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000503namespace {
504bool adjust_pixel_ops_params(int surfaceWidth,
505 int surfaceHeight,
506 size_t bpp,
507 int* left, int* top, int* width, int* height,
508 const void** data,
509 size_t* rowBytes) {
510 if (!*rowBytes) {
511 *rowBytes = *width * bpp;
512 }
513
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000514 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
515 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000516
517 if (!subRect.intersect(bounds)) {
518 return false;
519 }
520 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
521 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
522
523 *left = subRect.fLeft;
524 *top = subRect.fTop;
525 *width = subRect.width();
526 *height = subRect.height();
527 return true;
528}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000529
530GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
531 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
532 return GR_GL_GET_ERROR(interface);
533 } else {
534 return CHECK_ALLOC_ERROR(interface);
535 }
536}
537
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000538}
539
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000540bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
541 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 int left, int top, int width, int height,
543 GrPixelConfig dataConfig,
544 const void* data,
545 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700546 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000547
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000548 // If we're uploading compressed data then we should be using uploadCompressedTexData
549 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
550
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000551 size_t bpp = GrBytesPerPixel(dataConfig);
552 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
553 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000554 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000555 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000556 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000557
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000558 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700559 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000560
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000561 // We currently lazily create MIPMAPs when the we see a draw with
562 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
563 // MIP levels are all created when the texture is created. So for now we don't use
564 // texture storage.
565 bool useTexStorage = false &&
566 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000567 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000568
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000569 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000570 // 565 is not a sized internal format on desktop GL. So on desktop with
571 // 565 we always use an unsized internal format to let the system pick
572 // the best sized format to convert the 565 data to. Since TexStorage
573 // only allows sized internal formats we will instead use TexImage2D.
574 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000575 }
576
577 GrGLenum internalFormat;
bsalomone904c092014-07-17 10:50:59 -0700578 GrGLenum externalFormat = 0x0; // suprress warning
579 GrGLenum externalType = 0x0;// suprress warning
580
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000581 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
582 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
583 // size of the internal format whenever possible and so only use a sized internal format when
584 // using texture storage.
bsalomone904c092014-07-17 10:50:59 -0700585 bool useSizedFormat = useTexStorage;
586 // At least some versions of the desktop ES3 drivers for NVIDIA won't accept GL_RED in
587 // glTexImage2D for the internal format but will accept GL_R8.
588 if (!useSizedFormat && kNVIDIA_GrGLVendor == this->glContext().vendor() &&
589 kGLES_GrGLStandard == this->glStandard() && this->glVersion() >= GR_GL_VER(3, 0)) {
590 useSizedFormat = true;
591 }
592 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000593 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000594 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000595 }
596
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000597 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000598 * check whether to allocate a temporary buffer for flipping y or
599 * because our srcData has extra bytes past each row. If so, we need
600 * to trim those off here, since GL ES may not let us specify
601 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000602 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000603 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000604 bool swFlipY = false;
605 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700606 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000607 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000608 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000609 glFlipY = true;
610 } else {
611 swFlipY = true;
612 }
613 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000614 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000615 // can't use this for flipping, only non-neg values allowed. :(
616 if (rowBytes != trimRowBytes) {
617 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
618 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
619 restoreGLRowLength = true;
620 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000621 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000622 if (trimRowBytes != rowBytes || swFlipY) {
623 // copy data into our new storage, skipping the trailing bytes
624 size_t trimSize = height * trimRowBytes;
625 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000627 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000628 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 char* dst = (char*)tempStorage.reset(trimSize);
630 for (int y = 0; y < height; y++) {
631 memcpy(dst, src, trimRowBytes);
632 if (swFlipY) {
633 src -= rowBytes;
634 } else {
635 src += rowBytes;
636 }
637 dst += trimRowBytes;
638 }
639 // now point data to our copied version
640 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000641 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000642 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000643 if (glFlipY) {
644 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
645 }
joshualittee5da552014-07-16 13:32:56 -0700646 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
647 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000648 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000649 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000650 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000651 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000652 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000653 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000654 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000655 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000656 GL_ALLOC_CALL(this->glInterface(),
657 TexStorage2D(GR_GL_TEXTURE_2D,
658 1, // levels
659 internalFormat,
660 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000661 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700662 GL_ALLOC_CALL(this->glInterface(),
663 TexImage2D(GR_GL_TEXTURE_2D,
664 0, // level
665 internalFormat,
666 desc.fWidth, desc.fHeight,
667 0, // border
668 externalFormat, externalType,
669 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000670 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000671 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000672 if (error != GR_GL_NO_ERROR) {
673 succeeded = false;
674 } else {
675 // if we have data and we used TexStorage to create the texture, we
676 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700677 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000678 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
679 0, // level
680 left, top,
681 width, height,
682 externalFormat, externalType,
683 data));
684 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000685 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000686 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000687 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000688 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000689 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000690 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
691 0, // level
692 left, top,
693 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000694 externalFormat, externalType, data));
695 }
696
697 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000698 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000699 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000700 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000701 if (glFlipY) {
702 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
703 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000704 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000705}
706
krajcevski145d48c2014-06-11 16:07:50 -0700707// TODO: This function is using a lot of wonky semantics like, if width == -1
bungemanc7af8122014-07-16 09:10:41 -0700708// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700709// create a CompressedTexData struct that takes a desc/ptr and figures out
710// the proper upload semantics. Then users can construct this function how they
711// see fit if they want to go against the "standard" way to do it.
krajcevski9c0e6292014-06-02 07:38:14 -0700712bool GrGpuGL::uploadCompressedTexData(const GrGLTexture::Desc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700713 const void* data,
714 bool isNewTexture,
715 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700716 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700717
718 // No support for software flip y, yet...
719 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
720
krajcevski145d48c2014-06-11 16:07:50 -0700721 if (-1 == width) {
722 width = desc.fWidth;
723 }
724#ifdef SK_DEBUG
725 else {
726 SkASSERT(width <= desc.fWidth);
727 }
728#endif
729
730 if (-1 == height) {
731 height = desc.fHeight;
732 }
733#ifdef SK_DEBUG
734 else {
735 SkASSERT(height <= desc.fHeight);
736 }
737#endif
738
krajcevski9c0e6292014-06-02 07:38:14 -0700739 // Make sure that the width and height that we pass to OpenGL
740 // is a multiple of the block size.
krajcevski145d48c2014-06-11 16:07:50 -0700741 int dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700742
743 // We only need the internal format for compressed 2D textures.
744 GrGLenum internalFormat = 0;
745 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
746 return false;
747 }
748
krajcevski145d48c2014-06-11 16:07:50 -0700749 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700750 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700751 GL_ALLOC_CALL(this->glInterface(),
752 CompressedTexImage2D(GR_GL_TEXTURE_2D,
753 0, // level
754 internalFormat,
755 width, height,
756 0, // border
757 dataSize,
758 data));
bsalomond4cb9222014-08-11 14:19:09 -0700759 GrGLenum error = check_alloc_error(desc, this->glInterface());
760 if (error != GR_GL_NO_ERROR) {
761 return false;
762 }
krajcevski145d48c2014-06-11 16:07:50 -0700763 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700764 // Paletted textures can't be updated.
765 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
766 return false;
767 }
768 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
769 0, // level
770 left, top,
771 width, height,
772 internalFormat,
773 dataSize,
774 data));
krajcevski145d48c2014-06-11 16:07:50 -0700775 }
krajcevski9c0e6292014-06-02 07:38:14 -0700776
bsalomond4cb9222014-08-11 14:19:09 -0700777 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700778}
779
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000780static bool renderbuffer_storage_msaa(GrGLContext& ctx,
781 int sampleCount,
782 GrGLenum format,
783 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000784 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000785 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000786 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000787 case GrGLCaps::kDesktop_ARB_MSFBOType:
788 case GrGLCaps::kDesktop_EXT_MSFBOType:
789 case GrGLCaps::kES_3_0_MSFBOType:
790 GL_ALLOC_CALL(ctx.interface(),
791 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
792 sampleCount,
793 format,
794 width, height));
795 break;
796 case GrGLCaps::kES_Apple_MSFBOType:
797 GL_ALLOC_CALL(ctx.interface(),
798 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
799 sampleCount,
800 format,
801 width, height));
802 break;
803 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
804 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
805 GL_ALLOC_CALL(ctx.interface(),
806 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
807 sampleCount,
808 format,
809 width, height));
810 break;
811 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000812 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000813 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000814 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000815 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000816}
817
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000818bool GrGpuGL::createRenderTargetObjects(int width, int height,
819 GrGLuint texID,
820 GrGLRenderTarget::Desc* desc) {
821 desc->fMSColorRenderbufferID = 0;
822 desc->fRTFBOID = 0;
823 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000824 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000825
826 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827
bsalomon@google.comab15d612011-08-09 12:57:56 +0000828 GrGLenum msColorFormat = 0; // suppress warning
829
bsalomon@google.com347c3822013-05-01 20:10:01 +0000830 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
831 goto FAILED;
832 }
833
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000834 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000835 if (!desc->fTexFBOID) {
836 goto FAILED;
837 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000838
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000839
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000840 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
841 // the texture bound to the other. The exception is the IMG multisample extension. With this
842 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
843 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000844 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000845 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
846 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000847 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000848 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000849 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000850 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000851 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000852 &msColorFormat,
853 NULL,
854 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000855 goto FAILED;
856 }
857 } else {
858 desc->fRTFBOID = desc->fTexFBOID;
859 }
860
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000861 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700862 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000863 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000864 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000865 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000867 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000868 desc->fSampleCnt,
869 msColorFormat,
870 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000871 goto FAILED;
872 }
robertphillips754f4e92014-09-18 13:52:08 -0700873 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000874 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000875 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 GR_GL_COLOR_ATTACHMENT0,
877 GR_GL_RENDERBUFFER,
878 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000879 if (desc->fCheckAllocation ||
880 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000881 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
882 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
883 goto FAILED;
884 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000885 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000886 }
887 }
robertphillips754f4e92014-09-18 13:52:08 -0700888 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000889 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000890
bsalomon@google.com347c3822013-05-01 20:10:01 +0000891 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000892 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
893 GR_GL_COLOR_ATTACHMENT0,
894 GR_GL_TEXTURE_2D,
895 texID, 0, desc->fSampleCnt));
896 } else {
897 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
898 GR_GL_COLOR_ATTACHMENT0,
899 GR_GL_TEXTURE_2D,
900 texID, 0));
901 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000902 if (desc->fCheckAllocation ||
903 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000904 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
905 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
906 goto FAILED;
907 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000908 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 }
910
911 return true;
912
913FAILED:
914 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000915 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000916 }
917 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000918 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000919 }
920 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000921 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000922 }
923 return false;
924}
925
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000926// good to set a break-point here to know when createTexture fails
927static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000928// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000929 return NULL;
930}
931
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000932#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000933static size_t as_size_t(int x) {
934 return x;
935}
936#endif
937
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000938GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000939 const void* srcData,
940 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000941
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000942 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000943 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000944
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000945 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000946 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000947 // We fail if the MSAA was requested and is not available.
948 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
949 //GrPrintf("MSAA RT requested but not supported on this platform.");
950 return return_null_texture();
951 }
952 // If the sample count exceeds the max then we clamp it.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000953 glTexDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000954
robertphillips@google.com32716282012-06-04 12:48:45 +0000955 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000956 glTexDesc.fWidth = desc.fWidth;
957 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000958 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000959 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000960
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000961 glRTDesc.fMSColorRenderbufferID = 0;
962 glRTDesc.fRTFBOID = 0;
963 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000964 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000965 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000966 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000967
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000968 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000969
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000970 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
971 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000972
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000973 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000974 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000975 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000976 //GrPrintf("MSAA RT requested but not supported on this platform.");
977 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000978 }
979
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000981 int maxRTSize = this->caps()->maxRenderTargetSize();
982 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000983 return return_null_texture();
984 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000985 } else {
986 int maxSize = this->caps()->maxTextureSize();
987 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
988 return return_null_texture();
989 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 }
991
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000992 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000993
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000994 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000995 return return_null_texture();
996 }
997
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000998 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001000
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001001 if (renderTarget && this->glCaps().textureUsageSupport()) {
1002 // provides a hint about how this texture will be used
1003 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1004 GR_GL_TEXTURE_USAGE,
1005 GR_GL_FRAMEBUFFER_ATTACHMENT));
1006 }
1007
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001008 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1009 // drivers have a bug where an FBO won't be complete if it includes a
1010 // texture that is not mipmap complete (considering the filter in use).
1011 GrGLTexture::TexParams initialTexParams;
1012 // we only set a subset here so invalidate first
1013 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001014 initialTexParams.fMinFilter = GR_GL_NEAREST;
1015 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001016 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1017 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001018 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1019 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001020 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001021 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1022 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001023 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001024 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1025 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001026 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001027 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1028 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001029 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001030 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1031 glTexDesc.fWidth, glTexDesc.fHeight,
1032 desc.fConfig, srcData, rowBytes)) {
1033 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1034 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001035 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001036
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001037 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001038 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001039 // unbind the texture from the texture unit before binding it to the frame buffer
1040 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1041
bsalomon@google.com99621082011-11-15 16:47:16 +00001042 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1043 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001044 glTexDesc.fTextureID,
1045 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001046 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 return return_null_texture();
1048 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001049 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001050 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001051 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001053 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001054#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001055 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1056 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001057#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001058 return tex;
1059}
1060
krajcevski9c0e6292014-06-02 07:38:14 -07001061GrTexture* GrGpuGL::onCreateCompressedTexture(const GrTextureDesc& desc,
1062 const void* srcData) {
1063
1064 if(SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
1065 return return_null_texture();
1066 }
1067
1068 // Make sure that we're not flipping Y.
1069 GrSurfaceOrigin texOrigin = resolve_origin(desc.fOrigin, false);
1070 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1071 return return_null_texture();
1072 }
1073
1074 GrGLTexture::Desc glTexDesc;
1075
1076 glTexDesc.fFlags = desc.fFlags;
1077 glTexDesc.fWidth = desc.fWidth;
1078 glTexDesc.fHeight = desc.fHeight;
1079 glTexDesc.fConfig = desc.fConfig;
1080 glTexDesc.fIsWrapped = false;
1081 glTexDesc.fOrigin = texOrigin;
1082
1083 int maxSize = this->caps()->maxTextureSize();
1084 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1085 return return_null_texture();
1086 }
1087
1088 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
1089
1090 if (!glTexDesc.fTextureID) {
1091 return return_null_texture();
1092 }
1093
1094 this->setScratchTextureUnit();
1095 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1096
1097 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1098 // drivers have a bug where an FBO won't be complete if it includes a
1099 // texture that is not mipmap complete (considering the filter in use).
1100 GrGLTexture::TexParams initialTexParams;
1101 // we only set a subset here so invalidate first
1102 initialTexParams.invalidate();
1103 initialTexParams.fMinFilter = GR_GL_NEAREST;
1104 initialTexParams.fMagFilter = GR_GL_NEAREST;
1105 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1106 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1107 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1108 GR_GL_TEXTURE_MAG_FILTER,
1109 initialTexParams.fMagFilter));
1110 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1111 GR_GL_TEXTURE_MIN_FILTER,
1112 initialTexParams.fMinFilter));
1113 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1114 GR_GL_TEXTURE_WRAP_S,
1115 initialTexParams.fWrapS));
1116 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1117 GR_GL_TEXTURE_WRAP_T,
1118 initialTexParams.fWrapT));
1119
1120 if (!this->uploadCompressedTexData(glTexDesc, srcData)) {
1121 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1122 return return_null_texture();
1123 }
1124
1125 GrGLTexture* tex;
1126 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
1127 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1128#ifdef TRACE_TEXTURE_CREATION
1129 GrPrintf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
1130 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1131#endif
1132 return tex;
1133}
1134
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001136
1137const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1138
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001140 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001141
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001143 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 (kUnknownBitCount == format->fTotalBits));
1145 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001146 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1148 (GrGLint*)&format->fStencilBits);
1149 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001150 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001151 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1152 (GrGLint*)&format->fTotalBits);
1153 format->fTotalBits += format->fStencilBits;
1154 } else {
1155 format->fTotalBits = format->fStencilBits;
1156 }
1157 }
1158}
1159}
1160
1161bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1162 int width, int height) {
1163
1164 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001165 // 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 +00001166 SkASSERT(rt->asTexture());
1167 SkASSERT(width >= rt->width());
1168 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169
1170 int samples = rt->numSamples();
1171 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001172 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 if (!sbID) {
1174 return false;
1175 }
1176
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001177 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 // we start with the last stencil format that succeeded in hopes
1181 // that we won't go through this loop more than once after the
1182 // first (painful) stencil creation.
1183 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001184 const GrGLCaps::StencilFormat& sFmt =
1185 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001186 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001187 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001189 bool created;
1190 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001191 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001192 samples,
1193 sFmt.fInternalFormat,
1194 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001196 GL_ALLOC_CALL(this->glInterface(),
1197 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001198 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001199 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001200 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001201 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001203 if (created) {
1204 // After sized formats we attempt an unsized format and take
1205 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001206 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001207 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001208 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001209 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001210 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001211 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1213 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001214 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001215 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 return true;
1217 }
1218 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 }
1220 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001221 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001222 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223}
1224
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001225bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1227
1228 GrGLuint fbo = glrt->renderFBOID();
1229
1230 if (NULL == sb) {
bsalomon49f085d2014-09-05 13:34:00 -07001231 if (rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001232 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001233 GR_GL_STENCIL_ATTACHMENT,
1234 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001236 GR_GL_DEPTH_ATTACHMENT,
1237 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001238#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001239 GrGLenum status;
1240 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001241 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242#endif
1243 }
1244 return true;
1245 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001246 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 GrGLuint rb = glsb->renderbufferID();
1248
bsalomon1c63bf62014-07-22 13:09:46 -07001249 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001250 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001251 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1252 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001253 GR_GL_STENCIL_ATTACHMENT,
1254 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001257 GR_GL_DEPTH_ATTACHMENT,
1258 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001260 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001261 GR_GL_DEPTH_ATTACHMENT,
1262 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001265 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001266 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001267 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1268 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001269 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001270 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001272 if (glsb->format().fPacked) {
1273 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1274 GR_GL_DEPTH_ATTACHMENT,
1275 GR_GL_RENDERBUFFER, 0));
1276 }
1277 return false;
1278 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001279 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001280 rt->config(),
1281 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001282 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001283 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001284 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001286}
1287
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001288////////////////////////////////////////////////////////////////////////////////
1289
robertphillips@google.comadacc702013-10-14 21:53:24 +00001290GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001291 GrGLVertexBuffer::Desc desc;
1292 desc.fDynamic = dynamic;
1293 desc.fSizeInBytes = size;
1294 desc.fIsWrapped = false;
1295
bsalomon@google.com96966a52013-02-21 16:34:21 +00001296 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001297 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001298 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001300 } else {
1301 GL_CALL(GenBuffers(1, &desc.fID));
1302 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001303 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001304 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1305 // make sure driver can allocate memory for this buffer
1306 GL_ALLOC_CALL(this->glInterface(),
1307 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001308 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001309 NULL, // data ptr
1310 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1311 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1312 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001313 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001314 return NULL;
1315 }
1316 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1317 return vertexBuffer;
1318 }
1319 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001321}
1322
robertphillips@google.comadacc702013-10-14 21:53:24 +00001323GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001324 GrGLIndexBuffer::Desc desc;
1325 desc.fDynamic = dynamic;
1326 desc.fSizeInBytes = size;
1327 desc.fIsWrapped = false;
1328
bsalomon@google.com96966a52013-02-21 16:34:21 +00001329 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001330 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001331 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001333 } else {
1334 GL_CALL(GenBuffers(1, &desc.fID));
1335 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001336 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001337 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1338 // make sure driver can allocate memory for this buffer
1339 GL_ALLOC_CALL(this->glInterface(),
1340 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001341 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001342 NULL, // data ptr
1343 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1344 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1345 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001346 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001347 return NULL;
1348 }
1349 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1350 return indexBuffer;
1351 }
1352 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001354}
1355
bsalomonb0bd4f62014-09-03 07:19:50 -07001356void GrGpuGL::flushScissor(const GrGLIRect& rtViewport, GrSurfaceOrigin rtOrigin) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001357 if (fScissorState.fEnabled) {
1358 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001359 scissor.setRelativeTo(rtViewport,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001360 fScissorState.fRect.fLeft,
1361 fScissorState.fRect.fTop,
1362 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001363 fScissorState.fRect.height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001364 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001365 // if the scissor fully contains the viewport then we fall through and
1366 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001367 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001368 if (fHWScissorSettings.fRect != scissor) {
1369 scissor.pushToGLScissor(this->glInterface());
1370 fHWScissorSettings.fRect = scissor;
1371 }
1372 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1373 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1374 fHWScissorSettings.fEnabled = kYes_TriState;
1375 }
1376 return;
1377 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001378 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001379 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001380 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001381 fHWScissorSettings.fEnabled = kNo_TriState;
1382 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 }
1384}
1385
bsalomonb0bd4f62014-09-03 07:19:50 -07001386void GrGpuGL::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
1387 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001388 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001389 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001390 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001391
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001392 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1393 rect = NULL;
1394 }
1395
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001396 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001397 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001398 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001399 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001400 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 if (clippedRect.intersect(rtRect)) {
1402 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001403 } else {
1404 return;
1405 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001406 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001407
bsalomonb0bd4f62014-09-03 07:19:50 -07001408 this->flushRenderTarget(glRT, rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001409 GrAutoTRestore<ScissorState> asr(&fScissorState);
bsalomon49f085d2014-09-05 13:34:00 -07001410 fScissorState.fEnabled = SkToBool(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001411 if (fScissorState.fEnabled) {
1412 fScissorState.fRect = *rect;
1413 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001414 this->flushScissor(glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001415
1416 GrGLfloat r, g, b, a;
1417 static const GrGLfloat scale255 = 1.f / 255.f;
1418 a = GrColorUnpackA(color) * scale255;
1419 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001420 r = GrColorUnpackR(color) * scaleRGB;
1421 g = GrColorUnpackG(color) * scaleRGB;
1422 b = GrColorUnpackB(color) * scaleRGB;
1423
1424 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001425 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001426 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001427 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001430void GrGpuGL::discard(GrRenderTarget* renderTarget) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001431 if (!this->caps()->discardRenderTargetSupport()) {
1432 return;
1433 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001434 if (NULL == renderTarget) {
1435 renderTarget = this->drawState()->getRenderTarget();
1436 if (NULL == renderTarget) {
1437 return;
1438 }
1439 }
1440
1441 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001442 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1443 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001444 fGPUStats.incRenderTargetBinds();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001445 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1446 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001447 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001448 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001449 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001450 break;
1451 case GrGLCaps::kInvalidate_InvalidateFBType:
1452 if (0 == glRT->renderFBOID()) {
1453 // When rendering to the default framebuffer the legal values for attachments
1454 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1455 // types.
1456 static const GrGLenum attachments[] = { GR_GL_COLOR };
1457 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1458 attachments));
1459 } else {
1460 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1461 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1462 attachments));
1463 }
1464 break;
1465 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001466 if (0 == glRT->renderFBOID()) {
1467 // When rendering to the default framebuffer the legal values for attachments
1468 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1469 // types. See glDiscardFramebuffer() spec.
1470 static const GrGLenum attachments[] = { GR_GL_COLOR };
1471 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1472 attachments));
1473 } else {
1474 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1475 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1476 attachments));
1477 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001478 break;
1479 }
1480 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001481 renderTarget->flagAsResolved();
1482}
1483
1484
bsalomonb0bd4f62014-09-03 07:19:50 -07001485void GrGpuGL::clearStencil(GrRenderTarget* target) {
1486 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001487 return;
1488 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001489 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1490 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001491
bsalomon@google.coma3201942012-06-21 19:58:20 +00001492 GrAutoTRestore<ScissorState> asr(&fScissorState);
1493 fScissorState.fEnabled = false;
bsalomonb0bd4f62014-09-03 07:19:50 -07001494 this->flushScissor(glRT->getViewport(), glRT->origin());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001495
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001496 GL_CALL(StencilMask(0xffffffff));
1497 GL_CALL(ClearStencil(0));
1498 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001499 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001500}
1501
bsalomonb0bd4f62014-09-03 07:19:50 -07001502void GrGpuGL::clearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001503 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001504
1505 // this should only be called internally when we know we have a
1506 // stencil buffer.
bsalomon49f085d2014-09-05 13:34:00 -07001507 SkASSERT(target->getStencilBuffer());
bsalomonb0bd4f62014-09-03 07:19:50 -07001508 GrGLint stencilBitCount = target->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001509#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001510 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001511 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001512#else
1513 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001515 // turned into draws. Our contract on GrDrawTarget says that
1516 // changing the clip between stencil passes may or may not
1517 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001518 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001519#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001520 GrGLint value;
1521 if (insideClip) {
1522 value = (1 << (stencilBitCount - 1));
1523 } else {
1524 value = 0;
1525 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001526 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1527 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001528
1529 GrAutoTRestore<ScissorState> asr(&fScissorState);
1530 fScissorState.fEnabled = true;
1531 fScissorState.fRect = rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001532 this->flushScissor(glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001533
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001534 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001535 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001536 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001537 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001538}
1539
bsalomon@google.comc4364992011-11-07 15:54:49 +00001540bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1541 int left, int top,
1542 int width, int height,
1543 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001544 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001545 // If this rendertarget is aready TopLeft, we don't need to flip.
1546 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1547 return false;
1548 }
1549
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001550 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001551 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001552 return false;
1553 }
1554
1555 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001556 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001557 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001558 return true;
1559 }
1560 // If we have to do memcpys to handle rowBytes then y-flip is free
1561 // Note the rowBytes might be tight to the passed in data, but if data
1562 // gets clipped in x to the target the rowBytes will no longer be tight.
1563 if (left >= 0 && (left + width) < renderTarget->width()) {
1564 return 0 == rowBytes ||
1565 GrBytesPerPixel(config) * width == rowBytes;
1566 } else {
1567 return false;
1568 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001569}
1570
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001571bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001572 int left, int top,
1573 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001574 GrPixelConfig config,
1575 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001576 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001577 // We cannot read pixels into a compressed buffer
1578 if (GrPixelConfigIsCompressed(config)) {
1579 return false;
1580 }
1581
1582 GrGLenum format = 0;
1583 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001584 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001585 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001586 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001587 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001588 size_t bpp = GrBytesPerPixel(config);
1589 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1590 &left, &top, &width, &height,
1591 const_cast<const void**>(&buffer),
1592 &rowBytes)) {
1593 return false;
1594 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001595
bsalomon@google.comc6980972011-11-02 19:57:21 +00001596 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001597 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001598 switch (tgt->getResolveType()) {
1599 case GrGLRenderTarget::kCantResolve_ResolveType:
1600 return false;
1601 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomonb0bd4f62014-09-03 07:19:50 -07001602 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target),
1603 &SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001604 break;
1605 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001606 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001607 // we don't track the state of the READ FBO ID.
robertphillips754f4e92014-09-18 13:52:08 -07001608 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001609 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1610 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001611 break;
1612 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001613 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
1615
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001616 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001617
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001618 // the read rect is viewport-relative
1619 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001620 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001621
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001622 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001623 if (0 == rowBytes) {
1624 rowBytes = tightRowBytes;
1625 }
1626 size_t readDstRowBytes = tightRowBytes;
1627 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001628
bsalomon@google.comc6980972011-11-02 19:57:21 +00001629 // determine if GL can read using the passed rowBytes or if we need
1630 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001631 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001632 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001633 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001634 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001635 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001636 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001637 readDstRowBytes = rowBytes;
1638 } else {
1639 scratch.reset(tightRowBytes * height);
1640 readDst = scratch.get();
1641 }
1642 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001643 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001644 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1645 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001646 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1647 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001648 format, type, readDst));
1649 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001650 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001651 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1652 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001653 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001654 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001655 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001656 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001657
1658 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001659 // API presents top-to-bottom. We must preserve the padding contents. Note
1660 // that the above readPixels did not overwrite the padding.
1661 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001662 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001663 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001664 scratch.reset(tightRowBytes);
1665 void* tmpRow = scratch.get();
1666 // flip y in-place by rows
1667 const int halfY = height >> 1;
1668 char* top = reinterpret_cast<char*>(buffer);
1669 char* bottom = top + (height - 1) * rowBytes;
1670 for (int y = 0; y < halfY; y++) {
1671 memcpy(tmpRow, top, tightRowBytes);
1672 memcpy(top, bottom, tightRowBytes);
1673 memcpy(bottom, tmpRow, tightRowBytes);
1674 top += rowBytes;
1675 bottom -= rowBytes;
1676 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001677 }
1678 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001679 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001680 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001681 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001682 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001683 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001684 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001685 dst += (height-1) * rowBytes;
1686 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001687 for (int y = 0; y < height; y++) {
1688 memcpy(dst, src, tightRowBytes);
1689 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001690 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001691 dst += rowBytes;
1692 } else {
1693 dst -= rowBytes;
1694 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 }
1696 }
1697 return true;
1698}
1699
bsalomonb0bd4f62014-09-03 07:19:50 -07001700void GrGpuGL::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001701
bsalomon49f085d2014-09-05 13:34:00 -07001702 SkASSERT(target);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001703
bsalomonb0bd4f62014-09-03 07:19:50 -07001704 uint32_t rtID = target->getUniqueID();
bsalomon1c63bf62014-07-22 13:09:46 -07001705 if (fHWBoundRenderTargetUniqueID != rtID) {
robertphillips754f4e92014-09-18 13:52:08 -07001706 fGPUStats.incRenderTargetBinds();
bsalomonb0bd4f62014-09-03 07:19:50 -07001707 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001708#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001709 // don't do this check in Chromium -- this is causing
1710 // lots of repeated command buffer flushes when the compositor is
1711 // rendering with Ganesh, which is really slow; even too slow for
1712 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001713 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001714 GrGLenum status;
1715 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1716 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1717 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1718 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001720#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001721 fHWBoundRenderTargetUniqueID = rtID;
bsalomonb0bd4f62014-09-03 07:19:50 -07001722 const GrGLIRect& vp = target->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001723 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001724 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001725 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 }
1727 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001728 if (NULL == bound || !bound->isEmpty()) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001729 target->flagAsNeedingResolve(bound);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001730 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001731
bsalomonb0bd4f62014-09-03 07:19:50 -07001732 GrTexture *texture = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001733 if (texture) {
robertphillipsb06e5a22014-09-30 06:58:20 -07001734 texture->impl()->dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001735 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001736}
1737
twiz@google.com0f31ca72011-03-18 17:38:11 +00001738GrGLenum gPrimitiveType2GLMode[] = {
1739 GR_GL_TRIANGLES,
1740 GR_GL_TRIANGLE_STRIP,
1741 GR_GL_TRIANGLE_FAN,
1742 GR_GL_POINTS,
1743 GR_GL_LINES,
1744 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001745};
1746
bsalomon@google.comd302f142011-03-03 13:54:13 +00001747#define SWAP_PER_DRAW 0
1748
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001749#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001750 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001751 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001752 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001753 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001754 void SwapBuf() {
1755 DWORD procID = GetCurrentProcessId();
1756 HWND hwnd = GetTopWindow(GetDesktopWindow());
1757 while(hwnd) {
1758 DWORD wndProcID = 0;
1759 GetWindowThreadProcessId(hwnd, &wndProcID);
1760 if(wndProcID == procID) {
1761 SwapBuffers(GetDC(hwnd));
1762 }
1763 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1764 }
1765 }
1766 #endif
1767#endif
1768
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001769void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001770 size_t indexOffsetInBytes;
1771 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001773 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001774
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001775 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001776 GrGLvoid* indices =
1777 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001778 // info.startVertex() was accounted for by setupGeometry.
1779 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1780 info.indexCount(),
1781 GR_GL_UNSIGNED_SHORT,
1782 indices));
1783 } else {
1784 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1785 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1786 // accounted for startVertex.
1787 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1788 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001789#if SWAP_PER_DRAW
1790 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001791 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001792 aglSwapBuffers(aglGetCurrentContext());
1793 int set_a_break_pt_here = 9;
1794 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001795 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001796 SwapBuf();
1797 int set_a_break_pt_here = 9;
1798 SwapBuf();
1799 #endif
1800#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001801}
1802
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001803void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001804 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001805 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001806 // Some extensions automatically resolves the texture when it is read.
1807 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001808 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
robertphillips754f4e92014-09-18 13:52:08 -07001809 fGPUStats.incRenderTargetBinds();
1810 fGPUStats.incRenderTargetBinds();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001811 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1812 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1813 // make sure we go through flushRenderTarget() since we've modified
1814 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07001815 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001816 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001817 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001818 GrGLIRect r;
1819 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1820 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001821
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001822 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001823 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001824 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001825 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001826 fScissorState.fEnabled = true;
1827 fScissorState.fRect = dirtyRect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001828 this->flushScissor(rt->getViewport(), rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001829 GL_CALL(ResolveMultisampleFramebuffer());
1830 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001831 int right = r.fLeft + r.fWidth;
1832 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001833
1834 // BlitFrameBuffer respects the scissor, so disable it.
1835 asr.reset(&fScissorState);
1836 fScissorState.fEnabled = false;
1837 this->flushScissor(rt->getViewport(), rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001838 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1839 r.fLeft, r.fBottom, right, top,
1840 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001841 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001842 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001843 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 }
1845}
1846
bsalomon@google.com411dad02012-06-05 20:24:20 +00001847namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001848
bsalomon@google.com411dad02012-06-05 20:24:20 +00001849
1850GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1851 static const GrGLenum gTable[] = {
1852 GR_GL_KEEP, // kKeep_StencilOp
1853 GR_GL_REPLACE, // kReplace_StencilOp
1854 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1855 GR_GL_INCR, // kIncClamp_StencilOp
1856 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1857 GR_GL_DECR, // kDecClamp_StencilOp
1858 GR_GL_ZERO, // kZero_StencilOp
1859 GR_GL_INVERT, // kInvert_StencilOp
1860 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001861 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001862 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1863 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1864 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1865 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1866 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1867 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1868 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1869 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001870 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001871 return gTable[op];
1872}
1873
1874void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001875 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001876 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001877 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07001878 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001879 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1880 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1881
1882 GrGLint ref = settings.funcRef(grFace);
1883 GrGLint mask = settings.funcMask(grFace);
1884 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001885
1886 if (GR_GL_FRONT_AND_BACK == glFace) {
1887 // we call the combined func just in case separate stencil is not
1888 // supported.
1889 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1890 GR_GL_CALL(gl, StencilMask(writeMask));
1891 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1892 } else {
1893 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1894 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1895 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1896 }
1897}
1898}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001899
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001900void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001901 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001902 if (fStencilSettings.isDisabled()) {
1903 if (kNo_TriState != fHWStencilTestEnabled) {
1904 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1905 fHWStencilTestEnabled = kNo_TriState;
1906 }
1907 } else {
1908 if (kYes_TriState != fHWStencilTestEnabled) {
1909 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1910 fHWStencilTestEnabled = kYes_TriState;
1911 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001912 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001913 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001914 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001915 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001916 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001917 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001918 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001922 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001923 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001924 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001925 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001926 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001927 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001928 }
1929 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001930 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001931 }
1932}
1933
egdanield632ea42014-09-24 10:30:12 -07001934void GrGpuGL::flushAAState(const GrOptDrawState& optState, DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001935// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1936// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1937#if 0
1938 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1939 #define RT_HAS_MSAA rt->isMultisampled()
1940#else
1941 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1942#endif
1943
egdanield632ea42014-09-24 10:30:12 -07001944 const GrRenderTarget* rt = optState.getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00001945 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07001946 if (RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001947 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1948 // convex hulls of each segment appear to get filled.
1949 bool enableMSAA = kStencilPath_DrawType == type ||
egdanield632ea42014-09-24 10:30:12 -07001950 optState.isHWAntialiasState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001951 if (enableMSAA) {
egdanielb414f252014-07-29 13:15:47 -07001952 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001953 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07001954 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001955 }
1956 } else {
egdanielb414f252014-07-29 13:15:47 -07001957 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001958 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07001959 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001960 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001961 }
1962 }
1963 }
1964}
1965
egdanield632ea42014-09-24 10:30:12 -07001966void GrGpuGL::flushBlend(const GrOptDrawState& optState, bool isLines,
1967 GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdanielb414f252014-07-29 13:15:47 -07001968 // Any optimization to disable blending should have already been applied and
1969 // tweaked the coeffs to (1, 0).
1970 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
1971 if (blendOff) {
1972 if (kNo_TriState != fHWBlendState.fEnabled) {
1973 GL_CALL(Disable(GR_GL_BLEND));
1974 fHWBlendState.fEnabled = kNo_TriState;
1975 }
1976 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001977 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001978 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001979 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001980 }
egdanielb414f252014-07-29 13:15:47 -07001981 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1982 fHWBlendState.fDstCoeff != dstCoeff) {
1983 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1984 gXfermodeCoeff2Blend[dstCoeff]));
1985 fHWBlendState.fSrcCoeff = srcCoeff;
1986 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001987 }
egdanield632ea42014-09-24 10:30:12 -07001988 GrColor blendConst = optState.getBlendConstant();
egdanielb414f252014-07-29 13:15:47 -07001989 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1990 BlendCoeffReferencesConstant(dstCoeff)) &&
1991 (!fHWBlendState.fConstColorValid ||
1992 fHWBlendState.fConstColor != blendConst)) {
1993 GrGLfloat c[4];
1994 GrColorToRGBAFloat(blendConst, c);
1995 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
1996 fHWBlendState.fConstColor = blendConst;
1997 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001998 }
1999 }
2000}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002001
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002002static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002003 static const GrGLenum gWrapModes[] = {
2004 GR_GL_CLAMP_TO_EDGE,
2005 GR_GL_REPEAT,
2006 GR_GL_MIRRORED_REPEAT
2007 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002008 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002009 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2010 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2011 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2012 return gWrapModes[tm];
2013}
2014
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002015void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002016 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002017
bsalomon@google.comb8670992012-07-25 21:27:09 +00002018 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2019 // 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 +00002020 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002021 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002022 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002023 this->onResolveRenderTarget(texRT);
2024 }
2025
bsalomon1c63bf62014-07-22 13:09:46 -07002026 uint32_t textureID = texture->getUniqueID();
2027 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002028 this->setTextureUnit(unitIdx);
2029 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002030 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002031 }
2032
bsalomon@google.com4c883782012-06-04 19:05:11 +00002033 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002034 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002035 bool setAll = timestamp < this->getResetTimestamp();
2036 GrGLTexture::TexParams newTexParams;
2037
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002038 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002039 GR_GL_NEAREST,
2040 GR_GL_LINEAR,
2041 GR_GL_LINEAR_MIPMAP_LINEAR
2042 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002043 static GrGLenum glMagFilterModes[] = {
2044 GR_GL_NEAREST,
2045 GR_GL_LINEAR,
2046 GR_GL_LINEAR
2047 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002048 GrTextureParams::FilterMode filterMode = params.filterMode();
2049 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2050 filterMode = GrTextureParams::kBilerp_FilterMode;
2051 }
2052 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2053 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002054
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002055 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
robertphillipsb06e5a22014-09-30 06:58:20 -07002056 texture->mipMapsAreDirty() && !GrPixelConfigIsCompressed(texture->config())) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002057 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
robertphillipsb06e5a22014-09-30 06:58:20 -07002058 texture->dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002059 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002060
bsalomon@google.comb8670992012-07-25 21:27:09 +00002061 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2062 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002063 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002064 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002066 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002067 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002068 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2069 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002070 newTexParams.fMagFilter));
2071 }
2072 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2073 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002074 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2075 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002076 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002077 }
2078 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002079 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002080 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2081 GR_GL_TEXTURE_WRAP_S,
2082 newTexParams.fWrapS));
2083 }
2084 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002085 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002086 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2087 GR_GL_TEXTURE_WRAP_T,
2088 newTexParams.fWrapT));
2089 }
2090 if (this->glCaps().textureSwizzleSupport() &&
2091 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2092 oldTexParams.fSwizzleRGBA,
2093 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002094 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002095 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002096 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2097 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2098 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2099 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2100 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2101 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2102 } else {
2103 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2104 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2105 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2106 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002107 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002108 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002109}
2110
egdanield632ea42014-09-24 10:30:12 -07002111void GrGpuGL::flushMiscFixedFunctionState(const GrOptDrawState& optState) {
2112 if (optState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002113 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002114 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002115 fHWDitherEnabled = kYes_TriState;
2116 }
2117 } else {
2118 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002119 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002120 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002121 }
2122 }
2123
egdanield632ea42014-09-24 10:30:12 -07002124 if (optState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002125 if (kNo_TriState != fHWWriteToColor) {
2126 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2127 GR_GL_FALSE, GR_GL_FALSE));
2128 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002129 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002130 } else {
2131 if (kYes_TriState != fHWWriteToColor) {
2132 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2133 fHWWriteToColor = kYes_TriState;
2134 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002135 }
2136
egdanield632ea42014-09-24 10:30:12 -07002137 if (fHWDrawFace != optState.getDrawFace()) {
2138 switch (optState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002139 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002140 GL_CALL(Enable(GR_GL_CULL_FACE));
2141 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002142 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002143 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 GL_CALL(Enable(GR_GL_CULL_FACE));
2145 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002146 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002147 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002148 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002149 break;
2150 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002151 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002152 }
egdanield632ea42014-09-24 10:30:12 -07002153 fHWDrawFace = optState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002155}
2156
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002157bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2158 bool getSizedInternalFormat,
2159 GrGLenum* internalFormat,
2160 GrGLenum* externalFormat,
2161 GrGLenum* externalType) {
2162 GrGLenum dontCare;
2163 if (NULL == internalFormat) {
2164 internalFormat = &dontCare;
2165 }
2166 if (NULL == externalFormat) {
2167 externalFormat = &dontCare;
2168 }
2169 if (NULL == externalType) {
2170 externalType = &dontCare;
2171 }
2172
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002173 if(!this->glCaps().isConfigTexturable(config)) {
2174 return false;
2175 }
2176
reed@google.comac10a2d2010-12-22 21:39:39 +00002177 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002178 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002179 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002180 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002181 if (getSizedInternalFormat) {
2182 *internalFormat = GR_GL_RGBA8;
2183 } else {
2184 *internalFormat = GR_GL_RGBA;
2185 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002186 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002187 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002188 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002189 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002190 if (getSizedInternalFormat) {
2191 *internalFormat = GR_GL_BGRA8;
2192 } else {
2193 *internalFormat = GR_GL_BGRA;
2194 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002195 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002196 if (getSizedInternalFormat) {
2197 *internalFormat = GR_GL_RGBA8;
2198 } else {
2199 *internalFormat = GR_GL_RGBA;
2200 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002201 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002202 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002203 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002204 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002205 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002206 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002207 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002208 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002209 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002210 return false;
2211 } else {
2212 *internalFormat = GR_GL_RGB565;
2213 }
2214 } else {
2215 *internalFormat = GR_GL_RGB;
2216 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002217 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002218 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002219 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002220 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002221 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002222 if (getSizedInternalFormat) {
2223 *internalFormat = GR_GL_RGBA4;
2224 } else {
2225 *internalFormat = GR_GL_RGBA;
2226 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002227 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002228 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002229 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002230 // no sized/unsized internal format distinction here
2231 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002232 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002233 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002234 if (this->glCaps().textureRedSupport()) {
2235 *internalFormat = GR_GL_RED;
2236 *externalFormat = GR_GL_RED;
2237 if (getSizedInternalFormat) {
2238 *internalFormat = GR_GL_R8;
2239 } else {
2240 *internalFormat = GR_GL_RED;
2241 }
2242 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002243 } else {
2244 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002245 *externalFormat = GR_GL_ALPHA;
2246 if (getSizedInternalFormat) {
2247 *internalFormat = GR_GL_ALPHA8;
2248 } else {
2249 *internalFormat = GR_GL_ALPHA;
2250 }
2251 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002252 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002253 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002254 case kETC1_GrPixelConfig:
2255 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2256 break;
2257 case kLATC_GrPixelConfig:
2258 switch(this->glCaps().latcAlias()) {
2259 case GrGLCaps::kLATC_LATCAlias:
2260 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2261 break;
2262 case GrGLCaps::kRGTC_LATCAlias:
2263 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2264 break;
2265 case GrGLCaps::k3DC_LATCAlias:
2266 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2267 break;
2268 }
2269 break;
krajcevski238b4562014-06-30 09:09:22 -07002270 case kR11_EAC_GrPixelConfig:
2271 *internalFormat = GR_GL_COMPRESSED_R11;
2272 break;
krajcevski7ef21622014-07-16 15:21:13 -07002273
2274 case kASTC_12x12_GrPixelConfig:
2275 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2276 break;
2277
joshualittee5da552014-07-16 13:32:56 -07002278 case kRGBA_float_GrPixelConfig:
2279 *internalFormat = GR_GL_RGBA32F;
2280 *externalFormat = GR_GL_RGBA;
2281 *externalType = GR_GL_FLOAT;
2282 break;
krajcevski7ef21622014-07-16 15:21:13 -07002283
reed@google.comac10a2d2010-12-22 21:39:39 +00002284 default:
2285 return false;
2286 }
2287 return true;
2288}
2289
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002290void GrGpuGL::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002291 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002292 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002293 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002294 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002295 }
2296}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002297
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002298void GrGpuGL::setScratchTextureUnit() {
2299 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002300 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002301 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2302 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2303 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002304 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002305 // clear out the this field so that if a program does use this unit it will rebind the correct
2306 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002307 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002308}
2309
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002310namespace {
2311// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002312inline bool can_blit_framebuffer(const GrSurface* dst,
2313 const GrSurface* src,
2314 const GrGpuGL* gpu,
2315 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002316 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2317 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002318 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002319 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2320 // or the rects are not the same (not just the same size but have the same edges).
2321 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2322 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2323 return false;
2324 }
bsalomon49f085d2014-09-05 13:34:00 -07002325 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002326 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2327 }
2328 return true;
2329 } else {
2330 return false;
2331 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002332}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002333
2334inline bool can_copy_texsubimage(const GrSurface* dst,
2335 const GrSurface* src,
2336 const GrGpuGL* gpu,
2337 bool* wouldNeedTempFBO = NULL) {
2338 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2339 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2340 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002341 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002342 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2343 return false;
2344 }
2345 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2346 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2347 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon49f085d2014-09-05 13:34:00 -07002348 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002349 return false;
2350 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002351 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2352 // If the src is multisampled (and uses an extension where there is a separate MSAA
2353 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon49f085d2014-09-05 13:34:00 -07002354 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002355 return false;
2356 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002357 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002358 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002359 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002360 !GrPixelConfigIsCompressed(src->config())) {
bsalomon49f085d2014-09-05 13:34:00 -07002361 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002362 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2363 }
2364 return true;
2365 } else {
2366 return false;
2367 }
2368}
2369
robertphillips754f4e92014-09-18 13:52:08 -07002370}
2371
bsalomon@google.comeb851172013-04-15 13:51:00 +00002372// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2373// relative to is output.
robertphillips754f4e92014-09-18 13:52:08 -07002374GrGLuint GrGpuGL::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002375 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2376 GrGLuint tempFBOID;
2377 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002378 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002379 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
robertphillips754f4e92014-09-18 13:52:08 -07002380 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, &tempFBOID));
2381 fGPUStats.incRenderTargetBinds();
2382 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, tempFBOID));
2383 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2384 GR_GL_COLOR_ATTACHMENT0,
2385 GR_GL_TEXTURE_2D,
2386 texID,
2387 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002388 viewport->fLeft = 0;
2389 viewport->fBottom = 0;
2390 viewport->fWidth = surface->width();
2391 viewport->fHeight = surface->height();
2392 } else {
2393 tempFBOID = 0;
robertphillips754f4e92014-09-18 13:52:08 -07002394 fGPUStats.incRenderTargetBinds();
2395 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002396 *viewport = rt->getViewport();
2397 }
2398 return tempFBOID;
2399}
2400
bsalomon@google.comeb851172013-04-15 13:51:00 +00002401void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2402 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002403 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002404 kBGRA_8888_GrPixelConfig == src->config()) {
2405 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002406 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002407 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002408 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002409 } else if (NULL == src->asRenderTarget()) {
2410 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2411 // class handle it by rendering.
2412 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002413 return;
2414 }
2415
2416 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002417 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002418 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2419 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002420 } else {
2421 desc->fConfig = src->config();
2422 desc->fOrigin = src->origin();
2423 desc->fFlags = kNone_GrTextureFlags;
2424 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002425}
2426
2427bool GrGpuGL::onCopySurface(GrSurface* dst,
2428 GrSurface* src,
2429 const SkIRect& srcRect,
2430 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002431 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002432 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002433 bool wouldNeedTempFBO = false;
2434 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2435 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2436 GrGLuint srcFBO;
2437 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002438 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002439 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002440 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002441 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002442 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002443 GrGLIRect srcGLRect;
2444 srcGLRect.setRelativeTo(srcVP,
2445 srcRect.fLeft,
2446 srcRect.fTop,
2447 srcRect.width(),
2448 srcRect.height(),
2449 src->origin());
2450
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002451 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002452 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2453 GrGLint dstY;
2454 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2455 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2456 } else {
2457 dstY = dstPoint.fY;
2458 }
2459 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2460 dstPoint.fX, dstY,
2461 srcGLRect.fLeft, srcGLRect.fBottom,
2462 srcGLRect.fWidth, srcGLRect.fHeight));
2463 copied = true;
2464 if (srcFBO) {
2465 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2466 }
2467 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2468 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002469 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2470 srcRect.width(), srcRect.height());
2471 bool selfOverlap = false;
robertphillipsb06e5a22014-09-30 06:58:20 -07002472 if (dst->isSameAs(src)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002473 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2474 }
2475
2476 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002477 GrGLuint dstFBO;
2478 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002479 GrGLIRect dstVP;
2480 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002481 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2482 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002483 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002484 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002485 GrGLIRect srcGLRect;
2486 GrGLIRect dstGLRect;
2487 srcGLRect.setRelativeTo(srcVP,
2488 srcRect.fLeft,
2489 srcRect.fTop,
2490 srcRect.width(),
2491 srcRect.height(),
2492 src->origin());
2493 dstGLRect.setRelativeTo(dstVP,
2494 dstRect.fLeft,
2495 dstRect.fTop,
2496 dstRect.width(),
2497 dstRect.height(),
2498 dst->origin());
2499
2500 GrAutoTRestore<ScissorState> asr;
derekf8c8f71a2014-09-16 06:24:57 -07002501 // BlitFrameBuffer respects the scissor, so disable it.
2502 asr.reset(&fScissorState);
2503 fScissorState.fEnabled = false;
2504 this->flushScissor(dstGLRect, dst->origin());
2505
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002506 GrGLint srcY0;
2507 GrGLint srcY1;
2508 // Does the blit need to y-mirror or not?
2509 if (src->origin() == dst->origin()) {
2510 srcY0 = srcGLRect.fBottom;
2511 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2512 } else {
2513 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2514 srcY1 = srcGLRect.fBottom;
2515 }
2516 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2517 srcY0,
2518 srcGLRect.fLeft + srcGLRect.fWidth,
2519 srcY1,
2520 dstGLRect.fLeft,
2521 dstGLRect.fBottom,
2522 dstGLRect.fLeft + dstGLRect.fWidth,
2523 dstGLRect.fBottom + dstGLRect.fHeight,
2524 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2525 if (dstFBO) {
2526 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2527 }
2528 if (srcFBO) {
2529 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2530 }
2531 copied = true;
2532 }
2533 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002534 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002535 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002536 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002537 }
2538 return copied;
2539}
2540
2541bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2542 GrSurface* src,
2543 const SkIRect& srcRect,
2544 const SkIPoint& dstPoint) {
2545 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002546 if (can_copy_texsubimage(dst, src, this)) {
2547 return true;
2548 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002549 if (can_blit_framebuffer(dst, src, this)) {
robertphillipsb06e5a22014-09-30 06:58:20 -07002550 if (dst->isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002551 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2552 srcRect.width(), srcRect.height());
2553 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2554 return true;
2555 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002556 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002557 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002558 }
2559 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002560 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002561}
2562
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002563void GrGpuGL::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002564 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002565 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002566 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002567 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002568 }
2569}
2570
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002571void GrGpuGL::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002572 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002573 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002574 }
2575}
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002576///////////////////////////////////////////////////////////////////////////////
2577
bsalomon@google.com6918d482013-03-07 19:09:11 +00002578GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2579 GrGpuGL* gpu,
2580 const GrGLVertexBuffer* vbuffer,
2581 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002582 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002583 GrGLAttribArrayState* attribState;
2584
bsalomon@google.com6918d482013-03-07 19:09:11 +00002585 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2586 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002587 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002588 SkSafeUnref(fVBOVertexArray);
2589 GrGLuint arrayID;
2590 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2591 int attrCount = gpu->glCaps().maxVertexAttributes();
2592 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002593 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002594 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2595 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002596 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002597 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2598 } else {
2599 this->setVertexArrayID(gpu, 0);
2600 }
2601 int attrCount = gpu->glCaps().maxVertexAttributes();
2602 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2603 fDefaultVertexArrayAttribState.resize(attrCount);
2604 }
2605 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002606 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002607 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002608}