blob: ef4423ba6be17d7a1004eb886948a2fdf24ba650 [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"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "GrSurfacePriv.h"
bsalomon@google.coma3201942012-06-21 19:58:20 +000015#include "GrTemplates.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000017#include "GrTypes.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000018#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000019#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com0b77d682011-08-19 13:28:54 +000021#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000022#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000023
reed@google.comac10a2d2010-12-22 21:39:39 +000024#define SKIP_CACHE_CHECK true
25
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000026#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
27 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
28 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
29 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000030#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
32 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
33 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
34#endif
35
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000036
37///////////////////////////////////////////////////////////////////////////////
38
cdaltonb85a0aa2014-07-21 15:32:44 -070039
twiz@google.com0f31ca72011-03-18 17:38:11 +000040static const GrGLenum gXfermodeCoeff2Blend[] = {
41 GR_GL_ZERO,
42 GR_GL_ONE,
43 GR_GL_SRC_COLOR,
44 GR_GL_ONE_MINUS_SRC_COLOR,
45 GR_GL_DST_COLOR,
46 GR_GL_ONE_MINUS_DST_COLOR,
47 GR_GL_SRC_ALPHA,
48 GR_GL_ONE_MINUS_SRC_ALPHA,
49 GR_GL_DST_ALPHA,
50 GR_GL_ONE_MINUS_DST_ALPHA,
51 GR_GL_CONSTANT_COLOR,
52 GR_GL_ONE_MINUS_CONSTANT_COLOR,
53 GR_GL_CONSTANT_ALPHA,
54 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000055
56 // extended blend coeffs
57 GR_GL_SRC1_COLOR,
58 GR_GL_ONE_MINUS_SRC1_COLOR,
59 GR_GL_SRC1_ALPHA,
60 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +000061};
62
bsalomon861e1032014-12-16 07:33:49 -080063bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +000064 static const bool gCoeffReferencesBlendConst[] = {
65 false,
66 false,
67 false,
68 false,
69 false,
70 false,
71 false,
72 false,
73 false,
74 false,
75 true,
76 true,
77 true,
78 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000079
80 // extended blend coeffs
81 false,
82 false,
83 false,
84 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +000085 };
86 return gCoeffReferencesBlendConst[coeff];
bsalomon@google.com47059542012-06-06 20:51:20 +000087 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000088 SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000089
bsalomon@google.com47059542012-06-06 20:51:20 +000090 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
91 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
92 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
93 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
94 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
95 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
96 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
97 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
98 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
99 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
100 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
101 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
102 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
103 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000104
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
106 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
107 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
108 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000109
110 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomon@google.com47059542012-06-06 20:51:20 +0000111 GR_STATIC_ASSERT(kTotalGrBlendCoeffCount ==
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000112 SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000113}
114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115///////////////////////////////////////////////////////////////////////////////
116
rileya@google.come38160c2012-07-03 18:03:04 +0000117static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118
bsalomon861e1032014-12-16 07:33:49 -0800119GrGLGpu::GrGLGpu(const GrGLContext& ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000121 , fGLContext(ctx) {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000122
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000123 SkASSERT(ctx.isInitialized());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000124 fCaps.reset(SkRef(ctx.caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000125
bsalomon1c63bf62014-07-22 13:09:46 -0700126 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000127
robertphillips@google.com6177e692013-02-28 20:16:25 +0000128 GrGLClearErr(fGLContext.interface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000129 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000130 const GrGLubyte* vendor;
131 const GrGLubyte* renderer;
132 const GrGLubyte* version;
133 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
134 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
135 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800136 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000137 this);
tfarina38406c82014-10-31 07:11:12 -0700138 SkDebugf("------ VENDOR %s\n", vendor);
139 SkDebugf("------ RENDERER %s\n", renderer);
140 SkDebugf("------ VERSION %s\n", version);
141 SkDebugf("------ EXTENSIONS\n");
bsalomone904c092014-07-17 10:50:59 -0700142 ctx.extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700143 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800144 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000145 }
146
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000147 fProgramCache = SkNEW_ARGS(ProgramCache, (this));
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000148
joshualitt2dd1ae02014-12-03 06:24:10 -0800149 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000150
bsalomon@google.comfe676522011-06-17 18:12:21 +0000151 fLastSuccessfulStencilFmtIdx = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000152 fHWProgramID = 0;
egdaniel0f5f9672015-02-03 11:10:51 -0800153 fTempSrcFBOID = 0;
154 fTempDstFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700155
156 if (this->glCaps().pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700157 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700158 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000159}
160
bsalomon861e1032014-12-16 07:33:49 -0800161GrGLGpu::~GrGLGpu() {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000162 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000163 // detach the current program so there is no confusion on OpenGL's part
164 // that we want it to be deleted
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000165 SkASSERT(fHWProgramID == fCurrentProgram->programID());
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000166 GL_CALL(UseProgram(0));
167 }
168
egdaniel0f5f9672015-02-03 11:10:51 -0800169 if (0 != fTempSrcFBOID) {
170 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
171 }
172 if (0 != fTempDstFBOID) {
173 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
174 }
175
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000176 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700177}
178
bsalomon861e1032014-12-16 07:33:49 -0800179void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700180 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700181 fProgramCache->abandon();
182 fHWProgramID = 0;
egdaniel0f5f9672015-02-03 11:10:51 -0800183 fTempSrcFBOID = 0;
184 fTempDstFBOID = 0;
bsalomonc8dc1f72014-08-21 13:02:13 -0700185 if (this->glCaps().pathRenderingSupport()) {
186 this->glPathRendering()->abandonGpuResources();
187 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000188}
189
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000190///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -0800191GrPixelConfig GrGLGpu::preferredReadPixelsConfig(GrPixelConfig readConfig,
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000192 GrPixelConfig surfaceConfig) const {
193 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == readConfig) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000194 return kBGRA_8888_GrPixelConfig;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000195 } else if (this->glContext().isMesa() &&
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000196 GrBytesPerPixel(readConfig) == 4 &&
197 GrPixelConfigSwapRAndB(readConfig) == surfaceConfig) {
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000198 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
199 // Perhaps this should be guarded by some compiletime or runtime check.
200 return surfaceConfig;
piotaixre4b23142014-10-02 10:57:53 -0700201 } else if (readConfig == kBGRA_8888_GrPixelConfig
202 && !this->glCaps().readPixelsSupported(
203 this->glInterface(),
204 GR_GL_BGRA,
205 GR_GL_UNSIGNED_BYTE,
206 surfaceConfig
207 )) {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000208 return kRGBA_8888_GrPixelConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000209 } else {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000210 return readConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000211 }
212}
213
bsalomon861e1032014-12-16 07:33:49 -0800214GrPixelConfig GrGLGpu::preferredWritePixelsConfig(GrPixelConfig writeConfig,
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000215 GrPixelConfig surfaceConfig) const {
commit-bot@chromium.org28621512013-08-07 19:43:45 +0000216 if (GR_GL_RGBA_8888_PIXEL_OPS_SLOW && kRGBA_8888_GrPixelConfig == writeConfig) {
217 return kBGRA_8888_GrPixelConfig;
218 } else {
219 return writeConfig;
220 }
bsalomon@google.com9c680582013-02-06 18:17:50 +0000221}
222
bsalomon861e1032014-12-16 07:33:49 -0800223bool GrGLGpu::canWriteTexturePixels(const GrTexture* texture, GrPixelConfig srcConfig) const {
krajcevski145d48c2014-06-11 16:07:50 -0700224 if (kIndex_8_GrPixelConfig == srcConfig || kIndex_8_GrPixelConfig == texture->config()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000225 return false;
226 }
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000227 if (srcConfig != texture->config() && kGLES_GrGLStandard == this->glStandard()) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000228 // In general ES2 requires the internal format of the texture and the format of the src
229 // pixels to match. However, It may or may not be possible to upload BGRA data to a RGBA
230 // texture. It depends upon which extension added BGRA. The Apple extension allows it
231 // (BGRA's internal format is RGBA) while the EXT extension does not (BGRA is its own
232 // internal format).
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000233 if (this->glCaps().isConfigTexturable(kBGRA_8888_GrPixelConfig) &&
bsalomon@google.com9c680582013-02-06 18:17:50 +0000234 !this->glCaps().bgraIsInternalFormat() &&
235 kBGRA_8888_GrPixelConfig == srcConfig &&
236 kRGBA_8888_GrPixelConfig == texture->config()) {
237 return true;
238 } else {
239 return false;
240 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000241 } else {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000242 return true;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000243 }
244}
245
bsalomon861e1032014-12-16 07:33:49 -0800246bool GrGLGpu::fullReadPixelsIsFasterThanPartial() const {
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000247 return SkToBool(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL);
248}
249
bsalomon861e1032014-12-16 07:33:49 -0800250void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000251 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000252 if (resetBits & kMisc_GrGLBackendState) {
253 GL_CALL(Disable(GR_GL_DEPTH_TEST));
254 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000255
egdaniel8dd688b2015-01-22 10:16:09 -0800256 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000257 fHWDitherEnabled = kUnknown_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000258
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000259 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000260 // Desktop-only state that we never change
261 if (!this->glCaps().isCoreProfile()) {
262 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
263 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
264 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
265 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
266 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
267 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
268 }
269 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
270 // core profile. This seems like a bug since the core spec removes any mention of
271 // GL_ARB_imaging.
272 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
273 GL_CALL(Disable(GR_GL_COLOR_TABLE));
274 }
275 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
276 // Since ES doesn't support glPointSize at all we always use the VS to
277 // set the point size
278 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
279
280 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
281 // currently part of our gl interface. There are probably others as
282 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000283 }
joshualitt58162332014-08-01 06:44:53 -0700284
285 if (kGLES_GrGLStandard == this->glStandard() &&
286 fGLContext.hasExtension("GL_ARM_shader_framebuffer_fetch")) {
287 // The arm extension requires specifically enabling MSAA fetching per sample.
288 // On some devices this may have a perf hit. Also multiple render targets are disabled
289 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
290 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000291 fHWWriteToColor = kUnknown_TriState;
292 // we only ever use lines in hairline mode
293 GL_CALL(LineWidth(1));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000294 }
edisonn@google.comba669992013-06-28 16:03:21 +0000295
egdanielb414f252014-07-29 13:15:47 -0700296 if (resetBits & kMSAAEnable_GrGLBackendState) {
297 fMSAAEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000298 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000300 fHWActiveTextureUnitIdx = -1; // invalid
301
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000302 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700303 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
304 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000305 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000306 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000307
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000308 if (resetBits & kBlend_GrGLBackendState) {
309 fHWBlendState.invalidate();
310 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000311
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000312 if (resetBits & kView_GrGLBackendState) {
313 fHWScissorSettings.invalidate();
314 fHWViewport.invalidate();
315 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000317 if (resetBits & kStencil_GrGLBackendState) {
318 fHWStencilSettings.invalidate();
319 fHWStencilTestEnabled = kUnknown_TriState;
320 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000321
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000322 // Vertex
323 if (resetBits & kVertex_GrGLBackendState) {
324 fHWGeometryState.invalidate();
325 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000326
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000327 if (resetBits & kRenderTarget_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700328 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000329 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000330
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000331 if (resetBits & kPathRendering_GrGLBackendState) {
332 if (this->caps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700333 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000334 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000335 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000336
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000337 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000338 if (resetBits & kPixelStore_GrGLBackendState) {
339 if (this->glCaps().unpackRowLengthSupport()) {
340 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
341 }
342 if (this->glCaps().packRowLengthSupport()) {
343 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
344 }
345 if (this->glCaps().unpackFlipYSupport()) {
346 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
347 }
348 if (this->glCaps().packFlipYSupport()) {
349 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
350 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000351 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000352
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000353 if (resetBits & kProgram_GrGLBackendState) {
354 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000355 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000356}
357
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000358namespace {
359
360GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
361 // By default, GrRenderTargets are GL's normal orientation so that they
362 // can be drawn to by the outside world without the client having
363 // to render upside down.
364 if (kDefault_GrSurfaceOrigin == origin) {
365 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
366 } else {
367 return origin;
368 }
369}
370
371}
372
bsalomon861e1032014-12-16 07:33:49 -0800373GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000374 if (!this->configToGLFormats(desc.fConfig, false, NULL, NULL, NULL)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000375 return NULL;
376 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000377
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000378 if (0 == desc.fTextureHandle) {
379 return NULL;
380 }
381
bsalomon@google.combcce8922013-03-25 15:38:39 +0000382 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000383 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
384 return NULL;
385 }
386
bsalomonb15b4c12014-10-29 12:41:57 -0700387 GrGLTexture::IDDesc idDesc;
388 GrSurfaceDesc surfDesc;
389
390 idDesc.fTextureID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon5236cf42015-01-14 10:42:08 -0800391 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
bsalomonb15b4c12014-10-29 12:41:57 -0700392
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000393 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
bsalomonb15b4c12014-10-29 12:41:57 -0700394 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
395 surfDesc.fWidth = desc.fWidth;
396 surfDesc.fHeight = desc.fHeight;
397 surfDesc.fConfig = desc.fConfig;
398 surfDesc.fSampleCnt = desc.fSampleCnt;
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000399 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000400 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
401 // assuming the old behaviour, which is that backend textures are always
402 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
403 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
404 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700405 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000406 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700407 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000408 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000409
410 GrGLTexture* texture = NULL;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000411 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700412 GrGLRenderTarget::IDDesc rtIDDesc;
bsalomon5236cf42015-01-14 10:42:08 -0800413 if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000414 return NULL;
415 }
bsalomon37dd3312014-11-03 08:47:23 -0800416 texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000417 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700418 texture = SkNEW_ARGS(GrGLTexture, (this, surfDesc, idDesc));
bsalomon@google.come269f212011-11-07 13:29:52 +0000419 }
420 if (NULL == texture) {
421 return NULL;
422 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000423
bsalomon@google.come269f212011-11-07 13:29:52 +0000424 return texture;
425}
426
bsalomon861e1032014-12-16 07:33:49 -0800427GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc) {
bsalomonb15b4c12014-10-29 12:41:57 -0700428 GrGLRenderTarget::IDDesc idDesc;
429 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
430 idDesc.fMSColorRenderbufferID = 0;
431 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon5236cf42015-01-14 10:42:08 -0800432 idDesc.fLifeCycle = GrGpuResource::kWrapped_LifeCycle;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000433
bsalomonb15b4c12014-10-29 12:41:57 -0700434 GrSurfaceDesc desc;
435 desc.fConfig = wrapDesc.fConfig;
436 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
437 desc.fWidth = wrapDesc.fWidth;
438 desc.fHeight = wrapDesc.fHeight;
439 desc.fSampleCnt = wrapDesc.fSampleCnt;
440 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
441
bsalomon37dd3312014-11-03 08:47:23 -0800442 GrRenderTarget* tgt = SkNEW_ARGS(GrGLRenderTarget, (this, desc, idDesc));
bsalomonb15b4c12014-10-29 12:41:57 -0700443 if (wrapDesc.fStencilBits) {
kkinnunen36c57df2015-01-27 00:30:18 -0800444 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.come269f212011-11-07 13:29:52 +0000445 GrGLStencilBuffer::Format format;
446 format.fInternalFormat = GrGLStencilBuffer::kUnknownInternalFormat;
447 format.fPacked = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700448 format.fStencilBits = wrapDesc.fStencilBits;
449 format.fTotalBits = wrapDesc.fStencilBits;
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000450 GrGLStencilBuffer* sb = SkNEW_ARGS(GrGLStencilBuffer,
451 (this,
kkinnunen36c57df2015-01-27 00:30:18 -0800452 sbDesc,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000453 desc.fWidth,
454 desc.fHeight,
455 desc.fSampleCnt,
456 format));
bsalomon@google.come269f212011-11-07 13:29:52 +0000457 tgt->setStencilBuffer(sb);
458 sb->unref();
459 }
460 return tgt;
461}
462
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000463////////////////////////////////////////////////////////////////////////////////
464
bsalomon861e1032014-12-16 07:33:49 -0800465bool GrGLGpu::onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000466 int left, int top, int width, int height,
467 GrPixelConfig config, const void* buffer,
468 size_t rowBytes) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000469 if (NULL == buffer) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000470 return false;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000471 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000472 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
473
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000474 this->setScratchTextureUnit();
bsalomon@google.com6f379512011-11-16 20:36:03 +0000475 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000476
krajcevski145d48c2014-06-11 16:07:50 -0700477 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700478 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800479 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700480 SkASSERT(config == glTex->desc().fConfig);
481 success = this->uploadCompressedTexData(glTex->desc(), buffer, false, left, top, width,
482 height);
krajcevski145d48c2014-06-11 16:07:50 -0700483 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700484 success = this->uploadTexData(glTex->desc(), false, left, top, width, height, config,
485 buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700486 }
487
488 if (success) {
bsalomonafbf2d62014-09-30 12:18:44 -0700489 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000490 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000491 }
krajcevski145d48c2014-06-11 16:07:50 -0700492
493 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000494}
495
bsalomonb15b4c12014-10-29 12:41:57 -0700496static bool adjust_pixel_ops_params(int surfaceWidth,
497 int surfaceHeight,
498 size_t bpp,
499 int* left, int* top, int* width, int* height,
500 const void** data,
501 size_t* rowBytes) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000502 if (!*rowBytes) {
503 *rowBytes = *width * bpp;
504 }
505
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000506 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
507 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000508
509 if (!subRect.intersect(bounds)) {
510 return false;
511 }
512 *data = reinterpret_cast<const void*>(reinterpret_cast<intptr_t>(*data) +
513 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
514
515 *left = subRect.fLeft;
516 *top = subRect.fTop;
517 *width = subRect.width();
518 *height = subRect.height();
519 return true;
520}
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000521
bsalomonb15b4c12014-10-29 12:41:57 -0700522static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
523 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700524 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000525 return GR_GL_GET_ERROR(interface);
526 } else {
527 return CHECK_ALLOC_ERROR(interface);
528 }
529}
530
bsalomon861e1032014-12-16 07:33:49 -0800531bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000532 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533 int left, int top, int width, int height,
534 GrPixelConfig dataConfig,
535 const void* data,
536 size_t rowBytes) {
bsalomon49f085d2014-09-05 13:34:00 -0700537 SkASSERT(data || isNewTexture);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000538
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000539 // If we're uploading compressed data then we should be using uploadCompressedTexData
540 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
541
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000542 size_t bpp = GrBytesPerPixel(dataConfig);
543 if (!adjust_pixel_ops_params(desc.fWidth, desc.fHeight, bpp, &left, &top,
544 &width, &height, &data, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000545 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000546 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000547 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000548
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000549 // in case we need a temporary, trimmed copy of the src pixels
georgeb62508b2014-08-12 18:00:47 -0700550 GrAutoMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000551
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000552 // We currently lazily create MIPMAPs when the we see a draw with
553 // GrTextureParams::kMipMap_FilterMode. Using texture storage requires that the
554 // MIP levels are all created when the texture is created. So for now we don't use
555 // texture storage.
556 bool useTexStorage = false &&
557 isNewTexture &&
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000558 this->glCaps().texStorageSupport();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000559
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000560 if (useTexStorage && kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com313f0192012-07-10 17:21:02 +0000561 // 565 is not a sized internal format on desktop GL. So on desktop with
562 // 565 we always use an unsized internal format to let the system pick
563 // the best sized format to convert the 565 data to. Since TexStorage
564 // only allows sized internal formats we will instead use TexImage2D.
565 useTexStorage = desc.fConfig != kRGB_565_GrPixelConfig;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000566 }
567
jvanverth3f801cb2014-12-16 09:49:38 -0800568 GrGLenum internalFormat = 0x0; // suppress warning
569 GrGLenum externalFormat = 0x0; // suppress warning
570 GrGLenum externalType = 0x0; // suppress warning
bsalomone904c092014-07-17 10:50:59 -0700571
jvanverthe1869ca2014-12-16 13:32:27 -0800572 // glTexStorage requires sized internal formats on both desktop and ES. ES2 requires an unsized
jvanverthf72b7522014-12-17 10:46:01 -0800573 // format for glTexImage, unlike ES3 and desktop.
jvanverthe1869ca2014-12-16 13:32:27 -0800574 bool useSizedFormat = useTexStorage;
jvanverthf72b7522014-12-17 10:46:01 -0800575 if (kGL_GrGLStandard == this->glStandard() ||
576 (this->glVersion() >= GR_GL_VER(3, 0) &&
577 // ES3 only works with sized BGRA8 format if "GL_APPLE_texture_format_BGRA8888" enabled
578 (kBGRA_8888_GrPixelConfig != dataConfig || !this->glCaps().bgraIsInternalFormat()))) {
jvanverthe1869ca2014-12-16 13:32:27 -0800579 useSizedFormat = true;
580 }
jvanverthf72b7522014-12-17 10:46:01 -0800581
bsalomone904c092014-07-17 10:50:59 -0700582 if (!this->configToGLFormats(dataConfig, useSizedFormat, &internalFormat,
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000583 &externalFormat, &externalType)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000584 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000585 }
586
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000587 /*
bsalomon@google.com6f379512011-11-16 20:36:03 +0000588 * check whether to allocate a temporary buffer for flipping y or
589 * because our srcData has extra bytes past each row. If so, we need
590 * to trim those off here, since GL ES may not let us specify
591 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000592 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000593 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000594 bool swFlipY = false;
595 bool glFlipY = false;
bsalomon49f085d2014-09-05 13:34:00 -0700596 if (data) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000597 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000598 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000599 glFlipY = true;
600 } else {
601 swFlipY = true;
602 }
603 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000604 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000605 // can't use this for flipping, only non-neg values allowed. :(
606 if (rowBytes != trimRowBytes) {
607 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
608 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
609 restoreGLRowLength = true;
610 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000611 } else {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000612 if (trimRowBytes != rowBytes || swFlipY) {
613 // copy data into our new storage, skipping the trailing bytes
614 size_t trimSize = height * trimRowBytes;
615 const char* src = (const char*)data;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000616 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000617 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000618 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000619 char* dst = (char*)tempStorage.reset(trimSize);
620 for (int y = 0; y < height; y++) {
621 memcpy(dst, src, trimRowBytes);
622 if (swFlipY) {
623 src -= rowBytes;
624 } else {
625 src += rowBytes;
626 }
627 dst += trimRowBytes;
628 }
629 // now point data to our copied version
630 data = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000631 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000632 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000633 if (glFlipY) {
634 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
635 }
joshualittee5da552014-07-16 13:32:56 -0700636 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT,
637 static_cast<GrGLint>(GrUnpackAlignment(dataConfig))));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000638 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000639 bool succeeded = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640 if (isNewTexture &&
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000641 0 == left && 0 == top &&
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000642 desc.fWidth == width && desc.fHeight == height) {
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000643 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000644 if (useTexStorage) {
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +0000645 // We never resize or change formats of textures.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000646 GL_ALLOC_CALL(this->glInterface(),
647 TexStorage2D(GR_GL_TEXTURE_2D,
648 1, // levels
649 internalFormat,
650 desc.fWidth, desc.fHeight));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000651 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700652 GL_ALLOC_CALL(this->glInterface(),
653 TexImage2D(GR_GL_TEXTURE_2D,
654 0, // level
655 internalFormat,
656 desc.fWidth, desc.fHeight,
657 0, // border
658 externalFormat, externalType,
659 data));
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +0000660 }
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000661 GrGLenum error = check_alloc_error(desc, this->glInterface());
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000662 if (error != GR_GL_NO_ERROR) {
663 succeeded = false;
664 } else {
665 // if we have data and we used TexStorage to create the texture, we
666 // now upload with TexSubImage.
bsalomon49f085d2014-09-05 13:34:00 -0700667 if (data && useTexStorage) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000668 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
669 0, // level
670 left, top,
671 width, height,
672 externalFormat, externalType,
673 data));
674 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000675 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000676 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000677 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000678 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000679 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000680 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D,
681 0, // level
682 left, top,
683 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000684 externalFormat, externalType, data));
685 }
686
687 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000688 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000689 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000690 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000691 if (glFlipY) {
692 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
693 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000694 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000695}
696
krajcevski145d48c2014-06-11 16:07:50 -0700697// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700698// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700699// create a CompressedTexData struct that takes a desc/ptr and figures out
700// the proper upload semantics. Then users can construct this function how they
701// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800702bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
krajcevski145d48c2014-06-11 16:07:50 -0700703 const void* data,
704 bool isNewTexture,
705 int left, int top, int width, int height) {
bsalomon49f085d2014-09-05 13:34:00 -0700706 SkASSERT(data || isNewTexture);
krajcevski9c0e6292014-06-02 07:38:14 -0700707
708 // No support for software flip y, yet...
709 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
710
krajcevski145d48c2014-06-11 16:07:50 -0700711 if (-1 == width) {
712 width = desc.fWidth;
713 }
714#ifdef SK_DEBUG
715 else {
716 SkASSERT(width <= desc.fWidth);
717 }
718#endif
719
720 if (-1 == height) {
721 height = desc.fHeight;
722 }
723#ifdef SK_DEBUG
724 else {
725 SkASSERT(height <= desc.fHeight);
726 }
727#endif
728
krajcevski9c0e6292014-06-02 07:38:14 -0700729 // Make sure that the width and height that we pass to OpenGL
730 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800731 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700732
733 // We only need the internal format for compressed 2D textures.
734 GrGLenum internalFormat = 0;
735 if (!this->configToGLFormats(desc.fConfig, false, &internalFormat, NULL, NULL)) {
736 return false;
737 }
738
krajcevski145d48c2014-06-11 16:07:50 -0700739 if (isNewTexture) {
bsalomond4cb9222014-08-11 14:19:09 -0700740 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700741 GL_ALLOC_CALL(this->glInterface(),
742 CompressedTexImage2D(GR_GL_TEXTURE_2D,
743 0, // level
744 internalFormat,
745 width, height,
746 0, // border
bsalomon98806072014-12-12 15:11:17 -0800747 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700748 data));
bsalomond4cb9222014-08-11 14:19:09 -0700749 GrGLenum error = check_alloc_error(desc, this->glInterface());
750 if (error != GR_GL_NO_ERROR) {
751 return false;
752 }
krajcevski145d48c2014-06-11 16:07:50 -0700753 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700754 // Paletted textures can't be updated.
755 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
756 return false;
757 }
758 GL_CALL(CompressedTexSubImage2D(GR_GL_TEXTURE_2D,
759 0, // level
760 left, top,
761 width, height,
762 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800763 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700764 data));
krajcevski145d48c2014-06-11 16:07:50 -0700765 }
krajcevski9c0e6292014-06-02 07:38:14 -0700766
bsalomond4cb9222014-08-11 14:19:09 -0700767 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700768}
769
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000770static bool renderbuffer_storage_msaa(GrGLContext& ctx,
771 int sampleCount,
772 GrGLenum format,
773 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000774 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000775 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000776 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000777 case GrGLCaps::kDesktop_ARB_MSFBOType:
778 case GrGLCaps::kDesktop_EXT_MSFBOType:
779 case GrGLCaps::kES_3_0_MSFBOType:
780 GL_ALLOC_CALL(ctx.interface(),
781 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
782 sampleCount,
783 format,
784 width, height));
785 break;
786 case GrGLCaps::kES_Apple_MSFBOType:
787 GL_ALLOC_CALL(ctx.interface(),
788 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
789 sampleCount,
790 format,
791 width, height));
792 break;
793 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
794 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
795 GL_ALLOC_CALL(ctx.interface(),
796 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
797 sampleCount,
798 format,
799 width, height));
800 break;
801 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000802 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000803 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000804 }
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000805 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000806}
807
bsalomon5236cf42015-01-14 10:42:08 -0800808bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
bsalomonb15b4c12014-10-29 12:41:57 -0700809 GrGLRenderTarget::IDDesc* idDesc) {
810 idDesc->fMSColorRenderbufferID = 0;
811 idDesc->fRTFBOID = 0;
812 idDesc->fTexFBOID = 0;
bsalomon5236cf42015-01-14 10:42:08 -0800813 idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
814 GrGpuResource::kUncached_LifeCycle;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000815
816 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000817
bsalomon@google.comab15d612011-08-09 12:57:56 +0000818 GrGLenum msColorFormat = 0; // suppress warning
819
bsalomonb15b4c12014-10-29 12:41:57 -0700820 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000821 goto FAILED;
822 }
823
bsalomonb15b4c12014-10-29 12:41:57 -0700824 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
825 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000826 goto FAILED;
827 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000829
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000830 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
831 // the texture bound to the other. The exception is the IMG multisample extension. With this
832 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
833 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -0700834 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
835 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
836 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
837 if (!idDesc->fRTFBOID ||
838 !idDesc->fMSColorRenderbufferID ||
839 !this->configToGLFormats(desc.fConfig,
commit-bot@chromium.org87002952013-08-20 15:12:01 +0000840 // ES2 and ES3 require sized internal formats for rb storage.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000841 kGLES_GrGLStandard == this->glStandard(),
bsalomon@google.com347c3822013-05-01 20:10:01 +0000842 &msColorFormat,
843 NULL,
844 NULL)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000845 goto FAILED;
846 }
847 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700848 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000849 }
850
bsalomon@google.com0e9b41a2012-01-04 22:11:43 +0000851 // below here we may bind the FBO
bsalomon1c63bf62014-07-22 13:09:46 -0700852 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomonb15b4c12014-10-29 12:41:57 -0700853 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
854 SkASSERT(desc.fSampleCnt > 0);
855 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
robertphillips@google.com6177e692013-02-28 20:16:25 +0000856 if (!renderbuffer_storage_msaa(fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -0700857 desc.fSampleCnt,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000858 msColorFormat,
bsalomonb15b4c12014-10-29 12:41:57 -0700859 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000860 goto FAILED;
861 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800862 fStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700863 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000864 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000865 GR_GL_COLOR_ATTACHMENT0,
866 GR_GL_RENDERBUFFER,
bsalomonb15b4c12014-10-29 12:41:57 -0700867 idDesc->fMSColorRenderbufferID));
868 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
869 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000870 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
871 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
872 goto FAILED;
873 }
bsalomonb15b4c12014-10-29 12:41:57 -0700874 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000875 }
876 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800877 fStats.incRenderTargetBinds();
bsalomonb15b4c12014-10-29 12:41:57 -0700878 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000879
bsalomonb15b4c12014-10-29 12:41:57 -0700880 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000881 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
882 GR_GL_COLOR_ATTACHMENT0,
883 GR_GL_TEXTURE_2D,
bsalomonb15b4c12014-10-29 12:41:57 -0700884 texID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000885 } else {
886 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
887 GR_GL_COLOR_ATTACHMENT0,
888 GR_GL_TEXTURE_2D,
889 texID, 0));
890 }
bsalomonb15b4c12014-10-29 12:41:57 -0700891 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
892 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000893 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
894 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
895 goto FAILED;
896 }
bsalomonb15b4c12014-10-29 12:41:57 -0700897 fGLContext.caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000898 }
899
900 return true;
901
902FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -0700903 if (idDesc->fMSColorRenderbufferID) {
904 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000905 }
bsalomonb15b4c12014-10-29 12:41:57 -0700906 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
907 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000908 }
bsalomonb15b4c12014-10-29 12:41:57 -0700909 if (idDesc->fTexFBOID) {
910 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000911 }
912 return false;
913}
914
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000915// good to set a break-point here to know when createTexture fails
916static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +0000917// SkDEBUGFAIL("null texture");
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000918 return NULL;
919}
920
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000921#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000922static size_t as_size_t(int x) {
923 return x;
924}
925#endif
926
bsalomon5236cf42015-01-14 10:42:08 -0800927GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
928 const void* srcData, size_t rowBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000929
bsalomonb15b4c12014-10-29 12:41:57 -0700930 GrSurfaceDesc desc = origDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +0000931
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000932 // Attempt to catch un- or wrongly initialized sample counts;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000933 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000934 // We fail if the MSAA was requested and is not available.
935 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700936 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000937 return return_null_texture();
938 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000939
bsalomonf2703d82014-10-28 14:33:06 -0700940 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +0000941
bsalomonb15b4c12014-10-29 12:41:57 -0700942 // If the sample count exceeds the max then we clamp it.
943 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
944 desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000945
bsalomonb15b4c12014-10-29 12:41:57 -0700946 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -0700947 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com945bbe12012-06-15 14:30:34 +0000948 return return_null_texture();
reed@google.comac10a2d2010-12-22 21:39:39 +0000949 }
950
reed@google.comac10a2d2010-12-22 21:39:39 +0000951 if (renderTarget) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000952 int maxRTSize = this->caps()->maxRenderTargetSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700953 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000954 return return_null_texture();
955 }
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000956 } else {
957 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -0700958 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +0000959 return return_null_texture();
960 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000961 }
962
bsalomonb15b4c12014-10-29 12:41:57 -0700963 GrGLTexture::IDDesc idDesc;
964 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -0800965 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
966 GrGpuResource::kUncached_LifeCycle;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000967
bsalomonb15b4c12014-10-29 12:41:57 -0700968 if (!idDesc.fTextureID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000969 return return_null_texture();
970 }
971
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000972 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -0700973 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
bsalomon@google.come269f212011-11-07 13:29:52 +0000974
junov@chromium.org8b6b1c92013-08-29 16:22:09 +0000975 if (renderTarget && this->glCaps().textureUsageSupport()) {
976 // provides a hint about how this texture will be used
977 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
978 GR_GL_TEXTURE_USAGE,
979 GR_GL_FRAMEBUFFER_ATTACHMENT));
980 }
981
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000982 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
983 // drivers have a bug where an FBO won't be complete if it includes a
984 // texture that is not mipmap complete (considering the filter in use).
985 GrGLTexture::TexParams initialTexParams;
986 // we only set a subset here so invalidate first
987 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000988 initialTexParams.fMinFilter = GR_GL_NEAREST;
989 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000990 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
991 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000992 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
993 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000994 initialTexParams.fMagFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000995 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
996 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +0000997 initialTexParams.fMinFilter));
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000998 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
999 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001000 initialTexParams.fWrapS));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001001 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1002 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001003 initialTexParams.fWrapT));
bsalomonb15b4c12014-10-29 12:41:57 -07001004 if (!this->uploadTexData(desc, true, 0, 0,
1005 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001006 desc.fConfig, srcData, rowBytes)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001007 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001008 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001009 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001011 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001013 // unbind the texture from the texture unit before binding it to the frame buffer
1014 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001015 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001016
bsalomon5236cf42015-01-14 10:42:08 -08001017 if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
bsalomonb15b4c12014-10-29 12:41:57 -07001018 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 return return_null_texture();
1020 }
bsalomon37dd3312014-11-03 08:47:23 -08001021 tex = SkNEW_ARGS(GrGLTextureRenderTarget, (this, desc, idDesc, rtIDDesc));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001022 } else {
bsalomonb15b4c12014-10-29 12:41:57 -07001023 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001025 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001026#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001027 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001028 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001029#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001030 return tex;
1031}
1032
bsalomon5236cf42015-01-14 10:42:08 -08001033GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
1034 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001035
bsalomonb15b4c12014-10-29 12:41:57 -07001036 if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
krajcevski9c0e6292014-06-02 07:38:14 -07001037 return return_null_texture();
1038 }
1039
1040 // Make sure that we're not flipping Y.
bsalomonb15b4c12014-10-29 12:41:57 -07001041 GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
krajcevski9c0e6292014-06-02 07:38:14 -07001042 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
1043 return return_null_texture();
1044 }
bsalomonb15b4c12014-10-29 12:41:57 -07001045 GrSurfaceDesc desc = origDesc;
1046 desc.fOrigin = texOrigin;
krajcevski9c0e6292014-06-02 07:38:14 -07001047
1048 int maxSize = this->caps()->maxTextureSize();
bsalomonb15b4c12014-10-29 12:41:57 -07001049 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
krajcevski9c0e6292014-06-02 07:38:14 -07001050 return return_null_texture();
1051 }
1052
bsalomonb15b4c12014-10-29 12:41:57 -07001053 GrGLTexture::IDDesc idDesc;
1054 GL_CALL(GenTextures(1, &idDesc.fTextureID));
bsalomon5236cf42015-01-14 10:42:08 -08001055 idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
1056 GrGpuResource::kUncached_LifeCycle;
krajcevski9c0e6292014-06-02 07:38:14 -07001057
bsalomonb15b4c12014-10-29 12:41:57 -07001058 if (!idDesc.fTextureID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001059 return return_null_texture();
1060 }
1061
1062 this->setScratchTextureUnit();
bsalomonb15b4c12014-10-29 12:41:57 -07001063 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001064
1065 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1066 // drivers have a bug where an FBO won't be complete if it includes a
1067 // texture that is not mipmap complete (considering the filter in use).
1068 GrGLTexture::TexParams initialTexParams;
1069 // we only set a subset here so invalidate first
1070 initialTexParams.invalidate();
1071 initialTexParams.fMinFilter = GR_GL_NEAREST;
1072 initialTexParams.fMagFilter = GR_GL_NEAREST;
1073 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1074 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
1075 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1076 GR_GL_TEXTURE_MAG_FILTER,
1077 initialTexParams.fMagFilter));
1078 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1079 GR_GL_TEXTURE_MIN_FILTER,
1080 initialTexParams.fMinFilter));
1081 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1082 GR_GL_TEXTURE_WRAP_S,
1083 initialTexParams.fWrapS));
1084 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1085 GR_GL_TEXTURE_WRAP_T,
1086 initialTexParams.fWrapT));
1087
bsalomonb15b4c12014-10-29 12:41:57 -07001088 if (!this->uploadCompressedTexData(desc, srcData)) {
1089 GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
krajcevski9c0e6292014-06-02 07:38:14 -07001090 return return_null_texture();
1091 }
1092
1093 GrGLTexture* tex;
bsalomonb15b4c12014-10-29 12:41:57 -07001094 tex = SkNEW_ARGS(GrGLTexture, (this, desc, idDesc));
krajcevski9c0e6292014-06-02 07:38:14 -07001095 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1096#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001097 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001098 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1099#endif
1100 return tex;
1101}
1102
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001103namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001104
1105const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount;
1106
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107void inline get_stencil_rb_sizes(const GrGLInterface* gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001108 GrGLStencilBuffer::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001109
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001110 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001111 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001112 (kUnknownBitCount == format->fTotalBits));
1113 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001114 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001115 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1116 (GrGLint*)&format->fStencilBits);
1117 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001119 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1120 (GrGLint*)&format->fTotalBits);
1121 format->fTotalBits += format->fStencilBits;
1122 } else {
1123 format->fTotalBits = format->fStencilBits;
1124 }
1125 }
1126}
1127}
1128
bsalomon02a44a42015-02-19 09:09:00 -08001129bool GrGLGpu::createStencilBufferForRenderTarget(GrRenderTarget* rt, int width, int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001131 // 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 +00001132 SkASSERT(rt->asTexture());
1133 SkASSERT(width >= rt->width());
1134 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001135
1136 int samples = rt->numSamples();
kkinnunen36c57df2015-01-27 00:30:18 -08001137 GrGLStencilBuffer::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001138
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001139 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001140 for (int i = 0; i < stencilFmtCnt; ++i) {
kkinnunen36c57df2015-01-27 00:30:18 -08001141 if (!sbDesc.fRenderbufferID) {
1142 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon12299ab2014-11-14 13:33:09 -08001143 }
kkinnunen36c57df2015-01-27 00:30:18 -08001144 if (!sbDesc.fRenderbufferID) {
bsalomon12299ab2014-11-14 13:33:09 -08001145 return false;
1146 }
kkinnunen36c57df2015-01-27 00:30:18 -08001147 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 // we start with the last stencil format that succeeded in hopes
1149 // that we won't go through this loop more than once after the
1150 // first (painful) stencil creation.
1151 int sIdx = (i + fLastSuccessfulStencilFmtIdx) % stencilFmtCnt;
bsalomon12299ab2014-11-14 13:33:09 -08001152 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
bsalomon@google.com4f3c2532012-01-19 16:16:52 +00001153 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001154 // we do this "if" so that we don't call the multisample
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 // version on a GL that doesn't have an MSAA extension.
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001156 bool created;
1157 if (samples > 0) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001158 created = renderbuffer_storage_msaa(fGLContext,
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001159 samples,
1160 sFmt.fInternalFormat,
1161 width, height);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001162 } else {
bsalomon12299ab2014-11-14 13:33:09 -08001163 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1164 sFmt.fInternalFormat,
1165 width, height));
1166 created = (GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001167 }
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001168 if (created) {
kkinnunen36c57df2015-01-27 00:30:18 -08001169
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001170 // After sized formats we attempt an unsized format and take
1171 // whatever sizes GL gives us. In that case we query for the size.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001172 GrGLStencilBuffer::Format format = sFmt;
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001173 get_stencil_rb_sizes(this->glInterface(), &format);
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +00001174 SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
kkinnunen36c57df2015-01-27 00:30:18 -08001175 (this, sbDesc, width, height, samples, format)));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001176 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
1177 fLastSuccessfulStencilFmtIdx = sIdx;
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001178 rt->setStencilBuffer(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 return true;
bsalomon12299ab2014-11-14 13:33:09 -08001180 }
bsalomon10e23ca2014-11-25 05:52:06 -08001181 // Remove the scratch key from this resource so we don't grab it from the cache ever
1182 // again.
bsalomon3582d3e2015-02-13 14:20:05 -08001183 sb->resourcePriv().removeScratchKey();
bsalomon10e23ca2014-11-25 05:52:06 -08001184 // Set this to 0 since we handed the valid ID off to the failed stencil buffer resource.
kkinnunen36c57df2015-01-27 00:30:18 -08001185 sbDesc.fRenderbufferID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 }
1187 }
kkinnunen36c57df2015-01-27 00:30:18 -08001188 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
bsalomon@google.com558a75b2011-08-08 17:01:14 +00001189 return false;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190}
1191
bsalomon861e1032014-12-16 07:33:49 -08001192bool GrGLGpu::attachStencilBufferToRenderTarget(GrStencilBuffer* sb, GrRenderTarget* rt) {
bsalomon37dd3312014-11-03 08:47:23 -08001193 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194
1195 GrGLuint fbo = glrt->renderFBOID();
1196
1197 if (NULL == sb) {
bsalomon49f085d2014-09-05 13:34:00 -07001198 if (rt->getStencilBuffer()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001200 GR_GL_STENCIL_ATTACHMENT,
1201 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001203 GR_GL_DEPTH_ATTACHMENT,
1204 GR_GL_RENDERBUFFER, 0));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001205#ifdef SK_DEBUG
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001206 GrGLenum status;
1207 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001208 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209#endif
1210 }
1211 return true;
1212 } else {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001213 GrGLStencilBuffer* glsb = static_cast<GrGLStencilBuffer*>(sb);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001214 GrGLuint rb = glsb->renderbufferID();
1215
bsalomon1c63bf62014-07-22 13:09:46 -07001216 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
mtkleinb9eb4ac2015-02-02 18:26:03 -08001217 fStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001218 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fbo));
1219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001220 GR_GL_STENCIL_ATTACHMENT,
1221 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 if (glsb->format().fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001224 GR_GL_DEPTH_ATTACHMENT,
1225 GR_GL_RENDERBUFFER, rb));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 } else {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001227 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001228 GR_GL_DEPTH_ATTACHMENT,
1229 GR_GL_RENDERBUFFER, 0));
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001232 GrGLenum status;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001233 if (!this->glCaps().isColorConfigAndStencilFormatVerified(rt->config(), glsb->format())) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001234 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1235 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001237 GR_GL_STENCIL_ATTACHMENT,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001238 GR_GL_RENDERBUFFER, 0));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001239 if (glsb->format().fPacked) {
1240 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1241 GR_GL_DEPTH_ATTACHMENT,
1242 GR_GL_RENDERBUFFER, 0));
1243 }
1244 return false;
1245 } else {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001246 fGLContext.caps()->markColorConfigAndStencilFormatAsVerified(
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001247 rt->config(),
1248 glsb->format());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001249 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 }
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001251 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001253}
1254
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001255////////////////////////////////////////////////////////////////////////////////
1256
bsalomon861e1032014-12-16 07:33:49 -08001257GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001258 GrGLVertexBuffer::Desc desc;
1259 desc.fDynamic = dynamic;
1260 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001261
bsalomon@google.com96966a52013-02-21 16:34:21 +00001262 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001263 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001264 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001266 } else {
1267 GL_CALL(GenBuffers(1, &desc.fID));
1268 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001269 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001270 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1271 // make sure driver can allocate memory for this buffer
1272 GL_ALLOC_CALL(this->glInterface(),
1273 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001274 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001275 NULL, // data ptr
1276 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1277 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1278 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001279 this->notifyVertexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001280 return NULL;
1281 }
1282 GrGLVertexBuffer* vertexBuffer = SkNEW_ARGS(GrGLVertexBuffer, (this, desc));
1283 return vertexBuffer;
1284 }
1285 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001287}
1288
bsalomon861e1032014-12-16 07:33:49 -08001289GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001290 GrGLIndexBuffer::Desc desc;
1291 desc.fDynamic = dynamic;
1292 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001293
bsalomon@google.com96966a52013-02-21 16:34:21 +00001294 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && desc.fDynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001295 desc.fID = 0;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001296 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001298 } else {
1299 GL_CALL(GenBuffers(1, &desc.fID));
1300 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001301 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001302 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1303 // make sure driver can allocate memory for this buffer
1304 GL_ALLOC_CALL(this->glInterface(),
1305 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001306 (GrGLsizeiptr) desc.fSizeInBytes,
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001307 NULL, // data ptr
1308 desc.fDynamic ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW));
1309 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1310 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001311 this->notifyIndexBufferDelete(desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001312 return NULL;
1313 }
1314 GrIndexBuffer* indexBuffer = SkNEW_ARGS(GrGLIndexBuffer, (this, desc));
1315 return indexBuffer;
1316 }
1317 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +00001318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001319}
1320
bsalomon3e791242014-12-17 13:43:13 -08001321void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001322 const GrGLIRect& rtViewport,
1323 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001324 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001325 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001326 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001327 scissorState.rect().fLeft,
1328 scissorState.rect().fTop,
1329 scissorState.rect().width(),
1330 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001331 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001332 // if the scissor fully contains the viewport then we fall through and
1333 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001334 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001335 if (fHWScissorSettings.fRect != scissor) {
1336 scissor.pushToGLScissor(this->glInterface());
1337 fHWScissorSettings.fRect = scissor;
1338 }
1339 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1340 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1341 fHWScissorSettings.fEnabled = kYes_TriState;
1342 }
1343 return;
1344 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 }
joshualitt77b13072014-10-27 14:51:01 -07001346
1347 // See fall through note above
1348 this->disableScissor();
1349}
1350
joshualitt17e73142015-01-21 11:52:36 -08001351bool GrGLGpu::flushGLState(const DrawArgs& args, bool isLineDraw) {
egdaniel080e6732014-12-22 07:35:52 -08001352 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001353 const GrPipeline& pipeline = *args.fPipeline;
1354 args.fPipeline->getXferProcessor()->getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001355
egdaniel8dd688b2015-01-22 10:16:09 -08001356 this->flushDither(pipeline.isDitherState());
egdaniel080e6732014-12-22 07:35:52 -08001357 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001358 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001359
joshualitt873ad0e2015-01-20 09:08:51 -08001360 fCurrentProgram.reset(fProgramCache->getProgram(args));
bsalomon1f78c0a2014-12-17 09:43:13 -08001361 if (NULL == fCurrentProgram.get()) {
1362 SkDEBUGFAIL("Failed to create program!");
1363 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001364 }
1365
bsalomon1f78c0a2014-12-17 09:43:13 -08001366 fCurrentProgram.get()->ref();
1367
1368 GrGLuint programID = fCurrentProgram->programID();
1369 if (fHWProgramID != programID) {
1370 GL_CALL(UseProgram(programID));
1371 fHWProgramID = programID;
1372 }
1373
egdaniel080e6732014-12-22 07:35:52 -08001374 if (blendInfo.fWriteColor) {
1375 this->flushBlend(blendInfo);
1376 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001377
egdaniel8dd688b2015-01-22 10:16:09 -08001378 fCurrentProgram->setData(*args.fPrimitiveProcessor, pipeline, *args.fBatchTracker);
bsalomon1f78c0a2014-12-17 09:43:13 -08001379
egdaniel8dd688b2015-01-22 10:16:09 -08001380 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1381 this->flushStencil(pipeline.getStencil());
1382 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
1383 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), isLineDraw);
bsalomonbc3d0de2014-12-15 13:45:03 -08001384
1385 // This must come after textures are flushed because a texture may need
1386 // to be msaa-resolved (which will modify bound FBO state).
1387 this->flushRenderTarget(glRT, NULL);
1388
1389 return true;
1390}
1391
joshualitt873ad0e2015-01-20 09:08:51 -08001392void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomonbc3d0de2014-12-15 13:45:03 -08001393 const GrDrawTarget::DrawInfo& info,
1394 size_t* indexOffsetInBytes) {
1395 GrGLVertexBuffer* vbuf;
1396 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
1397
1398 SkASSERT(vbuf);
1399 SkASSERT(!vbuf->isMapped());
1400
1401 GrGLIndexBuffer* ibuf = NULL;
1402 if (info.isIndexed()) {
1403 SkASSERT(indexOffsetInBytes);
1404
1405 *indexOffsetInBytes = 0;
1406 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
1407
1408 SkASSERT(ibuf);
1409 SkASSERT(!ibuf->isMapped());
1410 *indexOffsetInBytes += ibuf->baseOffset();
1411 }
1412 GrGLAttribArrayState* attribState =
1413 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1414
joshualitt873ad0e2015-01-20 09:08:51 -08001415 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001416 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001417
joshualitt873ad0e2015-01-20 09:08:51 -08001418 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001419
1420 size_t vertexOffsetInBytes = stride * info.startVertex();
1421
1422 vertexOffsetInBytes += vbuf->baseOffset();
1423
bsalomonbc3d0de2014-12-15 13:45:03 -08001424 uint32_t usedAttribArraysMask = 0;
1425 size_t offset = 0;
1426
1427 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001428 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001429 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001430 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001431 attribState->set(this,
1432 attribIndex,
1433 vbuf,
1434 GrGLAttribTypeToLayout(attribType).fCount,
1435 GrGLAttribTypeToLayout(attribType).fType,
1436 GrGLAttribTypeToLayout(attribType).fNormalized,
1437 stride,
1438 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001439 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001440 }
1441 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1442 }
1443}
1444
joshualitt873ad0e2015-01-20 09:08:51 -08001445void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1446 const GrPrimitiveProcessor& primProc,
egdaniel8dd688b2015-01-22 10:16:09 -08001447 const GrPipeline& pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -08001448 const GrBatchTracker& batchTracker) const {
bsalomon50785a32015-02-06 07:02:37 -08001449 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, this, batchTracker)) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001450 SkDEBUGFAIL("Failed to generate GL program descriptor");
1451 }
1452}
1453
bsalomon861e1032014-12-16 07:33:49 -08001454void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001455 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001456 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001457 fHWScissorSettings.fEnabled = kNo_TriState;
1458 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001459 }
1460}
1461
bsalomon861e1032014-12-16 07:33:49 -08001462void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect* rect, GrColor color,
joshualitt4b68ec02014-11-07 14:11:45 -08001463 bool canIgnoreRect) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001464 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001465 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001466 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001467
robertphillips@google.com56ce48a2013-10-31 21:44:25 +00001468 if (canIgnoreRect && this->glCaps().fullClearIsFree()) {
1469 rect = NULL;
1470 }
1471
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001472 SkIRect clippedRect;
bsalomon49f085d2014-09-05 13:34:00 -07001473 if (rect) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001474 // flushScissor expects rect to be clipped to the target.
bsalomon@google.com74b98712011-11-11 19:46:16 +00001475 clippedRect = *rect;
bsalomonb0bd4f62014-09-03 07:19:50 -07001476 SkIRect rtRect = SkIRect::MakeWH(target->width(), target->height());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001477 if (clippedRect.intersect(rtRect)) {
1478 rect = &clippedRect;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +00001479 } else {
1480 return;
1481 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001482 }
rmistry@google.comd6bab022013-12-02 13:50:38 +00001483
bsalomonb0bd4f62014-09-03 07:19:50 -07001484 this->flushRenderTarget(glRT, rect);
bsalomon3e791242014-12-17 13:43:13 -08001485 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001486 if (rect) {
1487 scissorState.set(*rect);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001488 }
joshualitt77b13072014-10-27 14:51:01 -07001489 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001490
1491 GrGLfloat r, g, b, a;
1492 static const GrGLfloat scale255 = 1.f / 255.f;
1493 a = GrColorUnpackA(color) * scale255;
1494 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001495 r = GrColorUnpackR(color) * scaleRGB;
1496 g = GrColorUnpackG(color) * scaleRGB;
1497 b = GrColorUnpackB(color) * scaleRGB;
1498
1499 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001500 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001501 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001503}
1504
bsalomon861e1032014-12-16 07:33:49 -08001505void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001506 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001507 if (!this->caps()->discardRenderTargetSupport()) {
1508 return;
1509 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001510
1511 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
bsalomon1c63bf62014-07-22 13:09:46 -07001512 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1513 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
mtkleinb9eb4ac2015-02-02 18:26:03 -08001514 fStats.incRenderTargetBinds();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001515 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1516 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001517 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001518 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001519 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001520 break;
1521 case GrGLCaps::kInvalidate_InvalidateFBType:
1522 if (0 == glRT->renderFBOID()) {
1523 // When rendering to the default framebuffer the legal values for attachments
1524 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1525 // types.
1526 static const GrGLenum attachments[] = { GR_GL_COLOR };
1527 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1528 attachments));
1529 } else {
1530 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1531 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1532 attachments));
1533 }
1534 break;
1535 case GrGLCaps::kDiscard_InvalidateFBType: {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001536 if (0 == glRT->renderFBOID()) {
1537 // When rendering to the default framebuffer the legal values for attachments
1538 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1539 // types. See glDiscardFramebuffer() spec.
1540 static const GrGLenum attachments[] = { GR_GL_COLOR };
1541 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1542 attachments));
1543 } else {
1544 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
1545 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
1546 attachments));
1547 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001548 break;
1549 }
1550 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001551 renderTarget->flagAsResolved();
1552}
1553
1554
bsalomon861e1032014-12-16 07:33:49 -08001555void GrGLGpu::clearStencil(GrRenderTarget* target) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001556 if (NULL == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001557 return;
1558 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001559 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1560 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001561
joshualitt77b13072014-10-27 14:51:01 -07001562 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001563
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001564 GL_CALL(StencilMask(0xffffffff));
1565 GL_CALL(ClearStencil(0));
1566 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001567 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001568}
1569
bsalomon861e1032014-12-16 07:33:49 -08001570void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001571 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001572
1573 // this should only be called internally when we know we have a
1574 // stencil buffer.
bsalomon49f085d2014-09-05 13:34:00 -07001575 SkASSERT(target->getStencilBuffer());
bsalomonb0bd4f62014-09-03 07:19:50 -07001576 GrGLint stencilBitCount = target->getStencilBuffer()->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001577#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001578 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001579 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001580#else
1581 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001582 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001583 // turned into draws. Our contract on GrDrawTarget says that
1584 // changing the clip between stencil passes may or may not
1585 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001586 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001587#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001588 GrGLint value;
1589 if (insideClip) {
1590 value = (1 << (stencilBitCount - 1));
1591 } else {
1592 value = 0;
1593 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001594 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1595 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001596
bsalomon3e791242014-12-17 13:43:13 -08001597 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001598 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001599 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001600
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001601 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001602 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001603 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001604 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001605}
1606
bsalomon861e1032014-12-16 07:33:49 -08001607bool GrGLGpu::readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001608 int left, int top,
1609 int width, int height,
1610 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001611 size_t rowBytes) const {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001612 // If this rendertarget is aready TopLeft, we don't need to flip.
1613 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1614 return false;
1615 }
1616
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001617 // if GL can do the flip then we'll never pay for it.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001618 if (this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001619 return false;
1620 }
1621
1622 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001623 // get the flip for free. Otherwise it costs.
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001624 if (this->glCaps().packRowLengthSupport()) {
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001625 return true;
1626 }
1627 // If we have to do memcpys to handle rowBytes then y-flip is free
1628 // Note the rowBytes might be tight to the passed in data, but if data
1629 // gets clipped in x to the target the rowBytes will no longer be tight.
1630 if (left >= 0 && (left + width) < renderTarget->width()) {
1631 return 0 == rowBytes ||
1632 GrBytesPerPixel(config) * width == rowBytes;
1633 } else {
1634 return false;
1635 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001636}
1637
bsalomon861e1032014-12-16 07:33:49 -08001638bool GrGLGpu::onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001639 int left, int top,
1640 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001641 GrPixelConfig config,
1642 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001643 size_t rowBytes) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001644 // We cannot read pixels into a compressed buffer
1645 if (GrPixelConfigIsCompressed(config)) {
1646 return false;
1647 }
1648
1649 GrGLenum format = 0;
1650 GrGLenum type = 0;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001651 bool flipY = kBottomLeft_GrSurfaceOrigin == target->origin();
bsalomon@google.com280e99f2012-01-05 16:17:38 +00001652 if (!this->configToGLFormats(config, false, NULL, &format, &type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001654 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001655 size_t bpp = GrBytesPerPixel(config);
1656 if (!adjust_pixel_ops_params(target->width(), target->height(), bpp,
1657 &left, &top, &width, &height,
1658 const_cast<const void**>(&buffer),
1659 &rowBytes)) {
1660 return false;
1661 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001662
bsalomon@google.comc6980972011-11-02 19:57:21 +00001663 // resolve the render target if necessary
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001664 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001665 switch (tgt->getResolveType()) {
1666 case GrGLRenderTarget::kCantResolve_ResolveType:
1667 return false;
1668 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon37dd3312014-11-03 08:47:23 -08001669 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001670 break;
1671 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001672 this->onResolveRenderTarget(tgt);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001673 // we don't track the state of the READ FBO ID.
mtkleinb9eb4ac2015-02-02 18:26:03 -08001674 fStats.incRenderTargetBinds();
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001675 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER,
1676 tgt->textureFBOID()));
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001677 break;
1678 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001679 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00001680 }
1681
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001682 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00001683
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001684 // the read rect is viewport-relative
1685 GrGLIRect readRect;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001686 readRect.setRelativeTo(glvp, left, top, width, height, target->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001687
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001688 size_t tightRowBytes = bpp * width;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001689 if (0 == rowBytes) {
1690 rowBytes = tightRowBytes;
1691 }
1692 size_t readDstRowBytes = tightRowBytes;
1693 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001694
bsalomon@google.comc6980972011-11-02 19:57:21 +00001695 // determine if GL can read using the passed rowBytes or if we need
1696 // a scratch buffer.
georgeb62508b2014-08-12 18:00:47 -07001697 GrAutoMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001698 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001699 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001700 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00001701 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001702 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001703 readDstRowBytes = rowBytes;
1704 } else {
1705 scratch.reset(tightRowBytes * height);
1706 readDst = scratch.get();
1707 }
1708 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001709 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001710 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
1711 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001712 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
1713 readRect.fWidth, readRect.fHeight,
bsalomon@google.comc6980972011-11-02 19:57:21 +00001714 format, type, readDst));
1715 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001716 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00001717 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
1718 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001719 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001720 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001721 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001722 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001723
1724 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00001725 // API presents top-to-bottom. We must preserve the padding contents. Note
1726 // that the above readPixels did not overwrite the padding.
1727 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001728 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001729 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001730 scratch.reset(tightRowBytes);
1731 void* tmpRow = scratch.get();
1732 // flip y in-place by rows
1733 const int halfY = height >> 1;
1734 char* top = reinterpret_cast<char*>(buffer);
1735 char* bottom = top + (height - 1) * rowBytes;
1736 for (int y = 0; y < halfY; y++) {
1737 memcpy(tmpRow, top, tightRowBytes);
1738 memcpy(top, bottom, tightRowBytes);
1739 memcpy(bottom, tmpRow, tightRowBytes);
1740 top += rowBytes;
1741 bottom -= rowBytes;
1742 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001743 }
1744 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001745 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00001746 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001747 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00001748 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001749 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001750 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001751 dst += (height-1) * rowBytes;
1752 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00001753 for (int y = 0; y < height; y++) {
1754 memcpy(dst, src, tightRowBytes);
1755 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001756 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001757 dst += rowBytes;
1758 } else {
1759 dst -= rowBytes;
1760 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
1762 }
1763 return true;
1764}
1765
bsalomon861e1032014-12-16 07:33:49 -08001766void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001767
bsalomon49f085d2014-09-05 13:34:00 -07001768 SkASSERT(target);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +00001769
bsalomonb0bd4f62014-09-03 07:19:50 -07001770 uint32_t rtID = target->getUniqueID();
bsalomon1c63bf62014-07-22 13:09:46 -07001771 if (fHWBoundRenderTargetUniqueID != rtID) {
mtkleinb9eb4ac2015-02-02 18:26:03 -08001772 fStats.incRenderTargetBinds();
bsalomonb0bd4f62014-09-03 07:19:50 -07001773 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001774#ifdef SK_DEBUG
rmistry@google.comd6bab022013-12-02 13:50:38 +00001775 // don't do this check in Chromium -- this is causing
1776 // lots of repeated command buffer flushes when the compositor is
1777 // rendering with Ganesh, which is really slow; even too slow for
1778 // Debug mode.
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001779 if (!this->glContext().isChromium()) {
rmistry@google.comd6bab022013-12-02 13:50:38 +00001780 GrGLenum status;
1781 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1782 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
bsalomon861e1032014-12-16 07:33:49 -08001783 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
rmistry@google.comd6bab022013-12-02 13:50:38 +00001784 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001785 }
robertphillips@google.coma6ffb582013-04-29 16:50:17 +00001786#endif
bsalomon1c63bf62014-07-22 13:09:46 -07001787 fHWBoundRenderTargetUniqueID = rtID;
bsalomonb0bd4f62014-09-03 07:19:50 -07001788 const GrGLIRect& vp = target->getViewport();
bsalomon@google.coma3201942012-06-21 19:58:20 +00001789 if (fHWViewport != vp) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001790 vp.pushToGLViewport(this->glInterface());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001791 fHWViewport = vp;
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 }
1793 }
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001794 if (NULL == bound || !bound->isEmpty()) {
bsalomonb0bd4f62014-09-03 07:19:50 -07001795 target->flagAsNeedingResolve(bound);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001796 }
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00001797
bsalomonb0bd4f62014-09-03 07:19:50 -07001798 GrTexture *texture = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001799 if (texture) {
bsalomonafbf2d62014-09-30 12:18:44 -07001800 texture->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00001801 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001802}
1803
twiz@google.com0f31ca72011-03-18 17:38:11 +00001804GrGLenum gPrimitiveType2GLMode[] = {
1805 GR_GL_TRIANGLES,
1806 GR_GL_TRIANGLE_STRIP,
1807 GR_GL_TRIANGLE_FAN,
1808 GR_GL_POINTS,
1809 GR_GL_LINES,
1810 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00001811};
1812
bsalomon@google.comd302f142011-03-03 13:54:13 +00001813#define SWAP_PER_DRAW 0
1814
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001815#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001816 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001817 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001818 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00001819 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00001820 void SwapBuf() {
1821 DWORD procID = GetCurrentProcessId();
1822 HWND hwnd = GetTopWindow(GetDesktopWindow());
1823 while(hwnd) {
1824 DWORD wndProcID = 0;
1825 GetWindowThreadProcessId(hwnd, &wndProcID);
1826 if(wndProcID == procID) {
1827 SwapBuffers(GetDC(hwnd));
1828 }
1829 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
1830 }
1831 }
1832 #endif
1833#endif
1834
joshualitt873ad0e2015-01-20 09:08:51 -08001835void GrGLGpu::onDraw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
joshualitt17e73142015-01-21 11:52:36 -08001836 if (!this->flushGLState(args, GrIsPrimTypeLines(info.primitiveType()))) {
bsalomond95263c2014-12-16 13:05:12 -08001837 return;
1838 }
1839
joshualitt873ad0e2015-01-20 09:08:51 -08001840 size_t indexOffsetInBytes = 0;
1841 this->setupGeometry(*args.fPrimitiveProcessor, info, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00001842
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001843 SkASSERT((size_t)info.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001844
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001845 if (info.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00001846 GrGLvoid* indices =
1847 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) * info.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00001848 // info.startVertex() was accounted for by setupGeometry.
1849 GL_CALL(DrawElements(gPrimitiveType2GLMode[info.primitiveType()],
1850 info.indexCount(),
1851 GR_GL_UNSIGNED_SHORT,
1852 indices));
1853 } else {
1854 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
1855 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
1856 // accounted for startVertex.
1857 GL_CALL(DrawArrays(gPrimitiveType2GLMode[info.primitiveType()], 0, info.vertexCount()));
1858 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00001859#if SWAP_PER_DRAW
1860 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001861 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001862 aglSwapBuffers(aglGetCurrentContext());
1863 int set_a_break_pt_here = 9;
1864 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001865 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00001866 SwapBuf();
1867 int set_a_break_pt_here = 9;
1868 SwapBuf();
1869 #endif
1870#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00001871}
1872
bsalomon3e791242014-12-17 13:43:13 -08001873void GrGLGpu::onStencilPath(const GrPath* path, const StencilPathState& state) {
egdaniel080e6732014-12-22 07:35:52 -08001874 this->flushColorWrite(false);
egdaniel8dd688b2015-01-22 10:16:09 -08001875 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon3e791242014-12-17 13:43:13 -08001876
1877 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(state.fRenderTarget);
bsalomon1f78c0a2014-12-17 09:43:13 -08001878 SkISize size = SkISize::Make(rt->width(), rt->height());
bsalomon3e791242014-12-17 13:43:13 -08001879 this->glPathRendering()->setProjectionMatrix(*state.fViewMatrix, size, rt->origin());
1880 this->flushScissor(*state.fScissor, rt->getViewport(), rt->origin());
1881 this->flushHWAAState(rt, state.fUseHWAA, false);
bsalomon1f78c0a2014-12-17 09:43:13 -08001882 this->flushRenderTarget(rt, NULL);
1883
bsalomon3e791242014-12-17 13:43:13 -08001884 fPathRendering->stencilPath(path, *state.fStencil);
bsalomond95263c2014-12-16 13:05:12 -08001885}
1886
joshualitt873ad0e2015-01-20 09:08:51 -08001887void GrGLGpu::onDrawPath(const DrawArgs& args, const GrPath* path,
bsalomond95263c2014-12-16 13:05:12 -08001888 const GrStencilSettings& stencil) {
joshualitt17e73142015-01-21 11:52:36 -08001889 if (!this->flushGLState(args, false)) {
bsalomond95263c2014-12-16 13:05:12 -08001890 return;
1891 }
1892 fPathRendering->drawPath(path, stencil);
1893}
1894
joshualitt873ad0e2015-01-20 09:08:51 -08001895void GrGLGpu::onDrawPaths(const DrawArgs& args,
bsalomond95263c2014-12-16 13:05:12 -08001896 const GrPathRange* pathRange,
1897 const void* indices,
1898 GrDrawTarget::PathIndexType indexType,
1899 const float transformValues[],
1900 GrDrawTarget::PathTransformType transformType,
1901 int count,
1902 const GrStencilSettings& stencil) {
joshualitt17e73142015-01-21 11:52:36 -08001903 if (!this->flushGLState(args, false)) {
bsalomond95263c2014-12-16 13:05:12 -08001904 return;
1905 }
1906 fPathRendering->drawPaths(pathRange, indices, indexType, transformValues,
1907 transformType, count, stencil);
1908}
1909
bsalomon861e1032014-12-16 07:33:49 -08001910void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001911 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001912 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001913 // Some extensions automatically resolves the texture when it is read.
1914 if (this->glCaps().usesMSAARenderBuffers()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001915 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
mtkleinb9eb4ac2015-02-02 18:26:03 -08001916 fStats.incRenderTargetBinds();
1917 fStats.incRenderTargetBinds();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001918 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
1919 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
1920 // make sure we go through flushRenderTarget() since we've modified
1921 // the bound DRAW FBO ID.
bsalomon1c63bf62014-07-22 13:09:46 -07001922 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001923 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001924 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00001925
bsalomon@google.com347c3822013-05-01 20:10:01 +00001926 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001927 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08001928 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001929 scissorState.set(dirtyRect);
1930 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001931 GL_CALL(ResolveMultisampleFramebuffer());
1932 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08001933 GrGLIRect r;
1934 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
1935 dirtyRect.width(), dirtyRect.height(), target->origin());
1936
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001937 int right = r.fLeft + r.fWidth;
1938 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07001939
1940 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07001941 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001942 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
1943 r.fLeft, r.fBottom, right, top,
1944 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00001945 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001946 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00001947 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00001948 }
1949}
1950
bsalomon@google.com411dad02012-06-05 20:24:20 +00001951namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00001952
bsalomon@google.com411dad02012-06-05 20:24:20 +00001953
1954GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
1955 static const GrGLenum gTable[] = {
1956 GR_GL_KEEP, // kKeep_StencilOp
1957 GR_GL_REPLACE, // kReplace_StencilOp
1958 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
1959 GR_GL_INCR, // kIncClamp_StencilOp
1960 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
1961 GR_GL_DECR, // kDecClamp_StencilOp
1962 GR_GL_ZERO, // kZero_StencilOp
1963 GR_GL_INVERT, // kInvert_StencilOp
1964 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001965 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001966 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
1967 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
1968 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
1969 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
1970 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
1971 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
1972 GR_STATIC_ASSERT(6 == kZero_StencilOp);
1973 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001974 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001975 return gTable[op];
1976}
1977
1978void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001979 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00001980 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00001981 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07001982 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001983 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
1984 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
1985
1986 GrGLint ref = settings.funcRef(grFace);
1987 GrGLint mask = settings.funcMask(grFace);
1988 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00001989
1990 if (GR_GL_FRONT_AND_BACK == glFace) {
1991 // we call the combined func just in case separate stencil is not
1992 // supported.
1993 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
1994 GR_GL_CALL(gl, StencilMask(writeMask));
1995 GR_GL_CALL(gl, StencilOp(glFailOp, glPassOp, glPassOp));
1996 } else {
1997 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
1998 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
1999 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, glPassOp, glPassOp));
2000 }
2001}
2002}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002003
bsalomon3e791242014-12-17 13:43:13 -08002004void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2005 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002006 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002007 if (kNo_TriState != fHWStencilTestEnabled) {
2008 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2009 fHWStencilTestEnabled = kNo_TriState;
2010 }
2011 } else {
2012 if (kYes_TriState != fHWStencilTestEnabled) {
2013 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2014 fHWStencilTestEnabled = kYes_TriState;
2015 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002016 }
joshualitta58fe352014-10-27 08:39:00 -07002017 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002018 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002019 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002020 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002021 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002022 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002023 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002024 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002025 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002026 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002027 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002028 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002029 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002030 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002031 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002032 }
2033 }
joshualitta58fe352014-10-27 08:39:00 -07002034 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002035 }
2036}
2037
bsalomon3e791242014-12-17 13:43:13 -08002038void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool isLineDraw) {
bsalomon@google.com202d1392013-03-19 18:58:08 +00002039// At least some ATI linux drivers will render GL_LINES incorrectly when MSAA state is enabled but
2040// the target is not multisampled. Single pixel wide lines are rendered thicker than 1 pixel wide.
2041#if 0
2042 // Replace RT_HAS_MSAA with this definition once this driver bug is no longer a relevant concern
2043 #define RT_HAS_MSAA rt->isMultisampled()
2044#else
bsalomon3e791242014-12-17 13:43:13 -08002045 #define RT_HAS_MSAA (rt->isMultisampled() || isLineDraw)
bsalomon@google.com202d1392013-03-19 18:58:08 +00002046#endif
2047
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002048 if (kGL_GrGLStandard == this->glStandard()) {
egdanielb414f252014-07-29 13:15:47 -07002049 if (RT_HAS_MSAA) {
bsalomon3e791242014-12-17 13:43:13 -08002050 if (useHWAA) {
egdanielb414f252014-07-29 13:15:47 -07002051 if (kYes_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002052 GL_CALL(Enable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002053 fMSAAEnabled = kYes_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002054 }
2055 } else {
egdanielb414f252014-07-29 13:15:47 -07002056 if (kNo_TriState != fMSAAEnabled) {
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002057 GL_CALL(Disable(GR_GL_MULTISAMPLE));
egdanielb414f252014-07-29 13:15:47 -07002058 fMSAAEnabled = kNo_TriState;
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +00002059 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002060 }
2061 }
2062 }
2063}
2064
egdaniel080e6732014-12-22 07:35:52 -08002065void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo) {
egdanielb414f252014-07-29 13:15:47 -07002066 // Any optimization to disable blending should have already been applied and
2067 // tweaked the coeffs to (1, 0).
egdanielc2304142014-12-11 13:15:13 -08002068
egdanielc2304142014-12-11 13:15:13 -08002069 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2070 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
egdanielb414f252014-07-29 13:15:47 -07002071 bool blendOff = kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
2072 if (blendOff) {
2073 if (kNo_TriState != fHWBlendState.fEnabled) {
2074 GL_CALL(Disable(GR_GL_BLEND));
2075 fHWBlendState.fEnabled = kNo_TriState;
2076 }
2077 } else {
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002078 if (kYes_TriState != fHWBlendState.fEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002079 GL_CALL(Enable(GR_GL_BLEND));
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +00002080 fHWBlendState.fEnabled = kYes_TriState;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002081 }
egdanielb414f252014-07-29 13:15:47 -07002082 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2083 fHWBlendState.fDstCoeff != dstCoeff) {
2084 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2085 gXfermodeCoeff2Blend[dstCoeff]));
2086 fHWBlendState.fSrcCoeff = srcCoeff;
2087 fHWBlendState.fDstCoeff = dstCoeff;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002088 }
egdanielc2304142014-12-11 13:15:13 -08002089 GrColor blendConst = blendInfo.fBlendConstant;
egdanielb414f252014-07-29 13:15:47 -07002090 if ((BlendCoeffReferencesConstant(srcCoeff) ||
2091 BlendCoeffReferencesConstant(dstCoeff)) &&
2092 (!fHWBlendState.fConstColorValid ||
2093 fHWBlendState.fConstColor != blendConst)) {
2094 GrGLfloat c[4];
2095 GrColorToRGBAFloat(blendConst, c);
2096 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2097 fHWBlendState.fConstColor = blendConst;
2098 fHWBlendState.fConstColorValid = true;
bsalomon@google.com0650e812011-04-08 18:07:53 +00002099 }
2100 }
2101}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002102
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002103static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002104 static const GrGLenum gWrapModes[] = {
2105 GR_GL_CLAMP_TO_EDGE,
2106 GR_GL_REPEAT,
2107 GR_GL_MIRRORED_REPEAT
2108 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002109 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002110 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2111 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2112 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2113 return gWrapModes[tm];
2114}
2115
bsalomon861e1032014-12-16 07:33:49 -08002116void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002117 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002118
bsalomon@google.comb8670992012-07-25 21:27:09 +00002119 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2120 // 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 +00002121 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002122 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002123 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002124 this->onResolveRenderTarget(texRT);
2125 }
2126
bsalomon1c63bf62014-07-22 13:09:46 -07002127 uint32_t textureID = texture->getUniqueID();
2128 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002129 this->setTextureUnit(unitIdx);
2130 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002131 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002132 }
2133
bsalomon@google.com4c883782012-06-04 19:05:11 +00002134 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002135 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002136 bool setAll = timestamp < this->getResetTimestamp();
2137 GrGLTexture::TexParams newTexParams;
2138
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002139 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002140 GR_GL_NEAREST,
2141 GR_GL_LINEAR,
2142 GR_GL_LINEAR_MIPMAP_LINEAR
2143 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002144 static GrGLenum glMagFilterModes[] = {
2145 GR_GL_NEAREST,
2146 GR_GL_LINEAR,
2147 GR_GL_LINEAR
2148 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002149 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002150
2151 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2152 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2153 filterMode = GrTextureParams::kBilerp_FilterMode;
2154 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002155 }
bsalomonefd7d452014-10-23 14:17:46 -07002156
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002157 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2158 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002159
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002160 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002161 texture->texturePriv().mipMapsAreDirty()) {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002162 GL_CALL(GenerateMipmap(GR_GL_TEXTURE_2D));
bsalomonafbf2d62014-09-30 12:18:44 -07002163 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002164 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002165
bsalomon@google.comb8670992012-07-25 21:27:09 +00002166 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2167 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002168 memcpy(newTexParams.fSwizzleRGBA,
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002169 GrGLShaderBuilder::GetTexParamSwizzle(texture->config(), this->glCaps()),
bsalomon@google.com4c883782012-06-04 19:05:11 +00002170 sizeof(newTexParams.fSwizzleRGBA));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002171 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002172 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002173 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2174 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002175 newTexParams.fMagFilter));
2176 }
2177 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2178 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002179 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2180 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002181 newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002182 }
2183 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002184 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002185 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2186 GR_GL_TEXTURE_WRAP_S,
2187 newTexParams.fWrapS));
2188 }
2189 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002190 this->setTextureUnit(unitIdx);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002191 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
2192 GR_GL_TEXTURE_WRAP_T,
2193 newTexParams.fWrapT));
2194 }
2195 if (this->glCaps().textureSwizzleSupport() &&
2196 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2197 oldTexParams.fSwizzleRGBA,
2198 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002199 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002200 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002201 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2202 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
2203 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2204 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2205 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2206 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
2207 } else {
2208 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2209 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
2210 GL_CALL(TexParameteriv(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
2211 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002212 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002213 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002214}
2215
bsalomon3e791242014-12-17 13:43:13 -08002216void GrGLGpu::flushDither(bool dither) {
2217 if (dither) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002218 if (kYes_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(Enable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002220 fHWDitherEnabled = kYes_TriState;
2221 }
2222 } else {
2223 if (kNo_TriState != fHWDitherEnabled) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002224 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002225 fHWDitherEnabled = kNo_TriState;
reed@google.comac10a2d2010-12-22 21:39:39 +00002226 }
2227 }
bsalomon3e791242014-12-17 13:43:13 -08002228}
reed@google.comac10a2d2010-12-22 21:39:39 +00002229
egdaniel080e6732014-12-22 07:35:52 -08002230void GrGLGpu::flushColorWrite(bool writeColor) {
2231 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002232 if (kNo_TriState != fHWWriteToColor) {
2233 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2234 GR_GL_FALSE, GR_GL_FALSE));
2235 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002236 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002237 } else {
2238 if (kYes_TriState != fHWWriteToColor) {
2239 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2240 fHWWriteToColor = kYes_TriState;
2241 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002242 }
bsalomon3e791242014-12-17 13:43:13 -08002243}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002244
egdaniel8dd688b2015-01-22 10:16:09 -08002245void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002246 if (fHWDrawFace != face) {
2247 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002248 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002249 GL_CALL(Enable(GR_GL_CULL_FACE));
2250 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002251 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002252 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002253 GL_CALL(Enable(GR_GL_CULL_FACE));
2254 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002255 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002256 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002257 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002258 break;
2259 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002260 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002261 }
bsalomon3e791242014-12-17 13:43:13 -08002262 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002263 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002264}
2265
bsalomon861e1032014-12-16 07:33:49 -08002266bool GrGLGpu::configToGLFormats(GrPixelConfig config,
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002267 bool getSizedInternalFormat,
2268 GrGLenum* internalFormat,
2269 GrGLenum* externalFormat,
2270 GrGLenum* externalType) {
2271 GrGLenum dontCare;
2272 if (NULL == internalFormat) {
2273 internalFormat = &dontCare;
2274 }
2275 if (NULL == externalFormat) {
2276 externalFormat = &dontCare;
2277 }
2278 if (NULL == externalType) {
2279 externalType = &dontCare;
2280 }
2281
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002282 if(!this->glCaps().isConfigTexturable(config)) {
2283 return false;
2284 }
2285
reed@google.comac10a2d2010-12-22 21:39:39 +00002286 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00002287 case kRGBA_8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +00002288 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002289 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002290 if (getSizedInternalFormat) {
2291 *internalFormat = GR_GL_RGBA8;
2292 } else {
2293 *internalFormat = GR_GL_RGBA;
2294 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002295 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002296 break;
bsalomon@google.com0342a852012-08-20 19:22:38 +00002297 case kBGRA_8888_GrPixelConfig:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002298 if (this->glCaps().bgraIsInternalFormat()) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002299 if (getSizedInternalFormat) {
2300 *internalFormat = GR_GL_BGRA8;
2301 } else {
2302 *internalFormat = GR_GL_BGRA;
2303 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002304 } else {
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002305 if (getSizedInternalFormat) {
2306 *internalFormat = GR_GL_RGBA8;
2307 } else {
2308 *internalFormat = GR_GL_RGBA;
2309 }
twiz@google.comb65e0cb2011-03-18 20:41:44 +00002310 }
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002311 *externalFormat = GR_GL_BGRA;
bsalomon@google.com6f379512011-11-16 20:36:03 +00002312 *externalType = GR_GL_UNSIGNED_BYTE;
reed@google.comac10a2d2010-12-22 21:39:39 +00002313 break;
jvanverthfa1e8a72014-12-22 08:31:49 -08002314 case kSRGBA_8888_GrPixelConfig:
2315 *internalFormat = GR_GL_SRGB_ALPHA;
2316 *externalFormat = GR_GL_SRGB_ALPHA;
2317 if (getSizedInternalFormat) {
2318 *internalFormat = GR_GL_SRGB8_ALPHA8;
2319 } else {
2320 *internalFormat = GR_GL_SRGB_ALPHA;
2321 }
2322 *externalType = GR_GL_UNSIGNED_BYTE;
2323 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002324 case kRGB_565_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002325 *internalFormat = GR_GL_RGB;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002326 *externalFormat = GR_GL_RGB;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002327 if (getSizedInternalFormat) {
jvanverth3f801cb2014-12-16 09:49:38 -08002328 if (!this->glCaps().ES2CompatibilitySupport()) {
2329 *internalFormat = GR_GL_RGB5;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002330 } else {
2331 *internalFormat = GR_GL_RGB565;
2332 }
2333 } else {
2334 *internalFormat = GR_GL_RGB;
2335 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002336 *externalType = GR_GL_UNSIGNED_SHORT_5_6_5;
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002338 case kRGBA_4444_GrPixelConfig:
twiz@google.com0f31ca72011-03-18 17:38:11 +00002339 *internalFormat = GR_GL_RGBA;
bsalomon@google.com32e4d2a2011-12-09 16:14:25 +00002340 *externalFormat = GR_GL_RGBA;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002341 if (getSizedInternalFormat) {
2342 *internalFormat = GR_GL_RGBA4;
2343 } else {
2344 *internalFormat = GR_GL_RGBA;
2345 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00002346 *externalType = GR_GL_UNSIGNED_SHORT_4_4_4_4;
reed@google.comac10a2d2010-12-22 21:39:39 +00002347 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002348 case kIndex_8_GrPixelConfig:
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002349 // no sized/unsized internal format distinction here
2350 *internalFormat = GR_GL_PALETTE8_RGBA8;
reed@google.comac10a2d2010-12-22 21:39:39 +00002351 break;
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002352 case kAlpha_8_GrPixelConfig:
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002353 if (this->glCaps().textureRedSupport()) {
2354 *internalFormat = GR_GL_RED;
2355 *externalFormat = GR_GL_RED;
2356 if (getSizedInternalFormat) {
2357 *internalFormat = GR_GL_R8;
2358 } else {
2359 *internalFormat = GR_GL_RED;
2360 }
2361 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002362 } else {
2363 *internalFormat = GR_GL_ALPHA;
robertphillips@google.com443e5a52012-04-30 20:01:21 +00002364 *externalFormat = GR_GL_ALPHA;
2365 if (getSizedInternalFormat) {
2366 *internalFormat = GR_GL_ALPHA8;
2367 } else {
2368 *internalFormat = GR_GL_ALPHA;
2369 }
2370 *externalType = GR_GL_UNSIGNED_BYTE;
bsalomon@google.com280e99f2012-01-05 16:17:38 +00002371 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002372 break;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002373 case kETC1_GrPixelConfig:
2374 *internalFormat = GR_GL_COMPRESSED_RGB8_ETC1;
2375 break;
2376 case kLATC_GrPixelConfig:
2377 switch(this->glCaps().latcAlias()) {
2378 case GrGLCaps::kLATC_LATCAlias:
2379 *internalFormat = GR_GL_COMPRESSED_LUMINANCE_LATC1;
2380 break;
2381 case GrGLCaps::kRGTC_LATCAlias:
2382 *internalFormat = GR_GL_COMPRESSED_RED_RGTC1;
2383 break;
2384 case GrGLCaps::k3DC_LATCAlias:
2385 *internalFormat = GR_GL_COMPRESSED_3DC_X;
2386 break;
2387 }
2388 break;
krajcevski238b4562014-06-30 09:09:22 -07002389 case kR11_EAC_GrPixelConfig:
2390 *internalFormat = GR_GL_COMPRESSED_R11;
2391 break;
krajcevski7ef21622014-07-16 15:21:13 -07002392
2393 case kASTC_12x12_GrPixelConfig:
2394 *internalFormat = GR_GL_COMPRESSED_RGBA_ASTC_12x12;
2395 break;
2396
joshualittee5da552014-07-16 13:32:56 -07002397 case kRGBA_float_GrPixelConfig:
2398 *internalFormat = GR_GL_RGBA32F;
2399 *externalFormat = GR_GL_RGBA;
2400 *externalType = GR_GL_FLOAT;
2401 break;
krajcevski7ef21622014-07-16 15:21:13 -07002402
jvanverth28f9c602014-12-05 13:06:35 -08002403 case kAlpha_half_GrPixelConfig:
jvanverth1334c212014-12-18 05:44:55 -08002404 if (this->glCaps().textureRedSupport()) {
2405 if (getSizedInternalFormat) {
2406 *internalFormat = GR_GL_R16F;
2407 } else {
2408 *internalFormat = GR_GL_RED;
2409 }
jvanverth28f9c602014-12-05 13:06:35 -08002410 *externalFormat = GR_GL_RED;
jvanverth1334c212014-12-18 05:44:55 -08002411 } else {
2412 if (getSizedInternalFormat) {
2413 *internalFormat = GR_GL_ALPHA16F;
2414 } else {
2415 *internalFormat = GR_GL_ALPHA;
2416 }
2417 *externalFormat = GR_GL_ALPHA;
2418 }
2419 if (kGL_GrGLStandard == this->glStandard() || this->glVersion() >= GR_GL_VER(3, 0)) {
jvanvertha60b2ea2014-12-12 05:58:06 -08002420 *externalType = GR_GL_HALF_FLOAT;
jvanverth28f9c602014-12-05 13:06:35 -08002421 } else {
jvanverth1334c212014-12-18 05:44:55 -08002422 *externalType = GR_GL_HALF_FLOAT_OES;
jvanverth28f9c602014-12-05 13:06:35 -08002423 }
2424 break;
2425
reed@google.comac10a2d2010-12-22 21:39:39 +00002426 default:
2427 return false;
2428 }
2429 return true;
2430}
2431
bsalomon861e1032014-12-16 07:33:49 -08002432void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002433 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002434 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002435 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002436 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002437 }
2438}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002439
bsalomon861e1032014-12-16 07:33:49 -08002440void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002441 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002442 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002443 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2444 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2445 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002446 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002447 // clear out the this field so that if a program does use this unit it will rebind the correct
2448 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002449 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002450}
2451
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002452namespace {
2453// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon@google.comeb851172013-04-15 13:51:00 +00002454inline bool can_blit_framebuffer(const GrSurface* dst,
2455 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002456 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002457 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2458 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002459 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002460 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2461 // or the rects are not the same (not just the same size but have the same edges).
2462 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2463 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2464 return false;
2465 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002466 return true;
2467 } else {
2468 return false;
2469 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002470}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002471
2472inline bool can_copy_texsubimage(const GrSurface* dst,
2473 const GrSurface* src,
egdaniel0f5f9672015-02-03 11:10:51 -08002474 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002475 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2476 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2477 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002478 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002479 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2480 return false;
2481 }
2482 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2483 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2484 // then we don't want to copy to the texture but to the MSAA buffer.
bsalomon49f085d2014-09-05 13:34:00 -07002485 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002486 return false;
2487 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002488 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2489 // If the src is multisampled (and uses an extension where there is a separate MSAA
2490 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
bsalomon49f085d2014-09-05 13:34:00 -07002491 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002492 return false;
2493 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002494 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon49f085d2014-09-05 13:34:00 -07002495 dst->asTexture() &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002496 dst->origin() == src->origin() &&
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002497 !GrPixelConfigIsCompressed(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002498 return true;
2499 } else {
2500 return false;
2501 }
2502}
2503
robertphillips754f4e92014-09-18 13:52:08 -07002504}
2505
bsalomon@google.comeb851172013-04-15 13:51:00 +00002506// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2507// relative to is output.
egdaniel0f5f9672015-02-03 11:10:51 -08002508GrGLuint GrGLGpu::bindSurfaceAsFBO(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2509 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002510 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002511 if (NULL == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002512 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002513 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
egdaniel0f5f9672015-02-03 11:10:51 -08002514 GrGLuint* tempFBOID;
2515 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
2516
2517 if (0 == *tempFBOID) {
2518 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
2519 }
2520
mtkleinb9eb4ac2015-02-02 18:26:03 -08002521 fStats.incRenderTargetBinds();
egdaniel0f5f9672015-02-03 11:10:51 -08002522 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
robertphillips754f4e92014-09-18 13:52:08 -07002523 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2524 GR_GL_COLOR_ATTACHMENT0,
2525 GR_GL_TEXTURE_2D,
2526 texID,
2527 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002528 viewport->fLeft = 0;
2529 viewport->fBottom = 0;
2530 viewport->fWidth = surface->width();
2531 viewport->fHeight = surface->height();
egdaniel0f5f9672015-02-03 11:10:51 -08002532 return *tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002533 } else {
egdaniel0f5f9672015-02-03 11:10:51 -08002534 GrGLuint tempFBOID = 0;
mtkleinb9eb4ac2015-02-02 18:26:03 -08002535 fStats.incRenderTargetBinds();
robertphillips754f4e92014-09-18 13:52:08 -07002536 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002537 *viewport = rt->getViewport();
egdaniel0f5f9672015-02-03 11:10:51 -08002538 return tempFBOID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002539 }
egdaniel0f5f9672015-02-03 11:10:51 -08002540}
2541
2542void GrGLGpu::unbindTextureFromFBO(GrGLenum fboTarget) {
2543 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2544 GR_GL_COLOR_ATTACHMENT0,
2545 GR_GL_TEXTURE_2D,
2546 0,
2547 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002548}
2549
bsalomon861e1032014-12-16 07:33:49 -08002550bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
bsalomonf90a02b2014-11-26 12:28:00 -08002551 // In here we look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
2552 // possible and we return false to fallback to creating a render target dst for render-to-
2553 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2554 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2555
bsalomon@google.comeb851172013-04-15 13:51:00 +00002556 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002557 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002558 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002559 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2560 // then we set up for that, otherwise fail.
2561 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2562 desc->fOrigin = kDefault_GrSurfaceOrigin;
2563 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
2564 desc->fConfig = kBGRA_8888_GrPixelConfig;
2565 return true;
2566 }
2567 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002568 } else if (NULL == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002569 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2570 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002571 }
2572
2573 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002574 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002575 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2576 // fail.
2577 if (this->caps()->isConfigRenderable(src->config(), false)) {
2578 desc->fOrigin = kDefault_GrSurfaceOrigin;
2579 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
2580 desc->fConfig = src->config();
2581 return true;
2582 }
2583 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002584 }
bsalomonf90a02b2014-11-26 12:28:00 -08002585
2586 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2587 desc->fConfig = src->config();
2588 desc->fOrigin = src->origin();
2589 desc->fFlags = kNone_GrSurfaceFlags;
2590 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002591}
2592
bsalomon861e1032014-12-16 07:33:49 -08002593bool GrGLGpu::copySurface(GrSurface* dst,
joshualitta7024152014-11-03 14:16:35 -08002594 GrSurface* src,
2595 const SkIRect& srcRect,
2596 const SkIPoint& dstPoint) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002597 bool copied = false;
joshualitta7024152014-11-03 14:16:35 -08002598 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002599 GrGLuint srcFBO;
2600 GrGLIRect srcVP;
egdaniel0f5f9672015-02-03 11:10:51 -08002601 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002602 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
bsalomon49f085d2014-09-05 13:34:00 -07002603 SkASSERT(dstTex);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002604 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002605 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002606 GrGLIRect srcGLRect;
2607 srcGLRect.setRelativeTo(srcVP,
2608 srcRect.fLeft,
2609 srcRect.fTop,
2610 srcRect.width(),
2611 srcRect.height(),
2612 src->origin());
2613
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002614 this->setScratchTextureUnit();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002615 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, dstTex->textureID()));
2616 GrGLint dstY;
2617 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
2618 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
2619 } else {
2620 dstY = dstPoint.fY;
2621 }
2622 GL_CALL(CopyTexSubImage2D(GR_GL_TEXTURE_2D, 0,
2623 dstPoint.fX, dstY,
2624 srcGLRect.fLeft, srcGLRect.fBottom,
2625 srcGLRect.fWidth, srcGLRect.fHeight));
2626 copied = true;
2627 if (srcFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002628 this->unbindTextureFromFBO(GR_GL_FRAMEBUFFER);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002629 }
joshualitta7024152014-11-03 14:16:35 -08002630 } else if (can_blit_framebuffer(dst, src, this)) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002631 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2632 srcRect.width(), srcRect.height());
2633 bool selfOverlap = false;
bsalomona2c23232014-11-25 07:41:12 -08002634 if (dst == src) {
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002635 selfOverlap = SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect);
2636 }
2637
2638 if (!selfOverlap) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002639 GrGLuint dstFBO;
2640 GrGLuint srcFBO;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002641 GrGLIRect dstVP;
2642 GrGLIRect srcVP;
egdaniel0f5f9672015-02-03 11:10:51 -08002643 dstFBO = this->bindSurfaceAsFBO(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP,
2644 kDst_TempFBOTarget);
2645 srcFBO = this->bindSurfaceAsFBO(src, GR_GL_READ_FRAMEBUFFER, &srcVP,
2646 kSrc_TempFBOTarget);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002647 // We modified the bound FBO
bsalomon1c63bf62014-07-22 13:09:46 -07002648 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002649 GrGLIRect srcGLRect;
2650 GrGLIRect dstGLRect;
2651 srcGLRect.setRelativeTo(srcVP,
2652 srcRect.fLeft,
2653 srcRect.fTop,
2654 srcRect.width(),
2655 srcRect.height(),
2656 src->origin());
2657 dstGLRect.setRelativeTo(dstVP,
2658 dstRect.fLeft,
2659 dstRect.fTop,
2660 dstRect.width(),
2661 dstRect.height(),
2662 dst->origin());
2663
derekf8c8f71a2014-09-16 06:24:57 -07002664 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002665 this->disableScissor();
derekf8c8f71a2014-09-16 06:24:57 -07002666
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002667 GrGLint srcY0;
2668 GrGLint srcY1;
2669 // Does the blit need to y-mirror or not?
2670 if (src->origin() == dst->origin()) {
2671 srcY0 = srcGLRect.fBottom;
2672 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
2673 } else {
2674 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
2675 srcY1 = srcGLRect.fBottom;
2676 }
2677 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
2678 srcY0,
2679 srcGLRect.fLeft + srcGLRect.fWidth,
2680 srcY1,
2681 dstGLRect.fLeft,
2682 dstGLRect.fBottom,
2683 dstGLRect.fLeft + dstGLRect.fWidth,
2684 dstGLRect.fBottom + dstGLRect.fHeight,
2685 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
2686 if (dstFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002687 this->unbindTextureFromFBO(GR_GL_DRAW_FRAMEBUFFER);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002688 }
2689 if (srcFBO) {
egdaniel0f5f9672015-02-03 11:10:51 -08002690 this->unbindTextureFromFBO(GR_GL_READ_FRAMEBUFFER);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002691 }
2692 copied = true;
2693 }
2694 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002695 return copied;
2696}
2697
bsalomon861e1032014-12-16 07:33:49 -08002698bool GrGLGpu::canCopySurface(const GrSurface* dst,
joshualitt9853cce2014-11-17 14:22:48 -08002699 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -08002700 const SkIRect& srcRect,
2701 const SkIPoint& dstPoint) {
egdaniel0f5f9672015-02-03 11:10:51 -08002702 // This mirrors the logic in onCopySurface.
2703 if (can_copy_texsubimage(dst, src, this)) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002704 return true;
2705 }
egdaniel0f5f9672015-02-03 11:10:51 -08002706 if (can_blit_framebuffer(dst, src, this)) {
bsalomona2c23232014-11-25 07:41:12 -08002707 if (dst == src) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002708 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2709 srcRect.width(), srcRect.height());
2710 if(!SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
2711 return true;
2712 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002713 } else {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002714 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002715 }
2716 }
joshualitta7024152014-11-03 14:16:35 -08002717 return false;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002718}
2719
bsalomon861e1032014-12-16 07:33:49 -08002720void GrGLGpu::didAddGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002721 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002722 const GrTraceMarkerSet& markerArray = this->getActiveTraceMarkers();
egdanield78a1682014-07-09 10:41:26 -07002723 SkString markerString = markerArray.toStringLast();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002724 GL_CALL(PushGroupMarker(0, markerString.c_str()));
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002725 }
2726}
2727
bsalomon861e1032014-12-16 07:33:49 -08002728void GrGLGpu::didRemoveGpuTraceMarker() {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002729 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00002730 GL_CALL(PopGroupMarker());
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00002731 }
2732}
joshualitt79f8fae2014-10-28 17:59:26 -07002733
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002734///////////////////////////////////////////////////////////////////////////////
2735
bsalomon861e1032014-12-16 07:33:49 -08002736GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
2737 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00002738 const GrGLVertexBuffer* vbuffer,
2739 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07002740 SkASSERT(vbuffer);
robertphillips@google.com4f65a272013-03-26 19:40:46 +00002741 GrGLAttribArrayState* attribState;
2742
bsalomon@google.com6918d482013-03-07 19:09:11 +00002743 // We use a vertex array if we're on a core profile and the verts are in a VBO.
2744 if (gpu->glCaps().isCoreProfile() && !vbuffer->isCPUBacked()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +00002745 if (NULL == fVBOVertexArray || fVBOVertexArray->wasDestroyed()) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002746 SkSafeUnref(fVBOVertexArray);
2747 GrGLuint arrayID;
2748 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
2749 int attrCount = gpu->glCaps().maxVertexAttributes();
2750 fVBOVertexArray = SkNEW_ARGS(GrGLVertexArray, (gpu, arrayID, attrCount));
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002751 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002752 attribState = fVBOVertexArray->bindWithIndexBuffer(ibuffer);
2753 } else {
bsalomon49f085d2014-09-05 13:34:00 -07002754 if (ibuffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00002755 this->setIndexBufferIDOnDefaultVertexArray(gpu, ibuffer->bufferID());
2756 } else {
2757 this->setVertexArrayID(gpu, 0);
2758 }
2759 int attrCount = gpu->glCaps().maxVertexAttributes();
2760 if (fDefaultVertexArrayAttribState.count() != attrCount) {
2761 fDefaultVertexArrayAttribState.resize(attrCount);
2762 }
2763 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002764 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00002765 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00002766}