blob: ad85707b3bf47636fd8f09bb4b5452e9887f767e [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
jvanverth39edf762014-12-22 11:44:19 -08008#include "GrGLGpu.h"
jvanverthcba99b82015-06-24 06:59:57 -07009#include "GrGLGLSL.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070010#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080011#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
egdaniel8dd688b2015-01-22 10:16:09 -080013#include "GrPipeline.h"
ethannicholas22793252016-01-30 09:59:10 -080014#include "GrPLSGeometryProcessor.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"
ethannicholas22793252016-01-30 09:59:10 -080023#include "glsl/GrGLSLPLSPathRendering.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000024#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000025#include "SkTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000028#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000029
reed@google.comac10a2d2010-12-22 21:39:39 +000030#define SKIP_CACHE_CHECK true
31
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000032#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
33 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
34 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
35 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000037 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
38 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
39 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
40#endif
41
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;
ethannicholas22793252016-01-30 09:59:10 -0800227 if (this->glCaps().shaderCaps()->plsPathRenderingSupport()) {
228 this->createPLSSetupProgram();
229 }
230 else {
231 memset(&fPLSSetupProgram, 0, sizeof(fPLSSetupProgram));
232 }
233 fHWPLSEnabled = false;
234 fPLSHasBeenUsed = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000235}
236
bsalomon861e1032014-12-16 07:33:49 -0800237GrGLGpu::~GrGLGpu() {
kkinnunen702501d2016-01-13 23:36:45 -0800238 // Delete the path rendering explicitly, since it will need working gpu object to release the
239 // resources the object itself holds.
240 fPathRendering.reset();
241
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000242 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000243 // detach the current program so there is no confusion on OpenGL's part
244 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000245 GL_CALL(UseProgram(0));
246 }
247
egdanield803f272015-03-18 13:01:52 -0700248 if (0 != fTempSrcFBOID) {
249 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800250 }
egdanield803f272015-03-18 13:01:52 -0700251 if (0 != fTempDstFBOID) {
252 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800253 }
egdanield803f272015-03-18 13:01:52 -0700254 if (0 != fStencilClearFBOID) {
255 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800256 }
egdaniel0f5f9672015-02-03 11:10:51 -0800257
bsalomon7ea33f52015-11-22 14:51:00 -0800258 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
259 if (0 != fCopyPrograms[i].fProgram) {
260 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
261 }
bsalomon6df86402015-06-01 10:41:49 -0700262 }
bsalomon6dea83f2015-12-03 12:58:06 -0800263
bsalomon7ea33f52015-11-22 14:51:00 -0800264 if (0 != fCopyProgramArrayBuffer) {
265 GL_CALL(DeleteBuffers(1, &fCopyProgramArrayBuffer));
bsalomon6df86402015-06-01 10:41:49 -0700266 }
267
bsalomon6dea83f2015-12-03 12:58:06 -0800268 if (0 != fWireRectProgram.fProgram) {
269 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
270 }
271
272 if (0 != fWireRectArrayBuffer) {
273 GL_CALL(DeleteBuffers(1, &fWireRectArrayBuffer));
274 }
275
ethannicholas22793252016-01-30 09:59:10 -0800276 if (0 != fPLSSetupProgram.fArrayBuffer) {
277 GL_CALL(DeleteBuffers(1, &fPLSSetupProgram.fArrayBuffer));
278 }
279
280 if (0 != fPLSSetupProgram.fProgram) {
281 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
282 }
283
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000284 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700285}
286
ethannicholas22793252016-01-30 09:59:10 -0800287void GrGLGpu::createPLSSetupProgram() {
288 const char* version = this->glCaps().glslCaps()->versionDeclString();
289
290 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
291 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
292 GrShaderVar::kUniform_TypeModifier);
293 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
294 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
295 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
296
297 SkString vshaderTxt(version);
298 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
299 vshaderTxt.append(";");
300 uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
301 vshaderTxt.append(";");
302 uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
303 vshaderTxt.append(";");
304 vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
305 vshaderTxt.append(";");
306
307 vshaderTxt.append(
308 "// PLS Setup Program VS\n"
309 "void main() {"
310 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
311 " gl_Position.zw = vec2(0, 1);"
312 "}"
313 );
314
315 SkString fshaderTxt(version);
316 fshaderTxt.append("#extension ");
317 fshaderTxt.append(this->glCaps().glslCaps()->fbFetchExtensionString());
318 fshaderTxt.append(" : require\n");
319 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
320 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
321 *this->glCaps().glslCaps(),
322 &fshaderTxt);
323 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
324 vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
325 fshaderTxt.append(";");
326 uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
327 fshaderTxt.append(";");
328
329 fshaderTxt.appendf(
330 "// PLS Setup Program FS\n"
331 GR_GL_PLS_PATH_DATA_DECL
332 "void main() {\n"
333 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
334 " pls.windings = ivec4(0, 0, 0, 0);\n"
335 "}"
336 );
337 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
338 const char* str;
339 GrGLint length;
340
341 str = vshaderTxt.c_str();
342 length = SkToInt(vshaderTxt.size());
343 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
344 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
345
346 str = fshaderTxt.c_str();
347 length = SkToInt(fshaderTxt.size());
348 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
349 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
350
351 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
352
353 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
354 "u_posXform"));
355
356 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
357
358 GL_CALL(DeleteShader(vshader));
359 GL_CALL(DeleteShader(fshader));
360
361 GL_CALL(GenBuffers(1, &fPLSSetupProgram.fArrayBuffer));
362 fHWGeometryState.setVertexBufferID(this, fPLSSetupProgram.fArrayBuffer);
363 static const GrGLfloat vdata[] = {
364 0, 0,
365 0, 1,
366 1, 0,
367 1, 1
368 };
369 GL_ALLOC_CALL(this->glInterface(),
370 BufferData(GR_GL_ARRAY_BUFFER,
371 (GrGLsizeiptr) sizeof(vdata),
372 vdata, // data ptr
373 GR_GL_STATIC_DRAW));
374}
375
bsalomon861e1032014-12-16 07:33:49 -0800376void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700377 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700378 fProgramCache->abandon();
379 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700380 fTempSrcFBOID = 0;
381 fTempDstFBOID = 0;
382 fStencilClearFBOID = 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800383 fCopyProgramArrayBuffer = 0;
384 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
385 fCopyPrograms[i].fProgram = 0;
386 }
bsalomon6dea83f2015-12-03 12:58:06 -0800387 fWireRectProgram.fProgram = 0;
388 fWireRectArrayBuffer = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700389 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700390 this->glPathRendering()->abandonGpuResources();
391 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000392}
393
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000394///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000395
bsalomon861e1032014-12-16 07:33:49 -0800396void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000397 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000398 if (resetBits & kMisc_GrGLBackendState) {
399 GL_CALL(Disable(GR_GL_DEPTH_TEST));
400 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000401
egdaniel8dd688b2015-01-22 10:16:09 -0800402 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000403
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000404 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000405 // Desktop-only state that we never change
406 if (!this->glCaps().isCoreProfile()) {
407 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
408 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
409 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
410 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
411 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
412 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
413 }
414 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
415 // core profile. This seems like a bug since the core spec removes any mention of
416 // GL_ARB_imaging.
417 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
418 GL_CALL(Disable(GR_GL_COLOR_TABLE));
419 }
420 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
421 // Since ES doesn't support glPointSize at all we always use the VS to
422 // set the point size
423 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
424
425 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
426 // currently part of our gl interface. There are probably others as
427 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000428 }
joshualitt58162332014-08-01 06:44:53 -0700429
430 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700431 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700432 // The arm extension requires specifically enabling MSAA fetching per sample.
433 // On some devices this may have a perf hit. Also multiple render targets are disabled
434 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
435 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000436 fHWWriteToColor = kUnknown_TriState;
437 // we only ever use lines in hairline mode
438 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700439 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000440 }
edisonn@google.comba669992013-06-28 16:03:21 +0000441
egdanielb414f252014-07-29 13:15:47 -0700442 if (resetBits & kMSAAEnable_GrGLBackendState) {
443 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700444
egdanieleed519e2016-01-15 11:36:18 -0800445 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800446 if (0 != this->caps()->maxRasterSamples()) {
447 fHWRasterMultisampleEnabled = kUnknown_TriState;
448 fHWNumRasterSamples = 0;
449 }
450
451 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
452 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700453 GL_CALL(CoverageModulation(GR_GL_RGBA));
454 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000455 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000456
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000457 fHWActiveTextureUnitIdx = -1; // invalid
458
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000459 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700460 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
461 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000462 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000463 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000464
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000465 if (resetBits & kBlend_GrGLBackendState) {
466 fHWBlendState.invalidate();
467 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000468
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000469 if (resetBits & kView_GrGLBackendState) {
470 fHWScissorSettings.invalidate();
471 fHWViewport.invalidate();
472 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000474 if (resetBits & kStencil_GrGLBackendState) {
475 fHWStencilSettings.invalidate();
476 fHWStencilTestEnabled = kUnknown_TriState;
477 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000478
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000479 // Vertex
480 if (resetBits & kVertex_GrGLBackendState) {
481 fHWGeometryState.invalidate();
482 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000484 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700485 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700486 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000488
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000489 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700490 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700491 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000493 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000494
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000495 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 if (resetBits & kPixelStore_GrGLBackendState) {
497 if (this->glCaps().unpackRowLengthSupport()) {
498 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
499 }
500 if (this->glCaps().packRowLengthSupport()) {
501 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
502 }
503 if (this->glCaps().unpackFlipYSupport()) {
504 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
505 }
506 if (this->glCaps().packFlipYSupport()) {
507 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
508 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000509 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000510
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000511 if (resetBits & kProgram_GrGLBackendState) {
512 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000513 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000514}
515
egdanielcf614fd2015-04-22 13:58:58 -0700516static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000517 // By default, GrRenderTargets are GL's normal orientation so that they
518 // can be drawn to by the outside world without the client having
519 // to render upside down.
520 if (kDefault_GrSurfaceOrigin == origin) {
521 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
522 } else {
523 return origin;
524 }
525}
526
bsalomon6dc6f5f2015-06-18 09:12:16 -0700527GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
528 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800529#ifdef SK_IGNORE_GL_TEXTURE_TARGET
530 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700531 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000532 }
bsalomon091f60c2015-11-10 11:54:56 -0800533#else
534 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
535 if (!info || !info->fID) {
536 return nullptr;
537 }
538#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000539
bsalomon@google.combcce8922013-03-25 15:38:39 +0000540 int maxSize = this->caps()->maxTextureSize();
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000541 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
halcanary96fcdcc2015-08-27 07:41:13 -0700542 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000543 }
544
bsalomon7ea33f52015-11-22 14:51:00 -0800545 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
546 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
547
bsalomonb15b4c12014-10-29 12:41:57 -0700548 GrGLTexture::IDDesc idDesc;
549 GrSurfaceDesc surfDesc;
550
bsalomon091f60c2015-11-10 11:54:56 -0800551#ifdef SK_IGNORE_GL_TEXTURE_TARGET
552 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
bsalomon10528f12015-10-14 12:54:52 -0700553 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -0800554 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
555#else
556 idDesc.fInfo = *info;
557#endif
bsalomone5286e02016-01-14 09:24:09 -0800558
bsalomon7ea33f52015-11-22 14:51:00 -0800559 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
560 if (renderTarget) {
561 // This combination is not supported.
562 return nullptr;
563 }
564 if (!this->glCaps().externalTextureSupport()) {
565 return nullptr;
566 }
bsalomone5286e02016-01-14 09:24:09 -0800567 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
568 if (!this->glCaps().rectangleTextureSupport()) {
569 return nullptr;
570 }
571 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
572 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800573 }
bsalomone5286e02016-01-14 09:24:09 -0800574
575 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800576 // for a render buffer that resolves to the texture. We don't support MSAA textures.
577 if (desc.fSampleCnt && !renderTarget) {
578 return nullptr;
579 }
bsalomon10528f12015-10-14 12:54:52 -0700580
bsalomon6dc6f5f2015-06-18 09:12:16 -0700581 switch (ownership) {
582 case kAdopt_GrWrapOwnership:
583 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
584 break;
585 case kBorrow_GrWrapOwnership:
586 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
587 break;
bsalomone5286e02016-01-14 09:24:09 -0800588 }
bsalomonb15b4c12014-10-29 12:41:57 -0700589
bsalomonb15b4c12014-10-29 12:41:57 -0700590 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
591 surfDesc.fWidth = desc.fWidth;
592 surfDesc.fHeight = desc.fHeight;
593 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700594 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000595 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
596 // assuming the old behaviour, which is that backend textures are always
597 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
598 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
599 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700600 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000601 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700602 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000603 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000604
halcanary96fcdcc2015-08-27 07:41:13 -0700605 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000606 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700607 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700608 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800609 idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700610 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000611 }
halcanary385fe4d2015-08-26 13:07:48 -0700612 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000613 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700614 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000615 }
halcanary96fcdcc2015-08-27 07:41:13 -0700616 if (nullptr == texture) {
617 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000618 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000619
bsalomon@google.come269f212011-11-07 13:29:52 +0000620 return texture;
621}
622
bsalomon6dc6f5f2015-06-18 09:12:16 -0700623GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
624 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700625 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700626 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700627 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700628 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700629 switch (ownership) {
630 case kAdopt_GrWrapOwnership:
631 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
632 break;
633 case kBorrow_GrWrapOwnership:
634 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
635 break;
cblume61214052016-01-26 09:10:48 -0800636 }
senorblancod7395d82015-06-18 13:26:52 -0700637 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000638
bsalomonb15b4c12014-10-29 12:41:57 -0700639 GrSurfaceDesc desc;
640 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700641 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700642 desc.fWidth = wrapDesc.fWidth;
643 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700644 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700645 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
646
egdanielec00d942015-09-14 12:56:10 -0700647 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000648}
649
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000650////////////////////////////////////////////////////////////////////////////////
bsalomonf0674512015-07-28 13:26:15 -0700651bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
652 size_t rowBytes, GrPixelConfig srcConfig,
653 DrawPreference* drawPreference,
654 WritePixelTempDrawInfo* tempDrawInfo) {
655 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
656 return false;
657 }
658
bsalomon6cb3cbe2015-07-30 07:34:27 -0700659 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
660 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
661 if (!dstSurface->asTexture()) {
662 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800663 } else {
664 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800665 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
666 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800667 return false;
668 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700669 }
670
bsalomon16921ec2015-07-30 15:34:56 -0700671 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
672 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
673 }
674
bsalomon6c9cd552016-01-22 07:17:34 -0800675 // Start off assuming no swizzling
676 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
677 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700678
679 // These settings we will always want if a temp draw is performed. Initially set the config
680 // to srcConfig, though that may be modified if we decide to do a R/G swap.
681 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
682 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
683 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
684 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
685 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
686 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
687
688 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
689
690 if (configsAreRBSwaps) {
691 if (!this->caps()->isConfigTexturable(srcConfig)) {
692 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
693 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800694 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
695 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700696 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
697 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700698 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
699 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800700 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
701 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700702 } else if (kGLES_GrGLStandard == this->glStandard() &&
703 this->glCaps().bgraIsInternalFormat()) {
704 // The internal format and external formats must match texture uploads so we can't
705 // swizzle while uploading when BGRA is a distinct internal format.
706 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
707 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800708 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
709 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700710 }
711 }
712
713 if (!this->glCaps().unpackFlipYSupport() &&
714 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
715 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
716 }
717
718 return true;
719}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000720
jvanverth17aa0472016-01-05 10:41:27 -0800721static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
722 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700723 if (!glTex) {
724 return false;
725 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000726
bsalomon16921ec2015-07-30 15:34:56 -0700727 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
728 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
729 return false;
730 }
731
bsalomone5286e02016-01-14 09:24:09 -0800732 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
733 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800734 return false;
735 }
736
jvanverth17aa0472016-01-05 10:41:27 -0800737 return true;
738}
739
740bool GrGLGpu::onWritePixels(GrSurface* surface,
741 int left, int top, int width, int height,
742 GrPixelConfig config, const void* buffer,
743 size_t rowBytes) {
744 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
745
746 if (!check_write_and_transfer_input(glTex, surface, config)) {
747 return false;
748 }
749
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000750 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700751 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000752
krajcevski145d48c2014-06-11 16:07:50 -0700753 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700754 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800755 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700756 SkASSERT(config == glTex->desc().fConfig);
jvanverth17aa0472016-01-05 10:41:27 -0800757 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), buffer,
758 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700759 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800760 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
761 left, top, width, height, config, buffer, rowBytes);
krajcevski145d48c2014-06-11 16:07:50 -0700762 }
763
764 if (success) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700765 glTex->texturePriv().dirtyMipMaps(true);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000766 return true;
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +0000767 }
krajcevski145d48c2014-06-11 16:07:50 -0700768
769 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000770}
771
jvanverth17aa0472016-01-05 10:41:27 -0800772bool GrGLGpu::onTransferPixels(GrSurface* surface,
773 int left, int top, int width, int height,
774 GrPixelConfig config, GrTransferBuffer* buffer,
775 size_t offset, size_t rowBytes) {
776 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
777
778 if (!check_write_and_transfer_input(glTex, surface, config)) {
779 return false;
780 }
781
782 // For the moment, can't transfer compressed data
783 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
784 return false;
785 }
786
787 this->setScratchTextureUnit();
788 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
789
790 SkASSERT(!buffer->isMapped());
791 GrGLTransferBuffer* glBuffer = reinterpret_cast<GrGLTransferBuffer*>(buffer);
792 // bind the transfer buffer
793 SkASSERT(GR_GL_PIXEL_UNPACK_BUFFER == glBuffer->bufferType() ||
794 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM == glBuffer->bufferType());
795 GL_CALL(BindBuffer(glBuffer->bufferType(), glBuffer->bufferID()));
796
797 bool success = false;
jcgregorioe7d7f902016-02-04 19:51:25 -0800798 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
799 left, top, width, height, config, buffer, rowBytes);
jvanverth17aa0472016-01-05 10:41:27 -0800800
801 if (success) {
802 glTex->texturePriv().dirtyMipMaps(true);
803 return true;
804 }
805
806 return false;
807}
808
bsalomonf46a1242015-12-15 12:37:38 -0800809// For GL_[UN]PACK_ALIGNMENT.
810static inline GrGLint config_alignment(GrPixelConfig config) {
811 SkASSERT(!GrPixelConfigIsCompressed(config));
812 switch (config) {
813 case kAlpha_8_GrPixelConfig:
814 return 1;
815 case kRGB_565_GrPixelConfig:
816 case kRGBA_4444_GrPixelConfig:
817 case kAlpha_half_GrPixelConfig:
818 case kRGBA_half_GrPixelConfig:
819 return 2;
820 case kRGBA_8888_GrPixelConfig:
821 case kBGRA_8888_GrPixelConfig:
822 case kSRGBA_8888_GrPixelConfig:
823 case kRGBA_float_GrPixelConfig:
824 return 4;
825 default:
826 return 0;
827 }
828}
829
bsalomonb15b4c12014-10-29 12:41:57 -0700830static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
831 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700832 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000833 return GR_GL_GET_ERROR(interface);
834 } else {
835 return CHECK_ALLOC_ERROR(interface);
836 }
837}
838
bsalomon861e1032014-12-16 07:33:49 -0800839bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800840 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800841 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000842 int left, int top, int width, int height,
843 GrPixelConfig dataConfig,
jvanverth17aa0472016-01-05 10:41:27 -0800844 const void* dataOrOffset,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000845 size_t rowBytes) {
jvanverth17aa0472016-01-05 10:41:27 -0800846 SkASSERT(dataOrOffset || kNewTexture_UploadType == uploadType ||
847 kTransfer_UploadType == uploadType);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000848
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000849 // If we're uploading compressed data then we should be using uploadCompressedTexData
850 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
851
bsalomon5b30c6f2015-12-17 14:17:34 -0800852 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
853
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000854 size_t bpp = GrBytesPerPixel(dataConfig);
bsalomone8d21e82015-07-16 08:23:13 -0700855 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
jvanverth17aa0472016-01-05 10:41:27 -0800856 &width, &height, &dataOrOffset, &rowBytes)) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000857 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000858 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000859 size_t trimRowBytes = width * bpp;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000860
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000861 // in case we need a temporary, trimmed copy of the src pixels
joshualitt29f86792015-05-29 08:06:48 -0700862 SkAutoSMalloc<128 * 128> tempStorage;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000863
bsalomon5b30c6f2015-12-17 14:17:34 -0800864 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -0800865 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -0800866 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800867 GrGLenum externalFormat;
868 GrGLenum externalType;
869 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
870 &externalFormat, &externalType)) {
871 return false;
872 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000873 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800874 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000875 * because our srcData has extra bytes past each row. If so, we need
876 * to trim those off here, since GL ES may not let us specify
877 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000878 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000879 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000880 bool swFlipY = false;
881 bool glFlipY = false;
jvanverth17aa0472016-01-05 10:41:27 -0800882 if (dataOrOffset) {
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +0000883 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000884 if (this->glCaps().unpackFlipYSupport()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000885 glFlipY = true;
886 } else {
887 swFlipY = true;
888 }
889 }
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000890 if (this->glCaps().unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000891 // can't use this for flipping, only non-neg values allowed. :(
892 if (rowBytes != trimRowBytes) {
893 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
894 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
895 restoreGLRowLength = true;
896 }
jvanverth17aa0472016-01-05 10:41:27 -0800897 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000898 if (trimRowBytes != rowBytes || swFlipY) {
899 // copy data into our new storage, skipping the trailing bytes
900 size_t trimSize = height * trimRowBytes;
jvanverth17aa0472016-01-05 10:41:27 -0800901 const char* src = (const char*)dataOrOffset;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000902 if (swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000903 src += (height - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000904 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000905 char* dst = (char*)tempStorage.reset(trimSize);
906 for (int y = 0; y < height; y++) {
907 memcpy(dst, src, trimRowBytes);
908 if (swFlipY) {
909 src -= rowBytes;
910 } else {
911 src += rowBytes;
912 }
913 dst += trimRowBytes;
914 }
915 // now point data to our copied version
jvanverth17aa0472016-01-05 10:41:27 -0800916 dataOrOffset = tempStorage.get();
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000917 }
jvanverth17aa0472016-01-05 10:41:27 -0800918 } else {
919 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000920 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000921 if (glFlipY) {
922 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
923 }
bsalomonf46a1242015-12-15 12:37:38 -0800924 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(dataConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000925 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000926 bool succeeded = true;
jvanverth17aa0472016-01-05 10:41:27 -0800927 if (kNewTexture_UploadType == uploadType) {
928 if (dataOrOffset &&
929 !(0 == left && 0 == top && desc.fWidth == width && desc.fHeight == height)) {
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000930 succeeded = false;
931 } else {
erikchen7fec91c2016-02-05 12:10:55 -0800932 if (desc.fTextureStorageAllocator.fAllocateTextureStorage) {
933 if (dataOrOffset) {
934 GL_CALL(TexSubImage2D(target,
935 0, // level
936 left, top,
937 width, height,
938 externalFormat, externalType, dataOrOffset));
939 }
940 } else {
941 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
942 GL_ALLOC_CALL(this->glInterface(), TexImage2D(
943 target, 0, internalFormat, desc.fWidth, desc.fHeight, 0, externalFormat,
944 externalType, dataOrOffset));
945 GrGLenum error = check_alloc_error(desc, this->glInterface());
946 if (error != GR_GL_NO_ERROR) {
947 succeeded = false;
948 }
949 }
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000950 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000951 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000952 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000953 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000954 }
jcgregorioe7d7f902016-02-04 19:51:25 -0800955 GL_CALL(TexSubImage2D(target,
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000956 0, // level
957 left, top,
958 width, height,
jvanverth17aa0472016-01-05 10:41:27 -0800959 externalFormat, externalType, dataOrOffset));
bsalomon@google.com6f379512011-11-16 20:36:03 +0000960 }
961
962 if (restoreGLRowLength) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000963 SkASSERT(this->glCaps().unpackRowLengthSupport());
bsalomon@google.com6f379512011-11-16 20:36:03 +0000964 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000965 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000966 if (glFlipY) {
967 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
968 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +0000969 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000970}
971
krajcevski145d48c2014-06-11 16:07:50 -0700972// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -0700973// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -0700974// create a CompressedTexData struct that takes a desc/ptr and figures out
975// the proper upload semantics. Then users can construct this function how they
976// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -0800977bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700978 GrGLenum target,
krajcevski145d48c2014-06-11 16:07:50 -0700979 const void* data,
jvanverth17aa0472016-01-05 10:41:27 -0800980 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700981 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -0800982 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
jvanverth17aa0472016-01-05 10:41:27 -0800983 SkASSERT(kTransfer_UploadType != uploadType &&
984 (data || kNewTexture_UploadType != uploadType));
krajcevski9c0e6292014-06-02 07:38:14 -0700985
986 // No support for software flip y, yet...
987 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
988
krajcevski145d48c2014-06-11 16:07:50 -0700989 if (-1 == width) {
990 width = desc.fWidth;
991 }
992#ifdef SK_DEBUG
993 else {
994 SkASSERT(width <= desc.fWidth);
995 }
996#endif
997
998 if (-1 == height) {
999 height = desc.fHeight;
1000 }
1001#ifdef SK_DEBUG
1002 else {
1003 SkASSERT(height <= desc.fHeight);
1004 }
1005#endif
1006
krajcevski9c0e6292014-06-02 07:38:14 -07001007 // Make sure that the width and height that we pass to OpenGL
1008 // is a multiple of the block size.
bsalomon98806072014-12-12 15:11:17 -08001009 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, width, height);
krajcevski9c0e6292014-06-02 07:38:14 -07001010
bsalomon76148af2016-01-12 11:13:47 -08001011 // We only need the internal format for compressed 2D textures.
1012 GrGLenum internalFormat;
1013 if (!this->glCaps().getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
1014 return false;
1015 }
krajcevski9c0e6292014-06-02 07:38:14 -07001016
jvanverth17aa0472016-01-05 10:41:27 -08001017 if (kNewTexture_UploadType == uploadType) {
bsalomond4cb9222014-08-11 14:19:09 -07001018 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
krajcevski145d48c2014-06-11 16:07:50 -07001019 GL_ALLOC_CALL(this->glInterface(),
bsalomon10528f12015-10-14 12:54:52 -07001020 CompressedTexImage2D(target,
krajcevski145d48c2014-06-11 16:07:50 -07001021 0, // level
1022 internalFormat,
1023 width, height,
1024 0, // border
bsalomon98806072014-12-12 15:11:17 -08001025 SkToInt(dataSize),
krajcevski145d48c2014-06-11 16:07:50 -07001026 data));
bsalomond4cb9222014-08-11 14:19:09 -07001027 GrGLenum error = check_alloc_error(desc, this->glInterface());
1028 if (error != GR_GL_NO_ERROR) {
1029 return false;
1030 }
krajcevski145d48c2014-06-11 16:07:50 -07001031 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001032 // Paletted textures can't be updated.
1033 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1034 return false;
1035 }
bsalomon10528f12015-10-14 12:54:52 -07001036 GL_CALL(CompressedTexSubImage2D(target,
bsalomond4cb9222014-08-11 14:19:09 -07001037 0, // level
1038 left, top,
1039 width, height,
1040 internalFormat,
bsalomon98806072014-12-12 15:11:17 -08001041 SkToInt(dataSize),
bsalomond4cb9222014-08-11 14:19:09 -07001042 data));
krajcevski145d48c2014-06-11 16:07:50 -07001043 }
krajcevski9c0e6292014-06-02 07:38:14 -07001044
bsalomond4cb9222014-08-11 14:19:09 -07001045 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001046}
1047
bsalomon424cc262015-05-22 10:37:30 -07001048static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001049 int sampleCount,
1050 GrGLenum format,
1051 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001052 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001053 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001054 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001055 case GrGLCaps::kDesktop_ARB_MSFBOType:
1056 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001057 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001058 case GrGLCaps::kES_3_0_MSFBOType:
1059 GL_ALLOC_CALL(ctx.interface(),
1060 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1061 sampleCount,
1062 format,
1063 width, height));
1064 break;
1065 case GrGLCaps::kES_Apple_MSFBOType:
1066 GL_ALLOC_CALL(ctx.interface(),
1067 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1068 sampleCount,
1069 format,
1070 width, height));
1071 break;
1072 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1073 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1074 GL_ALLOC_CALL(ctx.interface(),
1075 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1076 sampleCount,
1077 format,
1078 width, height));
1079 break;
1080 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001081 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001082 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001083 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001084 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001085}
1086
egdanielb0e1be22015-04-22 13:27:39 -07001087bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
1088 GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -08001089 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001090 GrGLRenderTarget::IDDesc* idDesc) {
1091 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001092 idDesc->fRTFBOID = 0;
1093 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -07001094 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -07001095 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
1096 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
1097 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001098
1099 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001100
bsalomona11e5fc2015-12-18 07:59:41 -08001101 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001102
bsalomonb15b4c12014-10-29 12:41:57 -07001103 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001104 goto FAILED;
1105 }
1106
egdanield803f272015-03-18 13:01:52 -07001107 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1108 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001109 goto FAILED;
1110 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001111
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001112 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1113 // the texture bound to the other. The exception is the IMG multisample extension. With this
1114 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1115 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001116 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001117 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001118 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001119 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001120 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121 goto FAILED;
1122 }
bsalomon76148af2016-01-12 11:13:47 -08001123 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1124 return false;
1125 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001126 } else {
egdanield803f272015-03-18 13:01:52 -07001127 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001128 }
1129
egdanield803f272015-03-18 13:01:52 -07001130 // below here we may bind the FBO
1131 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1132 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001133 SkASSERT(desc.fSampleCnt > 0);
1134 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001135 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001136 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001137 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001138 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001139 goto FAILED;
1140 }
egdanield803f272015-03-18 13:01:52 -07001141 fStats.incRenderTargetBinds();
1142 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1143 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001144 GR_GL_COLOR_ATTACHMENT0,
1145 GR_GL_RENDERBUFFER,
1146 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001147 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1148 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001149 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1150 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1151 goto FAILED;
1152 }
bsalomon424cc262015-05-22 10:37:30 -07001153 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 }
1155 }
egdanield803f272015-03-18 13:01:52 -07001156 fStats.incRenderTargetBinds();
1157 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158
bsalomonb15b4c12014-10-29 12:41:57 -07001159 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001160 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001161 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001162 texInfo.fTarget,
1163 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001164 } else {
egdanield803f272015-03-18 13:01:52 -07001165 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001166 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001167 texInfo.fTarget,
1168 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001169 }
bsalomonb15b4c12014-10-29 12:41:57 -07001170 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1171 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001172 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001173 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1174 goto FAILED;
1175 }
bsalomon424cc262015-05-22 10:37:30 -07001176 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 }
1178
1179 return true;
1180
1181FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001182 if (idDesc->fMSColorRenderbufferID) {
1183 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001184 }
egdanield803f272015-03-18 13:01:52 -07001185 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1186 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 }
egdanield803f272015-03-18 13:01:52 -07001188 if (idDesc->fTexFBOID) {
1189 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 }
1191 return false;
1192}
1193
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001194// good to set a break-point here to know when createTexture fails
1195static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001196// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001197 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001198}
1199
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001200#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001201static size_t as_size_t(int x) {
1202 return x;
1203}
1204#endif
1205
egdanielb0e1be22015-04-22 13:27:39 -07001206GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1207 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001208 const void* srcData, size_t rowBytes) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001209 // We fail if the MSAA was requested and is not available.
1210 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001211 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001212 return return_null_texture();
1213 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001214
bsalomonf2703d82014-10-28 14:33:06 -07001215 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001216
bsalomonb15b4c12014-10-29 12:41:57 -07001217 GrGLTexture::IDDesc idDesc;
egdanielb0e1be22015-04-22 13:27:39 -07001218 idDesc.fLifeCycle = lifeCycle;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001219 GrGLTexture::TexParams initialTexParams;
erikchen7fec91c2016-02-05 12:10:55 -08001220 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, srcData,
1221 &initialTexParams, rowBytes)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001222 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001223 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001224
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001227 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001228 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001229 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001230
bsalomon091f60c2015-11-10 11:54:56 -08001231 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) {
1232 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 return return_null_texture();
1234 }
halcanary385fe4d2015-08-26 13:07:48 -07001235 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 } else {
halcanary385fe4d2015-08-26 13:07:48 -07001237 tex = new GrGLTexture(this, desc, idDesc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001239 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001240#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001241 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
bsalomon@google.com64c4fe42011-11-05 14:51:01 +00001242 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001243#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 return tex;
1245}
1246
egdanielb0e1be22015-04-22 13:27:39 -07001247GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1248 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -08001249 const void* srcData) {
krajcevski9c0e6292014-06-02 07:38:14 -07001250 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001251 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001252 return return_null_texture();
1253 }
1254
bsalomonb15b4c12014-10-29 12:41:57 -07001255 GrGLTexture::IDDesc idDesc;
kkinnunen546eb5c2015-12-11 00:05:33 -08001256 idDesc.fInfo.fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08001257 GL_CALL(GenTextures(1, &idDesc.fInfo.fID));
egdanielb0e1be22015-04-22 13:27:39 -07001258 idDesc.fLifeCycle = lifeCycle;
bsalomon10528f12015-10-14 12:54:52 -07001259 // We only support GL_TEXTURE_2D at the moment.
bsalomon091f60c2015-11-10 11:54:56 -08001260 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
krajcevski9c0e6292014-06-02 07:38:14 -07001261
bsalomon091f60c2015-11-10 11:54:56 -08001262 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001263 return return_null_texture();
1264 }
1265
1266 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001267 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001268
1269 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1270 // drivers have a bug where an FBO won't be complete if it includes a
1271 // texture that is not mipmap complete (considering the filter in use).
1272 GrGLTexture::TexParams initialTexParams;
1273 // we only set a subset here so invalidate first
1274 initialTexParams.invalidate();
1275 initialTexParams.fMinFilter = GR_GL_NEAREST;
1276 initialTexParams.fMagFilter = GR_GL_NEAREST;
1277 initialTexParams.fWrapS = GR_GL_CLAMP_TO_EDGE;
1278 initialTexParams.fWrapT = GR_GL_CLAMP_TO_EDGE;
bsalomon091f60c2015-11-10 11:54:56 -08001279 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001280 GR_GL_TEXTURE_MAG_FILTER,
1281 initialTexParams.fMagFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001282 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001283 GR_GL_TEXTURE_MIN_FILTER,
1284 initialTexParams.fMinFilter));
bsalomon091f60c2015-11-10 11:54:56 -08001285 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001286 GR_GL_TEXTURE_WRAP_S,
1287 initialTexParams.fWrapS));
bsalomon091f60c2015-11-10 11:54:56 -08001288 GL_CALL(TexParameteri(idDesc.fInfo.fTarget,
krajcevski9c0e6292014-06-02 07:38:14 -07001289 GR_GL_TEXTURE_WRAP_T,
1290 initialTexParams.fWrapT));
1291
bsalomon091f60c2015-11-10 11:54:56 -08001292 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, srcData)) {
1293 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001294 return return_null_texture();
1295 }
1296
1297 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001298 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001299 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1300#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001301 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
krajcevski9c0e6292014-06-02 07:38:14 -07001302 glTexDesc.fTextureID, desc.fWidth, desc.fHeight, desc.fConfig);
1303#endif
1304 return tex;
1305}
1306
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001308
egdaniel8dc7c3a2015-04-16 11:22:42 -07001309const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001310
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001311void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001312 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001313
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001314 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001315 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001316 (kUnknownBitCount == format->fTotalBits));
1317 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001318 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001319 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1320 (GrGLint*)&format->fStencilBits);
1321 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001322 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001323 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1324 (GrGLint*)&format->fTotalBits);
1325 format->fTotalBits += format->fStencilBits;
1326 } else {
1327 format->fTotalBits = format->fStencilBits;
1328 }
1329 }
1330}
1331}
1332
egdanielff1d5472015-09-10 08:37:20 -07001333int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001334 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001335 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001336 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1337 // Default to unsupported, set this if we find a stencil format that works.
1338 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001339 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001340 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001341 GL_CALL(GenTextures(1, &colorID));
1342 this->setScratchTextureUnit();
1343 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1344 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1345 GR_GL_TEXTURE_MAG_FILTER,
1346 GR_GL_NEAREST));
1347 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1348 GR_GL_TEXTURE_MIN_FILTER,
1349 GR_GL_NEAREST));
1350 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1351 GR_GL_TEXTURE_WRAP_S,
1352 GR_GL_CLAMP_TO_EDGE));
1353 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1354 GR_GL_TEXTURE_WRAP_T,
1355 GR_GL_CLAMP_TO_EDGE));
1356
bsalomon76148af2016-01-12 11:13:47 -08001357 GrGLenum internalFormat;
1358 GrGLenum externalFormat;
1359 GrGLenum externalType;
1360 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1361 &externalType)) {
1362 return false;
1363 }
egdanielff1d5472015-09-10 08:37:20 -07001364 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1365 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001366 0,
bsalomon76148af2016-01-12 11:13:47 -08001367 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001368 kSize,
1369 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001370 0,
bsalomon76148af2016-01-12 11:13:47 -08001371 externalFormat,
1372 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001373 NULL));
bsalomon30447372015-12-21 09:03:05 -08001374 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001375 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001376 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001377 }
1378
1379 // unbind the texture from the texture unit before binding it to the frame buffer
1380 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1381
1382 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001383 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001384 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001385 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001386 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1387 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1388 GR_GL_COLOR_ATTACHMENT0,
1389 GR_GL_TEXTURE_2D,
1390 colorID,
1391 0));
bsalomon30447372015-12-21 09:03:05 -08001392 GrGLuint sbRBID = 0;
1393 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001394
1395 // look over formats till I find a compatible one
1396 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001397 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001398 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001399 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1400 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1401 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1402 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1403 sFmt.fInternalFormat,
1404 kSize, kSize));
1405 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001406 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001407 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001408 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001409 if (sFmt.fPacked) {
1410 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1411 GR_GL_DEPTH_ATTACHMENT,
1412 GR_GL_RENDERBUFFER, sbRBID));
1413 } else {
1414 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1415 GR_GL_DEPTH_ATTACHMENT,
1416 GR_GL_RENDERBUFFER, 0));
1417 }
1418 GrGLenum status;
1419 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1420 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1421 firstWorkingStencilFormatIndex = i;
1422 break;
1423 }
egdanielff1d5472015-09-10 08:37:20 -07001424 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1425 GR_GL_STENCIL_ATTACHMENT,
1426 GR_GL_RENDERBUFFER, 0));
1427 if (sFmt.fPacked) {
1428 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1429 GR_GL_DEPTH_ATTACHMENT,
1430 GR_GL_RENDERBUFFER, 0));
1431 }
egdanielff1d5472015-09-10 08:37:20 -07001432 }
1433 }
bsalomon30447372015-12-21 09:03:05 -08001434 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001435 }
1436 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001437 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1438 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001439 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001440 }
bsalomon30447372015-12-21 09:03:05 -08001441 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001442}
1443
erikchen7fec91c2016-02-05 12:10:55 -08001444bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
1445 bool renderTarget, const void* srcData,
1446 GrGLTexture::TexParams* initialTexParams, size_t rowBytes) {
1447 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1448 // drivers have a bug where an FBO won't be complete if it includes a
1449 // texture that is not mipmap complete (considering the filter in use).
1450
1451 // we only set a subset here so invalidate first
1452 initialTexParams->invalidate();
1453 initialTexParams->fMinFilter = GR_GL_NEAREST;
1454 initialTexParams->fMagFilter = GR_GL_NEAREST;
1455 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1456 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1457
1458 if (desc.fTextureStorageAllocator.fAllocateTextureStorage) {
1459 return this->createTextureExternalAllocatorImpl(desc, info, srcData, rowBytes);
1460 }
1461
1462 info->fID = 0;
1463 info->fTarget = GR_GL_TEXTURE_2D;
1464 GL_CALL(GenTextures(1, &(info->fID)));
1465
1466 if (!info->fID) {
1467 return false;
1468 }
1469
1470 this->setScratchTextureUnit();
1471 GL_CALL(BindTexture(info->fTarget, info->fID));
1472
1473 if (renderTarget && this->glCaps().textureUsageSupport()) {
1474 // provides a hint about how this texture will be used
1475 GL_CALL(TexParameteri(info->fTarget,
1476 GR_GL_TEXTURE_USAGE,
1477 GR_GL_FRAMEBUFFER_ATTACHMENT));
1478 }
1479
1480 GL_CALL(TexParameteri(info->fTarget,
1481 GR_GL_TEXTURE_MAG_FILTER,
1482 initialTexParams->fMagFilter));
1483 GL_CALL(TexParameteri(info->fTarget,
1484 GR_GL_TEXTURE_MIN_FILTER,
1485 initialTexParams->fMinFilter));
1486 GL_CALL(TexParameteri(info->fTarget,
1487 GR_GL_TEXTURE_WRAP_S,
1488 initialTexParams->fWrapS));
1489 GL_CALL(TexParameteri(info->fTarget,
1490 GR_GL_TEXTURE_WRAP_T,
1491 initialTexParams->fWrapT));
1492 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1493 desc.fWidth, desc.fHeight,
1494 desc.fConfig, srcData, rowBytes)) {
1495 GL_CALL(DeleteTextures(1, &(info->fID)));
1496 return false;
1497 }
1498 return true;
1499}
1500
1501bool GrGLGpu::createTextureExternalAllocatorImpl(
1502 const GrSurfaceDesc& desc, GrGLTextureInfo* info, const void* srcData, size_t rowBytes) {
1503 switch (desc.fTextureStorageAllocator.fAllocateTextureStorage(
1504 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info),
1505 desc.fWidth, desc.fHeight, desc.fConfig, srcData, desc.fOrigin)) {
1506 case GrTextureStorageAllocator::Result::kSucceededAndUploaded:
1507 return true;
1508 case GrTextureStorageAllocator::Result::kFailed:
1509 return false;
1510 case GrTextureStorageAllocator::Result::kSucceededWithoutUpload:
1511 break;
1512 }
1513
1514 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1515 desc.fWidth, desc.fHeight,
1516 desc.fConfig, srcData, rowBytes)) {
1517 desc.fTextureStorageAllocator.fDeallocateTextureStorage(
1518 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info));
1519 return false;
1520 }
1521 return true;
1522}
1523
egdanielec00d942015-09-14 12:56:10 -07001524GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1525 int width,
1526 int height) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001527 // All internally created RTs are also textures. We don't create
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001528 // 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 +00001529 SkASSERT(rt->asTexture());
1530 SkASSERT(width >= rt->width());
1531 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001532
vbuzinovdded6962015-06-12 08:59:45 -07001533 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001534 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001535
egdanielff1d5472015-09-10 08:37:20 -07001536 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001537 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001538 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001539 }
egdanielff1d5472015-09-10 08:37:20 -07001540
1541 if (!sbDesc.fRenderbufferID) {
1542 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1543 }
1544 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001545 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001546 }
1547 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1548 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1549 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1550 // we do this "if" so that we don't call the multisample
1551 // version on a GL that doesn't have an MSAA extension.
1552 if (samples > 0) {
1553 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1554 samples,
1555 sFmt.fInternalFormat,
1556 width, height));
1557 } else {
1558 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1559 sFmt.fInternalFormat,
1560 width, height));
1561 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1562 }
1563 fStats.incStencilAttachmentCreates();
1564 // After sized formats we attempt an unsized format and take
1565 // whatever sizes GL gives us. In that case we query for the size.
1566 GrGLStencilAttachment::Format format = sFmt;
1567 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001568 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1569 sbDesc,
1570 width,
1571 height,
1572 samples,
1573 format);
1574 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001575}
1576
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001577////////////////////////////////////////////////////////////////////////////////
1578
jvanverth73063dc2015-12-03 09:15:47 -08001579// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1580// objects are implemented as client-side-arrays on tile-deferred architectures.
1581#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1582
bsalomon861e1032014-12-16 07:33:49 -08001583GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001584 GrGLVertexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001585 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001586 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001587
jvanverth73063dc2015-12-03 09:15:47 -08001588 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001589 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001590 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001592 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001593 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001594 GL_CALL(GenBuffers(1, &desc.fID));
1595 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001596 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001597 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1598 // make sure driver can allocate memory for this buffer
1599 GL_ALLOC_CALL(this->glInterface(),
1600 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001601 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001602 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001603 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001604 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1605 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001606 this->notifyVertexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001607 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001608 }
halcanary385fe4d2015-08-26 13:07:48 -07001609 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001610 return vertexBuffer;
1611 }
halcanary96fcdcc2015-08-27 07:41:13 -07001612 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001614}
1615
bsalomon861e1032014-12-16 07:33:49 -08001616GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001617 GrGLIndexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001618 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001619 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001620
jvanverth73063dc2015-12-03 09:15:47 -08001621 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001622 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001623 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001624 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001625 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001626 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001627 GL_CALL(GenBuffers(1, &desc.fID));
1628 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001629 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001630 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1631 // make sure driver can allocate memory for this buffer
1632 GL_ALLOC_CALL(this->glInterface(),
1633 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001634 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001635 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001636 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001637 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1638 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001639 this->notifyIndexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001640 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001641 }
halcanary385fe4d2015-08-26 13:07:48 -07001642 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001643 return indexBuffer;
1644 }
halcanary96fcdcc2015-08-27 07:41:13 -07001645 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647}
1648
jvanverthd7a2c1f2015-12-07 07:36:44 -08001649GrTransferBuffer* GrGLGpu::onCreateTransferBuffer(size_t size, TransferType xferType) {
1650 GrGLCaps::TransferBufferType xferBufferType = this->ctxInfo().caps()->transferBufferType();
1651 if (GrGLCaps::kNone_TransferBufferType == xferBufferType) {
1652 return nullptr;
1653 }
1654
jvanverth73063dc2015-12-03 09:15:47 -08001655 GrGLTransferBuffer::Desc desc;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001656 bool toGpu = (kCpuToGpu_TransferType == xferType);
jvanverth73063dc2015-12-03 09:15:47 -08001657 desc.fUsage = toGpu ? GrGLBufferImpl::kStreamDraw_Usage : GrGLBufferImpl::kStreamRead_Usage;
1658
1659 desc.fSizeInBytes = size;
kkinnunen546eb5c2015-12-11 00:05:33 -08001660 desc.fID = 0;
jvanverth73063dc2015-12-03 09:15:47 -08001661 GL_CALL(GenBuffers(1, &desc.fID));
1662 if (desc.fID) {
1663 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
jvanverthd7a2c1f2015-12-07 07:36:44 -08001664 // make sure driver can allocate memory for this bmapuffer
jvanverth17aa0472016-01-05 10:41:27 -08001665 GrGLenum target;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001666 if (GrGLCaps::kChromium_TransferBufferType == xferBufferType) {
jvanverth17aa0472016-01-05 10:41:27 -08001667 target = toGpu ? GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM
jvanverthd7a2c1f2015-12-07 07:36:44 -08001668 : GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
1669 } else {
1670 SkASSERT(GrGLCaps::kPBO_TransferBufferType == xferBufferType);
jvanverth17aa0472016-01-05 10:41:27 -08001671 target = toGpu ? GR_GL_PIXEL_UNPACK_BUFFER : GR_GL_PIXEL_PACK_BUFFER;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001672 }
jvanverth17aa0472016-01-05 10:41:27 -08001673 GL_CALL(BindBuffer(target, desc.fID));
1674 GL_ALLOC_CALL(this->glInterface(),
1675 BufferData(target,
jvanverth73063dc2015-12-03 09:15:47 -08001676 (GrGLsizeiptr) desc.fSizeInBytes,
1677 nullptr, // data ptr
1678 (toGpu ? GR_GL_STREAM_DRAW : GR_GL_STREAM_READ)));
1679 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1680 GL_CALL(DeleteBuffers(1, &desc.fID));
1681 return nullptr;
1682 }
jvanverth17aa0472016-01-05 10:41:27 -08001683 GrTransferBuffer* transferBuffer = new GrGLTransferBuffer(this, desc, target);
jvanverth73063dc2015-12-03 09:15:47 -08001684 return transferBuffer;
1685 }
1686
1687 return nullptr;
1688}
1689
bsalomon3e791242014-12-17 13:43:13 -08001690void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001691 const GrGLIRect& rtViewport,
1692 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001693 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001694 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001695 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001696 scissorState.rect().fLeft,
1697 scissorState.rect().fTop,
1698 scissorState.rect().width(),
1699 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001700 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001701 // if the scissor fully contains the viewport then we fall through and
1702 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001703 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001704 if (fHWScissorSettings.fRect != scissor) {
1705 scissor.pushToGLScissor(this->glInterface());
1706 fHWScissorSettings.fRect = scissor;
1707 }
1708 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1709 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1710 fHWScissorSettings.fEnabled = kYes_TriState;
1711 }
1712 return;
1713 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 }
joshualitt77b13072014-10-27 14:51:01 -07001715
1716 // See fall through note above
1717 this->disableScissor();
1718}
1719
cdaltond0a840d2015-03-16 17:19:58 -07001720bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001721 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001722 const GrPipeline& pipeline = *args.fPipeline;
bsalomon2047b782015-12-21 13:12:54 -08001723 args.fPipeline->getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001724
egdaniel080e6732014-12-22 07:35:52 -08001725 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001726 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001727
bsalomon6df86402015-06-01 10:41:49 -07001728 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args));
1729 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001730 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08001731 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001732 }
1733
bsalomon6df86402015-06-01 10:41:49 -07001734 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001735 if (fHWProgramID != programID) {
1736 GL_CALL(UseProgram(programID));
1737 fHWProgramID = programID;
1738 }
1739
egdanield803f272015-03-18 13:01:52 -07001740 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001741 // Swizzle the blend to match what the shader will output.
1742 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
1743 args.fPipeline->getRenderTarget()->config());
1744 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001745 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001746
cdalton42717652015-06-18 11:54:30 -07001747 SkSTArray<8, const GrTextureAccess*> textureAccesses;
joshualitt465283c2015-09-11 08:19:35 -07001748 program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07001749
1750 int numTextureAccesses = textureAccesses.count();
1751 for (int i = 0; i < numTextureAccesses; i++) {
1752 this->bindTexture(i, textureAccesses[i]->getParams(),
1753 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
1754 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001755
egdaniel8dd688b2015-01-22 10:16:09 -08001756 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
1757 this->flushStencil(pipeline.getStencil());
1758 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltonaf8bc7d2016-02-05 09:35:20 -08001759 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001760
1761 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001762 // to be msaa-resolved (which will modify bound FBO state).
cdaltond4727922015-11-10 12:49:06 -08001763 this->flushRenderTarget(glRT, nullptr);
bsalomonbc3d0de2014-12-15 13:45:03 -08001764
1765 return true;
1766}
1767
joshualitt873ad0e2015-01-20 09:08:51 -08001768void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomone64eb572015-05-07 11:35:55 -07001769 const GrNonInstancedVertices& vertices,
bsalomonbc3d0de2014-12-15 13:45:03 -08001770 size_t* indexOffsetInBytes) {
1771 GrGLVertexBuffer* vbuf;
bsalomone64eb572015-05-07 11:35:55 -07001772 vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001773
1774 SkASSERT(vbuf);
1775 SkASSERT(!vbuf->isMapped());
1776
halcanary96fcdcc2015-08-27 07:41:13 -07001777 GrGLIndexBuffer* ibuf = nullptr;
bsalomone64eb572015-05-07 11:35:55 -07001778 if (vertices.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001779 SkASSERT(indexOffsetInBytes);
1780
1781 *indexOffsetInBytes = 0;
bsalomone64eb572015-05-07 11:35:55 -07001782 ibuf = (GrGLIndexBuffer*)vertices.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08001783
1784 SkASSERT(ibuf);
1785 SkASSERT(!ibuf->isMapped());
1786 *indexOffsetInBytes += ibuf->baseOffset();
1787 }
1788 GrGLAttribArrayState* attribState =
1789 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
1790
joshualitt873ad0e2015-01-20 09:08:51 -08001791 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08001792 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001793
joshualitt873ad0e2015-01-20 09:08:51 -08001794 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08001795
bsalomone64eb572015-05-07 11:35:55 -07001796 size_t vertexOffsetInBytes = stride * vertices.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08001797
1798 vertexOffsetInBytes += vbuf->baseOffset();
1799
bsalomonbc3d0de2014-12-15 13:45:03 -08001800 uint32_t usedAttribArraysMask = 0;
1801 size_t offset = 0;
1802
1803 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08001804 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08001805 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08001806 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08001807 attribState->set(this,
1808 attribIndex,
bsalomon6df86402015-06-01 10:41:49 -07001809 vbuf->bufferID(),
bsalomonbc3d0de2014-12-15 13:45:03 -08001810 GrGLAttribTypeToLayout(attribType).fCount,
1811 GrGLAttribTypeToLayout(attribType).fType,
1812 GrGLAttribTypeToLayout(attribType).fNormalized,
1813 stride,
1814 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08001815 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08001816 }
1817 attribState->disableUnusedArrays(this, usedAttribArraysMask);
1818 }
1819}
1820
joshualitt873ad0e2015-01-20 09:08:51 -08001821void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
1822 const GrPrimitiveProcessor& primProc,
joshualitt465283c2015-09-11 08:19:35 -07001823 const GrPipeline& pipeline) const {
bsalomon7f9b2e42016-01-12 13:29:26 -08001824 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, *this->glCaps().glslCaps())) {
bsalomonbc3d0de2014-12-15 13:45:03 -08001825 SkDEBUGFAIL("Failed to generate GL program descriptor");
1826 }
1827}
1828
joshualitt93316b92015-10-23 09:08:08 -07001829void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) {
1830 this->handleDirtyContext();
1831 if (GR_GL_ARRAY_BUFFER == type) {
1832 this->bindVertexBuffer(id);
jvanverth73063dc2015-12-03 09:15:47 -08001833 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07001834 this->bindIndexBufferAndDefaultVertexArray(id);
jvanverthd7a2c1f2015-12-07 07:36:44 -08001835 } else {
1836 GR_GL_CALL(this->glInterface(), BindBuffer(type, id));
joshualitt93316b92015-10-23 09:08:08 -07001837 }
1838}
1839
1840void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) {
1841 this->handleDirtyContext();
1842 GL_CALL(DeleteBuffers(1, &id));
1843 if (GR_GL_ARRAY_BUFFER == type) {
1844 this->notifyVertexBufferDelete(id);
jvanverth73063dc2015-12-03 09:15:47 -08001845 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07001846 this->notifyIndexBufferDelete(id);
1847 }
1848}
1849
jvanverth73063dc2015-12-03 09:15:47 -08001850static GrGLenum get_gl_usage(GrGLBufferImpl::Usage usage) {
1851 static const GrGLenum grToGL[] = {
1852 GR_GL_STATIC_DRAW, // GrGLBufferImpl::kStaticDraw_Usage
1853 DYNAMIC_USAGE_PARAM, // GrGLBufferImpl::kDynamicDraw_Usage
1854 GR_GL_STREAM_DRAW, // GrGLBufferImpl::kStreamDraw_Usage
1855 GR_GL_STREAM_READ, // GrGLBufferImpl::kStreamRead_Usage
1856 };
1857 static_assert(SK_ARRAY_COUNT(grToGL) == GrGLBufferImpl::kUsageCount, "array_size_mismatch");
joshualitt93316b92015-10-23 09:08:08 -07001858
jvanverth73063dc2015-12-03 09:15:47 -08001859 return grToGL[usage];
1860}
1861
1862void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
1863 size_t currentSize, size_t requestedSize) {
joshualitt93316b92015-10-23 09:08:08 -07001864 void* mapPtr = nullptr;
jvanverth73063dc2015-12-03 09:15:47 -08001865 GrGLenum glUsage = get_gl_usage(usage);
jvanverthd7a2c1f2015-12-07 07:36:44 -08001866 bool readOnly = (GrGLBufferImpl::kStreamRead_Usage == usage);
1867
joshualitt93316b92015-10-23 09:08:08 -07001868 // Handling dirty context is done in the bindBuffer call
1869 switch (this->glCaps().mapBufferType()) {
1870 case GrGLCaps::kNone_MapBufferType:
1871 break;
1872 case GrGLCaps::kMapBuffer_MapBufferType:
1873 this->bindBuffer(id, type);
1874 // Let driver know it can discard the old data
joshualitt6df232d2015-10-23 13:54:12 -07001875 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001876 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001877 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001878 GL_CALL_RET(mapPtr, MapBuffer(type, readOnly ? GR_GL_READ_ONLY : GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07001879 break;
1880 case GrGLCaps::kMapBufferRange_MapBufferType: {
1881 this->bindBuffer(id, type);
1882 // Make sure the GL buffer size agrees with fDesc before mapping.
1883 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001884 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001885 }
jvanverth17aa0472016-01-05 10:41:27 -08001886 GrGLbitfield writeAccess = GR_GL_MAP_WRITE_BIT;
1887 // TODO: allow the client to specify invalidation in the stream draw case
1888 if (GrGLBufferImpl::kStreamDraw_Usage != usage) {
1889 writeAccess |= GR_GL_MAP_INVALIDATE_BUFFER_BIT;
1890 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001891 GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, readOnly ?
1892 GR_GL_MAP_READ_BIT :
jvanverth17aa0472016-01-05 10:41:27 -08001893 writeAccess));
joshualitt93316b92015-10-23 09:08:08 -07001894 break;
1895 }
1896 case GrGLCaps::kChromium_MapBufferType:
1897 this->bindBuffer(id, type);
1898 // Make sure the GL buffer size agrees with fDesc before mapping.
1899 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08001900 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07001901 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08001902 GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, readOnly ?
1903 GR_GL_READ_ONLY :
1904 GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07001905 break;
1906 }
1907 return mapPtr;
1908}
1909
jvanverth73063dc2015-12-03 09:15:47 -08001910void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
1911 size_t currentSize, const void* src, size_t srcSizeInBytes) {
joshualitt93316b92015-10-23 09:08:08 -07001912 SkASSERT(srcSizeInBytes <= currentSize);
1913 // bindbuffer handles dirty context
1914 this->bindBuffer(id, type);
jvanverth73063dc2015-12-03 09:15:47 -08001915 GrGLenum glUsage = get_gl_usage(usage);
joshualitt93316b92015-10-23 09:08:08 -07001916
joshualitt6df232d2015-10-23 13:54:12 -07001917#if GR_GL_USE_BUFFER_DATA_NULL_HINT
1918 if (currentSize == srcSizeInBytes) {
jvanverth73063dc2015-12-03 09:15:47 -08001919 GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001920 } else {
1921 // Before we call glBufferSubData we give the driver a hint using
1922 // glBufferData with nullptr. This makes the old buffer contents
1923 // inaccessible to future draws. The GPU may still be processing
1924 // draws that reference the old contents. With this hint it can
1925 // assign a different allocation for the new contents to avoid
1926 // flushing the gpu past draws consuming the old contents.
1927 // TODO I think we actually want to try calling bufferData here
jvanverth73063dc2015-12-03 09:15:47 -08001928 GL_CALL(BufferData(type, currentSize, nullptr, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001929 GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src));
1930 }
1931#else
joshualitt93316b92015-10-23 09:08:08 -07001932 // Note that we're cheating on the size here. Currently no methods
1933 // allow a partial update that preserves contents of non-updated
1934 // portions of the buffer (map() does a glBufferData(..size, nullptr..))
jvanverth73063dc2015-12-03 09:15:47 -08001935 GL_CALL(BufferData(type, srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07001936#endif
joshualitt93316b92015-10-23 09:08:08 -07001937}
1938
1939void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) {
1940 // bind buffer handles the dirty context
1941 switch (this->glCaps().mapBufferType()) {
1942 case GrGLCaps::kNone_MapBufferType:
1943 SkDEBUGFAIL("Shouldn't get here.");
1944 return;
1945 case GrGLCaps::kMapBuffer_MapBufferType: // fall through
1946 case GrGLCaps::kMapBufferRange_MapBufferType:
1947 this->bindBuffer(id, type);
1948 GL_CALL(UnmapBuffer(type));
1949 break;
1950 case GrGLCaps::kChromium_MapBufferType:
1951 this->bindBuffer(id, type);
1952 GL_CALL(UnmapBufferSubData(mapPtr));
1953 break;
1954 }
1955}
1956
bsalomon861e1032014-12-16 07:33:49 -08001957void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001958 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001959 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001960 fHWScissorSettings.fEnabled = kNo_TriState;
1961 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001962 }
1963}
1964
egdaniel51c8d402015-08-06 10:54:13 -07001965void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001966 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001967 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001968 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001969
egdaniel51c8d402015-08-06 10:54:13 -07001970 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08001971 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07001972 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07001973 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00001974
1975 GrGLfloat r, g, b, a;
1976 static const GrGLfloat scale255 = 1.f / 255.f;
1977 a = GrColorUnpackA(color) * scale255;
1978 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001979 r = GrColorUnpackR(color) * scaleRGB;
1980 g = GrColorUnpackG(color) * scaleRGB;
1981 b = GrColorUnpackB(color) * scaleRGB;
1982
1983 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001984 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001985 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001986 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001987}
1988
bsalomon861e1032014-12-16 07:33:49 -08001989void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08001990 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00001991 if (!this->caps()->discardRenderTargetSupport()) {
1992 return;
1993 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001994
1995 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07001996 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
1997 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1998 fStats.incRenderTargetBinds();
1999 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
2000 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002001 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07002002 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002003 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002004 break;
2005 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07002006 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002007 // When rendering to the default framebuffer the legal values for attachments
2008 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2009 // types.
2010 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002011 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002012 attachments));
2013 } else {
2014 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002015 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002016 attachments));
2017 }
2018 break;
2019 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07002020 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002021 // When rendering to the default framebuffer the legal values for attachments
2022 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2023 // types. See glDiscardFramebuffer() spec.
2024 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002025 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002026 attachments));
2027 } else {
2028 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002029 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002030 attachments));
2031 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002032 break;
2033 }
2034 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002035 renderTarget->flagAsResolved();
2036}
2037
bsalomon861e1032014-12-16 07:33:49 -08002038void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002039 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002040 return;
2041 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002042 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002043 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002044
joshualitt77b13072014-10-27 14:51:01 -07002045 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002046
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002047 GL_CALL(StencilMask(0xffffffff));
2048 GL_CALL(ClearStencil(0));
2049 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002050 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002051}
2052
bsalomon861e1032014-12-16 07:33:49 -08002053void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07002054 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002055
egdaniel8dc7c3a2015-04-16 11:22:42 -07002056 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002057 // this should only be called internally when we know we have a
2058 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002059 SkASSERT(sb);
2060 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002061#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002062 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002063 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002064#else
2065 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002066 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002067 // turned into draws. Our contract on GrDrawTarget says that
2068 // changing the clip between stencil passes may or may not
2069 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002070 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002071#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002072 GrGLint value;
2073 if (insideClip) {
2074 value = (1 << (stencilBitCount - 1));
2075 } else {
2076 value = 0;
2077 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002078 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002079 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002080
bsalomon3e791242014-12-17 13:43:13 -08002081 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002082 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002083 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002084
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002085 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002086 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002087 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002088 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002089}
2090
bsalomon39826022015-07-23 08:07:21 -07002091static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
2092 int width, int height, GrPixelConfig config,
2093 size_t rowBytes) {
2094 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002095 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
2096 return false;
2097 }
2098
bsalomon494aa592015-07-23 11:45:02 -07002099 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002100 static const int kMinSize = 32;
2101 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002102 return false;
2103 }
2104
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002105 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002106 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002107 return false;
2108 }
2109
2110 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002111 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002112 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2113 // clipped.
2114 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2115}
2116
bsalomon1aa20292016-01-22 08:16:09 -08002117bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2118 auto bindRenderTarget = [this, target]() -> bool {
2119 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2120 return true;
2121 };
2122 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2123 GR_GL_GetIntegerv(this->glInterface(), query, value);
2124 };
2125 GrPixelConfig rtConfig = target->config();
2126 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2127}
2128
2129bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
2130 auto bindRenderTarget = [this, rtConfig]() -> bool {
2131 GrTextureDesc desc;
2132 desc.fConfig = rtConfig;
2133 desc.fWidth = desc.fHeight = 16;
2134 desc.fFlags = kRenderTarget_GrSurfaceFlag;
2135 SkAutoTUnref<GrTexture> temp(this->createTexture(desc, false, nullptr, 0));
2136 if (!temp) {
2137 return false;
2138 }
2139 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2140 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2141 return true;
2142 };
2143 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2144 GR_GL_GetIntegerv(this->glInterface(), query, value);
2145 };
2146 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2147}
2148
2149bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2150 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2151 return this->readPixelsSupported(rt, readConfig);
2152 } else {
2153 GrPixelConfig config = surfaceForConfig->config();
2154 return this->readPixelsSupported(config, readConfig);
2155 }
2156}
2157
bsalomone9573312016-01-25 14:33:25 -08002158static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2159 if (GrPixelConfigIsSRGB(a)) {
2160 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2161 } else if (GrPixelConfigIsSRGB(b)) {
2162 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2163 }
2164 return false;
2165}
2166
bsalomonf0674512015-07-28 13:26:15 -07002167bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2168 GrPixelConfig readConfig, DrawPreference* drawPreference,
2169 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002170 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002171
bsalomone9573312016-01-25 14:33:25 -08002172 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002173 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2174 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2175 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2176 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2177 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07002178 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07002179
bsalomone9573312016-01-25 14:33:25 -08002180 // For now assume no swizzling, we may change that below.
2181 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2182
2183 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2184 // from will be srcConfig and we will read readConfig pixels from it.
2185 // Not that if we require a draw and return a non-renderable format for the temp surface the
2186 // base class will fail for us.
2187 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2188 tempDrawInfo->fReadConfig = readConfig;
2189
2190 if (requires_srgb_conversion(srcConfig, readConfig)) {
2191 if (!this->readPixelsSupported(readConfig, readConfig)) {
2192 return false;
2193 }
2194 // Draw to do srgb to linear conversion or vice versa.
2195 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2196 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2197 tempDrawInfo->fReadConfig = readConfig;
2198 return true;
2199 }
2200
2201 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2202 if (!srcAsRT) {
2203 // For now keep assuming the draw is not a format transformation, just a draw to get to a
2204 // RT. We may add additional transformations below.
2205 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2206 }
bsalomon1aa20292016-01-22 08:16:09 -08002207 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2208 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002209 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002210 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2211 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002212 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002213 } else if (kMesa_GrGLDriver == this->glContext().driver() &&
2214 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002215 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2216 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002217 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002218 // Better to do a draw with a R/B swap and then read as the original config.
2219 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002220 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2221 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002222 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002223 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2224 if (readConfig == kBGRA_8888_GrPixelConfig &&
2225 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2226 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002227 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2228 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2229 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002230 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2231 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2232 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2233 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002234 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2235 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2236 // it's unsupported, but 32bit RGBA reads are supported.
2237 // Don't attempt to do any srgb conversions since we only care about alpha.
2238 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2239 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2240 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2241 }
2242 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2243 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2244 // first and then onReadPixels will read that to a 32bit temporary buffer.
2245 if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
2246 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2247 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2248 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2249 } else {
2250 return false;
2251 }
2252 } else {
2253 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2254 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2255 }
bsalomon1aa20292016-01-22 08:16:09 -08002256 } else {
2257 return false;
2258 }
bsalomon39826022015-07-23 08:07:21 -07002259 }
2260
bsalomon1aa20292016-01-22 08:16:09 -08002261 if (srcAsRT &&
2262 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002263 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002264 }
2265
bsalomon39826022015-07-23 08:07:21 -07002266 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002267}
2268
bsalomon6cb3cbe2015-07-30 07:34:27 -07002269bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002270 int left, int top,
2271 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002272 GrPixelConfig config,
2273 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002274 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002275 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002276
bsalomone9573312016-01-25 14:33:25 -08002277 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2278 if (!renderTarget) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002279 return false;
2280 }
2281
bsalomon16921ec2015-07-30 15:34:56 -07002282 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002283 if (requires_srgb_conversion(surface->config(), config)) {
2284 return false;
2285 }
2286
2287 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2288 // bit channels as RGBA and then extract A.
2289 if (!this->readPixelsSupported(renderTarget, config)) {
2290 // Don't attempt to do any srgb conversions since we only care about alpha.
2291 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
2292 if (GrPixelConfigIsSRGB(renderTarget->config())) {
2293 tempConfig = kSRGBA_8888_GrPixelConfig;
2294 }
2295 if (kAlpha_8_GrPixelConfig == config &&
2296 this->readPixelsSupported(renderTarget, tempConfig)) {
2297 SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
2298 if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, temp.get(),
2299 width*4)) {
2300 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2301 for (int j = 0; j < height; ++j) {
2302 for (int i = 0; i < width; ++i) {
2303 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2304 }
2305 }
2306 return true;
2307 }
2308 }
bsalomon16921ec2015-07-30 15:34:56 -07002309 return false;
2310 }
2311
bsalomon76148af2016-01-12 11:13:47 -08002312 GrGLenum externalFormat;
2313 GrGLenum externalType;
bsalomone9573312016-01-25 14:33:25 -08002314 if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002315 &externalType)) {
2316 return false;
2317 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002318 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002319
bsalomon@google.comc6980972011-11-02 19:57:21 +00002320 // resolve the render target if necessary
bsalomone9573312016-01-25 14:33:25 -08002321 switch (renderTarget->getResolveType()) {
egdanield803f272015-03-18 13:01:52 -07002322 case GrGLRenderTarget::kCantResolve_ResolveType:
2323 return false;
2324 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002325 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002326 break;
2327 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002328 this->onResolveRenderTarget(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002329 // we don't track the state of the READ FBO ID.
2330 fStats.incRenderTargetBinds();
bsalomone9573312016-01-25 14:33:25 -08002331 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002332 break;
2333 default:
2334 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002335 }
2336
bsalomone9573312016-01-25 14:33:25 -08002337 const GrGLIRect& glvp = renderTarget->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002338
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002339 // the read rect is viewport-relative
2340 GrGLIRect readRect;
bsalomone9573312016-01-25 14:33:25 -08002341 readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002342
bsalomon9d02b262016-02-01 12:49:30 -08002343 size_t bytesPerPixel = GrBytesPerPixel(config);
2344 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002345
bsalomon@google.comc6980972011-11-02 19:57:21 +00002346 size_t readDstRowBytes = tightRowBytes;
2347 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002348
bsalomon@google.comc6980972011-11-02 19:57:21 +00002349 // determine if GL can read using the passed rowBytes or if we need
2350 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002351 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002352 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002353 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002354 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002355 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002356 readDstRowBytes = rowBytes;
2357 } else {
2358 scratch.reset(tightRowBytes * height);
2359 readDst = scratch.get();
2360 }
2361 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002362 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002363 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2364 }
bsalomonf46a1242015-12-15 12:37:38 -08002365 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2366
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002367 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2368 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002369 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002370 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002371 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002372 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2373 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002374 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002375 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002376 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002377 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002378
2379 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002380 // API presents top-to-bottom. We must preserve the padding contents. Note
2381 // that the above readPixels did not overwrite the padding.
2382 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002383 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002384 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002385 scratch.reset(tightRowBytes);
2386 void* tmpRow = scratch.get();
2387 // flip y in-place by rows
2388 const int halfY = height >> 1;
2389 char* top = reinterpret_cast<char*>(buffer);
2390 char* bottom = top + (height - 1) * rowBytes;
2391 for (int y = 0; y < halfY; y++) {
2392 memcpy(tmpRow, top, tightRowBytes);
2393 memcpy(top, bottom, tightRowBytes);
2394 memcpy(bottom, tmpRow, tightRowBytes);
2395 top += rowBytes;
2396 bottom -= rowBytes;
2397 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002398 }
2399 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002400 SkASSERT(readDst != buffer);
2401 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002402 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002403 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002404 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002405 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002406 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002407 dst += (height-1) * rowBytes;
2408 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002409 for (int y = 0; y < height; y++) {
2410 memcpy(dst, src, tightRowBytes);
2411 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002412 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002413 dst += rowBytes;
2414 } else {
2415 dst -= rowBytes;
2416 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002417 }
2418 }
2419 return true;
2420}
2421
ethannicholas22793252016-01-30 09:59:10 -08002422void GrGLGpu::performFlushWorkaround() {
2423 if (fPLSHasBeenUsed) {
2424 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2425 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
2426 * trivial way every frame. And since we use it every frame, there's never a point at which
2427 * it becomes safe to stop using this workaround once we start.
2428 */
2429 this->disableScissor();
2430 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002431 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002432 SkASSERT(!fHWPLSEnabled);
2433 SkASSERT(fMSAAEnabled != kYes_TriState);
2434 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2435 this->stampRectUsingProgram(fPLSSetupProgram.fProgram,
2436 SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f),
2437 fPLSSetupProgram.fPosXformUniform,
2438 fPLSSetupProgram.fArrayBuffer);
2439 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2440 }
2441}
ethannicholas5366a092016-01-22 09:45:47 -08002442
ethannicholas22793252016-01-30 09:59:10 -08002443void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bound) {
egdanield803f272015-03-18 13:01:52 -07002444 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002445
egdanield803f272015-03-18 13:01:52 -07002446 uint32_t rtID = target->getUniqueID();
2447 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002448 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002449 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2450#ifdef SK_DEBUG
2451 // don't do this check in Chromium -- this is causing
2452 // lots of repeated command buffer flushes when the compositor is
2453 // rendering with Ganesh, which is really slow; even too slow for
2454 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002455 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002456 GrGLenum status;
2457 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2458 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2459 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2460 }
bsalomon160f24c2015-03-17 15:55:42 -07002461 }
egdanield803f272015-03-18 13:01:52 -07002462#endif
2463 fHWBoundRenderTargetUniqueID = rtID;
2464 const GrGLIRect& vp = target->getViewport();
2465 if (fHWViewport != vp) {
2466 vp.pushToGLViewport(this->glInterface());
2467 fHWViewport = vp;
bsalomon160f24c2015-03-17 15:55:42 -07002468 }
bsalomon16921ec2015-07-30 15:34:56 -07002469 if (this->glCaps().srgbWriteControl()) {
2470 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config());
2471 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2472 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2473 fHWSRGBFramebuffer = kYes_TriState;
2474 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2475 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2476 fHWSRGBFramebuffer = kNo_TriState;
2477 }
2478 }
bsalomon5cd020f2015-03-17 12:46:56 -07002479 }
bsalomona9909122016-01-23 10:41:40 -08002480
2481 // 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 -07002482 if (nullptr == bound || !bound->isEmpty()) {
egdanield803f272015-03-18 13:01:52 -07002483 target->flagAsNeedingResolve(bound);
bsalomona9909122016-01-23 10:41:40 -08002484 if (GrTexture *texture = target->asTexture()) {
2485 texture->texturePriv().dirtyMipMaps(true);
2486 }
egdanield803f272015-03-18 13:01:52 -07002487 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002488}
2489
twiz@google.com0f31ca72011-03-18 17:38:11 +00002490GrGLenum gPrimitiveType2GLMode[] = {
2491 GR_GL_TRIANGLES,
2492 GR_GL_TRIANGLE_STRIP,
2493 GR_GL_TRIANGLE_FAN,
2494 GR_GL_POINTS,
2495 GR_GL_LINES,
2496 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002497};
2498
bsalomon@google.comd302f142011-03-03 13:54:13 +00002499#define SWAP_PER_DRAW 0
2500
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002501#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002502 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002503 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002504 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002505 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002506 void SwapBuf() {
2507 DWORD procID = GetCurrentProcessId();
2508 HWND hwnd = GetTopWindow(GetDesktopWindow());
2509 while(hwnd) {
2510 DWORD wndProcID = 0;
2511 GetWindowThreadProcessId(hwnd, &wndProcID);
2512 if(wndProcID == procID) {
2513 SwapBuffers(GetDC(hwnd));
2514 }
2515 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2516 }
2517 }
2518 #endif
2519#endif
2520
bsalomone64eb572015-05-07 11:35:55 -07002521void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) {
cdaltond0a840d2015-03-16 17:19:58 -07002522 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08002523 return;
2524 }
2525
ethannicholas22793252016-01-30 09:59:10 -08002526 GrPixelLocalStorageState plsState = args.fPrimitiveProcessor->getPixelLocalStorageState();
2527 if (!fHWPLSEnabled && plsState !=
2528 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2529 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2530 this->setupPixelLocalStorage(args);
2531 fHWPLSEnabled = true;
2532 }
2533 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2534 GrStencilSettings stencil;
2535 stencil.setDisabled();
2536 this->flushStencil(stencil);
2537 }
2538
joshualitt873ad0e2015-01-20 09:08:51 -08002539 size_t indexOffsetInBytes = 0;
bsalomoncb8979d2015-05-05 09:51:38 -07002540 this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00002541
bsalomoncb8979d2015-05-05 09:51:38 -07002542 SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002543
bsalomoncb8979d2015-05-05 09:51:38 -07002544 if (vertices.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002545 GrGLvoid* indices =
bsalomoncb8979d2015-05-05 09:51:38 -07002546 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2547 vertices.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002548 // info.startVertex() was accounted for by setupGeometry.
bsalomoncb8979d2015-05-05 09:51:38 -07002549 GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()],
2550 vertices.indexCount(),
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002551 GR_GL_UNSIGNED_SHORT,
2552 indices));
2553 } else {
2554 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
2555 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
2556 // accounted for startVertex.
bsalomoncb8979d2015-05-05 09:51:38 -07002557 GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0,
2558 vertices.vertexCount()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002559 }
ethannicholas22793252016-01-30 09:59:10 -08002560
2561 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2562 // PLS draws always involve multiple draws, finishing up with a non-PLS
2563 // draw that writes to the color buffer. That draw ends up here; we wait
2564 // until after it is complete to actually disable PLS.
2565 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2566 fHWPLSEnabled = false;
2567 this->disableScissor();
2568 }
2569
bsalomon@google.comd302f142011-03-03 13:54:13 +00002570#if SWAP_PER_DRAW
2571 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002572 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002573 aglSwapBuffers(aglGetCurrentContext());
2574 int set_a_break_pt_here = 9;
2575 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002576 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002577 SwapBuf();
2578 int set_a_break_pt_here = 9;
2579 SwapBuf();
2580 #endif
2581#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002582}
2583
ethannicholas22793252016-01-30 09:59:10 -08002584void GrGLGpu::stampRectUsingProgram(GrGLuint program, const SkRect& bounds, GrGLint posXformUniform,
2585 GrGLuint arrayBuffer) {
2586 GL_CALL(UseProgram(program));
2587 this->fHWGeometryState.setVertexArrayID(this, 0);
2588
2589 GrGLAttribArrayState* attribs =
2590 this->fHWGeometryState.bindArrayAndBufferToDraw(this, arrayBuffer);
2591 attribs->set(this, 0, arrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0);
2592 attribs->disableUnusedArrays(this, 0x1);
2593
2594 GL_CALL(Uniform4f(posXformUniform, bounds.width(), bounds.height(), bounds.left(),
2595 bounds.top()));
2596
2597 GrXferProcessor::BlendInfo blendInfo;
2598 blendInfo.reset();
2599 this->flushBlend(blendInfo, GrSwizzle());
2600 this->flushColorWrite(true);
2601 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
2602 if (!fHWStencilSettings.isDisabled()) {
2603 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2604 }
2605 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2606 GL_CALL(UseProgram(fHWProgramID));
2607 if (!fHWStencilSettings.isDisabled()) {
2608 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2609 }
2610}
2611
2612void GrGLGpu::setupPixelLocalStorage(const DrawArgs& args) {
2613 fPLSHasBeenUsed = true;
2614 const SkRect& bounds =
2615 static_cast<const GrPLSGeometryProcessor*>(args.fPrimitiveProcessor)->getBounds();
2616 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2617 // and initializing the winding counts to zero
2618 GrRenderTarget* rt = args.fPipeline->getRenderTarget();
2619 SkScalar width = SkIntToScalar(rt->width());
2620 SkScalar height = SkIntToScalar(rt->height());
2621 // dst rect edges in NDC (-1 to 1)
2622 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2623 // it contained properly
2624 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2625 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2626 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2627 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2628 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
2629
2630 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
2631 this->stampRectUsingProgram(fPLSSetupProgram.fProgram, deviceBounds,
2632 fPLSSetupProgram.fPosXformUniform, fPLSSetupProgram.fArrayBuffer);
2633}
2634
bsalomon861e1032014-12-16 07:33:49 -08002635void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002636 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002637 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002638 // Some extensions automatically resolves the texture when it is read.
2639 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002640 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2641 fStats.incRenderTargetBinds();
2642 fStats.incRenderTargetBinds();
2643 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2644 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2645 // make sure we go through flushRenderTarget() since we've modified
2646 // the bound DRAW FBO ID.
2647 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002648 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002649 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002650
bsalomon@google.com347c3822013-05-01 20:10:01 +00002651 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002652 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002653 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002654 scissorState.set(dirtyRect);
2655 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002656 GL_CALL(ResolveMultisampleFramebuffer());
2657 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002658 GrGLIRect r;
2659 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2660 dirtyRect.width(), dirtyRect.height(), target->origin());
2661
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002662 int right = r.fLeft + r.fWidth;
2663 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002664
2665 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002666 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002667 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2668 r.fLeft, r.fBottom, right, top,
2669 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002670 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002671 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002672 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002673 }
2674}
2675
bsalomon@google.com411dad02012-06-05 20:24:20 +00002676namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002677
bsalomon@google.com411dad02012-06-05 20:24:20 +00002678
2679GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2680 static const GrGLenum gTable[] = {
2681 GR_GL_KEEP, // kKeep_StencilOp
2682 GR_GL_REPLACE, // kReplace_StencilOp
2683 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2684 GR_GL_INCR, // kIncClamp_StencilOp
2685 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2686 GR_GL_DECR, // kDecClamp_StencilOp
2687 GR_GL_ZERO, // kZero_StencilOp
2688 GR_GL_INVERT, // kInvert_StencilOp
2689 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002690 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002691 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2692 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2693 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2694 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2695 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2696 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2697 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2698 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002699 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002700 return gTable[op];
2701}
2702
2703void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002704 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002705 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002706 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002707 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002708 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2709 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2710
2711 GrGLint ref = settings.funcRef(grFace);
2712 GrGLint mask = settings.funcMask(grFace);
2713 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002714
2715 if (GR_GL_FRONT_AND_BACK == glFace) {
2716 // we call the combined func just in case separate stencil is not
2717 // supported.
2718 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2719 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002720 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002721 } else {
2722 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2723 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002724 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002725 }
2726}
2727}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002728
bsalomon3e791242014-12-17 13:43:13 -08002729void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2730 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002731 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002732 if (kNo_TriState != fHWStencilTestEnabled) {
2733 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2734 fHWStencilTestEnabled = kNo_TriState;
2735 }
2736 } else {
2737 if (kYes_TriState != fHWStencilTestEnabled) {
2738 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2739 fHWStencilTestEnabled = kYes_TriState;
2740 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002741 }
joshualitta58fe352014-10-27 08:39:00 -07002742 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002743 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002744 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002745 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002746 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002747 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002748 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002749 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002750 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002751 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002752 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002753 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002754 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002755 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002756 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002757 }
2758 }
joshualitta58fe352014-10-27 08:39:00 -07002759 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002760 }
2761}
2762
cdaltonaf8bc7d2016-02-05 09:35:20 -08002763void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
vbuzinovdded6962015-06-12 08:59:45 -07002764 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002765
cdaltond4727922015-11-10 12:49:06 -08002766 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002767 if (useHWAA) {
2768 if (kYes_TriState != fMSAAEnabled) {
2769 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2770 fMSAAEnabled = kYes_TriState;
2771 }
2772 } else {
2773 if (kNo_TriState != fMSAAEnabled) {
2774 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2775 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002776 }
2777 }
2778 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002779
2780 if (0 != this->caps()->maxRasterSamples()) {
2781 if (useHWAA && rt->hasMixedSamples() && !stencilEnabled) {
2782 // Since stencil is disabled and we want more samples than are in the color buffer, we
2783 // need to tell the rasterizer explicitly how many to run.
2784 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2785 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2786 fHWRasterMultisampleEnabled = kYes_TriState;
2787 }
2788 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2789 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2790 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2791 fHWNumRasterSamples = rt->numStencilSamples();
2792 }
2793 } else {
2794 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2795 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2796 fHWRasterMultisampleEnabled = kNo_TriState;
2797 }
2798 }
2799 } else {
2800 SkASSERT(!useHWAA || !rt->hasMixedSamples() || stencilEnabled);
2801 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002802}
2803
bsalomon7f9b2e42016-01-12 13:29:26 -08002804void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002805 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002806 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002807
cdalton8917d622015-05-06 13:40:21 -07002808 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002809 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2810 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002811 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2812 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002813 if (blendOff) {
2814 if (kNo_TriState != fHWBlendState.fEnabled) {
2815 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002816
2817 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2818 // https://code.google.com/p/skia/issues/detail?id=3943
2819 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2820 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2821 SkASSERT(this->caps()->advancedBlendEquationSupport());
2822 // Set to any basic blending equation.
2823 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2824 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2825 fHWBlendState.fEquation = blend_equation;
2826 }
2827
egdanielb414f252014-07-29 13:15:47 -07002828 fHWBlendState.fEnabled = kNo_TriState;
2829 }
cdalton8917d622015-05-06 13:40:21 -07002830 return;
2831 }
2832
2833 if (kYes_TriState != fHWBlendState.fEnabled) {
2834 GL_CALL(Enable(GR_GL_BLEND));
2835 fHWBlendState.fEnabled = kYes_TriState;
2836 }
2837
2838 if (fHWBlendState.fEquation != equation) {
2839 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2840 fHWBlendState.fEquation = equation;
2841 }
2842
2843 if (GrBlendEquationIsAdvanced(equation)) {
2844 SkASSERT(this->caps()->advancedBlendEquationSupport());
2845 // Advanced equations have no other blend state.
2846 return;
2847 }
2848
bsalomone63ffef2016-02-05 07:17:34 -08002849 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002850 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2851 gXfermodeCoeff2Blend[dstCoeff]));
2852 fHWBlendState.fSrcCoeff = srcCoeff;
2853 fHWBlendState.fDstCoeff = dstCoeff;
2854 }
2855
bsalomon7f9b2e42016-01-12 13:29:26 -08002856 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2857 GrColor blendConst = blendInfo.fBlendConstant;
2858 blendConst = swizzle.applyTo(blendConst);
2859 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2860 GrGLfloat c[4];
2861 GrColorToRGBAFloat(blendConst, c);
2862 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2863 fHWBlendState.fConstColor = blendConst;
2864 fHWBlendState.fConstColorValid = true;
2865 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002866 }
2867}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002868
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002869static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002870 static const GrGLenum gWrapModes[] = {
2871 GR_GL_CLAMP_TO_EDGE,
2872 GR_GL_REPEAT,
2873 GR_GL_MIRRORED_REPEAT
2874 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002875 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002876 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2877 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2878 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2879 return gWrapModes[tm];
2880}
2881
egdanielb7e7d572015-11-04 04:23:53 -08002882static GrGLenum get_component_enum_from_char(char component) {
2883 switch (component) {
2884 case 'r':
2885 return GR_GL_RED;
2886 case 'g':
2887 return GR_GL_GREEN;
2888 case 'b':
2889 return GR_GL_BLUE;
2890 case 'a':
2891 return GR_GL_ALPHA;
2892 default:
2893 SkFAIL("Unsupported component");
2894 return 0;
2895 }
2896}
2897
2898/** If texture swizzling is available using tex parameters then it is preferred over mangling
2899 the generated shader code. This potentially allows greater reuse of cached shaders. */
2900static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002901 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002902 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002903 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002904 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002905 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002906 }
2907}
2908
bsalomon861e1032014-12-16 07:33:49 -08002909void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002910 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002911
reed856e9d92015-09-30 12:21:45 -07002912#ifdef SK_DEBUG
2913 if (!this->caps()->npotTextureTileSupport()) {
2914 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2915 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2916 if (tileX || tileY) {
2917 const int w = texture->width();
2918 const int h = texture->height();
2919 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2920 }
2921 }
2922#endif
2923
bsalomon@google.comb8670992012-07-25 21:27:09 +00002924 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2925 // 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 +00002926 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002927 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002928 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00002929 this->onResolveRenderTarget(texRT);
2930 }
2931
bsalomon1c63bf62014-07-22 13:09:46 -07002932 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002933 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002934 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002935 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002936 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002937 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002938 }
2939
bsalomon@google.com4c883782012-06-04 19:05:11 +00002940 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002941 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002942 bool setAll = timestamp < this->getResetTimestamp();
2943 GrGLTexture::TexParams newTexParams;
2944
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002945 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002946 GR_GL_NEAREST,
2947 GR_GL_LINEAR,
2948 GR_GL_LINEAR_MIPMAP_LINEAR
2949 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002950 static GrGLenum glMagFilterModes[] = {
2951 GR_GL_NEAREST,
2952 GR_GL_LINEAR,
2953 GR_GL_LINEAR
2954 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002955 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002956
2957 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
2958 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
2959 filterMode = GrTextureParams::kBilerp_FilterMode;
2960 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002961 }
bsalomonefd7d452014-10-23 14:17:46 -07002962
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002963 newTexParams.fMinFilter = glMinFilterModes[filterMode];
2964 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00002965
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00002966 if (GrTextureParams::kMipMap_FilterMode == filterMode &&
bsalomonefd7d452014-10-23 14:17:46 -07002967 texture->texturePriv().mipMapsAreDirty()) {
bsalomon10528f12015-10-14 12:54:52 -07002968 GL_CALL(GenerateMipmap(target));
bsalomonafbf2d62014-09-30 12:18:44 -07002969 texture->texturePriv().dirtyMipMaps(false);
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002970 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00002971
bsalomon@google.comb8670992012-07-25 21:27:09 +00002972 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
2973 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08002974 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002975 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002976 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002977 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002978 }
2979 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
2980 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002981 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002982 }
2983 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002984 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002985 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002986 }
2987 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002988 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002989 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00002990 }
bsalomoncdee0092016-01-08 13:20:12 -08002991 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00002992 (setAll || memcmp(newTexParams.fSwizzleRGBA,
2993 oldTexParams.fSwizzleRGBA,
2994 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002995 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00002996 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002997 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2998 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07002999 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3000 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3001 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3002 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003003 } else {
3004 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
3005 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07003006 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003007 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003008 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003009 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003010}
3011
egdaniel080e6732014-12-22 07:35:52 -08003012void GrGLGpu::flushColorWrite(bool writeColor) {
3013 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003014 if (kNo_TriState != fHWWriteToColor) {
3015 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3016 GR_GL_FALSE, GR_GL_FALSE));
3017 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003018 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003019 } else {
3020 if (kYes_TriState != fHWWriteToColor) {
3021 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3022 fHWWriteToColor = kYes_TriState;
3023 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003024 }
bsalomon3e791242014-12-17 13:43:13 -08003025}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003026
egdaniel8dd688b2015-01-22 10:16:09 -08003027void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003028 if (fHWDrawFace != face) {
3029 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08003030 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003031 GL_CALL(Enable(GR_GL_CULL_FACE));
3032 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003033 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003034 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003035 GL_CALL(Enable(GR_GL_CULL_FACE));
3036 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003037 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003038 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003039 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003040 break;
3041 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003042 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003043 }
bsalomon3e791242014-12-17 13:43:13 -08003044 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003045 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003046}
3047
bsalomon861e1032014-12-16 07:33:49 -08003048void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003049 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003050 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003051 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003052 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003053 }
3054}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003055
bsalomon861e1032014-12-16 07:33:49 -08003056void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003057 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003058 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003059 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3060 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3061 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003062 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003063 // clear out the this field so that if a program does use this unit it will rebind the correct
3064 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003065 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003066}
3067
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003068// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08003069static inline bool can_blit_framebuffer(const GrSurface* dst,
3070 const GrSurface* src,
3071 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003072 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
3073 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon@google.com347c3822013-05-01 20:10:01 +00003074 gpu->glCaps().usesMSAARenderBuffers()) {
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00003075 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
3076 // or the rects are not the same (not just the same size but have the same edges).
3077 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
3078 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
3079 return false;
3080 }
bsalomon7ea33f52015-11-22 14:51:00 -08003081 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3082 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3083 return false;
3084 }
3085 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3086 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3087 return false;
3088 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003089 return true;
3090 } else {
3091 return false;
3092 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003093}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003094
bsalomon7ea33f52015-11-22 14:51:00 -08003095static inline bool can_copy_texsubimage(const GrSurface* dst,
3096 const GrSurface* src,
3097 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003098 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3099 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3100 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003101 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003102 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3103 return false;
3104 }
3105 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3106 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3107 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003108 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003109 return false;
3110 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003111 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3112 // If the src is multisampled (and uses an extension where there is a separate MSAA
3113 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003114 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003115 return false;
3116 }
bsalomon7ea33f52015-11-22 14:51:00 -08003117
3118 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3119 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3120 // texture.
3121 if (!dstTex) {
3122 return false;
3123 }
3124
3125 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003126
bsalomon7ea33f52015-11-22 14:51:00 -08003127 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3128 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003129 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003130 !GrPixelConfigIsCompressed(src->config()) &&
3131 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
3132 dstTex->target() == GR_GL_TEXTURE_2D &&
3133 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003134 return true;
3135 } else {
3136 return false;
3137 }
3138}
3139
3140// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3141// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07003142void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3143 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003144 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -07003145 if (nullptr == rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003146 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003147 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003148 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003149 GrGLuint* tempFBOID;
3150 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003151
egdanield803f272015-03-18 13:01:52 -07003152 if (0 == *tempFBOID) {
3153 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003154 }
3155
egdanield803f272015-03-18 13:01:52 -07003156 fStats.incRenderTargetBinds();
3157 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3158 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003159 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003160 target,
robertphillips754f4e92014-09-18 13:52:08 -07003161 texID,
3162 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003163 viewport->fLeft = 0;
3164 viewport->fBottom = 0;
3165 viewport->fWidth = surface->width();
3166 viewport->fHeight = surface->height();
3167 } else {
egdanield803f272015-03-18 13:01:52 -07003168 fStats.incRenderTargetBinds();
3169 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003170 *viewport = rt->getViewport();
3171 }
egdaniel0f5f9672015-02-03 11:10:51 -08003172}
3173
bsalomon10528f12015-10-14 12:54:52 -07003174void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
cblume61214052016-01-26 09:10:48 -08003175 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003176 if (!surface->asRenderTarget()) {
3177 SkASSERT(surface->asTexture());
3178 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3179 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3180 GR_GL_COLOR_ATTACHMENT0,
3181 textureTarget,
3182 0,
3183 0));
3184 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003185}
3186
joshualitt1c735482015-07-13 08:08:25 -07003187bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003188 // If the src is a texture, we can implement the blit as a draw assuming the config is
3189 // renderable.
3190 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3191 desc->fOrigin = kDefault_GrSurfaceOrigin;
3192 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3193 desc->fConfig = src->config();
3194 return true;
3195 }
3196
bsalomon7ea33f52015-11-22 14:51:00 -08003197 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3198 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3199 // Not supported for FBO blit or CopyTexSubImage
3200 return false;
3201 }
3202
bsalomon6df86402015-06-01 10:41:49 -07003203 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003204 // possible and we return false to fallback to creating a render target dst for render-to-
3205 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3206 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3207
bsalomon@google.comeb851172013-04-15 13:51:00 +00003208 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003209 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003210 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003211 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3212 // then we set up for that, otherwise fail.
3213 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
3214 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003215 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003216 desc->fConfig = kBGRA_8888_GrPixelConfig;
3217 return true;
3218 }
3219 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07003220 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003221 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
3222 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003223 }
3224
3225 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07003226 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003227 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3228 // fail.
3229 if (this->caps()->isConfigRenderable(src->config(), false)) {
3230 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003231 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003232 desc->fConfig = src->config();
3233 return true;
3234 }
3235 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003236 }
bsalomonf90a02b2014-11-26 12:28:00 -08003237
3238 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3239 desc->fConfig = src->config();
3240 desc->fOrigin = src->origin();
3241 desc->fFlags = kNone_GrSurfaceFlags;
3242 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003243}
3244
joshualitt1cbdcde2015-08-21 11:53:29 -07003245bool GrGLGpu::onCopySurface(GrSurface* dst,
3246 GrSurface* src,
3247 const SkIRect& srcRect,
3248 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003249 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3250 // swizzle.
3251 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3252 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3253 return false;
3254 }
bsalomon6df86402015-06-01 10:41:49 -07003255 if (src->asTexture() && dst->asRenderTarget()) {
3256 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
mtklein404b3b22015-05-18 09:29:10 -07003257 return true;
bsalomon5df6fee2015-05-18 06:26:15 -07003258 }
cblume61214052016-01-26 09:10:48 -08003259
bsalomon6df86402015-06-01 10:41:49 -07003260 if (can_copy_texsubimage(dst, src, this)) {
3261 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3262 return true;
3263 }
3264
mtklein404b3b22015-05-18 09:29:10 -07003265 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003266 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3267 }
3268
3269 return false;
3270}
3271
bsalomon7ea33f52015-11-22 14:51:00 -08003272void GrGLGpu::createCopyPrograms() {
3273 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
3274 fCopyPrograms[i].fProgram = 0;
bsalomon27a04872015-11-20 19:34:37 -08003275 }
bsalomon7ea33f52015-11-22 14:51:00 -08003276 const char* version = this->glCaps().glslCaps()->versionDeclString();
bsalomone5286e02016-01-14 09:24:09 -08003277 static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType,
3278 kSampler2DRect_GrSLType };
3279 SkASSERT(3 == SK_ARRAY_COUNT(fCopyPrograms));
3280 for (int i = 0; i < 3; ++i) {
3281 if (kSamplerExternal_GrSLType == kSamplerTypes[i] &&
3282 !this->glCaps().externalTextureSupport()) {
3283 continue;
3284 }
3285 if (kSampler2DRect_GrSLType == kSamplerTypes[i] &&
3286 !this->glCaps().rectangleTextureSupport()) {
3287 continue;
3288 }
bsalomon7ea33f52015-11-22 14:51:00 -08003289 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3290 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3291 GrShaderVar::kUniform_TypeModifier);
3292 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3293 GrShaderVar::kUniform_TypeModifier);
3294 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[i],
3295 GrShaderVar::kUniform_TypeModifier);
3296 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3297 GrShaderVar::kVaryingOut_TypeModifier);
3298 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3299 GrShaderVar::kOut_TypeModifier);
bsalomon6dea83f2015-12-03 12:58:06 -08003300
bsalomon7ea33f52015-11-22 14:51:00 -08003301 SkString vshaderTxt(version);
3302 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3303 vshaderTxt.append(";");
3304 uTexCoordXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3305 vshaderTxt.append(";");
3306 uPosXform.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3307 vshaderTxt.append(";");
3308 vTexCoord.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3309 vshaderTxt.append(";");
cblume61214052016-01-26 09:10:48 -08003310
bsalomon7ea33f52015-11-22 14:51:00 -08003311 vshaderTxt.append(
3312 "// Copy Program VS\n"
3313 "void main() {"
3314 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3315 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3316 " gl_Position.zw = vec2(0, 1);"
3317 "}"
3318 );
bsalomon27a04872015-11-20 19:34:37 -08003319
bsalomon7ea33f52015-11-22 14:51:00 -08003320 SkString fshaderTxt(version);
3321 if (kSamplerTypes[i] == kSamplerExternal_GrSLType) {
3322 fshaderTxt.appendf("#extension %s : require\n",
3323 this->glCaps().glslCaps()->externalTextureExtensionString());
3324 }
3325 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3326 *this->glCaps().glslCaps(),
3327 &fshaderTxt);
3328 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3329 vTexCoord.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3330 fshaderTxt.append(";");
3331 uTexture.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3332 fshaderTxt.append(";");
3333 const char* fsOutName;
3334 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3335 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3336 fshaderTxt.append(";");
3337 fsOutName = oFragColor.c_str();
3338 } else {
3339 fsOutName = "gl_FragColor";
3340 }
3341 fshaderTxt.appendf(
3342 "// Copy Program FS\n"
3343 "void main() {"
3344 " %s = %s(u_texture, v_texCoord);"
3345 "}",
3346 fsOutName,
bsalomone5286e02016-01-14 09:24:09 -08003347 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[i], this->glslGeneration())
bsalomon7ea33f52015-11-22 14:51:00 -08003348 );
cblume61214052016-01-26 09:10:48 -08003349
bsalomon7ea33f52015-11-22 14:51:00 -08003350 GL_CALL_RET(fCopyPrograms[i].fProgram, CreateProgram());
3351 const char* str;
3352 GrGLint length;
bsalomon0315dbc2015-11-20 20:24:31 -08003353
bsalomon7ea33f52015-11-22 14:51:00 -08003354 str = vshaderTxt.c_str();
3355 length = SkToInt(vshaderTxt.size());
3356 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3357 GR_GL_VERTEX_SHADER, &str, &length, 1,
3358 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003359
bsalomon7ea33f52015-11-22 14:51:00 -08003360 str = fshaderTxt.c_str();
3361 length = SkToInt(fshaderTxt.size());
3362 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3363 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3364 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003365
bsalomon7ea33f52015-11-22 14:51:00 -08003366 GL_CALL(LinkProgram(fCopyPrograms[i].fProgram));
bsalomon0315dbc2015-11-20 20:24:31 -08003367
bsalomon7ea33f52015-11-22 14:51:00 -08003368 GL_CALL_RET(fCopyPrograms[i].fTextureUniform,
3369 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texture"));
3370 GL_CALL_RET(fCopyPrograms[i].fPosXformUniform,
3371 GetUniformLocation(fCopyPrograms[i].fProgram, "u_posXform"));
3372 GL_CALL_RET(fCopyPrograms[i].fTexCoordXformUniform,
3373 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texCoordXform"));
bsalomon0315dbc2015-11-20 20:24:31 -08003374
bsalomon7ea33f52015-11-22 14:51:00 -08003375 GL_CALL(BindAttribLocation(fCopyPrograms[i].fProgram, 0, "a_vertex"));
bsalomon0315dbc2015-11-20 20:24:31 -08003376
bsalomon7ea33f52015-11-22 14:51:00 -08003377 GL_CALL(DeleteShader(vshader));
3378 GL_CALL(DeleteShader(fshader));
3379 }
kkinnunen546eb5c2015-12-11 00:05:33 -08003380 fCopyProgramArrayBuffer = 0;
bsalomon7ea33f52015-11-22 14:51:00 -08003381 GL_CALL(GenBuffers(1, &fCopyProgramArrayBuffer));
3382 fHWGeometryState.setVertexBufferID(this, fCopyProgramArrayBuffer);
bsalomon6df86402015-06-01 10:41:49 -07003383 static const GrGLfloat vdata[] = {
3384 0, 0,
3385 0, 1,
3386 1, 0,
3387 1, 1
3388 };
3389 GL_ALLOC_CALL(this->glInterface(),
3390 BufferData(GR_GL_ARRAY_BUFFER,
3391 (GrGLsizeiptr) sizeof(vdata),
3392 vdata, // data ptr
3393 GR_GL_STATIC_DRAW));
3394}
3395
bsalomon6dea83f2015-12-03 12:58:06 -08003396void GrGLGpu::createWireRectProgram() {
3397 SkASSERT(!fWireRectProgram.fProgram);
3398 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3399 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3400 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3401 const char* version = this->glCaps().glslCaps()->versionDeclString();
3402
3403 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3404 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3405 // uniform using the 4 vbo vertices.
3406 SkString vshaderTxt(version);
3407 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3408 vshaderTxt.append(";");
3409 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3410 vshaderTxt.append(";");
3411 vshaderTxt.append(
3412 "// Wire Rect Program VS\n"
3413 "void main() {"
3414 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3415 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3416 " gl_Position.zw = vec2(0, 1);"
3417 "}"
3418 );
3419
3420 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
3421
3422 SkString fshaderTxt(version);
3423 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3424 *this->glCaps().glslCaps(),
3425 &fshaderTxt);
3426 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3427 fshaderTxt.append(";");
3428 const char* fsOutName;
3429 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3430 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3431 fshaderTxt.append(";");
3432 fsOutName = oFragColor.c_str();
3433 } else {
3434 fsOutName = "gl_FragColor";
3435 }
3436 fshaderTxt.appendf(
3437 "// Write Rect Program FS\n"
3438 "void main() {"
3439 " %s = %s;"
3440 "}",
3441 fsOutName,
3442 uColor.c_str()
3443 );
3444
3445 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3446 const char* str;
3447 GrGLint length;
3448
3449 str = vshaderTxt.c_str();
3450 length = SkToInt(vshaderTxt.size());
3451 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3452 GR_GL_VERTEX_SHADER, &str, &length, 1,
3453 &fStats);
3454
3455 str = fshaderTxt.c_str();
3456 length = SkToInt(fshaderTxt.size());
3457 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3458 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3459 &fStats);
3460
3461 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3462
3463 GL_CALL_RET(fWireRectProgram.fColorUniform,
3464 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3465 GL_CALL_RET(fWireRectProgram.fRectUniform,
3466 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3467 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3468
3469 GL_CALL(DeleteShader(vshader));
3470 GL_CALL(DeleteShader(fshader));
3471 GL_CALL(GenBuffers(1, &fWireRectArrayBuffer));
3472 fHWGeometryState.setVertexBufferID(this, fWireRectArrayBuffer);
3473 static const GrGLfloat vdata[] = {
3474 0, 0,
3475 0, 1,
3476 1, 1,
3477 1, 0,
3478 };
3479 GL_ALLOC_CALL(this->glInterface(),
3480 BufferData(GR_GL_ARRAY_BUFFER,
3481 (GrGLsizeiptr) sizeof(vdata),
3482 vdata, // data ptr
3483 GR_GL_STATIC_DRAW));
3484}
3485
3486void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003487 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3488 // visualization.
3489
bsalomon6dea83f2015-12-03 12:58:06 -08003490 this->handleDirtyContext();
3491 if (!fWireRectProgram.fProgram) {
3492 this->createWireRectProgram();
3493 }
3494
3495 int w = rt->width();
3496 int h = rt->height();
3497
3498 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3499 // whether the render target is flipped or not.
3500 GrGLfloat edges[4];
3501 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3502 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3503 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3504 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3505 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3506 } else {
3507 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3508 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3509 }
3510 edges[0] = 2 * edges[0] / w - 1.0f;
3511 edges[1] = 2 * edges[1] / h - 1.0f;
3512 edges[2] = 2 * edges[2] / w - 1.0f;
3513 edges[3] = 2 * edges[3] / h - 1.0f;
3514
3515 GrGLfloat channels[4];
3516 static const GrGLfloat scale255 = 1.f / 255.f;
3517 channels[0] = GrColorUnpackR(color) * scale255;
3518 channels[1] = GrColorUnpackG(color) * scale255;
3519 channels[2] = GrColorUnpackB(color) * scale255;
3520 channels[3] = GrColorUnpackA(color) * scale255;
3521
3522 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3523 this->flushRenderTarget(glRT, &rect);
3524
3525 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3526 fHWProgramID = fWireRectProgram.fProgram;
3527
3528 fHWGeometryState.setVertexArrayID(this, 0);
3529
3530 GrGLAttribArrayState* attribs =
3531 fHWGeometryState.bindArrayAndBufferToDraw(this, fWireRectArrayBuffer);
3532 attribs->set(this, 0, fWireRectArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0);
3533 attribs->disableUnusedArrays(this, 0x1);
3534
3535 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3536 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3537
3538 GrXferProcessor::BlendInfo blendInfo;
3539 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003540 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003541 this->flushColorWrite(true);
3542 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003543 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003544 this->disableScissor();
3545 GrStencilSettings stencil;
3546 stencil.setDisabled();
3547 this->flushStencil(stencil);
3548
3549 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3550}
3551
3552
bsalomon6df86402015-06-01 10:41:49 -07003553void GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3554 GrSurface* src,
3555 const SkIRect& srcRect,
3556 const SkIPoint& dstPoint) {
3557 int w = srcRect.width();
3558 int h = srcRect.height();
3559
3560 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3561 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
3562 this->bindTexture(0, params, srcTex);
3563
3564 GrGLRenderTarget* dstRT = static_cast<GrGLRenderTarget*>(dst->asRenderTarget());
3565 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
3566 this->flushRenderTarget(dstRT, &dstRect);
3567
bsalomon7ea33f52015-11-22 14:51:00 -08003568 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
3569
3570 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3571 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003572
3573 fHWGeometryState.setVertexArrayID(this, 0);
3574
3575 GrGLAttribArrayState* attribs =
bsalomon7ea33f52015-11-22 14:51:00 -08003576 fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgramArrayBuffer);
bsalomon6dea83f2015-12-03 12:58:06 -08003577 attribs->set(this, 0, fCopyProgramArrayBuffer, 2, GR_GL_FLOAT, false, 2 * sizeof(GrGLfloat), 0);
bsalomond6246342015-06-04 13:57:00 -07003578 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003579
3580 // dst rect edges in NDC (-1 to 1)
3581 int dw = dst->width();
3582 int dh = dst->height();
3583 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3584 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3585 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3586 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3587 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3588 dy0 = -dy0;
3589 dy1 = -dy1;
3590 }
3591
bsalomone5286e02016-01-14 09:24:09 -08003592 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3593 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3594 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3595 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07003596 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003597 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003598 sy0 = sh - sy0;
3599 sy1 = sh - sy1;
3600 }
3601 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
3602 GrGLenum srcTarget = srcTex->target();
3603 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
3604 int sw = src->width();
3605 sx0 /= sw;
3606 sx1 /= sw;
3607 sy0 /= sh;
3608 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003609 }
3610
bsalomon7ea33f52015-11-22 14:51:00 -08003611 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3612 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3613 sx1 - sx0, sy1 - sy0, sx0, sy0));
3614 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003615
3616 GrXferProcessor::BlendInfo blendInfo;
3617 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003618 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003619 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003620 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003621 this->flushHWAAState(dstRT, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003622 this->disableScissor();
3623 GrStencilSettings stencil;
3624 stencil.setDisabled();
3625 this->flushStencil(stencil);
3626
3627 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3628}
3629
3630void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3631 GrSurface* src,
3632 const SkIRect& srcRect,
3633 const SkIPoint& dstPoint) {
3634 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003635 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003636 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003637 GrGLTexture* dstTex = static_cast<GrGLTexture*>(dst->asTexture());
3638 SkASSERT(dstTex);
3639 // We modified the bound FBO
3640 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3641 GrGLIRect srcGLRect;
3642 srcGLRect.setRelativeTo(srcVP,
3643 srcRect.fLeft,
3644 srcRect.fTop,
3645 srcRect.width(),
3646 srcRect.height(),
3647 src->origin());
3648
3649 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003650 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003651 GrGLint dstY;
3652 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3653 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3654 } else {
3655 dstY = dstPoint.fY;
3656 }
bsalomon10528f12015-10-14 12:54:52 -07003657 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon6df86402015-06-01 10:41:49 -07003658 dstPoint.fX, dstY,
3659 srcGLRect.fLeft, srcGLRect.fBottom,
3660 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003661 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003662}
3663
3664bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3665 GrSurface* src,
3666 const SkIRect& srcRect,
3667 const SkIPoint& dstPoint) {
3668 SkASSERT(can_blit_framebuffer(dst, src, this));
3669 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3670 srcRect.width(), srcRect.height());
3671 if (dst == src) {
3672 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3673 return false;
mtklein404b3b22015-05-18 09:29:10 -07003674 }
bsalomon5df6fee2015-05-18 06:26:15 -07003675 }
bsalomon6df86402015-06-01 10:41:49 -07003676
bsalomon6df86402015-06-01 10:41:49 -07003677 GrGLIRect dstVP;
3678 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003679 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3680 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003681 // We modified the bound FBO
3682 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3683 GrGLIRect srcGLRect;
3684 GrGLIRect dstGLRect;
3685 srcGLRect.setRelativeTo(srcVP,
3686 srcRect.fLeft,
3687 srcRect.fTop,
3688 srcRect.width(),
3689 srcRect.height(),
3690 src->origin());
3691 dstGLRect.setRelativeTo(dstVP,
3692 dstRect.fLeft,
3693 dstRect.fTop,
3694 dstRect.width(),
3695 dstRect.height(),
3696 dst->origin());
3697
3698 // BlitFrameBuffer respects the scissor, so disable it.
3699 this->disableScissor();
3700
3701 GrGLint srcY0;
3702 GrGLint srcY1;
3703 // Does the blit need to y-mirror or not?
3704 if (src->origin() == dst->origin()) {
3705 srcY0 = srcGLRect.fBottom;
3706 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
3707 } else {
3708 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
3709 srcY1 = srcGLRect.fBottom;
3710 }
3711 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
3712 srcY0,
3713 srcGLRect.fLeft + srcGLRect.fWidth,
3714 srcY1,
3715 dstGLRect.fLeft,
3716 dstGLRect.fBottom,
3717 dstGLRect.fLeft + dstGLRect.fWidth,
3718 dstGLRect.fBottom + dstGLRect.fHeight,
3719 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07003720 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
3721 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon6df86402015-06-01 10:41:49 -07003722 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003723}
3724
cdalton231c5fd2015-05-13 12:35:36 -07003725void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003726 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003727 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003728 case kTexture_GrXferBarrierType: {
3729 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
3730 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3731 // The render target uses separate storage so no need for glTextureBarrier.
3732 // FIXME: The render target will resolve automatically when its texture is bound,
3733 // but we could resolve only the bounds that will be read if we do it here instead.
3734 return;
3735 }
cdalton9954bc32015-04-29 14:17:00 -07003736 SkASSERT(this->caps()->textureBarrierSupport());
3737 GL_CALL(TextureBarrier());
3738 return;
cdalton231c5fd2015-05-13 12:35:36 -07003739 }
cdalton8917d622015-05-06 13:40:21 -07003740 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003741 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003742 this->caps()->blendEquationSupport());
3743 GL_CALL(BlendBarrier());
3744 return;
bsalomoncb02b382015-08-12 11:14:50 -07003745 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003746 }
3747}
3748
jvanverth88957922015-07-14 11:02:52 -07003749GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -08003750 GrPixelConfig config) {
bsalomon926cb022015-12-17 18:15:11 -08003751 if (!this->caps()->isConfigTexturable(config)) {
3752 return false;
3753 }
bsalomon091f60c2015-11-10 11:54:56 -08003754 GrGLTextureInfo* info = new GrGLTextureInfo;
3755 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08003756 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08003757 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07003758 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
3759 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08003760 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08003761 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08003762 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
3763 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
3764 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
3765 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07003766
bsalomon76148af2016-01-12 11:13:47 -08003767 GrGLenum internalFormat;
3768 GrGLenum externalFormat;
3769 GrGLenum externalType;
3770
3771 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
3772 &externalType)) {
3773 delete info;
3774#ifdef SK_IGNORE_GL_TEXTURE_TARGET
3775 return 0;
3776#else
3777 return reinterpret_cast<GrBackendObject>(nullptr);
3778#endif
3779 }
jvanverth672bb7f2015-07-13 07:19:57 -07003780
bsalomon091f60c2015-11-10 11:54:56 -08003781 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07003782 externalType, pixels));
3783
bsalomon091f60c2015-11-10 11:54:56 -08003784#ifdef SK_IGNORE_GL_TEXTURE_TARGET
3785 GrGLuint id = info->fID;
3786 delete info;
3787 return id;
3788#else
3789 return reinterpret_cast<GrBackendObject>(info);
3790#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003791}
3792
jvanverth88957922015-07-14 11:02:52 -07003793bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08003794#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003795 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003796#else
3797 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
3798#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003799
3800 GrGLboolean result;
3801 GL_CALL_RET(result, IsTexture(texID));
3802
3803 return (GR_GL_TRUE == result);
3804}
3805
bsalomone63ffef2016-02-05 07:17:34 -08003806void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
bsalomon091f60c2015-11-10 11:54:56 -08003807#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07003808 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08003809#else
3810 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
3811 GrGLuint texID = info->fID;
3812#endif
3813
bsalomon67d76202015-11-11 12:40:42 -08003814 if (!abandonTexture) {
3815 GL_CALL(DeleteTextures(1, &texID));
3816 }
bsalomon091f60c2015-11-10 11:54:56 -08003817
3818#ifndef SK_IGNORE_GL_TEXTURE_TARGET
3819 delete info;
3820#endif
jvanverth672bb7f2015-07-13 07:19:57 -07003821}
3822
joshualitt8fd844f2015-12-02 13:36:47 -08003823void GrGLGpu::resetShaderCacheForTesting() const {
3824 fProgramCache->abandon();
3825}
3826
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003827///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -08003828GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
3829 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00003830 const GrGLVertexBuffer* vbuffer,
3831 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07003832 SkASSERT(vbuffer);
bsalomon6df86402015-06-01 10:41:49 -07003833 GrGLuint vbufferID = vbuffer->bufferID();
halcanary96fcdcc2015-08-27 07:41:13 -07003834 GrGLuint* ibufferIDPtr = nullptr;
bsalomon6df86402015-06-01 10:41:49 -07003835 GrGLuint ibufferID;
3836 if (ibuffer) {
3837 ibufferID = ibuffer->bufferID();
3838 ibufferIDPtr = &ibufferID;
3839 }
3840 return this->internalBind(gpu, vbufferID, ibufferIDPtr);
3841}
3842
3843GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu,
3844 GrGLuint vbufferID) {
halcanary96fcdcc2015-08-27 07:41:13 -07003845 return this->internalBind(gpu, vbufferID, nullptr);
bsalomon6df86402015-06-01 10:41:49 -07003846}
3847
3848GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu,
3849 GrGLuint vbufferID,
3850 GrGLuint ibufferID) {
3851 return this->internalBind(gpu, vbufferID, &ibufferID);
3852}
3853
3854GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu,
3855 GrGLuint vbufferID,
3856 GrGLuint* ibufferID) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003857 GrGLAttribArrayState* attribState;
3858
bsalomon6df86402015-06-01 10:41:49 -07003859 if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) {
bsalomon8780bc62015-05-13 09:56:37 -07003860 if (!fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003861 GrGLuint arrayID;
3862 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3863 int attrCount = gpu->glCaps().maxVertexAttributes();
halcanary385fe4d2015-08-26 13:07:48 -07003864 fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003865 }
bsalomon6df86402015-06-01 10:41:49 -07003866 if (ibufferID) {
3867 attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID);
3868 } else {
3869 attribState = fVBOVertexArray->bind(gpu);
3870 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003871 } else {
bsalomon6df86402015-06-01 10:41:49 -07003872 if (ibufferID) {
3873 this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003874 } else {
3875 this->setVertexArrayID(gpu, 0);
3876 }
3877 int attrCount = gpu->glCaps().maxVertexAttributes();
3878 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3879 fDefaultVertexArrayAttribState.resize(attrCount);
3880 }
3881 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003882 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003883 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003884}
bsalomone179a912016-01-20 06:18:10 -08003885
3886bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
3887 GrTextureProducer::CopyParams* copyParams) const {
3888 if (textureParams.isTiled() ||
3889 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
3890 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
3891 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
3892 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
3893 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
3894 copyParams->fWidth = texture->width();
3895 copyParams->fHeight = texture->height();
3896 return true;
3897 }
3898 }
3899 return false;
3900}