blob: bf0b297c8cf064620b4ac55586c9672ff969f75c [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"
jvanverthcba99b82015-06-24 06:59:57 -070010#include "GrGLGLSL.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070011#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080012#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080014#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080015#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000018#include "GrTypes.h"
bsalomoncb8979d2015-05-05 09:51:38 -070019#include "GrVertices.h"
bsalomon6df86402015-06-01 10:41:49 -070020#include "builders/GrGLShaderStringBuilder.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070021#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070022#include "glsl/GrGLSLCaps.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000023#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000024#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000027#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029#define SKIP_CACHE_CHECK true
30
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000031#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
32 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
33 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
34 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000036 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
37 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
38 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
39#endif
40
ethannicholas5366a092016-01-22 09:45:47 -080041
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000042///////////////////////////////////////////////////////////////////////////////
43
cdaltonb85a0aa2014-07-21 15:32:44 -070044
cdalton8917d622015-05-06 13:40:21 -070045static const GrGLenum gXfermodeEquation2Blend[] = {
46 // Basic OpenGL blend equations.
47 GR_GL_FUNC_ADD,
48 GR_GL_FUNC_SUBTRACT,
49 GR_GL_FUNC_REVERSE_SUBTRACT,
50
51 // GL_KHR_blend_equation_advanced.
52 GR_GL_SCREEN,
53 GR_GL_OVERLAY,
54 GR_GL_DARKEN,
55 GR_GL_LIGHTEN,
56 GR_GL_COLORDODGE,
57 GR_GL_COLORBURN,
58 GR_GL_HARDLIGHT,
59 GR_GL_SOFTLIGHT,
60 GR_GL_DIFFERENCE,
61 GR_GL_EXCLUSION,
62 GR_GL_MULTIPLY,
63 GR_GL_HSL_HUE,
64 GR_GL_HSL_SATURATION,
65 GR_GL_HSL_COLOR,
66 GR_GL_HSL_LUMINOSITY
67};
68GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
69GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
70GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
71GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
72GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
73GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
74GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
75GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
76GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
77GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
78GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
79GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
80GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
81GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
82GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
83GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
84GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
85GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070086GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070087
twiz@google.com0f31ca72011-03-18 17:38:11 +000088static const GrGLenum gXfermodeCoeff2Blend[] = {
89 GR_GL_ZERO,
90 GR_GL_ONE,
91 GR_GL_SRC_COLOR,
92 GR_GL_ONE_MINUS_SRC_COLOR,
93 GR_GL_DST_COLOR,
94 GR_GL_ONE_MINUS_DST_COLOR,
95 GR_GL_SRC_ALPHA,
96 GR_GL_ONE_MINUS_SRC_ALPHA,
97 GR_GL_DST_ALPHA,
98 GR_GL_ONE_MINUS_DST_ALPHA,
99 GR_GL_CONSTANT_COLOR,
100 GR_GL_ONE_MINUS_CONSTANT_COLOR,
101 GR_GL_CONSTANT_ALPHA,
102 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000103
104 // extended blend coeffs
105 GR_GL_SRC1_COLOR,
106 GR_GL_ONE_MINUS_SRC1_COLOR,
107 GR_GL_SRC1_ALPHA,
108 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000109};
110
bsalomon861e1032014-12-16 07:33:49 -0800111bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000112 static const bool gCoeffReferencesBlendConst[] = {
113 false,
114 false,
115 false,
116 false,
117 false,
118 false,
119 false,
120 false,
121 false,
122 false,
123 true,
124 true,
125 true,
126 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000127
128 // extended blend coeffs
129 false,
130 false,
131 false,
132 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 };
134 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700135 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000136
bsalomon@google.com47059542012-06-06 20:51:20 +0000137 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
138 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
139 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
140 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
141 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
142 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
143 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
144 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
145 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
146 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
147 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
148 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
149 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
150 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151
bsalomon@google.com47059542012-06-06 20:51:20 +0000152 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
153 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
154 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
155 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000156
157 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700158 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000159}
160
reed@google.comac10a2d2010-12-22 21:39:39 +0000161///////////////////////////////////////////////////////////////////////////////
162
egdanielff1d5472015-09-10 08:37:20 -0700163
bsalomon682c2692015-05-22 14:01:46 -0700164GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
165 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700166 SkAutoTUnref<const GrGLInterface> glInterface(
167 reinterpret_cast<const GrGLInterface*>(backendContext));
168 if (!glInterface) {
169 glInterface.reset(GrGLDefaultInterface());
170 } else {
171 glInterface->ref();
172 }
173 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700174 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700175 }
bsalomon682c2692015-05-22 14:01:46 -0700176 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700177 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700178 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700179 }
halcanary96fcdcc2015-08-27 07:41:13 -0700180 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700181}
182
rileya@google.come38160c2012-07-03 18:03:04 +0000183static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000184
bsalomon424cc262015-05-22 10:37:30 -0700185GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000186 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000187 , fGLContext(ctx) {
bsalomon424cc262015-05-22 10:37:30 -0700188 SkASSERT(ctx);
189 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000190
bsalomon1c63bf62014-07-22 13:09:46 -0700191 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000192
bsalomon424cc262015-05-22 10:37:30 -0700193 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000194 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000195 const GrGLubyte* vendor;
196 const GrGLubyte* renderer;
197 const GrGLubyte* version;
198 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
199 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
200 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800201 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000202 this);
tfarina38406c82014-10-31 07:11:12 -0700203 SkDebugf("------ VENDOR %s\n", vendor);
204 SkDebugf("------ RENDERER %s\n", renderer);
205 SkDebugf("------ VERSION %s\n", version);
206 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700207 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700208 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800209 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000210 }
211
halcanary385fe4d2015-08-26 13:07:48 -0700212 fProgramCache = new ProgramCache(this);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000213
joshualitt2dd1ae02014-12-03 06:24:10 -0800214 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000215
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000216 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700217 fTempSrcFBOID = 0;
218 fTempDstFBOID = 0;
219 fStencilClearFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700220
jvanverthe9c0fc62015-04-29 11:18:05 -0700221 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700222 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700223 }
bsalomon7ea33f52015-11-22 14:51:00 -0800224 this->createCopyPrograms();
bsalomon6dea83f2015-12-03 12:58:06 -0800225 fWireRectProgram.fProgram = 0;
226 fWireRectArrayBuffer = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000227}
228
bsalomon861e1032014-12-16 07:33:49 -0800229GrGLGpu::~GrGLGpu() {
kkinnunen702501d2016-01-13 23:36:45 -0800230 // Delete the path rendering explicitly, since it will need working gpu object to release the
231 // resources the object itself holds.
232 fPathRendering.reset();
233
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000234 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000235 // detach the current program so there is no confusion on OpenGL's part
236 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000237 GL_CALL(UseProgram(0));
238 }
239
egdanield803f272015-03-18 13:01:52 -0700240 if (0 != fTempSrcFBOID) {
241 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800242 }
egdanield803f272015-03-18 13:01:52 -0700243 if (0 != fTempDstFBOID) {
244 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800245 }
egdanield803f272015-03-18 13:01:52 -0700246 if (0 != fStencilClearFBOID) {
247 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800248 }
egdaniel0f5f9672015-02-03 11:10:51 -0800249
bsalomon7ea33f52015-11-22 14:51:00 -0800250 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
251 if (0 != fCopyPrograms[i].fProgram) {
252 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
253 }
bsalomon6df86402015-06-01 10:41:49 -0700254 }
bsalomon6dea83f2015-12-03 12:58:06 -0800255
bsalomon7ea33f52015-11-22 14:51:00 -0800256 if (0 != fCopyProgramArrayBuffer) {
257 GL_CALL(DeleteBuffers(1, &fCopyProgramArrayBuffer));
bsalomon6df86402015-06-01 10:41:49 -0700258 }
259
bsalomon6dea83f2015-12-03 12:58:06 -0800260 if (0 != fWireRectProgram.fProgram) {
261 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
262 }
263
264 if (0 != fWireRectArrayBuffer) {
265 GL_CALL(DeleteBuffers(1, &fWireRectArrayBuffer));
266 }
267
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000268 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700269}
270
bsalomon861e1032014-12-16 07:33:49 -0800271void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700272 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700273 fProgramCache->abandon();
274 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700275 fTempSrcFBOID = 0;
276 fTempDstFBOID = 0;
277 fStencilClearFBOID = 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800278 fCopyProgramArrayBuffer = 0;
279 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
280 fCopyPrograms[i].fProgram = 0;
281 }
bsalomon6dea83f2015-12-03 12:58:06 -0800282 fWireRectProgram.fProgram = 0;
283 fWireRectArrayBuffer = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700284 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700285 this->glPathRendering()->abandonGpuResources();
286 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000287}
288
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000289///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000290
bsalomon861e1032014-12-16 07:33:49 -0800291void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000292 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000293 if (resetBits & kMisc_GrGLBackendState) {
294 GL_CALL(Disable(GR_GL_DEPTH_TEST));
295 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000296
egdaniel8dd688b2015-01-22 10:16:09 -0800297 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000298
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000299 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000300 // Desktop-only state that we never change
301 if (!this->glCaps().isCoreProfile()) {
302 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
303 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
304 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
305 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
306 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
307 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
308 }
309 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
310 // core profile. This seems like a bug since the core spec removes any mention of
311 // GL_ARB_imaging.
312 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
313 GL_CALL(Disable(GR_GL_COLOR_TABLE));
314 }
315 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
316 // Since ES doesn't support glPointSize at all we always use the VS to
317 // set the point size
318 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
319
320 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
321 // currently part of our gl interface. There are probably others as
322 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000323 }
joshualitt58162332014-08-01 06:44:53 -0700324
325 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700326 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700327 // The arm extension requires specifically enabling MSAA fetching per sample.
328 // On some devices this may have a perf hit. Also multiple render targets are disabled
329 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
330 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000331 fHWWriteToColor = kUnknown_TriState;
332 // we only ever use lines in hairline mode
333 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700334 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000335 }
edisonn@google.comba669992013-06-28 16:03:21 +0000336
egdanielb414f252014-07-29 13:15:47 -0700337 if (resetBits & kMSAAEnable_GrGLBackendState) {
338 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700339
340 // In mixed samples mode coverage modulation allows the coverage to be converted to
341 // "opacity", which can then be blended into the color buffer to accomplish antialiasing.
342 // Enable coverage modulation suitable for premultiplied alpha colors.
343 // This state has no effect when not rendering to a mixed sampled target.
egdanieleed519e2016-01-15 11:36:18 -0800344 if (this->caps()->usesMixedSamples()) {
vbuzinovdded6962015-06-12 08:59:45 -0700345 GL_CALL(CoverageModulation(GR_GL_RGBA));
346 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000347 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000349 fHWActiveTextureUnitIdx = -1; // invalid
350
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000351 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700352 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
353 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000354 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000355 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000356
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000357 if (resetBits & kBlend_GrGLBackendState) {
358 fHWBlendState.invalidate();
359 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000360
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000361 if (resetBits & kView_GrGLBackendState) {
362 fHWScissorSettings.invalidate();
363 fHWViewport.invalidate();
364 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000365
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000366 if (resetBits & kStencil_GrGLBackendState) {
367 fHWStencilSettings.invalidate();
368 fHWStencilTestEnabled = kUnknown_TriState;
369 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000370
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000371 // Vertex
372 if (resetBits & kVertex_GrGLBackendState) {
373 fHWGeometryState.invalidate();
374 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000375
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000376 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700377 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700378 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000379 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000380
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000381 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700382 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700383 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000384 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000385 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000386
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000387 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000388 if (resetBits & kPixelStore_GrGLBackendState) {
389 if (this->glCaps().unpackRowLengthSupport()) {
390 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
391 }
392 if (this->glCaps().packRowLengthSupport()) {
393 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
394 }
395 if (this->glCaps().unpackFlipYSupport()) {
396 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
397 }
398 if (this->glCaps().packFlipYSupport()) {
399 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
400 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000401 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000402
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000403 if (resetBits & kProgram_GrGLBackendState) {
404 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000405 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000406}
407
egdanielcf614fd2015-04-22 13:58:58 -0700408static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000409 // By default, GrRenderTargets are GL's normal orientation so that they
410 // can be drawn to by the outside world without the client having
411 // to render upside down.
412 if (kDefault_GrSurfaceOrigin == origin) {
413 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
414 } else {
415 return origin;
416 }
417}
418
bsalomon6dc6f5f2015-06-18 09:12:16 -0700419GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
420 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800421#ifdef SK_IGNORE_GL_TEXTURE_TARGET
422 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700423 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000424 }
bsalomon091f60c2015-11-10 11:54:56 -0800425#else
426 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
427 if (!info || !info->fID) {
428 return nullptr;
429 }
430#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000431
bsalomon@google.combcce8922013-03-25 15:38:39 +0000432 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000433 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700434 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000435 }
436
bsalomon7ea33f52015-11-22 14:51:00 -0800437 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
438 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
439
bsalomonb15b4c12014-10-29 12:41:57 -0700440 GrGLTexture::IDDesc idDesc;
441 GrSurfaceDesc surfDesc;
442
bsalomon091f60c2015-11-10 11:54:56 -0800443#ifdef SK_IGNORE_GL_TEXTURE_TARGET
444 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon10528f12015-10-14 12:54:52 -0700445 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -0800446 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
447#else
448 idDesc.fInfo = *info;
449#endif
bsalomone5286e02016-01-14 09:24:09 -0800450
bsalomon7ea33f52015-11-22 14:51:00 -0800451 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
452 if (renderTarget) {
453 // This combination is not supported.
454 return nullptr;
455 }
456 if (!this->glCaps().externalTextureSupport()) {
457 return nullptr;
458 }
bsalomone5286e02016-01-14 09:24:09 -0800459 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
460 if (!this->glCaps().rectangleTextureSupport()) {
461 return nullptr;
462 }
463 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
464 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800465 }
bsalomone5286e02016-01-14 09:24:09 -0800466
467 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800468 // for a render buffer that resolves to the texture. We don't support MSAA textures.
469 if (desc.fSampleCnt && !renderTarget) {
470 return nullptr;
471 }
bsalomon10528f12015-10-14 12:54:52 -0700472
bsalomon6dc6f5f2015-06-18 09:12:16 -0700473 switch (ownership) {
474 case kAdopt_GrWrapOwnership:
475 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
476 break;
477 case kBorrow_GrWrapOwnership:
478 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
479 break;
bsalomone5286e02016-01-14 09:24:09 -0800480 }
bsalomonb15b4c12014-10-29 12:41:57 -0700481
bsalomonb15b4c12014-10-29 12:41:57 -0700482 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
483 surfDesc.fWidth = desc.fWidth;
484 surfDesc.fHeight = desc.fHeight;
485 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700486 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000487 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
488 // assuming the old behaviour, which is that backend textures are always
489 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
490 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
491 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700492 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000493 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700494 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000495 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000496
halcanary96fcdcc2015-08-27 07:41:13 -0700497 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000498 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700499 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700500 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800501 idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700502 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000503 }
halcanary385fe4d2015-08-26 13:07:48 -0700504 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000505 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700506 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000507 }
halcanary96fcdcc2015-08-27 07:41:13 -0700508 if (nullptr == texture) {
509 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000510 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000511
bsalomon@google.come269f212011-11-07 13:29:52 +0000512 return texture;
513}
514
bsalomon6dc6f5f2015-06-18 09:12:16 -0700515GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
516 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700517 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700518 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700519 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700520 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700521 switch (ownership) {
522 case kAdopt_GrWrapOwnership:
523 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
524 break;
525 case kBorrow_GrWrapOwnership:
526 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
527 break;
528 }
senorblancod7395d82015-06-18 13:26:52 -0700529 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000530
bsalomonb15b4c12014-10-29 12:41:57 -0700531 GrSurfaceDesc desc;
532 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700533 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700534 desc.fWidth = wrapDesc.fWidth;
535 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700536 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700537 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
538
egdanielec00d942015-09-14 12:56:10 -0700539 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000540}
541
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000542////////////////////////////////////////////////////////////////////////////////
bsalomonf0674512015-07-28 13:26:15 -0700543bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
544 size_t rowBytes, GrPixelConfig srcConfig,
545 DrawPreference* drawPreference,
546 WritePixelTempDrawInfo* tempDrawInfo) {
547 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
548 return false;
549 }
550
bsalomon6cb3cbe2015-07-30 07:34:27 -0700551 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
552 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
553 if (!dstSurface->asTexture()) {
554 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800555 } else {
556 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800557 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
558 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800559 return false;
560 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700561 }
562
bsalomon16921ec2015-07-30 15:34:56 -0700563 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
564 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
565 }
566
bsalomon6c9cd552016-01-22 07:17:34 -0800567 // Start off assuming no swizzling
568 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
569 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700570
571 // These settings we will always want if a temp draw is performed. Initially set the config
572 // to srcConfig, though that may be modified if we decide to do a R/G swap.
573 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
574 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
575 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
576 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
577 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
578 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
579
580 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
581
582 if (configsAreRBSwaps) {
583 if (!this->caps()->isConfigTexturable(srcConfig)) {
584 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
585 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800586 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
587 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700588 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
589 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700590 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
591 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800592 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
593 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700594 } else if (kGLES_GrGLStandard == this->glStandard() &&
595 this->glCaps().bgraIsInternalFormat()) {
596 // The internal format and external formats must match texture uploads so we can't
597 // swizzle while uploading when BGRA is a distinct internal format.
598 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
599 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800600 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
601 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700602 }
603 }
604
605 if (!this->glCaps().unpackFlipYSupport() &&
606 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
607 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
608 }
609
610 return true;
611}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000612
jvanverth17aa0472016-01-05 10:41:27 -0800613static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
614 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700615 if (!glTex) {
616 return false;
617 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000618
bsalomon16921ec2015-07-30 15:34:56 -0700619 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
620 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
621 return false;
622 }
623
bsalomone5286e02016-01-14 09:24:09 -0800624 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
625 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800626 return false;
627 }
628
jvanverth17aa0472016-01-05 10:41:27 -0800629 return true;
630}
631
632bool GrGLGpu::onWritePixels(GrSurface* surface,
633 int left, int top, int width, int height,
634 GrPixelConfig config, const void* buffer,
635 size_t rowBytes) {
636 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
637
638 if (!check_write_and_transfer_input(glTex, surface, config)) {
639 return false;
640 }
641
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000642 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700643 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000644
krajcevski145d48c2014-06-11 16:07:50 -0700645 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700646 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800647 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700648 SkASSERT(config == glTex->desc().fConfig);
jvanverth17aa0472016-01-05 10:41:27 -0800649 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), buffer,
650 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700651 } else {
jvanverth17aa0472016-01-05 10:41:27 -0800652 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
653 left, top, width, height, config, buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700654 }
655
656 if (success) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700657 glTex->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000658 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000659 }
krajcevski145d48c2014-06-11 16:07:50 -0700660
661 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000662}
663
jvanverth17aa0472016-01-05 10:41:27 -0800664bool GrGLGpu::onTransferPixels(GrSurface* surface,
665 int left, int top, int width, int height,
666 GrPixelConfig config, GrTransferBuffer* buffer,
667 size_t offset, size_t rowBytes) {
668 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
669
670 if (!check_write_and_transfer_input(glTex, surface, config)) {
671 return false;
672 }
673
674 // For the moment, can't transfer compressed data
675 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
676 return false;
677 }
678
679 this->setScratchTextureUnit();
680 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
681
682 SkASSERT(!buffer->isMapped());
683 GrGLTransferBuffer* glBuffer = reinterpret_cast<GrGLTransferBuffer*>(buffer);
684 // bind the transfer buffer
685 SkASSERT(GR_GL_PIXEL_UNPACK_BUFFER == glBuffer->bufferType() ||
686 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM == glBuffer->bufferType());
687 GL_CALL(BindBuffer(glBuffer->bufferType(), glBuffer->bufferID()));
688
689 bool success = false;
690 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
691 left, top, width, height, config, buffer, rowBytes);
692
693 if (success) {
694 glTex->texturePriv().dirtyMipMaps(true);
695 return true;
696 }
697
698 return false;
699}
700
bsalomonf46a1242015-12-15 12:37:38 -0800701// For GL_[UN]PACK_ALIGNMENT.
702static inline GrGLint config_alignment(GrPixelConfig config) {
703 SkASSERT(!GrPixelConfigIsCompressed(config));
704 switch (config) {
705 case kAlpha_8_GrPixelConfig:
706 return 1;
707 case kRGB_565_GrPixelConfig:
708 case kRGBA_4444_GrPixelConfig:
709 case kAlpha_half_GrPixelConfig:
710 case kRGBA_half_GrPixelConfig:
711 return 2;
712 case kRGBA_8888_GrPixelConfig:
713 case kBGRA_8888_GrPixelConfig:
714 case kSRGBA_8888_GrPixelConfig:
715 case kRGBA_float_GrPixelConfig:
716 return 4;
717 default:
718 return 0;
719 }
720}
721
bsalomonb15b4c12014-10-29 12:41:57 -0700722static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
723 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700724 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000725 return GR_GL_GET_ERROR(interface);
726 } else {
727 return CHECK_ALLOC_ERROR(interface);
728 }
729}
730
bsalomon861e1032014-12-16 07:33:49 -0800731bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700732 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800733 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000734 int left, int top, int width, int height,
735 GrPixelConfig dataConfig,
jvanverth17aa0472016-01-05 10:41:27 -0800736 const void* dataOrOffset,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000737 size_t rowBytes) {
jvanverth17aa0472016-01-05 10:41:27 -0800738 SkASSERT(dataOrOffset || kNewTexture_UploadType == uploadType ||
739 kTransfer_UploadType == uploadType);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000740
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000741 // If we're uploading compressed data then we should be using uploadCompressedTexData
742 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
743
bsalomon5b30c6f2015-12-17 14:17:34 -0800744 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
745
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000746 size_t bpp = GrBytesPerPixel(dataConfig);
bsalomone8d21e82015-07-16 08:23:13 -0700747 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
jvanverth17aa0472016-01-05 10:41:27 -0800748 &width, &height, &dataOrOffset, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000749 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000750 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000751 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000752
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000753 // in case we need a temporary, trimmed copy of the src pixels
benjaminwagner7d974f52015-10-19 13:55:55 -0700754#if defined(GOOGLE3)
755 // Stack frame size is limited in GOOGLE3.
756 SkAutoSMalloc<64 * 128> tempStorage;
757#else
joshualitt29f86792015-05-29 08:06:48 -0700758 SkAutoSMalloc<128 * 128> tempStorage;
benjaminwagner7d974f52015-10-19 13:55:55 -0700759#endif
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000760
bsalomon5b30c6f2015-12-17 14:17:34 -0800761 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -0800762 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -0800763 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800764 GrGLenum externalFormat;
765 GrGLenum externalType;
766 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
767 &externalFormat, &externalType)) {
768 return false;
769 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000770 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800771 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000772 * because our srcData has extra bytes past each row. If so, we need
773 * to trim those off here, since GL ES may not let us specify
774 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000775 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000776 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000777 bool swFlipY = false;
778 bool glFlipY = false;
jvanverth17aa0472016-01-05 10:41:27 -0800779 if (dataOrOffset) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000780 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000781 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000782 glFlipY = true;
783 } else {
784 swFlipY = true;
785 }
786 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000787 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000788 // can't use this for flipping, only non-neg values allowed. :(
789 if (rowBytes != trimRowBytes) {
790 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
791 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
792 restoreGLRowLength = true;
793 }
jvanverth17aa0472016-01-05 10:41:27 -0800794 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000795 if (trimRowBytes != rowBytes || swFlipY) {
796 // copy data into our new storage, skipping the trailing bytes
797 size_t trimSize = height * trimRowBytes;
jvanverth17aa0472016-01-05 10:41:27 -0800798 const char* src = (const char*)dataOrOffset;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000799 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000800 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000801 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000802 char* dst = (char*)tempStorage.reset(trimSize);
803 for (int y = 0; y < height; y++) {
804 memcpy(dst, src, trimRowBytes);
805 if (swFlipY) {
806 src -= rowBytes;
807 } else {
808 src += rowBytes;
809 }
810 dst += trimRowBytes;
811 }
812 // now point data to our copied version
jvanverth17aa0472016-01-05 10:41:27 -0800813 dataOrOffset = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000814 }
jvanverth17aa0472016-01-05 10:41:27 -0800815 } else {
816 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000817 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000818 if (glFlipY) {
819 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
820 }
bsalomonf46a1242015-12-15 12:37:38 -0800821 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(dataConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000822 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000823 bool succeeded = true;
jvanverth17aa0472016-01-05 10:41:27 -0800824 if (kNewTexture_UploadType == uploadType) {
825 if (dataOrOffset &&
826 !(0 == left && 0 == top && desc.fWidth == width && desc.fHeight == height)) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000827 succeeded = false;
828 } else {
bsalomon102b07e2015-12-17 15:37:33 -0800829 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
830 GL_ALLOC_CALL(this->glInterface(), TexImage2D(target, 0, internalFormat, desc.fWidth,
bsalomon76148af2016-01-12 11:13:47 -0800831 desc.fHeight, 0, externalFormat,
jvanverth17aa0472016-01-05 10:41:27 -0800832 externalType, dataOrOffset));
bsalomon102b07e2015-12-17 15:37:33 -0800833 GrGLenum error = check_alloc_error(desc, this->glInterface());
834 if (error != GR_GL_NO_ERROR) {
835 succeeded = false;
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000836 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000837 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000838 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000839 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000840 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000841 }
bsalomon10528f12015-10-14 12:54:52 -0700842 GL_CALL(TexSubImage2D(target,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000843 0, // level
844 left, top,
845 width, height,
jvanverth17aa0472016-01-05 10:41:27 -0800846 externalFormat, externalType, dataOrOffset));
bsalomon@google.com6f379512011-11-16 20:36:03 +0000847 }
848
849 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000850 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000851 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000852 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000853 if (glFlipY) {
854 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
855 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000856 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000857}
858
krajcevski145d48c2014-06-11 16:07:50 -0700859// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700860// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700861// create a CompressedTexData struct that takes a desc/ptr and figures out
862// the proper upload semantics. Then users can construct this function how they
863// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800864bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700865 GrGLenum target,
krajcevski145d48c2014-06-11 16:07:50 -0700866 const void* data,
jvanverth17aa0472016-01-05 10:41:27 -0800867 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700868 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -0800869 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
jvanverth17aa0472016-01-05 10:41:27 -0800870 SkASSERT(kTransfer_UploadType != uploadType &&
871 (data || kNewTexture_UploadType != uploadType));
krajcevski9c0e6292014-06-02 07:38:14 -0700872
873 // No support for software flip y, yet...
874 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
875
krajcevski145d48c2014-06-11 16:07:50 -0700876 if (-1 == width) {
877 width = desc.fWidth;
878 }
879#ifdef SK_DEBUG
880 else {
881 SkASSERT(width <= desc.fWidth);
882 }
883#endif
884
885 if (-1 == height) {
886 height = desc.fHeight;
887 }
888#ifdef SK_DEBUG
889 else {
890 SkASSERT(height <= desc.fHeight);
891 }
892#endif
893
krajcevski9c0e6292014-06-02 07:38:14 -0700894 // Make sure that the width and height that we pass to OpenGL
895 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -0800896 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -0700897
bsalomon76148af2016-01-12 11:13:47 -0800898 // We only need the internal format for compressed 2D textures.
899 GrGLenum internalFormat;
900 if (!this->glCaps().getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
901 return false;
902 }
krajcevski9c0e6292014-06-02 07:38:14 -0700903
jvanverth17aa0472016-01-05 10:41:27 -0800904 if (kNewTexture_UploadType == uploadType) {
bsalomond4cb9222014-08-11 14:19:09 -0700905 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -0700906 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -0700907 CompressedTexImage2D(target,
krajcevski145d48c2014-06-11 16:07:50 -0700908 0, // level
909 internalFormat,
910 width, height,
911 0, // border
bsalomon98806072014-12-12 15:11:17 -0800912 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -0700913 data));
bsalomond4cb9222014-08-11 14:19:09 -0700914 GrGLenum error = check_alloc_error(desc, this->glInterface());
915 if (error != GR_GL_NO_ERROR) {
916 return false;
917 }
krajcevski145d48c2014-06-11 16:07:50 -0700918 } else {
bsalomond4cb9222014-08-11 14:19:09 -0700919 // Paletted textures can't be updated.
920 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
921 return false;
922 }
bsalomon10528f12015-10-14 12:54:52 -0700923 GL_CALL(CompressedTexSubImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -0700924 0, // level
925 left, top,
926 width, height,
927 internalFormat,
bsalomon98806072014-12-12 15:11:17 -0800928 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -0700929 data));
krajcevski145d48c2014-06-11 16:07:50 -0700930 }
krajcevski9c0e6292014-06-02 07:38:14 -0700931
bsalomond4cb9222014-08-11 14:19:09 -0700932 return true;
krajcevski9c0e6292014-06-02 07:38:14 -0700933}
934
bsalomon424cc262015-05-22 10:37:30 -0700935static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000936 int sampleCount,
937 GrGLenum format,
938 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +0000939 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000940 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +0000941 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000942 case GrGLCaps::kDesktop_ARB_MSFBOType:
943 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -0700944 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000945 case GrGLCaps::kES_3_0_MSFBOType:
946 GL_ALLOC_CALL(ctx.interface(),
947 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
948 sampleCount,
949 format,
950 width, height));
951 break;
952 case GrGLCaps::kES_Apple_MSFBOType:
953 GL_ALLOC_CALL(ctx.interface(),
954 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
955 sampleCount,
956 format,
957 width, height));
958 break;
959 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
960 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
961 GL_ALLOC_CALL(ctx.interface(),
962 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
963 sampleCount,
964 format,
965 width, height));
966 break;
967 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000968 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +0000969 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000970 }
Brian Salomon9251d4e2015-03-17 16:03:19 -0400971 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +0000972}
973
egdanielb0e1be22015-04-22 13:27:39 -0700974bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
975 GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800976 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -0700977 GrGLRenderTarget::IDDesc* idDesc) {
978 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700979 idDesc->fRTFBOID = 0;
980 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700981 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -0700982 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
983 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
984 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000985
986 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000987
bsalomona11e5fc2015-12-18 07:59:41 -0800988 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +0000989
bsalomonb15b4c12014-10-29 12:41:57 -0700990 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +0000991 goto FAILED;
992 }
993
egdanield803f272015-03-18 13:01:52 -0700994 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
995 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000996 goto FAILED;
997 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000998
bsalomon@google.comf3a60c02013-03-19 19:06:09 +0000999 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1000 // the texture bound to the other. The exception is the IMG multisample extension. With this
1001 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1002 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001003 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001004 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001005 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001006 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001007 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001008 goto FAILED;
1009 }
bsalomon76148af2016-01-12 11:13:47 -08001010 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1011 return false;
1012 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001013 } else {
egdanield803f272015-03-18 13:01:52 -07001014 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001015 }
1016
egdanield803f272015-03-18 13:01:52 -07001017 // below here we may bind the FBO
1018 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1019 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001020 SkASSERT(desc.fSampleCnt > 0);
1021 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001022 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001023 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001024 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001025 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001026 goto FAILED;
1027 }
egdanield803f272015-03-18 13:01:52 -07001028 fStats.incRenderTargetBinds();
1029 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1030 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001031 GR_GL_COLOR_ATTACHMENT0,
1032 GR_GL_RENDERBUFFER,
1033 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001034 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1035 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001036 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1037 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1038 goto FAILED;
1039 }
bsalomon424cc262015-05-22 10:37:30 -07001040 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001041 }
1042 }
egdanield803f272015-03-18 13:01:52 -07001043 fStats.incRenderTargetBinds();
1044 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001045
bsalomonb15b4c12014-10-29 12:41:57 -07001046 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001047 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001048 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001049 texInfo.fTarget,
1050 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001051 } else {
egdanield803f272015-03-18 13:01:52 -07001052 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001053 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001054 texInfo.fTarget,
1055 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001056 }
bsalomonb15b4c12014-10-29 12:41:57 -07001057 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1058 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001059 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001060 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1061 goto FAILED;
1062 }
bsalomon424cc262015-05-22 10:37:30 -07001063 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001064 }
1065
1066 return true;
1067
1068FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001069 if (idDesc->fMSColorRenderbufferID) {
1070 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001071 }
egdanield803f272015-03-18 13:01:52 -07001072 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1073 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001074 }
egdanield803f272015-03-18 13:01:52 -07001075 if (idDesc->fTexFBOID) {
1076 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001077 }
1078 return false;
1079}
1080
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001081// good to set a break-point here to know when createTexture fails
1082static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001083// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001084 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001085}
1086
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001087#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001088static size_t as_size_t(int x) {
1089 return x;
1090}
1091#endif
1092
egdanielb0e1be22015-04-22 13:27:39 -07001093GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1094 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001095 const void* srcData, size_t rowBytes) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001096 // We fail if the MSAA was requested and is not available.
1097 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001098 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001099 return return_null_texture();
1100 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001101
bsalomonf2703d82014-10-28 14:33:06 -07001102 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001103
bsalomonb15b4c12014-10-29 12:41:57 -07001104 GrGLTexture::IDDesc idDesc;
kkinnunen546eb5c2015-12-11 00:05:33 -08001105 idDesc.fInfo.fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08001106 GL_CALL(GenTextures(1, &idDesc.fInfo.fID));
egdanielb0e1be22015-04-22 13:27:39 -07001107 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001108 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -08001109 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001110
bsalomon091f60c2015-11-10 11:54:56 -08001111 if (!idDesc.fInfo.fID) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001112 return return_null_texture();
1113 }
1114
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00001115 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001116 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
bsalomon@google.come269f212011-11-07 13:29:52 +00001117
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001118 if (renderTarget && this->glCaps().textureUsageSupport()) {
1119 // provides a hint about how this texture will be used
bsalomon091f60c2015-11-10 11:54:56 -08001120 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
junov@chromium.org8b6b1c92013-08-29 16:22:09 +00001121 GR_GL_TEXTURE_USAGE,
1122 GR_GL_FRAMEBUFFER_ATTACHMENT));
1123 }
1124
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001125 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1126 // drivers have a bug where an FBO won't be complete if it includes a
1127 // texture that is not mipmap complete (considering the filter in use).
1128 GrGLTexture::TexParams initialTexParams;
1129 // we only set a subset here so invalidate first
1130 initialTexParams.invalidate();
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001131 initialTexParams.fMinFilter = GR_GL_NEAREST;
1132 initialTexParams.fMagFilter = GR_GL_NEAREST;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001133 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1134 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon091f60c2015-11-10 11:54:56 -08001135 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001136 GR_GL_TEXTURE_MAG_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001137 initialTexParams.fMagFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001138 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GR_GL_TEXTURE_MIN_FILTER,
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00001140 initialTexParams.fMinFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001141 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001142 GR_GL_TEXTURE_WRAP_S,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001143 initialTexParams.fWrapS));
bsalomon091f60c2015-11-10 11:54:56 -08001144 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GR_GL_TEXTURE_WRAP_T,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001146 initialTexParams.fWrapT));
jvanverth17aa0472016-01-05 10:41:27 -08001147 if (!this->uploadTexData(desc, idDesc.fInfo.fTarget, kNewTexture_UploadType, 0, 0,
bsalomonb15b4c12014-10-29 12:41:57 -07001148 desc.fWidth, desc.fHeight,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001149 desc.fConfig, srcData, rowBytes)) {
bsalomon091f60c2015-11-10 11:54:56 -08001150 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001151 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001152 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001153
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001156 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001157 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001158 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001159
bsalomon091f60c2015-11-10 11:54:56 -08001160 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) {
1161 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001162 return return_null_texture();
1163 }
halcanary385fe4d2015-08-26 13:07:48 -07001164 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 } else {
halcanary385fe4d2015-08-26 13:07:48 -07001166 tex = new GrGLTexture(this, desc, idDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001168 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001169#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001170 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001171 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001172#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173 return tex;
1174}
1175
egdanielb0e1be22015-04-22 13:27:39 -07001176GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1177 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001178 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001179 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001180 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001181 return return_null_texture();
1182 }
1183
bsalomonb15b4c12014-10-29 12:41:57 -07001184 GrGLTexture::IDDesc idDesc;
kkinnunen546eb5c2015-12-11 00:05:33 -08001185 idDesc.fInfo.fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08001186 GL_CALL(GenTextures(1, &idDesc.fInfo.fID));
egdanielb0e1be22015-04-22 13:27:39 -07001187 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001188 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -08001189 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
krajcevski9c0e6292014-06-02 07:38:14 -07001190
bsalomon091f60c2015-11-10 11:54:56 -08001191 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001192 return return_null_texture();
1193 }
1194
1195 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001196 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001197
1198 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1199 // drivers have a bug where an FBO won't be complete if it includes a
1200 // texture that is not mipmap complete (considering the filter in use).
1201 GrGLTexture::TexParams initialTexParams;
1202 // we only set a subset here so invalidate first
1203 initialTexParams.invalidate();
1204 initialTexParams.fMinFilter = GR_GL_NEAREST;
1205 initialTexParams.fMagFilter = GR_GL_NEAREST;
1206 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1207 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon091f60c2015-11-10 11:54:56 -08001208 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001209 GR_GL_TEXTURE_MAG_FILTER,
1210 initialTexParams.fMagFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001211 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001212 GR_GL_TEXTURE_MIN_FILTER,
1213 initialTexParams.fMinFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001214 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001215 GR_GL_TEXTURE_WRAP_S,
1216 initialTexParams.fWrapS));
bsalomon091f60c2015-11-10 11:54:56 -08001217 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001218 GR_GL_TEXTURE_WRAP_T,
1219 initialTexParams.fWrapT));
1220
bsalomon091f60c2015-11-10 11:54:56 -08001221 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, srcData)) {
1222 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001223 return return_null_texture();
1224 }
1225
1226 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001227 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001228 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1229#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001230 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001231 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1232#endif
1233 return tex;
1234}
1235
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001237
egdaniel8dc7c3a2015-04-16 11:22:42 -07001238const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001239
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001240void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001241 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001242
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001244 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 (kUnknownBitCount == format->fTotalBits));
1246 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001247 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001248 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1249 (GrGLint*)&format->fStencilBits);
1250 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001251 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1253 (GrGLint*)&format->fTotalBits);
1254 format->fTotalBits += format->fStencilBits;
1255 } else {
1256 format->fTotalBits = format->fStencilBits;
1257 }
1258 }
1259}
1260}
1261
egdanielff1d5472015-09-10 08:37:20 -07001262int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001263 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001264 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001265 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1266 // Default to unsupported, set this if we find a stencil format that works.
1267 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001268 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001269 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001270 GL_CALL(GenTextures(1, &colorID));
1271 this->setScratchTextureUnit();
1272 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1273 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1274 GR_GL_TEXTURE_MAG_FILTER,
1275 GR_GL_NEAREST));
1276 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1277 GR_GL_TEXTURE_MIN_FILTER,
1278 GR_GL_NEAREST));
1279 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1280 GR_GL_TEXTURE_WRAP_S,
1281 GR_GL_CLAMP_TO_EDGE));
1282 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1283 GR_GL_TEXTURE_WRAP_T,
1284 GR_GL_CLAMP_TO_EDGE));
1285
bsalomon76148af2016-01-12 11:13:47 -08001286 GrGLenum internalFormat;
1287 GrGLenum externalFormat;
1288 GrGLenum externalType;
1289 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1290 &externalType)) {
1291 return false;
1292 }
egdanielff1d5472015-09-10 08:37:20 -07001293 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1294 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001295 0,
bsalomon76148af2016-01-12 11:13:47 -08001296 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001297 kSize,
1298 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001299 0,
bsalomon76148af2016-01-12 11:13:47 -08001300 externalFormat,
1301 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001302 NULL));
bsalomon30447372015-12-21 09:03:05 -08001303 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001304 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001305 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001306 }
1307
1308 // unbind the texture from the texture unit before binding it to the frame buffer
1309 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1310
1311 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001312 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001313 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001314 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001315 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1316 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1317 GR_GL_COLOR_ATTACHMENT0,
1318 GR_GL_TEXTURE_2D,
1319 colorID,
1320 0));
bsalomon30447372015-12-21 09:03:05 -08001321 GrGLuint sbRBID = 0;
1322 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001323
1324 // look over formats till I find a compatible one
1325 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001326 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001327 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001328 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1329 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1330 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1331 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1332 sFmt.fInternalFormat,
1333 kSize, kSize));
1334 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001335 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001336 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001337 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001338 if (sFmt.fPacked) {
1339 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1340 GR_GL_DEPTH_ATTACHMENT,
1341 GR_GL_RENDERBUFFER, sbRBID));
1342 } else {
1343 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1344 GR_GL_DEPTH_ATTACHMENT,
1345 GR_GL_RENDERBUFFER, 0));
1346 }
1347 GrGLenum status;
1348 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1349 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1350 firstWorkingStencilFormatIndex = i;
1351 break;
1352 }
egdanielff1d5472015-09-10 08:37:20 -07001353 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1354 GR_GL_STENCIL_ATTACHMENT,
1355 GR_GL_RENDERBUFFER, 0));
1356 if (sFmt.fPacked) {
1357 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1358 GR_GL_DEPTH_ATTACHMENT,
1359 GR_GL_RENDERBUFFER, 0));
1360 }
egdanielff1d5472015-09-10 08:37:20 -07001361 }
1362 }
bsalomon30447372015-12-21 09:03:05 -08001363 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001364 }
1365 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001366 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1367 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001368 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001369 }
bsalomon30447372015-12-21 09:03:05 -08001370 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001371}
1372
egdanielec00d942015-09-14 12:56:10 -07001373GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1374 int width,
1375 int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001376 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001377 // 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 +00001378 SkASSERT(rt->asTexture());
1379 SkASSERT(width >= rt->width());
1380 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001381
vbuzinovdded6962015-06-12 08:59:45 -07001382 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001383 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001384
egdanielff1d5472015-09-10 08:37:20 -07001385 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001386 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001387 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001388 }
egdanielff1d5472015-09-10 08:37:20 -07001389
1390 if (!sbDesc.fRenderbufferID) {
1391 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1392 }
1393 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001394 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001395 }
1396 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1397 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1398 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1399 // we do this "if" so that we don't call the multisample
1400 // version on a GL that doesn't have an MSAA extension.
1401 if (samples > 0) {
1402 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1403 samples,
1404 sFmt.fInternalFormat,
1405 width, height));
1406 } else {
1407 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1408 sFmt.fInternalFormat,
1409 width, height));
1410 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1411 }
1412 fStats.incStencilAttachmentCreates();
1413 // After sized formats we attempt an unsized format and take
1414 // whatever sizes GL gives us. In that case we query for the size.
1415 GrGLStencilAttachment::Format format = sFmt;
1416 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001417 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1418 sbDesc,
1419 width,
1420 height,
1421 samples,
1422 format);
1423 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001426////////////////////////////////////////////////////////////////////////////////
1427
jvanverth73063dc2015-12-03 09:15:47 -08001428// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1429// objects are implemented as client-side-arrays on tile-deferred architectures.
1430#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1431
bsalomon861e1032014-12-16 07:33:49 -08001432GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001433 GrGLVertexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001434 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001435 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001436
jvanverth73063dc2015-12-03 09:15:47 -08001437 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001438 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001439 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001440 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001441 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001442 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001443 GL_CALL(GenBuffers(1, &desc.fID));
1444 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001445 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001446 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1447 // make sure driver can allocate memory for this buffer
1448 GL_ALLOC_CALL(this->glInterface(),
1449 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001450 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001451 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001452 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001453 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1454 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001455 this->notifyVertexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001456 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001457 }
halcanary385fe4d2015-08-26 13:07:48 -07001458 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001459 return vertexBuffer;
1460 }
halcanary96fcdcc2015-08-27 07:41:13 -07001461 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001462 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001463}
1464
bsalomon861e1032014-12-16 07:33:49 -08001465GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001466 GrGLIndexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001467 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001468 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001469
jvanverth73063dc2015-12-03 09:15:47 -08001470 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001471 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001472 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001474 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001475 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001476 GL_CALL(GenBuffers(1, &desc.fID));
1477 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001478 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001479 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1480 // make sure driver can allocate memory for this buffer
1481 GL_ALLOC_CALL(this->glInterface(),
1482 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001483 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001484 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001485 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001486 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1487 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001488 this->notifyIndexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001489 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001490 }
halcanary385fe4d2015-08-26 13:07:48 -07001491 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001492 return indexBuffer;
1493 }
halcanary96fcdcc2015-08-27 07:41:13 -07001494 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001495 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001496}
1497
jvanverthd7a2c1f2015-12-07 07:36:44 -08001498GrTransferBuffer* GrGLGpu::onCreateTransferBuffer(size_t size, TransferType xferType) {
1499 GrGLCaps::TransferBufferType xferBufferType = this->ctxInfo().caps()->transferBufferType();
1500 if (GrGLCaps::kNone_TransferBufferType == xferBufferType) {
1501 return nullptr;
1502 }
1503
jvanverth73063dc2015-12-03 09:15:47 -08001504 GrGLTransferBuffer::Desc desc;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001505 bool toGpu = (kCpuToGpu_TransferType == xferType);
jvanverth73063dc2015-12-03 09:15:47 -08001506 desc.fUsage = toGpu ? GrGLBufferImpl::kStreamDraw_Usage : GrGLBufferImpl::kStreamRead_Usage;
1507
1508 desc.fSizeInBytes = size;
kkinnunen546eb5c2015-12-11 00:05:33 -08001509 desc.fID = 0;
jvanverth73063dc2015-12-03 09:15:47 -08001510 GL_CALL(GenBuffers(1, &desc.fID));
1511 if (desc.fID) {
1512 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
jvanverthd7a2c1f2015-12-07 07:36:44 -08001513 // make sure driver can allocate memory for this bmapuffer
jvanverth17aa0472016-01-05 10:41:27 -08001514 GrGLenum target;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001515 if (GrGLCaps::kChromium_TransferBufferType == xferBufferType) {
jvanverth17aa0472016-01-05 10:41:27 -08001516 target = toGpu ? GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM
jvanverthd7a2c1f2015-12-07 07:36:44 -08001517 : GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
1518 } else {
1519 SkASSERT(GrGLCaps::kPBO_TransferBufferType == xferBufferType);
jvanverth17aa0472016-01-05 10:41:27 -08001520 target = toGpu ? GR_GL_PIXEL_UNPACK_BUFFER : GR_GL_PIXEL_PACK_BUFFER;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001521 }
jvanverth17aa0472016-01-05 10:41:27 -08001522 GL_CALL(BindBuffer(target, desc.fID));
1523 GL_ALLOC_CALL(this->glInterface(),
1524 BufferData(target,
jvanverth73063dc2015-12-03 09:15:47 -08001525 (GrGLsizeiptr) desc.fSizeInBytes,
1526 nullptr, // data ptr
1527 (toGpu ? GR_GL_STREAM_DRAW : GR_GL_STREAM_READ)));
1528 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1529 GL_CALL(DeleteBuffers(1, &desc.fID));
1530 return nullptr;
1531 }
jvanverth17aa0472016-01-05 10:41:27 -08001532 GrTransferBuffer* transferBuffer = new GrGLTransferBuffer(this, desc, target);
jvanverth73063dc2015-12-03 09:15:47 -08001533 return transferBuffer;
1534 }
1535
1536 return nullptr;
1537}
1538
bsalomon3e791242014-12-17 13:43:13 -08001539void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001540 const GrGLIRect& rtViewport,
1541 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001542 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001543 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001544 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001545 scissorState.rect().fLeft,
1546 scissorState.rect().fTop,
1547 scissorState.rect().width(),
1548 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001549 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001550 // if the scissor fully contains the viewport then we fall through and
1551 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001552 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001553 if (fHWScissorSettings.fRect != scissor) {
1554 scissor.pushToGLScissor(this->glInterface());
1555 fHWScissorSettings.fRect = scissor;
1556 }
1557 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1558 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1559 fHWScissorSettings.fEnabled = kYes_TriState;
1560 }
1561 return;
1562 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001563 }
joshualitt77b13072014-10-27 14:51:01 -07001564
1565 // See fall through note above
1566 this->disableScissor();
1567}
1568
cdaltond0a840d2015-03-16 17:19:58 -07001569bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001570 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001571 const GrPipeline& pipeline = *args.fPipeline;
bsalomon2047b782015-12-21 13:12:54 -08001572 args.fPipeline->getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001573
egdaniel080e6732014-12-22 07:35:52 -08001574 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001575 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001576
bsalomon6df86402015-06-01 10:41:49 -07001577 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args));
1578 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001579 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08001580 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001581 }
1582
bsalomon6df86402015-06-01 10:41:49 -07001583 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001584 if (fHWProgramID != programID) {
1585 GL_CALL(UseProgram(programID));
1586 fHWProgramID = programID;
1587 }
1588
egdanield803f272015-03-18 13:01:52 -07001589 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001590 // Swizzle the blend to match what the shader will output.
1591 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
1592 args.fPipeline->getRenderTarget()->config());
1593 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001594 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001595
cdalton42717652015-06-18 11:54:30 -07001596 SkSTArray<8, const GrTextureAccess*> textureAccesses;
joshualitt465283c2015-09-11 08:19:35 -07001597 program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07001598
1599 int numTextureAccesses = textureAccesses.count();
1600 for (int i = 0; i < numTextureAccesses; i++) {
1601 this->bindTexture(i, textureAccesses[i]->getParams(),
1602 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
1603 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001604
egdaniel8dd688b2015-01-22 10:16:09 -08001605 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1606 this->flushStencil(pipeline.getStencil());
1607 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdalton7e806f32015-11-11 15:16:07 -08001608 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001609
1610 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001611 // to be msaa-resolved (which will modify bound FBO state).
cdaltond4727922015-11-10 12:49:06 -08001612 this->flushRenderTarget(glRT, nullptr);
bsalomonbc3d0de2014-12-15 13:45:03 -08001613
1614 return true;
1615}
1616
joshualitt873ad0e2015-01-20 09:08:51 -08001617void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomone64eb572015-05-07 11:35:55 -07001618 const GrNonInstancedVertices& vertices,
bsalomonbc3d0de2014-12-15 13:45:03 -08001619 size_t* indexOffsetInBytes) {
1620 GrGLVertexBuffer* vbuf;
bsalomone64eb572015-05-07 11:35:55 -07001621 vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001622
1623 SkASSERT(vbuf);
1624 SkASSERT(!vbuf->isMapped());
1625
halcanary96fcdcc2015-08-27 07:41:13 -07001626 GrGLIndexBuffer* ibuf = nullptr;
bsalomone64eb572015-05-07 11:35:55 -07001627 if (vertices.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001628 SkASSERT(indexOffsetInBytes);
1629
1630 *indexOffsetInBytes = 0;
bsalomone64eb572015-05-07 11:35:55 -07001631 ibuf = (GrGLIndexBuffer*)vertices.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001632
1633 SkASSERT(ibuf);
1634 SkASSERT(!ibuf->isMapped());
1635 *indexOffsetInBytes += ibuf->baseOffset();
1636 }
1637 GrGLAttribArrayState* attribState =
1638 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1639
joshualitt873ad0e2015-01-20 09:08:51 -08001640 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001641 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001642
joshualitt873ad0e2015-01-20 09:08:51 -08001643 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001644
bsalomone64eb572015-05-07 11:35:55 -07001645 size_t vertexOffsetInBytes = stride * vertices.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08001646
1647 vertexOffsetInBytes += vbuf->baseOffset();
1648
bsalomonbc3d0de2014-12-15 13:45:03 -08001649 uint32_t usedAttribArraysMask = 0;
1650 size_t offset = 0;
1651
1652 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001653 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001654 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001655 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001656 attribState->set(this,
1657 attribIndex,
bsalomon6df86402015-06-01 10:41:49 -07001658 vbuf->bufferID(),
bsalomonbc3d0de2014-12-15 13:45:03 -08001659 GrGLAttribTypeToLayout(attribType).fCount,
1660 GrGLAttribTypeToLayout(attribType).fType,
1661 GrGLAttribTypeToLayout(attribType).fNormalized,
1662 stride,
1663 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001664 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001665 }
1666 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1667 }
1668}
1669
joshualitt873ad0e2015-01-20 09:08:51 -08001670void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1671 const GrPrimitiveProcessor& primProc,
joshualitt465283c2015-09-11 08:19:35 -07001672 const GrPipeline& pipeline) const {
bsalomon7f9b2e42016-01-12 13:29:26 -08001673 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, *this->glCaps().glslCaps())) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001674 SkDEBUGFAIL("Failed to generate GL program descriptor");
1675 }
1676}
1677
joshualitt93316b92015-10-23 09:08:08 -07001678void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) {
1679 this->handleDirtyContext();
1680 if (GR_GL_ARRAY_BUFFER == type) {
1681 this->bindVertexBuffer(id);
jvanverth73063dc2015-12-03 09:15:47 -08001682 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07001683 this->bindIndexBufferAndDefaultVertexArray(id);
jvanverthd7a2c1f2015-12-07 07:36:44 -08001684 } else {
1685 GR_GL_CALL(this->glInterface(), BindBuffer(type, id));
joshualitt93316b92015-10-23 09:08:08 -07001686 }
1687}
1688
1689void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) {
1690 this->handleDirtyContext();
1691 GL_CALL(DeleteBuffers(1, &id));
1692 if (GR_GL_ARRAY_BUFFER == type) {
1693 this->notifyVertexBufferDelete(id);
jvanverth73063dc2015-12-03 09:15:47 -08001694 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07001695 this->notifyIndexBufferDelete(id);
1696 }
1697}
1698
jvanverth73063dc2015-12-03 09:15:47 -08001699static GrGLenum get_gl_usage(GrGLBufferImpl::Usage usage) {
1700 static const GrGLenum grToGL[] = {
1701 GR_GL_STATIC_DRAW, // GrGLBufferImpl::kStaticDraw_Usage
1702 DYNAMIC_USAGE_PARAM, // GrGLBufferImpl::kDynamicDraw_Usage
1703 GR_GL_STREAM_DRAW, // GrGLBufferImpl::kStreamDraw_Usage
1704 GR_GL_STREAM_READ, // GrGLBufferImpl::kStreamRead_Usage
1705 };
1706 static_assert(SK_ARRAY_COUNT(grToGL) == GrGLBufferImpl::kUsageCount, "array_size_mismatch");
joshualitt93316b92015-10-23 09:08:08 -07001707
jvanverth73063dc2015-12-03 09:15:47 -08001708 return grToGL[usage];
1709}
1710
1711void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
1712 size_t currentSize, size_t requestedSize) {
joshualitt93316b92015-10-23 09:08:08 -07001713 void* mapPtr = nullptr;
jvanverth73063dc2015-12-03 09:15:47 -08001714 GrGLenum glUsage = get_gl_usage(usage);
jvanverthd7a2c1f2015-12-07 07:36:44 -08001715 bool readOnly = (GrGLBufferImpl::kStreamRead_Usage == usage);
1716
joshualitt93316b92015-10-23 09:08:08 -07001717 // Handling dirty context is done in the bindBuffer call
1718 switch (this->glCaps().mapBufferType()) {
1719 case GrGLCaps::kNone_MapBufferType:
1720 break;
1721 case GrGLCaps::kMapBuffer_MapBufferType:
1722 this->bindBuffer(id, type);
1723 // Let driver know it can discard the old data
joshualitt6df232d2015-10-23 13:54:12 -07001724 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001725 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001726 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001727 GL_CALL_RET(mapPtr, MapBuffer(type, readOnly ? GR_GL_READ_ONLY : GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07001728 break;
1729 case GrGLCaps::kMapBufferRange_MapBufferType: {
1730 this->bindBuffer(id, type);
1731 // Make sure the GL buffer size agrees with fDesc before mapping.
1732 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001733 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001734 }
jvanverth17aa0472016-01-05 10:41:27 -08001735 GrGLbitfield writeAccess = GR_GL_MAP_WRITE_BIT;
1736 // TODO: allow the client to specify invalidation in the stream draw case
1737 if (GrGLBufferImpl::kStreamDraw_Usage != usage) {
1738 writeAccess |= GR_GL_MAP_INVALIDATE_BUFFER_BIT;
1739 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001740 GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, readOnly ?
1741 GR_GL_MAP_READ_BIT :
jvanverth17aa0472016-01-05 10:41:27 -08001742 writeAccess));
joshualitt93316b92015-10-23 09:08:08 -07001743 break;
1744 }
1745 case GrGLCaps::kChromium_MapBufferType:
1746 this->bindBuffer(id, type);
1747 // Make sure the GL buffer size agrees with fDesc before mapping.
1748 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001749 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001750 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001751 GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, readOnly ?
1752 GR_GL_READ_ONLY :
1753 GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07001754 break;
1755 }
1756 return mapPtr;
1757}
1758
jvanverth73063dc2015-12-03 09:15:47 -08001759void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
1760 size_t currentSize, const void* src, size_t srcSizeInBytes) {
joshualitt93316b92015-10-23 09:08:08 -07001761 SkASSERT(srcSizeInBytes <= currentSize);
1762 // bindbuffer handles dirty context
1763 this->bindBuffer(id, type);
jvanverth73063dc2015-12-03 09:15:47 -08001764 GrGLenum glUsage = get_gl_usage(usage);
joshualitt93316b92015-10-23 09:08:08 -07001765
joshualitt6df232d2015-10-23 13:54:12 -07001766#if GR_GL_USE_BUFFER_DATA_NULL_HINT
1767 if (currentSize == srcSizeInBytes) {
jvanverth73063dc2015-12-03 09:15:47 -08001768 GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001769 } else {
1770 // Before we call glBufferSubData we give the driver a hint using
1771 // glBufferData with nullptr. This makes the old buffer contents
1772 // inaccessible to future draws. The GPU may still be processing
1773 // draws that reference the old contents. With this hint it can
1774 // assign a different allocation for the new contents to avoid
1775 // flushing the gpu past draws consuming the old contents.
1776 // TODO I think we actually want to try calling bufferData here
jvanverth73063dc2015-12-03 09:15:47 -08001777 GL_CALL(BufferData(type, currentSize, nullptr, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001778 GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src));
1779 }
1780#else
joshualitt93316b92015-10-23 09:08:08 -07001781 // Note that we're cheating on the size here. Currently no methods
1782 // allow a partial update that preserves contents of non-updated
1783 // portions of the buffer (map() does a glBufferData(..size, nullptr..))
jvanverth73063dc2015-12-03 09:15:47 -08001784 GL_CALL(BufferData(type, srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001785#endif
joshualitt93316b92015-10-23 09:08:08 -07001786}
1787
1788void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) {
1789 // bind buffer handles the dirty context
1790 switch (this->glCaps().mapBufferType()) {
1791 case GrGLCaps::kNone_MapBufferType:
1792 SkDEBUGFAIL("Shouldn't get here.");
1793 return;
1794 case GrGLCaps::kMapBuffer_MapBufferType: // fall through
1795 case GrGLCaps::kMapBufferRange_MapBufferType:
1796 this->bindBuffer(id, type);
1797 GL_CALL(UnmapBuffer(type));
1798 break;
1799 case GrGLCaps::kChromium_MapBufferType:
1800 this->bindBuffer(id, type);
1801 GL_CALL(UnmapBufferSubData(mapPtr));
1802 break;
1803 }
1804}
1805
bsalomon861e1032014-12-16 07:33:49 -08001806void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001807 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001808 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001809 fHWScissorSettings.fEnabled = kNo_TriState;
1810 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 }
1812}
1813
egdaniel51c8d402015-08-06 10:54:13 -07001814void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001815 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001816 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001817 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001818
egdaniel51c8d402015-08-06 10:54:13 -07001819 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08001820 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07001821 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001822 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001823
1824 GrGLfloat r, g, b, a;
1825 static const GrGLfloat scale255 = 1.f / 255.f;
1826 a = GrColorUnpackA(color) * scale255;
1827 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001828 r = GrColorUnpackR(color) * scaleRGB;
1829 g = GrColorUnpackG(color) * scaleRGB;
1830 b = GrColorUnpackB(color) * scaleRGB;
1831
1832 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001833 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001834 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001835 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001836}
1837
bsalomon861e1032014-12-16 07:33:49 -08001838void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001839 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001840 if (!this->caps()->discardRenderTargetSupport()) {
1841 return;
1842 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001843
1844 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001845 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1846 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1847 fStats.incRenderTargetBinds();
1848 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
1849 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001850 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07001851 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001852 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001853 break;
1854 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07001855 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001856 // When rendering to the default framebuffer the legal values for attachments
1857 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1858 // types.
1859 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001860 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001861 attachments));
1862 } else {
1863 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001864 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001865 attachments));
1866 }
1867 break;
1868 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07001869 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001870 // When rendering to the default framebuffer the legal values for attachments
1871 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
1872 // types. See glDiscardFramebuffer() spec.
1873 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07001874 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001875 attachments));
1876 } else {
1877 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07001878 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00001879 attachments));
1880 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001881 break;
1882 }
1883 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001884 renderTarget->flagAsResolved();
1885}
1886
bsalomon861e1032014-12-16 07:33:49 -08001887void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07001888 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001889 return;
1890 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001891 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001892 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001893
joshualitt77b13072014-10-27 14:51:01 -07001894 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001895
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001896 GL_CALL(StencilMask(0xffffffff));
1897 GL_CALL(ClearStencil(0));
1898 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001899 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001900}
1901
bsalomon861e1032014-12-16 07:33:49 -08001902void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07001903 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001904
egdaniel8dc7c3a2015-04-16 11:22:42 -07001905 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001906 // this should only be called internally when we know we have a
1907 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001908 SkASSERT(sb);
1909 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001910#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001911 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001912 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001913#else
1914 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001915 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001916 // turned into draws. Our contract on GrDrawTarget says that
1917 // changing the clip between stencil passes may or may not
1918 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001919 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001920#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001921 GrGLint value;
1922 if (insideClip) {
1923 value = (1 << (stencilBitCount - 1));
1924 } else {
1925 value = 0;
1926 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001927 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07001928 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929
bsalomon3e791242014-12-17 13:43:13 -08001930 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08001931 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001932 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00001933
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001934 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001935 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001936 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001937 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001938}
1939
bsalomon39826022015-07-23 08:07:21 -07001940static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
1941 int width, int height, GrPixelConfig config,
1942 size_t rowBytes) {
1943 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001944 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
1945 return false;
1946 }
1947
bsalomon494aa592015-07-23 11:45:02 -07001948 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07001949 static const int kMinSize = 32;
1950 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07001951 return false;
1952 }
1953
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001954 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07001955 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001956 return false;
1957 }
1958
1959 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00001960 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07001961 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
1962 // clipped.
1963 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
1964}
1965
bsalomon1aa20292016-01-22 08:16:09 -08001966bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
1967 auto bindRenderTarget = [this, target]() -> bool {
1968 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
1969 return true;
1970 };
1971 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
1972 GR_GL_GetIntegerv(this->glInterface(), query, value);
1973 };
1974 GrPixelConfig rtConfig = target->config();
1975 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
1976}
1977
1978bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
1979 auto bindRenderTarget = [this, rtConfig]() -> bool {
1980 GrTextureDesc desc;
1981 desc.fConfig = rtConfig;
1982 desc.fWidth = desc.fHeight = 16;
1983 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1984 SkAutoTUnref<GrTexture> temp(this->createTexture(desc, false, nullptr, 0));
1985 if (!temp) {
1986 return false;
1987 }
1988 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
1989 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
1990 return true;
1991 };
1992 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
1993 GR_GL_GetIntegerv(this->glInterface(), query, value);
1994 };
1995 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
1996}
1997
1998bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
1999 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2000 return this->readPixelsSupported(rt, readConfig);
2001 } else {
2002 GrPixelConfig config = surfaceForConfig->config();
2003 return this->readPixelsSupported(config, readConfig);
2004 }
2005}
2006
bsalomonf0674512015-07-28 13:26:15 -07002007bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2008 GrPixelConfig readConfig, DrawPreference* drawPreference,
2009 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomon1aa20292016-01-22 08:16:09 -08002010 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2011
bsalomon6cb3cbe2015-07-30 07:34:27 -07002012 // This subclass can only read pixels from a render target. We could use glTexSubImage2D on
2013 // GL versions that support it but we don't today.
bsalomon1aa20292016-01-22 08:16:09 -08002014 if (!srcAsRT) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002015 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2016 }
2017
bsalomon16921ec2015-07-30 15:34:56 -07002018 if (GrPixelConfigIsSRGB(srcSurface->config()) != GrPixelConfigIsSRGB(readConfig)) {
2019 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2020 }
2021
bsalomon6c9cd552016-01-22 07:17:34 -08002022 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2023 tempDrawInfo->fReadConfig = readConfig;
bsalomon39826022015-07-23 08:07:21 -07002024
2025 // These settings we will always want if a temp draw is performed. The config is set below
2026 // depending on whether we want to do a R/B swap or not.
2027 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2028 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2029 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2030 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2031 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07002032 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07002033
2034 // Start off assuming that any temp draw should be to the readConfig, then check if that will
2035 // be inefficient.
2036 GrPixelConfig srcConfig = srcSurface->config();
2037 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2038
bsalomon1aa20292016-01-22 08:16:09 -08002039 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2040 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002041 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002042 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2043 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002044 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002045 } else if (kMesa_GrGLDriver == this->glContext().driver() &&
2046 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002047 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2048 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002049 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002050 // Better to do a draw with a R/B swap and then read as the original config.
2051 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002052 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2053 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002054 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002055 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2056 if (readConfig == kBGRA_8888_GrPixelConfig &&
2057 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2058 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
2059
2060 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2061 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2062 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2063 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2064 } else {
2065 return false;
2066 }
bsalomon39826022015-07-23 08:07:21 -07002067 }
2068
bsalomon1aa20292016-01-22 08:16:09 -08002069 if (srcAsRT &&
2070 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002071 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002072 }
2073
bsalomon39826022015-07-23 08:07:21 -07002074 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002075}
2076
bsalomon6cb3cbe2015-07-30 07:34:27 -07002077bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002078 int left, int top,
2079 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002080 GrPixelConfig config,
2081 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002082 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002083 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002084
bsalomon6cb3cbe2015-07-30 07:34:27 -07002085 GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2086 if (!tgt) {
2087 return false;
2088 }
2089
bsalomon16921ec2015-07-30 15:34:56 -07002090 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
2091 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
2092 return false;
2093 }
2094
bsalomon76148af2016-01-12 11:13:47 -08002095 GrGLenum externalFormat;
2096 GrGLenum externalType;
2097 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
2098 &externalType)) {
2099 return false;
2100 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002101 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002102
bsalomon@google.comc6980972011-11-02 19:57:21 +00002103 // resolve the render target if necessary
egdanield803f272015-03-18 13:01:52 -07002104 switch (tgt->getResolveType()) {
2105 case GrGLRenderTarget::kCantResolve_ResolveType:
2106 return false;
2107 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomon6cb3cbe2015-07-30 07:34:27 -07002108 this->flushRenderTarget(tgt, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002109 break;
2110 case GrGLRenderTarget::kCanResolve_ResolveType:
2111 this->onResolveRenderTarget(tgt);
2112 // we don't track the state of the READ FBO ID.
2113 fStats.incRenderTargetBinds();
bsalomon6c9cd552016-01-22 07:17:34 -08002114 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002115 break;
2116 default:
2117 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
2119
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002120 const GrGLIRect& glvp = tgt->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002121
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002122 // the read rect is viewport-relative
2123 GrGLIRect readRect;
bsalomon6cb3cbe2015-07-30 07:34:27 -07002124 readRect.setRelativeTo(glvp, left, top, width, height, tgt->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002125
egdaniel6d901da2015-07-30 12:02:15 -07002126 size_t tightRowBytes = GrBytesPerPixel(config) * width;
2127
bsalomon@google.comc6980972011-11-02 19:57:21 +00002128 size_t readDstRowBytes = tightRowBytes;
2129 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002130
bsalomon@google.comc6980972011-11-02 19:57:21 +00002131 // determine if GL can read using the passed rowBytes or if we need
2132 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002133 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002134 if (rowBytes != tightRowBytes) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00002135 if (this->glCaps().packRowLengthSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002136 SkASSERT(!(rowBytes % sizeof(GrColor)));
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002137 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00002138 static_cast<GrGLint>(rowBytes / sizeof(GrColor))));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002139 readDstRowBytes = rowBytes;
2140 } else {
2141 scratch.reset(tightRowBytes * height);
2142 readDst = scratch.get();
2143 }
2144 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002145 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002146 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2147 }
bsalomonf46a1242015-12-15 12:37:38 -08002148 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2149
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2151 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002152 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002153 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002154 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002155 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2156 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002157 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002158 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002159 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002160 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002161
2162 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002163 // API presents top-to-bottom. We must preserve the padding contents. Note
2164 // that the above readPixels did not overwrite the padding.
2165 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002166 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002167 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002168 scratch.reset(tightRowBytes);
2169 void* tmpRow = scratch.get();
2170 // flip y in-place by rows
2171 const int halfY = height >> 1;
2172 char* top = reinterpret_cast<char*>(buffer);
2173 char* bottom = top + (height - 1) * rowBytes;
2174 for (int y = 0; y < halfY; y++) {
2175 memcpy(tmpRow, top, tightRowBytes);
2176 memcpy(top, bottom, tightRowBytes);
2177 memcpy(bottom, tmpRow, tightRowBytes);
2178 top += rowBytes;
2179 bottom -= rowBytes;
2180 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002181 }
2182 } else {
egdanield803f272015-03-18 13:01:52 -07002183 SkASSERT(readDst != buffer); SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002184 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002185 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002186 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002187 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002188 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002189 dst += (height-1) * rowBytes;
2190 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002191 for (int y = 0; y < height; y++) {
2192 memcpy(dst, src, tightRowBytes);
2193 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002194 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002195 dst += rowBytes;
2196 } else {
2197 dst -= rowBytes;
2198 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002199 }
2200 }
2201 return true;
2202}
2203
ethannicholas7df3f5e2016-01-22 06:48:46 -08002204void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
ethannicholas5366a092016-01-22 09:45:47 -08002205
egdanield803f272015-03-18 13:01:52 -07002206 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002207
egdanield803f272015-03-18 13:01:52 -07002208 uint32_t rtID = target->getUniqueID();
2209 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002210 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002211 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2212#ifdef SK_DEBUG
2213 // don't do this check in Chromium -- this is causing
2214 // lots of repeated command buffer flushes when the compositor is
2215 // rendering with Ganesh, which is really slow; even too slow for
2216 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002217 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002218 GrGLenum status;
2219 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2220 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2221 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2222 }
bsalomon160f24c2015-03-17 15:55:42 -07002223 }
egdanield803f272015-03-18 13:01:52 -07002224#endif
2225 fHWBoundRenderTargetUniqueID = rtID;
2226 const GrGLIRect& vp = target->getViewport();
2227 if (fHWViewport != vp) {
2228 vp.pushToGLViewport(this->glInterface());
2229 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07002230 }
bsalomon16921ec2015-07-30 15:34:56 -07002231 if (this->glCaps().srgbWriteControl()) {
2232 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config());
2233 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2234 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2235 fHWSRGBFramebuffer = kYes_TriState;
2236 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2237 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2238 fHWSRGBFramebuffer = kNo_TriState;
2239 }
2240 }
bsalomon5cd020f2015-03-17 12:46:56 -07002241 }
bsalomona9909122016-01-23 10:41:40 -08002242
2243 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
halcanary96fcdcc2015-08-27 07:41:13 -07002244 if (nullptr == bound || !bound->isEmpty()) {
egdanield803f272015-03-18 13:01:52 -07002245 target->flagAsNeedingResolve(bound);
bsalomona9909122016-01-23 10:41:40 -08002246 if (GrTexture *texture = target->asTexture()) {
2247 texture->texturePriv().dirtyMipMaps(true);
2248 }
egdanield803f272015-03-18 13:01:52 -07002249 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002250}
2251
twiz@google.com0f31ca72011-03-18 17:38:11 +00002252GrGLenum gPrimitiveType2GLMode[] = {
2253 GR_GL_TRIANGLES,
2254 GR_GL_TRIANGLE_STRIP,
2255 GR_GL_TRIANGLE_FAN,
2256 GR_GL_POINTS,
2257 GR_GL_LINES,
2258 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002259};
2260
bsalomon@google.comd302f142011-03-03 13:54:13 +00002261#define SWAP_PER_DRAW 0
2262
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002263#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002264 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002265 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002266 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002267 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002268 void SwapBuf() {
2269 DWORD procID = GetCurrentProcessId();
2270 HWND hwnd = GetTopWindow(GetDesktopWindow());
2271 while(hwnd) {
2272 DWORD wndProcID = 0;
2273 GetWindowThreadProcessId(hwnd, &wndProcID);
2274 if(wndProcID == procID) {
2275 SwapBuffers(GetDC(hwnd));
2276 }
2277 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2278 }
2279 }
2280 #endif
2281#endif
2282
bsalomone64eb572015-05-07 11:35:55 -07002283void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) {
cdaltond0a840d2015-03-16 17:19:58 -07002284 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08002285 return;
2286 }
2287
joshualitt873ad0e2015-01-20 09:08:51 -08002288 size_t indexOffsetInBytes = 0;
bsalomoncb8979d2015-05-05 09:51:38 -07002289 this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00002290
bsalomoncb8979d2015-05-05 09:51:38 -07002291 SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002292
bsalomoncb8979d2015-05-05 09:51:38 -07002293 if (vertices.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002294 GrGLvoid* indices =
bsalomoncb8979d2015-05-05 09:51:38 -07002295 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2296 vertices.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002297 // info.startVertex() was accounted for by setupGeometry.
bsalomoncb8979d2015-05-05 09:51:38 -07002298 GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()],
2299 vertices.indexCount(),
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002300 GR_GL_UNSIGNED_SHORT,
2301 indices));
2302 } else {
2303 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
2304 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
2305 // accounted for startVertex.
bsalomoncb8979d2015-05-05 09:51:38 -07002306 GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0,
2307 vertices.vertexCount()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002308 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002309#if SWAP_PER_DRAW
2310 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002311 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002312 aglSwapBuffers(aglGetCurrentContext());
2313 int set_a_break_pt_here = 9;
2314 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002315 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002316 SwapBuf();
2317 int set_a_break_pt_here = 9;
2318 SwapBuf();
2319 #endif
2320#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002321}
2322
bsalomon861e1032014-12-16 07:33:49 -08002323void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002324 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002325 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002326 // Some extensions automatically resolves the texture when it is read.
2327 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002328 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2329 fStats.incRenderTargetBinds();
2330 fStats.incRenderTargetBinds();
2331 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2332 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2333 // make sure we go through flushRenderTarget() since we've modified
2334 // the bound DRAW FBO ID.
2335 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002336 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002337 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002338
bsalomon@google.com347c3822013-05-01 20:10:01 +00002339 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002340 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002341 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002342 scissorState.set(dirtyRect);
2343 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002344 GL_CALL(ResolveMultisampleFramebuffer());
2345 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002346 GrGLIRect r;
2347 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2348 dirtyRect.width(), dirtyRect.height(), target->origin());
2349
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002350 int right = r.fLeft + r.fWidth;
2351 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002352
2353 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002354 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002355 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2356 r.fLeft, r.fBottom, right, top,
2357 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002358 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002359 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002360 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 }
2362}
2363
bsalomon@google.com411dad02012-06-05 20:24:20 +00002364namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002365
bsalomon@google.com411dad02012-06-05 20:24:20 +00002366
2367GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2368 static const GrGLenum gTable[] = {
2369 GR_GL_KEEP, // kKeep_StencilOp
2370 GR_GL_REPLACE, // kReplace_StencilOp
2371 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2372 GR_GL_INCR, // kIncClamp_StencilOp
2373 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2374 GR_GL_DECR, // kDecClamp_StencilOp
2375 GR_GL_ZERO, // kZero_StencilOp
2376 GR_GL_INVERT, // kInvert_StencilOp
2377 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002378 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002379 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2380 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2381 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2382 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2383 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2384 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2385 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2386 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002387 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002388 return gTable[op];
2389}
2390
2391void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002392 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002393 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002394 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002395 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002396 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2397 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2398
2399 GrGLint ref = settings.funcRef(grFace);
2400 GrGLint mask = settings.funcMask(grFace);
2401 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002402
2403 if (GR_GL_FRONT_AND_BACK == glFace) {
2404 // we call the combined func just in case separate stencil is not
2405 // supported.
2406 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2407 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002408 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002409 } else {
2410 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2411 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002412 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002413 }
2414}
2415}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002416
bsalomon3e791242014-12-17 13:43:13 -08002417void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2418 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002419 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002420 if (kNo_TriState != fHWStencilTestEnabled) {
2421 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2422 fHWStencilTestEnabled = kNo_TriState;
2423 }
2424 } else {
2425 if (kYes_TriState != fHWStencilTestEnabled) {
2426 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2427 fHWStencilTestEnabled = kYes_TriState;
2428 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002429 }
joshualitta58fe352014-10-27 08:39:00 -07002430 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002431 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002432 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002433 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002434 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002435 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002436 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002437 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002438 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002439 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002440 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002441 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002442 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002443 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002444 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002445 }
2446 }
joshualitta58fe352014-10-27 08:39:00 -07002447 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002448 }
2449}
2450
cdalton7e806f32015-11-11 15:16:07 -08002451void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
vbuzinovdded6962015-06-12 08:59:45 -07002452 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002453
cdaltond4727922015-11-10 12:49:06 -08002454 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002455 if (useHWAA) {
2456 if (kYes_TriState != fMSAAEnabled) {
2457 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2458 fMSAAEnabled = kYes_TriState;
2459 }
2460 } else {
2461 if (kNo_TriState != fMSAAEnabled) {
2462 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2463 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002464 }
2465 }
2466 }
2467}
2468
bsalomon7f9b2e42016-01-12 13:29:26 -08002469void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002470 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002471 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002472
cdalton8917d622015-05-06 13:40:21 -07002473 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002474 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2475 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002476 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2477 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002478 if (blendOff) {
2479 if (kNo_TriState != fHWBlendState.fEnabled) {
2480 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002481
2482 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2483 // https://code.google.com/p/skia/issues/detail?id=3943
2484 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2485 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2486 SkASSERT(this->caps()->advancedBlendEquationSupport());
2487 // Set to any basic blending equation.
2488 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2489 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2490 fHWBlendState.fEquation = blend_equation;
2491 }
2492
egdanielb414f252014-07-29 13:15:47 -07002493 fHWBlendState.fEnabled = kNo_TriState;
2494 }
cdalton8917d622015-05-06 13:40:21 -07002495 return;
2496 }
2497
2498 if (kYes_TriState != fHWBlendState.fEnabled) {
2499 GL_CALL(Enable(GR_GL_BLEND));
2500 fHWBlendState.fEnabled = kYes_TriState;
2501 }
2502
2503 if (fHWBlendState.fEquation != equation) {
2504 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2505 fHWBlendState.fEquation = equation;
2506 }
2507
2508 if (GrBlendEquationIsAdvanced(equation)) {
2509 SkASSERT(this->caps()->advancedBlendEquationSupport());
2510 // Advanced equations have no other blend state.
2511 return;
2512 }
2513
2514 if (fHWBlendState.fSrcCoeff != srcCoeff ||
2515 fHWBlendState.fDstCoeff != dstCoeff) {
2516 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2517 gXfermodeCoeff2Blend[dstCoeff]));
2518 fHWBlendState.fSrcCoeff = srcCoeff;
2519 fHWBlendState.fDstCoeff = dstCoeff;
2520 }
2521
bsalomon7f9b2e42016-01-12 13:29:26 -08002522 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2523 GrColor blendConst = blendInfo.fBlendConstant;
2524 blendConst = swizzle.applyTo(blendConst);
2525 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2526 GrGLfloat c[4];
2527 GrColorToRGBAFloat(blendConst, c);
2528 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2529 fHWBlendState.fConstColor = blendConst;
2530 fHWBlendState.fConstColorValid = true;
2531 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002532 }
2533}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002534
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002535static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002536 static const GrGLenum gWrapModes[] = {
2537 GR_GL_CLAMP_TO_EDGE,
2538 GR_GL_REPEAT,
2539 GR_GL_MIRRORED_REPEAT
2540 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002541 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002542 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2543 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2544 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2545 return gWrapModes[tm];
2546}
2547
egdanielb7e7d572015-11-04 04:23:53 -08002548static GrGLenum get_component_enum_from_char(char component) {
2549 switch (component) {
2550 case 'r':
2551 return GR_GL_RED;
2552 case 'g':
2553 return GR_GL_GREEN;
2554 case 'b':
2555 return GR_GL_BLUE;
2556 case 'a':
2557 return GR_GL_ALPHA;
2558 default:
2559 SkFAIL("Unsupported component");
2560 return 0;
2561 }
2562}
2563
2564/** If texture swizzling is available using tex parameters then it is preferred over mangling
2565 the generated shader code. This potentially allows greater reuse of cached shaders. */
2566static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002567 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002568 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002569 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002570 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002571 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002572 }
2573}
2574
bsalomon861e1032014-12-16 07:33:49 -08002575void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002576 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002577
reed856e9d92015-09-30 12:21:45 -07002578#ifdef SK_DEBUG
2579 if (!this->caps()->npotTextureTileSupport()) {
2580 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2581 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2582 if (tileX || tileY) {
2583 const int w = texture->width();
2584 const int h = texture->height();
2585 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2586 }
2587 }
2588#endif
2589
bsalomon@google.comb8670992012-07-25 21:27:09 +00002590 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2591 // 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 +00002592 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002593 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002594 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002595 this->onResolveRenderTarget(texRT);
2596 }
2597
bsalomon1c63bf62014-07-22 13:09:46 -07002598 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002599 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002600 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002601 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002602 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002603 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002604 }
2605
bsalomon@google.com4c883782012-06-04 19:05:11 +00002606 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002607 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002608 bool setAll = timestamp < this->getResetTimestamp();
2609 GrGLTexture::TexParams newTexParams;
2610
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002611 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002612 GR_GL_NEAREST,
2613 GR_GL_LINEAR,
2614 GR_GL_LINEAR_MIPMAP_LINEAR
2615 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002616 static GrGLenum glMagFilterModes[] = {
2617 GR_GL_NEAREST,
2618 GR_GL_LINEAR,
2619 GR_GL_LINEAR
2620 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002621 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002622
2623 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2624 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2625 filterMode = GrTextureParams::kBilerp_FilterMode;
2626 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002627 }
bsalomonefd7d452014-10-23 14:17:46 -07002628
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002629 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2630 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002631
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002632 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002633 texture->texturePriv().mipMapsAreDirty()) {
bsalomon10528f12015-10-14 12:54:52 -07002634 GL_CALL(GenerateMipmap(target));
bsalomonafbf2d62014-09-30 12:18:44 -07002635 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002636 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002637
bsalomon@google.comb8670992012-07-25 21:27:09 +00002638 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2639 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08002640 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002641 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002642 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002643 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002644 }
2645 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2646 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002647 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002648 }
2649 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002650 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002651 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002652 }
2653 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002654 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002655 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002656 }
bsalomoncdee0092016-01-08 13:20:12 -08002657 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00002658 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2659 oldTexParams.fSwizzleRGBA,
2660 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002661 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002662 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002663 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2664 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07002665 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
2666 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
2667 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
2668 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002669 } else {
2670 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
2671 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07002672 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002673 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002674 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002675 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00002676}
2677
egdaniel080e6732014-12-22 07:35:52 -08002678void GrGLGpu::flushColorWrite(bool writeColor) {
2679 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002680 if (kNo_TriState != fHWWriteToColor) {
2681 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2682 GR_GL_FALSE, GR_GL_FALSE));
2683 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002684 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002685 } else {
2686 if (kYes_TriState != fHWWriteToColor) {
2687 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
2688 fHWWriteToColor = kYes_TriState;
2689 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002690 }
bsalomon3e791242014-12-17 13:43:13 -08002691}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002692
egdaniel8dd688b2015-01-22 10:16:09 -08002693void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08002694 if (fHWDrawFace != face) {
2695 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08002696 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002697 GL_CALL(Enable(GR_GL_CULL_FACE));
2698 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002699 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002700 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002701 GL_CALL(Enable(GR_GL_CULL_FACE));
2702 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002703 break;
egdaniel8dd688b2015-01-22 10:16:09 -08002704 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002705 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00002706 break;
2707 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002708 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00002709 }
bsalomon3e791242014-12-17 13:43:13 -08002710 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002711 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002712}
2713
bsalomon861e1032014-12-16 07:33:49 -08002714void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07002715 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002716 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002717 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002718 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002719 }
2720}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002721
bsalomon861e1032014-12-16 07:33:49 -08002722void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002723 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07002724 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002725 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2726 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2727 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002728 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002729 // clear out the this field so that if a program does use this unit it will rebind the correct
2730 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07002731 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002732}
2733
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002734// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08002735static inline bool can_blit_framebuffer(const GrSurface* dst,
2736 const GrSurface* src,
2737 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002738 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
2739 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00002740 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00002741 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
2742 // or the rects are not the same (not just the same size but have the same edges).
2743 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
2744 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
2745 return false;
2746 }
bsalomon7ea33f52015-11-22 14:51:00 -08002747 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
2748 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
2749 return false;
2750 }
2751 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
2752 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
2753 return false;
2754 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002755 return true;
2756 } else {
2757 return false;
2758 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002759}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002760
bsalomon7ea33f52015-11-22 14:51:00 -08002761static inline bool can_copy_texsubimage(const GrSurface* dst,
2762 const GrSurface* src,
2763 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002764 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
2765 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
2766 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002767 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002768 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
2769 return false;
2770 }
2771 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
2772 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
2773 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07002774 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002775 return false;
2776 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00002777 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
2778 // If the src is multisampled (and uses an extension where there is a separate MSAA
2779 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07002780 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00002781 return false;
2782 }
bsalomon7ea33f52015-11-22 14:51:00 -08002783
2784 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
2785 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
2786 // texture.
2787 if (!dstTex) {
2788 return false;
2789 }
2790
2791 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
2792
2793 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
2794 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00002795 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08002796 !GrPixelConfigIsCompressed(src->config()) &&
2797 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
2798 dstTex->target() == GR_GL_TEXTURE_2D &&
2799 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002800 return true;
2801 } else {
2802 return false;
2803 }
2804}
2805
2806// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
2807// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07002808void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
2809 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002810 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -07002811 if (nullptr == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07002812 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002813 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07002814 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07002815 GrGLuint* tempFBOID;
2816 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002817
egdanield803f272015-03-18 13:01:52 -07002818 if (0 == *tempFBOID) {
2819 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002820 }
2821
egdanield803f272015-03-18 13:01:52 -07002822 fStats.incRenderTargetBinds();
2823 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
2824 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07002825 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07002826 target,
robertphillips754f4e92014-09-18 13:52:08 -07002827 texID,
2828 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002829 viewport->fLeft = 0;
2830 viewport->fBottom = 0;
2831 viewport->fWidth = surface->width();
2832 viewport->fHeight = surface->height();
2833 } else {
egdanield803f272015-03-18 13:01:52 -07002834 fStats.incRenderTargetBinds();
2835 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00002836 *viewport = rt->getViewport();
2837 }
egdaniel0f5f9672015-02-03 11:10:51 -08002838}
2839
bsalomon10528f12015-10-14 12:54:52 -07002840void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
2841 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
2842 if (!surface->asRenderTarget()) {
2843 SkASSERT(surface->asTexture());
2844 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2845 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2846 GR_GL_COLOR_ATTACHMENT0,
2847 textureTarget,
2848 0,
2849 0));
2850 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002851}
2852
joshualitt1c735482015-07-13 08:08:25 -07002853bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07002854 // If the src is a texture, we can implement the blit as a draw assuming the config is
2855 // renderable.
2856 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
2857 desc->fOrigin = kDefault_GrSurfaceOrigin;
2858 desc->fFlags = kRenderTarget_GrSurfaceFlag;
2859 desc->fConfig = src->config();
2860 return true;
2861 }
2862
bsalomon7ea33f52015-11-22 14:51:00 -08002863 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
2864 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
2865 // Not supported for FBO blit or CopyTexSubImage
2866 return false;
2867 }
2868
bsalomon6df86402015-06-01 10:41:49 -07002869 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08002870 // possible and we return false to fallback to creating a render target dst for render-to-
2871 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
2872 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
2873
bsalomon@google.comeb851172013-04-15 13:51:00 +00002874 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002875 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00002876 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002877 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
2878 // then we set up for that, otherwise fail.
2879 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
2880 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002881 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002882 desc->fConfig = kBGRA_8888_GrPixelConfig;
2883 return true;
2884 }
2885 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07002886 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002887 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
2888 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00002889 }
2890
2891 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07002892 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08002893 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
2894 // fail.
2895 if (this->caps()->isConfigRenderable(src->config(), false)) {
2896 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08002897 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08002898 desc->fConfig = src->config();
2899 return true;
2900 }
2901 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00002902 }
bsalomonf90a02b2014-11-26 12:28:00 -08002903
2904 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
2905 desc->fConfig = src->config();
2906 desc->fOrigin = src->origin();
2907 desc->fFlags = kNone_GrSurfaceFlags;
2908 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002909}
2910
joshualitt1cbdcde2015-08-21 11:53:29 -07002911bool GrGLGpu::onCopySurface(GrSurface* dst,
2912 GrSurface* src,
2913 const SkIRect& srcRect,
2914 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002915 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
2916 // swizzle.
2917 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
2918 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
2919 return false;
2920 }
bsalomon6df86402015-06-01 10:41:49 -07002921 if (src->asTexture() && dst->asRenderTarget()) {
2922 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
mtklein404b3b22015-05-18 09:29:10 -07002923 return true;
bsalomon5df6fee2015-05-18 06:26:15 -07002924 }
bsalomon6df86402015-06-01 10:41:49 -07002925
2926 if (can_copy_texsubimage(dst, src, this)) {
2927 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
2928 return true;
2929 }
2930
mtklein404b3b22015-05-18 09:29:10 -07002931 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07002932 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
2933 }
2934
2935 return false;
2936}
2937
bsalomon7ea33f52015-11-22 14:51:00 -08002938void GrGLGpu::createCopyPrograms() {
2939 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
2940 fCopyPrograms[i].fProgram = 0;
bsalomon27a04872015-11-20 19:34:37 -08002941 }
bsalomon7ea33f52015-11-22 14:51:00 -08002942 const char* version = this->glCaps().glslCaps()->versionDeclString();
bsalomone5286e02016-01-14 09:24:09 -08002943 static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType,
2944 kSampler2DRect_GrSLType };
2945 SkASSERT(3 == SK_ARRAY_COUNT(fCopyPrograms));
2946 for (int i = 0; i < 3; ++i) {
2947 if (kSamplerExternal_GrSLType == kSamplerTypes[i] &&
2948 !this->glCaps().externalTextureSupport()) {
2949 continue;
2950 }
2951 if (kSampler2DRect_GrSLType == kSamplerTypes[i] &&
2952 !this->glCaps().rectangleTextureSupport()) {
2953 continue;
2954 }
bsalomon7ea33f52015-11-22 14:51:00 -08002955 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
2956 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
2957 GrShaderVar::kUniform_TypeModifier);
2958 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
2959 GrShaderVar::kUniform_TypeModifier);
2960 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[i],
2961 GrShaderVar::kUniform_TypeModifier);
2962 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
2963 GrShaderVar::kVaryingOut_TypeModifier);
2964 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
2965 GrShaderVar::kOut_TypeModifier);
bsalomon6dea83f2015-12-03 12:58:06 -08002966
bsalomon7ea33f52015-11-22 14:51:00 -08002967 SkString vshaderTxt(version);
2968 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2969 vshaderTxt.append(";");
2970 uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2971 vshaderTxt.append(";");
2972 uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2973 vshaderTxt.append(";");
2974 vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
2975 vshaderTxt.append(";");
2976
2977 vshaderTxt.append(
2978 "// Copy Program VS\n"
2979 "void main() {"
2980 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
2981 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
2982 " gl_Position.zw = vec2(0, 1);"
2983 "}"
2984 );
bsalomon27a04872015-11-20 19:34:37 -08002985
bsalomon7ea33f52015-11-22 14:51:00 -08002986 SkString fshaderTxt(version);
2987 if (kSamplerTypes[i] == kSamplerExternal_GrSLType) {
2988 fshaderTxt.appendf("#extension %s : require\n",
2989 this->glCaps().glslCaps()->externalTextureExtensionString());
2990 }
2991 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
2992 *this->glCaps().glslCaps(),
2993 &fshaderTxt);
2994 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
2995 vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
2996 fshaderTxt.append(";");
2997 uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
2998 fshaderTxt.append(";");
2999 const char* fsOutName;
3000 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3001 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3002 fshaderTxt.append(";");
3003 fsOutName = oFragColor.c_str();
3004 } else {
3005 fsOutName = "gl_FragColor";
3006 }
3007 fshaderTxt.appendf(
3008 "// Copy Program FS\n"
3009 "void main() {"
3010 " %s = %s(u_texture, v_texCoord);"
3011 "}",
3012 fsOutName,
bsalomone5286e02016-01-14 09:24:09 -08003013 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[i], this->glslGeneration())
bsalomon7ea33f52015-11-22 14:51:00 -08003014 );
3015
3016 GL_CALL_RET(fCopyPrograms[i].fProgram, CreateProgram());
3017 const char* str;
3018 GrGLint length;
bsalomon0315dbc2015-11-20 20:24:31 -08003019
bsalomon7ea33f52015-11-22 14:51:00 -08003020 str = vshaderTxt.c_str();
3021 length = SkToInt(vshaderTxt.size());
3022 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3023 GR_GL_VERTEX_SHADER, &str, &length, 1,
3024 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003025
bsalomon7ea33f52015-11-22 14:51:00 -08003026 str = fshaderTxt.c_str();
3027 length = SkToInt(fshaderTxt.size());
3028 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3029 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3030 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003031
bsalomon7ea33f52015-11-22 14:51:00 -08003032 GL_CALL(LinkProgram(fCopyPrograms[i].fProgram));
bsalomon0315dbc2015-11-20 20:24:31 -08003033
bsalomon7ea33f52015-11-22 14:51:00 -08003034 GL_CALL_RET(fCopyPrograms[i].fTextureUniform,
3035 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texture"));
3036 GL_CALL_RET(fCopyPrograms[i].fPosXformUniform,
3037 GetUniformLocation(fCopyPrograms[i].fProgram, "u_posXform"));
3038 GL_CALL_RET(fCopyPrograms[i].fTexCoordXformUniform,
3039 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texCoordXform"));
bsalomon0315dbc2015-11-20 20:24:31 -08003040
bsalomon7ea33f52015-11-22 14:51:00 -08003041 GL_CALL(BindAttribLocation(fCopyPrograms[i].fProgram, 0, "a_vertex"));
bsalomon0315dbc2015-11-20 20:24:31 -08003042
bsalomon7ea33f52015-11-22 14:51:00 -08003043 GL_CALL(DeleteShader(vshader));
3044 GL_CALL(DeleteShader(fshader));
3045 }
kkinnunen546eb5c2015-12-11 00:05:33 -08003046 fCopyProgramArrayBuffer = 0;
bsalomon7ea33f52015-11-22 14:51:00 -08003047 GL_CALL(GenBuffers(1, &fCopyProgramArrayBuffer));
3048 fHWGeometryState.setVertexBufferID(this, fCopyProgramArrayBuffer);
bsalomon6df86402015-06-01 10:41:49 -07003049 static const GrGLfloat vdata[] = {
3050 0, 0,
3051 0, 1,
3052 1, 0,
3053 1, 1
3054 };
3055 GL_ALLOC_CALL(this->glInterface(),
3056 BufferData(GR_GL_ARRAY_BUFFER,
3057 (GrGLsizeiptr) sizeof(vdata),
3058 vdata, // data ptr
3059 GR_GL_STATIC_DRAW));
3060}
3061
bsalomon6dea83f2015-12-03 12:58:06 -08003062void GrGLGpu::createWireRectProgram() {
3063 SkASSERT(!fWireRectProgram.fProgram);
3064 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3065 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3066 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3067 const char* version = this->glCaps().glslCaps()->versionDeclString();
3068
3069 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3070 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3071 // uniform using the 4 vbo vertices.
3072 SkString vshaderTxt(version);
3073 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3074 vshaderTxt.append(";");
3075 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3076 vshaderTxt.append(";");
3077 vshaderTxt.append(
3078 "// Wire Rect Program VS\n"
3079 "void main() {"
3080 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3081 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3082 " gl_Position.zw = vec2(0, 1);"
3083 "}"
3084 );
3085
3086 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
3087
3088 SkString fshaderTxt(version);
3089 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3090 *this->glCaps().glslCaps(),
3091 &fshaderTxt);
3092 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3093 fshaderTxt.append(";");
3094 const char* fsOutName;
3095 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3096 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3097 fshaderTxt.append(";");
3098 fsOutName = oFragColor.c_str();
3099 } else {
3100 fsOutName = "gl_FragColor";
3101 }
3102 fshaderTxt.appendf(
3103 "// Write Rect Program FS\n"
3104 "void main() {"
3105 " %s = %s;"
3106 "}",
3107 fsOutName,
3108 uColor.c_str()
3109 );
3110
3111 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3112 const char* str;
3113 GrGLint length;
3114
3115 str = vshaderTxt.c_str();
3116 length = SkToInt(vshaderTxt.size());
3117 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3118 GR_GL_VERTEX_SHADER, &str, &length, 1,
3119 &fStats);
3120
3121 str = fshaderTxt.c_str();
3122 length = SkToInt(fshaderTxt.size());
3123 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3124 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3125 &fStats);
3126
3127 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3128
3129 GL_CALL_RET(fWireRectProgram.fColorUniform,
3130 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3131 GL_CALL_RET(fWireRectProgram.fRectUniform,
3132 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3133 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3134
3135 GL_CALL(DeleteShader(vshader));
3136 GL_CALL(DeleteShader(fshader));
3137 GL_CALL(GenBuffers(1, &fWireRectArrayBuffer));
3138 fHWGeometryState.setVertexBufferID(this, fWireRectArrayBuffer);
3139 static const GrGLfloat vdata[] = {
3140 0, 0,
3141 0, 1,
3142 1, 1,
3143 1, 0,
3144 };
3145 GL_ALLOC_CALL(this->glInterface(),
3146 BufferData(GR_GL_ARRAY_BUFFER,
3147 (GrGLsizeiptr) sizeof(vdata),
3148 vdata, // data ptr
3149 GR_GL_STATIC_DRAW));
3150}
3151
3152void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003153 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3154 // visualization.
3155
bsalomon6dea83f2015-12-03 12:58:06 -08003156 this->handleDirtyContext();
3157 if (!fWireRectProgram.fProgram) {
3158 this->createWireRectProgram();
3159 }
3160
3161 int w = rt->width();
3162 int h = rt->height();
3163
3164 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3165 // whether the render target is flipped or not.
3166 GrGLfloat edges[4];
3167 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3168 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3169 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3170 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3171 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3172 } else {
3173 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3174 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3175 }
3176 edges[0] = 2 * edges[0] / w - 1.0f;
3177 edges[1] = 2 * edges[1] / h - 1.0f;
3178 edges[2] = 2 * edges[2] / w - 1.0f;
3179 edges[3] = 2 * edges[3] / h - 1.0f;
3180
3181 GrGLfloat channels[4];
3182 static const GrGLfloat scale255 = 1.f / 255.f;
3183 channels[0] = GrColorUnpackR(color) * scale255;
3184 channels[1] = GrColorUnpackG(color) * scale255;
3185 channels[2] = GrColorUnpackB(color) * scale255;
3186 channels[3] = GrColorUnpackA(color) * scale255;
3187
3188 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3189 this->flushRenderTarget(glRT, &rect);
3190
3191 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3192 fHWProgramID = fWireRectProgram.fProgram;
3193
3194 fHWGeometryState.setVertexArrayID(this, 0);
3195
3196 GrGLAttribArrayState* attribs =
3197 fHWGeometryState.bindArrayAndBufferToDraw(this, fWireRectArrayBuffer);
3198 attribs->set(this, 0, fWireRectArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0);
3199 attribs->disableUnusedArrays(this, 0x1);
3200
3201 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3202 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3203
3204 GrXferProcessor::BlendInfo blendInfo;
3205 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003206 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003207 this->flushColorWrite(true);
3208 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
3209 this->flushHWAAState(glRT, false);
3210 this->disableScissor();
3211 GrStencilSettings stencil;
3212 stencil.setDisabled();
3213 this->flushStencil(stencil);
3214
3215 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3216}
3217
3218
bsalomon6df86402015-06-01 10:41:49 -07003219void GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3220 GrSurface* src,
3221 const SkIRect& srcRect,
3222 const SkIPoint& dstPoint) {
3223 int w = srcRect.width();
3224 int h = srcRect.height();
3225
3226 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3227 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
3228 this->bindTexture(0, params, srcTex);
3229
3230 GrGLRenderTarget* dstRT = static_cast<GrGLRenderTarget*>(dst->asRenderTarget());
3231 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
3232 this->flushRenderTarget(dstRT, &dstRect);
3233
bsalomon7ea33f52015-11-22 14:51:00 -08003234 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
3235
3236 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3237 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003238
3239 fHWGeometryState.setVertexArrayID(this, 0);
3240
3241 GrGLAttribArrayState* attribs =
bsalomon7ea33f52015-11-22 14:51:00 -08003242 fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgramArrayBuffer);
bsalomon6dea83f2015-12-03 12:58:06 -08003243 attribs->set(this, 0, fCopyProgramArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07003244 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003245
3246 // dst rect edges in NDC (-1 to 1)
3247 int dw = dst->width();
3248 int dh = dst->height();
3249 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3250 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3251 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3252 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3253 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3254 dy0 = -dy0;
3255 dy1 = -dy1;
3256 }
3257
bsalomone5286e02016-01-14 09:24:09 -08003258 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3259 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3260 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3261 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07003262 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003263 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003264 sy0 = sh - sy0;
3265 sy1 = sh - sy1;
3266 }
3267 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
3268 GrGLenum srcTarget = srcTex->target();
3269 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
3270 int sw = src->width();
3271 sx0 /= sw;
3272 sx1 /= sw;
3273 sy0 /= sh;
3274 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003275 }
3276
bsalomon7ea33f52015-11-22 14:51:00 -08003277 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3278 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3279 sx1 - sx0, sy1 - sy0, sx0, sy0));
3280 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003281
3282 GrXferProcessor::BlendInfo blendInfo;
3283 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003284 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003285 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003286 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdalton7e806f32015-11-11 15:16:07 -08003287 this->flushHWAAState(dstRT, false);
bsalomon6df86402015-06-01 10:41:49 -07003288 this->disableScissor();
3289 GrStencilSettings stencil;
3290 stencil.setDisabled();
3291 this->flushStencil(stencil);
3292
3293 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3294}
3295
3296void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3297 GrSurface* src,
3298 const SkIRect& srcRect,
3299 const SkIPoint& dstPoint) {
3300 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003301 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003302 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003303 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
3304 SkASSERT(dstTex);
3305 // We modified the bound FBO
3306 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3307 GrGLIRect srcGLRect;
3308 srcGLRect.setRelativeTo(srcVP,
3309 srcRect.fLeft,
3310 srcRect.fTop,
3311 srcRect.width(),
3312 srcRect.height(),
3313 src->origin());
3314
3315 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003316 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003317 GrGLint dstY;
3318 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3319 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3320 } else {
3321 dstY = dstPoint.fY;
3322 }
bsalomon10528f12015-10-14 12:54:52 -07003323 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon6df86402015-06-01 10:41:49 -07003324 dstPoint.fX, dstY,
3325 srcGLRect.fLeft, srcGLRect.fBottom,
3326 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003327 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003328}
3329
3330bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3331 GrSurface* src,
3332 const SkIRect& srcRect,
3333 const SkIPoint& dstPoint) {
3334 SkASSERT(can_blit_framebuffer(dst, src, this));
3335 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3336 srcRect.width(), srcRect.height());
3337 if (dst == src) {
3338 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3339 return false;
mtklein404b3b22015-05-18 09:29:10 -07003340 }
bsalomon5df6fee2015-05-18 06:26:15 -07003341 }
bsalomon6df86402015-06-01 10:41:49 -07003342
bsalomon6df86402015-06-01 10:41:49 -07003343 GrGLIRect dstVP;
3344 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003345 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3346 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003347 // We modified the bound FBO
3348 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3349 GrGLIRect srcGLRect;
3350 GrGLIRect dstGLRect;
3351 srcGLRect.setRelativeTo(srcVP,
3352 srcRect.fLeft,
3353 srcRect.fTop,
3354 srcRect.width(),
3355 srcRect.height(),
3356 src->origin());
3357 dstGLRect.setRelativeTo(dstVP,
3358 dstRect.fLeft,
3359 dstRect.fTop,
3360 dstRect.width(),
3361 dstRect.height(),
3362 dst->origin());
3363
3364 // BlitFrameBuffer respects the scissor, so disable it.
3365 this->disableScissor();
3366
3367 GrGLint srcY0;
3368 GrGLint srcY1;
3369 // Does the blit need to y-mirror or not?
3370 if (src->origin() == dst->origin()) {
3371 srcY0 = srcGLRect.fBottom;
3372 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3373 } else {
3374 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3375 srcY1 = srcGLRect.fBottom;
3376 }
3377 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3378 srcY0,
3379 srcGLRect.fLeft + srcGLRect.fWidth,
3380 srcY1,
3381 dstGLRect.fLeft,
3382 dstGLRect.fBottom,
3383 dstGLRect.fLeft + dstGLRect.fWidth,
3384 dstGLRect.fBottom + dstGLRect.fHeight,
3385 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07003386 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
3387 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003388 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003389}
3390
cdalton231c5fd2015-05-13 12:35:36 -07003391void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003392 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003393 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003394 case kTexture_GrXferBarrierType: {
3395 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
3396 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3397 // The render target uses separate storage so no need for glTextureBarrier.
3398 // FIXME: The render target will resolve automatically when its texture is bound,
3399 // but we could resolve only the bounds that will be read if we do it here instead.
3400 return;
3401 }
cdalton9954bc32015-04-29 14:17:00 -07003402 SkASSERT(this->caps()->textureBarrierSupport());
3403 GL_CALL(TextureBarrier());
3404 return;
cdalton231c5fd2015-05-13 12:35:36 -07003405 }
cdalton8917d622015-05-06 13:40:21 -07003406 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003407 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003408 this->caps()->blendEquationSupport());
3409 GL_CALL(BlendBarrier());
3410 return;
bsalomoncb02b382015-08-12 11:14:50 -07003411 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003412 }
3413}
3414
jvanverth88957922015-07-14 11:02:52 -07003415GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomon091f60c2015-11-10 11:54:56 -08003416 GrPixelConfig config) const {
bsalomon926cb022015-12-17 18:15:11 -08003417 if (!this->caps()->isConfigTexturable(config)) {
3418 return false;
3419 }
bsalomon091f60c2015-11-10 11:54:56 -08003420 GrGLTextureInfo* info = new GrGLTextureInfo;
3421 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08003422 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08003423 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07003424 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
3425 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08003426 GL_CALL(BindTexture(info->fTarget, info->fID));
3427 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3428 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3429 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3430 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07003431
bsalomon76148af2016-01-12 11:13:47 -08003432 GrGLenum internalFormat;
3433 GrGLenum externalFormat;
3434 GrGLenum externalType;
3435
3436 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
3437 &externalType)) {
3438 delete info;
3439#ifdef SK_IGNORE_GL_TEXTURE_TARGET
3440 return 0;
3441#else
3442 return reinterpret_cast<GrBackendObject>(nullptr);
3443#endif
3444 }
jvanverth672bb7f2015-07-13 07:19:57 -07003445
bsalomon091f60c2015-11-10 11:54:56 -08003446 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07003447 externalType, pixels));
3448
bsalomon091f60c2015-11-10 11:54:56 -08003449#ifdef SK_IGNORE_GL_TEXTURE_TARGET
3450 GrGLuint id = info->fID;
3451 delete info;
3452 return id;
3453#else
3454 return reinterpret_cast<GrBackendObject>(info);
3455#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003456}
3457
jvanverth88957922015-07-14 11:02:52 -07003458bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08003459#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003460 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003461#else
3462 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
3463#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003464
3465 GrGLboolean result;
3466 GL_CALL_RET(result, IsTexture(texID));
3467
3468 return (GR_GL_TRUE == result);
3469}
3470
bsalomon67d76202015-11-11 12:40:42 -08003471void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) const {
bsalomon091f60c2015-11-10 11:54:56 -08003472#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003473 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003474#else
3475 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
3476 GrGLuint texID = info->fID;
3477#endif
3478
bsalomon67d76202015-11-11 12:40:42 -08003479 if (!abandonTexture) {
3480 GL_CALL(DeleteTextures(1, &texID));
3481 }
bsalomon091f60c2015-11-10 11:54:56 -08003482
3483#ifndef SK_IGNORE_GL_TEXTURE_TARGET
3484 delete info;
3485#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003486}
3487
joshualitt8fd844f2015-12-02 13:36:47 -08003488void GrGLGpu::resetShaderCacheForTesting() const {
3489 fProgramCache->abandon();
3490}
3491
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003492///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -08003493GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
3494 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00003495 const GrGLVertexBuffer* vbuffer,
3496 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07003497 SkASSERT(vbuffer);
bsalomon6df86402015-06-01 10:41:49 -07003498 GrGLuint vbufferID = vbuffer->bufferID();
halcanary96fcdcc2015-08-27 07:41:13 -07003499 GrGLuint* ibufferIDPtr = nullptr;
bsalomon6df86402015-06-01 10:41:49 -07003500 GrGLuint ibufferID;
3501 if (ibuffer) {
3502 ibufferID = ibuffer->bufferID();
3503 ibufferIDPtr = &ibufferID;
3504 }
3505 return this->internalBind(gpu, vbufferID, ibufferIDPtr);
3506}
3507
3508GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu,
3509 GrGLuint vbufferID) {
halcanary96fcdcc2015-08-27 07:41:13 -07003510 return this->internalBind(gpu, vbufferID, nullptr);
bsalomon6df86402015-06-01 10:41:49 -07003511}
3512
3513GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu,
3514 GrGLuint vbufferID,
3515 GrGLuint ibufferID) {
3516 return this->internalBind(gpu, vbufferID, &ibufferID);
3517}
3518
3519GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu,
3520 GrGLuint vbufferID,
3521 GrGLuint* ibufferID) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003522 GrGLAttribArrayState* attribState;
3523
bsalomon6df86402015-06-01 10:41:49 -07003524 if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) {
bsalomon8780bc62015-05-13 09:56:37 -07003525 if (!fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003526 GrGLuint arrayID;
3527 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3528 int attrCount = gpu->glCaps().maxVertexAttributes();
halcanary385fe4d2015-08-26 13:07:48 -07003529 fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003530 }
bsalomon6df86402015-06-01 10:41:49 -07003531 if (ibufferID) {
3532 attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID);
3533 } else {
3534 attribState = fVBOVertexArray->bind(gpu);
3535 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003536 } else {
bsalomon6df86402015-06-01 10:41:49 -07003537 if (ibufferID) {
3538 this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003539 } else {
3540 this->setVertexArrayID(gpu, 0);
3541 }
3542 int attrCount = gpu->glCaps().maxVertexAttributes();
3543 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3544 fDefaultVertexArrayAttribState.resize(attrCount);
3545 }
3546 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003547 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003548 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003549}
bsalomone179a912016-01-20 06:18:10 -08003550
3551bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
3552 GrTextureProducer::CopyParams* copyParams) const {
3553 if (textureParams.isTiled() ||
3554 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
3555 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
3556 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
3557 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
3558 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
3559 copyParams->fWidth = texture->width();
3560 copyParams->fHeight = texture->height();
3561 return true;
3562 }
3563 }
3564 return false;
3565}