blob: 071b11383d1691cf134207da8efeaaaefec8fb41 [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"
cblume55f2d2d2016-02-26 13:20:48 -080024#include "SkMipMap.h"
25#include "SkPixmap.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000026#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000027#include "SkTemplates.h"
cblume55f2d2d2016-02-26 13:20:48 -080028#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000029
bsalomon@google.com0b77d682011-08-19 13:28:54 +000030#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000031#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033#define SKIP_CACHE_CHECK true
34
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000035#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
36 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
37 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
38 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000040 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
41 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
42 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
43#endif
44
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000045///////////////////////////////////////////////////////////////////////////////
46
cdaltonb85a0aa2014-07-21 15:32:44 -070047
cdalton8917d622015-05-06 13:40:21 -070048static const GrGLenum gXfermodeEquation2Blend[] = {
49 // Basic OpenGL blend equations.
50 GR_GL_FUNC_ADD,
51 GR_GL_FUNC_SUBTRACT,
52 GR_GL_FUNC_REVERSE_SUBTRACT,
53
54 // GL_KHR_blend_equation_advanced.
55 GR_GL_SCREEN,
56 GR_GL_OVERLAY,
57 GR_GL_DARKEN,
58 GR_GL_LIGHTEN,
59 GR_GL_COLORDODGE,
60 GR_GL_COLORBURN,
61 GR_GL_HARDLIGHT,
62 GR_GL_SOFTLIGHT,
63 GR_GL_DIFFERENCE,
64 GR_GL_EXCLUSION,
65 GR_GL_MULTIPLY,
66 GR_GL_HSL_HUE,
67 GR_GL_HSL_SATURATION,
68 GR_GL_HSL_COLOR,
69 GR_GL_HSL_LUMINOSITY
70};
71GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
72GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
73GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
74GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
75GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
76GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
77GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
78GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
79GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
80GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
81GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
82GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
83GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
84GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
85GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
86GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
87GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
88GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070089GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070090
twiz@google.com0f31ca72011-03-18 17:38:11 +000091static const GrGLenum gXfermodeCoeff2Blend[] = {
92 GR_GL_ZERO,
93 GR_GL_ONE,
94 GR_GL_SRC_COLOR,
95 GR_GL_ONE_MINUS_SRC_COLOR,
96 GR_GL_DST_COLOR,
97 GR_GL_ONE_MINUS_DST_COLOR,
98 GR_GL_SRC_ALPHA,
99 GR_GL_ONE_MINUS_SRC_ALPHA,
100 GR_GL_DST_ALPHA,
101 GR_GL_ONE_MINUS_DST_ALPHA,
102 GR_GL_CONSTANT_COLOR,
103 GR_GL_ONE_MINUS_CONSTANT_COLOR,
104 GR_GL_CONSTANT_ALPHA,
105 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000106
107 // extended blend coeffs
108 GR_GL_SRC1_COLOR,
109 GR_GL_ONE_MINUS_SRC1_COLOR,
110 GR_GL_SRC1_ALPHA,
111 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000112};
113
bsalomon861e1032014-12-16 07:33:49 -0800114bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000115 static const bool gCoeffReferencesBlendConst[] = {
116 false,
117 false,
118 false,
119 false,
120 false,
121 false,
122 false,
123 false,
124 false,
125 false,
126 true,
127 true,
128 true,
129 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000130
131 // extended blend coeffs
132 false,
133 false,
134 false,
135 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000136 };
137 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700138 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000139
bsalomon@google.com47059542012-06-06 20:51:20 +0000140 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
141 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
142 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
143 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
144 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
145 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
146 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
147 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
148 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
149 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
150 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
151 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
152 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
153 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000154
bsalomon@google.com47059542012-06-06 20:51:20 +0000155 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
156 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
157 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
158 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000159
160 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700161 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000162}
163
reed@google.comac10a2d2010-12-22 21:39:39 +0000164///////////////////////////////////////////////////////////////////////////////
165
egdanielff1d5472015-09-10 08:37:20 -0700166
bsalomon682c2692015-05-22 14:01:46 -0700167GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
168 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700169 SkAutoTUnref<const GrGLInterface> glInterface(
170 reinterpret_cast<const GrGLInterface*>(backendContext));
171 if (!glInterface) {
172 glInterface.reset(GrGLDefaultInterface());
173 } else {
174 glInterface->ref();
175 }
176 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700177 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700178 }
bsalomon682c2692015-05-22 14:01:46 -0700179 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700180 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700181 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700182 }
halcanary96fcdcc2015-08-27 07:41:13 -0700183 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700184}
185
rileya@google.come38160c2012-07-03 18:03:04 +0000186static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000187
bsalomon424cc262015-05-22 10:37:30 -0700188GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000189 : GrGpu(context)
robertphillips@google.com6177e692013-02-28 20:16:25 +0000190 , fGLContext(ctx) {
bsalomon424cc262015-05-22 10:37:30 -0700191 SkASSERT(ctx);
192 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000193
bsalomon1c63bf62014-07-22 13:09:46 -0700194 fHWBoundTextureUniqueIDs.reset(this->glCaps().maxFragmentTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000195
bsalomon424cc262015-05-22 10:37:30 -0700196 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000197 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000198 const GrGLubyte* vendor;
199 const GrGLubyte* renderer;
200 const GrGLubyte* version;
201 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
202 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
203 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800204 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000205 this);
tfarina38406c82014-10-31 07:11:12 -0700206 SkDebugf("------ VENDOR %s\n", vendor);
207 SkDebugf("------ RENDERER %s\n", renderer);
208 SkDebugf("------ VERSION %s\n", version);
209 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700210 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700211 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800212 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000213 }
214
halcanary385fe4d2015-08-26 13:07:48 -0700215 fProgramCache = new ProgramCache(this);
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000216
joshualitt2dd1ae02014-12-03 06:24:10 -0800217 SkASSERT(this->glCaps().maxVertexAttributes() >= GrGeometryProcessor::kMaxVertexAttribs);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000218
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000219 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700220 fTempSrcFBOID = 0;
221 fTempDstFBOID = 0;
222 fStencilClearFBOID = 0;
cdaltonc7103a12014-08-11 14:05:05 -0700223
jvanverthe9c0fc62015-04-29 11:18:05 -0700224 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
kkinnunen5b653572014-08-20 04:13:27 -0700225 fPathRendering.reset(new GrGLPathRendering(this));
cdaltonc7103a12014-08-11 14:05:05 -0700226 }
bsalomon7ea33f52015-11-22 14:51:00 -0800227 this->createCopyPrograms();
bsalomon6dea83f2015-12-03 12:58:06 -0800228 fWireRectProgram.fProgram = 0;
229 fWireRectArrayBuffer = 0;
ethannicholas22793252016-01-30 09:59:10 -0800230 if (this->glCaps().shaderCaps()->plsPathRenderingSupport()) {
231 this->createPLSSetupProgram();
232 }
233 else {
234 memset(&fPLSSetupProgram, 0, sizeof(fPLSSetupProgram));
235 }
236 fHWPLSEnabled = false;
237 fPLSHasBeenUsed = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000238}
239
bsalomon861e1032014-12-16 07:33:49 -0800240GrGLGpu::~GrGLGpu() {
kkinnunen702501d2016-01-13 23:36:45 -0800241 // Delete the path rendering explicitly, since it will need working gpu object to release the
242 // resources the object itself holds.
243 fPathRendering.reset();
244
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000245 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000246 // detach the current program so there is no confusion on OpenGL's part
247 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000248 GL_CALL(UseProgram(0));
249 }
250
egdanield803f272015-03-18 13:01:52 -0700251 if (0 != fTempSrcFBOID) {
252 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800253 }
egdanield803f272015-03-18 13:01:52 -0700254 if (0 != fTempDstFBOID) {
255 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800256 }
egdanield803f272015-03-18 13:01:52 -0700257 if (0 != fStencilClearFBOID) {
258 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800259 }
egdaniel0f5f9672015-02-03 11:10:51 -0800260
bsalomon7ea33f52015-11-22 14:51:00 -0800261 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
262 if (0 != fCopyPrograms[i].fProgram) {
263 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
264 }
bsalomon6df86402015-06-01 10:41:49 -0700265 }
bsalomon6dea83f2015-12-03 12:58:06 -0800266
bsalomon7ea33f52015-11-22 14:51:00 -0800267 if (0 != fCopyProgramArrayBuffer) {
268 GL_CALL(DeleteBuffers(1, &fCopyProgramArrayBuffer));
bsalomon6df86402015-06-01 10:41:49 -0700269 }
270
bsalomon6dea83f2015-12-03 12:58:06 -0800271 if (0 != fWireRectProgram.fProgram) {
272 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
273 }
274
275 if (0 != fWireRectArrayBuffer) {
276 GL_CALL(DeleteBuffers(1, &fWireRectArrayBuffer));
277 }
278
ethannicholas22793252016-01-30 09:59:10 -0800279 if (0 != fPLSSetupProgram.fArrayBuffer) {
280 GL_CALL(DeleteBuffers(1, &fPLSSetupProgram.fArrayBuffer));
281 }
282
283 if (0 != fPLSSetupProgram.fProgram) {
284 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
285 }
286
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000287 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700288}
289
ethannicholas22793252016-01-30 09:59:10 -0800290void GrGLGpu::createPLSSetupProgram() {
cdaltonc08f1962016-02-12 12:14:06 -0800291 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
292 const char* version = glslCaps->versionDeclString();
ethannicholas22793252016-01-30 09:59:10 -0800293
294 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
295 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
296 GrShaderVar::kUniform_TypeModifier);
297 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
298 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
299 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
300
301 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800302 if (glslCaps->noperspectiveInterpolationSupport()) {
303 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
304 vshaderTxt.appendf("#extension %s : require\n", extension);
305 }
306 vTexCoord.addModifier("noperspective");
307 }
308 aVertex.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800309 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800310 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800311 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800312 uPosXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800313 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800314 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800315 vshaderTxt.append(";");
316
317 vshaderTxt.append(
318 "// PLS Setup Program VS\n"
319 "void main() {"
320 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
321 " gl_Position.zw = vec2(0, 1);"
322 "}"
323 );
324
325 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800326 if (glslCaps->noperspectiveInterpolationSupport()) {
327 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
328 fshaderTxt.appendf("#extension %s : require\n", extension);
329 }
330 }
ethannicholas22793252016-01-30 09:59:10 -0800331 fshaderTxt.append("#extension ");
cdaltonc08f1962016-02-12 12:14:06 -0800332 fshaderTxt.append(glslCaps->fbFetchExtensionString());
ethannicholas22793252016-01-30 09:59:10 -0800333 fshaderTxt.append(" : require\n");
334 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
cdaltonc08f1962016-02-12 12:14:06 -0800335 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800336 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -0800337 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800338 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800339 uTexture.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800340 fshaderTxt.append(";");
341
342 fshaderTxt.appendf(
343 "// PLS Setup Program FS\n"
344 GR_GL_PLS_PATH_DATA_DECL
345 "void main() {\n"
346 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
347 " pls.windings = ivec4(0, 0, 0, 0);\n"
348 "}"
349 );
350 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
351 const char* str;
352 GrGLint length;
353
354 str = vshaderTxt.c_str();
355 length = SkToInt(vshaderTxt.size());
356 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
357 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
358
359 str = fshaderTxt.c_str();
360 length = SkToInt(fshaderTxt.size());
361 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
362 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
363
364 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
365
366 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
367 "u_posXform"));
368
369 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
370
371 GL_CALL(DeleteShader(vshader));
372 GL_CALL(DeleteShader(fshader));
373
374 GL_CALL(GenBuffers(1, &fPLSSetupProgram.fArrayBuffer));
375 fHWGeometryState.setVertexBufferID(this, fPLSSetupProgram.fArrayBuffer);
376 static const GrGLfloat vdata[] = {
377 0, 0,
378 0, 1,
379 1, 0,
380 1, 1
381 };
382 GL_ALLOC_CALL(this->glInterface(),
383 BufferData(GR_GL_ARRAY_BUFFER,
384 (GrGLsizeiptr) sizeof(vdata),
385 vdata, // data ptr
386 GR_GL_STATIC_DRAW));
387}
388
bsalomon861e1032014-12-16 07:33:49 -0800389void GrGLGpu::contextAbandoned() {
robertphillipse3371302014-09-17 06:01:06 -0700390 INHERITED::contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -0700391 fProgramCache->abandon();
392 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700393 fTempSrcFBOID = 0;
394 fTempDstFBOID = 0;
395 fStencilClearFBOID = 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800396 fCopyProgramArrayBuffer = 0;
397 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
398 fCopyPrograms[i].fProgram = 0;
399 }
bsalomon6dea83f2015-12-03 12:58:06 -0800400 fWireRectProgram.fProgram = 0;
401 fWireRectArrayBuffer = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700402 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700403 this->glPathRendering()->abandonGpuResources();
404 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000405}
406
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000407///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000408
bsalomon861e1032014-12-16 07:33:49 -0800409void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000410 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000411 if (resetBits & kMisc_GrGLBackendState) {
412 GL_CALL(Disable(GR_GL_DEPTH_TEST));
413 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000414
egdaniel8dd688b2015-01-22 10:16:09 -0800415 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000417 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000418 // Desktop-only state that we never change
419 if (!this->glCaps().isCoreProfile()) {
420 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
421 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
422 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
423 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
424 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
425 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
426 }
427 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
428 // core profile. This seems like a bug since the core spec removes any mention of
429 // GL_ARB_imaging.
430 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
431 GL_CALL(Disable(GR_GL_COLOR_TABLE));
432 }
433 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
434 // Since ES doesn't support glPointSize at all we always use the VS to
435 // set the point size
436 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
437
438 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
439 // currently part of our gl interface. There are probably others as
440 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000441 }
joshualitt58162332014-08-01 06:44:53 -0700442
443 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700444 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700445 // The arm extension requires specifically enabling MSAA fetching per sample.
446 // On some devices this may have a perf hit. Also multiple render targets are disabled
447 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
448 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000449 fHWWriteToColor = kUnknown_TriState;
450 // we only ever use lines in hairline mode
451 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700452 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000453 }
edisonn@google.comba669992013-06-28 16:03:21 +0000454
egdanielb414f252014-07-29 13:15:47 -0700455 if (resetBits & kMSAAEnable_GrGLBackendState) {
456 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700457
egdanieleed519e2016-01-15 11:36:18 -0800458 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800459 if (0 != this->caps()->maxRasterSamples()) {
460 fHWRasterMultisampleEnabled = kUnknown_TriState;
461 fHWNumRasterSamples = 0;
462 }
463
464 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
465 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700466 GL_CALL(CoverageModulation(GR_GL_RGBA));
467 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000468 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000470 fHWActiveTextureUnitIdx = -1; // invalid
471
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000472 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700473 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
474 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000475 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000476 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000477
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000478 if (resetBits & kBlend_GrGLBackendState) {
479 fHWBlendState.invalidate();
480 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000481
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000482 if (resetBits & kView_GrGLBackendState) {
483 fHWScissorSettings.invalidate();
484 fHWViewport.invalidate();
485 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 if (resetBits & kStencil_GrGLBackendState) {
488 fHWStencilSettings.invalidate();
489 fHWStencilTestEnabled = kUnknown_TriState;
490 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000491
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 // Vertex
493 if (resetBits & kVertex_GrGLBackendState) {
494 fHWGeometryState.invalidate();
495 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000497 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700498 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700499 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000500 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000501
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000502 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700503 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700504 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000505 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000506 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000507
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000508 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000509 if (resetBits & kPixelStore_GrGLBackendState) {
510 if (this->glCaps().unpackRowLengthSupport()) {
511 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
512 }
513 if (this->glCaps().packRowLengthSupport()) {
514 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
515 }
516 if (this->glCaps().unpackFlipYSupport()) {
517 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
518 }
519 if (this->glCaps().packFlipYSupport()) {
520 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
521 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000522 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000523
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000524 if (resetBits & kProgram_GrGLBackendState) {
525 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000526 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000527}
528
egdanielcf614fd2015-04-22 13:58:58 -0700529static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000530 // By default, GrRenderTargets are GL's normal orientation so that they
531 // can be drawn to by the outside world without the client having
532 // to render upside down.
533 if (kDefault_GrSurfaceOrigin == origin) {
534 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
535 } else {
536 return origin;
537 }
538}
539
bsalomon6dc6f5f2015-06-18 09:12:16 -0700540GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
541 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800542#ifdef SK_IGNORE_GL_TEXTURE_TARGET
543 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700544 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000545 }
bsalomon091f60c2015-11-10 11:54:56 -0800546#else
547 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
548 if (!info || !info->fID) {
549 return nullptr;
550 }
551#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000552
bsalomon7ea33f52015-11-22 14:51:00 -0800553 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
554 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
555
bsalomonb15b4c12014-10-29 12:41:57 -0700556 GrGLTexture::IDDesc idDesc;
557 GrSurfaceDesc surfDesc;
558
bsalomon091f60c2015-11-10 11:54:56 -0800559#ifdef SK_IGNORE_GL_TEXTURE_TARGET
560 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
cblume55f2d2d2016-02-26 13:20:48 -0800561 // When we create the texture, we only
562 // create GL_TEXTURE_2D at the moment.
563 // External clients can do something different.
564
bsalomon091f60c2015-11-10 11:54:56 -0800565 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
566#else
567 idDesc.fInfo = *info;
568#endif
bsalomone5286e02016-01-14 09:24:09 -0800569
bsalomon7ea33f52015-11-22 14:51:00 -0800570 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
571 if (renderTarget) {
572 // This combination is not supported.
573 return nullptr;
574 }
575 if (!this->glCaps().externalTextureSupport()) {
576 return nullptr;
577 }
bsalomone5286e02016-01-14 09:24:09 -0800578 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
579 if (!this->glCaps().rectangleTextureSupport()) {
580 return nullptr;
581 }
582 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
583 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800584 }
bsalomone5286e02016-01-14 09:24:09 -0800585
586 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800587 // for a render buffer that resolves to the texture. We don't support MSAA textures.
588 if (desc.fSampleCnt && !renderTarget) {
589 return nullptr;
590 }
bsalomon10528f12015-10-14 12:54:52 -0700591
bsalomon6dc6f5f2015-06-18 09:12:16 -0700592 switch (ownership) {
593 case kAdopt_GrWrapOwnership:
594 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
595 break;
596 case kBorrow_GrWrapOwnership:
597 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
598 break;
bsalomone5286e02016-01-14 09:24:09 -0800599 }
bsalomonb15b4c12014-10-29 12:41:57 -0700600
bsalomonb15b4c12014-10-29 12:41:57 -0700601 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
602 surfDesc.fWidth = desc.fWidth;
603 surfDesc.fHeight = desc.fHeight;
604 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700605 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000606 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
607 // assuming the old behaviour, which is that backend textures are always
608 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
609 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
610 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700611 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000612 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700613 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000614 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000615
halcanary96fcdcc2015-08-27 07:41:13 -0700616 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000617 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700618 GrGLRenderTarget::IDDesc rtIDDesc;
egdanielb0e1be22015-04-22 13:27:39 -0700619 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800620 idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700621 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000622 }
halcanary385fe4d2015-08-26 13:07:48 -0700623 texture = new GrGLTextureRenderTarget(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000624 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700625 texture = new GrGLTexture(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000626 }
halcanary96fcdcc2015-08-27 07:41:13 -0700627 if (nullptr == texture) {
628 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000629 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000630
bsalomon@google.come269f212011-11-07 13:29:52 +0000631 return texture;
632}
633
bsalomon6dc6f5f2015-06-18 09:12:16 -0700634GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
635 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700636 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700637 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700638 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700639 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700640 switch (ownership) {
641 case kAdopt_GrWrapOwnership:
642 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
643 break;
644 case kBorrow_GrWrapOwnership:
645 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
646 break;
cblume61214052016-01-26 09:10:48 -0800647 }
senorblancod7395d82015-06-18 13:26:52 -0700648 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000649
bsalomonb15b4c12014-10-29 12:41:57 -0700650 GrSurfaceDesc desc;
651 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700652 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700653 desc.fWidth = wrapDesc.fWidth;
654 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700655 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700656 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
657
egdanielec00d942015-09-14 12:56:10 -0700658 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000659}
660
ericrkf7b8b8a2016-02-24 14:49:51 -0800661GrRenderTarget* GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc,
662 GrWrapOwnership ownership) {
663#ifdef SK_IGNORE_GL_TEXTURE_TARGET
664 if (!desc.fTextureHandle) {
665 return nullptr;
666 }
667#else
668 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
669 if (!info || !info->fID) {
670 return nullptr;
671 }
672#endif
673
674 GrGLTexture::IDDesc idDesc;
675 GrSurfaceDesc surfDesc;
676
677#ifdef SK_IGNORE_GL_TEXTURE_TARGET
678 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
679 // We only support GL_TEXTURE_2D at the moment.
680 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
681#else
682 idDesc.fInfo = *info;
683#endif
684
685 if (GR_GL_TEXTURE_RECTANGLE != idDesc.fInfo.fTarget &&
686 GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
687 // Only texture rectangle and texture 2d are supported. We do not check whether texture
688 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
689 // we assume the necessary support exists.
690 return nullptr;
691 }
692
693 switch (ownership) {
694 case kAdopt_GrWrapOwnership:
695 idDesc.fLifeCycle = GrGpuResource::kAdopted_LifeCycle;
696 break;
697 case kBorrow_GrWrapOwnership:
698 idDesc.fLifeCycle = GrGpuResource::kBorrowed_LifeCycle;
699 break;
700 }
701
702 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
703 surfDesc.fWidth = desc.fWidth;
704 surfDesc.fHeight = desc.fHeight;
705 surfDesc.fConfig = desc.fConfig;
706 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
707 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
708 // assuming the old behaviour, which is that backend textures are always
709 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
710 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
711 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
712 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
713 } else {
714 surfDesc.fOrigin = desc.fOrigin;
715 }
716
717 GrGLRenderTarget::IDDesc rtIDDesc;
718 if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
719 idDesc.fInfo, &rtIDDesc)) {
720 return nullptr;
721 }
722 return GrGLRenderTarget::CreateWrapped(this, surfDesc, rtIDDesc, 0);
723}
724
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000725////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800726
bsalomonf0674512015-07-28 13:26:15 -0700727bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800728 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700729 DrawPreference* drawPreference,
730 WritePixelTempDrawInfo* tempDrawInfo) {
731 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
732 return false;
733 }
734
bsalomon6cb3cbe2015-07-30 07:34:27 -0700735 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
736 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
737 if (!dstSurface->asTexture()) {
738 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800739 } else {
740 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800741 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
742 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800743 return false;
744 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700745 }
746
bsalomon16921ec2015-07-30 15:34:56 -0700747 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
748 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
749 }
750
bsalomon6c9cd552016-01-22 07:17:34 -0800751 // Start off assuming no swizzling
752 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
753 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700754
755 // These settings we will always want if a temp draw is performed. Initially set the config
756 // to srcConfig, though that may be modified if we decide to do a R/G swap.
757 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
758 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
759 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
760 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
761 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
762 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
763
764 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
765
766 if (configsAreRBSwaps) {
767 if (!this->caps()->isConfigTexturable(srcConfig)) {
768 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
769 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800770 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
771 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700772 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
773 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700774 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
775 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800776 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
777 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700778 } else if (kGLES_GrGLStandard == this->glStandard() &&
779 this->glCaps().bgraIsInternalFormat()) {
780 // The internal format and external formats must match texture uploads so we can't
781 // swizzle while uploading when BGRA is a distinct internal format.
782 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
783 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800784 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
785 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700786 }
787 }
788
789 if (!this->glCaps().unpackFlipYSupport() &&
790 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
791 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
792 }
793
794 return true;
795}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000796
jvanverth17aa0472016-01-05 10:41:27 -0800797static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
798 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700799 if (!glTex) {
800 return false;
801 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000802
bsalomon16921ec2015-07-30 15:34:56 -0700803 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
804 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
805 return false;
806 }
807
bsalomone5286e02016-01-14 09:24:09 -0800808 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
809 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800810 return false;
811 }
812
jvanverth17aa0472016-01-05 10:41:27 -0800813 return true;
814}
815
816bool GrGLGpu::onWritePixels(GrSurface* surface,
817 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800818 GrPixelConfig config,
819 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800820 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
821
822 if (!check_write_and_transfer_input(glTex, surface, config)) {
823 return false;
824 }
825
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000826 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700827 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000828
krajcevski145d48c2014-06-11 16:07:50 -0700829 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700830 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800831 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700832 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800833 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800834 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700835 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800836 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800837 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700838 }
839
cblume55f2d2d2016-02-26 13:20:48 -0800840 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000841}
842
jvanverth17aa0472016-01-05 10:41:27 -0800843bool GrGLGpu::onTransferPixels(GrSurface* surface,
844 int left, int top, int width, int height,
845 GrPixelConfig config, GrTransferBuffer* buffer,
846 size_t offset, size_t rowBytes) {
847 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
848
849 if (!check_write_and_transfer_input(glTex, surface, config)) {
850 return false;
851 }
852
853 // For the moment, can't transfer compressed data
854 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
855 return false;
856 }
857
858 this->setScratchTextureUnit();
859 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
860
861 SkASSERT(!buffer->isMapped());
862 GrGLTransferBuffer* glBuffer = reinterpret_cast<GrGLTransferBuffer*>(buffer);
863 // bind the transfer buffer
864 SkASSERT(GR_GL_PIXEL_UNPACK_BUFFER == glBuffer->bufferType() ||
865 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM == glBuffer->bufferType());
866 GL_CALL(BindBuffer(glBuffer->bufferType(), glBuffer->bufferID()));
867
868 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800869 GrMipLevel mipLevel;
870 mipLevel.fPixels = buffer;
871 mipLevel.fRowBytes = rowBytes;
872 SkSTArray<1, GrMipLevel> texels;
873 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800874 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800875 left, top, width, height, config, texels);
jvanverth17aa0472016-01-05 10:41:27 -0800876 if (success) {
877 glTex->texturePriv().dirtyMipMaps(true);
878 return true;
879 }
880
881 return false;
882}
883
bsalomonf46a1242015-12-15 12:37:38 -0800884// For GL_[UN]PACK_ALIGNMENT.
885static inline GrGLint config_alignment(GrPixelConfig config) {
886 SkASSERT(!GrPixelConfigIsCompressed(config));
887 switch (config) {
888 case kAlpha_8_GrPixelConfig:
889 return 1;
890 case kRGB_565_GrPixelConfig:
891 case kRGBA_4444_GrPixelConfig:
892 case kAlpha_half_GrPixelConfig:
893 case kRGBA_half_GrPixelConfig:
894 return 2;
895 case kRGBA_8888_GrPixelConfig:
896 case kBGRA_8888_GrPixelConfig:
897 case kSRGBA_8888_GrPixelConfig:
898 case kRGBA_float_GrPixelConfig:
899 return 4;
900 default:
901 return 0;
902 }
903}
904
bsalomonb15b4c12014-10-29 12:41:57 -0700905static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
906 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700907 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000908 return GR_GL_GET_ERROR(interface);
909 } else {
910 return CHECK_ALLOC_ERROR(interface);
911 }
912}
913
cblume55f2d2d2016-02-26 13:20:48 -0800914/**
915 * Creates storage space for the texture and fills it with texels.
916 *
917 * @param desc The surface descriptor for the texture being created.
918 * @param interface The GL interface in use.
919 * @param target The GL target to which the texture is bound
920 * @param internalFormat The data format used for the internal storage of the texture.
921 * @param externalFormat The data format used for the external storage of the texture.
922 * @param externalType The type of the data used for the external storage of the texture.
923 * @param texels The texel data of the texture being created.
924 * @param baseWidth The width of the texture's base mipmap level
925 * @param baseHeight The height of the texture's base mipmap level
926 * @param succeeded Set to true if allocating and populating the texture completed
927 * without error.
928 */
929static void allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
930 const GrGLInterface& interface,
931 GrGLenum target,
932 GrGLenum internalFormat,
933 GrGLenum externalFormat,
934 GrGLenum externalType,
935 const SkTArray<GrMipLevel>& texels,
936 int baseWidth, int baseHeight,
937 bool* succeeded) {
938 CLEAR_ERROR_BEFORE_ALLOC(&interface);
939 *succeeded = true;
940 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
941 int twoToTheMipLevel = 1 << currentMipLevel;
942 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
943 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
944 const void* currentMipData = texels[currentMipLevel].fPixels;
945 // Even if curremtMipData is nullptr, continue to call TexImage2D.
946 // This will allocate texture memory which we can later populate.
947 GL_ALLOC_CALL(&interface,
948 TexImage2D(target,
949 currentMipLevel,
950 internalFormat,
951 currentWidth,
952 currentHeight,
953 0, // border
954 externalFormat, externalType,
955 currentMipData));
956 GrGLenum error = check_alloc_error(desc, &interface);
957 if (error != GR_GL_NO_ERROR) {
958 *succeeded = false;
959 break;
960 }
961 }
962}
963
964/**
965 * Creates storage space for the texture and fills it with texels.
966 *
967 * @param desc The surface descriptor for the texture being created.
968 * @param interface The GL interface in use.
969 * @param target The GL target to which the texture is bound
970 * @param internalFormat The data format used for the internal storage of the texture.
971 * @param texels The texel data of the texture being created.
972 * @param baseWidth The width of the texture's base mipmap level
973 * @param baseHeight The height of the texture's base mipmap level
974 */
975static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
976 const GrGLInterface& interface,
977 GrGLenum target, GrGLenum internalFormat,
978 const SkTArray<GrMipLevel>& texels,
979 int baseWidth, int baseHeight) {
980 CLEAR_ERROR_BEFORE_ALLOC(&interface);
981 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
982 int twoToTheMipLevel = 1 << currentMipLevel;
983 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
984 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
985
986 // Make sure that the width and height that we pass to OpenGL
987 // is a multiple of the block size.
988 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
989
990 GL_ALLOC_CALL(&interface,
991 CompressedTexImage2D(target,
992 currentMipLevel,
993 internalFormat,
994 currentWidth,
995 currentHeight,
996 0, // border
997 SkToInt(dataSize),
998 texels[currentMipLevel].fPixels));
999
1000 GrGLenum error = check_alloc_error(desc, &interface);
1001 if (error != GR_GL_NO_ERROR) {
1002 return false;
1003 }
1004 }
1005
1006 return true;
1007}
1008
1009/**
1010 * After a texture is created, any state which was altered during its creation
1011 * needs to be restored.
1012 *
1013 * @param interface The GL interface to use.
1014 * @param caps The capabilities of the GL device.
1015 * @param restoreGLRowLength Should the row length unpacking be restored?
1016 * @param glFlipY Did GL flip the texture vertically?
1017 */
1018static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1019 bool restoreGLRowLength, bool glFlipY) {
1020 if (restoreGLRowLength) {
1021 SkASSERT(caps.unpackRowLengthSupport());
1022 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1023 }
1024 if (glFlipY) {
1025 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1026 }
1027}
1028
bsalomon861e1032014-12-16 07:33:49 -08001029bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001030 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001031 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001032 int left, int top, int width, int height,
1033 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001034 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001035 // If we're uploading compressed data then we should be using uploadCompressedTexData
1036 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1037
bsalomon5b30c6f2015-12-17 14:17:34 -08001038 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1039
cblume55f2d2d2016-02-26 13:20:48 -08001040 // texels is const.
1041 // But we may need to flip the texture vertically to prepare it.
1042 // Rather than flip in place and alter the incoming data,
1043 // we allocate a new buffer to flip into.
1044 // This means we need to make a non-const shallow copy of texels.
1045 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1046
1047 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1048 currentMipLevel--) {
1049 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels ||
1050 kNewTexture_UploadType == uploadType || kTransfer_UploadType == uploadType);
1051 }
1052
1053 const GrGLInterface* interface = this->glInterface();
1054 const GrGLCaps& caps = this->glCaps();
1055
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001056 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001057
1058 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001059 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001060 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001061
cblume55f2d2d2016-02-26 13:20:48 -08001062 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1063 int twoToTheMipLevel = 1 << currentMipLevel;
1064 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1065 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1066
1067 if (texelsShallowCopy[currentMipLevel].fPixels == nullptr) {
1068 continue;
1069 }
1070
1071 if (currentHeight > SK_MaxS32 ||
1072 currentWidth > SK_MaxS32) {
1073 return false;
1074 }
1075 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1076 &currentWidth,
1077 &currentHeight,
1078 &texelsShallowCopy[currentMipLevel].fPixels,
1079 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1080 return false;
1081 }
1082 if (currentWidth < 0 || currentHeight < 0) {
1083 return false;
1084 }
1085 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001086
bsalomon5b30c6f2015-12-17 14:17:34 -08001087 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001088 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001089 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001090 GrGLenum externalFormat;
1091 GrGLenum externalType;
1092 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1093 &externalFormat, &externalType)) {
1094 return false;
1095 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001096 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001097 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001098 * because our srcData has extra bytes past each row. If so, we need
1099 * to trim those off here, since GL ES may not let us specify
1100 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001101 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001102 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001103 bool swFlipY = false;
1104 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001105
1106 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
1107 if (caps.unpackFlipYSupport()) {
1108 glFlipY = true;
1109 } else {
1110 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001111 }
cblume55f2d2d2016-02-26 13:20:48 -08001112 }
1113
1114 // in case we need a temporary, trimmed copy of the src pixels
1115 SkAutoSMalloc<128 * 128> tempStorage;
1116
1117 // find the combined size of all the mip levels and the relative offset of
1118 // each into the collective buffer
1119 size_t combined_buffer_size = 0;
1120 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1121 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1122 int twoToTheMipLevel = 1 << currentMipLevel;
1123 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1124 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1125 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1126 individual_mip_offsets.push_back(combined_buffer_size);
1127 combined_buffer_size += trimmedSize;
1128 }
1129 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1130
1131 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1132 if (texelsShallowCopy[currentMipLevel].fPixels == nullptr) {
1133 continue;
1134 }
1135
1136 int twoToTheMipLevel = 1 << currentMipLevel;
1137 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1138 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1139 const size_t trimRowBytes = currentWidth * bpp;
1140
1141 /*
1142 * check whether to allocate a temporary buffer for flipping y or
1143 * because our srcData has extra bytes past each row. If so, we need
1144 * to trim those off here, since GL ES may not let us specify
1145 * GL_UNPACK_ROW_LENGTH.
1146 */
1147 restoreGLRowLength = false;
1148
1149 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
1150 if (caps.unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001151 // can't use this for flipping, only non-neg values allowed. :(
1152 if (rowBytes != trimRowBytes) {
1153 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001154 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001155 restoreGLRowLength = true;
1156 }
jvanverth17aa0472016-01-05 10:41:27 -08001157 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001158 if (trimRowBytes != rowBytes || swFlipY) {
1159 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001160 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1161 if (swFlipY && currentHeight >= 1) {
1162 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001163 }
cblume55f2d2d2016-02-26 13:20:48 -08001164 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1165 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001166 memcpy(dst, src, trimRowBytes);
1167 if (swFlipY) {
1168 src -= rowBytes;
1169 } else {
1170 src += rowBytes;
1171 }
1172 dst += trimRowBytes;
1173 }
1174 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001175 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1176 individual_mip_offsets[currentMipLevel];
1177 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001178 }
jvanverth17aa0472016-01-05 10:41:27 -08001179 } else {
1180 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001181 }
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001182 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001183 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001184 }
cblume55f2d2d2016-02-26 13:20:48 -08001185 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1186 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001187 }
cblume55f2d2d2016-02-26 13:20:48 -08001188
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001189 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001190 if (kNewTexture_UploadType == uploadType &&
1191 0 == left && 0 == top &&
1192 desc.fWidth == width && desc.fHeight == height &&
1193 !desc.fTextureStorageAllocator.fAllocateTextureStorage) {
1194 allocate_and_populate_uncompressed_texture(desc, *interface, target,
1195 internalFormat, externalFormat,
1196 externalType, texelsShallowCopy,
1197 width, height, &succeeded);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001198 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001199 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001200 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001201 }
cblume55f2d2d2016-02-26 13:20:48 -08001202 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1203 currentMipLevel++) {
1204 int twoToTheMipLevel = 1 << currentMipLevel;
1205 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1206 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1207 if (texelsShallowCopy[currentMipLevel].fPixels == nullptr) {
1208 continue;
1209 }
1210
1211 GL_CALL(TexSubImage2D(target,
1212 currentMipLevel,
1213 left, top,
1214 currentWidth,
1215 currentHeight,
1216 externalFormat, externalType,
1217 texelsShallowCopy[currentMipLevel].fPixels));
1218 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001219 }
1220
cblume55f2d2d2016-02-26 13:20:48 -08001221 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1222
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001223 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001224}
1225
krajcevski145d48c2014-06-11 16:07:50 -07001226// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001227// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001228// create a CompressedTexData struct that takes a desc/ptr and figures out
1229// the proper upload semantics. Then users can construct this function how they
1230// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001231bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001232 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001233 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001234 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001235 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001236 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
cblume55f2d2d2016-02-26 13:20:48 -08001237 SkASSERT(kTransfer_UploadType != uploadType &&
1238 (texels[0].fPixels || kNewTexture_UploadType != uploadType));
krajcevski9c0e6292014-06-02 07:38:14 -07001239
1240 // No support for software flip y, yet...
1241 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1242
cblume55f2d2d2016-02-26 13:20:48 -08001243 const GrGLInterface* interface = this->glInterface();
1244 const GrGLCaps& caps = this->glCaps();
1245
krajcevski145d48c2014-06-11 16:07:50 -07001246 if (-1 == width) {
1247 width = desc.fWidth;
1248 }
1249#ifdef SK_DEBUG
1250 else {
1251 SkASSERT(width <= desc.fWidth);
1252 }
1253#endif
1254
1255 if (-1 == height) {
1256 height = desc.fHeight;
1257 }
1258#ifdef SK_DEBUG
1259 else {
1260 SkASSERT(height <= desc.fHeight);
1261 }
1262#endif
1263
bsalomon76148af2016-01-12 11:13:47 -08001264 // We only need the internal format for compressed 2D textures.
1265 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001266 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001267 return false;
1268 }
krajcevski9c0e6292014-06-02 07:38:14 -07001269
jvanverth17aa0472016-01-05 10:41:27 -08001270 if (kNewTexture_UploadType == uploadType) {
cblume55f2d2d2016-02-26 13:20:48 -08001271 return allocate_and_populate_compressed_texture(desc, *interface, target, internalFormat,
1272 texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001273 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001274 // Paletted textures can't be updated.
1275 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1276 return false;
1277 }
cblume55f2d2d2016-02-26 13:20:48 -08001278 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1279 if (texels[currentMipLevel].fPixels == nullptr) {
1280 continue;
1281 }
1282
1283 int twoToTheMipLevel = 1 << currentMipLevel;
1284 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1285 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1286
1287 // Make sure that the width and height that we pass to OpenGL
1288 // is a multiple of the block size.
1289 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1290 currentHeight);
1291 GL_CALL(CompressedTexSubImage2D(target,
1292 currentMipLevel,
1293 left, top,
1294 currentWidth,
1295 currentHeight,
1296 internalFormat,
1297 SkToInt(dataSize),
1298 texels[currentMipLevel].fPixels));
1299 }
krajcevski145d48c2014-06-11 16:07:50 -07001300 }
krajcevski9c0e6292014-06-02 07:38:14 -07001301
bsalomond4cb9222014-08-11 14:19:09 -07001302 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001303}
1304
bsalomon424cc262015-05-22 10:37:30 -07001305static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001306 int sampleCount,
1307 GrGLenum format,
1308 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001309 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001310 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001311 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001312 case GrGLCaps::kDesktop_ARB_MSFBOType:
1313 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001314 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001315 case GrGLCaps::kES_3_0_MSFBOType:
1316 GL_ALLOC_CALL(ctx.interface(),
1317 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1318 sampleCount,
1319 format,
1320 width, height));
1321 break;
1322 case GrGLCaps::kES_Apple_MSFBOType:
1323 GL_ALLOC_CALL(ctx.interface(),
1324 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1325 sampleCount,
1326 format,
1327 width, height));
1328 break;
1329 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1330 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1331 GL_ALLOC_CALL(ctx.interface(),
1332 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1333 sampleCount,
1334 format,
1335 width, height));
1336 break;
1337 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001338 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001339 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001340 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001341 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001342}
1343
egdanielb0e1be22015-04-22 13:27:39 -07001344bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
1345 GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -08001346 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001347 GrGLRenderTarget::IDDesc* idDesc) {
1348 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001349 idDesc->fRTFBOID = 0;
1350 idDesc->fTexFBOID = 0;
egdanielb0e1be22015-04-22 13:27:39 -07001351 idDesc->fLifeCycle = lifeCycle;
vbuzinovdded6962015-06-12 08:59:45 -07001352 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
1353 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
1354 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001355
1356 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357
bsalomona11e5fc2015-12-18 07:59:41 -08001358 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001359
bsalomonb15b4c12014-10-29 12:41:57 -07001360 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001361 goto FAILED;
1362 }
1363
egdanield803f272015-03-18 13:01:52 -07001364 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1365 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366 goto FAILED;
1367 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001368
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001369 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1370 // the texture bound to the other. The exception is the IMG multisample extension. With this
1371 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1372 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001373 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001374 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001375 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001376 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001377 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001378 goto FAILED;
1379 }
bsalomon76148af2016-01-12 11:13:47 -08001380 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1381 return false;
1382 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001383 } else {
egdanield803f272015-03-18 13:01:52 -07001384 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385 }
1386
egdanield803f272015-03-18 13:01:52 -07001387 // below here we may bind the FBO
1388 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1389 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001390 SkASSERT(desc.fSampleCnt > 0);
1391 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001392 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001393 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001394 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001395 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396 goto FAILED;
1397 }
egdanield803f272015-03-18 13:01:52 -07001398 fStats.incRenderTargetBinds();
1399 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1400 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001401 GR_GL_COLOR_ATTACHMENT0,
1402 GR_GL_RENDERBUFFER,
1403 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001404 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1405 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001406 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1407 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1408 goto FAILED;
1409 }
bsalomon424cc262015-05-22 10:37:30 -07001410 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001411 }
1412 }
egdanield803f272015-03-18 13:01:52 -07001413 fStats.incRenderTargetBinds();
1414 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001415
bsalomonb15b4c12014-10-29 12:41:57 -07001416 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001417 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001418 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001419 texInfo.fTarget,
1420 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001421 } else {
egdanield803f272015-03-18 13:01:52 -07001422 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001423 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001424 texInfo.fTarget,
1425 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001426 }
bsalomonb15b4c12014-10-29 12:41:57 -07001427 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1428 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001429 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001430 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1431 goto FAILED;
1432 }
bsalomon424cc262015-05-22 10:37:30 -07001433 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001434 }
1435
1436 return true;
1437
1438FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001439 if (idDesc->fMSColorRenderbufferID) {
1440 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001441 }
egdanield803f272015-03-18 13:01:52 -07001442 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1443 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444 }
egdanield803f272015-03-18 13:01:52 -07001445 if (idDesc->fTexFBOID) {
1446 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001447 }
1448 return false;
1449}
1450
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001451// good to set a break-point here to know when createTexture fails
1452static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001453// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001454 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001455}
1456
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001457#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001458static size_t as_size_t(int x) {
1459 return x;
1460}
1461#endif
1462
cblume55f2d2d2016-02-26 13:20:48 -08001463static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface,
1464 GrGpuResource::LifeCycle lifeCycle) {
1465 GrGLTexture::IDDesc idDesc;
1466 idDesc.fInfo.fID = 0;
1467 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
1468 idDesc.fLifeCycle = lifeCycle;
1469 // When we create the texture, we only
1470 // create GL_TEXTURE_2D at the moment.
1471 // External clients can do something different.
1472 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1473 return idDesc;
1474}
1475
1476static void set_initial_texture_params(const GrGLInterface* interface,
1477 const GrGLTextureInfo& info,
1478 GrGLTexture::TexParams* initialTexParams) {
1479 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1480 // drivers have a bug where an FBO won't be complete if it includes a
1481 // texture that is not mipmap complete (considering the filter in use).
1482 // we only set a subset here so invalidate first
1483 initialTexParams->invalidate();
1484 initialTexParams->fMinFilter = GR_GL_NEAREST;
1485 initialTexParams->fMagFilter = GR_GL_NEAREST;
1486 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1487 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1488 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1489 GR_GL_TEXTURE_MAG_FILTER,
1490 initialTexParams->fMagFilter));
1491 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1492 GR_GL_TEXTURE_MIN_FILTER,
1493 initialTexParams->fMinFilter));
1494 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1495 GR_GL_TEXTURE_WRAP_S,
1496 initialTexParams->fWrapS));
1497 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1498 GR_GL_TEXTURE_WRAP_T,
1499 initialTexParams->fWrapT));
1500}
1501
egdanielb0e1be22015-04-22 13:27:39 -07001502GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1503 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -08001504 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001505 // We fail if the MSAA was requested and is not available.
1506 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001507 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001508 return return_null_texture();
1509 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001510
bsalomonf2703d82014-10-28 14:33:06 -07001511 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001512
bsalomonb15b4c12014-10-29 12:41:57 -07001513 GrGLTexture::IDDesc idDesc;
egdanielb0e1be22015-04-22 13:27:39 -07001514 idDesc.fLifeCycle = lifeCycle;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001515 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001516 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001517 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001518 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001519
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001520 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001522 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001523 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001524 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001525
bsalomon091f60c2015-11-10 11:54:56 -08001526 if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fInfo, &rtIDDesc)) {
1527 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001528 return return_null_texture();
1529 }
halcanary385fe4d2015-08-26 13:07:48 -07001530 tex = new GrGLTextureRenderTarget(this, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001531 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001532 bool wasMipMapDataProvided = false;
1533 if (texels.count() > 1) {
1534 wasMipMapDataProvided = true;
1535 }
1536 tex = new GrGLTexture(this, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001537 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001538 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001539#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001540 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001541 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001542#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001543 return tex;
1544}
1545
egdanielb0e1be22015-04-22 13:27:39 -07001546GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
1547 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -08001548 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001549 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001550 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001551 return return_null_texture();
1552 }
1553
cblume55f2d2d2016-02-26 13:20:48 -08001554 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface(), lifeCycle);
bsalomon091f60c2015-11-10 11:54:56 -08001555 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001556 return return_null_texture();
1557 }
1558
1559 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001560 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001561
krajcevski9c0e6292014-06-02 07:38:14 -07001562 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001563 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001564
cblume55f2d2d2016-02-26 13:20:48 -08001565 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001566 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001567 return return_null_texture();
1568 }
1569
1570 GrGLTexture* tex;
halcanary385fe4d2015-08-26 13:07:48 -07001571 tex = new GrGLTexture(this, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001572 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1573#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001574 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001575 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001576#endif
1577 return tex;
1578}
1579
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001580namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001581
egdaniel8dc7c3a2015-04-16 11:22:42 -07001582const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001583
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001584void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001585 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001586
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001587 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001588 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001589 (kUnknownBitCount == format->fTotalBits));
1590 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001591 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001592 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1593 (GrGLint*)&format->fStencilBits);
1594 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001595 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001596 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1597 (GrGLint*)&format->fTotalBits);
1598 format->fTotalBits += format->fStencilBits;
1599 } else {
1600 format->fTotalBits = format->fStencilBits;
1601 }
1602 }
1603}
1604}
1605
egdanielff1d5472015-09-10 08:37:20 -07001606int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001607 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001608 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001609 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1610 // Default to unsupported, set this if we find a stencil format that works.
1611 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001612 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001613 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001614 GL_CALL(GenTextures(1, &colorID));
1615 this->setScratchTextureUnit();
1616 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1617 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1618 GR_GL_TEXTURE_MAG_FILTER,
1619 GR_GL_NEAREST));
1620 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1621 GR_GL_TEXTURE_MIN_FILTER,
1622 GR_GL_NEAREST));
1623 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1624 GR_GL_TEXTURE_WRAP_S,
1625 GR_GL_CLAMP_TO_EDGE));
1626 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1627 GR_GL_TEXTURE_WRAP_T,
1628 GR_GL_CLAMP_TO_EDGE));
1629
bsalomon76148af2016-01-12 11:13:47 -08001630 GrGLenum internalFormat;
1631 GrGLenum externalFormat;
1632 GrGLenum externalType;
1633 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1634 &externalType)) {
1635 return false;
1636 }
egdanielff1d5472015-09-10 08:37:20 -07001637 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1638 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001639 0,
bsalomon76148af2016-01-12 11:13:47 -08001640 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001641 kSize,
1642 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001643 0,
bsalomon76148af2016-01-12 11:13:47 -08001644 externalFormat,
1645 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001646 NULL));
bsalomon30447372015-12-21 09:03:05 -08001647 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001648 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001649 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001650 }
1651
1652 // unbind the texture from the texture unit before binding it to the frame buffer
1653 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1654
1655 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001656 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001657 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001658 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001659 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1660 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1661 GR_GL_COLOR_ATTACHMENT0,
1662 GR_GL_TEXTURE_2D,
1663 colorID,
1664 0));
bsalomon30447372015-12-21 09:03:05 -08001665 GrGLuint sbRBID = 0;
1666 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001667
1668 // look over formats till I find a compatible one
1669 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001670 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001671 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001672 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1673 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1674 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1675 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1676 sFmt.fInternalFormat,
1677 kSize, kSize));
1678 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001679 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001680 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001681 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001682 if (sFmt.fPacked) {
1683 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1684 GR_GL_DEPTH_ATTACHMENT,
1685 GR_GL_RENDERBUFFER, sbRBID));
1686 } else {
1687 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1688 GR_GL_DEPTH_ATTACHMENT,
1689 GR_GL_RENDERBUFFER, 0));
1690 }
1691 GrGLenum status;
1692 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1693 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1694 firstWorkingStencilFormatIndex = i;
1695 break;
1696 }
egdanielff1d5472015-09-10 08:37:20 -07001697 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1698 GR_GL_STENCIL_ATTACHMENT,
1699 GR_GL_RENDERBUFFER, 0));
1700 if (sFmt.fPacked) {
1701 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1702 GR_GL_DEPTH_ATTACHMENT,
1703 GR_GL_RENDERBUFFER, 0));
1704 }
egdanielff1d5472015-09-10 08:37:20 -07001705 }
1706 }
bsalomon30447372015-12-21 09:03:05 -08001707 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001708 }
1709 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001710 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1711 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001712 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001713 }
bsalomon30447372015-12-21 09:03:05 -08001714 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001715}
1716
erikchen9a1ed5d2016-02-10 16:32:34 -08001717bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001718 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1719 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001720 if (desc.fTextureStorageAllocator.fAllocateTextureStorage) {
cblume55f2d2d2016-02-26 13:20:48 -08001721 return this->createTextureExternalAllocatorImpl(desc, info, texels);
erikchen9a1ed5d2016-02-10 16:32:34 -08001722 }
1723
1724 info->fID = 0;
1725 info->fTarget = GR_GL_TEXTURE_2D;
1726 GL_CALL(GenTextures(1, &(info->fID)));
1727
1728 if (!info->fID) {
1729 return false;
1730 }
1731
1732 this->setScratchTextureUnit();
1733 GL_CALL(BindTexture(info->fTarget, info->fID));
1734
1735 if (renderTarget && this->glCaps().textureUsageSupport()) {
1736 // provides a hint about how this texture will be used
1737 GL_CALL(TexParameteri(info->fTarget,
1738 GR_GL_TEXTURE_USAGE,
1739 GR_GL_FRAMEBUFFER_ATTACHMENT));
1740 }
1741
cblume55f2d2d2016-02-26 13:20:48 -08001742 if (info) {
1743 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1744 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001745 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1746 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001747 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001748 GL_CALL(DeleteTextures(1, &(info->fID)));
1749 return false;
1750 }
1751 return true;
1752}
1753
cblume55f2d2d2016-02-26 13:20:48 -08001754bool GrGLGpu::createTextureExternalAllocatorImpl(const GrSurfaceDesc& desc,
1755 GrGLTextureInfo* info,
1756 const SkTArray<GrMipLevel>& texels) {
1757 // We do not make SkTArray available outside of Skia,
1758 // and so we do not want to allow mipmaps to external
1759 // allocators just yet.
1760 SkASSERT(texels.count() == 1);
1761 SkSTArray<1, GrMipLevel> texelsShallowCopy(1);
1762 texelsShallowCopy.push_back(texels[0]);
1763
erikchen9a1ed5d2016-02-10 16:32:34 -08001764 switch (desc.fTextureStorageAllocator.fAllocateTextureStorage(
1765 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info),
cblume55f2d2d2016-02-26 13:20:48 -08001766 desc.fWidth, desc.fHeight, desc.fConfig, texelsShallowCopy[0].fPixels,
1767 desc.fOrigin)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001768 case GrTextureStorageAllocator::Result::kSucceededAndUploaded:
1769 return true;
1770 case GrTextureStorageAllocator::Result::kFailed:
1771 return false;
1772 case GrTextureStorageAllocator::Result::kSucceededWithoutUpload:
1773 break;
1774 }
1775
1776 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1777 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001778 desc.fConfig, texelsShallowCopy)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001779 desc.fTextureStorageAllocator.fDeallocateTextureStorage(
1780 desc.fTextureStorageAllocator.fCtx, reinterpret_cast<GrBackendObject>(info));
1781 return false;
1782 }
1783 return true;
1784}
1785
egdanielec00d942015-09-14 12:56:10 -07001786GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1787 int width,
1788 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001789 SkASSERT(width >= rt->width());
1790 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001791
vbuzinovdded6962015-06-12 08:59:45 -07001792 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001793 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001794
egdanielff1d5472015-09-10 08:37:20 -07001795 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001796 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001797 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001798 }
egdanielff1d5472015-09-10 08:37:20 -07001799
1800 if (!sbDesc.fRenderbufferID) {
1801 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1802 }
1803 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001804 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001805 }
1806 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1807 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1808 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1809 // we do this "if" so that we don't call the multisample
1810 // version on a GL that doesn't have an MSAA extension.
1811 if (samples > 0) {
1812 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1813 samples,
1814 sFmt.fInternalFormat,
1815 width, height));
1816 } else {
1817 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1818 sFmt.fInternalFormat,
1819 width, height));
1820 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1821 }
1822 fStats.incStencilAttachmentCreates();
1823 // After sized formats we attempt an unsized format and take
1824 // whatever sizes GL gives us. In that case we query for the size.
1825 GrGLStencilAttachment::Format format = sFmt;
1826 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001827 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1828 sbDesc,
1829 width,
1830 height,
1831 samples,
1832 format);
1833 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001834}
1835
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001836////////////////////////////////////////////////////////////////////////////////
1837
jvanverth73063dc2015-12-03 09:15:47 -08001838// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1839// objects are implemented as client-side-arrays on tile-deferred architectures.
1840#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1841
bsalomon861e1032014-12-16 07:33:49 -08001842GrVertexBuffer* GrGLGpu::onCreateVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001843 GrGLVertexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001844 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001845 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001846
jvanverth73063dc2015-12-03 09:15:47 -08001847 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001848 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001849 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 return vertexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001851 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001852 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001853 GL_CALL(GenBuffers(1, &desc.fID));
1854 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001855 fHWGeometryState.setVertexBufferID(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001856 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1857 // make sure driver can allocate memory for this buffer
1858 GL_ALLOC_CALL(this->glInterface(),
1859 BufferData(GR_GL_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001860 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001861 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001862 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001863 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1864 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001865 this->notifyVertexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001866 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001867 }
halcanary385fe4d2015-08-26 13:07:48 -07001868 GrGLVertexBuffer* vertexBuffer = new GrGLVertexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001869 return vertexBuffer;
1870 }
halcanary96fcdcc2015-08-27 07:41:13 -07001871 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001872 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001873}
1874
bsalomon861e1032014-12-16 07:33:49 -08001875GrIndexBuffer* GrGLGpu::onCreateIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.come49ad452013-02-20 19:33:20 +00001876 GrGLIndexBuffer::Desc desc;
jvanverth73063dc2015-12-03 09:15:47 -08001877 desc.fUsage = dynamic ? GrGLBufferImpl::kDynamicDraw_Usage : GrGLBufferImpl::kStaticDraw_Usage;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001878 desc.fSizeInBytes = size;
bsalomon@google.come49ad452013-02-20 19:33:20 +00001879
jvanverth73063dc2015-12-03 09:15:47 -08001880 if (this->glCaps().useNonVBOVertexAndIndexDynamicData() && dynamic) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001881 desc.fID = 0;
halcanary385fe4d2015-08-26 13:07:48 -07001882 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001883 return indexBuffer;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001884 } else {
kkinnunen546eb5c2015-12-11 00:05:33 -08001885 desc.fID = 0;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001886 GL_CALL(GenBuffers(1, &desc.fID));
1887 if (desc.fID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00001888 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, desc.fID);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001889 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1890 // make sure driver can allocate memory for this buffer
1891 GL_ALLOC_CALL(this->glInterface(),
1892 BufferData(GR_GL_ELEMENT_ARRAY_BUFFER,
robertphillips@google.come9cd27d2013-10-16 17:48:11 +00001893 (GrGLsizeiptr) desc.fSizeInBytes,
halcanary96fcdcc2015-08-27 07:41:13 -07001894 nullptr, // data ptr
jvanverth73063dc2015-12-03 09:15:47 -08001895 dynamic ? DYNAMIC_USAGE_PARAM : GR_GL_STATIC_DRAW));
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001896 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1897 GL_CALL(DeleteBuffers(1, &desc.fID));
bsalomon@google.com6918d482013-03-07 19:09:11 +00001898 this->notifyIndexBufferDelete(desc.fID);
halcanary96fcdcc2015-08-27 07:41:13 -07001899 return nullptr;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001900 }
halcanary385fe4d2015-08-26 13:07:48 -07001901 GrIndexBuffer* indexBuffer = new GrGLIndexBuffer(this, desc);
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +00001902 return indexBuffer;
1903 }
halcanary96fcdcc2015-08-27 07:41:13 -07001904 return nullptr;
reed@google.comac10a2d2010-12-22 21:39:39 +00001905 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001906}
1907
jvanverthd7a2c1f2015-12-07 07:36:44 -08001908GrTransferBuffer* GrGLGpu::onCreateTransferBuffer(size_t size, TransferType xferType) {
1909 GrGLCaps::TransferBufferType xferBufferType = this->ctxInfo().caps()->transferBufferType();
1910 if (GrGLCaps::kNone_TransferBufferType == xferBufferType) {
1911 return nullptr;
1912 }
1913
jvanverth73063dc2015-12-03 09:15:47 -08001914 GrGLTransferBuffer::Desc desc;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001915 bool toGpu = (kCpuToGpu_TransferType == xferType);
jvanverth73063dc2015-12-03 09:15:47 -08001916 desc.fUsage = toGpu ? GrGLBufferImpl::kStreamDraw_Usage : GrGLBufferImpl::kStreamRead_Usage;
1917
1918 desc.fSizeInBytes = size;
kkinnunen546eb5c2015-12-11 00:05:33 -08001919 desc.fID = 0;
jvanverth73063dc2015-12-03 09:15:47 -08001920 GL_CALL(GenBuffers(1, &desc.fID));
1921 if (desc.fID) {
1922 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
jvanverthd7a2c1f2015-12-07 07:36:44 -08001923 // make sure driver can allocate memory for this bmapuffer
jvanverth17aa0472016-01-05 10:41:27 -08001924 GrGLenum target;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001925 if (GrGLCaps::kChromium_TransferBufferType == xferBufferType) {
jvanverth17aa0472016-01-05 10:41:27 -08001926 target = toGpu ? GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM
jvanverthd7a2c1f2015-12-07 07:36:44 -08001927 : GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
1928 } else {
1929 SkASSERT(GrGLCaps::kPBO_TransferBufferType == xferBufferType);
jvanverth17aa0472016-01-05 10:41:27 -08001930 target = toGpu ? GR_GL_PIXEL_UNPACK_BUFFER : GR_GL_PIXEL_PACK_BUFFER;
jvanverthd7a2c1f2015-12-07 07:36:44 -08001931 }
jvanverth17aa0472016-01-05 10:41:27 -08001932 GL_CALL(BindBuffer(target, desc.fID));
1933 GL_ALLOC_CALL(this->glInterface(),
1934 BufferData(target,
jvanverth73063dc2015-12-03 09:15:47 -08001935 (GrGLsizeiptr) desc.fSizeInBytes,
1936 nullptr, // data ptr
1937 (toGpu ? GR_GL_STREAM_DRAW : GR_GL_STREAM_READ)));
1938 if (CHECK_ALLOC_ERROR(this->glInterface()) != GR_GL_NO_ERROR) {
1939 GL_CALL(DeleteBuffers(1, &desc.fID));
1940 return nullptr;
1941 }
jvanverth17aa0472016-01-05 10:41:27 -08001942 GrTransferBuffer* transferBuffer = new GrGLTransferBuffer(this, desc, target);
jvanverth73063dc2015-12-03 09:15:47 -08001943 return transferBuffer;
1944 }
1945
1946 return nullptr;
1947}
1948
bsalomon3e791242014-12-17 13:43:13 -08001949void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001950 const GrGLIRect& rtViewport,
1951 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001952 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001953 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001954 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001955 scissorState.rect().fLeft,
1956 scissorState.rect().fTop,
1957 scissorState.rect().width(),
1958 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001959 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001960 // if the scissor fully contains the viewport then we fall through and
1961 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001962 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001963 if (fHWScissorSettings.fRect != scissor) {
1964 scissor.pushToGLScissor(this->glInterface());
1965 fHWScissorSettings.fRect = scissor;
1966 }
1967 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1968 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1969 fHWScissorSettings.fEnabled = kYes_TriState;
1970 }
1971 return;
1972 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001973 }
joshualitt77b13072014-10-27 14:51:01 -07001974
1975 // See fall through note above
1976 this->disableScissor();
1977}
1978
cdaltond0a840d2015-03-16 17:19:58 -07001979bool GrGLGpu::flushGLState(const DrawArgs& args) {
egdaniel080e6732014-12-22 07:35:52 -08001980 GrXferProcessor::BlendInfo blendInfo;
egdaniel8dd688b2015-01-22 10:16:09 -08001981 const GrPipeline& pipeline = *args.fPipeline;
bsalomon2047b782015-12-21 13:12:54 -08001982 args.fPipeline->getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001983
egdaniel080e6732014-12-22 07:35:52 -08001984 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001985 this->flushDrawFace(pipeline.getDrawFace());
bsalomonbc3d0de2014-12-15 13:45:03 -08001986
bsalomon6df86402015-06-01 10:41:49 -07001987 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(args));
1988 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001989 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08001990 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08001991 }
1992
bsalomon6df86402015-06-01 10:41:49 -07001993 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001994 if (fHWProgramID != programID) {
1995 GL_CALL(UseProgram(programID));
1996 fHWProgramID = programID;
1997 }
1998
egdanield803f272015-03-18 13:01:52 -07001999 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002000 // Swizzle the blend to match what the shader will output.
2001 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
2002 args.fPipeline->getRenderTarget()->config());
2003 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07002004 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002005
cdalton42717652015-06-18 11:54:30 -07002006 SkSTArray<8, const GrTextureAccess*> textureAccesses;
joshualitt465283c2015-09-11 08:19:35 -07002007 program->setData(*args.fPrimitiveProcessor, pipeline, &textureAccesses);
cdalton42717652015-06-18 11:54:30 -07002008
2009 int numTextureAccesses = textureAccesses.count();
2010 for (int i = 0; i < numTextureAccesses; i++) {
2011 this->bindTexture(i, textureAccesses[i]->getParams(),
2012 static_cast<GrGLTexture*>(textureAccesses[i]->getTexture()));
2013 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002014
egdaniel8dd688b2015-01-22 10:16:09 -08002015 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
2016 this->flushStencil(pipeline.getStencil());
2017 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltonaf8bc7d2016-02-05 09:35:20 -08002018 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002019
2020 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002021 // to be msaa-resolved (which will modify bound FBO state).
cdaltond4727922015-11-10 12:49:06 -08002022 this->flushRenderTarget(glRT, nullptr);
bsalomonbc3d0de2014-12-15 13:45:03 -08002023
2024 return true;
2025}
2026
joshualitt873ad0e2015-01-20 09:08:51 -08002027void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
bsalomone64eb572015-05-07 11:35:55 -07002028 const GrNonInstancedVertices& vertices,
bsalomonbc3d0de2014-12-15 13:45:03 -08002029 size_t* indexOffsetInBytes) {
2030 GrGLVertexBuffer* vbuf;
bsalomone64eb572015-05-07 11:35:55 -07002031 vbuf = (GrGLVertexBuffer*) vertices.vertexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002032
2033 SkASSERT(vbuf);
2034 SkASSERT(!vbuf->isMapped());
2035
halcanary96fcdcc2015-08-27 07:41:13 -07002036 GrGLIndexBuffer* ibuf = nullptr;
bsalomone64eb572015-05-07 11:35:55 -07002037 if (vertices.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002038 SkASSERT(indexOffsetInBytes);
2039
2040 *indexOffsetInBytes = 0;
bsalomone64eb572015-05-07 11:35:55 -07002041 ibuf = (GrGLIndexBuffer*)vertices.indexBuffer();
bsalomonbc3d0de2014-12-15 13:45:03 -08002042
2043 SkASSERT(ibuf);
2044 SkASSERT(!ibuf->isMapped());
2045 *indexOffsetInBytes += ibuf->baseOffset();
2046 }
2047 GrGLAttribArrayState* attribState =
2048 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
2049
joshualitt873ad0e2015-01-20 09:08:51 -08002050 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002051 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002052
joshualitt873ad0e2015-01-20 09:08:51 -08002053 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002054
bsalomone64eb572015-05-07 11:35:55 -07002055 size_t vertexOffsetInBytes = stride * vertices.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08002056
2057 vertexOffsetInBytes += vbuf->baseOffset();
2058
bsalomonbc3d0de2014-12-15 13:45:03 -08002059 uint32_t usedAttribArraysMask = 0;
2060 size_t offset = 0;
2061
2062 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002063 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002064 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002065 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002066 attribState->set(this,
2067 attribIndex,
bsalomon6df86402015-06-01 10:41:49 -07002068 vbuf->bufferID(),
cdalton793dc262016-02-08 10:11:47 -08002069 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002070 stride,
2071 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002072 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002073 }
2074 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2075 }
2076}
2077
joshualitt873ad0e2015-01-20 09:08:51 -08002078void GrGLGpu::buildProgramDesc(GrProgramDesc* desc,
2079 const GrPrimitiveProcessor& primProc,
joshualitt465283c2015-09-11 08:19:35 -07002080 const GrPipeline& pipeline) const {
bsalomon7f9b2e42016-01-12 13:29:26 -08002081 if (!GrGLProgramDescBuilder::Build(desc, primProc, pipeline, *this->glCaps().glslCaps())) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002082 SkDEBUGFAIL("Failed to generate GL program descriptor");
2083 }
2084}
2085
joshualitt93316b92015-10-23 09:08:08 -07002086void GrGLGpu::bindBuffer(GrGLuint id, GrGLenum type) {
2087 this->handleDirtyContext();
2088 if (GR_GL_ARRAY_BUFFER == type) {
2089 this->bindVertexBuffer(id);
jvanverth73063dc2015-12-03 09:15:47 -08002090 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07002091 this->bindIndexBufferAndDefaultVertexArray(id);
jvanverthd7a2c1f2015-12-07 07:36:44 -08002092 } else {
2093 GR_GL_CALL(this->glInterface(), BindBuffer(type, id));
joshualitt93316b92015-10-23 09:08:08 -07002094 }
2095}
2096
2097void GrGLGpu::releaseBuffer(GrGLuint id, GrGLenum type) {
2098 this->handleDirtyContext();
2099 GL_CALL(DeleteBuffers(1, &id));
2100 if (GR_GL_ARRAY_BUFFER == type) {
2101 this->notifyVertexBufferDelete(id);
jvanverth73063dc2015-12-03 09:15:47 -08002102 } else if (GR_GL_ELEMENT_ARRAY_BUFFER == type) {
joshualitt93316b92015-10-23 09:08:08 -07002103 this->notifyIndexBufferDelete(id);
2104 }
2105}
2106
jvanverth73063dc2015-12-03 09:15:47 -08002107static GrGLenum get_gl_usage(GrGLBufferImpl::Usage usage) {
2108 static const GrGLenum grToGL[] = {
2109 GR_GL_STATIC_DRAW, // GrGLBufferImpl::kStaticDraw_Usage
2110 DYNAMIC_USAGE_PARAM, // GrGLBufferImpl::kDynamicDraw_Usage
2111 GR_GL_STREAM_DRAW, // GrGLBufferImpl::kStreamDraw_Usage
2112 GR_GL_STREAM_READ, // GrGLBufferImpl::kStreamRead_Usage
2113 };
2114 static_assert(SK_ARRAY_COUNT(grToGL) == GrGLBufferImpl::kUsageCount, "array_size_mismatch");
joshualitt93316b92015-10-23 09:08:08 -07002115
jvanverth73063dc2015-12-03 09:15:47 -08002116 return grToGL[usage];
2117}
2118
2119void* GrGLGpu::mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
2120 size_t currentSize, size_t requestedSize) {
joshualitt93316b92015-10-23 09:08:08 -07002121 void* mapPtr = nullptr;
jvanverth73063dc2015-12-03 09:15:47 -08002122 GrGLenum glUsage = get_gl_usage(usage);
jvanverthd7a2c1f2015-12-07 07:36:44 -08002123 bool readOnly = (GrGLBufferImpl::kStreamRead_Usage == usage);
2124
joshualitt93316b92015-10-23 09:08:08 -07002125 // Handling dirty context is done in the bindBuffer call
2126 switch (this->glCaps().mapBufferType()) {
2127 case GrGLCaps::kNone_MapBufferType:
2128 break;
2129 case GrGLCaps::kMapBuffer_MapBufferType:
2130 this->bindBuffer(id, type);
2131 // Let driver know it can discard the old data
joshualitt6df232d2015-10-23 13:54:12 -07002132 if (GR_GL_USE_BUFFER_DATA_NULL_HINT || currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08002133 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07002134 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08002135 GL_CALL_RET(mapPtr, MapBuffer(type, readOnly ? GR_GL_READ_ONLY : GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07002136 break;
2137 case GrGLCaps::kMapBufferRange_MapBufferType: {
2138 this->bindBuffer(id, type);
2139 // Make sure the GL buffer size agrees with fDesc before mapping.
2140 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08002141 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07002142 }
jvanverth17aa0472016-01-05 10:41:27 -08002143 GrGLbitfield writeAccess = GR_GL_MAP_WRITE_BIT;
2144 // TODO: allow the client to specify invalidation in the stream draw case
2145 if (GrGLBufferImpl::kStreamDraw_Usage != usage) {
2146 writeAccess |= GR_GL_MAP_INVALIDATE_BUFFER_BIT;
2147 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08002148 GL_CALL_RET(mapPtr, MapBufferRange(type, 0, requestedSize, readOnly ?
2149 GR_GL_MAP_READ_BIT :
jvanverth17aa0472016-01-05 10:41:27 -08002150 writeAccess));
joshualitt93316b92015-10-23 09:08:08 -07002151 break;
2152 }
2153 case GrGLCaps::kChromium_MapBufferType:
2154 this->bindBuffer(id, type);
2155 // Make sure the GL buffer size agrees with fDesc before mapping.
2156 if (currentSize != requestedSize) {
jvanverth73063dc2015-12-03 09:15:47 -08002157 GL_CALL(BufferData(type, requestedSize, nullptr, glUsage));
joshualitt93316b92015-10-23 09:08:08 -07002158 }
jvanverthd7a2c1f2015-12-07 07:36:44 -08002159 GL_CALL_RET(mapPtr, MapBufferSubData(type, 0, requestedSize, readOnly ?
2160 GR_GL_READ_ONLY :
2161 GR_GL_WRITE_ONLY));
joshualitt93316b92015-10-23 09:08:08 -07002162 break;
2163 }
2164 return mapPtr;
2165}
2166
jvanverth73063dc2015-12-03 09:15:47 -08002167void GrGLGpu::bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage,
2168 size_t currentSize, const void* src, size_t srcSizeInBytes) {
joshualitt93316b92015-10-23 09:08:08 -07002169 SkASSERT(srcSizeInBytes <= currentSize);
2170 // bindbuffer handles dirty context
2171 this->bindBuffer(id, type);
jvanverth73063dc2015-12-03 09:15:47 -08002172 GrGLenum glUsage = get_gl_usage(usage);
joshualitt93316b92015-10-23 09:08:08 -07002173
joshualitt6df232d2015-10-23 13:54:12 -07002174#if GR_GL_USE_BUFFER_DATA_NULL_HINT
2175 if (currentSize == srcSizeInBytes) {
jvanverth73063dc2015-12-03 09:15:47 -08002176 GL_CALL(BufferData(type, (GrGLsizeiptr) srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07002177 } else {
2178 // Before we call glBufferSubData we give the driver a hint using
2179 // glBufferData with nullptr. This makes the old buffer contents
2180 // inaccessible to future draws. The GPU may still be processing
2181 // draws that reference the old contents. With this hint it can
2182 // assign a different allocation for the new contents to avoid
2183 // flushing the gpu past draws consuming the old contents.
2184 // TODO I think we actually want to try calling bufferData here
jvanverth73063dc2015-12-03 09:15:47 -08002185 GL_CALL(BufferData(type, currentSize, nullptr, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07002186 GL_CALL(BufferSubData(type, 0, (GrGLsizeiptr) srcSizeInBytes, src));
2187 }
2188#else
joshualitt93316b92015-10-23 09:08:08 -07002189 // Note that we're cheating on the size here. Currently no methods
2190 // allow a partial update that preserves contents of non-updated
2191 // portions of the buffer (map() does a glBufferData(..size, nullptr..))
jvanverth73063dc2015-12-03 09:15:47 -08002192 GL_CALL(BufferData(type, srcSizeInBytes, src, glUsage));
joshualitt6df232d2015-10-23 13:54:12 -07002193#endif
joshualitt93316b92015-10-23 09:08:08 -07002194}
2195
2196void GrGLGpu::unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr) {
2197 // bind buffer handles the dirty context
2198 switch (this->glCaps().mapBufferType()) {
2199 case GrGLCaps::kNone_MapBufferType:
2200 SkDEBUGFAIL("Shouldn't get here.");
2201 return;
2202 case GrGLCaps::kMapBuffer_MapBufferType: // fall through
2203 case GrGLCaps::kMapBufferRange_MapBufferType:
2204 this->bindBuffer(id, type);
2205 GL_CALL(UnmapBuffer(type));
2206 break;
2207 case GrGLCaps::kChromium_MapBufferType:
2208 this->bindBuffer(id, type);
2209 GL_CALL(UnmapBufferSubData(mapPtr));
2210 break;
2211 }
2212}
2213
bsalomon861e1032014-12-16 07:33:49 -08002214void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002215 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002216 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002217 fHWScissorSettings.fEnabled = kNo_TriState;
2218 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002219 }
2220}
2221
egdaniel51c8d402015-08-06 10:54:13 -07002222void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002223 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002224 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002225 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002226
egdaniel51c8d402015-08-06 10:54:13 -07002227 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08002228 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07002229 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002230 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00002231
2232 GrGLfloat r, g, b, a;
2233 static const GrGLfloat scale255 = 1.f / 255.f;
2234 a = GrColorUnpackA(color) * scale255;
2235 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002236 r = GrColorUnpackR(color) * scaleRGB;
2237 g = GrColorUnpackG(color) * scaleRGB;
2238 b = GrColorUnpackB(color) * scaleRGB;
2239
2240 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002241 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002242 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002243 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002244}
2245
bsalomon861e1032014-12-16 07:33:49 -08002246void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08002247 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002248 if (!this->caps()->discardRenderTargetSupport()) {
2249 return;
2250 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002251
2252 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002253 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
2254 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
2255 fStats.incRenderTargetBinds();
2256 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
2257 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002258 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07002259 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002260 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002261 break;
2262 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07002263 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002264 // When rendering to the default framebuffer the legal values for attachments
2265 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2266 // types.
2267 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002268 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002269 attachments));
2270 } else {
2271 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002272 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002273 attachments));
2274 }
2275 break;
2276 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07002277 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002278 // When rendering to the default framebuffer the legal values for attachments
2279 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2280 // types. See glDiscardFramebuffer() spec.
2281 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002282 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002283 attachments));
2284 } else {
2285 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002286 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002287 attachments));
2288 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002289 break;
2290 }
2291 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002292 renderTarget->flagAsResolved();
2293}
2294
bsalomon861e1032014-12-16 07:33:49 -08002295void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002296 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002297 return;
2298 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002299 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002300 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002301
joshualitt77b13072014-10-27 14:51:01 -07002302 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002303
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002304 GL_CALL(StencilMask(0xffffffff));
2305 GL_CALL(ClearStencil(0));
2306 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002307 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002308}
2309
bsalomon861e1032014-12-16 07:33:49 -08002310void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07002311 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002312
egdaniel8dc7c3a2015-04-16 11:22:42 -07002313 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002314 // this should only be called internally when we know we have a
2315 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002316 SkASSERT(sb);
2317 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002318#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002319 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002320 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002321#else
2322 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002323 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002324 // turned into draws. Our contract on GrDrawTarget says that
2325 // changing the clip between stencil passes may or may not
2326 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002327 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002328#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002329 GrGLint value;
2330 if (insideClip) {
2331 value = (1 << (stencilBitCount - 1));
2332 } else {
2333 value = 0;
2334 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002335 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002336 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002337
bsalomon3e791242014-12-17 13:43:13 -08002338 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002339 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002340 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002341
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002342 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002343 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002344 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002345 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002346}
2347
bsalomon39826022015-07-23 08:07:21 -07002348static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
2349 int width, int height, GrPixelConfig config,
2350 size_t rowBytes) {
2351 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002352 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
2353 return false;
2354 }
2355
bsalomon494aa592015-07-23 11:45:02 -07002356 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002357 static const int kMinSize = 32;
2358 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002359 return false;
2360 }
2361
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002362 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002363 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002364 return false;
2365 }
2366
2367 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002368 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002369 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2370 // clipped.
2371 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2372}
2373
bsalomon1aa20292016-01-22 08:16:09 -08002374bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2375 auto bindRenderTarget = [this, target]() -> bool {
2376 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2377 return true;
2378 };
2379 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2380 GR_GL_GetIntegerv(this->glInterface(), query, value);
2381 };
2382 GrPixelConfig rtConfig = target->config();
2383 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2384}
2385
2386bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
2387 auto bindRenderTarget = [this, rtConfig]() -> bool {
2388 GrTextureDesc desc;
2389 desc.fConfig = rtConfig;
2390 desc.fWidth = desc.fHeight = 16;
2391 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon5ec26ae2016-02-25 08:33:02 -08002392 SkAutoTUnref<GrTexture> temp(this->createTexture(desc, SkBudgeted::kNo, nullptr, 0));
bsalomon1aa20292016-01-22 08:16:09 -08002393 if (!temp) {
2394 return false;
2395 }
2396 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2397 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2398 return true;
2399 };
2400 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2401 GR_GL_GetIntegerv(this->glInterface(), query, value);
2402 };
2403 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2404}
2405
2406bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2407 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2408 return this->readPixelsSupported(rt, readConfig);
2409 } else {
2410 GrPixelConfig config = surfaceForConfig->config();
2411 return this->readPixelsSupported(config, readConfig);
2412 }
2413}
2414
bsalomone9573312016-01-25 14:33:25 -08002415static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2416 if (GrPixelConfigIsSRGB(a)) {
2417 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2418 } else if (GrPixelConfigIsSRGB(b)) {
2419 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2420 }
2421 return false;
2422}
2423
bsalomonf0674512015-07-28 13:26:15 -07002424bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2425 GrPixelConfig readConfig, DrawPreference* drawPreference,
2426 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002427 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002428
bsalomone9573312016-01-25 14:33:25 -08002429 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002430 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2431 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2432 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2433 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2434 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07002435 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07002436
bsalomone9573312016-01-25 14:33:25 -08002437 // For now assume no swizzling, we may change that below.
2438 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2439
2440 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2441 // from will be srcConfig and we will read readConfig pixels from it.
2442 // Not that if we require a draw and return a non-renderable format for the temp surface the
2443 // base class will fail for us.
2444 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2445 tempDrawInfo->fReadConfig = readConfig;
2446
2447 if (requires_srgb_conversion(srcConfig, readConfig)) {
2448 if (!this->readPixelsSupported(readConfig, readConfig)) {
2449 return false;
2450 }
2451 // Draw to do srgb to linear conversion or vice versa.
2452 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2453 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2454 tempDrawInfo->fReadConfig = readConfig;
2455 return true;
2456 }
2457
2458 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2459 if (!srcAsRT) {
2460 // For now keep assuming the draw is not a format transformation, just a draw to get to a
2461 // RT. We may add additional transformations below.
2462 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2463 }
bsalomon1aa20292016-01-22 08:16:09 -08002464 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2465 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002466 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002467 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2468 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002469 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002470 } else if (kMesa_GrGLDriver == this->glContext().driver() &&
2471 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002472 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2473 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002474 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002475 // Better to do a draw with a R/B swap and then read as the original config.
2476 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002477 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2478 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002479 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002480 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2481 if (readConfig == kBGRA_8888_GrPixelConfig &&
2482 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2483 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002484 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2485 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2486 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002487 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2488 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2489 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2490 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002491 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2492 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2493 // it's unsupported, but 32bit RGBA reads are supported.
2494 // Don't attempt to do any srgb conversions since we only care about alpha.
2495 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2496 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2497 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2498 }
2499 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2500 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2501 // first and then onReadPixels will read that to a 32bit temporary buffer.
2502 if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
2503 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2504 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2505 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2506 } else {
2507 return false;
2508 }
2509 } else {
2510 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2511 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2512 }
bsalomon1aa20292016-01-22 08:16:09 -08002513 } else {
2514 return false;
2515 }
bsalomon39826022015-07-23 08:07:21 -07002516 }
2517
bsalomon1aa20292016-01-22 08:16:09 -08002518 if (srcAsRT &&
2519 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002520 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002521 }
2522
bsalomon39826022015-07-23 08:07:21 -07002523 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002524}
2525
bsalomon6cb3cbe2015-07-30 07:34:27 -07002526bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002527 int left, int top,
2528 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002529 GrPixelConfig config,
2530 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002531 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002532 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002533
bsalomone9573312016-01-25 14:33:25 -08002534 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2535 if (!renderTarget) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002536 return false;
2537 }
2538
bsalomon16921ec2015-07-30 15:34:56 -07002539 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002540 if (requires_srgb_conversion(surface->config(), config)) {
2541 return false;
2542 }
2543
2544 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2545 // bit channels as RGBA and then extract A.
2546 if (!this->readPixelsSupported(renderTarget, config)) {
2547 // Don't attempt to do any srgb conversions since we only care about alpha.
2548 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
2549 if (GrPixelConfigIsSRGB(renderTarget->config())) {
2550 tempConfig = kSRGBA_8888_GrPixelConfig;
2551 }
2552 if (kAlpha_8_GrPixelConfig == config &&
2553 this->readPixelsSupported(renderTarget, tempConfig)) {
2554 SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
2555 if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, temp.get(),
2556 width*4)) {
2557 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2558 for (int j = 0; j < height; ++j) {
2559 for (int i = 0; i < width; ++i) {
2560 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2561 }
2562 }
2563 return true;
2564 }
2565 }
bsalomon16921ec2015-07-30 15:34:56 -07002566 return false;
2567 }
2568
bsalomon76148af2016-01-12 11:13:47 -08002569 GrGLenum externalFormat;
2570 GrGLenum externalType;
bsalomone9573312016-01-25 14:33:25 -08002571 if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002572 &externalType)) {
2573 return false;
2574 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002575 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002576
bsalomon@google.comc6980972011-11-02 19:57:21 +00002577 // resolve the render target if necessary
bsalomone9573312016-01-25 14:33:25 -08002578 switch (renderTarget->getResolveType()) {
egdanield803f272015-03-18 13:01:52 -07002579 case GrGLRenderTarget::kCantResolve_ResolveType:
2580 return false;
2581 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002582 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002583 break;
2584 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002585 this->onResolveRenderTarget(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002586 // we don't track the state of the READ FBO ID.
2587 fStats.incRenderTargetBinds();
bsalomone9573312016-01-25 14:33:25 -08002588 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002589 break;
2590 default:
2591 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002592 }
2593
bsalomone9573312016-01-25 14:33:25 -08002594 const GrGLIRect& glvp = renderTarget->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002595
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002596 // the read rect is viewport-relative
2597 GrGLIRect readRect;
bsalomone9573312016-01-25 14:33:25 -08002598 readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002599
bsalomon9d02b262016-02-01 12:49:30 -08002600 size_t bytesPerPixel = GrBytesPerPixel(config);
2601 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002602
bsalomon@google.comc6980972011-11-02 19:57:21 +00002603 size_t readDstRowBytes = tightRowBytes;
2604 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002605
bsalomon@google.comc6980972011-11-02 19:57:21 +00002606 // determine if GL can read using the passed rowBytes or if we need
2607 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002608 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002609 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002610 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002611 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002612 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002613 readDstRowBytes = rowBytes;
2614 } else {
2615 scratch.reset(tightRowBytes * height);
2616 readDst = scratch.get();
2617 }
2618 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002619 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002620 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2621 }
bsalomonf46a1242015-12-15 12:37:38 -08002622 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2623
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002624 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2625 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002626 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002627 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002628 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002629 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2630 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002631 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002632 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002633 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002634 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002635
2636 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002637 // API presents top-to-bottom. We must preserve the padding contents. Note
2638 // that the above readPixels did not overwrite the padding.
2639 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002640 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002641 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002642 scratch.reset(tightRowBytes);
2643 void* tmpRow = scratch.get();
2644 // flip y in-place by rows
2645 const int halfY = height >> 1;
2646 char* top = reinterpret_cast<char*>(buffer);
2647 char* bottom = top + (height - 1) * rowBytes;
2648 for (int y = 0; y < halfY; y++) {
2649 memcpy(tmpRow, top, tightRowBytes);
2650 memcpy(top, bottom, tightRowBytes);
2651 memcpy(bottom, tmpRow, tightRowBytes);
2652 top += rowBytes;
2653 bottom -= rowBytes;
2654 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002655 }
2656 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002657 SkASSERT(readDst != buffer);
2658 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002659 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002660 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002661 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002662 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002663 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002664 dst += (height-1) * rowBytes;
2665 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002666 for (int y = 0; y < height; y++) {
2667 memcpy(dst, src, tightRowBytes);
2668 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002669 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002670 dst += rowBytes;
2671 } else {
2672 dst -= rowBytes;
2673 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002674 }
2675 }
2676 return true;
2677}
2678
jvanverthd2d2eb92016-02-17 14:04:46 -08002679void GrGLGpu::finishDrawTarget() {
ethannicholas22793252016-01-30 09:59:10 -08002680 if (fPLSHasBeenUsed) {
2681 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2682 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
2683 * trivial way every frame. And since we use it every frame, there's never a point at which
2684 * it becomes safe to stop using this workaround once we start.
2685 */
2686 this->disableScissor();
2687 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002688 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002689 SkASSERT(!fHWPLSEnabled);
2690 SkASSERT(fMSAAEnabled != kYes_TriState);
2691 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2692 this->stampRectUsingProgram(fPLSSetupProgram.fProgram,
2693 SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f),
2694 fPLSSetupProgram.fPosXformUniform,
2695 fPLSSetupProgram.fArrayBuffer);
2696 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2697 }
2698}
ethannicholas5366a092016-01-22 09:45:47 -08002699
bsalomon083617b2016-02-12 12:10:14 -08002700void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds) {
egdanield803f272015-03-18 13:01:52 -07002701 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002702
egdanield803f272015-03-18 13:01:52 -07002703 uint32_t rtID = target->getUniqueID();
2704 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002705 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002706 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2707#ifdef SK_DEBUG
2708 // don't do this check in Chromium -- this is causing
2709 // lots of repeated command buffer flushes when the compositor is
2710 // rendering with Ganesh, which is really slow; even too slow for
2711 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002712 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002713 GrGLenum status;
2714 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2715 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2716 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2717 }
bsalomon160f24c2015-03-17 15:55:42 -07002718 }
egdanield803f272015-03-18 13:01:52 -07002719#endif
2720 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002721 this->flushViewport(target->getViewport());
bsalomon16921ec2015-07-30 15:34:56 -07002722 if (this->glCaps().srgbWriteControl()) {
2723 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config());
2724 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2725 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2726 fHWSRGBFramebuffer = kYes_TriState;
2727 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2728 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2729 fHWSRGBFramebuffer = kNo_TriState;
2730 }
2731 }
bsalomon5cd020f2015-03-17 12:46:56 -07002732 }
bsalomon083617b2016-02-12 12:10:14 -08002733 this->didWriteToSurface(target, bounds);
2734}
bsalomona9909122016-01-23 10:41:40 -08002735
bsalomon083617b2016-02-12 12:10:14 -08002736void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2737 if (fHWViewport != viewport) {
2738 viewport.pushToGLViewport(this->glInterface());
2739 fHWViewport = viewport;
2740 }
2741}
2742
2743void GrGLGpu::didWriteToSurface(GrSurface* surface, const SkIRect* bounds) const {
2744 SkASSERT(surface);
bsalomona9909122016-01-23 10:41:40 -08002745 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
bsalomon083617b2016-02-12 12:10:14 -08002746 if (nullptr == bounds || !bounds->isEmpty()) {
2747 if (GrRenderTarget* target = surface->asRenderTarget()) {
2748 target->flagAsNeedingResolve(bounds);
2749 }
2750 if (GrTexture* texture = surface->asTexture()) {
bsalomona9909122016-01-23 10:41:40 -08002751 texture->texturePriv().dirtyMipMaps(true);
2752 }
egdanield803f272015-03-18 13:01:52 -07002753 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002754}
2755
twiz@google.com0f31ca72011-03-18 17:38:11 +00002756GrGLenum gPrimitiveType2GLMode[] = {
2757 GR_GL_TRIANGLES,
2758 GR_GL_TRIANGLE_STRIP,
2759 GR_GL_TRIANGLE_FAN,
2760 GR_GL_POINTS,
2761 GR_GL_LINES,
2762 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002763};
2764
bsalomon@google.comd302f142011-03-03 13:54:13 +00002765#define SWAP_PER_DRAW 0
2766
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002767#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002768 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002769 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002770 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002771 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002772 void SwapBuf() {
2773 DWORD procID = GetCurrentProcessId();
2774 HWND hwnd = GetTopWindow(GetDesktopWindow());
2775 while(hwnd) {
2776 DWORD wndProcID = 0;
2777 GetWindowThreadProcessId(hwnd, &wndProcID);
2778 if(wndProcID == procID) {
2779 SwapBuffers(GetDC(hwnd));
2780 }
2781 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2782 }
2783 }
2784 #endif
2785#endif
2786
bsalomone64eb572015-05-07 11:35:55 -07002787void GrGLGpu::onDraw(const DrawArgs& args, const GrNonInstancedVertices& vertices) {
cdaltond0a840d2015-03-16 17:19:58 -07002788 if (!this->flushGLState(args)) {
bsalomond95263c2014-12-16 13:05:12 -08002789 return;
2790 }
2791
ethannicholas22793252016-01-30 09:59:10 -08002792 GrPixelLocalStorageState plsState = args.fPrimitiveProcessor->getPixelLocalStorageState();
2793 if (!fHWPLSEnabled && plsState !=
2794 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2795 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2796 this->setupPixelLocalStorage(args);
2797 fHWPLSEnabled = true;
2798 }
2799 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2800 GrStencilSettings stencil;
2801 stencil.setDisabled();
2802 this->flushStencil(stencil);
2803 }
2804
joshualitt873ad0e2015-01-20 09:08:51 -08002805 size_t indexOffsetInBytes = 0;
bsalomoncb8979d2015-05-05 09:51:38 -07002806 this->setupGeometry(*args.fPrimitiveProcessor, vertices, &indexOffsetInBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +00002807
bsalomoncb8979d2015-05-05 09:51:38 -07002808 SkASSERT((size_t)vertices.primitiveType() < SK_ARRAY_COUNT(gPrimitiveType2GLMode));
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002809
bsalomoncb8979d2015-05-05 09:51:38 -07002810 if (vertices.isIndexed()) {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00002811 GrGLvoid* indices =
bsalomoncb8979d2015-05-05 09:51:38 -07002812 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2813 vertices.startIndex());
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002814 // info.startVertex() was accounted for by setupGeometry.
bsalomoncb8979d2015-05-05 09:51:38 -07002815 GL_CALL(DrawElements(gPrimitiveType2GLMode[vertices.primitiveType()],
2816 vertices.indexCount(),
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002817 GR_GL_UNSIGNED_SHORT,
2818 indices));
2819 } else {
2820 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account for
2821 // startVertex in the DrawElements case. So we always rely on setupGeometry to have
2822 // accounted for startVertex.
bsalomoncb8979d2015-05-05 09:51:38 -07002823 GL_CALL(DrawArrays(gPrimitiveType2GLMode[vertices.primitiveType()], 0,
2824 vertices.vertexCount()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002825 }
ethannicholas22793252016-01-30 09:59:10 -08002826
2827 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2828 // PLS draws always involve multiple draws, finishing up with a non-PLS
2829 // draw that writes to the color buffer. That draw ends up here; we wait
2830 // until after it is complete to actually disable PLS.
2831 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2832 fHWPLSEnabled = false;
2833 this->disableScissor();
2834 }
2835
bsalomon@google.comd302f142011-03-03 13:54:13 +00002836#if SWAP_PER_DRAW
2837 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002838 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002839 aglSwapBuffers(aglGetCurrentContext());
2840 int set_a_break_pt_here = 9;
2841 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002842 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002843 SwapBuf();
2844 int set_a_break_pt_here = 9;
2845 SwapBuf();
2846 #endif
2847#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002848}
2849
ethannicholas22793252016-01-30 09:59:10 -08002850void GrGLGpu::stampRectUsingProgram(GrGLuint program, const SkRect& bounds, GrGLint posXformUniform,
2851 GrGLuint arrayBuffer) {
2852 GL_CALL(UseProgram(program));
2853 this->fHWGeometryState.setVertexArrayID(this, 0);
2854
2855 GrGLAttribArrayState* attribs =
2856 this->fHWGeometryState.bindArrayAndBufferToDraw(this, arrayBuffer);
cdalton793dc262016-02-08 10:11:47 -08002857 attribs->set(this, 0, arrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat), 0);
ethannicholas22793252016-01-30 09:59:10 -08002858 attribs->disableUnusedArrays(this, 0x1);
2859
2860 GL_CALL(Uniform4f(posXformUniform, bounds.width(), bounds.height(), bounds.left(),
2861 bounds.top()));
2862
2863 GrXferProcessor::BlendInfo blendInfo;
2864 blendInfo.reset();
2865 this->flushBlend(blendInfo, GrSwizzle());
2866 this->flushColorWrite(true);
2867 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
2868 if (!fHWStencilSettings.isDisabled()) {
2869 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2870 }
2871 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2872 GL_CALL(UseProgram(fHWProgramID));
2873 if (!fHWStencilSettings.isDisabled()) {
2874 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2875 }
2876}
2877
2878void GrGLGpu::setupPixelLocalStorage(const DrawArgs& args) {
2879 fPLSHasBeenUsed = true;
2880 const SkRect& bounds =
2881 static_cast<const GrPLSGeometryProcessor*>(args.fPrimitiveProcessor)->getBounds();
2882 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2883 // and initializing the winding counts to zero
2884 GrRenderTarget* rt = args.fPipeline->getRenderTarget();
2885 SkScalar width = SkIntToScalar(rt->width());
2886 SkScalar height = SkIntToScalar(rt->height());
2887 // dst rect edges in NDC (-1 to 1)
2888 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2889 // it contained properly
2890 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2891 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2892 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2893 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2894 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
2895
2896 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
2897 this->stampRectUsingProgram(fPLSSetupProgram.fProgram, deviceBounds,
2898 fPLSSetupProgram.fPosXformUniform, fPLSSetupProgram.fArrayBuffer);
2899}
2900
bsalomon861e1032014-12-16 07:33:49 -08002901void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002902 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002903 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002904 // Some extensions automatically resolves the texture when it is read.
2905 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002906 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2907 fStats.incRenderTargetBinds();
2908 fStats.incRenderTargetBinds();
2909 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2910 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2911 // make sure we go through flushRenderTarget() since we've modified
2912 // the bound DRAW FBO ID.
2913 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002914 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002915 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002916
bsalomon@google.com347c3822013-05-01 20:10:01 +00002917 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002918 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002919 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002920 scissorState.set(dirtyRect);
2921 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002922 GL_CALL(ResolveMultisampleFramebuffer());
2923 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002924 GrGLIRect r;
2925 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2926 dirtyRect.width(), dirtyRect.height(), target->origin());
2927
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002928 int right = r.fLeft + r.fWidth;
2929 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002930
2931 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002932 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002933 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2934 r.fLeft, r.fBottom, right, top,
2935 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002936 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002937 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002938 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002939 }
2940}
2941
bsalomon@google.com411dad02012-06-05 20:24:20 +00002942namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002943
bsalomon@google.com411dad02012-06-05 20:24:20 +00002944
2945GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2946 static const GrGLenum gTable[] = {
2947 GR_GL_KEEP, // kKeep_StencilOp
2948 GR_GL_REPLACE, // kReplace_StencilOp
2949 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2950 GR_GL_INCR, // kIncClamp_StencilOp
2951 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2952 GR_GL_DECR, // kDecClamp_StencilOp
2953 GR_GL_ZERO, // kZero_StencilOp
2954 GR_GL_INVERT, // kInvert_StencilOp
2955 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00002956 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002957 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2958 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2959 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2960 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2961 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2962 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2963 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2964 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002965 SkASSERT((unsigned) op < kStencilOpCount);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002966 return gTable[op];
2967}
2968
2969void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002970 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002971 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002972 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002973 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002974 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2975 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2976
2977 GrGLint ref = settings.funcRef(grFace);
2978 GrGLint mask = settings.funcMask(grFace);
2979 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002980
2981 if (GR_GL_FRONT_AND_BACK == glFace) {
2982 // we call the combined func just in case separate stencil is not
2983 // supported.
2984 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2985 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002986 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002987 } else {
2988 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2989 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002990 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002991 }
2992}
2993}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002994
bsalomon3e791242014-12-17 13:43:13 -08002995void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2996 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002997 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002998 if (kNo_TriState != fHWStencilTestEnabled) {
2999 GL_CALL(Disable(GR_GL_STENCIL_TEST));
3000 fHWStencilTestEnabled = kNo_TriState;
3001 }
3002 } else {
3003 if (kYes_TriState != fHWStencilTestEnabled) {
3004 GL_CALL(Enable(GR_GL_STENCIL_TEST));
3005 fHWStencilTestEnabled = kYes_TriState;
3006 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00003007 }
joshualitta58fe352014-10-27 08:39:00 -07003008 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00003009 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00003010 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07003011 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00003012 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00003013 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00003014 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07003015 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00003016 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00003017 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00003018 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00003019 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07003020 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00003021 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00003022 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00003023 }
3024 }
joshualitta58fe352014-10-27 08:39:00 -07003025 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00003026 }
3027}
3028
cdaltonaf8bc7d2016-02-05 09:35:20 -08003029void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08003030 // rt is only optional if useHWAA is false.
3031 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07003032 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00003033
cdaltond4727922015-11-10 12:49:06 -08003034 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07003035 if (useHWAA) {
3036 if (kYes_TriState != fMSAAEnabled) {
3037 GL_CALL(Enable(GR_GL_MULTISAMPLE));
3038 fMSAAEnabled = kYes_TriState;
3039 }
3040 } else {
3041 if (kNo_TriState != fMSAAEnabled) {
3042 GL_CALL(Disable(GR_GL_MULTISAMPLE));
3043 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003044 }
3045 }
3046 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08003047
3048 if (0 != this->caps()->maxRasterSamples()) {
3049 if (useHWAA && rt->hasMixedSamples() && !stencilEnabled) {
3050 // Since stencil is disabled and we want more samples than are in the color buffer, we
3051 // need to tell the rasterizer explicitly how many to run.
3052 if (kYes_TriState != fHWRasterMultisampleEnabled) {
3053 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
3054 fHWRasterMultisampleEnabled = kYes_TriState;
3055 }
3056 if (rt->numStencilSamples() != fHWNumRasterSamples) {
3057 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
3058 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
3059 fHWNumRasterSamples = rt->numStencilSamples();
3060 }
3061 } else {
3062 if (kNo_TriState != fHWRasterMultisampleEnabled) {
3063 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
3064 fHWRasterMultisampleEnabled = kNo_TriState;
3065 }
3066 }
3067 } else {
3068 SkASSERT(!useHWAA || !rt->hasMixedSamples() || stencilEnabled);
3069 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003070}
3071
bsalomon7f9b2e42016-01-12 13:29:26 -08003072void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07003073 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07003074 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07003075
cdalton8917d622015-05-06 13:40:21 -07003076 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08003077 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
3078 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07003079 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
3080 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07003081 if (blendOff) {
3082 if (kNo_TriState != fHWBlendState.fEnabled) {
3083 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07003084
3085 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
3086 // https://code.google.com/p/skia/issues/detail?id=3943
3087 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
3088 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
3089 SkASSERT(this->caps()->advancedBlendEquationSupport());
3090 // Set to any basic blending equation.
3091 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
3092 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
3093 fHWBlendState.fEquation = blend_equation;
3094 }
3095
egdanielb414f252014-07-29 13:15:47 -07003096 fHWBlendState.fEnabled = kNo_TriState;
3097 }
cdalton8917d622015-05-06 13:40:21 -07003098 return;
3099 }
3100
3101 if (kYes_TriState != fHWBlendState.fEnabled) {
3102 GL_CALL(Enable(GR_GL_BLEND));
3103 fHWBlendState.fEnabled = kYes_TriState;
3104 }
3105
3106 if (fHWBlendState.fEquation != equation) {
3107 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
3108 fHWBlendState.fEquation = equation;
3109 }
3110
3111 if (GrBlendEquationIsAdvanced(equation)) {
3112 SkASSERT(this->caps()->advancedBlendEquationSupport());
3113 // Advanced equations have no other blend state.
3114 return;
3115 }
3116
bsalomone63ffef2016-02-05 07:17:34 -08003117 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07003118 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
3119 gXfermodeCoeff2Blend[dstCoeff]));
3120 fHWBlendState.fSrcCoeff = srcCoeff;
3121 fHWBlendState.fDstCoeff = dstCoeff;
3122 }
3123
bsalomon7f9b2e42016-01-12 13:29:26 -08003124 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
3125 GrColor blendConst = blendInfo.fBlendConstant;
3126 blendConst = swizzle.applyTo(blendConst);
3127 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
3128 GrGLfloat c[4];
3129 GrColorToRGBAFloat(blendConst, c);
3130 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
3131 fHWBlendState.fConstColor = blendConst;
3132 fHWBlendState.fConstColorValid = true;
3133 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00003134 }
3135}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00003136
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003137static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00003138 static const GrGLenum gWrapModes[] = {
3139 GR_GL_CLAMP_TO_EDGE,
3140 GR_GL_REPEAT,
3141 GR_GL_MIRRORED_REPEAT
3142 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00003143 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00003144 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
3145 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
3146 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
3147 return gWrapModes[tm];
3148}
3149
egdanielb7e7d572015-11-04 04:23:53 -08003150static GrGLenum get_component_enum_from_char(char component) {
3151 switch (component) {
3152 case 'r':
3153 return GR_GL_RED;
3154 case 'g':
3155 return GR_GL_GREEN;
3156 case 'b':
3157 return GR_GL_BLUE;
3158 case 'a':
3159 return GR_GL_ALPHA;
3160 default:
3161 SkFAIL("Unsupported component");
3162 return 0;
3163 }
3164}
3165
3166/** If texture swizzling is available using tex parameters then it is preferred over mangling
3167 the generated shader code. This potentially allows greater reuse of cached shaders. */
3168static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08003169 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08003170 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08003171 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003172 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003173 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003174 }
3175}
3176
bsalomon861e1032014-12-16 07:33:49 -08003177void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07003178 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003179
reed856e9d92015-09-30 12:21:45 -07003180#ifdef SK_DEBUG
3181 if (!this->caps()->npotTextureTileSupport()) {
3182 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
3183 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
3184 if (tileX || tileY) {
3185 const int w = texture->width();
3186 const int h = texture->height();
3187 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3188 }
3189 }
3190#endif
3191
bsalomon@google.comb8670992012-07-25 21:27:09 +00003192 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3193 // 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 +00003194 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003195 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003196 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003197 this->onResolveRenderTarget(texRT);
3198 }
3199
bsalomon1c63bf62014-07-22 13:09:46 -07003200 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003201 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003202 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003203 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003204 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003205 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003206 }
3207
bsalomon@google.com4c883782012-06-04 19:05:11 +00003208 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003209 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003210 bool setAll = timestamp < this->getResetTimestamp();
3211 GrGLTexture::TexParams newTexParams;
3212
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003213 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003214 GR_GL_NEAREST,
3215 GR_GL_LINEAR,
3216 GR_GL_LINEAR_MIPMAP_LINEAR
3217 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003218 static GrGLenum glMagFilterModes[] = {
3219 GR_GL_NEAREST,
3220 GR_GL_LINEAR,
3221 GR_GL_LINEAR
3222 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003223 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003224
3225 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3226 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3227 filterMode = GrTextureParams::kBilerp_FilterMode;
3228 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003229 }
bsalomonefd7d452014-10-23 14:17:46 -07003230
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003231 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3232 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003233
cblume55f2d2d2016-02-26 13:20:48 -08003234 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3235 if (texture->texturePriv().mipMapsAreDirty()) {
3236 GL_CALL(GenerateMipmap(target));
3237 texture->texturePriv().dirtyMipMaps(false);
3238 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3239 texture->width(), texture->height()));
3240 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003241 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003242
cblume55f2d2d2016-02-26 13:20:48 -08003243 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3244
bsalomon@google.comb8670992012-07-25 21:27:09 +00003245 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3246 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003247 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003248 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003249 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003250 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003251 }
3252 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3253 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003254 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003255 }
cblume55f2d2d2016-02-26 13:20:48 -08003256 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
3257 if (newTexParams.fMaxMipMapLevel != 0) {
3258 this->setTextureUnit(unitIdx);
3259 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3260 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3261 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3262 newTexParams.fMaxMipMapLevel));
3263 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3264 newTexParams.fMaxMipMapLevel));
3265 }
3266 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003267 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003268 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003269 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003270 }
3271 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003272 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003273 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003274 }
bsalomoncdee0092016-01-08 13:20:12 -08003275 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003276 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3277 oldTexParams.fSwizzleRGBA,
3278 sizeof(newTexParams.fSwizzleRGBA)))) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003279 this->setTextureUnit(unitIdx);
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003280 if (this->glStandard() == kGLES_GrGLStandard) {
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003281 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3282 const GrGLenum* swizzle = newTexParams.fSwizzleRGBA;
bsalomon10528f12015-10-14 12:54:52 -07003283 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3284 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3285 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3286 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003287 } else {
3288 GR_STATIC_ASSERT(sizeof(newTexParams.fSwizzleRGBA[0]) == sizeof(GrGLint));
3289 const GrGLint* swizzle = reinterpret_cast<const GrGLint*>(newTexParams.fSwizzleRGBA);
bsalomon10528f12015-10-14 12:54:52 -07003290 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, swizzle));
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003291 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003292 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003293 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003294}
3295
egdaniel080e6732014-12-22 07:35:52 -08003296void GrGLGpu::flushColorWrite(bool writeColor) {
3297 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003298 if (kNo_TriState != fHWWriteToColor) {
3299 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3300 GR_GL_FALSE, GR_GL_FALSE));
3301 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003302 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003303 } else {
3304 if (kYes_TriState != fHWWriteToColor) {
3305 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3306 fHWWriteToColor = kYes_TriState;
3307 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003308 }
bsalomon3e791242014-12-17 13:43:13 -08003309}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003310
egdaniel8dd688b2015-01-22 10:16:09 -08003311void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003312 if (fHWDrawFace != face) {
3313 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08003314 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003315 GL_CALL(Enable(GR_GL_CULL_FACE));
3316 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003317 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003318 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003319 GL_CALL(Enable(GR_GL_CULL_FACE));
3320 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003321 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003322 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003323 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003324 break;
3325 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003326 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003327 }
bsalomon3e791242014-12-17 13:43:13 -08003328 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003329 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003330}
3331
bsalomon861e1032014-12-16 07:33:49 -08003332void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003333 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003334 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003335 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003336 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003337 }
3338}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003339
bsalomon861e1032014-12-16 07:33:49 -08003340void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003341 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003342 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003343 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3344 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3345 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003346 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003347 // clear out the this field so that if a program does use this unit it will rebind the correct
3348 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003349 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003350}
3351
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003352// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08003353static inline bool can_blit_framebuffer(const GrSurface* dst,
3354 const GrSurface* src,
3355 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003356 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
bsalomon083617b2016-02-12 12:10:14 -08003357 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0)) {
3358 switch (gpu->glCaps().blitFramebufferSupport()) {
3359 case GrGLCaps::kNone_BlitFramebufferSupport:
3360 return false;
3361 case GrGLCaps::kNoScalingNoMirroring_BlitFramebufferSupport:
3362 // Our copy surface doesn't support scaling so just check for mirroring.
3363 if (dst->origin() != src->origin()) {
3364 return false;
3365 }
3366 break;
3367 case GrGLCaps::kFull_BlitFramebufferSupport:
3368 break;
3369 }
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00003370 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
3371 // or the rects are not the same (not just the same size but have the same edges).
3372 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
3373 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
3374 return false;
3375 }
bsalomon7ea33f52015-11-22 14:51:00 -08003376 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3377 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3378 return false;
3379 }
3380 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3381 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3382 return false;
3383 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003384 return true;
3385 } else {
3386 return false;
3387 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003388}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003389
bsalomon7ea33f52015-11-22 14:51:00 -08003390static inline bool can_copy_texsubimage(const GrSurface* dst,
3391 const GrSurface* src,
3392 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003393 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3394 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3395 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003396 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003397 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3398 return false;
3399 }
3400 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3401 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3402 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003403 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003404 return false;
3405 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003406 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3407 // If the src is multisampled (and uses an extension where there is a separate MSAA
3408 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003409 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003410 return false;
3411 }
bsalomon7ea33f52015-11-22 14:51:00 -08003412
3413 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3414 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3415 // texture.
3416 if (!dstTex) {
3417 return false;
3418 }
3419
3420 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003421
bsalomon7ea33f52015-11-22 14:51:00 -08003422 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3423 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003424 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003425 !GrPixelConfigIsCompressed(src->config()) &&
3426 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
3427 dstTex->target() == GR_GL_TEXTURE_2D &&
3428 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003429 return true;
3430 } else {
3431 return false;
3432 }
3433}
3434
3435// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3436// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07003437void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3438 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003439 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003440 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003441 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003442 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003443 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003444 GrGLuint* tempFBOID;
3445 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003446
egdanield803f272015-03-18 13:01:52 -07003447 if (0 == *tempFBOID) {
3448 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003449 }
3450
egdanield803f272015-03-18 13:01:52 -07003451 fStats.incRenderTargetBinds();
3452 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3453 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003454 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003455 target,
robertphillips754f4e92014-09-18 13:52:08 -07003456 texID,
3457 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003458 viewport->fLeft = 0;
3459 viewport->fBottom = 0;
3460 viewport->fWidth = surface->width();
3461 viewport->fHeight = surface->height();
3462 } else {
egdanield803f272015-03-18 13:01:52 -07003463 fStats.incRenderTargetBinds();
3464 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003465 *viewport = rt->getViewport();
3466 }
egdaniel0f5f9672015-02-03 11:10:51 -08003467}
3468
bsalomon10528f12015-10-14 12:54:52 -07003469void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
cblume61214052016-01-26 09:10:48 -08003470 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003471 if (!surface->asRenderTarget()) {
3472 SkASSERT(surface->asTexture());
3473 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3474 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3475 GR_GL_COLOR_ATTACHMENT0,
3476 textureTarget,
3477 0,
3478 0));
3479 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003480}
3481
joshualitt1c735482015-07-13 08:08:25 -07003482bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003483 // If the src is a texture, we can implement the blit as a draw assuming the config is
3484 // renderable.
3485 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3486 desc->fOrigin = kDefault_GrSurfaceOrigin;
3487 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3488 desc->fConfig = src->config();
3489 return true;
3490 }
3491
bsalomon7ea33f52015-11-22 14:51:00 -08003492 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3493 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3494 // Not supported for FBO blit or CopyTexSubImage
3495 return false;
3496 }
3497
bsalomon6df86402015-06-01 10:41:49 -07003498 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003499 // possible and we return false to fallback to creating a render target dst for render-to-
3500 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3501 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3502
bsalomon@google.comeb851172013-04-15 13:51:00 +00003503 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003504 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003505 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003506 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3507 // then we set up for that, otherwise fail.
3508 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
3509 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003510 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003511 desc->fConfig = kBGRA_8888_GrPixelConfig;
3512 return true;
3513 }
3514 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07003515 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003516 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
3517 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003518 }
3519
3520 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07003521 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003522 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3523 // fail.
3524 if (this->caps()->isConfigRenderable(src->config(), false)) {
3525 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003526 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003527 desc->fConfig = src->config();
3528 return true;
3529 }
3530 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003531 }
bsalomonf90a02b2014-11-26 12:28:00 -08003532
3533 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3534 desc->fConfig = src->config();
3535 desc->fOrigin = src->origin();
3536 desc->fFlags = kNone_GrSurfaceFlags;
3537 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003538}
3539
joshualitt1cbdcde2015-08-21 11:53:29 -07003540bool GrGLGpu::onCopySurface(GrSurface* dst,
3541 GrSurface* src,
3542 const SkIRect& srcRect,
3543 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003544 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3545 // swizzle.
3546 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3547 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3548 return false;
3549 }
bsalomon083617b2016-02-12 12:10:14 -08003550 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3551 bool preferCopy = SkToBool(dst->asRenderTarget());
3552 if (preferCopy && src->asTexture()) {
bsalomon6df86402015-06-01 10:41:49 -07003553 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
mtklein404b3b22015-05-18 09:29:10 -07003554 return true;
bsalomon5df6fee2015-05-18 06:26:15 -07003555 }
cblume61214052016-01-26 09:10:48 -08003556
bsalomon6df86402015-06-01 10:41:49 -07003557 if (can_copy_texsubimage(dst, src, this)) {
3558 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3559 return true;
3560 }
3561
mtklein404b3b22015-05-18 09:29:10 -07003562 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003563 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3564 }
3565
bsalomon083617b2016-02-12 12:10:14 -08003566 if (!preferCopy && src->asTexture()) {
3567 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
3568 return true;
3569 }
3570
bsalomon6df86402015-06-01 10:41:49 -07003571 return false;
3572}
3573
bsalomon7ea33f52015-11-22 14:51:00 -08003574void GrGLGpu::createCopyPrograms() {
3575 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
3576 fCopyPrograms[i].fProgram = 0;
bsalomon27a04872015-11-20 19:34:37 -08003577 }
cdaltonc08f1962016-02-12 12:14:06 -08003578 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
3579 const char* version = glslCaps->versionDeclString();
bsalomone5286e02016-01-14 09:24:09 -08003580 static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType,
3581 kSampler2DRect_GrSLType };
3582 SkASSERT(3 == SK_ARRAY_COUNT(fCopyPrograms));
3583 for (int i = 0; i < 3; ++i) {
3584 if (kSamplerExternal_GrSLType == kSamplerTypes[i] &&
3585 !this->glCaps().externalTextureSupport()) {
3586 continue;
3587 }
3588 if (kSampler2DRect_GrSLType == kSamplerTypes[i] &&
3589 !this->glCaps().rectangleTextureSupport()) {
3590 continue;
3591 }
bsalomon7ea33f52015-11-22 14:51:00 -08003592 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3593 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3594 GrShaderVar::kUniform_TypeModifier);
3595 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3596 GrShaderVar::kUniform_TypeModifier);
3597 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[i],
3598 GrShaderVar::kUniform_TypeModifier);
3599 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3600 GrShaderVar::kVaryingOut_TypeModifier);
3601 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3602 GrShaderVar::kOut_TypeModifier);
bsalomon6dea83f2015-12-03 12:58:06 -08003603
bsalomon7ea33f52015-11-22 14:51:00 -08003604 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -08003605 if (glslCaps->noperspectiveInterpolationSupport()) {
3606 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3607 vshaderTxt.appendf("#extension %s : require\n", extension);
3608 }
3609 vTexCoord.addModifier("noperspective");
3610 }
3611
3612 aVertex.appendDecl(glslCaps, &vshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003613 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -08003614 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003615 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -08003616 uPosXform.appendDecl(glslCaps, &vshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003617 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -08003618 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003619 vshaderTxt.append(";");
cblume61214052016-01-26 09:10:48 -08003620
bsalomon7ea33f52015-11-22 14:51:00 -08003621 vshaderTxt.append(
3622 "// Copy Program VS\n"
3623 "void main() {"
3624 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3625 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3626 " gl_Position.zw = vec2(0, 1);"
3627 "}"
3628 );
bsalomon27a04872015-11-20 19:34:37 -08003629
bsalomon7ea33f52015-11-22 14:51:00 -08003630 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -08003631 if (glslCaps->noperspectiveInterpolationSupport()) {
3632 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3633 fshaderTxt.appendf("#extension %s : require\n", extension);
3634 }
3635 }
bsalomon7ea33f52015-11-22 14:51:00 -08003636 if (kSamplerTypes[i] == kSamplerExternal_GrSLType) {
3637 fshaderTxt.appendf("#extension %s : require\n",
cdaltonc08f1962016-02-12 12:14:06 -08003638 glslCaps->externalTextureExtensionString());
bsalomon7ea33f52015-11-22 14:51:00 -08003639 }
cdaltonc08f1962016-02-12 12:14:06 -08003640 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
bsalomon7ea33f52015-11-22 14:51:00 -08003641 &fshaderTxt);
3642 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -08003643 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003644 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -08003645 uTexture.appendDecl(glslCaps, &fshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003646 fshaderTxt.append(";");
3647 const char* fsOutName;
cdaltonc08f1962016-02-12 12:14:06 -08003648 if (glslCaps->mustDeclareFragmentShaderOutput()) {
3649 oFragColor.appendDecl(glslCaps, &fshaderTxt);
bsalomon7ea33f52015-11-22 14:51:00 -08003650 fshaderTxt.append(";");
3651 fsOutName = oFragColor.c_str();
3652 } else {
3653 fsOutName = "gl_FragColor";
3654 }
3655 fshaderTxt.appendf(
3656 "// Copy Program FS\n"
3657 "void main() {"
3658 " %s = %s(u_texture, v_texCoord);"
3659 "}",
3660 fsOutName,
bsalomone5286e02016-01-14 09:24:09 -08003661 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[i], this->glslGeneration())
bsalomon7ea33f52015-11-22 14:51:00 -08003662 );
cblume61214052016-01-26 09:10:48 -08003663
bsalomon7ea33f52015-11-22 14:51:00 -08003664 GL_CALL_RET(fCopyPrograms[i].fProgram, CreateProgram());
3665 const char* str;
3666 GrGLint length;
bsalomon0315dbc2015-11-20 20:24:31 -08003667
bsalomon7ea33f52015-11-22 14:51:00 -08003668 str = vshaderTxt.c_str();
3669 length = SkToInt(vshaderTxt.size());
3670 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3671 GR_GL_VERTEX_SHADER, &str, &length, 1,
3672 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003673
bsalomon7ea33f52015-11-22 14:51:00 -08003674 str = fshaderTxt.c_str();
3675 length = SkToInt(fshaderTxt.size());
3676 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[i].fProgram,
3677 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3678 &fStats);
bsalomon0315dbc2015-11-20 20:24:31 -08003679
bsalomon7ea33f52015-11-22 14:51:00 -08003680 GL_CALL(LinkProgram(fCopyPrograms[i].fProgram));
bsalomon0315dbc2015-11-20 20:24:31 -08003681
bsalomon7ea33f52015-11-22 14:51:00 -08003682 GL_CALL_RET(fCopyPrograms[i].fTextureUniform,
3683 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texture"));
3684 GL_CALL_RET(fCopyPrograms[i].fPosXformUniform,
3685 GetUniformLocation(fCopyPrograms[i].fProgram, "u_posXform"));
3686 GL_CALL_RET(fCopyPrograms[i].fTexCoordXformUniform,
3687 GetUniformLocation(fCopyPrograms[i].fProgram, "u_texCoordXform"));
bsalomon0315dbc2015-11-20 20:24:31 -08003688
bsalomon7ea33f52015-11-22 14:51:00 -08003689 GL_CALL(BindAttribLocation(fCopyPrograms[i].fProgram, 0, "a_vertex"));
bsalomon0315dbc2015-11-20 20:24:31 -08003690
bsalomon7ea33f52015-11-22 14:51:00 -08003691 GL_CALL(DeleteShader(vshader));
3692 GL_CALL(DeleteShader(fshader));
3693 }
kkinnunen546eb5c2015-12-11 00:05:33 -08003694 fCopyProgramArrayBuffer = 0;
bsalomon7ea33f52015-11-22 14:51:00 -08003695 GL_CALL(GenBuffers(1, &fCopyProgramArrayBuffer));
3696 fHWGeometryState.setVertexBufferID(this, fCopyProgramArrayBuffer);
bsalomon6df86402015-06-01 10:41:49 -07003697 static const GrGLfloat vdata[] = {
3698 0, 0,
3699 0, 1,
3700 1, 0,
3701 1, 1
3702 };
3703 GL_ALLOC_CALL(this->glInterface(),
3704 BufferData(GR_GL_ARRAY_BUFFER,
3705 (GrGLsizeiptr) sizeof(vdata),
3706 vdata, // data ptr
3707 GR_GL_STATIC_DRAW));
3708}
3709
bsalomon6dea83f2015-12-03 12:58:06 -08003710void GrGLGpu::createWireRectProgram() {
3711 SkASSERT(!fWireRectProgram.fProgram);
3712 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3713 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3714 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3715 const char* version = this->glCaps().glslCaps()->versionDeclString();
3716
3717 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3718 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3719 // uniform using the 4 vbo vertices.
3720 SkString vshaderTxt(version);
3721 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3722 vshaderTxt.append(";");
3723 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3724 vshaderTxt.append(";");
3725 vshaderTxt.append(
3726 "// Wire Rect Program VS\n"
3727 "void main() {"
3728 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3729 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3730 " gl_Position.zw = vec2(0, 1);"
3731 "}"
3732 );
3733
3734 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
3735
3736 SkString fshaderTxt(version);
3737 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3738 *this->glCaps().glslCaps(),
3739 &fshaderTxt);
3740 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3741 fshaderTxt.append(";");
3742 const char* fsOutName;
3743 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3744 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3745 fshaderTxt.append(";");
3746 fsOutName = oFragColor.c_str();
3747 } else {
3748 fsOutName = "gl_FragColor";
3749 }
3750 fshaderTxt.appendf(
3751 "// Write Rect Program FS\n"
3752 "void main() {"
3753 " %s = %s;"
3754 "}",
3755 fsOutName,
3756 uColor.c_str()
3757 );
3758
3759 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3760 const char* str;
3761 GrGLint length;
3762
3763 str = vshaderTxt.c_str();
3764 length = SkToInt(vshaderTxt.size());
3765 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3766 GR_GL_VERTEX_SHADER, &str, &length, 1,
3767 &fStats);
3768
3769 str = fshaderTxt.c_str();
3770 length = SkToInt(fshaderTxt.size());
3771 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3772 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3773 &fStats);
3774
3775 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3776
3777 GL_CALL_RET(fWireRectProgram.fColorUniform,
3778 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3779 GL_CALL_RET(fWireRectProgram.fRectUniform,
3780 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3781 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3782
3783 GL_CALL(DeleteShader(vshader));
3784 GL_CALL(DeleteShader(fshader));
3785 GL_CALL(GenBuffers(1, &fWireRectArrayBuffer));
3786 fHWGeometryState.setVertexBufferID(this, fWireRectArrayBuffer);
3787 static const GrGLfloat vdata[] = {
3788 0, 0,
3789 0, 1,
3790 1, 1,
3791 1, 0,
3792 };
3793 GL_ALLOC_CALL(this->glInterface(),
3794 BufferData(GR_GL_ARRAY_BUFFER,
3795 (GrGLsizeiptr) sizeof(vdata),
3796 vdata, // data ptr
3797 GR_GL_STATIC_DRAW));
3798}
3799
3800void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003801 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3802 // visualization.
3803
bsalomon6dea83f2015-12-03 12:58:06 -08003804 this->handleDirtyContext();
3805 if (!fWireRectProgram.fProgram) {
3806 this->createWireRectProgram();
3807 }
3808
3809 int w = rt->width();
3810 int h = rt->height();
3811
3812 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3813 // whether the render target is flipped or not.
3814 GrGLfloat edges[4];
3815 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3816 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3817 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3818 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3819 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3820 } else {
3821 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3822 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3823 }
3824 edges[0] = 2 * edges[0] / w - 1.0f;
3825 edges[1] = 2 * edges[1] / h - 1.0f;
3826 edges[2] = 2 * edges[2] / w - 1.0f;
3827 edges[3] = 2 * edges[3] / h - 1.0f;
3828
3829 GrGLfloat channels[4];
3830 static const GrGLfloat scale255 = 1.f / 255.f;
3831 channels[0] = GrColorUnpackR(color) * scale255;
3832 channels[1] = GrColorUnpackG(color) * scale255;
3833 channels[2] = GrColorUnpackB(color) * scale255;
3834 channels[3] = GrColorUnpackA(color) * scale255;
3835
3836 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3837 this->flushRenderTarget(glRT, &rect);
3838
3839 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3840 fHWProgramID = fWireRectProgram.fProgram;
3841
3842 fHWGeometryState.setVertexArrayID(this, 0);
3843
3844 GrGLAttribArrayState* attribs =
3845 fHWGeometryState.bindArrayAndBufferToDraw(this, fWireRectArrayBuffer);
cdalton793dc262016-02-08 10:11:47 -08003846 attribs->set(this, 0, fWireRectArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3847 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003848 attribs->disableUnusedArrays(this, 0x1);
3849
3850 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3851 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3852
3853 GrXferProcessor::BlendInfo blendInfo;
3854 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003855 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003856 this->flushColorWrite(true);
3857 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003858 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003859 this->disableScissor();
3860 GrStencilSettings stencil;
3861 stencil.setDisabled();
3862 this->flushStencil(stencil);
3863
3864 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3865}
3866
3867
bsalomon6df86402015-06-01 10:41:49 -07003868void GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3869 GrSurface* src,
3870 const SkIRect& srcRect,
3871 const SkIPoint& dstPoint) {
3872 int w = srcRect.width();
3873 int h = srcRect.height();
3874
3875 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3876 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
3877 this->bindTexture(0, params, srcTex);
3878
bsalomon083617b2016-02-12 12:10:14 -08003879 GrGLIRect dstVP;
3880 this->bindSurfaceFBOForCopy(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3881 this->flushViewport(dstVP);
3882 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3883
bsalomon6df86402015-06-01 10:41:49 -07003884 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003885
bsalomon7ea33f52015-11-22 14:51:00 -08003886 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
3887
3888 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3889 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003890
3891 fHWGeometryState.setVertexArrayID(this, 0);
3892
3893 GrGLAttribArrayState* attribs =
bsalomon7ea33f52015-11-22 14:51:00 -08003894 fHWGeometryState.bindArrayAndBufferToDraw(this, fCopyProgramArrayBuffer);
cdalton793dc262016-02-08 10:11:47 -08003895 attribs->set(this, 0, fCopyProgramArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3896 0);
bsalomond6246342015-06-04 13:57:00 -07003897 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003898
3899 // dst rect edges in NDC (-1 to 1)
3900 int dw = dst->width();
3901 int dh = dst->height();
3902 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3903 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3904 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3905 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3906 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3907 dy0 = -dy0;
3908 dy1 = -dy1;
3909 }
3910
bsalomone5286e02016-01-14 09:24:09 -08003911 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3912 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3913 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3914 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07003915 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003916 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003917 sy0 = sh - sy0;
3918 sy1 = sh - sy1;
3919 }
3920 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
3921 GrGLenum srcTarget = srcTex->target();
3922 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
3923 int sw = src->width();
3924 sx0 /= sw;
3925 sx1 /= sw;
3926 sy0 /= sh;
3927 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003928 }
3929
bsalomon7ea33f52015-11-22 14:51:00 -08003930 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3931 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3932 sx1 - sx0, sy1 - sy0, sx0, sy0));
3933 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003934
3935 GrXferProcessor::BlendInfo blendInfo;
3936 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003937 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003938 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003939 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon083617b2016-02-12 12:10:14 -08003940 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003941 this->disableScissor();
3942 GrStencilSettings stencil;
3943 stencil.setDisabled();
3944 this->flushStencil(stencil);
3945
3946 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon083617b2016-02-12 12:10:14 -08003947 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, dst);
3948 this->didWriteToSurface(dst, &dstRect);
3949
bsalomon6df86402015-06-01 10:41:49 -07003950}
3951
3952void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3953 GrSurface* src,
3954 const SkIRect& srcRect,
3955 const SkIPoint& dstPoint) {
3956 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003957 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07003958 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003959 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003960 SkASSERT(dstTex);
3961 // We modified the bound FBO
3962 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3963 GrGLIRect srcGLRect;
3964 srcGLRect.setRelativeTo(srcVP,
3965 srcRect.fLeft,
3966 srcRect.fTop,
3967 srcRect.width(),
3968 srcRect.height(),
3969 src->origin());
3970
3971 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003972 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003973 GrGLint dstY;
3974 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3975 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3976 } else {
3977 dstY = dstPoint.fY;
3978 }
bsalomon10528f12015-10-14 12:54:52 -07003979 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003980 dstPoint.fX, dstY,
3981 srcGLRect.fLeft, srcGLRect.fBottom,
3982 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07003983 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003984 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3985 srcRect.width(), srcRect.height());
3986 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003987}
3988
3989bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3990 GrSurface* src,
3991 const SkIRect& srcRect,
3992 const SkIPoint& dstPoint) {
3993 SkASSERT(can_blit_framebuffer(dst, src, this));
3994 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3995 srcRect.width(), srcRect.height());
3996 if (dst == src) {
3997 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3998 return false;
mtklein404b3b22015-05-18 09:29:10 -07003999 }
bsalomon5df6fee2015-05-18 06:26:15 -07004000 }
bsalomon6df86402015-06-01 10:41:49 -07004001
bsalomon6df86402015-06-01 10:41:49 -07004002 GrGLIRect dstVP;
4003 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004004 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4005 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004006 // We modified the bound FBO
4007 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4008 GrGLIRect srcGLRect;
4009 GrGLIRect dstGLRect;
4010 srcGLRect.setRelativeTo(srcVP,
4011 srcRect.fLeft,
4012 srcRect.fTop,
4013 srcRect.width(),
4014 srcRect.height(),
4015 src->origin());
4016 dstGLRect.setRelativeTo(dstVP,
4017 dstRect.fLeft,
4018 dstRect.fTop,
4019 dstRect.width(),
4020 dstRect.height(),
4021 dst->origin());
4022
4023 // BlitFrameBuffer respects the scissor, so disable it.
4024 this->disableScissor();
4025
4026 GrGLint srcY0;
4027 GrGLint srcY1;
4028 // Does the blit need to y-mirror or not?
4029 if (src->origin() == dst->origin()) {
4030 srcY0 = srcGLRect.fBottom;
4031 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4032 } else {
4033 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4034 srcY1 = srcGLRect.fBottom;
4035 }
4036 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4037 srcY0,
4038 srcGLRect.fLeft + srcGLRect.fWidth,
4039 srcY1,
4040 dstGLRect.fLeft,
4041 dstGLRect.fBottom,
4042 dstGLRect.fLeft + dstGLRect.fWidth,
4043 dstGLRect.fBottom + dstGLRect.fHeight,
4044 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07004045 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
4046 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004047 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004048 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004049}
4050
cdalton231c5fd2015-05-13 12:35:36 -07004051void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004052 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004053 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004054 case kTexture_GrXferBarrierType: {
4055 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4056 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4057 // The render target uses separate storage so no need for glTextureBarrier.
4058 // FIXME: The render target will resolve automatically when its texture is bound,
4059 // but we could resolve only the bounds that will be read if we do it here instead.
4060 return;
4061 }
cdalton9954bc32015-04-29 14:17:00 -07004062 SkASSERT(this->caps()->textureBarrierSupport());
4063 GL_CALL(TextureBarrier());
4064 return;
cdalton231c5fd2015-05-13 12:35:36 -07004065 }
cdalton8917d622015-05-06 13:40:21 -07004066 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004067 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004068 this->caps()->blendEquationSupport());
4069 GL_CALL(BlendBarrier());
4070 return;
bsalomoncb02b382015-08-12 11:14:50 -07004071 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004072 }
4073}
4074
jvanverth88957922015-07-14 11:02:52 -07004075GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -08004076 GrPixelConfig config) {
bsalomon926cb022015-12-17 18:15:11 -08004077 if (!this->caps()->isConfigTexturable(config)) {
4078 return false;
4079 }
bsalomon091f60c2015-11-10 11:54:56 -08004080 GrGLTextureInfo* info = new GrGLTextureInfo;
4081 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004082 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004083 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004084 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4085 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004086 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08004087 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004088 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4089 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4090 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4091 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004092
bsalomon76148af2016-01-12 11:13:47 -08004093 GrGLenum internalFormat;
4094 GrGLenum externalFormat;
4095 GrGLenum externalType;
4096
4097 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4098 &externalType)) {
4099 delete info;
4100#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4101 return 0;
4102#else
4103 return reinterpret_cast<GrBackendObject>(nullptr);
4104#endif
4105 }
jvanverth672bb7f2015-07-13 07:19:57 -07004106
bsalomon091f60c2015-11-10 11:54:56 -08004107 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004108 externalType, pixels));
4109
bsalomon091f60c2015-11-10 11:54:56 -08004110#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4111 GrGLuint id = info->fID;
4112 delete info;
4113 return id;
4114#else
4115 return reinterpret_cast<GrBackendObject>(info);
4116#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004117}
4118
jvanverth88957922015-07-14 11:02:52 -07004119bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004120#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004121 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004122#else
4123 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
4124#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004125
4126 GrGLboolean result;
4127 GL_CALL_RET(result, IsTexture(texID));
4128
4129 return (GR_GL_TRUE == result);
4130}
4131
bsalomone63ffef2016-02-05 07:17:34 -08004132void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
bsalomon091f60c2015-11-10 11:54:56 -08004133#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004134 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004135#else
4136 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
4137 GrGLuint texID = info->fID;
4138#endif
4139
bsalomon67d76202015-11-11 12:40:42 -08004140 if (!abandonTexture) {
4141 GL_CALL(DeleteTextures(1, &texID));
4142 }
bsalomon091f60c2015-11-10 11:54:56 -08004143
4144#ifndef SK_IGNORE_GL_TEXTURE_TARGET
4145 delete info;
4146#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004147}
4148
joshualitt8fd844f2015-12-02 13:36:47 -08004149void GrGLGpu::resetShaderCacheForTesting() const {
4150 fProgramCache->abandon();
4151}
4152
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004153///////////////////////////////////////////////////////////////////////////////
bsalomon861e1032014-12-16 07:33:49 -08004154GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(
4155 GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +00004156 const GrGLVertexBuffer* vbuffer,
4157 const GrGLIndexBuffer* ibuffer) {
bsalomon49f085d2014-09-05 13:34:00 -07004158 SkASSERT(vbuffer);
bsalomon6df86402015-06-01 10:41:49 -07004159 GrGLuint vbufferID = vbuffer->bufferID();
halcanary96fcdcc2015-08-27 07:41:13 -07004160 GrGLuint* ibufferIDPtr = nullptr;
bsalomon6df86402015-06-01 10:41:49 -07004161 GrGLuint ibufferID;
4162 if (ibuffer) {
4163 ibufferID = ibuffer->bufferID();
4164 ibufferIDPtr = &ibufferID;
4165 }
4166 return this->internalBind(gpu, vbufferID, ibufferIDPtr);
4167}
4168
4169GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBufferToDraw(GrGLGpu* gpu,
4170 GrGLuint vbufferID) {
halcanary96fcdcc2015-08-27 07:41:13 -07004171 return this->internalBind(gpu, vbufferID, nullptr);
bsalomon6df86402015-06-01 10:41:49 -07004172}
4173
4174GrGLAttribArrayState* GrGLGpu::HWGeometryState::bindArrayAndBuffersToDraw(GrGLGpu* gpu,
4175 GrGLuint vbufferID,
4176 GrGLuint ibufferID) {
4177 return this->internalBind(gpu, vbufferID, &ibufferID);
4178}
4179
4180GrGLAttribArrayState* GrGLGpu::HWGeometryState::internalBind(GrGLGpu* gpu,
4181 GrGLuint vbufferID,
4182 GrGLuint* ibufferID) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004183 GrGLAttribArrayState* attribState;
4184
bsalomon6df86402015-06-01 10:41:49 -07004185 if (gpu->glCaps().isCoreProfile() && 0 != vbufferID) {
bsalomon8780bc62015-05-13 09:56:37 -07004186 if (!fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004187 GrGLuint arrayID;
4188 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4189 int attrCount = gpu->glCaps().maxVertexAttributes();
halcanary385fe4d2015-08-26 13:07:48 -07004190 fVBOVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004191 }
bsalomon6df86402015-06-01 10:41:49 -07004192 if (ibufferID) {
4193 attribState = fVBOVertexArray->bindWithIndexBuffer(gpu, *ibufferID);
4194 } else {
4195 attribState = fVBOVertexArray->bind(gpu);
4196 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004197 } else {
bsalomon6df86402015-06-01 10:41:49 -07004198 if (ibufferID) {
4199 this->setIndexBufferIDOnDefaultVertexArray(gpu, *ibufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004200 } else {
4201 this->setVertexArrayID(gpu, 0);
4202 }
4203 int attrCount = gpu->glCaps().maxVertexAttributes();
4204 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4205 fDefaultVertexArrayAttribState.resize(attrCount);
4206 }
4207 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004208 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004209 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004210}
bsalomone179a912016-01-20 06:18:10 -08004211
4212bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
4213 GrTextureProducer::CopyParams* copyParams) const {
4214 if (textureParams.isTiled() ||
4215 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
4216 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
4217 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4218 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
4219 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
4220 copyParams->fWidth = texture->width();
4221 copyParams->fHeight = texture->height();
4222 return true;
4223 }
4224 }
4225 return false;
4226}