blob: 8be1118d64839e2f2bb687cdb2709360e8470b04 [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"
bsalomonafbf2d62014-09-30 12:18:44 -070012#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000013#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000015#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000016#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000017#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000020#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021
reed@google.comac10a2d2010-12-22 21:39:39 +000022#define SKIP_CACHE_CHECK true
23
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000024#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
25 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
26 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
27 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000028#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000029 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
30 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
31 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
32#endif
33
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000034
35///////////////////////////////////////////////////////////////////////////////
36
cdaltonb85a0aa2014-07-21 15:32:44 -070037
twiz@google.com0f31ca72011-03-18 17:38:11 +000038static const GrGLenum gXfermodeCoeff2Blend[] = {
39 GR_GL_ZERO,
40 GR_GL_ONE,
41 GR_GL_SRC_COLOR,
42 GR_GL_ONE_MINUS_SRC_COLOR,
43 GR_GL_DST_COLOR,
44 GR_GL_ONE_MINUS_DST_COLOR,
45 GR_GL_SRC_ALPHA,
46 GR_GL_ONE_MINUS_SRC_ALPHA,
47 GR_GL_DST_ALPHA,
48 GR_GL_ONE_MINUS_DST_ALPHA,
49 GR_GL_CONSTANT_COLOR,
50 GR_GL_ONE_MINUS_CONSTANT_COLOR,
51 GR_GL_CONSTANT_ALPHA,
52 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000053
54 // extended blend coeffs
55 GR_GL_SRC1_COLOR,
56 GR_GL_ONE_MINUS_SRC1_COLOR,
57 GR_GL_SRC1_ALPHA,
58 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000059};
60
bsalomon@google.com271cffc2011-05-20 14:13:56 +000061bool GrGpuGL::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000062 static const bool gCoeffReferencesBlendConst[] = {
63 false,
64 false,
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 true,
74 true,
75 true,
76 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000077
78 // extended blend coeffs
79 false,
80 false,
81 false,
82 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000083 };
84 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000085 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000086 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000087
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
89 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
90 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
91 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
92 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
93 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
94 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
95 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
96 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
97 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
98 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
99 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
100 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
101 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000102
bsalomon@google.com47059542012-06-06 20:51:20 +0000103 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
104 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
105 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
106 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000107
108 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000109 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000110 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000111}
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113///////////////////////////////////////////////////////////////////////////////
114
rileya@google.come38160c2012-07-03 18:03:04 +0000115static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000116
robertphillips@google.com6177e692013-02-28 20:16:25 +0000117GrGpuGL::GrGpuGL(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000118 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000119 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000120
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000121 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000122 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000123
bsalomon1c63bf62014-07-22 13:09:46 -0700124 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000125
robertphillips@google.com6177e692013-02-28 20:16:25 +0000126 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000127 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000128 const GrGLubyte* vendor;
129 const GrGLubyte* renderer;
130 const GrGLubyte* version;
131 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
132 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
133 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000134 GrPrintf("------------------------- create GrGpuGL %p --------------\n",
135 this);
136 GrPrintf("------ VENDOR %s\n", vendor);
137 GrPrintf("------ RENDERER %s\n", renderer);
138 GrPrintf("------ VERSION %s\n", version);
bsalomon@google.com00142c42013-05-02 19:42:54 +0000139 GrPrintf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700140 ctx.extensions().print();
bsalomon@google.com00142c42013-05-02 19:42:54 +0000141 GrPrintf("\n");
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000142 GrPrintf(this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000143 }
144
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000145 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000146
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000147 SkASSERT(this->glCaps().maxVertexAttributes() >= GrDrawState::kMaxVertexAttribCnt);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000148
bsalomon@google.comfe676522011-06-17 18:12:21 +0000149 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000150 fHWProgramID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700151
152 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700153 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700154 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000155}
156
157GrGpuGL::~GrGpuGL() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000158 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000159 // detach the current program so there is no confusion on OpenGL's part
160 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000161 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000162 GL_CALL(UseProgram(0));
163 }
164
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000165 delete fProgramCache;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000166
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000167 // This must be called by before the GrDrawTarget destructor
168 this->releaseGeometry();
bsalomonc8dc1f72014-08-21 13:02:13 -0700169}
170
robertphillipse3371302014-09-17 06:01:06 -0700171void GrGpuGL::contextAbandoned() {
172 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700173 fProgramCache->abandon();
174 fHWProgramID = 0;
175 if (this->glCaps().pathRenderingSupport()) {
176 this->glPathRendering()->abandonGpuResources();
177 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000178}
179
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000180///////////////////////////////////////////////////////////////////////////////
bungemanc7af8122014-07-16 09:10:41 -0700181
182
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000183GrPixelConfig GrGpuGL::preferredReadPixelsConfig(GrPixelConfig readConfig,
184 GrPixelConfig surfaceConfig) const {
185 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000186 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000187 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000188 GrBytesPerPixel(readConfig) == 4 &&
189 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000190 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
191 // Perhaps this should be guarded by some compiletime or runtime check.
192 return surfaceConfig;
bungemanc7af8122014-07-16 09:10:41 -0700193 } else if (readConfig == kBGRA_8888_GrPixelConfig &&
194 !this->glCaps().readPixelsSupported(this->glInterface(),
195 GR_GL_BGRA, GR_GL_UNSIGNED_BYTE)) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000196 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000197 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000198 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000199 }
200}
201
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000202GrPixelConfig GrGpuGL::preferredWritePixelsConfig(GrPixelConfig writeConfig,
203 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000204 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
205 return kBGRA_8888_GrPixelConfig;
206 } else {
207 return writeConfig;
208 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000209}
210
211bool GrGpuGL::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700212 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000213 return false;
214 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000215 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000216 // In general ES2 requires the internal format of the texture and the format of the src
217 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
218 // texture. It depends upon which extension added BGRA. The Apple extension allows it
219 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
220 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000221 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000222 !this->glCaps().bgraIsInternalFormat() &&
223 kBGRA_8888_GrPixelConfig == srcConfig &&
224 kRGBA_8888_GrPixelConfig == texture->config()) {
225 return true;
226 } else {
227 return false;
228 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000229 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000230 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000231 }
232}
233
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000234bool GrGpuGL::fullReadPixelsIsFasterThanPartial() const {
235 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
236}
237
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000238void GrGpuGL::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000239 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000240 if (resetBits & kMisc_GrGLBackendState) {
241 GL_CALL(Disable(GR_GL_DEPTH_TEST));
242 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000243
egdaniel89af44a2014-09-26 06:15:04 -0700244 fHWDrawFace = GrOptDrawState::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000245 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000247 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000248 // Desktop-only state that we never change
249 if (!this->glCaps().isCoreProfile()) {
250 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
251 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
252 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
253 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
254 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
255 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
256 }
257 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
258 // core profile. This seems like a bug since the core spec removes any mention of
259 // GL_ARB_imaging.
260 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
261 GL_CALL(Disable(GR_GL_COLOR_TABLE));
262 }
263 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
264 // Since ES doesn't support glPointSize at all we always use the VS to
265 // set the point size
266 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
267
268 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
269 // currently part of our gl interface. There are probably others as
270 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000271 }
joshualitt58162332014-08-01 06:44:53 -0700272
273 if (kGLES_GrGLStandard == this->glStandard() &&
274 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
275 // The arm extension requires specifically enabling MSAA fetching per sample.
276 // On some devices this may have a perf hit. Also multiple render targets are disabled
277 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
278 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000279 fHWWriteToColor = kUnknown_TriState;
280 // we only ever use lines in hairline mode
281 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000282 }
edisonn@google.comba669992013-06-28 16:03:21 +0000283
egdanielb414f252014-07-29 13:15:47 -0700284 if (resetBits & kMSAAEnable_GrGLBackendState) {
285 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000286 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000287
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000288 fHWActiveTextureUnitIdx = -1; // invalid
289
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000290 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700291 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
292 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000293 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000294 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000295
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000296 if (resetBits & kBlend_GrGLBackendState) {
297 fHWBlendState.invalidate();
298 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000299
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 if (resetBits & kView_GrGLBackendState) {
301 fHWScissorSettings.invalidate();
302 fHWViewport.invalidate();
303 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000304
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000305 if (resetBits & kStencil_GrGLBackendState) {
306 fHWStencilSettings.invalidate();
307 fHWStencilTestEnabled = kUnknown_TriState;
308 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000309
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000310 // Vertex
311 if (resetBits & kVertex_GrGLBackendState) {
312 fHWGeometryState.invalidate();
313 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000315 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700316 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000317 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000318
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000319 if (resetBits & kPathRendering_GrGLBackendState) {
320 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700321 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000322 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000323 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000324
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000325 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000326 if (resetBits & kPixelStore_GrGLBackendState) {
327 if (this->glCaps().unpackRowLengthSupport()) {
328 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
329 }
330 if (this->glCaps().packRowLengthSupport()) {
331 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
332 }
333 if (this->glCaps().unpackFlipYSupport()) {
334 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
335 }
336 if (this->glCaps().packFlipYSupport()) {
337 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
338 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000339 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000340
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000341 if (resetBits & kProgram_GrGLBackendState) {
342 fHWProgramID = 0;
343 fSharedGLProgramState.invalidate();
344 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000347namespace {
348
349GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
350 // By default, GrRenderTargets are GL's normal orientation so that they
351 // can be drawn to by the outside world without the client having
352 // to render upside down.
353 if (kDefault_GrSurfaceOrigin == origin) {
354 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
355 } else {
356 return origin;
357 }
358}
359
360}
361
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000362GrTexture* GrGpuGL::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000363 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000364 return NULL;
365 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000366
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000367 if (0 == desc.fTextureHandle) {
368 return NULL;
369 }
370
bsalomon@google.combcce8922013-03-25 15:38:39 +0000371 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000372 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
373 return NULL;
374 }
375
376 GrGLTexture::Desc glTexDesc;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000377 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
robertphillips@google.com32716282012-06-04 12:48:45 +0000378 glTexDesc.fFlags = (GrTextureFlags) desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000379 glTexDesc.fWidth = desc.fWidth;
380 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.come269f212011-11-07 13:29:52 +0000381 glTexDesc.fConfig = desc.fConfig;
robertphillips@google.com32716282012-06-04 12:48:45 +0000382 glTexDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.come269f212011-11-07 13:29:52 +0000383 glTexDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon@google.com72830222013-01-23 20:25:22 +0000384 glTexDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000385 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000386 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
387 // assuming the old behaviour, which is that backend textures are always
388 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
389 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
390 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
391 glTexDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
392 } else {
393 glTexDesc.fOrigin = desc.fOrigin;
394 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000395
396 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000397 if (renderTarget) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000398 GrGLRenderTarget::Desc glRTDesc;
399 glRTDesc.fRTFBOID = 0;
400 glRTDesc.fTexFBOID = 0;
401 glRTDesc.fMSColorRenderbufferID = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000402 glRTDesc.fConfig = desc.fConfig;
403 glRTDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000404 glRTDesc.fOrigin = glTexDesc.fOrigin;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000405 glRTDesc.fCheckAllocation = false;
bsalomon@google.com99621082011-11-15 16:47:16 +0000406 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
407 glTexDesc.fHeight,
bsalomon@google.come269f212011-11-07 13:29:52 +0000408 glTexDesc.fTextureID,
409 &glRTDesc)) {
410 return NULL;
411 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000412 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000413 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000414 texture = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 }
416 if (NULL == texture) {
417 return NULL;
418 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000419
bsalomon@google.come269f212011-11-07 13:29:52 +0000420 return texture;
421}
422
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000423GrRenderTarget* GrGpuGL::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000424 GrGLRenderTarget::Desc glDesc;
425 glDesc.fConfig = desc.fConfig;
426 glDesc.fRTFBOID = static_cast<GrGLuint>(desc.fRenderTargetHandle);
427 glDesc.fMSColorRenderbufferID = 0;
428 glDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
429 glDesc.fSampleCnt = desc.fSampleCnt;
bsalomon@google.com72830222013-01-23 20:25:22 +0000430 glDesc.fIsWrapped = true;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000431 glDesc.fCheckAllocation = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000432
433 glDesc.fOrigin = resolve_origin(desc.fOrigin, true);
bsalomon@google.come269f212011-11-07 13:29:52 +0000434 GrGLIRect viewport;
435 viewport.fLeft = 0;
436 viewport.fBottom = 0;
437 viewport.fWidth = desc.fWidth;
438 viewport.fHeight = desc.fHeight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000439
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000440 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget,
441 (this, glDesc, viewport));
bsalomon@google.come269f212011-11-07 13:29:52 +0000442 if (desc.fStencilBits) {
443 GrGLStencilBuffer::Format format;
444 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
445 format.fPacked = false;
446 format.fStencilBits = desc.fStencilBits;
447 format.fTotalBits = desc.fStencilBits;
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000448 static const bool kIsSBWrapped = false;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000449 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
450 (this,
bsalomon@google.com1f0f1a32013-01-23 21:32:32 +0000451 kIsSBWrapped,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000452 0,
453 desc.fWidth,
454 desc.fHeight,
455 desc.fSampleCnt,
456 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000457 tgt->setStencilBuffer(sb);
458 sb->unref();
459 }
460 return tgt;
461}
462
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000463////////////////////////////////////////////////////////////////////////////////
464
bsalomon@google.com9c680582013-02-06 18:17:50 +0000465bool GrGpuGL::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000466 int left, int top, int width, int height,
467 GrPixelConfig config, const void* buffer,
468 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000469 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000470 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000471 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000472 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
473
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000474 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000475 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
476 GrGLTexture::Desc desc;
robertphillips@google.com32716282012-06-04 12:48:45 +0000477 desc.fFlags = glTex->desc().fFlags;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478 desc.fWidth = glTex->width();
479 desc.fHeight = glTex->height();
robertphillips@google.com32716282012-06-04 12:48:45 +0000480 desc.fConfig = glTex->config();
481 desc.fSampleCnt = glTex->desc().fSampleCnt;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000482 desc.fTextureID = glTex->textureID();
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000483 desc.fOrigin = glTex->origin();
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000484
krajcevski145d48c2014-06-11 16:07:50 -0700485 bool success = false;
486 if (GrPixelConfigIsCompressed(desc.fConfig)) {
487 // We check that config == desc.fConfig in GrGpuGL::canWriteTexturePixels()
488 SkASSERT(config == desc.fConfig);
489 success = this->uploadCompressedTexData(desc, buffer, false,
490 left, top, width, height);
491 } else {
492 success = this->uploadTexData(desc, false,
493 left, top, width, height,
494 config, buffer, rowBytes);
495 }
496
497 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700498 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000499 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000500 }
krajcevski145d48c2014-06-11 16:07:50 -0700501
502 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000503}
504
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000505namespace {
506bool adjust_pixel_ops_params(int surfaceWidth,
507 int surfaceHeight,
508 size_t bpp,
509 int* left, int* top, int* width, int* height,
510 const void** data,
511 size_t* rowBytes) {
512 if (!*rowBytes) {
513 *rowBytes = *width * bpp;
514 }
515
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000516 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
517 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000518
519 if (!subRect.intersect(bounds)) {
520 return false;
521 }
522 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
523 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
524
525 *left = subRect.fLeft;
526 *top = subRect.fTop;
527 *width = subRect.width();
528 *height = subRect.height();
529 return true;
530}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000531
532GrGLenum check_alloc_error(const GrTextureDesc& desc, const GrGLInterface* interface) {
533 if (SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit)) {
534 return GR_GL_GET_ERROR(interface);
535 } else {
536 return CHECK_ALLOC_ERROR(interface);
537 }
538}
539
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000540}
541
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000542bool GrGpuGL::uploadTexData(const GrGLTexture::Desc& desc,
543 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544 int left, int top, int width, int height,
545 GrPixelConfig dataConfig,
546 const void* data,
547 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700548 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000549
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000550 // If we're uploading compressed data then we should be using uploadCompressedTexData
551 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
552
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000553 size_t bpp = GrBytesPerPixel(dataConfig);
554 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
555 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000556 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000557 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000558 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000559
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000560 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700561 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000562
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000563 // We currently lazily create MIPMAPs when the we see a draw with
564 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
565 // MIP levels are all created when the texture is created. So for now we don't use
566 // texture storage.
567 bool useTexStorage = false &&
568 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000569 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000570
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000571 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000572 // 565 is not a sized internal format on desktop GL. So on desktop with
573 // 565 we always use an unsized internal format to let the system pick
574 // the best sized format to convert the 565 data to. Since TexStorage
575 // only allows sized internal formats we will instead use TexImage2D.
576 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000577 }
578
579 GrGLenum internalFormat;
bsalomone904c092014-07-17 10:50:59 -0700580 GrGLenum externalFormat = 0x0; // suprress warning
581 GrGLenum externalType = 0x0;// suprress warning
582
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000583 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
584 // format for glTexImage, unlike ES3 and desktop. However, we allow the driver to decide the
585 // size of the internal format whenever possible and so only use a sized internal format when
586 // using texture storage.
bsalomone904c092014-07-17 10:50:59 -0700587 bool useSizedFormat = useTexStorage;
588 // At least some versions of the desktop ES3 drivers for NVIDIA won't accept GL_RED in
589 // glTexImage2D for the internal format but will accept GL_R8.
590 if (!useSizedFormat && kNVIDIA_GrGLVendor == this->glContext().vendor() &&
591 kGLES_GrGLStandard == this->glStandard() && this->glVersion() >= GR_GL_VER(3, 0)) {
592 useSizedFormat = true;
593 }
594 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000595 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000596 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000597 }
598
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000599 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000600 * check whether to allocate a temporary buffer for flipping y or
601 * because our srcData has extra bytes past each row. If so, we need
602 * to trim those off here, since GL ES may not let us specify
603 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000604 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000605 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000606 bool swFlipY = false;
607 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700608 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000609 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000610 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000611 glFlipY = true;
612 } else {
613 swFlipY = true;
614 }
615 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000616 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000617 // can't use this for flipping, only non-neg values allowed. :(
618 if (rowBytes != trimRowBytes) {
619 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
620 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
621 restoreGLRowLength = true;
622 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000623 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000624 if (trimRowBytes != rowBytes || swFlipY) {
625 // copy data into our new storage, skipping the trailing bytes
626 size_t trimSize = height * trimRowBytes;
627 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000628 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000629 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000630 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000631 char* dst = (char*)tempStorage.reset(trimSize);
632 for (int y = 0; y < height; y++) {
633 memcpy(dst, src, trimRowBytes);
634 if (swFlipY) {
635 src -= rowBytes;
636 } else {
637 src += rowBytes;
638 }
639 dst += trimRowBytes;
640 }
641 // now point data to our copied version
642 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000643 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000644 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000645 if (glFlipY) {
646 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
647 }
joshualittee5da552014-07-16 13:32:56 -0700648 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
649 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000650 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000651 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000652 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000653 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000654 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000655 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000656 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000657 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000658 GL_ALLOC_CALL(this->glInterface(),
659 TexStorage2D(GR_GL_TEXTURE_2D,
660 1, // levels
661 internalFormat,
662 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000663 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700664 GL_ALLOC_CALL(this->glInterface(),
665 TexImage2D(GR_GL_TEXTURE_2D,
666 0, // level
667 internalFormat,
668 desc.fWidth, desc.fHeight,
669 0, // border
670 externalFormat, externalType,
671 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000672 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000673 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000674 if (error != GR_GL_NO_ERROR) {
675 succeeded = false;
676 } else {
677 // if we have data and we used TexStorage to create the texture, we
678 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700679 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000680 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
681 0, // level
682 left, top,
683 width, height,
684 externalFormat, externalType,
685 data));
686 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000687 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000688 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000689 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000691 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000692 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
693 0, // level
694 left, top,
695 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000696 externalFormat, externalType, data));
697 }
698
699 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000700 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000701 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000702 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000703 if (glFlipY) {
704 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
705 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000706 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000707}
708
krajcevski145d48c2014-06-11 16:07:50 -0700709// TODO: This function is using a lot of wonky semantics like, if width == -1
bungemanc7af8122014-07-16 09:10:41 -0700710// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700711// create a CompressedTexData struct that takes a desc/ptr and figures out
712// the proper upload semantics. Then users can construct this function how they
713// see fit if they want to go against the "standard" way to do it.
krajcevski9c0e6292014-06-02 07:38:14 -0700714bool GrGpuGL::uploadCompressedTexData(const GrGLTexture::Desc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700715 const void* data,
716 bool isNewTexture,
717 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700718 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700719
720 // No support for software flip y, yet...
721 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
722
krajcevski145d48c2014-06-11 16:07:50 -0700723 if (-1 == width) {
724 width = desc.fWidth;
725 }
726#ifdef SK_DEBUG
727 else {
728 SkASSERT(width <= desc.fWidth);
729 }
730#endif
731
732 if (-1 == height) {
733 height = desc.fHeight;
734 }
735#ifdef SK_DEBUG
736 else {
737 SkASSERT(height <= desc.fHeight);
738 }
739#endif
740
krajcevski9c0e6292014-06-02 07:38:14 -0700741 // Make sure that the width and height that we pass to OpenGL
742 // is a multiple of the block size.
krajcevski145d48c2014-06-11 16:07:50 -0700743 int dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700744
745 // We only need the internal format for compressed 2D textures.
746 GrGLenum internalFormat = 0;
747 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
748 return false;
749 }
750
krajcevski145d48c2014-06-11 16:07:50 -0700751 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700752 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700753 GL_ALLOC_CALL(this->glInterface(),
754 CompressedTexImage2D(GR_GL_TEXTURE_2D,
755 0, // level
756 internalFormat,
757 width, height,
758 0, // border
759 dataSize,
760 data));
bsalomond4cb9222014-08-11 14:19:09 -0700761 GrGLenum error = check_alloc_error(desc, this->glInterface());
762 if (error != GR_GL_NO_ERROR) {
763 return false;
764 }
krajcevski145d48c2014-06-11 16:07:50 -0700765 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700766 // Paletted textures can't be updated.
767 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
768 return false;
769 }
770 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
771 0, // level
772 left, top,
773 width, height,
774 internalFormat,
775 dataSize,
776 data));
krajcevski145d48c2014-06-11 16:07:50 -0700777 }
krajcevski9c0e6292014-06-02 07:38:14 -0700778
bsalomond4cb9222014-08-11 14:19:09 -0700779 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700780}
781
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000782static bool renderbuffer_storage_msaa(GrGLContext& ctx,
783 int sampleCount,
784 GrGLenum format,
785 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000786 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000787 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000788 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000789 case GrGLCaps::kDesktop_ARB_MSFBOType:
790 case GrGLCaps::kDesktop_EXT_MSFBOType:
791 case GrGLCaps::kES_3_0_MSFBOType:
792 GL_ALLOC_CALL(ctx.interface(),
793 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
794 sampleCount,
795 format,
796 width, height));
797 break;
798 case GrGLCaps::kES_Apple_MSFBOType:
799 GL_ALLOC_CALL(ctx.interface(),
800 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
801 sampleCount,
802 format,
803 width, height));
804 break;
805 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
806 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
807 GL_ALLOC_CALL(ctx.interface(),
808 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
809 sampleCount,
810 format,
811 width, height));
812 break;
813 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000814 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000815 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000816 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000817 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000818}
819
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000820bool GrGpuGL::createRenderTargetObjects(int width, int height,
821 GrGLuint texID,
822 GrGLRenderTarget::Desc* desc) {
823 desc->fMSColorRenderbufferID = 0;
824 desc->fRTFBOID = 0;
825 desc->fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000826 desc->fIsWrapped = false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827
828 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000829
bsalomon@google.comab15d612011-08-09 12:57:56 +0000830 GrGLenum msColorFormat = 0; // suppress warning
831
bsalomon@google.com347c3822013-05-01 20:10:01 +0000832 if (desc->fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
833 goto FAILED;
834 }
835
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000836 GL_CALL(GenFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000837 if (!desc->fTexFBOID) {
838 goto FAILED;
839 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000840
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000841
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000842 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
843 // the texture bound to the other. The exception is the IMG multisample extension. With this
844 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
845 // rendered from.
bsalomon@google.com347c3822013-05-01 20:10:01 +0000846 if (desc->fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000847 GL_CALL(GenFramebuffers(1, &desc->fRTFBOID));
848 GL_CALL(GenRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000849 if (!desc->fRTFBOID ||
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000850 !desc->fMSColorRenderbufferID ||
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000851 !this->configToGLFormats(desc->fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000852 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000853 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000854 &msColorFormat,
855 NULL,
856 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000857 goto FAILED;
858 }
859 } else {
860 desc->fRTFBOID = desc->fTexFBOID;
861 }
862
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000863 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700864 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000865 if (desc->fRTFBOID != desc->fTexFBOID) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000866 SkASSERT(desc->fSampleCnt > 0);
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000867 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000868 desc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000869 if (!renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000870 desc->fSampleCnt,
871 msColorFormat,
872 width, height)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000873 goto FAILED;
874 }
robertphillips754f4e92014-09-18 13:52:08 -0700875 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000876 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000877 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000878 GR_GL_COLOR_ATTACHMENT0,
879 GR_GL_RENDERBUFFER,
880 desc->fMSColorRenderbufferID));
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000881 if (desc->fCheckAllocation ||
882 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000883 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
884 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
885 goto FAILED;
886 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000887 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000888 }
889 }
robertphillips754f4e92014-09-18 13:52:08 -0700890 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000891 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000892
bsalomon@google.com347c3822013-05-01 20:10:01 +0000893 if (this->glCaps().usesImplicitMSAAResolve() && desc->fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000894 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
895 GR_GL_COLOR_ATTACHMENT0,
896 GR_GL_TEXTURE_2D,
897 texID, 0, desc->fSampleCnt));
898 } else {
899 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
900 GR_GL_COLOR_ATTACHMENT0,
901 GR_GL_TEXTURE_2D,
902 texID, 0));
903 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000904 if (desc->fCheckAllocation ||
905 !this->glCaps().isConfigVerifiedColorAttachment(desc->fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000906 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
907 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
908 goto FAILED;
909 }
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000910 fGLContext.caps()->markConfigAsValidColorAttachment(desc->fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 }
912
913 return true;
914
915FAILED:
916 if (desc->fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000917 GL_CALL(DeleteRenderbuffers(1, &desc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000918 }
919 if (desc->fRTFBOID != desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000920 GL_CALL(DeleteFramebuffers(1, &desc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000921 }
922 if (desc->fTexFBOID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000923 GL_CALL(DeleteFramebuffers(1, &desc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000924 }
925 return false;
926}
927
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000928// good to set a break-point here to know when createTexture fails
929static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000930// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000931 return NULL;
932}
933
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000934#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000935static size_t as_size_t(int x) {
936 return x;
937}
938#endif
939
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000940GrTexture* GrGpuGL::onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000941 const void* srcData,
942 size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000943
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000944 GrGLTexture::Desc glTexDesc;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000945 GrGLRenderTarget::Desc glRTDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000946
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000947 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000948 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000949 // We fail if the MSAA was requested and is not available.
950 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
951 //GrPrintf("MSAA RT requested but not supported on this platform.");
952 return return_null_texture();
953 }
954 // If the sample count exceeds the max then we clamp it.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000955 glTexDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000956
robertphillips@google.com32716282012-06-04 12:48:45 +0000957 glTexDesc.fFlags = desc.fFlags;
bsalomon@google.com99621082011-11-15 16:47:16 +0000958 glTexDesc.fWidth = desc.fWidth;
959 glTexDesc.fHeight = desc.fHeight;
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000960 glTexDesc.fConfig = desc.fConfig;
bsalomon@google.com72830222013-01-23 20:25:22 +0000961 glTexDesc.fIsWrapped = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000962
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000963 glRTDesc.fMSColorRenderbufferID = 0;
964 glRTDesc.fRTFBOID = 0;
965 glRTDesc.fTexFBOID = 0;
bsalomon@google.com72830222013-01-23 20:25:22 +0000966 glRTDesc.fIsWrapped = false;
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000967 glRTDesc.fConfig = glTexDesc.fConfig;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000968 glRTDesc.fCheckAllocation = SkToBool(desc.fFlags & kCheckAllocation_GrTextureFlagBit);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000969
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000970 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit);
reed@google.comac10a2d2010-12-22 21:39:39 +0000971
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000972 glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
973 glRTDesc.fOrigin = glTexDesc.fOrigin;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000974
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000975 glRTDesc.fSampleCnt = glTexDesc.fSampleCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000976 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() &&
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000977 desc.fSampleCnt) {
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000978 //GrPrintf("MSAA RT requested but not supported on this platform.");
979 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 }
981
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000983 int maxRTSize = this->caps()->maxRenderTargetSize();
984 if (glTexDesc.fWidth > maxRTSize || glTexDesc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000985 return return_null_texture();
986 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000987 } else {
988 int maxSize = this->caps()->maxTextureSize();
989 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
990 return return_null_texture();
991 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000992 }
993
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000994 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000995
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000996 if (!glTexDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000997 return return_null_texture();
998 }
999
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001000 this->setScratchTextureUnit();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001002
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001003 if (renderTarget && this->glCaps().textureUsageSupport()) {
1004 // provides a hint about how this texture will be used
1005 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1006 GR_GL_TEXTURE_USAGE,
1007 GR_GL_FRAMEBUFFER_ATTACHMENT));
1008 }
1009
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001010 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1011 // drivers have a bug where an FBO won't be complete if it includes a
1012 // texture that is not mipmap complete (considering the filter in use).
1013 GrGLTexture::TexParams initialTexParams;
1014 // we only set a subset here so invalidate first
1015 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001016 initialTexParams.fMinFilter = GR_GL_NEAREST;
1017 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001018 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1019 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001020 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1021 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001022 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001023 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1024 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001025 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001026 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1027 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001028 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001029 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1030 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001031 initialTexParams.fWrapT));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001032 if (!this->uploadTexData(glTexDesc, true, 0, 0,
1033 glTexDesc.fWidth, glTexDesc.fHeight,
1034 desc.fConfig, srcData, rowBytes)) {
1035 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1036 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001037 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001038
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001039 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001040 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001041 // unbind the texture from the texture unit before binding it to the frame buffer
1042 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1043
bsalomon@google.com99621082011-11-15 16:47:16 +00001044 if (!this->createRenderTargetObjects(glTexDesc.fWidth,
1045 glTexDesc.fHeight,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001046 glTexDesc.fTextureID,
1047 &glRTDesc)) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001048 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 return return_null_texture();
1050 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001051 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc, glRTDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001052 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001053 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001055 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001056#ifdef TRACE_TEXTURE_CREATION
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001057 GrPrintf("--- new texture [%d] size=(%d %d) config=%d\n",
1058 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001059#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001060 return tex;
1061}
1062
krajcevski9c0e6292014-06-02 07:38:14 -07001063GrTexture* GrGpuGL::onCreateCompressedTexture(const GrTextureDesc& desc,
1064 const void* srcData) {
1065
1066 if(SkToBool(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
1067 return return_null_texture();
1068 }
1069
1070 // Make sure that we're not flipping Y.
1071 GrSurfaceOrigin texOrigin = resolve_origin(desc.fOrigin, false);
1072 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1073 return return_null_texture();
1074 }
1075
1076 GrGLTexture::Desc glTexDesc;
1077
1078 glTexDesc.fFlags = desc.fFlags;
1079 glTexDesc.fWidth = desc.fWidth;
1080 glTexDesc.fHeight = desc.fHeight;
1081 glTexDesc.fConfig = desc.fConfig;
1082 glTexDesc.fIsWrapped = false;
1083 glTexDesc.fOrigin = texOrigin;
1084
1085 int maxSize = this->caps()->maxTextureSize();
1086 if (glTexDesc.fWidth > maxSize || glTexDesc.fHeight > maxSize) {
1087 return return_null_texture();
1088 }
1089
1090 GL_CALL(GenTextures(1, &glTexDesc.fTextureID));
1091
1092 if (!glTexDesc.fTextureID) {
1093 return return_null_texture();
1094 }
1095
1096 this->setScratchTextureUnit();
1097 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTexDesc.fTextureID));
1098
1099 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1100 // drivers have a bug where an FBO won't be complete if it includes a
1101 // texture that is not mipmap complete (considering the filter in use).
1102 GrGLTexture::TexParams initialTexParams;
1103 // we only set a subset here so invalidate first
1104 initialTexParams.invalidate();
1105 initialTexParams.fMinFilter = GR_GL_NEAREST;
1106 initialTexParams.fMagFilter = GR_GL_NEAREST;
1107 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1108 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1109 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1110 GR_GL_TEXTURE_MAG_FILTER,
1111 initialTexParams.fMagFilter));
1112 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1113 GR_GL_TEXTURE_MIN_FILTER,
1114 initialTexParams.fMinFilter));
1115 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1116 GR_GL_TEXTURE_WRAP_S,
1117 initialTexParams.fWrapS));
1118 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1119 GR_GL_TEXTURE_WRAP_T,
1120 initialTexParams.fWrapT));
1121
1122 if (!this->uploadCompressedTexData(glTexDesc, srcData)) {
1123 GL_CALL(DeleteTextures(1, &glTexDesc.fTextureID));
1124 return return_null_texture();
1125 }
1126
1127 GrGLTexture* tex;
1128 tex = SkNEW_ARGS(GrGLTexture, (this, glTexDesc));
1129 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1130#ifdef TRACE_TEXTURE_CREATION
1131 GrPrintf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
1132 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1133#endif
1134 return tex;
1135}
1136
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001137namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001138
1139const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1140
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001141void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001142 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001143
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001144 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001145 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001146 (kUnknownBitCount == format->fTotalBits));
1147 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001148 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1150 (GrGLint*)&format->fStencilBits);
1151 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001152 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001153 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1154 (GrGLint*)&format->fTotalBits);
1155 format->fTotalBits += format->fStencilBits;
1156 } else {
1157 format->fTotalBits = format->fStencilBits;
1158 }
1159 }
1160}
1161}
1162
1163bool GrGpuGL::createStencilBufferForRenderTarget(GrRenderTarget* rt,
1164 int width, int height) {
1165
1166 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001167 // 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 +00001168 SkASSERT(rt->asTexture());
1169 SkASSERT(width >= rt->width());
1170 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171
1172 int samples = rt->numSamples();
1173 GrGLuint sbID;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GL_CALL(GenRenderbuffers(1, &sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 if (!sbID) {
1176 return false;
1177 }
1178
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001179 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 for (int i = 0; i < stencilFmtCnt; ++i) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 // we start with the last stencil format that succeeded in hopes
1183 // that we won't go through this loop more than once after the
1184 // first (painful) stencil creation.
1185 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001186 const GrGLCaps::StencilFormat& sFmt =
1187 this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001188 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001189 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001191 bool created;
1192 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001193 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001194 samples,
1195 sFmt.fInternalFormat,
1196 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 } else {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001198 GL_ALLOC_CALL(this->glInterface(),
1199 RenderbufferStorage(GR_GL_RENDERBUFFER,
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001200 sFmt.fInternalFormat,
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001201 width, height));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001202 created =
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +00001203 (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001205 if (created) {
1206 // After sized formats we attempt an unsized format and take
1207 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001208 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001209 get_stencil_rb_sizes(this->glInterface(), &format);
bsalomon@google.com72830222013-01-23 20:25:22 +00001210 static const bool kIsWrapped = false;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001211 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
bsalomon@google.com72830222013-01-23 20:25:22 +00001212 (this, kIsWrapped, sbID, width, height,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001213 samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1215 fLastSuccessfulStencilFmtIdx = sIdx;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001216 sb->transferToCache();
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001217 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 return true;
1219 }
1220 sb->abandon(); // otherwise we lose sbID
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 }
1222 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(DeleteRenderbuffers(1, &sbID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001224 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225}
1226
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001227bool GrGpuGL::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228 GrGLRenderTarget* glrt = (GrGLRenderTarget*) rt;
1229
1230 GrGLuint fbo = glrt->renderFBOID();
1231
1232 if (NULL == sb) {
bsalomon49f085d2014-09-05 13:34:00 -07001233 if (rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001235 GR_GL_STENCIL_ATTACHMENT,
1236 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001238 GR_GL_DEPTH_ATTACHMENT,
1239 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001240#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001241 GrGLenum status;
1242 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001243 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244#endif
1245 }
1246 return true;
1247 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001248 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 GrGLuint rb = glsb->renderbufferID();
1250
bsalomon1c63bf62014-07-22 13:09:46 -07001251 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001252 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001253 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1254 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001255 GR_GL_STENCIL_ATTACHMENT,
1256 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001258 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001259 GR_GL_DEPTH_ATTACHMENT,
1260 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001261 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001262 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001263 GR_GL_DEPTH_ATTACHMENT,
1264 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001267 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001268 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001269 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1270 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001271 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001272 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001273 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001274 if (glsb->format().fPacked) {
1275 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1276 GR_GL_DEPTH_ATTACHMENT,
1277 GR_GL_RENDERBUFFER, 0));
1278 }
1279 return false;
1280 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001281 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001282 rt->config(),
1283 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001284 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001285 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001286 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001288}
1289
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001290////////////////////////////////////////////////////////////////////////////////
1291
robertphillips@google.comadacc702013-10-14 21:53:24 +00001292GrVertexBuffer* GrGpuGL::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001293 GrGLVertexBuffer::Desc desc;
1294 desc.fDynamic = dynamic;
1295 desc.fSizeInBytes = size;
1296 desc.fIsWrapped = false;
1297
bsalomon@google.com96966a52013-02-21 16:34:21 +00001298 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001299 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001300 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001302 } else {
1303 GL_CALL(GenBuffers(1, &desc.fID));
1304 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001305 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001306 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1307 // make sure driver can allocate memory for this buffer
1308 GL_ALLOC_CALL(this->glInterface(),
1309 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001310 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001311 NULL, // data ptr
1312 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1313 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1314 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001315 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001316 return NULL;
1317 }
1318 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1319 return vertexBuffer;
1320 }
1321 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001323}
1324
robertphillips@google.comadacc702013-10-14 21:53:24 +00001325GrIndexBuffer* GrGpuGL::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001326 GrGLIndexBuffer::Desc desc;
1327 desc.fDynamic = dynamic;
1328 desc.fSizeInBytes = size;
1329 desc.fIsWrapped = false;
1330
bsalomon@google.com96966a52013-02-21 16:34:21 +00001331 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001332 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001333 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001335 } else {
1336 GL_CALL(GenBuffers(1, &desc.fID));
1337 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001338 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001339 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1340 // make sure driver can allocate memory for this buffer
1341 GL_ALLOC_CALL(this->glInterface(),
1342 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001343 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001344 NULL, // data ptr
1345 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1346 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1347 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001348 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001349 return NULL;
1350 }
1351 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1352 return indexBuffer;
1353 }
1354 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001356}
1357
bsalomonb0bd4f62014-09-03 07:19:50 -07001358void GrGpuGL::flushScissor(const GrGLIRect& rtViewport, GrSurfaceOrigin rtOrigin) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001359 if (fScissorState.fEnabled) {
1360 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001361 scissor.setRelativeTo(rtViewport,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001362 fScissorState.fRect.fLeft,
1363 fScissorState.fRect.fTop,
1364 fScissorState.fRect.width(),
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001365 fScissorState.fRect.height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001366 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001367 // if the scissor fully contains the viewport then we fall through and
1368 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001369 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001370 if (fHWScissorSettings.fRect != scissor) {
1371 scissor.pushToGLScissor(this->glInterface());
1372 fHWScissorSettings.fRect = scissor;
1373 }
1374 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1375 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1376 fHWScissorSettings.fEnabled = kYes_TriState;
1377 }
1378 return;
1379 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001381 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001382 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001383 fHWScissorSettings.fEnabled = kNo_TriState;
1384 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001385 }
1386}
1387
bsalomonb0bd4f62014-09-03 07:19:50 -07001388void GrGpuGL::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
1389 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001390 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001391 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001392 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001393
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001394 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1395 rect = NULL;
1396 }
1397
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001398 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001399 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001400 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001401 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001402 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001403 if (clippedRect.intersect(rtRect)) {
1404 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001405 } else {
1406 return;
1407 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001409
bsalomonb0bd4f62014-09-03 07:19:50 -07001410 this->flushRenderTarget(glRT, rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001411 GrAutoTRestore<ScissorState> asr(&fScissorState);
bsalomon49f085d2014-09-05 13:34:00 -07001412 fScissorState.fEnabled = SkToBool(rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001413 if (fScissorState.fEnabled) {
1414 fScissorState.fRect = *rect;
1415 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001416 this->flushScissor(glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001417
1418 GrGLfloat r, g, b, a;
1419 static const GrGLfloat scale255 = 1.f / 255.f;
1420 a = GrColorUnpackA(color) * scale255;
1421 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001422 r = GrColorUnpackR(color) * scaleRGB;
1423 g = GrColorUnpackG(color) * scaleRGB;
1424 b = GrColorUnpackB(color) * scaleRGB;
1425
1426 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001427 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001428 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001429 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001430}
1431
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001432void GrGpuGL::discard(GrRenderTarget* renderTarget) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001433 if (!this->caps()->discardRenderTargetSupport()) {
1434 return;
1435 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001436 if (NULL == renderTarget) {
1437 renderTarget = this->drawState()->getRenderTarget();
1438 if (NULL == renderTarget) {
1439 return;
1440 }
1441 }
1442
1443 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001444 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1445 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
robertphillips754f4e92014-09-18 13:52:08 -07001446 fGPUStats.incRenderTargetBinds();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001447 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1448 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001449 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001450 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001451 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001452 break;
1453 case GrGLCaps::kInvalidate_InvalidateFBType:
1454 if (0 == glRT->renderFBOID()) {
1455 // When rendering to the default framebuffer the legal values for attachments
1456 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1457 // types.
1458 static const GrGLenum attachments[] = { GR_GL_COLOR };
1459 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1460 attachments));
1461 } else {
1462 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1463 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1464 attachments));
1465 }
1466 break;
1467 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001468 if (0 == glRT->renderFBOID()) {
1469 // When rendering to the default framebuffer the legal values for attachments
1470 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1471 // types. See glDiscardFramebuffer() spec.
1472 static const GrGLenum attachments[] = { GR_GL_COLOR };
1473 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1474 attachments));
1475 } else {
1476 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1477 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1478 attachments));
1479 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001480 break;
1481 }
1482 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001483 renderTarget->flagAsResolved();
1484}
1485
1486
bsalomonb0bd4f62014-09-03 07:19:50 -07001487void GrGpuGL::clearStencil(GrRenderTarget* target) {
1488 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001489 return;
1490 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001491 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1492 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001493
bsalomon@google.coma3201942012-06-21 19:58:20 +00001494 GrAutoTRestore<ScissorState> asr(&fScissorState);
1495 fScissorState.fEnabled = false;
bsalomonb0bd4f62014-09-03 07:19:50 -07001496 this->flushScissor(glRT->getViewport(), glRT->origin());
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001497
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001498 GL_CALL(StencilMask(0xffffffff));
1499 GL_CALL(ClearStencil(0));
1500 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001501 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001502}
1503
bsalomonb0bd4f62014-09-03 07:19:50 -07001504void GrGpuGL::clearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001505 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001506
1507 // this should only be called internally when we know we have a
1508 // stencil buffer.
bsalomon49f085d2014-09-05 13:34:00 -07001509 SkASSERT(target->getStencilBuffer());
bsalomonb0bd4f62014-09-03 07:19:50 -07001510 GrGLint stencilBitCount = target->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001511#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001512 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001513 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001514#else
1515 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001516 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001517 // turned into draws. Our contract on GrDrawTarget says that
1518 // changing the clip between stencil passes may or may not
1519 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001520 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001521#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001522 GrGLint value;
1523 if (insideClip) {
1524 value = (1 << (stencilBitCount - 1));
1525 } else {
1526 value = 0;
1527 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001528 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1529 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001530
1531 GrAutoTRestore<ScissorState> asr(&fScissorState);
1532 fScissorState.fEnabled = true;
1533 fScissorState.fRect = rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001534 this->flushScissor(glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001535
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001536 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001537 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001538 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001539 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001540}
1541
bsalomon@google.comc4364992011-11-07 15:54:49 +00001542bool GrGpuGL::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
1543 int left, int top,
1544 int width, int height,
1545 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001546 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001547 // If this rendertarget is aready TopLeft, we don't need to flip.
1548 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1549 return false;
1550 }
1551
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001552 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001553 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001554 return false;
1555 }
1556
1557 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001558 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001559 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001560 return true;
1561 }
1562 // If we have to do memcpys to handle rowBytes then y-flip is free
1563 // Note the rowBytes might be tight to the passed in data, but if data
1564 // gets clipped in x to the target the rowBytes will no longer be tight.
1565 if (left >= 0 && (left + width) < renderTarget->width()) {
1566 return 0 == rowBytes ||
1567 GrBytesPerPixel(config) * width == rowBytes;
1568 } else {
1569 return false;
1570 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001571}
1572
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001573bool GrGpuGL::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001574 int left, int top,
1575 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576 GrPixelConfig config,
1577 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001578 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001579 // We cannot read pixels into a compressed buffer
1580 if (GrPixelConfigIsCompressed(config)) {
1581 return false;
1582 }
1583
1584 GrGLenum format = 0;
1585 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001586 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001587 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001589 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001590 size_t bpp = GrBytesPerPixel(config);
1591 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1592 &left, &top, &width, &height,
1593 const_cast<const void**>(&buffer),
1594 &rowBytes)) {
1595 return false;
1596 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001597
bsalomon@google.comc6980972011-11-02 19:57:21 +00001598 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001599 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001600 switch (tgt->getResolveType()) {
1601 case GrGLRenderTarget::kCantResolve_ResolveType:
1602 return false;
1603 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomonb0bd4f62014-09-03 07:19:50 -07001604 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target),
1605 &SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001606 break;
1607 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001608 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001609 // we don't track the state of the READ FBO ID.
robertphillips754f4e92014-09-18 13:52:08 -07001610 fGPUStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001611 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1612 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001613 break;
1614 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001615 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 }
1617
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001618 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001619
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001620 // the read rect is viewport-relative
1621 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001622 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001623
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001624 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001625 if (0 == rowBytes) {
1626 rowBytes = tightRowBytes;
1627 }
1628 size_t readDstRowBytes = tightRowBytes;
1629 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001630
bsalomon@google.comc6980972011-11-02 19:57:21 +00001631 // determine if GL can read using the passed rowBytes or if we need
1632 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001633 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001634 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001635 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001636 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001637 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001638 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001639 readDstRowBytes = rowBytes;
1640 } else {
1641 scratch.reset(tightRowBytes * height);
1642 readDst = scratch.get();
1643 }
1644 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001645 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001646 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1647 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001648 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1649 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001650 format, type, readDst));
1651 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001652 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001653 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1654 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001655 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001656 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001657 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001658 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001659
1660 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001661 // API presents top-to-bottom. We must preserve the padding contents. Note
1662 // that the above readPixels did not overwrite the padding.
1663 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001664 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001665 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001666 scratch.reset(tightRowBytes);
1667 void* tmpRow = scratch.get();
1668 // flip y in-place by rows
1669 const int halfY = height >> 1;
1670 char* top = reinterpret_cast<char*>(buffer);
1671 char* bottom = top + (height - 1) * rowBytes;
1672 for (int y = 0; y < halfY; y++) {
1673 memcpy(tmpRow, top, tightRowBytes);
1674 memcpy(top, bottom, tightRowBytes);
1675 memcpy(bottom, tmpRow, tightRowBytes);
1676 top += rowBytes;
1677 bottom -= rowBytes;
1678 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001679 }
1680 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001681 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001682 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001683 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001684 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001685 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001686 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001687 dst += (height-1) * rowBytes;
1688 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001689 for (int y = 0; y < height; y++) {
1690 memcpy(dst, src, tightRowBytes);
1691 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001692 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001693 dst += rowBytes;
1694 } else {
1695 dst -= rowBytes;
1696 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 }
1698 }
1699 return true;
1700}
1701
bsalomonb0bd4f62014-09-03 07:19:50 -07001702void GrGpuGL::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001703
bsalomon49f085d2014-09-05 13:34:00 -07001704 SkASSERT(target);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001705
bsalomonb0bd4f62014-09-03 07:19:50 -07001706 uint32_t rtID = target->getUniqueID();
bsalomon1c63bf62014-07-22 13:09:46 -07001707 if (fHWBoundRenderTargetUniqueID != rtID) {
robertphillips754f4e92014-09-18 13:52:08 -07001708 fGPUStats.incRenderTargetBinds();
bsalomonb0bd4f62014-09-03 07:19:50 -07001709 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001710#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001711 // don't do this check in Chromium -- this is causing
1712 // lots of repeated command buffer flushes when the compositor is
1713 // rendering with Ganesh, which is really slow; even too slow for
1714 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001715 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001716 GrGLenum status;
1717 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1718 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1719 GrPrintf("GrGpuGL::flushRenderTarget glCheckFramebufferStatus %x\n", status);
1720 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001722#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001723 fHWBoundRenderTargetUniqueID = rtID;
bsalomonb0bd4f62014-09-03 07:19:50 -07001724 const GrGLIRect& vp = target->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001725 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001727 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 }
1729 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001730 if (NULL == bound || !bound->isEmpty()) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001731 target->flagAsNeedingResolve(bound);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001732 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001733
bsalomonb0bd4f62014-09-03 07:19:50 -07001734 GrTexture *texture = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001735 if (texture) {
bsalomonafbf2d62014-09-30 12:18:44 -07001736 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001737 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001738}
1739
twiz@google.com0f31ca72011-03-18 17:38:11 +00001740GrGLenum gPrimitiveType2GLMode[] = {
1741 GR_GL_TRIANGLES,
1742 GR_GL_TRIANGLE_STRIP,
1743 GR_GL_TRIANGLE_FAN,
1744 GR_GL_POINTS,
1745 GR_GL_LINES,
1746 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001747};
1748
bsalomon@google.comd302f142011-03-03 13:54:13 +00001749#define SWAP_PER_DRAW 0
1750
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001751#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001752 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001753 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001754 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001755 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001756 void SwapBuf() {
1757 DWORD procID = GetCurrentProcessId();
1758 HWND hwnd = GetTopWindow(GetDesktopWindow());
1759 while(hwnd) {
1760 DWORD wndProcID = 0;
1761 GetWindowThreadProcessId(hwnd, &wndProcID);
1762 if(wndProcID == procID) {
1763 SwapBuffers(GetDC(hwnd));
1764 }
1765 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1766 }
1767 }
1768 #endif
1769#endif
1770
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001771void GrGpuGL::onGpuDraw(const DrawInfo& info) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001772 size_t indexOffsetInBytes;
1773 this->setupGeometry(info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001774
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001775 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001776
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001777 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001778 GrGLvoid* indices =
1779 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001780 // info.startVertex() was accounted for by setupGeometry.
1781 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1782 info.indexCount(),
1783 GR_GL_UNSIGNED_SHORT,
1784 indices));
1785 } else {
1786 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1787 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1788 // accounted for startVertex.
1789 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1790 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001791#if SWAP_PER_DRAW
1792 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001793 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001794 aglSwapBuffers(aglGetCurrentContext());
1795 int set_a_break_pt_here = 9;
1796 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001797 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001798 SwapBuf();
1799 int set_a_break_pt_here = 9;
1800 SwapBuf();
1801 #endif
1802#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001803}
1804
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001805void GrGpuGL::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001806 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001807 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001808 // Some extensions automatically resolves the texture when it is read.
1809 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001810 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
robertphillips754f4e92014-09-18 13:52:08 -07001811 fGPUStats.incRenderTargetBinds();
1812 fGPUStats.incRenderTargetBinds();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001813 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1814 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1815 // make sure we go through flushRenderTarget() since we've modified
1816 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07001817 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001818 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001819 const SkIRect dirtyRect = rt->getResolveRect();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001820 GrGLIRect r;
1821 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1822 dirtyRect.width(), dirtyRect.height(), target->origin());
reed@google.comac10a2d2010-12-22 21:39:39 +00001823
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001824 GrAutoTRestore<ScissorState> asr;
bsalomon@google.com347c3822013-05-01 20:10:01 +00001825 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001826 // Apple's extension uses the scissor as the blit bounds.
bsalomon@google.coma3201942012-06-21 19:58:20 +00001827 asr.reset(&fScissorState);
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001828 fScissorState.fEnabled = true;
1829 fScissorState.fRect = dirtyRect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001830 this->flushScissor(rt->getViewport(), rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001831 GL_CALL(ResolveMultisampleFramebuffer());
1832 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001833 int right = r.fLeft + r.fWidth;
1834 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001835
1836 // BlitFrameBuffer respects the scissor, so disable it.
1837 asr.reset(&fScissorState);
1838 fScissorState.fEnabled = false;
1839 this->flushScissor(rt->getViewport(), rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001840 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1841 r.fLeft, r.fBottom, right, top,
1842 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001843 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001845 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001846 }
1847}
1848
bsalomon@google.com411dad02012-06-05 20:24:20 +00001849namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001850
bsalomon@google.com411dad02012-06-05 20:24:20 +00001851
1852GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1853 static const GrGLenum gTable[] = {
1854 GR_GL_KEEP, // kKeep_StencilOp
1855 GR_GL_REPLACE, // kReplace_StencilOp
1856 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1857 GR_GL_INCR, // kIncClamp_StencilOp
1858 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1859 GR_GL_DECR, // kDecClamp_StencilOp
1860 GR_GL_ZERO, // kZero_StencilOp
1861 GR_GL_INVERT, // kInvert_StencilOp
1862 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001863 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001864 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1865 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1866 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1867 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1868 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1869 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1870 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1871 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001872 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001873 return gTable[op];
1874}
1875
1876void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001877 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001878 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001879 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07001880 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001881 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1882 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1883
1884 GrGLint ref = settings.funcRef(grFace);
1885 GrGLint mask = settings.funcMask(grFace);
1886 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001887
1888 if (GR_GL_FRONT_AND_BACK == glFace) {
1889 // we call the combined func just in case separate stencil is not
1890 // supported.
1891 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1892 GR_GL_CALL(gl, StencilMask(writeMask));
1893 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1894 } else {
1895 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1896 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1897 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
1898 }
1899}
1900}
bsalomon@google.comd302f142011-03-03 13:54:13 +00001901
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001902void GrGpuGL::flushStencil(DrawType type) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001903 if (kStencilPath_DrawType != type && fHWStencilSettings != fStencilSettings) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001904 if (fStencilSettings.isDisabled()) {
1905 if (kNo_TriState != fHWStencilTestEnabled) {
1906 GL_CALL(Disable(GR_GL_STENCIL_TEST));
1907 fHWStencilTestEnabled = kNo_TriState;
1908 }
1909 } else {
1910 if (kYes_TriState != fHWStencilTestEnabled) {
1911 GL_CALL(Enable(GR_GL_STENCIL_TEST));
1912 fHWStencilTestEnabled = kYes_TriState;
1913 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001914 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001915 if (!fStencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00001916 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001917 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001918 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001919 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001921 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001922 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001923 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001924 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001925 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00001926 set_gl_stencil(this->glInterface(),
bsalomon@google.coma3201942012-06-21 19:58:20 +00001927 fStencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001928 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00001930 }
1931 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00001932 fHWStencilSettings = fStencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00001933 }
1934}
1935
egdanield632ea42014-09-24 10:30:12 -07001936void GrGpuGL::flushAAState(const GrOptDrawState& optState, DrawType type) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00001937// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
1938// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
1939#if 0
1940 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
1941 #define RT_HAS_MSAA rt->isMultisampled()
1942#else
1943 #define RT_HAS_MSAA (rt->isMultisampled() || kDrawLines_DrawType == type)
1944#endif
1945
egdanield632ea42014-09-24 10:30:12 -07001946 const GrRenderTarget* rt = optState.getRenderTarget();
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00001947 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07001948 if (RT_HAS_MSAA) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001949 // FIXME: GL_NV_pr doesn't seem to like MSAA disabled. The paths
1950 // convex hulls of each segment appear to get filled.
1951 bool enableMSAA = kStencilPath_DrawType == type ||
egdanield632ea42014-09-24 10:30:12 -07001952 optState.isHWAntialiasState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001953 if (enableMSAA) {
egdanielb414f252014-07-29 13:15:47 -07001954 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001955 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07001956 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001957 }
1958 } else {
egdanielb414f252014-07-29 13:15:47 -07001959 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001960 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07001961 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00001962 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00001963 }
1964 }
1965 }
1966}
1967
egdanield632ea42014-09-24 10:30:12 -07001968void GrGpuGL::flushBlend(const GrOptDrawState& optState, bool isLines,
1969 GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
egdanielb414f252014-07-29 13:15:47 -07001970 // Any optimization to disable blending should have already been applied and
1971 // tweaked the coeffs to (1, 0).
1972 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
1973 if (blendOff) {
1974 if (kNo_TriState != fHWBlendState.fEnabled) {
1975 GL_CALL(Disable(GR_GL_BLEND));
1976 fHWBlendState.fEnabled = kNo_TriState;
1977 }
1978 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001979 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001980 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00001981 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001982 }
egdanielb414f252014-07-29 13:15:47 -07001983 if (fHWBlendState.fSrcCoeff != srcCoeff ||
1984 fHWBlendState.fDstCoeff != dstCoeff) {
1985 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
1986 gXfermodeCoeff2Blend[dstCoeff]));
1987 fHWBlendState.fSrcCoeff = srcCoeff;
1988 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00001989 }
egdanield632ea42014-09-24 10:30:12 -07001990 GrColor blendConst = optState.getBlendConstant();
egdanielb414f252014-07-29 13:15:47 -07001991 if ((BlendCoeffReferencesConstant(srcCoeff) ||
1992 BlendCoeffReferencesConstant(dstCoeff)) &&
1993 (!fHWBlendState.fConstColorValid ||
1994 fHWBlendState.fConstColor != blendConst)) {
1995 GrGLfloat c[4];
1996 GrColorToRGBAFloat(blendConst, c);
1997 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
1998 fHWBlendState.fConstColor = blendConst;
1999 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002000 }
2001 }
2002}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002003
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002004static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002005 static const GrGLenum gWrapModes[] = {
2006 GR_GL_CLAMP_TO_EDGE,
2007 GR_GL_REPEAT,
2008 GR_GL_MIRRORED_REPEAT
2009 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002010 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002011 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2012 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2013 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2014 return gWrapModes[tm];
2015}
2016
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002017void GrGpuGL::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002018 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002019
bsalomon@google.comb8670992012-07-25 21:27:09 +00002020 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2021 // 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 +00002022 // out of the "last != next" check.
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002023 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002024 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002025 this->onResolveRenderTarget(texRT);
2026 }
2027
bsalomon1c63bf62014-07-22 13:09:46 -07002028 uint32_t textureID = texture->getUniqueID();
2029 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002030 this->setTextureUnit(unitIdx);
2031 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002032 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002033 }
2034
bsalomon@google.com4c883782012-06-04 19:05:11 +00002035 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002036 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002037 bool setAll = timestamp < this->getResetTimestamp();
2038 GrGLTexture::TexParams newTexParams;
2039
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002040 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002041 GR_GL_NEAREST,
2042 GR_GL_LINEAR,
2043 GR_GL_LINEAR_MIPMAP_LINEAR
2044 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002045 static GrGLenum glMagFilterModes[] = {
2046 GR_GL_NEAREST,
2047 GR_GL_LINEAR,
2048 GR_GL_LINEAR
2049 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002050 GrTextureParams::FilterMode filterMode = params.filterMode();
2051 if (!this->caps()->mipMapSupport() && GrTextureParams::kMipMap_FilterMode == filterMode) {
2052 filterMode = GrTextureParams::kBilerp_FilterMode;
2053 }
2054 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2055 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002056
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002057 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonafbf2d62014-09-30 12:18:44 -07002058 texture->texturePriv().mipMapsAreDirty() && !GrPixelConfigIsCompressed(texture->config())) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002059 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002060 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002061 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002062
bsalomon@google.comb8670992012-07-25 21:27:09 +00002063 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2064 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002065 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002066 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002067 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002068 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002069 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002070 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2071 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002072 newTexParams.fMagFilter));
2073 }
2074 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2075 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2077 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002078 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002079 }
2080 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002081 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2083 GR_GL_TEXTURE_WRAP_S,
2084 newTexParams.fWrapS));
2085 }
2086 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002087 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002088 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2089 GR_GL_TEXTURE_WRAP_T,
2090 newTexParams.fWrapT));
2091 }
2092 if (this->glCaps().textureSwizzleSupport() &&
2093 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2094 oldTexParams.fSwizzleRGBA,
2095 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002096 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002097 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002098 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2099 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2100 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2101 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2102 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2103 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2104 } else {
2105 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2106 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2107 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2108 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002109 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002110 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002111}
2112
egdanield632ea42014-09-24 10:30:12 -07002113void GrGpuGL::flushMiscFixedFunctionState(const GrOptDrawState& optState) {
2114 if (optState.isDitherState()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002115 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002116 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002117 fHWDitherEnabled = kYes_TriState;
2118 }
2119 } else {
2120 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002121 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002122 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002123 }
2124 }
2125
egdanield632ea42014-09-24 10:30:12 -07002126 if (optState.isColorWriteDisabled()) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002127 if (kNo_TriState != fHWWriteToColor) {
2128 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2129 GR_GL_FALSE, GR_GL_FALSE));
2130 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002131 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002132 } else {
2133 if (kYes_TriState != fHWWriteToColor) {
2134 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2135 fHWWriteToColor = kYes_TriState;
2136 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002137 }
2138
egdanield632ea42014-09-24 10:30:12 -07002139 if (fHWDrawFace != optState.getDrawFace()) {
2140 switch (optState.getDrawFace()) {
tomhudson@google.com93813632011-10-27 20:21:16 +00002141 case GrDrawState::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002142 GL_CALL(Enable(GR_GL_CULL_FACE));
2143 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002144 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002145 case GrDrawState::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002146 GL_CALL(Enable(GR_GL_CULL_FACE));
2147 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002148 break;
tomhudson@google.com93813632011-10-27 20:21:16 +00002149 case GrDrawState::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002151 break;
2152 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002153 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002154 }
egdanield632ea42014-09-24 10:30:12 -07002155 fHWDrawFace = optState.getDrawFace();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002156 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002157}
2158
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002159bool GrGpuGL::configToGLFormats(GrPixelConfig config,
2160 bool getSizedInternalFormat,
2161 GrGLenum* internalFormat,
2162 GrGLenum* externalFormat,
2163 GrGLenum* externalType) {
2164 GrGLenum dontCare;
2165 if (NULL == internalFormat) {
2166 internalFormat = &dontCare;
2167 }
2168 if (NULL == externalFormat) {
2169 externalFormat = &dontCare;
2170 }
2171 if (NULL == externalType) {
2172 externalType = &dontCare;
2173 }
2174
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002175 if(!this->glCaps().isConfigTexturable(config)) {
2176 return false;
2177 }
2178
reed@google.comac10a2d2010-12-22 21:39:39 +00002179 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002180 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002181 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002182 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002183 if (getSizedInternalFormat) {
2184 *internalFormat = GR_GL_RGBA8;
2185 } else {
2186 *internalFormat = GR_GL_RGBA;
2187 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002188 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002189 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002190 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002191 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002192 if (getSizedInternalFormat) {
2193 *internalFormat = GR_GL_BGRA8;
2194 } else {
2195 *internalFormat = GR_GL_BGRA;
2196 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002197 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002198 if (getSizedInternalFormat) {
2199 *internalFormat = GR_GL_RGBA8;
2200 } else {
2201 *internalFormat = GR_GL_RGBA;
2202 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002203 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002204 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002205 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002206 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002207 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002208 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002209 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002210 if (getSizedInternalFormat) {
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002211 if (this->glStandard() == kGL_GrGLStandard) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002212 return false;
2213 } else {
2214 *internalFormat = GR_GL_RGB565;
2215 }
2216 } else {
2217 *internalFormat = GR_GL_RGB;
2218 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002219 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002220 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002221 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002222 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002223 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002224 if (getSizedInternalFormat) {
2225 *internalFormat = GR_GL_RGBA4;
2226 } else {
2227 *internalFormat = GR_GL_RGBA;
2228 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002229 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002230 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002231 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002232 // no sized/unsized internal format distinction here
2233 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002235 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002236 if (this->glCaps().textureRedSupport()) {
2237 *internalFormat = GR_GL_RED;
2238 *externalFormat = GR_GL_RED;
2239 if (getSizedInternalFormat) {
2240 *internalFormat = GR_GL_R8;
2241 } else {
2242 *internalFormat = GR_GL_RED;
2243 }
2244 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002245 } else {
2246 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002247 *externalFormat = GR_GL_ALPHA;
2248 if (getSizedInternalFormat) {
2249 *internalFormat = GR_GL_ALPHA8;
2250 } else {
2251 *internalFormat = GR_GL_ALPHA;
2252 }
2253 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002255 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002256 case kETC1_GrPixelConfig:
2257 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2258 break;
2259 case kLATC_GrPixelConfig:
2260 switch(this->glCaps().latcAlias()) {
2261 case GrGLCaps::kLATC_LATCAlias:
2262 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2263 break;
2264 case GrGLCaps::kRGTC_LATCAlias:
2265 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2266 break;
2267 case GrGLCaps::k3DC_LATCAlias:
2268 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2269 break;
2270 }
2271 break;
krajcevski238b4562014-06-30 09:09:22 -07002272 case kR11_EAC_GrPixelConfig:
2273 *internalFormat = GR_GL_COMPRESSED_R11;
2274 break;
krajcevski7ef21622014-07-16 15:21:13 -07002275
2276 case kASTC_12x12_GrPixelConfig:
2277 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2278 break;
2279
joshualittee5da552014-07-16 13:32:56 -07002280 case kRGBA_float_GrPixelConfig:
2281 *internalFormat = GR_GL_RGBA32F;
2282 *externalFormat = GR_GL_RGBA;
2283 *externalType = GR_GL_FLOAT;
2284 break;
krajcevski7ef21622014-07-16 15:21:13 -07002285
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 default:
2287 return false;
2288 }
2289 return true;
2290}
2291
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002292void GrGpuGL::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002293 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002294 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002295 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002296 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002297 }
2298}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002299
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002300void GrGpuGL::setScratchTextureUnit() {
2301 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002302 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002303 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2304 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2305 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002306 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002307 // clear out the this field so that if a program does use this unit it will rebind the correct
2308 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002309 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002310}
2311
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002312namespace {
2313// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002314inline bool can_blit_framebuffer(const GrSurface* dst,
2315 const GrSurface* src,
2316 const GrGpuGL* gpu,
2317 bool* wouldNeedTempFBO = NULL) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002318 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2319 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002320 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002321 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2322 // or the rects are not the same (not just the same size but have the same edges).
2323 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2324 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2325 return false;
2326 }
bsalomon49f085d2014-09-05 13:34:00 -07002327 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002328 *wouldNeedTempFBO = NULL == dst->asRenderTarget() || NULL == src->asRenderTarget();
2329 }
2330 return true;
2331 } else {
2332 return false;
2333 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002334}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002335
2336inline bool can_copy_texsubimage(const GrSurface* dst,
2337 const GrSurface* src,
2338 const GrGpuGL* gpu,
2339 bool* wouldNeedTempFBO = NULL) {
2340 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2341 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2342 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002343 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002344 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2345 return false;
2346 }
2347 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2348 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2349 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon49f085d2014-09-05 13:34:00 -07002350 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002351 return false;
2352 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002353 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2354 // If the src is multisampled (and uses an extension where there is a separate MSAA
2355 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon49f085d2014-09-05 13:34:00 -07002356 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002357 return false;
2358 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002359 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002360 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002361 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002362 !GrPixelConfigIsCompressed(src->config())) {
bsalomon49f085d2014-09-05 13:34:00 -07002363 if (wouldNeedTempFBO) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002364 *wouldNeedTempFBO = NULL == src->asRenderTarget();
2365 }
2366 return true;
2367 } else {
2368 return false;
2369 }
2370}
2371
robertphillips754f4e92014-09-18 13:52:08 -07002372}
2373
bsalomon@google.comeb851172013-04-15 13:51:00 +00002374// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2375// relative to is output.
robertphillips754f4e92014-09-18 13:52:08 -07002376GrGLuint GrGpuGL::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002377 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2378 GrGLuint tempFBOID;
2379 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002380 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002381 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
robertphillips754f4e92014-09-18 13:52:08 -07002382 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, &tempFBOID));
2383 fGPUStats.incRenderTargetBinds();
2384 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, tempFBOID));
2385 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2386 GR_GL_COLOR_ATTACHMENT0,
2387 GR_GL_TEXTURE_2D,
2388 texID,
2389 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002390 viewport->fLeft = 0;
2391 viewport->fBottom = 0;
2392 viewport->fWidth = surface->width();
2393 viewport->fHeight = surface->height();
2394 } else {
2395 tempFBOID = 0;
robertphillips754f4e92014-09-18 13:52:08 -07002396 fGPUStats.incRenderTargetBinds();
2397 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002398 *viewport = rt->getViewport();
2399 }
2400 return tempFBOID;
2401}
2402
bsalomon@google.comeb851172013-04-15 13:51:00 +00002403void GrGpuGL::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
2404 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002405 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002406 kBGRA_8888_GrPixelConfig == src->config()) {
2407 // glCopyTexSubImage2D doesn't work with this config. We'll want to make it a render target
bsalomon@google.com31c4e892013-04-15 13:55:02 +00002408 // in order to call glBlitFramebuffer or to copy to it by rendering.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002409 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002410 return;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002411 } else if (NULL == src->asRenderTarget()) {
2412 // We don't want to have to create an FBO just to use glCopyTexSubImage2D. Let the base
2413 // class handle it by rendering.
2414 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.coma2719852013-04-17 14:25:27 +00002415 return;
2416 }
2417
2418 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002419 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002420 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer.
2421 INHERITED::initCopySurfaceDstDesc(src, desc);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002422 } else {
2423 desc->fConfig = src->config();
2424 desc->fOrigin = src->origin();
2425 desc->fFlags = kNone_GrTextureFlags;
2426 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002427}
2428
2429bool GrGpuGL::onCopySurface(GrSurface* dst,
2430 GrSurface* src,
2431 const SkIRect& srcRect,
2432 const SkIPoint& dstPoint) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002433 bool inheritedCouldCopy = INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002434 bool copied = false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002435 bool wouldNeedTempFBO = false;
2436 if (can_copy_texsubimage(dst, src, this, &wouldNeedTempFBO) &&
2437 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
2438 GrGLuint srcFBO;
2439 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002440 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002441 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002442 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002443 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002444 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002445 GrGLIRect srcGLRect;
2446 srcGLRect.setRelativeTo(srcVP,
2447 srcRect.fLeft,
2448 srcRect.fTop,
2449 srcRect.width(),
2450 srcRect.height(),
2451 src->origin());
2452
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002453 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002454 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2455 GrGLint dstY;
2456 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2457 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2458 } else {
2459 dstY = dstPoint.fY;
2460 }
2461 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2462 dstPoint.fX, dstY,
2463 srcGLRect.fLeft, srcGLRect.fBottom,
2464 srcGLRect.fWidth, srcGLRect.fHeight));
2465 copied = true;
2466 if (srcFBO) {
2467 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2468 }
2469 } else if (can_blit_framebuffer(dst, src, this, &wouldNeedTempFBO) &&
2470 (!wouldNeedTempFBO || !inheritedCouldCopy)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002471 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2472 srcRect.width(), srcRect.height());
2473 bool selfOverlap = false;
bsalomonafbf2d62014-09-30 12:18:44 -07002474 if (dst->surfacePriv().isSameAs(src)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002475 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2476 }
2477
2478 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002479 GrGLuint dstFBO;
2480 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002481 GrGLIRect dstVP;
2482 GrGLIRect srcVP;
robertphillips754f4e92014-09-18 13:52:08 -07002483 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP);
2484 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002485 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002486 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002487 GrGLIRect srcGLRect;
2488 GrGLIRect dstGLRect;
2489 srcGLRect.setRelativeTo(srcVP,
2490 srcRect.fLeft,
2491 srcRect.fTop,
2492 srcRect.width(),
2493 srcRect.height(),
2494 src->origin());
2495 dstGLRect.setRelativeTo(dstVP,
2496 dstRect.fLeft,
2497 dstRect.fTop,
2498 dstRect.width(),
2499 dstRect.height(),
2500 dst->origin());
2501
2502 GrAutoTRestore<ScissorState> asr;
derekf8c8f71a2014-09-16 06:24:57 -07002503 // BlitFrameBuffer respects the scissor, so disable it.
2504 asr.reset(&fScissorState);
2505 fScissorState.fEnabled = false;
2506 this->flushScissor(dstGLRect, dst->origin());
2507
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002508 GrGLint srcY0;
2509 GrGLint srcY1;
2510 // Does the blit need to y-mirror or not?
2511 if (src->origin() == dst->origin()) {
2512 srcY0 = srcGLRect.fBottom;
2513 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2514 } else {
2515 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2516 srcY1 = srcGLRect.fBottom;
2517 }
2518 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2519 srcY0,
2520 srcGLRect.fLeft + srcGLRect.fWidth,
2521 srcY1,
2522 dstGLRect.fLeft,
2523 dstGLRect.fBottom,
2524 dstGLRect.fLeft + dstGLRect.fWidth,
2525 dstGLRect.fBottom + dstGLRect.fHeight,
2526 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2527 if (dstFBO) {
2528 GL_CALL(DeleteFramebuffers(1, &dstFBO));
2529 }
2530 if (srcFBO) {
2531 GL_CALL(DeleteFramebuffers(1, &srcFBO));
2532 }
2533 copied = true;
2534 }
2535 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002536 if (!copied && inheritedCouldCopy) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002537 copied = INHERITED::onCopySurface(dst, src, srcRect, dstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002538 SkASSERT(copied);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002539 }
2540 return copied;
2541}
2542
2543bool GrGpuGL::onCanCopySurface(GrSurface* dst,
2544 GrSurface* src,
2545 const SkIRect& srcRect,
2546 const SkIPoint& dstPoint) {
2547 // This mirrors the logic in onCopySurface.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002548 if (can_copy_texsubimage(dst, src, this)) {
2549 return true;
2550 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002551 if (can_blit_framebuffer(dst, src, this)) {
bsalomonafbf2d62014-09-30 12:18:44 -07002552 if (dst->surfacePriv().isSameAs(src)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002553 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2554 srcRect.width(), srcRect.height());
2555 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2556 return true;
2557 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002558 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002559 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002560 }
2561 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002562 return INHERITED::onCanCopySurface(dst, src, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002563}
2564
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002565void GrGpuGL::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002566 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002567 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002568 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002569 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002570 }
2571}
2572
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002573void GrGpuGL::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002574 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002575 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002576 }
2577}
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002578///////////////////////////////////////////////////////////////////////////////
2579
bsalomon@google.com6918d482013-03-07 19:09:11 +00002580GrGLAttribArrayState* GrGpuGL::HWGeometryState::bindArrayAndBuffersToDraw(
2581 GrGpuGL* gpu,
2582 const GrGLVertexBuffer* vbuffer,
2583 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002584 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002585 GrGLAttribArrayState* attribState;
2586
bsalomon@google.com6918d482013-03-07 19:09:11 +00002587 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2588 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002589 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002590 SkSafeUnref(fVBOVertexArray);
2591 GrGLuint arrayID;
2592 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2593 int attrCount = gpu->glCaps().maxVertexAttributes();
2594 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002595 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002596 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2597 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002598 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002599 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2600 } else {
2601 this->setVertexArrayID(gpu, 0);
2602 }
2603 int attrCount = gpu->glCaps().maxVertexAttributes();
2604 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2605 fDefaultVertexArrayAttribState.resize(attrCount);
2606 }
2607 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002608 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002609 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002610}