blob: 8710931301eae53fc39b2d930ece9f04301d7e5a [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
jvanverth39edf762014-12-22 11:44:19 -08009#include "GrGLGpu.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000010#include "GrGLStencilBuffer.h"
bsalomon37dd3312014-11-03 08:47:23 -080011#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080014#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070015#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000016#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000018#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000019#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000020#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com0b77d682011-08-19 13:28:54 +000022#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000023#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025#define SKIP_CACHE_CHECK true
26
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000027#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
28 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
29 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
30 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000032 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
35#endif
36
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000037
38///////////////////////////////////////////////////////////////////////////////
39
cdaltonb85a0aa2014-07-21 15:32:44 -070040
twiz@google.com0f31ca72011-03-18 17:38:11 +000041static const GrGLenum gXfermodeCoeff2Blend[] = {
42 GR_GL_ZERO,
43 GR_GL_ONE,
44 GR_GL_SRC_COLOR,
45 GR_GL_ONE_MINUS_SRC_COLOR,
46 GR_GL_DST_COLOR,
47 GR_GL_ONE_MINUS_DST_COLOR,
48 GR_GL_SRC_ALPHA,
49 GR_GL_ONE_MINUS_SRC_ALPHA,
50 GR_GL_DST_ALPHA,
51 GR_GL_ONE_MINUS_DST_ALPHA,
52 GR_GL_CONSTANT_COLOR,
53 GR_GL_ONE_MINUS_CONSTANT_COLOR,
54 GR_GL_CONSTANT_ALPHA,
55 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056
57 // extended blend coeffs
58 GR_GL_SRC1_COLOR,
59 GR_GL_ONE_MINUS_SRC1_COLOR,
60 GR_GL_SRC1_ALPHA,
61 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000062};
63
bsalomon861e1032014-12-16 07:33:49 -080064bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000065 static const bool gCoeffReferencesBlendConst[] = {
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 false,
76 true,
77 true,
78 true,
79 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000080
81 // extended blend coeffs
82 false,
83 false,
84 false,
85 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000086 };
87 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000088 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000089 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000090
bsalomon@google.com47059542012-06-06 20:51:20 +000091 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
92 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
93 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
94 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
95 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
97 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
98 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
99 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
101 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
103 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
104 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000105
bsalomon@google.com47059542012-06-06 20:51:20 +0000106 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
108 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
109 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
111 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000112 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000113 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000114}
115
reed@google.comac10a2d2010-12-22 21:39:39 +0000116///////////////////////////////////////////////////////////////////////////////
117
rileya@google.come38160c2012-07-03 18:03:04 +0000118static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000119
bsalomon861e1032014-12-16 07:33:49 -0800120GrGLGpu::GrGLGpu(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000121 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000122 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000123
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000124 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000125 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000126
bsalomon1c63bf62014-07-22 13:09:46 -0700127 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000128
robertphillips@google.com6177e692013-02-28 20:16:25 +0000129 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000131 const GrGLubyte* vendor;
132 const GrGLubyte* renderer;
133 const GrGLubyte* version;
134 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
135 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
136 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800137 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138 this);
tfarina38406c82014-10-31 07:11:12 -0700139 SkDebugf("------ VENDOR %s\n", vendor);
140 SkDebugf("------ RENDERER %s\n", renderer);
141 SkDebugf("------ VERSION %s\n", version);
142 SkDebugf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700143 ctx.extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700144 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800145 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000146 }
147
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000148 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000149
joshualitt2dd1ae02014-12-03 06:24:10 -0800150 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000151
bsalomon@google.comfe676522011-06-17 18:12:21 +0000152 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000153 fHWProgramID = 0;
egdaniel0f5f9672015-02-03 11:10:51 -0800154 fTempSrcFBOID = 0;
155 fTempDstFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700156
157 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700158 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700159 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000160}
161
bsalomon861e1032014-12-16 07:33:49 -0800162GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000163 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000164 // detach the current program so there is no confusion on OpenGL's part
165 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000166 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000167 GL_CALL(UseProgram(0));
168 }
169
egdaniel0f5f9672015-02-03 11:10:51 -0800170 if (0 != fTempSrcFBOID) {
171 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
172 }
173 if (0 != fTempDstFBOID) {
174 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
175 }
176
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000177 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700178}
179
bsalomon861e1032014-12-16 07:33:49 -0800180void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700181 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700182 fProgramCache->abandon();
183 fHWProgramID = 0;
egdaniel0f5f9672015-02-03 11:10:51 -0800184 fTempSrcFBOID = 0;
185 fTempDstFBOID = 0;
bsalomonc8dc1f72014-08-21 13:02:13 -0700186 if (this->glCaps().pathRenderingSupport()) {
187 this->glPathRendering()->abandonGpuResources();
188 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000189}
190
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000191///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -0800192GrPixelConfig GrGLGpu::preferredReadPixelsConfig(GrPixelConfig readConfig,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000193 GrPixelConfig surfaceConfig) const {
194 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000195 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000196 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000197 GrBytesPerPixel(readConfig) == 4 &&
198 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000199 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
200 // Perhaps this should be guarded by some compiletime or runtime check.
201 return surfaceConfig;
piotaixre4b23142014-10-02 10:57:53 -0700202 } else if (readConfig == kBGRA_8888_GrPixelConfig
203 && !this->glCaps().readPixelsSupported(
204 this->glInterface(),
205 GR_GL_BGRA,
206 GR_GL_UNSIGNED_BYTE,
207 surfaceConfig
208 )) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000209 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000210 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000211 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000212 }
213}
214
bsalomon861e1032014-12-16 07:33:49 -0800215GrPixelConfig GrGLGpu::preferredWritePixelsConfig(GrPixelConfig writeConfig,
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000216 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000217 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
218 return kBGRA_8888_GrPixelConfig;
219 } else {
220 return writeConfig;
221 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000222}
223
bsalomon861e1032014-12-16 07:33:49 -0800224bool GrGLGpu::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700225 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000226 return false;
227 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000228 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000229 // In general ES2 requires the internal format of the texture and the format of the src
230 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
231 // texture. It depends upon which extension added BGRA. The Apple extension allows it
232 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
233 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000234 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000235 !this->glCaps().bgraIsInternalFormat() &&
236 kBGRA_8888_GrPixelConfig == srcConfig &&
237 kRGBA_8888_GrPixelConfig == texture->config()) {
238 return true;
239 } else {
240 return false;
241 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000242 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000243 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000244 }
245}
246
bsalomon861e1032014-12-16 07:33:49 -0800247bool GrGLGpu::fullReadPixelsIsFasterThanPartial() const {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000248 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
249}
250
bsalomon861e1032014-12-16 07:33:49 -0800251void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000252 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000253 if (resetBits & kMisc_GrGLBackendState) {
254 GL_CALL(Disable(GR_GL_DEPTH_TEST));
255 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000256
egdaniel8dd688b2015-01-22 10:16:09 -0800257 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000258 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000260 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000261 // Desktop-only state that we never change
262 if (!this->glCaps().isCoreProfile()) {
263 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
264 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
265 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
266 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
267 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
268 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
269 }
270 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
271 // core profile. This seems like a bug since the core spec removes any mention of
272 // GL_ARB_imaging.
273 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
274 GL_CALL(Disable(GR_GL_COLOR_TABLE));
275 }
276 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
277 // Since ES doesn't support glPointSize at all we always use the VS to
278 // set the point size
279 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
280
281 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
282 // currently part of our gl interface. There are probably others as
283 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000284 }
joshualitt58162332014-08-01 06:44:53 -0700285
286 if (kGLES_GrGLStandard == this->glStandard() &&
287 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
288 // The arm extension requires specifically enabling MSAA fetching per sample.
289 // On some devices this may have a perf hit. Also multiple render targets are disabled
290 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
291 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000292 fHWWriteToColor = kUnknown_TriState;
293 // we only ever use lines in hairline mode
294 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000295 }
edisonn@google.comba669992013-06-28 16:03:21 +0000296
egdanielb414f252014-07-29 13:15:47 -0700297 if (resetBits & kMSAAEnable_GrGLBackendState) {
298 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000299 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000300
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000301 fHWActiveTextureUnitIdx = -1; // invalid
302
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000303 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700304 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
305 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000306 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000307 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000308
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000309 if (resetBits & kBlend_GrGLBackendState) {
310 fHWBlendState.invalidate();
311 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000312
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000313 if (resetBits & kView_GrGLBackendState) {
314 fHWScissorSettings.invalidate();
315 fHWViewport.invalidate();
316 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000318 if (resetBits & kStencil_GrGLBackendState) {
319 fHWStencilSettings.invalidate();
320 fHWStencilTestEnabled = kUnknown_TriState;
321 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000322
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000323 // Vertex
324 if (resetBits & kVertex_GrGLBackendState) {
325 fHWGeometryState.invalidate();
326 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000328 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700329 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000330 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000331
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000332 if (resetBits & kPathRendering_GrGLBackendState) {
333 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700334 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000335 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000336 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000337
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000338 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000339 if (resetBits & kPixelStore_GrGLBackendState) {
340 if (this->glCaps().unpackRowLengthSupport()) {
341 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
342 }
343 if (this->glCaps().packRowLengthSupport()) {
344 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
345 }
346 if (this->glCaps().unpackFlipYSupport()) {
347 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
348 }
349 if (this->glCaps().packFlipYSupport()) {
350 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
351 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000352 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000353
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000354 if (resetBits & kProgram_GrGLBackendState) {
355 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000356 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000357}
358
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000359namespace {
360
361GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
362 // By default, GrRenderTargets are GL's normal orientation so that they
363 // can be drawn to by the outside world without the client having
364 // to render upside down.
365 if (kDefault_GrSurfaceOrigin == origin) {
366 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
367 } else {
368 return origin;
369 }
370}
371
372}
373
bsalomon861e1032014-12-16 07:33:49 -0800374GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000375 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000376 return NULL;
377 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000378
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000379 if (0 == desc.fTextureHandle) {
380 return NULL;
381 }
382
bsalomon@google.combcce8922013-03-25 15:38:39 +0000383 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000384 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
385 return NULL;
386 }
387
bsalomonb15b4c12014-10-29 12:41:57 -0700388 GrGLTexture::IDDesc idDesc;
389 GrSurfaceDesc surfDesc;
390
391 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon5236cf42015-01-14 10:42:08 -0800392 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
bsalomonb15b4c12014-10-29 12:41:57 -0700393
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000394 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700395 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
396 surfDesc.fWidth = desc.fWidth;
397 surfDesc.fHeight = desc.fHeight;
398 surfDesc.fConfig = desc.fConfig;
399 surfDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000400 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000401 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
402 // assuming the old behaviour, which is that backend textures are always
403 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
404 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
405 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700406 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000407 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700408 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000409 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000410
411 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000412 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700413 GrGLRenderTarget::IDDesc rtIDDesc;
bsalomon5236cf42015-01-14 10:42:08 -0800414 if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000415 return NULL;
416 }
bsalomon37dd3312014-11-03 08:47:23 -0800417 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000418 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700419 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000420 }
421 if (NULL == texture) {
422 return NULL;
423 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000424
bsalomon@google.come269f212011-11-07 13:29:52 +0000425 return texture;
426}
427
bsalomon861e1032014-12-16 07:33:49 -0800428GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700429 GrGLRenderTarget::IDDesc idDesc;
430 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
431 idDesc.fMSColorRenderbufferID = 0;
432 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon5236cf42015-01-14 10:42:08 -0800433 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000434
bsalomonb15b4c12014-10-29 12:41:57 -0700435 GrSurfaceDesc desc;
436 desc.fConfig = wrapDesc.fConfig;
437 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
438 desc.fWidth = wrapDesc.fWidth;
439 desc.fHeight = wrapDesc.fHeight;
440 desc.fSampleCnt = wrapDesc.fSampleCnt;
441 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
442
bsalomon37dd3312014-11-03 08:47:23 -0800443 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700444 if (wrapDesc.fStencilBits) {
kkinnunen36c57df2015-01-27 00:30:18 -0800445 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.come269f212011-11-07 13:29:52 +0000446 GrGLStencilBuffer::Format format;
447 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
448 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700449 format.fStencilBits = wrapDesc.fStencilBits;
450 format.fTotalBits = wrapDesc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000451 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
452 (this,
kkinnunen36c57df2015-01-27 00:30:18 -0800453 sbDesc,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000454 desc.fWidth,
455 desc.fHeight,
456 desc.fSampleCnt,
457 format));
bsalomon6bc1b5f2015-02-23 09:06:38 -0800458 tgt->renderTargetPriv().didAttachStencilBuffer(sb);
bsalomon@google.come269f212011-11-07 13:29:52 +0000459 sb->unref();
460 }
461 return tgt;
462}
463
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000464////////////////////////////////////////////////////////////////////////////////
465
bsalomon861e1032014-12-16 07:33:49 -0800466bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000467 int left, int top, int width, int height,
468 GrPixelConfig config, const void* buffer,
469 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000470 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000471 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000472 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000473 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
474
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000475 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000476 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000477
krajcevski145d48c2014-06-11 16:07:50 -0700478 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700479 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800480 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700481 SkASSERT(config == glTex->desc().fConfig);
482 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
483 height);
krajcevski145d48c2014-06-11 16:07:50 -0700484 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700485 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
486 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700487 }
488
489 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700490 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000491 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000492 }
krajcevski145d48c2014-06-11 16:07:50 -0700493
494 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000495}
496
bsalomonb15b4c12014-10-29 12:41:57 -0700497static bool adjust_pixel_ops_params(int surfaceWidth,
498 int surfaceHeight,
499 size_t bpp,
500 int* left, int* top, int* width, int* height,
501 const void** data,
502 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000503 if (!*rowBytes) {
504 *rowBytes = *width * bpp;
505 }
506
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000507 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
508 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000509
510 if (!subRect.intersect(bounds)) {
511 return false;
512 }
513 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
514 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
515
516 *left = subRect.fLeft;
517 *top = subRect.fTop;
518 *width = subRect.width();
519 *height = subRect.height();
520 return true;
521}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000522
bsalomonb15b4c12014-10-29 12:41:57 -0700523static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
524 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700525 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000526 return GR_GL_GET_ERROR(interface);
527 } else {
528 return CHECK_ALLOC_ERROR(interface);
529 }
530}
531
bsalomon861e1032014-12-16 07:33:49 -0800532bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000533 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 int left, int top, int width, int height,
535 GrPixelConfig dataConfig,
536 const void* data,
537 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700538 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000539
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000540 // If we're uploading compressed data then we should be using uploadCompressedTexData
541 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
542
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000543 size_t bpp = GrBytesPerPixel(dataConfig);
544 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
545 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000546 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000547 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000548 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000549
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000550 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700551 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000552
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000553 // We currently lazily create MIPMAPs when the we see a draw with
554 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
555 // MIP levels are all created when the texture is created. So for now we don't use
556 // texture storage.
557 bool useTexStorage = false &&
558 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000559 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000560
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000561 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000562 // 565 is not a sized internal format on desktop GL. So on desktop with
563 // 565 we always use an unsized internal format to let the system pick
564 // the best sized format to convert the 565 data to. Since TexStorage
565 // only allows sized internal formats we will instead use TexImage2D.
566 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000567 }
568
jvanverth3f801cb2014-12-16 09:49:38 -0800569 GrGLenum internalFormat = 0x0; // suppress warning
570 GrGLenum externalFormat = 0x0; // suppress warning
571 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700572
jvanverthe1869ca2014-12-16 13:32:27 -0800573 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800574 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800575 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800576 if (kGL_GrGLStandard == this->glStandard() ||
577 (this->glVersion() >= GR_GL_VER(3, 0) &&
578 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
579 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800580 useSizedFormat = true;
581 }
jvanverthf72b7522014-12-17 10:46:01 -0800582
bsalomone904c092014-07-17 10:50:59 -0700583 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000584 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000585 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000586 }
587
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000588 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000589 * check whether to allocate a temporary buffer for flipping y or
590 * because our srcData has extra bytes past each row. If so, we need
591 * to trim those off here, since GL ES may not let us specify
592 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000593 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000594 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000595 bool swFlipY = false;
596 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700597 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000598 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000599 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000600 glFlipY = true;
601 } else {
602 swFlipY = true;
603 }
604 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000605 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000606 // can't use this for flipping, only non-neg values allowed. :(
607 if (rowBytes != trimRowBytes) {
608 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
609 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
610 restoreGLRowLength = true;
611 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000612 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000613 if (trimRowBytes != rowBytes || swFlipY) {
614 // copy data into our new storage, skipping the trailing bytes
615 size_t trimSize = height * trimRowBytes;
616 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000617 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000618 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000619 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000620 char* dst = (char*)tempStorage.reset(trimSize);
621 for (int y = 0; y < height; y++) {
622 memcpy(dst, src, trimRowBytes);
623 if (swFlipY) {
624 src -= rowBytes;
625 } else {
626 src += rowBytes;
627 }
628 dst += trimRowBytes;
629 }
630 // now point data to our copied version
631 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000632 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000633 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000634 if (glFlipY) {
635 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
636 }
joshualittee5da552014-07-16 13:32:56 -0700637 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
638 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000639 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000640 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000641 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000642 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000643 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000644 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000645 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000646 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000647 GL_ALLOC_CALL(this->glInterface(),
648 TexStorage2D(GR_GL_TEXTURE_2D,
649 1, // levels
650 internalFormat,
651 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000652 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700653 GL_ALLOC_CALL(this->glInterface(),
654 TexImage2D(GR_GL_TEXTURE_2D,
655 0, // level
656 internalFormat,
657 desc.fWidth, desc.fHeight,
658 0, // border
659 externalFormat, externalType,
660 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000661 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000662 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000663 if (error != GR_GL_NO_ERROR) {
664 succeeded = false;
665 } else {
666 // if we have data and we used TexStorage to create the texture, we
667 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700668 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000669 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
670 0, // level
671 left, top,
672 width, height,
673 externalFormat, externalType,
674 data));
675 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000676 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000677 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000678 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000679 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000680 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000681 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
682 0, // level
683 left, top,
684 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000685 externalFormat, externalType, data));
686 }
687
688 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000689 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000690 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000691 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000692 if (glFlipY) {
693 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
694 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000695 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000696}
697
krajcevski145d48c2014-06-11 16:07:50 -0700698// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700699// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700700// create a CompressedTexData struct that takes a desc/ptr and figures out
701// the proper upload semantics. Then users can construct this function how they
702// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800703bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700704 const void* data,
705 bool isNewTexture,
706 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700707 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700708
709 // No support for software flip y, yet...
710 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
711
krajcevski145d48c2014-06-11 16:07:50 -0700712 if (-1 == width) {
713 width = desc.fWidth;
714 }
715#ifdef SK_DEBUG
716 else {
717 SkASSERT(width <= desc.fWidth);
718 }
719#endif
720
721 if (-1 == height) {
722 height = desc.fHeight;
723 }
724#ifdef SK_DEBUG
725 else {
726 SkASSERT(height <= desc.fHeight);
727 }
728#endif
729
krajcevski9c0e6292014-06-02 07:38:14 -0700730 // Make sure that the width and height that we pass to OpenGL
731 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800732 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700733
734 // We only need the internal format for compressed 2D textures.
735 GrGLenum internalFormat = 0;
736 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
737 return false;
738 }
739
krajcevski145d48c2014-06-11 16:07:50 -0700740 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700741 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700742 GL_ALLOC_CALL(this->glInterface(),
743 CompressedTexImage2D(GR_GL_TEXTURE_2D,
744 0, // level
745 internalFormat,
746 width, height,
747 0, // border
bsalomon98806072014-12-12 15:11:17 -0800748 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700749 data));
bsalomond4cb9222014-08-11 14:19:09 -0700750 GrGLenum error = check_alloc_error(desc, this->glInterface());
751 if (error != GR_GL_NO_ERROR) {
752 return false;
753 }
krajcevski145d48c2014-06-11 16:07:50 -0700754 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700755 // Paletted textures can't be updated.
756 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
757 return false;
758 }
759 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
760 0, // level
761 left, top,
762 width, height,
763 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800764 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700765 data));
krajcevski145d48c2014-06-11 16:07:50 -0700766 }
krajcevski9c0e6292014-06-02 07:38:14 -0700767
bsalomond4cb9222014-08-11 14:19:09 -0700768 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700769}
770
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000771static bool renderbuffer_storage_msaa(GrGLContext& ctx,
772 int sampleCount,
773 GrGLenum format,
774 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000775 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000776 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000777 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000778 case GrGLCaps::kDesktop_ARB_MSFBOType:
779 case GrGLCaps::kDesktop_EXT_MSFBOType:
780 case GrGLCaps::kES_3_0_MSFBOType:
781 GL_ALLOC_CALL(ctx.interface(),
782 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
783 sampleCount,
784 format,
785 width, height));
786 break;
787 case GrGLCaps::kES_Apple_MSFBOType:
788 GL_ALLOC_CALL(ctx.interface(),
789 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
790 sampleCount,
791 format,
792 width, height));
793 break;
794 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
795 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
796 GL_ALLOC_CALL(ctx.interface(),
797 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
798 sampleCount,
799 format,
800 width, height));
801 break;
802 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000803 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000804 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000805 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000806 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000807}
808
bsalomon5236cf42015-01-14 10:42:08 -0800809bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700810 GrGLRenderTarget::IDDesc* idDesc) {
811 idDesc->fMSColorRenderbufferID = 0;
812 idDesc->fRTFBOID = 0;
813 idDesc->fTexFBOID = 0;
bsalomon5236cf42015-01-14 10:42:08 -0800814 idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
815 GrGpuResource::kUncached_LifeCycle;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000816
817 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000818
bsalomon@google.comab15d612011-08-09 12:57:56 +0000819 GrGLenum msColorFormat = 0; // suppress warning
820
bsalomonb15b4c12014-10-29 12:41:57 -0700821 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000822 goto FAILED;
823 }
824
bsalomonb15b4c12014-10-29 12:41:57 -0700825 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
826 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000827 goto FAILED;
828 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000829
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000830
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000831 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
832 // the texture bound to the other. The exception is the IMG multisample extension. With this
833 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
834 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700835 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
836 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
837 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
838 if (!idDesc->fRTFBOID ||
839 !idDesc->fMSColorRenderbufferID ||
840 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000841 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000842 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000843 &msColorFormat,
844 NULL,
845 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000846 goto FAILED;
847 }
848 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700849 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000850 }
851
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000852 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700853 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomonb15b4c12014-10-29 12:41:57 -0700854 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
855 SkASSERT(desc.fSampleCnt > 0);
856 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000857 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700858 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000859 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700860 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000861 goto FAILED;
862 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800863 fStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700864 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000865 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000866 GR_GL_COLOR_ATTACHMENT0,
867 GR_GL_RENDERBUFFER,
bsalomonb15b4c12014-10-29 12:41:57 -0700868 idDesc->fMSColorRenderbufferID));
869 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
870 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000871 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
872 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
873 goto FAILED;
874 }
bsalomonb15b4c12014-10-29 12:41:57 -0700875 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000876 }
877 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800878 fStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700879 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000880
bsalomonb15b4c12014-10-29 12:41:57 -0700881 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000882 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
883 GR_GL_COLOR_ATTACHMENT0,
884 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700885 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000886 } else {
887 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
888 GR_GL_COLOR_ATTACHMENT0,
889 GR_GL_TEXTURE_2D,
890 texID, 0));
891 }
bsalomonb15b4c12014-10-29 12:41:57 -0700892 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
893 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000894 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
895 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
896 goto FAILED;
897 }
bsalomonb15b4c12014-10-29 12:41:57 -0700898 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000899 }
900
901 return true;
902
903FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700904 if (idDesc->fMSColorRenderbufferID) {
905 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000906 }
bsalomonb15b4c12014-10-29 12:41:57 -0700907 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
908 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000909 }
bsalomonb15b4c12014-10-29 12:41:57 -0700910 if (idDesc->fTexFBOID) {
911 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000912 }
913 return false;
914}
915
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000916// good to set a break-point here to know when createTexture fails
917static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000918// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000919 return NULL;
920}
921
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000922#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000923static size_t as_size_t(int x) {
924 return x;
925}
926#endif
927
bsalomon5236cf42015-01-14 10:42:08 -0800928GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
929 const void* srcData, size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000930
bsalomonb15b4c12014-10-29 12:41:57 -0700931 GrSurfaceDesc desc = origDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000932
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000933 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000934 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000935 // We fail if the MSAA was requested and is not available.
936 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700937 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000938 return return_null_texture();
939 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000940
bsalomonf2703d82014-10-28 14:33:06 -0700941 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
bsalomonb15b4c12014-10-29 12:41:57 -0700943 // If the sample count exceeds the max then we clamp it.
944 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
945 desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000946
bsalomonb15b4c12014-10-29 12:41:57 -0700947 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700948 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000949 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000950 }
951
reed@google.comac10a2d2010-12-22 21:39:39 +0000952 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000953 int maxRTSize = this->caps()->maxRenderTargetSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700954 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000955 return return_null_texture();
956 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000957 } else {
958 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700959 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000960 return return_null_texture();
961 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000962 }
963
bsalomonb15b4c12014-10-29 12:41:57 -0700964 GrGLTexture::IDDesc idDesc;
965 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -0800966 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
967 GrGpuResource::kUncached_LifeCycle;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000968
bsalomonb15b4c12014-10-29 12:41:57 -0700969 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000970 return return_null_texture();
971 }
972
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000973 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700974 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000975
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000976 if (renderTarget && this->glCaps().textureUsageSupport()) {
977 // provides a hint about how this texture will be used
978 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
979 GR_GL_TEXTURE_USAGE,
980 GR_GL_FRAMEBUFFER_ATTACHMENT));
981 }
982
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000983 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
984 // drivers have a bug where an FBO won't be complete if it includes a
985 // texture that is not mipmap complete (considering the filter in use).
986 GrGLTexture::TexParams initialTexParams;
987 // we only set a subset here so invalidate first
988 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000989 initialTexParams.fMinFilter = GR_GL_NEAREST;
990 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000991 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
992 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000993 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
994 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000995 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000996 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
997 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000998 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000999 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1000 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001001 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001002 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1003 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001004 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -07001005 if (!this->uploadTexData(desc, true, 0, 0,
1006 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001007 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001008 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001009 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001010 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001012 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001014 // unbind the texture from the texture unit before binding it to the frame buffer
1015 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001016 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001017
bsalomon5236cf42015-01-14 10:42:08 -08001018 if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001019 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001020 return return_null_texture();
1021 }
bsalomon37dd3312014-11-03 08:47:23 -08001022 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001023 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001024 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001025 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001026 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001027#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001028 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001029 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001030#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001031 return tex;
1032}
1033
bsalomon5236cf42015-01-14 10:42:08 -08001034GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
1035 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001036
bsalomonb15b4c12014-10-29 12:41:57 -07001037 if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
krajcevski9c0e6292014-06-02 07:38:14 -07001038 return return_null_texture();
1039 }
1040
1041 // Make sure that we're not flipping Y.
bsalomonb15b4c12014-10-29 12:41:57 -07001042 GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
krajcevski9c0e6292014-06-02 07:38:14 -07001043 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1044 return return_null_texture();
1045 }
bsalomonb15b4c12014-10-29 12:41:57 -07001046 GrSurfaceDesc desc = origDesc;
1047 desc.fOrigin = texOrigin;
krajcevski9c0e6292014-06-02 07:38:14 -07001048
1049 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -07001050 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
krajcevski9c0e6292014-06-02 07:38:14 -07001051 return return_null_texture();
1052 }
1053
bsalomonb15b4c12014-10-29 12:41:57 -07001054 GrGLTexture::IDDesc idDesc;
1055 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -08001056 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
1057 GrGpuResource::kUncached_LifeCycle;
krajcevski9c0e6292014-06-02 07:38:14 -07001058
bsalomonb15b4c12014-10-29 12:41:57 -07001059 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001060 return return_null_texture();
1061 }
1062
1063 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001064 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001065
1066 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1067 // drivers have a bug where an FBO won't be complete if it includes a
1068 // texture that is not mipmap complete (considering the filter in use).
1069 GrGLTexture::TexParams initialTexParams;
1070 // we only set a subset here so invalidate first
1071 initialTexParams.invalidate();
1072 initialTexParams.fMinFilter = GR_GL_NEAREST;
1073 initialTexParams.fMagFilter = GR_GL_NEAREST;
1074 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1075 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1076 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1077 GR_GL_TEXTURE_MAG_FILTER,
1078 initialTexParams.fMagFilter));
1079 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1080 GR_GL_TEXTURE_MIN_FILTER,
1081 initialTexParams.fMinFilter));
1082 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1083 GR_GL_TEXTURE_WRAP_S,
1084 initialTexParams.fWrapS));
1085 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1086 GR_GL_TEXTURE_WRAP_T,
1087 initialTexParams.fWrapT));
1088
bsalomonb15b4c12014-10-29 12:41:57 -07001089 if (!this->uploadCompressedTexData(desc, srcData)) {
1090 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001091 return return_null_texture();
1092 }
1093
1094 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001095 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001096 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1097#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001098 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001099 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1100#endif
1101 return tex;
1102}
1103
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001104namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001105
1106const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1107
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001110
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001111 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001112 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001113 (kUnknownBitCount == format->fTotalBits));
1114 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001115 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001116 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1117 (GrGLint*)&format->fStencilBits);
1118 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001119 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001120 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1121 (GrGLint*)&format->fTotalBits);
1122 format->fTotalBits += format->fStencilBits;
1123 } else {
1124 format->fTotalBits = format->fStencilBits;
1125 }
1126 }
1127}
1128}
1129
bsalomon02a44a42015-02-19 09:09:00 -08001130bool GrGLGpu::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001131 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001132 // 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 +00001133 SkASSERT(rt->asTexture());
1134 SkASSERT(width >= rt->width());
1135 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136
1137 int samples = rt->numSamples();
kkinnunen36c57df2015-01-27 00:30:18 -08001138 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001140 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141 for (int i = 0; i < stencilFmtCnt; ++i) {
kkinnunen36c57df2015-01-27 00:30:18 -08001142 if (!sbDesc.fRenderbufferID) {
1143 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon12299ab2014-11-14 13:33:09 -08001144 }
kkinnunen36c57df2015-01-27 00:30:18 -08001145 if (!sbDesc.fRenderbufferID) {
bsalomon12299ab2014-11-14 13:33:09 -08001146 return false;
1147 }
kkinnunen36c57df2015-01-27 00:30:18 -08001148 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001149 // we start with the last stencil format that succeeded in hopes
1150 // that we won't go through this loop more than once after the
1151 // first (painful) stencil creation.
1152 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001153 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001154 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001155 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001157 bool created;
1158 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001159 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001160 samples,
1161 sFmt.fInternalFormat,
1162 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001164 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1165 sFmt.fInternalFormat,
1166 width, height));
1167 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001169 if (created) {
kkinnunen36c57df2015-01-27 00:30:18 -08001170
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001171 // After sized formats we attempt an unsized format and take
1172 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001173 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001174 get_stencil_rb_sizes(this->glInterface(), &format);
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001175 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
kkinnunen36c57df2015-01-27 00:30:18 -08001176 (this, sbDesc, width, height, samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1178 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon6bc1b5f2015-02-23 09:06:38 -08001179 rt->renderTargetPriv().didAttachStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001181 }
bsalomon10e23ca2014-11-25 05:52:06 -08001182 // Remove the scratch key from this resource so we don't grab it from the cache ever
1183 // again.
bsalomon3582d3e2015-02-13 14:20:05 -08001184 sb->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -08001185 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
kkinnunen36c57df2015-01-27 00:30:18 -08001186 sbDesc.fRenderbufferID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 }
1188 }
kkinnunen36c57df2015-01-27 00:30:18 -08001189 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001190 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191}
1192
bsalomon861e1032014-12-16 07:33:49 -08001193bool GrGLGpu::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001194 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195
1196 GrGLuint fbo = glrt->renderFBOID();
1197
1198 if (NULL == sb) {
bsalomon6bc1b5f2015-02-23 09:06:38 -08001199 if (rt->renderTargetPriv().getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001200 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001201 GR_GL_STENCIL_ATTACHMENT,
1202 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001203 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001204 GR_GL_DEPTH_ATTACHMENT,
1205 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001206#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001207 GrGLenum status;
1208 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001209 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210#endif
1211 }
1212 return true;
1213 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001214 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 GrGLuint rb = glsb->renderbufferID();
1216
bsalomon1c63bf62014-07-22 13:09:46 -07001217 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
mtkleinb9eb4ac2015-02-02 18:26:03 -08001218 fStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001221 GR_GL_STENCIL_ATTACHMENT,
1222 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001225 GR_GL_DEPTH_ATTACHMENT,
1226 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001229 GR_GL_DEPTH_ATTACHMENT,
1230 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001233 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001234 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001235 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1236 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001238 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001240 if (glsb->format().fPacked) {
1241 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1242 GR_GL_DEPTH_ATTACHMENT,
1243 GR_GL_RENDERBUFFER, 0));
1244 }
1245 return false;
1246 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001247 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001248 rt->config(),
1249 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001251 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001252 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001254}
1255
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001256////////////////////////////////////////////////////////////////////////////////
1257
bsalomon861e1032014-12-16 07:33:49 -08001258GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001259 GrGLVertexBuffer::Desc desc;
1260 desc.fDynamic = dynamic;
1261 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001262
bsalomon@google.com96966a52013-02-21 16:34:21 +00001263 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001264 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001265 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001267 } else {
1268 GL_CALL(GenBuffers(1, &desc.fID));
1269 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001270 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001271 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1272 // make sure driver can allocate memory for this buffer
1273 GL_ALLOC_CALL(this->glInterface(),
1274 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001275 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001276 NULL, // data ptr
1277 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1278 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1279 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001280 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001281 return NULL;
1282 }
1283 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1284 return vertexBuffer;
1285 }
1286 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001288}
1289
bsalomon861e1032014-12-16 07:33:49 -08001290GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001291 GrGLIndexBuffer::Desc desc;
1292 desc.fDynamic = dynamic;
1293 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001294
bsalomon@google.com96966a52013-02-21 16:34:21 +00001295 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001296 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001297 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001299 } else {
1300 GL_CALL(GenBuffers(1, &desc.fID));
1301 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001302 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001303 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1304 // make sure driver can allocate memory for this buffer
1305 GL_ALLOC_CALL(this->glInterface(),
1306 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001307 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001308 NULL, // data ptr
1309 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1310 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1311 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001312 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001313 return NULL;
1314 }
1315 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1316 return indexBuffer;
1317 }
1318 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001319 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001320}
1321
bsalomon3e791242014-12-17 13:43:13 -08001322void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001323 const GrGLIRect& rtViewport,
1324 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001325 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001326 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001327 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001328 scissorState.rect().fLeft,
1329 scissorState.rect().fTop,
1330 scissorState.rect().width(),
1331 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001332 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001333 // if the scissor fully contains the viewport then we fall through and
1334 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001335 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001336 if (fHWScissorSettings.fRect != scissor) {
1337 scissor.pushToGLScissor(this->glInterface());
1338 fHWScissorSettings.fRect = scissor;
1339 }
1340 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1341 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1342 fHWScissorSettings.fEnabled = kYes_TriState;
1343 }
1344 return;
1345 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
joshualitt77b13072014-10-27 14:51:01 -07001347
1348 // See fall through note above
1349 this->disableScissor();
1350}
1351
joshualitt17e73142015-01-21 11:52:36 -08001352bool GrGLGpu::flushGLState(const DrawArgs& args, bool isLineDraw) {
egdaniel080e6732014-12-22 07:35:52 -08001353 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001354 const GrPipeline& pipeline = *args.fPipeline;
1355 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001356
egdaniel8dd688b2015-01-22 10:16:09 -08001357 this->flushDither(pipeline.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001358 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001359 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001360
joshualitt873ad0e2015-01-20 09:08:51 -08001361 fCurrentProgram.reset(fProgramCache->getProgram(args));
bsalomon1f78c0a2014-12-17 09:43:13 -08001362 if (NULL == fCurrentProgram.get()) {
1363 SkDEBUGFAIL("Failed to create program!");
1364 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001365 }
1366
bsalomon1f78c0a2014-12-17 09:43:13 -08001367 fCurrentProgram.get()->ref();
1368
1369 GrGLuint programID = fCurrentProgram->programID();
1370 if (fHWProgramID != programID) {
1371 GL_CALL(UseProgram(programID));
1372 fHWProgramID = programID;
1373 }
1374
egdaniel080e6732014-12-22 07:35:52 -08001375 if (blendInfo.fWriteColor) {
1376 this->flushBlend(blendInfo);
1377 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001378
egdaniel8dd688b2015-01-22 10:16:09 -08001379 fCurrentProgram->setData(*args.fPrimitiveProcessor, pipeline, *args.fBatchTracker);
bsalomon1f78c0a2014-12-17 09:43:13 -08001380
egdaniel8dd688b2015-01-22 10:16:09 -08001381 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1382 this->flushStencil(pipeline.getStencil());
1383 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
1384 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), isLineDraw);
bsalomonbc3d0de2014-12-15 13:45:03 -08001385
1386 // This must come after textures are flushed because a texture may need
1387 // to be msaa-resolved (which will modify bound FBO state).
1388 this->flushRenderTarget(glRT, NULL);
1389
1390 return true;
1391}
1392
joshualitt873ad0e2015-01-20 09:08:51 -08001393void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomonbc3d0de2014-12-15 13:45:03 -08001394 const GrDrawTarget::DrawInfo& info,
1395 size_t* indexOffsetInBytes) {
1396 GrGLVertexBuffer* vbuf;
1397 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1398
1399 SkASSERT(vbuf);
1400 SkASSERT(!vbuf->isMapped());
1401
1402 GrGLIndexBuffer* ibuf = NULL;
1403 if (info.isIndexed()) {
1404 SkASSERT(indexOffsetInBytes);
1405
1406 *indexOffsetInBytes = 0;
1407 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1408
1409 SkASSERT(ibuf);
1410 SkASSERT(!ibuf->isMapped());
1411 *indexOffsetInBytes += ibuf->baseOffset();
1412 }
1413 GrGLAttribArrayState* attribState =
1414 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1415
joshualitt873ad0e2015-01-20 09:08:51 -08001416 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001417 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001418
joshualitt873ad0e2015-01-20 09:08:51 -08001419 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001420
1421 size_t vertexOffsetInBytes = stride * info.startVertex();
1422
1423 vertexOffsetInBytes += vbuf->baseOffset();
1424
bsalomonbc3d0de2014-12-15 13:45:03 -08001425 uint32_t usedAttribArraysMask = 0;
1426 size_t offset = 0;
1427
1428 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001429 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001430 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001431 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001432 attribState->set(this,
1433 attribIndex,
1434 vbuf,
1435 GrGLAttribTypeToLayout(attribType).fCount,
1436 GrGLAttribTypeToLayout(attribType).fType,
1437 GrGLAttribTypeToLayout(attribType).fNormalized,
1438 stride,
1439 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001440 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001441 }
1442 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1443 }
1444}
1445
joshualitt873ad0e2015-01-20 09:08:51 -08001446void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1447 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -08001448 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -08001449 const GrBatchTracker& batchTracker) const {
bsalomon50785a32015-02-06 07:02:37 -08001450 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this, batchTracker)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001451 SkDEBUGFAIL("Failed to generate GL program descriptor");
1452 }
1453}
1454
bsalomon861e1032014-12-16 07:33:49 -08001455void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001456 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001457 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001458 fHWScissorSettings.fEnabled = kNo_TriState;
1459 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 }
1461}
1462
bsalomon861e1032014-12-16 07:33:49 -08001463void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001464 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001465 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001466 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001467 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001468
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001469 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1470 rect = NULL;
1471 }
1472
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001473 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001474 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001475 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001476 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001477 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001478 if (clippedRect.intersect(rtRect)) {
1479 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001480 } else {
1481 return;
1482 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001483 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001484
bsalomonb0bd4f62014-09-03 07:19:50 -07001485 this->flushRenderTarget(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001486 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001487 if (rect) {
1488 scissorState.set(*rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001489 }
joshualitt77b13072014-10-27 14:51:01 -07001490 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001491
1492 GrGLfloat r, g, b, a;
1493 static const GrGLfloat scale255 = 1.f / 255.f;
1494 a = GrColorUnpackA(color) * scale255;
1495 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001496 r = GrColorUnpackR(color) * scaleRGB;
1497 g = GrColorUnpackG(color) * scaleRGB;
1498 b = GrColorUnpackB(color) * scaleRGB;
1499
1500 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001501 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001502 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001503 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001504}
1505
bsalomon861e1032014-12-16 07:33:49 -08001506void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001507 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001508 if (!this->caps()->discardRenderTargetSupport()) {
1509 return;
1510 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001511
1512 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001513 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1514 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
mtkleinb9eb4ac2015-02-02 18:26:03 -08001515 fStats.incRenderTargetBinds();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001516 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1517 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001518 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001519 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001520 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001521 break;
1522 case GrGLCaps::kInvalidate_InvalidateFBType:
1523 if (0 == glRT->renderFBOID()) {
1524 // When rendering to the default framebuffer the legal values for attachments
1525 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1526 // types.
1527 static const GrGLenum attachments[] = { GR_GL_COLOR };
1528 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1529 attachments));
1530 } else {
1531 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1532 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1533 attachments));
1534 }
1535 break;
1536 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001537 if (0 == glRT->renderFBOID()) {
1538 // When rendering to the default framebuffer the legal values for attachments
1539 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1540 // types. See glDiscardFramebuffer() spec.
1541 static const GrGLenum attachments[] = { GR_GL_COLOR };
1542 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1543 attachments));
1544 } else {
1545 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1546 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1547 attachments));
1548 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001549 break;
1550 }
1551 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001552 renderTarget->flagAsResolved();
1553}
1554
1555
bsalomon861e1032014-12-16 07:33:49 -08001556void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001557 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001558 return;
1559 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001560 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1561 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001562
joshualitt77b13072014-10-27 14:51:01 -07001563 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001564
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001565 GL_CALL(StencilMask(0xffffffff));
1566 GL_CALL(ClearStencil(0));
1567 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001568 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001569}
1570
bsalomon861e1032014-12-16 07:33:49 -08001571void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001572 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001573
bsalomon6bc1b5f2015-02-23 09:06:38 -08001574 GrStencilBuffer* sb = target->renderTargetPriv().getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001575 // this should only be called internally when we know we have a
1576 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001577 SkASSERT(sb);
1578 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001579#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001580 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001581 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001582#else
1583 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001584 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001585 // turned into draws. Our contract on GrDrawTarget says that
1586 // changing the clip between stencil passes may or may not
1587 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001588 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001589#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001590 GrGLint value;
1591 if (insideClip) {
1592 value = (1 << (stencilBitCount - 1));
1593 } else {
1594 value = 0;
1595 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001596 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1597 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001598
bsalomon3e791242014-12-17 13:43:13 -08001599 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001600 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001601 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001602
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001603 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001604 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001605 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001606 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
bsalomon861e1032014-12-16 07:33:49 -08001609bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001610 int left, int top,
1611 int width, int height,
1612 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001613 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001614 // If this rendertarget is aready TopLeft, we don't need to flip.
1615 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1616 return false;
1617 }
1618
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001619 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001620 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001621 return false;
1622 }
1623
1624 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001625 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001626 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001627 return true;
1628 }
1629 // If we have to do memcpys to handle rowBytes then y-flip is free
1630 // Note the rowBytes might be tight to the passed in data, but if data
1631 // gets clipped in x to the target the rowBytes will no longer be tight.
1632 if (left >= 0 && (left + width) < renderTarget->width()) {
1633 return 0 == rowBytes ||
1634 GrBytesPerPixel(config) * width == rowBytes;
1635 } else {
1636 return false;
1637 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001638}
1639
bsalomon861e1032014-12-16 07:33:49 -08001640bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001641 int left, int top,
1642 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001643 GrPixelConfig config,
1644 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001645 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001646 // We cannot read pixels into a compressed buffer
1647 if (GrPixelConfigIsCompressed(config)) {
1648 return false;
1649 }
1650
1651 GrGLenum format = 0;
1652 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001653 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001654 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001656 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001657 size_t bpp = GrBytesPerPixel(config);
1658 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1659 &left, &top, &width, &height,
1660 const_cast<const void**>(&buffer),
1661 &rowBytes)) {
1662 return false;
1663 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001664
bsalomon@google.comc6980972011-11-02 19:57:21 +00001665 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001666 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001667 switch (tgt->getResolveType()) {
1668 case GrGLRenderTarget::kCantResolve_ResolveType:
1669 return false;
1670 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon37dd3312014-11-03 08:47:23 -08001671 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001672 break;
1673 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001674 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001675 // we don't track the state of the READ FBO ID.
mtkleinb9eb4ac2015-02-02 18:26:03 -08001676 fStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001677 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1678 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001679 break;
1680 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001681 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 }
1683
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001684 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001685
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001686 // the read rect is viewport-relative
1687 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001688 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001689
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001690 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001691 if (0 == rowBytes) {
1692 rowBytes = tightRowBytes;
1693 }
1694 size_t readDstRowBytes = tightRowBytes;
1695 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001696
bsalomon@google.comc6980972011-11-02 19:57:21 +00001697 // determine if GL can read using the passed rowBytes or if we need
1698 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001699 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001700 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001701 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001702 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001703 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001704 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001705 readDstRowBytes = rowBytes;
1706 } else {
1707 scratch.reset(tightRowBytes * height);
1708 readDst = scratch.get();
1709 }
1710 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001711 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001712 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1713 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001714 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1715 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001716 format, type, readDst));
1717 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001718 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001719 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1720 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001721 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001722 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001723 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001724 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001725
1726 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001727 // API presents top-to-bottom. We must preserve the padding contents. Note
1728 // that the above readPixels did not overwrite the padding.
1729 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001730 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001731 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001732 scratch.reset(tightRowBytes);
1733 void* tmpRow = scratch.get();
1734 // flip y in-place by rows
1735 const int halfY = height >> 1;
1736 char* top = reinterpret_cast<char*>(buffer);
1737 char* bottom = top + (height - 1) * rowBytes;
1738 for (int y = 0; y < halfY; y++) {
1739 memcpy(tmpRow, top, tightRowBytes);
1740 memcpy(top, bottom, tightRowBytes);
1741 memcpy(bottom, tmpRow, tightRowBytes);
1742 top += rowBytes;
1743 bottom -= rowBytes;
1744 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001745 }
1746 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001747 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001748 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001749 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001750 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001751 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001752 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001753 dst += (height-1) * rowBytes;
1754 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001755 for (int y = 0; y < height; y++) {
1756 memcpy(dst, src, tightRowBytes);
1757 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001758 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001759 dst += rowBytes;
1760 } else {
1761 dst -= rowBytes;
1762 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
1764 }
1765 return true;
1766}
1767
bsalomon861e1032014-12-16 07:33:49 -08001768void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001769
bsalomon49f085d2014-09-05 13:34:00 -07001770 SkASSERT(target);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001771
bsalomonb0bd4f62014-09-03 07:19:50 -07001772 uint32_t rtID = target->getUniqueID();
bsalomon1c63bf62014-07-22 13:09:46 -07001773 if (fHWBoundRenderTargetUniqueID != rtID) {
mtkleinb9eb4ac2015-02-02 18:26:03 -08001774 fStats.incRenderTargetBinds();
bsalomonb0bd4f62014-09-03 07:19:50 -07001775 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001776#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001777 // don't do this check in Chromium -- this is causing
1778 // lots of repeated command buffer flushes when the compositor is
1779 // rendering with Ganesh, which is really slow; even too slow for
1780 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001781 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001782 GrGLenum status;
1783 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1784 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon861e1032014-12-16 07:33:49 -08001785 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
rmistry@google.comd6bab022013-12-02 13:50:38 +00001786 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001787 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001788#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001789 fHWBoundRenderTargetUniqueID = rtID;
bsalomonb0bd4f62014-09-03 07:19:50 -07001790 const GrGLIRect& vp = target->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001791 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001792 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001793 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001794 }
1795 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001796 if (NULL == bound || !bound->isEmpty()) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001797 target->flagAsNeedingResolve(bound);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001798 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001799
bsalomonb0bd4f62014-09-03 07:19:50 -07001800 GrTexture *texture = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001801 if (texture) {
bsalomonafbf2d62014-09-30 12:18:44 -07001802 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001803 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001804}
1805
twiz@google.com0f31ca72011-03-18 17:38:11 +00001806GrGLenum gPrimitiveType2GLMode[] = {
1807 GR_GL_TRIANGLES,
1808 GR_GL_TRIANGLE_STRIP,
1809 GR_GL_TRIANGLE_FAN,
1810 GR_GL_POINTS,
1811 GR_GL_LINES,
1812 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001813};
1814
bsalomon@google.comd302f142011-03-03 13:54:13 +00001815#define SWAP_PER_DRAW 0
1816
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001817#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001818 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001819 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001820 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001821 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001822 void SwapBuf() {
1823 DWORD procID = GetCurrentProcessId();
1824 HWND hwnd = GetTopWindow(GetDesktopWindow());
1825 while(hwnd) {
1826 DWORD wndProcID = 0;
1827 GetWindowThreadProcessId(hwnd, &wndProcID);
1828 if(wndProcID == procID) {
1829 SwapBuffers(GetDC(hwnd));
1830 }
1831 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1832 }
1833 }
1834 #endif
1835#endif
1836
joshualitt873ad0e2015-01-20 09:08:51 -08001837void GrGLGpu::onDraw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
joshualitt17e73142015-01-21 11:52:36 -08001838 if (!this->flushGLState(args, GrIsPrimTypeLines(info.primitiveType()))) {
bsalomond95263c2014-12-16 13:05:12 -08001839 return;
1840 }
1841
joshualitt873ad0e2015-01-20 09:08:51 -08001842 size_t indexOffsetInBytes = 0;
1843 this->setupGeometry(*args.fPrimitiveProcessor, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001844
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001845 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001846
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001847 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001848 GrGLvoid* indices =
1849 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001850 // info.startVertex() was accounted for by setupGeometry.
1851 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1852 info.indexCount(),
1853 GR_GL_UNSIGNED_SHORT,
1854 indices));
1855 } else {
1856 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1857 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1858 // accounted for startVertex.
1859 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1860 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001861#if SWAP_PER_DRAW
1862 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001863 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001864 aglSwapBuffers(aglGetCurrentContext());
1865 int set_a_break_pt_here = 9;
1866 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001867 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001868 SwapBuf();
1869 int set_a_break_pt_here = 9;
1870 SwapBuf();
1871 #endif
1872#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001873}
1874
bsalomon3e791242014-12-17 13:43:13 -08001875void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001876 this->flushColorWrite(false);
egdaniel8dd688b2015-01-22 10:16:09 -08001877 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon3e791242014-12-17 13:43:13 -08001878
1879 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001880 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001881 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1882 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
1883 this->flushHWAAState(rt, state.fUseHWAA, false);
bsalomon1f78c0a2014-12-17 09:43:13 -08001884 this->flushRenderTarget(rt, NULL);
1885
bsalomon3e791242014-12-17 13:43:13 -08001886 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001887}
1888
joshualitt873ad0e2015-01-20 09:08:51 -08001889void GrGLGpu::onDrawPath(const DrawArgs& args, const GrPath* path,
bsalomond95263c2014-12-16 13:05:12 -08001890 const GrStencilSettings& stencil) {
joshualitt17e73142015-01-21 11:52:36 -08001891 if (!this->flushGLState(args, false)) {
bsalomond95263c2014-12-16 13:05:12 -08001892 return;
1893 }
1894 fPathRendering->drawPath(path, stencil);
1895}
1896
joshualitt873ad0e2015-01-20 09:08:51 -08001897void GrGLGpu::onDrawPaths(const DrawArgs& args,
bsalomond95263c2014-12-16 13:05:12 -08001898 const GrPathRange* pathRange,
1899 const void* indices,
1900 GrDrawTarget::PathIndexType indexType,
1901 const float transformValues[],
1902 GrDrawTarget::PathTransformType transformType,
1903 int count,
1904 const GrStencilSettings& stencil) {
joshualitt17e73142015-01-21 11:52:36 -08001905 if (!this->flushGLState(args, false)) {
bsalomond95263c2014-12-16 13:05:12 -08001906 return;
1907 }
1908 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1909 transformType, count, stencil);
1910}
1911
bsalomon861e1032014-12-16 07:33:49 -08001912void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001913 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001914 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001915 // Some extensions automatically resolves the texture when it is read.
1916 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001917 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
mtkleinb9eb4ac2015-02-02 18:26:03 -08001918 fStats.incRenderTargetBinds();
1919 fStats.incRenderTargetBinds();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001920 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1921 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1922 // make sure we go through flushRenderTarget() since we've modified
1923 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07001924 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001925 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001926 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00001927
bsalomon@google.com347c3822013-05-01 20:10:01 +00001928 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001929 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08001930 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001931 scissorState.set(dirtyRect);
1932 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001933 GL_CALL(ResolveMultisampleFramebuffer());
1934 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08001935 GrGLIRect r;
1936 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1937 dirtyRect.width(), dirtyRect.height(), target->origin());
1938
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001939 int right = r.fLeft + r.fWidth;
1940 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001941
1942 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07001943 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001944 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1945 r.fLeft, r.fBottom, right, top,
1946 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001947 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001948 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001949 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001950 }
1951}
1952
bsalomon@google.com411dad02012-06-05 20:24:20 +00001953namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001954
bsalomon@google.com411dad02012-06-05 20:24:20 +00001955
1956GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1957 static const GrGLenum gTable[] = {
1958 GR_GL_KEEP, // kKeep_StencilOp
1959 GR_GL_REPLACE, // kReplace_StencilOp
1960 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1961 GR_GL_INCR, // kIncClamp_StencilOp
1962 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1963 GR_GL_DECR, // kDecClamp_StencilOp
1964 GR_GL_ZERO, // kZero_StencilOp
1965 GR_GL_INVERT, // kInvert_StencilOp
1966 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001967 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001968 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1969 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1970 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1971 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1972 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1973 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1974 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1975 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001976 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001977 return gTable[op];
1978}
1979
1980void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001981 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001982 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001983 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07001984 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001985 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1986 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1987
1988 GrGLint ref = settings.funcRef(grFace);
1989 GrGLint mask = settings.funcMask(grFace);
1990 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001991
1992 if (GR_GL_FRONT_AND_BACK == glFace) {
1993 // we call the combined func just in case separate stencil is not
1994 // supported.
1995 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1996 GR_GL_CALL(gl, StencilMask(writeMask));
1997 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1998 } else {
1999 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2000 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
2001 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2002 }
2003}
2004}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002005
bsalomon3e791242014-12-17 13:43:13 -08002006void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2007 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002008 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002009 if (kNo_TriState != fHWStencilTestEnabled) {
2010 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2011 fHWStencilTestEnabled = kNo_TriState;
2012 }
2013 } else {
2014 if (kYes_TriState != fHWStencilTestEnabled) {
2015 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2016 fHWStencilTestEnabled = kYes_TriState;
2017 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002018 }
joshualitta58fe352014-10-27 08:39:00 -07002019 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002020 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002021 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002022 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002023 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002024 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002025 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002026 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002027 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002028 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002029 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002030 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002031 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002032 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002033 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002034 }
2035 }
joshualitta58fe352014-10-27 08:39:00 -07002036 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002037 }
2038}
2039
bsalomon3e791242014-12-17 13:43:13 -08002040void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool isLineDraw) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002041// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2042// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2043#if 0
2044 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2045 #define RT_HAS_MSAA rt->isMultisampled()
2046#else
bsalomon3e791242014-12-17 13:43:13 -08002047 #define RT_HAS_MSAA (rt->isMultisampled() || isLineDraw)
bsalomon@google.com202d1392013-03-19 18:58:08 +00002048#endif
2049
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002050 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07002051 if (RT_HAS_MSAA) {
bsalomon3e791242014-12-17 13:43:13 -08002052 if (useHWAA) {
egdanielb414f252014-07-29 13:15:47 -07002053 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002054 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002055 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002056 }
2057 } else {
egdanielb414f252014-07-29 13:15:47 -07002058 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002059 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002060 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002061 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002062 }
2063 }
2064 }
2065}
2066
egdaniel080e6732014-12-22 07:35:52 -08002067void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002068 // Any optimization to disable blending should have already been applied and
2069 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002070
egdanielc2304142014-12-11 13:15:13 -08002071 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2072 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002073 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2074 if (blendOff) {
2075 if (kNo_TriState != fHWBlendState.fEnabled) {
2076 GL_CALL(Disable(GR_GL_BLEND));
2077 fHWBlendState.fEnabled = kNo_TriState;
2078 }
2079 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002080 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002081 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002082 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002083 }
egdanielb414f252014-07-29 13:15:47 -07002084 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2085 fHWBlendState.fDstCoeff != dstCoeff) {
2086 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2087 gXfermodeCoeff2Blend[dstCoeff]));
2088 fHWBlendState.fSrcCoeff = srcCoeff;
2089 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002090 }
egdanielc2304142014-12-11 13:15:13 -08002091 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002092 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2093 BlendCoeffReferencesConstant(dstCoeff)) &&
2094 (!fHWBlendState.fConstColorValid ||
2095 fHWBlendState.fConstColor != blendConst)) {
2096 GrGLfloat c[4];
2097 GrColorToRGBAFloat(blendConst, c);
2098 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2099 fHWBlendState.fConstColor = blendConst;
2100 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002101 }
2102 }
2103}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002104
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002105static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002106 static const GrGLenum gWrapModes[] = {
2107 GR_GL_CLAMP_TO_EDGE,
2108 GR_GL_REPEAT,
2109 GR_GL_MIRRORED_REPEAT
2110 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002111 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002112 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2113 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2114 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2115 return gWrapModes[tm];
2116}
2117
bsalomon861e1032014-12-16 07:33:49 -08002118void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002119 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002120
bsalomon@google.comb8670992012-07-25 21:27:09 +00002121 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2122 // 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 +00002123 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002124 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002125 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002126 this->onResolveRenderTarget(texRT);
2127 }
2128
bsalomon1c63bf62014-07-22 13:09:46 -07002129 uint32_t textureID = texture->getUniqueID();
2130 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002131 this->setTextureUnit(unitIdx);
2132 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002133 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002134 }
2135
bsalomon@google.com4c883782012-06-04 19:05:11 +00002136 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002137 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002138 bool setAll = timestamp < this->getResetTimestamp();
2139 GrGLTexture::TexParams newTexParams;
2140
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002141 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002142 GR_GL_NEAREST,
2143 GR_GL_LINEAR,
2144 GR_GL_LINEAR_MIPMAP_LINEAR
2145 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002146 static GrGLenum glMagFilterModes[] = {
2147 GR_GL_NEAREST,
2148 GR_GL_LINEAR,
2149 GR_GL_LINEAR
2150 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002151 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002152
2153 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2154 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2155 filterMode = GrTextureParams::kBilerp_FilterMode;
2156 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002157 }
bsalomonefd7d452014-10-23 14:17:46 -07002158
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002159 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2160 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002161
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002162 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002163 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002164 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002165 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002166 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002167
bsalomon@google.comb8670992012-07-25 21:27:09 +00002168 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2169 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002170 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002171 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002172 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002173 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002174 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002175 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2176 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002177 newTexParams.fMagFilter));
2178 }
2179 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2180 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002181 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2182 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002183 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002184 }
2185 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002186 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002187 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2188 GR_GL_TEXTURE_WRAP_S,
2189 newTexParams.fWrapS));
2190 }
2191 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002192 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002193 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2194 GR_GL_TEXTURE_WRAP_T,
2195 newTexParams.fWrapT));
2196 }
2197 if (this->glCaps().textureSwizzleSupport() &&
2198 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2199 oldTexParams.fSwizzleRGBA,
2200 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002201 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002202 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002203 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2204 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2205 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2206 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2207 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2208 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2209 } else {
2210 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2211 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2212 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2213 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002214 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002215 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002216}
2217
bsalomon3e791242014-12-17 13:43:13 -08002218void GrGLGpu::flushDither(bool dither) {
2219 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002220 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002221 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002222 fHWDitherEnabled = kYes_TriState;
2223 }
2224 } else {
2225 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002226 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002227 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002228 }
2229 }
bsalomon3e791242014-12-17 13:43:13 -08002230}
reed@google.comac10a2d2010-12-22 21:39:39 +00002231
egdaniel080e6732014-12-22 07:35:52 -08002232void GrGLGpu::flushColorWrite(bool writeColor) {
2233 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002234 if (kNo_TriState != fHWWriteToColor) {
2235 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2236 GR_GL_FALSE, GR_GL_FALSE));
2237 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002238 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002239 } else {
2240 if (kYes_TriState != fHWWriteToColor) {
2241 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2242 fHWWriteToColor = kYes_TriState;
2243 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002244 }
bsalomon3e791242014-12-17 13:43:13 -08002245}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002246
egdaniel8dd688b2015-01-22 10:16:09 -08002247void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002248 if (fHWDrawFace != face) {
2249 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002250 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002251 GL_CALL(Enable(GR_GL_CULL_FACE));
2252 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002253 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002254 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002255 GL_CALL(Enable(GR_GL_CULL_FACE));
2256 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002257 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002258 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002259 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002260 break;
2261 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002262 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002263 }
bsalomon3e791242014-12-17 13:43:13 -08002264 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002265 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002266}
2267
bsalomon861e1032014-12-16 07:33:49 -08002268bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002269 bool getSizedInternalFormat,
2270 GrGLenum* internalFormat,
2271 GrGLenum* externalFormat,
2272 GrGLenum* externalType) {
2273 GrGLenum dontCare;
2274 if (NULL == internalFormat) {
2275 internalFormat = &dontCare;
2276 }
2277 if (NULL == externalFormat) {
2278 externalFormat = &dontCare;
2279 }
2280 if (NULL == externalType) {
2281 externalType = &dontCare;
2282 }
2283
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002284 if(!this->glCaps().isConfigTexturable(config)) {
2285 return false;
2286 }
2287
reed@google.comac10a2d2010-12-22 21:39:39 +00002288 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002289 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002290 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002291 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002292 if (getSizedInternalFormat) {
2293 *internalFormat = GR_GL_RGBA8;
2294 } else {
2295 *internalFormat = GR_GL_RGBA;
2296 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002297 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002298 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002299 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002300 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002301 if (getSizedInternalFormat) {
2302 *internalFormat = GR_GL_BGRA8;
2303 } else {
2304 *internalFormat = GR_GL_BGRA;
2305 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002306 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002307 if (getSizedInternalFormat) {
2308 *internalFormat = GR_GL_RGBA8;
2309 } else {
2310 *internalFormat = GR_GL_RGBA;
2311 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002312 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002313 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002314 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002315 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002316 case kSRGBA_8888_GrPixelConfig:
2317 *internalFormat = GR_GL_SRGB_ALPHA;
2318 *externalFormat = GR_GL_SRGB_ALPHA;
2319 if (getSizedInternalFormat) {
2320 *internalFormat = GR_GL_SRGB8_ALPHA8;
2321 } else {
2322 *internalFormat = GR_GL_SRGB_ALPHA;
2323 }
2324 *externalType = GR_GL_UNSIGNED_BYTE;
2325 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002326 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002327 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002328 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002329 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002330 if (!this->glCaps().ES2CompatibilitySupport()) {
2331 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002332 } else {
2333 *internalFormat = GR_GL_RGB565;
2334 }
2335 } else {
2336 *internalFormat = GR_GL_RGB;
2337 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002338 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002339 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002340 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002341 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002342 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002343 if (getSizedInternalFormat) {
2344 *internalFormat = GR_GL_RGBA4;
2345 } else {
2346 *internalFormat = GR_GL_RGBA;
2347 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002348 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002349 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002350 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002351 // no sized/unsized internal format distinction here
2352 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002353 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002354 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002355 if (this->glCaps().textureRedSupport()) {
2356 *internalFormat = GR_GL_RED;
2357 *externalFormat = GR_GL_RED;
2358 if (getSizedInternalFormat) {
2359 *internalFormat = GR_GL_R8;
2360 } else {
2361 *internalFormat = GR_GL_RED;
2362 }
2363 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002364 } else {
2365 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002366 *externalFormat = GR_GL_ALPHA;
2367 if (getSizedInternalFormat) {
2368 *internalFormat = GR_GL_ALPHA8;
2369 } else {
2370 *internalFormat = GR_GL_ALPHA;
2371 }
2372 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002373 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002374 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002375 case kETC1_GrPixelConfig:
2376 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2377 break;
2378 case kLATC_GrPixelConfig:
2379 switch(this->glCaps().latcAlias()) {
2380 case GrGLCaps::kLATC_LATCAlias:
2381 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2382 break;
2383 case GrGLCaps::kRGTC_LATCAlias:
2384 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2385 break;
2386 case GrGLCaps::k3DC_LATCAlias:
2387 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2388 break;
2389 }
2390 break;
krajcevski238b4562014-06-30 09:09:22 -07002391 case kR11_EAC_GrPixelConfig:
2392 *internalFormat = GR_GL_COMPRESSED_R11;
2393 break;
krajcevski7ef21622014-07-16 15:21:13 -07002394
2395 case kASTC_12x12_GrPixelConfig:
2396 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2397 break;
2398
joshualittee5da552014-07-16 13:32:56 -07002399 case kRGBA_float_GrPixelConfig:
2400 *internalFormat = GR_GL_RGBA32F;
2401 *externalFormat = GR_GL_RGBA;
2402 *externalType = GR_GL_FLOAT;
2403 break;
krajcevski7ef21622014-07-16 15:21:13 -07002404
jvanverth28f9c602014-12-05 13:06:35 -08002405 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002406 if (this->glCaps().textureRedSupport()) {
2407 if (getSizedInternalFormat) {
2408 *internalFormat = GR_GL_R16F;
2409 } else {
2410 *internalFormat = GR_GL_RED;
2411 }
jvanverth28f9c602014-12-05 13:06:35 -08002412 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002413 } else {
2414 if (getSizedInternalFormat) {
2415 *internalFormat = GR_GL_ALPHA16F;
2416 } else {
2417 *internalFormat = GR_GL_ALPHA;
2418 }
2419 *externalFormat = GR_GL_ALPHA;
2420 }
2421 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002422 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002423 } else {
jvanverth1334c212014-12-18 05:44:55 -08002424 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002425 }
2426 break;
2427
reed@google.comac10a2d2010-12-22 21:39:39 +00002428 default:
2429 return false;
2430 }
2431 return true;
2432}
2433
bsalomon861e1032014-12-16 07:33:49 -08002434void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002435 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002436 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002437 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002438 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002439 }
2440}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002441
bsalomon861e1032014-12-16 07:33:49 -08002442void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002443 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002444 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002445 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2446 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2447 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002448 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002449 // clear out the this field so that if a program does use this unit it will rebind the correct
2450 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002451 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002452}
2453
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002454namespace {
2455// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002456inline bool can_blit_framebuffer(const GrSurface* dst,
2457 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002458 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002459 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2460 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002461 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002462 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2463 // or the rects are not the same (not just the same size but have the same edges).
2464 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2465 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2466 return false;
2467 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002468 return true;
2469 } else {
2470 return false;
2471 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002472}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002473
2474inline bool can_copy_texsubimage(const GrSurface* dst,
2475 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002476 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002477 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2478 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2479 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002480 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002481 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2482 return false;
2483 }
2484 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2485 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2486 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon49f085d2014-09-05 13:34:00 -07002487 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002488 return false;
2489 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002490 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2491 // If the src is multisampled (and uses an extension where there is a separate MSAA
2492 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon49f085d2014-09-05 13:34:00 -07002493 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002494 return false;
2495 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002496 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002497 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002498 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002499 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002500 return true;
2501 } else {
2502 return false;
2503 }
2504}
2505
robertphillips754f4e92014-09-18 13:52:08 -07002506}
2507
bsalomon@google.comeb851172013-04-15 13:51:00 +00002508// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2509// relative to is output.
egdaniel0f5f9672015-02-03 11:10:51 -08002510GrGLuint GrGLGpu::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2511 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002512 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002513 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002514 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002515 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
egdaniel0f5f9672015-02-03 11:10:51 -08002516 GrGLuint* tempFBOID;
2517 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
2518
2519 if (0 == *tempFBOID) {
2520 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
2521 }
2522
mtkleinb9eb4ac2015-02-02 18:26:03 -08002523 fStats.incRenderTargetBinds();
egdaniel0f5f9672015-02-03 11:10:51 -08002524 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
robertphillips754f4e92014-09-18 13:52:08 -07002525 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2526 GR_GL_COLOR_ATTACHMENT0,
2527 GR_GL_TEXTURE_2D,
2528 texID,
2529 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002530 viewport->fLeft = 0;
2531 viewport->fBottom = 0;
2532 viewport->fWidth = surface->width();
2533 viewport->fHeight = surface->height();
egdaniel0f5f9672015-02-03 11:10:51 -08002534 return *tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002535 } else {
egdaniel0f5f9672015-02-03 11:10:51 -08002536 GrGLuint tempFBOID = 0;
mtkleinb9eb4ac2015-02-02 18:26:03 -08002537 fStats.incRenderTargetBinds();
robertphillips754f4e92014-09-18 13:52:08 -07002538 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002539 *viewport = rt->getViewport();
egdaniel0f5f9672015-02-03 11:10:51 -08002540 return tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002541 }
egdaniel0f5f9672015-02-03 11:10:51 -08002542}
2543
2544void GrGLGpu::unbindTextureFromFBO(GrGLenum fboTarget) {
2545 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2546 GR_GL_COLOR_ATTACHMENT0,
2547 GR_GL_TEXTURE_2D,
2548 0,
2549 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002550}
2551
bsalomon861e1032014-12-16 07:33:49 -08002552bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002553 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2554 // possible and we return false to fallback to creating a render target dst for render-to-
2555 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2556 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2557
bsalomon@google.comeb851172013-04-15 13:51:00 +00002558 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002559 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002560 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002561 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2562 // then we set up for that, otherwise fail.
2563 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2564 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002565 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002566 desc->fConfig = kBGRA_8888_GrPixelConfig;
2567 return true;
2568 }
2569 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002570 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002571 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2572 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002573 }
2574
2575 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002576 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002577 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2578 // fail.
2579 if (this->caps()->isConfigRenderable(src->config(), false)) {
2580 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002581 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002582 desc->fConfig = src->config();
2583 return true;
2584 }
2585 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002586 }
bsalomonf90a02b2014-11-26 12:28:00 -08002587
2588 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2589 desc->fConfig = src->config();
2590 desc->fOrigin = src->origin();
2591 desc->fFlags = kNone_GrSurfaceFlags;
2592 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002593}
2594
bsalomon861e1032014-12-16 07:33:49 -08002595bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002596 GrSurface* src,
2597 const SkIRect& srcRect,
2598 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002599 bool copied = false;
joshualitta7024152014-11-03 14:16:35 -08002600 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002601 GrGLuint srcFBO;
2602 GrGLIRect srcVP;
egdaniel0f5f9672015-02-03 11:10:51 -08002603 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002604 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002605 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002606 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002607 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002608 GrGLIRect srcGLRect;
2609 srcGLRect.setRelativeTo(srcVP,
2610 srcRect.fLeft,
2611 srcRect.fTop,
2612 srcRect.width(),
2613 srcRect.height(),
2614 src->origin());
2615
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002616 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002617 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2618 GrGLint dstY;
2619 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2620 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2621 } else {
2622 dstY = dstPoint.fY;
2623 }
2624 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2625 dstPoint.fX, dstY,
2626 srcGLRect.fLeft, srcGLRect.fBottom,
2627 srcGLRect.fWidth, srcGLRect.fHeight));
2628 copied = true;
2629 if (srcFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002630 this->unbindTextureFromFBO(GR_GL_FRAMEBUFFER);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002631 }
joshualitta7024152014-11-03 14:16:35 -08002632 } else if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002633 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2634 srcRect.width(), srcRect.height());
2635 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002636 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002637 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2638 }
2639
2640 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002641 GrGLuint dstFBO;
2642 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002643 GrGLIRect dstVP;
2644 GrGLIRect srcVP;
egdaniel0f5f9672015-02-03 11:10:51 -08002645 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP,
2646 kDst_TempFBOTarget);
2647 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP,
2648 kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002649 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002650 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002651 GrGLIRect srcGLRect;
2652 GrGLIRect dstGLRect;
2653 srcGLRect.setRelativeTo(srcVP,
2654 srcRect.fLeft,
2655 srcRect.fTop,
2656 srcRect.width(),
2657 srcRect.height(),
2658 src->origin());
2659 dstGLRect.setRelativeTo(dstVP,
2660 dstRect.fLeft,
2661 dstRect.fTop,
2662 dstRect.width(),
2663 dstRect.height(),
2664 dst->origin());
2665
derekf8c8f71a2014-09-16 06:24:57 -07002666 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002667 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002668
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002669 GrGLint srcY0;
2670 GrGLint srcY1;
2671 // Does the blit need to y-mirror or not?
2672 if (src->origin() == dst->origin()) {
2673 srcY0 = srcGLRect.fBottom;
2674 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2675 } else {
2676 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2677 srcY1 = srcGLRect.fBottom;
2678 }
2679 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2680 srcY0,
2681 srcGLRect.fLeft + srcGLRect.fWidth,
2682 srcY1,
2683 dstGLRect.fLeft,
2684 dstGLRect.fBottom,
2685 dstGLRect.fLeft + dstGLRect.fWidth,
2686 dstGLRect.fBottom + dstGLRect.fHeight,
2687 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2688 if (dstFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002689 this->unbindTextureFromFBO(GR_GL_DRAW_FRAMEBUFFER);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002690 }
2691 if (srcFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002692 this->unbindTextureFromFBO(GR_GL_READ_FRAMEBUFFER);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002693 }
2694 copied = true;
2695 }
2696 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002697 return copied;
2698}
2699
bsalomon861e1032014-12-16 07:33:49 -08002700bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002701 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002702 const SkIRect& srcRect,
2703 const SkIPoint& dstPoint) {
egdaniel0f5f9672015-02-03 11:10:51 -08002704 // This mirrors the logic in onCopySurface.
2705 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002706 return true;
2707 }
egdaniel0f5f9672015-02-03 11:10:51 -08002708 if (can_blit_framebuffer(dst, src, this)) {
bsalomona2c23232014-11-25 07:41:12 -08002709 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002710 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2711 srcRect.width(), srcRect.height());
2712 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2713 return true;
2714 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002715 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002716 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002717 }
2718 }
joshualitta7024152014-11-03 14:16:35 -08002719 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002720}
2721
bsalomon861e1032014-12-16 07:33:49 -08002722void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002723 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002724 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002725 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002726 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002727 }
2728}
2729
bsalomon861e1032014-12-16 07:33:49 -08002730void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002731 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002732 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002733 }
2734}
joshualitt79f8fae2014-10-28 17:59:26 -07002735
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002736///////////////////////////////////////////////////////////////////////////////
2737
bsalomon861e1032014-12-16 07:33:49 -08002738GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2739 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002740 const GrGLVertexBuffer* vbuffer,
2741 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002742 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002743 GrGLAttribArrayState* attribState;
2744
bsalomon@google.com6918d482013-03-07 19:09:11 +00002745 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2746 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002747 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002748 SkSafeUnref(fVBOVertexArray);
2749 GrGLuint arrayID;
2750 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2751 int attrCount = gpu->glCaps().maxVertexAttributes();
2752 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002753 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002754 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2755 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002756 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002757 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2758 } else {
2759 this->setVertexArrayID(gpu, 0);
2760 }
2761 int attrCount = gpu->glCaps().maxVertexAttributes();
2762 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2763 fDefaultVertexArrayAttribState.resize(attrCount);
2764 }
2765 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002766 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002767 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002768}