blob: 2fe9c023b2197513b6d8edda9be69f4123b11fd2 [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"
cdalton397536c2016-03-25 12:15:03 -07009#include "GrGLBuffer.h"
jvanverthcba99b82015-06-24 06:59:57 -070010#include "GrGLGLSL.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070011#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080012#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070014#include "GrMesh.h"
egdaniel8dd688b2015-01-22 10:16:09 -080015#include "GrPipeline.h"
ethannicholas22793252016-01-30 09:59:10 -080016#include "GrPLSGeometryProcessor.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000020#include "GrTypes.h"
bsalomon6df86402015-06-01 10:41:49 -070021#include "builders/GrGLShaderStringBuilder.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070022#include "glsl/GrGLSL.h"
jvanverthcba99b82015-06-24 06:59:57 -070023#include "glsl/GrGLSLCaps.h"
ethannicholas22793252016-01-30 09:59:10 -080024#include "glsl/GrGLSLPLSPathRendering.h"
cblume55f2d2d2016-02-26 13:20:48 -080025#include "SkMipMap.h"
26#include "SkPixmap.h"
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000027#include "SkStrokeRec.h"
bsalomon@google.com3582bf92011-06-30 21:32:31 +000028#include "SkTemplates.h"
cblume55f2d2d2016-02-26 13:20:48 -080029#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000030
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000032#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034#define SKIP_CACHE_CHECK true
35
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000036#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
37 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
38 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
39 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000041 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
42 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
43 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
44#endif
45
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000046///////////////////////////////////////////////////////////////////////////////
47
cdaltonb85a0aa2014-07-21 15:32:44 -070048
cdalton8917d622015-05-06 13:40:21 -070049static const GrGLenum gXfermodeEquation2Blend[] = {
50 // Basic OpenGL blend equations.
51 GR_GL_FUNC_ADD,
52 GR_GL_FUNC_SUBTRACT,
53 GR_GL_FUNC_REVERSE_SUBTRACT,
54
55 // GL_KHR_blend_equation_advanced.
56 GR_GL_SCREEN,
57 GR_GL_OVERLAY,
58 GR_GL_DARKEN,
59 GR_GL_LIGHTEN,
60 GR_GL_COLORDODGE,
61 GR_GL_COLORBURN,
62 GR_GL_HARDLIGHT,
63 GR_GL_SOFTLIGHT,
64 GR_GL_DIFFERENCE,
65 GR_GL_EXCLUSION,
66 GR_GL_MULTIPLY,
67 GR_GL_HSL_HUE,
68 GR_GL_HSL_SATURATION,
69 GR_GL_HSL_COLOR,
70 GR_GL_HSL_LUMINOSITY
71};
72GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
73GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
74GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
75GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
76GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
77GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
78GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
79GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
80GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
81GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
82GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
83GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
84GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
85GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
86GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
87GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
88GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
89GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -070090GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -070091
twiz@google.com0f31ca72011-03-18 17:38:11 +000092static const GrGLenum gXfermodeCoeff2Blend[] = {
93 GR_GL_ZERO,
94 GR_GL_ONE,
95 GR_GL_SRC_COLOR,
96 GR_GL_ONE_MINUS_SRC_COLOR,
97 GR_GL_DST_COLOR,
98 GR_GL_ONE_MINUS_DST_COLOR,
99 GR_GL_SRC_ALPHA,
100 GR_GL_ONE_MINUS_SRC_ALPHA,
101 GR_GL_DST_ALPHA,
102 GR_GL_ONE_MINUS_DST_ALPHA,
103 GR_GL_CONSTANT_COLOR,
104 GR_GL_ONE_MINUS_CONSTANT_COLOR,
105 GR_GL_CONSTANT_ALPHA,
106 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000107
108 // extended blend coeffs
109 GR_GL_SRC1_COLOR,
110 GR_GL_ONE_MINUS_SRC1_COLOR,
111 GR_GL_SRC1_ALPHA,
112 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000113};
114
bsalomon861e1032014-12-16 07:33:49 -0800115bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000116 static const bool gCoeffReferencesBlendConst[] = {
117 false,
118 false,
119 false,
120 false,
121 false,
122 false,
123 false,
124 false,
125 false,
126 false,
127 true,
128 true,
129 true,
130 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000131
132 // extended blend coeffs
133 false,
134 false,
135 false,
136 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000137 };
138 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700139 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000140
bsalomon@google.com47059542012-06-06 20:51:20 +0000141 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
142 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
143 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
144 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
145 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
146 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
147 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
148 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
149 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
150 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
151 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
152 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
153 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
154 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
bsalomon@google.com47059542012-06-06 20:51:20 +0000156 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
157 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
158 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
159 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000160
161 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700162 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000163}
164
reed@google.comac10a2d2010-12-22 21:39:39 +0000165///////////////////////////////////////////////////////////////////////////////
166
egdanielff1d5472015-09-10 08:37:20 -0700167
bsalomon682c2692015-05-22 14:01:46 -0700168GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
169 GrContext* context) {
bsalomon424cc262015-05-22 10:37:30 -0700170 SkAutoTUnref<const GrGLInterface> glInterface(
171 reinterpret_cast<const GrGLInterface*>(backendContext));
172 if (!glInterface) {
173 glInterface.reset(GrGLDefaultInterface());
174 } else {
175 glInterface->ref();
176 }
177 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700178 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700179 }
bsalomon682c2692015-05-22 14:01:46 -0700180 GrGLContext* glContext = GrGLContext::Create(glInterface, options);
bsalomon424cc262015-05-22 10:37:30 -0700181 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700182 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700183 }
halcanary96fcdcc2015-08-27 07:41:13 -0700184 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700185}
186
rileya@google.come38160c2012-07-03 18:03:04 +0000187static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000188
bsalomon424cc262015-05-22 10:37:30 -0700189GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000190 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700191 , fGLContext(ctx)
192 , fProgramCache(new ProgramCache(this))
193 , fHWProgramID(0)
194 , fTempSrcFBOID(0)
195 , fTempDstFBOID(0)
196 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700197 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700198 , fHWPLSEnabled(false)
199 , fPLSHasBeenUsed(false)
200 , fHWMinSampleShading(0.0) {
201 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
202 fCopyPrograms[i].fProgram = 0;
203 }
204 fWireRectProgram.fProgram = 0;
205 fPLSSetupProgram.fProgram = 0;
206
bsalomon424cc262015-05-22 10:37:30 -0700207 SkASSERT(ctx);
208 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000209
cdalton9c3f1432016-03-11 10:07:37 -0800210 fHWBoundTextureUniqueIDs.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000211
cdaltone2e71c22016-04-07 18:13:29 -0700212 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
213 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
214 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
215 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
216 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
217 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
218 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
219 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
220 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
221 } else {
222 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
223 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
224 }
225 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
226
cdalton74b8d322016-04-11 14:47:28 -0700227 if (this->caps()->shaderCaps()->texelBufferSupport()) {
228 fHWBufferTextures.reset(this->glCaps().glslCaps()->maxCombinedSamplers());
229 }
230
cdaltone2e71c22016-04-07 18:13:29 -0700231 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
232 fPathRendering.reset(new GrGLPathRendering(this));
233 }
234
bsalomon424cc262015-05-22 10:37:30 -0700235 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000236 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000237 const GrGLubyte* vendor;
238 const GrGLubyte* renderer;
239 const GrGLubyte* version;
240 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
241 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
242 GL_CALL_RET(version, GetString(GR_GL_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800243 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000244 this);
tfarina38406c82014-10-31 07:11:12 -0700245 SkDebugf("------ VENDOR %s\n", vendor);
246 SkDebugf("------ RENDERER %s\n", renderer);
247 SkDebugf("------ VERSION %s\n", version);
248 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700249 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700250 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800251 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000252 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000253}
254
bsalomon861e1032014-12-16 07:33:49 -0800255GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700256 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
257 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800258 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700259 fCopyProgramArrayBuffer.reset();
260 fWireRectArrayBuffer.reset();
261 fPLSSetupProgram.fArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800262
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000263 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000264 // detach the current program so there is no confusion on OpenGL's part
265 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000266 GL_CALL(UseProgram(0));
267 }
268
egdanield803f272015-03-18 13:01:52 -0700269 if (0 != fTempSrcFBOID) {
270 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800271 }
egdanield803f272015-03-18 13:01:52 -0700272 if (0 != fTempDstFBOID) {
273 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800274 }
egdanield803f272015-03-18 13:01:52 -0700275 if (0 != fStencilClearFBOID) {
276 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800277 }
egdaniel0f5f9672015-02-03 11:10:51 -0800278
bsalomon7ea33f52015-11-22 14:51:00 -0800279 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
280 if (0 != fCopyPrograms[i].fProgram) {
281 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
282 }
bsalomon6df86402015-06-01 10:41:49 -0700283 }
bsalomon6dea83f2015-12-03 12:58:06 -0800284
bsalomon6dea83f2015-12-03 12:58:06 -0800285 if (0 != fWireRectProgram.fProgram) {
286 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
287 }
288
ethannicholas22793252016-01-30 09:59:10 -0800289 if (0 != fPLSSetupProgram.fProgram) {
290 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
291 }
292
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000293 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700294}
295
cdaltone2e71c22016-04-07 18:13:29 -0700296bool GrGLGpu::createPLSSetupProgram() {
297 if (!fPLSSetupProgram.fArrayBuffer) {
298 static const GrGLfloat vdata[] = {
299 0, 0,
300 0, 1,
301 1, 0,
302 1, 1
303 };
304 fPLSSetupProgram.fArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
305 kVertex_GrBufferType,
306 kStatic_GrAccessPattern, vdata));
307 if (!fPLSSetupProgram.fArrayBuffer) {
308 return false;
309 }
310 }
311
312 SkASSERT(!fPLSSetupProgram.fProgram);
313 GL_CALL_RET(fPLSSetupProgram.fProgram, CreateProgram());
314 if (!fPLSSetupProgram.fProgram) {
315 return false;
316 }
317
cdaltonc08f1962016-02-12 12:14:06 -0800318 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
319 const char* version = glslCaps->versionDeclString();
ethannicholas22793252016-01-30 09:59:10 -0800320
321 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
322 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
323 GrShaderVar::kUniform_TypeModifier);
324 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
325 GrGLSLShaderVar uTexture("u_texture", kSampler2D_GrSLType, GrShaderVar::kUniform_TypeModifier);
326 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kVaryingOut_TypeModifier);
halcanary9d524f22016-03-29 09:03:52 -0700327
ethannicholas22793252016-01-30 09:59:10 -0800328 SkString vshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800329 if (glslCaps->noperspectiveInterpolationSupport()) {
330 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
331 vshaderTxt.appendf("#extension %s : require\n", extension);
332 }
333 vTexCoord.addModifier("noperspective");
334 }
335 aVertex.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800336 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800337 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800338 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800339 uPosXform.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800340 vshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800341 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800342 vshaderTxt.append(";");
halcanary9d524f22016-03-29 09:03:52 -0700343
ethannicholas22793252016-01-30 09:59:10 -0800344 vshaderTxt.append(
345 "// PLS Setup Program VS\n"
346 "void main() {"
347 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
348 " gl_Position.zw = vec2(0, 1);"
349 "}"
350 );
351
352 SkString fshaderTxt(version);
cdaltonc08f1962016-02-12 12:14:06 -0800353 if (glslCaps->noperspectiveInterpolationSupport()) {
354 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
355 fshaderTxt.appendf("#extension %s : require\n", extension);
356 }
357 }
ethannicholas22793252016-01-30 09:59:10 -0800358 fshaderTxt.append("#extension ");
cdaltonc08f1962016-02-12 12:14:06 -0800359 fshaderTxt.append(glslCaps->fbFetchExtensionString());
ethannicholas22793252016-01-30 09:59:10 -0800360 fshaderTxt.append(" : require\n");
361 fshaderTxt.append("#extension GL_EXT_shader_pixel_local_storage : require\n");
cdaltonc08f1962016-02-12 12:14:06 -0800362 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800363 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
cdaltonc08f1962016-02-12 12:14:06 -0800364 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800365 fshaderTxt.append(";");
cdaltonc08f1962016-02-12 12:14:06 -0800366 uTexture.appendDecl(glslCaps, &fshaderTxt);
ethannicholas22793252016-01-30 09:59:10 -0800367 fshaderTxt.append(";");
368
369 fshaderTxt.appendf(
370 "// PLS Setup Program FS\n"
371 GR_GL_PLS_PATH_DATA_DECL
372 "void main() {\n"
373 " " GR_GL_PLS_DSTCOLOR_NAME " = gl_LastFragColorARM;\n"
374 " pls.windings = ivec4(0, 0, 0, 0);\n"
375 "}"
376 );
cdaltone2e71c22016-04-07 18:13:29 -0700377
ethannicholas22793252016-01-30 09:59:10 -0800378 const char* str;
379 GrGLint length;
380
381 str = vshaderTxt.c_str();
382 length = SkToInt(vshaderTxt.size());
383 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
384 GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats);
385
386 str = fshaderTxt.c_str();
387 length = SkToInt(fshaderTxt.size());
388 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
389 GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats);
390
391 GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
392
393 GL_CALL_RET(fPLSSetupProgram.fPosXformUniform, GetUniformLocation(fPLSSetupProgram.fProgram,
394 "u_posXform"));
395
396 GL_CALL(BindAttribLocation(fPLSSetupProgram.fProgram, 0, "a_vertex"));
397
398 GL_CALL(DeleteShader(vshader));
399 GL_CALL(DeleteShader(fshader));
400
cdaltone2e71c22016-04-07 18:13:29 -0700401 return true;
ethannicholas22793252016-01-30 09:59:10 -0800402}
403
bsalomon6e2aad42016-04-01 11:54:31 -0700404void GrGLGpu::disconnect(DisconnectType type) {
405 INHERITED::disconnect(type);
406 if (DisconnectType::kCleanup == type) {
407 if (fHWProgramID) {
408 GL_CALL(UseProgram(0));
409 }
410 if (fTempSrcFBOID) {
411 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
412 }
413 if (fTempDstFBOID) {
414 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
415 }
416 if (fStencilClearFBOID) {
417 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
418 }
bsalomon6e2aad42016-04-01 11:54:31 -0700419 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
420 if (fCopyPrograms[i].fProgram) {
421 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
422 }
423 }
424 if (fWireRectProgram.fProgram) {
425 GL_CALL(DeleteProgram(fWireRectProgram.fProgram));
426 }
bsalomon6e2aad42016-04-01 11:54:31 -0700427 if (fPLSSetupProgram.fProgram) {
428 GL_CALL(DeleteProgram(fPLSSetupProgram.fProgram));
429 }
bsalomon6e2aad42016-04-01 11:54:31 -0700430 } else {
431 if (fProgramCache) {
432 fProgramCache->abandon();
433 }
434 }
435
436 delete fProgramCache;
437 fProgramCache = nullptr;
438
bsalomonc8dc1f72014-08-21 13:02:13 -0700439 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700440 fTempSrcFBOID = 0;
441 fTempDstFBOID = 0;
442 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700443 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800444 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
445 fCopyPrograms[i].fProgram = 0;
446 }
bsalomon6dea83f2015-12-03 12:58:06 -0800447 fWireRectProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700448 fWireRectArrayBuffer.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700449 fPLSSetupProgram.fProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700450 fPLSSetupProgram.fArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700451 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700452 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700453 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000454}
455
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000456///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000457
bsalomon861e1032014-12-16 07:33:49 -0800458void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000459 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000460 if (resetBits & kMisc_GrGLBackendState) {
461 GL_CALL(Disable(GR_GL_DEPTH_TEST));
462 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000463
cdaltone2e71c22016-04-07 18:13:29 -0700464 fHWBufferState[kTexel_GrBufferType].invalidate();
465 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
466 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
467 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700468
egdaniel8dd688b2015-01-22 10:16:09 -0800469 fHWDrawFace = GrPipelineBuilder::kInvalid_DrawFace;
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000471 if (kGL_GrGLStandard == this->glStandard()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000472 // Desktop-only state that we never change
473 if (!this->glCaps().isCoreProfile()) {
474 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
475 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
476 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
477 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
478 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
479 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
480 }
481 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
482 // core profile. This seems like a bug since the core spec removes any mention of
483 // GL_ARB_imaging.
484 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
485 GL_CALL(Disable(GR_GL_COLOR_TABLE));
486 }
487 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
488 // Since ES doesn't support glPointSize at all we always use the VS to
489 // set the point size
490 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
491
492 // We should set glPolygonMode(FRONT_AND_BACK,FILL) here, too. It isn't
493 // currently part of our gl interface. There are probably others as
494 // well.
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000495 }
joshualitt58162332014-08-01 06:44:53 -0700496
497 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700498 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700499 // The arm extension requires specifically enabling MSAA fetching per sample.
500 // On some devices this may have a perf hit. Also multiple render targets are disabled
501 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
502 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000503 fHWWriteToColor = kUnknown_TriState;
504 // we only ever use lines in hairline mode
505 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700506 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000507 }
edisonn@google.comba669992013-06-28 16:03:21 +0000508
egdanielb414f252014-07-29 13:15:47 -0700509 if (resetBits & kMSAAEnable_GrGLBackendState) {
510 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700511
egdanieleed519e2016-01-15 11:36:18 -0800512 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800513 if (0 != this->caps()->maxRasterSamples()) {
514 fHWRasterMultisampleEnabled = kUnknown_TriState;
515 fHWNumRasterSamples = 0;
516 }
517
518 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
519 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700520 GL_CALL(CoverageModulation(GR_GL_RGBA));
521 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000522 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000523
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000524 fHWActiveTextureUnitIdx = -1; // invalid
525
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000526 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700527 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
528 fHWBoundTextureUniqueIDs[s] = SK_InvalidUniqueID;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000529 }
cdalton74b8d322016-04-11 14:47:28 -0700530 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
531 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
532 fHWBufferTextures[b].fKnownBound = false;
533 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000534 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000535
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 if (resetBits & kBlend_GrGLBackendState) {
537 fHWBlendState.invalidate();
538 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000539
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000540 if (resetBits & kView_GrGLBackendState) {
541 fHWScissorSettings.invalidate();
542 fHWViewport.invalidate();
543 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000545 if (resetBits & kStencil_GrGLBackendState) {
546 fHWStencilSettings.invalidate();
547 fHWStencilTestEnabled = kUnknown_TriState;
548 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000549
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000550 // Vertex
551 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700552 fHWVertexArrayState.invalidate();
553 fHWBufferState[kVertex_GrBufferType].invalidate();
554 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000556
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000557 if (resetBits & kRenderTarget_GrGLBackendState) {
egdanield803f272015-03-18 13:01:52 -0700558 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon16921ec2015-07-30 15:34:56 -0700559 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000560 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000561
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000562 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700563 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700564 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000565 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000566 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000567
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000568 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 if (resetBits & kPixelStore_GrGLBackendState) {
570 if (this->glCaps().unpackRowLengthSupport()) {
571 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
572 }
573 if (this->glCaps().packRowLengthSupport()) {
574 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
575 }
576 if (this->glCaps().unpackFlipYSupport()) {
577 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
578 }
579 if (this->glCaps().packFlipYSupport()) {
580 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
581 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000582 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000583
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000584 if (resetBits & kProgram_GrGLBackendState) {
585 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587}
588
egdanielcf614fd2015-04-22 13:58:58 -0700589static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000590 // By default, GrRenderTargets are GL's normal orientation so that they
591 // can be drawn to by the outside world without the client having
592 // to render upside down.
593 if (kDefault_GrSurfaceOrigin == origin) {
594 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
595 } else {
596 return origin;
597 }
598}
599
bsalomon6dc6f5f2015-06-18 09:12:16 -0700600GrTexture* GrGLGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
601 GrWrapOwnership ownership) {
bsalomon091f60c2015-11-10 11:54:56 -0800602#ifdef SK_IGNORE_GL_TEXTURE_TARGET
603 if (!desc.fTextureHandle) {
halcanary96fcdcc2015-08-27 07:41:13 -0700604 return nullptr;
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000605 }
bsalomon091f60c2015-11-10 11:54:56 -0800606#else
607 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
608 if (!info || !info->fID) {
609 return nullptr;
610 }
611#endif
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000612
bsalomon7ea33f52015-11-22 14:51:00 -0800613 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
614 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
615
bsalomonb15b4c12014-10-29 12:41:57 -0700616 GrGLTexture::IDDesc idDesc;
617 GrSurfaceDesc surfDesc;
618
bsalomon091f60c2015-11-10 11:54:56 -0800619#ifdef SK_IGNORE_GL_TEXTURE_TARGET
620 idDesc.fInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
cblume55f2d2d2016-02-26 13:20:48 -0800621 // When we create the texture, we only
622 // create GL_TEXTURE_2D at the moment.
623 // External clients can do something different.
624
bsalomon091f60c2015-11-10 11:54:56 -0800625 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
626#else
627 idDesc.fInfo = *info;
628#endif
bsalomone5286e02016-01-14 09:24:09 -0800629
bsalomon7ea33f52015-11-22 14:51:00 -0800630 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
631 if (renderTarget) {
632 // This combination is not supported.
633 return nullptr;
634 }
cdalton9c3f1432016-03-11 10:07:37 -0800635 if (!this->glCaps().glslCaps()->externalTextureSupport()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800636 return nullptr;
637 }
bsalomone5286e02016-01-14 09:24:09 -0800638 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc.fInfo.fTarget) {
639 if (!this->glCaps().rectangleTextureSupport()) {
640 return nullptr;
641 }
642 } else if (GR_GL_TEXTURE_2D != idDesc.fInfo.fTarget) {
643 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800644 }
bsalomone5286e02016-01-14 09:24:09 -0800645
646 // Sample count is interpreted to mean the number of samples that Gr code should allocate
bsalomona98419b2015-11-23 07:09:50 -0800647 // for a render buffer that resolves to the texture. We don't support MSAA textures.
648 if (desc.fSampleCnt && !renderTarget) {
649 return nullptr;
650 }
bsalomon10528f12015-10-14 12:54:52 -0700651
kkinnunen2e6055b2016-04-22 01:48:29 -0700652 if (kAdopt_GrWrapOwnership == ownership) {
653 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
654 } else {
655 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
bsalomone5286e02016-01-14 09:24:09 -0800656 }
bsalomonb15b4c12014-10-29 12:41:57 -0700657
bsalomonb15b4c12014-10-29 12:41:57 -0700658 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
659 surfDesc.fWidth = desc.fWidth;
660 surfDesc.fHeight = desc.fHeight;
661 surfDesc.fConfig = desc.fConfig;
senorblanco94e50102015-04-06 09:42:57 -0700662 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000663 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
664 // assuming the old behaviour, which is that backend textures are always
665 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
666 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
667 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
bsalomonb15b4c12014-10-29 12:41:57 -0700668 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000669 } else {
bsalomonb15b4c12014-10-29 12:41:57 -0700670 surfDesc.fOrigin = desc.fOrigin;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000671 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000672
halcanary96fcdcc2015-08-27 07:41:13 -0700673 GrGLTexture* texture = nullptr;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000674 if (renderTarget) {
bsalomonb15b4c12014-10-29 12:41:57 -0700675 GrGLRenderTarget::IDDesc rtIDDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -0700676 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700677 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000678 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700679 texture = GrGLTextureRenderTarget::CreateWrapped(this, surfDesc, idDesc, rtIDDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000680 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -0700681 texture = GrGLTexture::CreateWrapped(this, surfDesc, idDesc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000682 }
halcanary96fcdcc2015-08-27 07:41:13 -0700683 if (nullptr == texture) {
684 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000685 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000686
bsalomon@google.come269f212011-11-07 13:29:52 +0000687 return texture;
688}
689
bsalomon6dc6f5f2015-06-18 09:12:16 -0700690GrRenderTarget* GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
691 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700692 GrGLRenderTarget::IDDesc idDesc;
egdanield803f272015-03-18 13:01:52 -0700693 idDesc.fRTFBOID = static_cast<GrGLuint>(wrapDesc.fRenderTargetHandle);
bsalomonb15b4c12014-10-29 12:41:57 -0700694 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700695 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
kkinnunen2e6055b2016-04-22 01:48:29 -0700696 if (kAdopt_GrWrapOwnership == ownership) {
697 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
698 } else {
699 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
cblume61214052016-01-26 09:10:48 -0800700 }
senorblancod7395d82015-06-18 13:26:52 -0700701 idDesc.fSampleConfig = GrRenderTarget::kUnified_SampleConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000702
bsalomonb15b4c12014-10-29 12:41:57 -0700703 GrSurfaceDesc desc;
704 desc.fConfig = wrapDesc.fConfig;
senorblanco4b013292015-08-19 09:10:28 -0700705 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
bsalomonb15b4c12014-10-29 12:41:57 -0700706 desc.fWidth = wrapDesc.fWidth;
707 desc.fHeight = wrapDesc.fHeight;
senorblanco94e50102015-04-06 09:42:57 -0700708 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
bsalomonb15b4c12014-10-29 12:41:57 -0700709 desc.fOrigin = resolve_origin(wrapDesc.fOrigin, true);
710
egdanielec00d942015-09-14 12:56:10 -0700711 return GrGLRenderTarget::CreateWrapped(this, desc, idDesc, wrapDesc.fStencilBits);
bsalomon@google.come269f212011-11-07 13:29:52 +0000712}
713
kkinnunen49c4c222016-04-01 04:50:37 -0700714GrRenderTarget* GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800715#ifdef SK_IGNORE_GL_TEXTURE_TARGET
716 if (!desc.fTextureHandle) {
717 return nullptr;
718 }
719#else
720 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(desc.fTextureHandle);
721 if (!info || !info->fID) {
722 return nullptr;
723 }
724#endif
725
benjaminwagner817fed32016-04-29 11:17:07 -0700726 GrGLTextureInfo texInfo;
ericrkf7b8b8a2016-02-24 14:49:51 -0800727 GrSurfaceDesc surfDesc;
728
729#ifdef SK_IGNORE_GL_TEXTURE_TARGET
benjaminwagner817fed32016-04-29 11:17:07 -0700730 texInfo.fID = static_cast<GrGLuint>(desc.fTextureHandle);
ericrkf7b8b8a2016-02-24 14:49:51 -0800731 // We only support GL_TEXTURE_2D at the moment.
benjaminwagner817fed32016-04-29 11:17:07 -0700732 texInfo.fTarget = GR_GL_TEXTURE_2D;
ericrkf7b8b8a2016-02-24 14:49:51 -0800733#else
benjaminwagner817fed32016-04-29 11:17:07 -0700734 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800735#endif
736
benjaminwagner817fed32016-04-29 11:17:07 -0700737 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
738 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800739 // Only texture rectangle and texture 2d are supported. We do not check whether texture
740 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
741 // we assume the necessary support exists.
742 return nullptr;
743 }
744
ericrkf7b8b8a2016-02-24 14:49:51 -0800745 surfDesc.fFlags = (GrSurfaceFlags) desc.fFlags;
746 surfDesc.fWidth = desc.fWidth;
747 surfDesc.fHeight = desc.fHeight;
748 surfDesc.fConfig = desc.fConfig;
749 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
750 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
751 // assuming the old behaviour, which is that backend textures are always
752 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
753 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
754 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
755 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
756 } else {
757 surfDesc.fOrigin = desc.fOrigin;
758 }
759
760 GrGLRenderTarget::IDDesc rtIDDesc;
benjaminwagner817fed32016-04-29 11:17:07 -0700761 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800762 return nullptr;
763 }
benjaminwagner817fed32016-04-29 11:17:07 -0700764 return GrGLRenderTarget::CreateWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800765}
766
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000767////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800768
bsalomonf0674512015-07-28 13:26:15 -0700769bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800770 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700771 DrawPreference* drawPreference,
772 WritePixelTempDrawInfo* tempDrawInfo) {
773 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
774 return false;
775 }
776
bsalomon6cb3cbe2015-07-30 07:34:27 -0700777 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
778 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
779 if (!dstSurface->asTexture()) {
780 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon7ea33f52015-11-22 14:51:00 -0800781 } else {
782 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
bsalomone5286e02016-01-14 09:24:09 -0800783 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
784 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800785 return false;
786 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700787 }
788
bsalomon16921ec2015-07-30 15:34:56 -0700789 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
790 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
791 }
792
bsalomon6c9cd552016-01-22 07:17:34 -0800793 // Start off assuming no swizzling
794 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
795 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700796
797 // These settings we will always want if a temp draw is performed. Initially set the config
798 // to srcConfig, though that may be modified if we decide to do a R/G swap.
799 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
800 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
801 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
802 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
803 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
804 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
805
806 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
807
808 if (configsAreRBSwaps) {
809 if (!this->caps()->isConfigTexturable(srcConfig)) {
810 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
811 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800812 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
813 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700814 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
815 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700816 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
817 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800818 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
819 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700820 } else if (kGLES_GrGLStandard == this->glStandard() &&
821 this->glCaps().bgraIsInternalFormat()) {
822 // The internal format and external formats must match texture uploads so we can't
823 // swizzle while uploading when BGRA is a distinct internal format.
824 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
825 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800826 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
827 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700828 }
829 }
830
831 if (!this->glCaps().unpackFlipYSupport() &&
832 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
833 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
834 }
835
836 return true;
837}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000838
jvanverth17aa0472016-01-05 10:41:27 -0800839static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
840 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700841 if (!glTex) {
842 return false;
843 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000844
bsalomon16921ec2015-07-30 15:34:56 -0700845 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
846 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
847 return false;
848 }
849
bsalomone5286e02016-01-14 09:24:09 -0800850 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
851 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800852 return false;
853 }
854
jvanverth17aa0472016-01-05 10:41:27 -0800855 return true;
856}
857
858bool GrGLGpu::onWritePixels(GrSurface* surface,
859 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800860 GrPixelConfig config,
861 const SkTArray<GrMipLevel>& texels) {
jvanverth17aa0472016-01-05 10:41:27 -0800862 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
863
864 if (!check_write_and_transfer_input(glTex, surface, config)) {
865 return false;
866 }
867
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000868 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700869 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000870
krajcevski145d48c2014-06-11 16:07:50 -0700871 bool success = false;
bsalomonb15b4c12014-10-29 12:41:57 -0700872 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
bsalomon861e1032014-12-16 07:33:49 -0800873 // We check that config == desc.fConfig in GrGLGpu::canWriteTexturePixels()
bsalomonb15b4c12014-10-29 12:41:57 -0700874 SkASSERT(config == glTex->desc().fConfig);
cblume55f2d2d2016-02-26 13:20:48 -0800875 success = this->uploadCompressedTexData(glTex->desc(), glTex->target(), texels,
jvanverth17aa0472016-01-05 10:41:27 -0800876 kWrite_UploadType, left, top, width, height);
krajcevski145d48c2014-06-11 16:07:50 -0700877 } else {
jcgregorioe7d7f902016-02-04 19:51:25 -0800878 success = this->uploadTexData(glTex->desc(), glTex->target(), kWrite_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800879 left, top, width, height, config, texels);
krajcevski145d48c2014-06-11 16:07:50 -0700880 }
881
brianosman3ebd2762016-03-29 11:27:20 -0700882 if (success) {
883 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
jvanverthfb591642016-04-29 09:50:25 -0700884 this->didWriteToSurface(surface, &rect, texels.count());
brianosman3ebd2762016-03-29 11:27:20 -0700885 }
886
cblume55f2d2d2016-02-26 13:20:48 -0800887 return success;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000888}
889
jvanverthc3d706f2016-04-20 10:33:27 -0700890bool GrGLGpu::onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800891 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700892 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800893 size_t offset, size_t rowBytes) {
jvanverthc3d706f2016-04-20 10:33:27 -0700894 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800895
jvanverthc3d706f2016-04-20 10:33:27 -0700896 if (!check_write_and_transfer_input(glTex, surface, config)) {
jvanverth17aa0472016-01-05 10:41:27 -0800897 return false;
898 }
899
900 // For the moment, can't transfer compressed data
901 if (GrPixelConfigIsCompressed(glTex->desc().fConfig)) {
902 return false;
903 }
904
905 this->setScratchTextureUnit();
906 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
907
cdalton397536c2016-03-25 12:15:03 -0700908 SkASSERT(!transferBuffer->isMapped());
909 SkASSERT(!transferBuffer->isCPUBacked());
910 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
cdaltone2e71c22016-04-07 18:13:29 -0700911 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800912
913 bool success = false;
cblume55f2d2d2016-02-26 13:20:48 -0800914 GrMipLevel mipLevel;
jvanverthc3d706f2016-04-20 10:33:27 -0700915 mipLevel.fPixels = transferBuffer;
cblume55f2d2d2016-02-26 13:20:48 -0800916 mipLevel.fRowBytes = rowBytes;
917 SkSTArray<1, GrMipLevel> texels;
918 texels.push_back(mipLevel);
jcgregorioe7d7f902016-02-04 19:51:25 -0800919 success = this->uploadTexData(glTex->desc(), glTex->target(), kTransfer_UploadType,
cblume55f2d2d2016-02-26 13:20:48 -0800920 left, top, width, height, config, texels);
jvanverth17aa0472016-01-05 10:41:27 -0800921 if (success) {
brianosman3ebd2762016-03-29 11:27:20 -0700922 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
jvanverthc3d706f2016-04-20 10:33:27 -0700923 this->didWriteToSurface(surface, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800924 return true;
925 }
926
927 return false;
928}
929
bsalomonf46a1242015-12-15 12:37:38 -0800930// For GL_[UN]PACK_ALIGNMENT.
931static inline GrGLint config_alignment(GrPixelConfig config) {
932 SkASSERT(!GrPixelConfigIsCompressed(config));
933 switch (config) {
934 case kAlpha_8_GrPixelConfig:
935 return 1;
936 case kRGB_565_GrPixelConfig:
937 case kRGBA_4444_GrPixelConfig:
938 case kAlpha_half_GrPixelConfig:
939 case kRGBA_half_GrPixelConfig:
940 return 2;
941 case kRGBA_8888_GrPixelConfig:
942 case kBGRA_8888_GrPixelConfig:
943 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700944 case kSBGRA_8888_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800945 case kRGBA_float_GrPixelConfig:
946 return 4;
947 default:
948 return 0;
949 }
950}
951
bsalomonb15b4c12014-10-29 12:41:57 -0700952static inline GrGLenum check_alloc_error(const GrSurfaceDesc& desc,
953 const GrGLInterface* interface) {
bsalomonf2703d82014-10-28 14:33:06 -0700954 if (SkToBool(desc.fFlags & kCheckAllocation_GrSurfaceFlag)) {
senorblanco@chromium.orgd0925242013-06-10 15:06:09 +0000955 return GR_GL_GET_ERROR(interface);
956 } else {
957 return CHECK_ALLOC_ERROR(interface);
958 }
959}
960
cblume55f2d2d2016-02-26 13:20:48 -0800961/**
962 * Creates storage space for the texture and fills it with texels.
963 *
964 * @param desc The surface descriptor for the texture being created.
965 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800966 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -0800967 * @param internalFormat The data format used for the internal storage of the texture.
968 * @param externalFormat The data format used for the external storage of the texture.
969 * @param externalType The type of the data used for the external storage of the texture.
970 * @param texels The texel data of the texture being created.
971 * @param baseWidth The width of the texture's base mipmap level
972 * @param baseHeight The height of the texture's base mipmap level
973 * @param succeeded Set to true if allocating and populating the texture completed
974 * without error.
975 */
bsalomone699d0c2016-03-09 06:25:15 -0800976static bool allocate_and_populate_uncompressed_texture(const GrSurfaceDesc& desc,
cblume55f2d2d2016-02-26 13:20:48 -0800977 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -0800978 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -0800979 GrGLenum target,
980 GrGLenum internalFormat,
981 GrGLenum externalFormat,
982 GrGLenum externalType,
983 const SkTArray<GrMipLevel>& texels,
bsalomone699d0c2016-03-09 06:25:15 -0800984 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800985 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800986
987 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
988 // We can only use TexStorage if we know we will not later change the storage requirements.
989 // This means if we may later want to add mipmaps, we cannot use TexStorage.
990 // Right now, we cannot know if we will later add mipmaps or not.
991 // The only time we can use TexStorage is when we already have the
992 // mipmaps.
993 useTexStorage &= texels.count() > 1;
994
995 if (useTexStorage) {
996 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800997 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -0800998 TexStorage2D(target,
999 texels.count(),
1000 internalFormat,
1001 desc.fWidth, desc.fHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001002 GrGLenum error = check_alloc_error(desc, &interface);
1003 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001004 return false;
cblume790d5132016-02-29 11:13:29 -08001005 } else {
1006 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1007 const void* currentMipData = texels[currentMipLevel].fPixels;
1008 if (currentMipData == nullptr) {
1009 continue;
1010 }
1011 int twoToTheMipLevel = 1 << currentMipLevel;
1012 int currentWidth = SkTMax(1, desc.fWidth / twoToTheMipLevel);
1013 int currentHeight = SkTMax(1, desc.fHeight / twoToTheMipLevel);
1014
1015 GR_GL_CALL(&interface,
1016 TexSubImage2D(target,
1017 currentMipLevel,
1018 0, // left
1019 0, // top
1020 currentWidth,
1021 currentHeight,
1022 externalFormat, externalType,
1023 currentMipData));
1024 }
bsalomone699d0c2016-03-09 06:25:15 -08001025 return true;
cblume790d5132016-02-29 11:13:29 -08001026 }
1027 } else {
bsalomone699d0c2016-03-09 06:25:15 -08001028 if (texels.empty()) {
cblume790d5132016-02-29 11:13:29 -08001029 GL_ALLOC_CALL(&interface,
1030 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001031 0,
cblume790d5132016-02-29 11:13:29 -08001032 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001033 baseWidth,
1034 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001035 0, // border
1036 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001037 nullptr));
cblume790d5132016-02-29 11:13:29 -08001038 GrGLenum error = check_alloc_error(desc, &interface);
1039 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001040 return false;
1041 }
1042 } else {
1043 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1044 int twoToTheMipLevel = 1 << currentMipLevel;
1045 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1046 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1047 const void* currentMipData = texels[currentMipLevel].fPixels;
1048 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1049 // This will allocate texture memory which we can later populate.
1050 GL_ALLOC_CALL(&interface,
1051 TexImage2D(target,
1052 currentMipLevel,
1053 internalFormat,
1054 currentWidth,
1055 currentHeight,
1056 0, // border
1057 externalFormat, externalType,
1058 currentMipData));
1059 GrGLenum error = check_alloc_error(desc, &interface);
1060 if (error != GR_GL_NO_ERROR) {
1061 return false;
1062 }
cblume790d5132016-02-29 11:13:29 -08001063 }
cblume55f2d2d2016-02-26 13:20:48 -08001064 }
1065 }
bsalomone699d0c2016-03-09 06:25:15 -08001066 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001067}
1068
1069/**
1070 * Creates storage space for the texture and fills it with texels.
1071 *
1072 * @param desc The surface descriptor for the texture being created.
1073 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -08001074 * @param caps The capabilities of the GL device.
cblume55f2d2d2016-02-26 13:20:48 -08001075 * @param internalFormat The data format used for the internal storage of the texture.
1076 * @param texels The texel data of the texture being created.
cblume55f2d2d2016-02-26 13:20:48 -08001077 */
1078static bool allocate_and_populate_compressed_texture(const GrSurfaceDesc& desc,
1079 const GrGLInterface& interface,
cblume790d5132016-02-29 11:13:29 -08001080 const GrGLCaps& caps,
cblume55f2d2d2016-02-26 13:20:48 -08001081 GrGLenum target, GrGLenum internalFormat,
1082 const SkTArray<GrMipLevel>& texels,
1083 int baseWidth, int baseHeight) {
1084 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001085
cblume790d5132016-02-29 11:13:29 -08001086 bool useTexStorage = caps.isConfigTexSupportEnabled(desc.fConfig);
1087 // We can only use TexStorage if we know we will not later change the storage requirements.
1088 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1089 // Right now, we cannot know if we will later add mipmaps or not.
1090 // The only time we can use TexStorage is when we already have the
1091 // mipmaps.
1092 useTexStorage &= texels.count() > 1;
cblume55f2d2d2016-02-26 13:20:48 -08001093
cblume790d5132016-02-29 11:13:29 -08001094 if (useTexStorage) {
1095 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001096 GL_ALLOC_CALL(&interface,
cblume790d5132016-02-29 11:13:29 -08001097 TexStorage2D(target,
1098 texels.count(),
1099 internalFormat,
1100 baseWidth, baseHeight));
cblume55f2d2d2016-02-26 13:20:48 -08001101 GrGLenum error = check_alloc_error(desc, &interface);
1102 if (error != GR_GL_NO_ERROR) {
1103 return false;
cblume790d5132016-02-29 11:13:29 -08001104 } else {
1105 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1106 const void* currentMipData = texels[currentMipLevel].fPixels;
1107 if (currentMipData == nullptr) {
1108 continue;
1109 }
1110
1111 int twoToTheMipLevel = 1 << currentMipLevel;
1112 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1113 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1114
1115 // Make sure that the width and height that we pass to OpenGL
1116 // is a multiple of the block size.
1117 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1118 currentHeight);
1119 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1120 currentMipLevel,
1121 0, // left
1122 0, // top
1123 currentWidth,
1124 currentHeight,
1125 internalFormat,
1126 SkToInt(dataSize),
1127 currentMipData));
1128 }
1129 }
1130 } else {
1131 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
1132 int twoToTheMipLevel = 1 << currentMipLevel;
1133 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1134 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1135
1136 // Make sure that the width and height that we pass to OpenGL
1137 // is a multiple of the block size.
1138 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, baseWidth, baseHeight);
1139
1140 GL_ALLOC_CALL(&interface,
1141 CompressedTexImage2D(target,
1142 currentMipLevel,
1143 internalFormat,
1144 currentWidth,
1145 currentHeight,
1146 0, // border
1147 SkToInt(dataSize),
1148 texels[currentMipLevel].fPixels));
1149
1150 GrGLenum error = check_alloc_error(desc, &interface);
1151 if (error != GR_GL_NO_ERROR) {
1152 return false;
1153 }
cblume55f2d2d2016-02-26 13:20:48 -08001154 }
1155 }
1156
1157 return true;
1158}
1159
1160/**
1161 * After a texture is created, any state which was altered during its creation
1162 * needs to be restored.
1163 *
1164 * @param interface The GL interface to use.
1165 * @param caps The capabilities of the GL device.
1166 * @param restoreGLRowLength Should the row length unpacking be restored?
1167 * @param glFlipY Did GL flip the texture vertically?
1168 */
1169static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1170 bool restoreGLRowLength, bool glFlipY) {
1171 if (restoreGLRowLength) {
1172 SkASSERT(caps.unpackRowLengthSupport());
1173 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1174 }
1175 if (glFlipY) {
1176 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1177 }
1178}
1179
bsalomon861e1032014-12-16 07:33:49 -08001180bool GrGLGpu::uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -08001181 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -08001182 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001183 int left, int top, int width, int height,
1184 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -08001185 const SkTArray<GrMipLevel>& texels) {
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001186 // If we're uploading compressed data then we should be using uploadCompressedTexData
1187 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1188
bsalomon5b30c6f2015-12-17 14:17:34 -08001189 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
1190
cblume55f2d2d2016-02-26 13:20:48 -08001191 // texels is const.
1192 // But we may need to flip the texture vertically to prepare it.
1193 // Rather than flip in place and alter the incoming data,
1194 // we allocate a new buffer to flip into.
1195 // This means we need to make a non-const shallow copy of texels.
1196 SkTArray<GrMipLevel> texelsShallowCopy(texels);
1197
1198 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
1199 currentMipLevel--) {
bsalomone699d0c2016-03-09 06:25:15 -08001200 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001201 }
1202
1203 const GrGLInterface* interface = this->glInterface();
1204 const GrGLCaps& caps = this->glCaps();
1205
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001206 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001207
1208 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001209 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001210 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001211
cblume55f2d2d2016-02-26 13:20:48 -08001212 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1213 int twoToTheMipLevel = 1 << currentMipLevel;
1214 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1215 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1216
cblume55f2d2d2016-02-26 13:20:48 -08001217 if (currentHeight > SK_MaxS32 ||
1218 currentWidth > SK_MaxS32) {
1219 return false;
1220 }
1221 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
1222 &currentWidth,
1223 &currentHeight,
1224 &texelsShallowCopy[currentMipLevel].fPixels,
1225 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
1226 return false;
1227 }
1228 if (currentWidth < 0 || currentHeight < 0) {
1229 return false;
1230 }
1231 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001232
bsalomon5b30c6f2015-12-17 14:17:34 -08001233 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001234 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001235 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001236 GrGLenum externalFormat;
1237 GrGLenum externalType;
1238 if (!this->glCaps().getTexImageFormats(desc.fConfig, dataConfig, &internalFormat,
1239 &externalFormat, &externalType)) {
1240 return false;
1241 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001242 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001243 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001244 * because our srcData has extra bytes past each row. If so, we need
1245 * to trim those off here, since GL ES may not let us specify
1246 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001247 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001248 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001249 bool swFlipY = false;
1250 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001251
bsalomone699d0c2016-03-09 06:25:15 -08001252 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()) {
cblume55f2d2d2016-02-26 13:20:48 -08001253 if (caps.unpackFlipYSupport()) {
1254 glFlipY = true;
1255 } else {
1256 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001257 }
cblume55f2d2d2016-02-26 13:20:48 -08001258 }
1259
1260 // in case we need a temporary, trimmed copy of the src pixels
1261 SkAutoSMalloc<128 * 128> tempStorage;
1262
1263 // find the combined size of all the mip levels and the relative offset of
1264 // each into the collective buffer
1265 size_t combined_buffer_size = 0;
1266 SkTArray<size_t> individual_mip_offsets(texelsShallowCopy.count());
1267 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
1268 int twoToTheMipLevel = 1 << currentMipLevel;
1269 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1270 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1271 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1272 individual_mip_offsets.push_back(combined_buffer_size);
1273 combined_buffer_size += trimmedSize;
1274 }
1275 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1276
1277 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001278 int twoToTheMipLevel = 1 << currentMipLevel;
1279 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1280 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1281 const size_t trimRowBytes = currentWidth * bpp;
1282
1283 /*
1284 * check whether to allocate a temporary buffer for flipping y or
1285 * because our srcData has extra bytes past each row. If so, we need
1286 * to trim those off here, since GL ES may not let us specify
1287 * GL_UNPACK_ROW_LENGTH.
1288 */
1289 restoreGLRowLength = false;
1290
1291 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
1292 if (caps.unpackRowLengthSupport() && !swFlipY) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001293 // can't use this for flipping, only non-neg values allowed. :(
1294 if (rowBytes != trimRowBytes) {
1295 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001296 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001297 restoreGLRowLength = true;
1298 }
jvanverth17aa0472016-01-05 10:41:27 -08001299 } else if (kTransfer_UploadType != uploadType) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001300 if (trimRowBytes != rowBytes || swFlipY) {
1301 // copy data into our new storage, skipping the trailing bytes
cblume55f2d2d2016-02-26 13:20:48 -08001302 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1303 if (swFlipY && currentHeight >= 1) {
1304 src += (currentHeight - 1) * rowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001305 }
cblume55f2d2d2016-02-26 13:20:48 -08001306 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1307 for (int y = 0; y < currentHeight; y++) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001308 memcpy(dst, src, trimRowBytes);
1309 if (swFlipY) {
1310 src -= rowBytes;
1311 } else {
1312 src += rowBytes;
1313 }
1314 dst += trimRowBytes;
1315 }
1316 // now point data to our copied version
cblume55f2d2d2016-02-26 13:20:48 -08001317 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1318 individual_mip_offsets[currentMipLevel];
1319 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001320 }
jvanverth17aa0472016-01-05 10:41:27 -08001321 } else {
1322 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001323 }
bsalomone699d0c2016-03-09 06:25:15 -08001324 }
1325
1326 if (!texelsShallowCopy.empty()) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001327 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001328 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001329 }
cblume55f2d2d2016-02-26 13:20:48 -08001330 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT,
1331 config_alignment(desc.fConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001332 }
cblume55f2d2d2016-02-26 13:20:48 -08001333
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001334 bool succeeded = true;
cblume55f2d2d2016-02-26 13:20:48 -08001335 if (kNewTexture_UploadType == uploadType &&
1336 0 == left && 0 == top &&
bsalomon7e68ab72016-04-13 14:29:25 -07001337 desc.fWidth == width && desc.fHeight == height) {
bsalomone699d0c2016-03-09 06:25:15 -08001338 succeeded = allocate_and_populate_uncompressed_texture(desc, *interface, caps, target,
1339 internalFormat, externalFormat,
1340 externalType, texelsShallowCopy,
1341 width, height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001342 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001343 if (swFlipY || glFlipY) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001344 top = desc.fHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001345 }
cblume55f2d2d2016-02-26 13:20:48 -08001346 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count();
1347 currentMipLevel++) {
1348 int twoToTheMipLevel = 1 << currentMipLevel;
1349 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1350 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001351
1352 GL_CALL(TexSubImage2D(target,
1353 currentMipLevel,
1354 left, top,
1355 currentWidth,
1356 currentHeight,
1357 externalFormat, externalType,
1358 texelsShallowCopy[currentMipLevel].fPixels));
1359 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001360 }
1361
cblume55f2d2d2016-02-26 13:20:48 -08001362 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1363
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001364 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001365}
1366
krajcevski145d48c2014-06-11 16:07:50 -07001367// TODO: This function is using a lot of wonky semantics like, if width == -1
piotaixre4b23142014-10-02 10:57:53 -07001368// then set width = desc.fWdith ... blah. A better way to do it might be to
krajcevski145d48c2014-06-11 16:07:50 -07001369// create a CompressedTexData struct that takes a desc/ptr and figures out
1370// the proper upload semantics. Then users can construct this function how they
1371// see fit if they want to go against the "standard" way to do it.
bsalomon861e1032014-12-16 07:33:49 -08001372bool GrGLGpu::uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -07001373 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -08001374 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -08001375 UploadType uploadType,
krajcevski145d48c2014-06-11 16:07:50 -07001376 int left, int top, int width, int height) {
bsalomon2555ee22015-12-17 18:39:46 -08001377 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
krajcevski9c0e6292014-06-02 07:38:14 -07001378
1379 // No support for software flip y, yet...
1380 SkASSERT(kBottomLeft_GrSurfaceOrigin != desc.fOrigin);
1381
cblume55f2d2d2016-02-26 13:20:48 -08001382 const GrGLInterface* interface = this->glInterface();
1383 const GrGLCaps& caps = this->glCaps();
1384
krajcevski145d48c2014-06-11 16:07:50 -07001385 if (-1 == width) {
1386 width = desc.fWidth;
1387 }
1388#ifdef SK_DEBUG
1389 else {
1390 SkASSERT(width <= desc.fWidth);
1391 }
1392#endif
1393
1394 if (-1 == height) {
1395 height = desc.fHeight;
1396 }
1397#ifdef SK_DEBUG
1398 else {
1399 SkASSERT(height <= desc.fHeight);
1400 }
1401#endif
1402
bsalomon76148af2016-01-12 11:13:47 -08001403 // We only need the internal format for compressed 2D textures.
1404 GrGLenum internalFormat;
cblume55f2d2d2016-02-26 13:20:48 -08001405 if (!caps.getCompressedTexImageFormats(desc.fConfig, &internalFormat)) {
bsalomon76148af2016-01-12 11:13:47 -08001406 return false;
1407 }
krajcevski9c0e6292014-06-02 07:38:14 -07001408
jvanverth17aa0472016-01-05 10:41:27 -08001409 if (kNewTexture_UploadType == uploadType) {
cblume790d5132016-02-29 11:13:29 -08001410 return allocate_and_populate_compressed_texture(desc, *interface, caps, target,
1411 internalFormat, texels, width, height);
krajcevski145d48c2014-06-11 16:07:50 -07001412 } else {
bsalomond4cb9222014-08-11 14:19:09 -07001413 // Paletted textures can't be updated.
1414 if (GR_GL_PALETTE8_RGBA8 == internalFormat) {
1415 return false;
1416 }
cblume55f2d2d2016-02-26 13:20:48 -08001417 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001418 SkASSERT(texels[currentMipLevel].fPixels || kTransfer_UploadType == uploadType);
cblume55f2d2d2016-02-26 13:20:48 -08001419
1420 int twoToTheMipLevel = 1 << currentMipLevel;
1421 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1422 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1423
1424 // Make sure that the width and height that we pass to OpenGL
1425 // is a multiple of the block size.
1426 size_t dataSize = GrCompressedFormatDataSize(desc.fConfig, currentWidth,
1427 currentHeight);
1428 GL_CALL(CompressedTexSubImage2D(target,
1429 currentMipLevel,
1430 left, top,
1431 currentWidth,
1432 currentHeight,
1433 internalFormat,
1434 SkToInt(dataSize),
1435 texels[currentMipLevel].fPixels));
1436 }
krajcevski145d48c2014-06-11 16:07:50 -07001437 }
krajcevski9c0e6292014-06-02 07:38:14 -07001438
bsalomond4cb9222014-08-11 14:19:09 -07001439 return true;
krajcevski9c0e6292014-06-02 07:38:14 -07001440}
1441
bsalomon424cc262015-05-22 10:37:30 -07001442static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001443 int sampleCount,
1444 GrGLenum format,
1445 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001446 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001447 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001448 switch (ctx.caps()->msFBOType()) {
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001449 case GrGLCaps::kDesktop_ARB_MSFBOType:
1450 case GrGLCaps::kDesktop_EXT_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001451 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001452 case GrGLCaps::kES_3_0_MSFBOType:
1453 GL_ALLOC_CALL(ctx.interface(),
1454 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1455 sampleCount,
1456 format,
1457 width, height));
1458 break;
1459 case GrGLCaps::kES_Apple_MSFBOType:
1460 GL_ALLOC_CALL(ctx.interface(),
1461 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1462 sampleCount,
1463 format,
1464 width, height));
1465 break;
1466 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1467 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1468 GL_ALLOC_CALL(ctx.interface(),
1469 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1470 sampleCount,
1471 format,
1472 width, height));
1473 break;
1474 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001475 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001476 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001477 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001478 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001479}
1480
egdanielb0e1be22015-04-22 13:27:39 -07001481bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001482 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001483 GrGLRenderTarget::IDDesc* idDesc) {
1484 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001485 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001486 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001487 idDesc->fTexFBOID = 0;
vbuzinovdded6962015-06-12 08:59:45 -07001488 idDesc->fSampleConfig = (GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType() &&
1489 desc.fSampleCnt > 0) ? GrRenderTarget::kStencil_SampleConfig :
1490 GrRenderTarget::kUnified_SampleConfig;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001491
1492 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001493
bsalomona11e5fc2015-12-18 07:59:41 -08001494 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001495
bsalomonb15b4c12014-10-29 12:41:57 -07001496 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001497 goto FAILED;
1498 }
1499
egdanield803f272015-03-18 13:01:52 -07001500 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1501 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502 goto FAILED;
1503 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001504
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001505 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1506 // the texture bound to the other. The exception is the IMG multisample extension. With this
1507 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1508 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001509 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001510 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001511 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001512 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001513 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 goto FAILED;
1515 }
bsalomon76148af2016-01-12 11:13:47 -08001516 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1517 return false;
1518 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001519 } else {
egdanield803f272015-03-18 13:01:52 -07001520 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001521 }
1522
egdanield803f272015-03-18 13:01:52 -07001523 // below here we may bind the FBO
1524 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1525 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001526 SkASSERT(desc.fSampleCnt > 0);
1527 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001528 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001529 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001530 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001531 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001532 goto FAILED;
1533 }
egdanield803f272015-03-18 13:01:52 -07001534 fStats.incRenderTargetBinds();
1535 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1536 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001537 GR_GL_COLOR_ATTACHMENT0,
1538 GR_GL_RENDERBUFFER,
1539 idDesc->fMSColorRenderbufferID));
bsalomonb15b4c12014-10-29 12:41:57 -07001540 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1541 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001542 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1543 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1544 goto FAILED;
1545 }
bsalomon424cc262015-05-22 10:37:30 -07001546 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001547 }
1548 }
egdanield803f272015-03-18 13:01:52 -07001549 fStats.incRenderTargetBinds();
1550 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001551
bsalomonb15b4c12014-10-29 12:41:57 -07001552 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001553 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001554 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001555 texInfo.fTarget,
1556 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001557 } else {
egdanield803f272015-03-18 13:01:52 -07001558 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001559 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001560 texInfo.fTarget,
1561 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001562 }
bsalomonb15b4c12014-10-29 12:41:57 -07001563 if ((desc.fFlags & kCheckAllocation_GrSurfaceFlag) ||
1564 !this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001565 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001566 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1567 goto FAILED;
1568 }
bsalomon424cc262015-05-22 10:37:30 -07001569 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001570 }
1571
1572 return true;
1573
1574FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001575 if (idDesc->fMSColorRenderbufferID) {
1576 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001577 }
egdanield803f272015-03-18 13:01:52 -07001578 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1579 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001580 }
egdanield803f272015-03-18 13:01:52 -07001581 if (idDesc->fTexFBOID) {
1582 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001583 }
1584 return false;
1585}
1586
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001587// good to set a break-point here to know when createTexture fails
1588static GrTexture* return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001589// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001590 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001591}
1592
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001593#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001594static size_t as_size_t(int x) {
1595 return x;
1596}
1597#endif
1598
kkinnunen2e6055b2016-04-22 01:48:29 -07001599static GrGLTexture::IDDesc generate_gl_texture(const GrGLInterface* interface) {
cblume55f2d2d2016-02-26 13:20:48 -08001600 GrGLTexture::IDDesc idDesc;
1601 idDesc.fInfo.fID = 0;
1602 GR_GL_CALL(interface, GenTextures(1, &idDesc.fInfo.fID));
kkinnunen2e6055b2016-04-22 01:48:29 -07001603 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
cblume55f2d2d2016-02-26 13:20:48 -08001604 // When we create the texture, we only
1605 // create GL_TEXTURE_2D at the moment.
1606 // External clients can do something different.
1607 idDesc.fInfo.fTarget = GR_GL_TEXTURE_2D;
1608 return idDesc;
1609}
1610
1611static void set_initial_texture_params(const GrGLInterface* interface,
1612 const GrGLTextureInfo& info,
1613 GrGLTexture::TexParams* initialTexParams) {
1614 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1615 // drivers have a bug where an FBO won't be complete if it includes a
1616 // texture that is not mipmap complete (considering the filter in use).
1617 // we only set a subset here so invalidate first
1618 initialTexParams->invalidate();
1619 initialTexParams->fMinFilter = GR_GL_NEAREST;
1620 initialTexParams->fMagFilter = GR_GL_NEAREST;
1621 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1622 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1623 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1624 GR_GL_TEXTURE_MAG_FILTER,
1625 initialTexParams->fMagFilter));
1626 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1627 GR_GL_TEXTURE_MIN_FILTER,
1628 initialTexParams->fMinFilter));
1629 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1630 GR_GL_TEXTURE_WRAP_S,
1631 initialTexParams->fWrapS));
1632 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1633 GR_GL_TEXTURE_WRAP_T,
1634 initialTexParams->fWrapT));
1635}
1636
egdanielb0e1be22015-04-22 13:27:39 -07001637GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001638 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001639 const SkTArray<GrMipLevel>& texels) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001640 // We fail if the MSAA was requested and is not available.
1641 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001642 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001643 return return_null_texture();
1644 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001645
bsalomonf2703d82014-10-28 14:33:06 -07001646 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001647
bsalomonb15b4c12014-10-29 12:41:57 -07001648 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001649 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001650 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001651 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderTarget, &initialTexParams, texels)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001652 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001653 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001654
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001655 GrGLTexture* tex;
reed@google.comac10a2d2010-12-22 21:39:39 +00001656 if (renderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001657 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001658 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001659 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001660
kkinnunen2e6055b2016-04-22 01:48:29 -07001661 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001662 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 return return_null_texture();
1664 }
kkinnunen2e6055b2016-04-22 01:48:29 -07001665 tex = new GrGLTextureRenderTarget(this, budgeted, desc, idDesc, rtIDDesc);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001666 } else {
cblume55f2d2d2016-02-26 13:20:48 -08001667 bool wasMipMapDataProvided = false;
1668 if (texels.count() > 1) {
1669 wasMipMapDataProvided = true;
1670 }
kkinnunen2e6055b2016-04-22 01:48:29 -07001671 tex = new GrGLTexture(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001673 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001674#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001675 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001676 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001677#endif
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001678 return tex;
1679}
1680
egdanielb0e1be22015-04-22 13:27:39 -07001681GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -07001682 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -08001683 const SkTArray<GrMipLevel>& texels) {
krajcevski9c0e6292014-06-02 07:38:14 -07001684 // Make sure that we're not flipping Y.
egdanielb0e1be22015-04-22 13:27:39 -07001685 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
krajcevski9c0e6292014-06-02 07:38:14 -07001686 return return_null_texture();
1687 }
1688
kkinnunen2e6055b2016-04-22 01:48:29 -07001689 GrGLTexture::IDDesc idDesc = generate_gl_texture(this->glInterface());
bsalomon091f60c2015-11-10 11:54:56 -08001690 if (!idDesc.fInfo.fID) {
krajcevski9c0e6292014-06-02 07:38:14 -07001691 return return_null_texture();
1692 }
1693
1694 this->setScratchTextureUnit();
bsalomon091f60c2015-11-10 11:54:56 -08001695 GL_CALL(BindTexture(idDesc.fInfo.fTarget, idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001696
krajcevski9c0e6292014-06-02 07:38:14 -07001697 GrGLTexture::TexParams initialTexParams;
cblume55f2d2d2016-02-26 13:20:48 -08001698 set_initial_texture_params(this->glInterface(), idDesc.fInfo, &initialTexParams);
krajcevski9c0e6292014-06-02 07:38:14 -07001699
cblume55f2d2d2016-02-26 13:20:48 -08001700 if (!this->uploadCompressedTexData(desc, idDesc.fInfo.fTarget, texels)) {
bsalomon091f60c2015-11-10 11:54:56 -08001701 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
krajcevski9c0e6292014-06-02 07:38:14 -07001702 return return_null_texture();
1703 }
1704
1705 GrGLTexture* tex;
kkinnunen2e6055b2016-04-22 01:48:29 -07001706 tex = new GrGLTexture(this, budgeted, desc, idDesc);
krajcevski9c0e6292014-06-02 07:38:14 -07001707 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
1708#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001709 SkDebugf("--- new compressed texture [%d] size=(%d %d) config=%d\n",
cblume55f2d2d2016-02-26 13:20:48 -08001710 glTexDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
krajcevski9c0e6292014-06-02 07:38:14 -07001711#endif
1712 return tex;
1713}
1714
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001715namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001716
egdaniel8dc7c3a2015-04-16 11:22:42 -07001717const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001718
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001719void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001720 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001721
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001722 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001723 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001724 (kUnknownBitCount == format->fTotalBits));
1725 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001726 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001727 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1728 (GrGLint*)&format->fStencilBits);
1729 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001730 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001731 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1732 (GrGLint*)&format->fTotalBits);
1733 format->fTotalBits += format->fStencilBits;
1734 } else {
1735 format->fTotalBits = format->fStencilBits;
1736 }
1737 }
1738}
1739}
1740
egdanielff1d5472015-09-10 08:37:20 -07001741int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001742 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001743 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001744 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1745 // Default to unsupported, set this if we find a stencil format that works.
1746 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001747 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001748 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001749 GL_CALL(GenTextures(1, &colorID));
1750 this->setScratchTextureUnit();
1751 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1752 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1753 GR_GL_TEXTURE_MAG_FILTER,
1754 GR_GL_NEAREST));
1755 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1756 GR_GL_TEXTURE_MIN_FILTER,
1757 GR_GL_NEAREST));
1758 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1759 GR_GL_TEXTURE_WRAP_S,
1760 GR_GL_CLAMP_TO_EDGE));
1761 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1762 GR_GL_TEXTURE_WRAP_T,
1763 GR_GL_CLAMP_TO_EDGE));
1764
bsalomon76148af2016-01-12 11:13:47 -08001765 GrGLenum internalFormat;
1766 GrGLenum externalFormat;
1767 GrGLenum externalType;
1768 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1769 &externalType)) {
1770 return false;
1771 }
egdanielff1d5472015-09-10 08:37:20 -07001772 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1773 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001774 0,
bsalomon76148af2016-01-12 11:13:47 -08001775 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001776 kSize,
1777 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001778 0,
bsalomon76148af2016-01-12 11:13:47 -08001779 externalFormat,
1780 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001781 NULL));
bsalomon30447372015-12-21 09:03:05 -08001782 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001783 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001784 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001785 }
1786
1787 // unbind the texture from the texture unit before binding it to the frame buffer
1788 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1789
1790 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001791 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001792 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001793 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
egdanielff1d5472015-09-10 08:37:20 -07001794 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
1795 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1796 GR_GL_COLOR_ATTACHMENT0,
1797 GR_GL_TEXTURE_2D,
1798 colorID,
1799 0));
bsalomon30447372015-12-21 09:03:05 -08001800 GrGLuint sbRBID = 0;
1801 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001802
1803 // look over formats till I find a compatible one
1804 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001805 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001806 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001807 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1808 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1809 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1810 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1811 sFmt.fInternalFormat,
1812 kSize, kSize));
1813 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001814 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001815 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001816 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001817 if (sFmt.fPacked) {
1818 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1819 GR_GL_DEPTH_ATTACHMENT,
1820 GR_GL_RENDERBUFFER, sbRBID));
1821 } else {
1822 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1823 GR_GL_DEPTH_ATTACHMENT,
1824 GR_GL_RENDERBUFFER, 0));
1825 }
1826 GrGLenum status;
1827 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1828 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1829 firstWorkingStencilFormatIndex = i;
1830 break;
1831 }
egdanielff1d5472015-09-10 08:37:20 -07001832 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1833 GR_GL_STENCIL_ATTACHMENT,
1834 GR_GL_RENDERBUFFER, 0));
1835 if (sFmt.fPacked) {
1836 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1837 GR_GL_DEPTH_ATTACHMENT,
1838 GR_GL_RENDERBUFFER, 0));
1839 }
egdanielff1d5472015-09-10 08:37:20 -07001840 }
1841 }
bsalomon30447372015-12-21 09:03:05 -08001842 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001843 }
1844 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001845 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1846 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001847 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001848 }
bsalomon30447372015-12-21 09:03:05 -08001849 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001850}
1851
erikchen9a1ed5d2016-02-10 16:32:34 -08001852bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001853 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
1854 const SkTArray<GrMipLevel>& texels) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001855 info->fID = 0;
1856 info->fTarget = GR_GL_TEXTURE_2D;
1857 GL_CALL(GenTextures(1, &(info->fID)));
1858
1859 if (!info->fID) {
1860 return false;
1861 }
1862
1863 this->setScratchTextureUnit();
1864 GL_CALL(BindTexture(info->fTarget, info->fID));
1865
1866 if (renderTarget && this->glCaps().textureUsageSupport()) {
1867 // provides a hint about how this texture will be used
1868 GL_CALL(TexParameteri(info->fTarget,
1869 GR_GL_TEXTURE_USAGE,
1870 GR_GL_FRAMEBUFFER_ATTACHMENT));
1871 }
1872
cblume55f2d2d2016-02-26 13:20:48 -08001873 if (info) {
1874 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1875 }
erikchen9a1ed5d2016-02-10 16:32:34 -08001876 if (!this->uploadTexData(desc, info->fTarget, kNewTexture_UploadType, 0, 0,
1877 desc.fWidth, desc.fHeight,
cblume55f2d2d2016-02-26 13:20:48 -08001878 desc.fConfig, texels)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001879 GL_CALL(DeleteTextures(1, &(info->fID)));
1880 return false;
1881 }
1882 return true;
1883}
1884
egdanielec00d942015-09-14 12:56:10 -07001885GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1886 int width,
1887 int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001888 SkASSERT(width >= rt->width());
1889 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001890
vbuzinovdded6962015-06-12 08:59:45 -07001891 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001892 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001893
egdanielff1d5472015-09-10 08:37:20 -07001894 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001895 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001896 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001897 }
egdanielff1d5472015-09-10 08:37:20 -07001898
1899 if (!sbDesc.fRenderbufferID) {
1900 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1901 }
1902 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001903 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001904 }
1905 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1906 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1907 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1908 // we do this "if" so that we don't call the multisample
1909 // version on a GL that doesn't have an MSAA extension.
1910 if (samples > 0) {
1911 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1912 samples,
1913 sFmt.fInternalFormat,
1914 width, height));
1915 } else {
1916 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1917 sFmt.fInternalFormat,
1918 width, height));
1919 SkASSERT(GR_GL_NO_ERROR == check_alloc_error(rt->desc(), this->glInterface()));
1920 }
1921 fStats.incStencilAttachmentCreates();
1922 // After sized formats we attempt an unsized format and take
1923 // whatever sizes GL gives us. In that case we query for the size.
1924 GrGLStencilAttachment::Format format = sFmt;
1925 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001926 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1927 sbDesc,
1928 width,
1929 height,
1930 samples,
1931 format);
1932 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001933}
1934
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001935////////////////////////////////////////////////////////////////////////////////
1936
jvanverth73063dc2015-12-03 09:15:47 -08001937// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1938// objects are implemented as client-side-arrays on tile-deferred architectures.
1939#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1940
cdaltone2e71c22016-04-07 18:13:29 -07001941GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001942 GrAccessPattern accessPattern, const void* data) {
1943 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001944}
1945
bsalomon3e791242014-12-17 13:43:13 -08001946void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001947 const GrGLIRect& rtViewport,
1948 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001949 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001950 GrGLIRect scissor;
bsalomonb0bd4f62014-09-03 07:19:50 -07001951 scissor.setRelativeTo(rtViewport,
robertphillipse85a32d2015-02-10 08:16:55 -08001952 scissorState.rect().fLeft,
1953 scissorState.rect().fTop,
1954 scissorState.rect().width(),
1955 scissorState.rect().height(),
bsalomonb0bd4f62014-09-03 07:19:50 -07001956 rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001957 // if the scissor fully contains the viewport then we fall through and
1958 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001959 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001960 if (fHWScissorSettings.fRect != scissor) {
1961 scissor.pushToGLScissor(this->glInterface());
1962 fHWScissorSettings.fRect = scissor;
1963 }
1964 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1965 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1966 fHWScissorSettings.fEnabled = kYes_TriState;
1967 }
1968 return;
1969 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001970 }
joshualitt77b13072014-10-27 14:51:01 -07001971
1972 // See fall through note above
1973 this->disableScissor();
1974}
1975
ethannicholas28ef4452016-03-25 09:26:03 -07001976void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1977 if (fHWMinSampleShading != minSampleShading) {
1978 if (minSampleShading > 0.0) {
1979 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1980 GL_CALL(MinSampleShading(minSampleShading));
1981 }
1982 else {
1983 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1984 }
1985 fHWMinSampleShading = minSampleShading;
1986 }
1987}
1988
egdaniel0e1853c2016-03-17 11:35:45 -07001989bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc) {
egdaniel080e6732014-12-22 07:35:52 -08001990 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001991 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001992
egdaniel080e6732014-12-22 07:35:52 -08001993 this->flushColorWrite(blendInfo.fWriteColor);
egdaniel8dd688b2015-01-22 10:16:09 -08001994 this->flushDrawFace(pipeline.getDrawFace());
ethannicholas28ef4452016-03-25 09:26:03 -07001995 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001996
egdaniel0e1853c2016-03-17 11:35:45 -07001997 SkAutoTUnref<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc));
bsalomon6df86402015-06-01 10:41:49 -07001998 if (!program) {
bsalomon682c2692015-05-22 14:01:46 -07001999 GrCapsDebugf(this->caps(), "Failed to create program!\n");
bsalomon1f78c0a2014-12-17 09:43:13 -08002000 return false;
bsalomonbc3d0de2014-12-15 13:45:03 -08002001 }
2002
bsalomon6df86402015-06-01 10:41:49 -07002003 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08002004 if (fHWProgramID != programID) {
2005 GL_CALL(UseProgram(programID));
2006 fHWProgramID = programID;
2007 }
2008
egdanield803f272015-03-18 13:01:52 -07002009 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08002010 // Swizzle the blend to match what the shader will output.
2011 const GrSwizzle& swizzle = this->glCaps().glslCaps()->configOutputSwizzle(
egdaniel0e1853c2016-03-17 11:35:45 -07002012 pipeline.getRenderTarget()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08002013 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07002014 }
bsalomon1f78c0a2014-12-17 09:43:13 -08002015
cdalton74b8d322016-04-11 14:47:28 -07002016 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08002017
brianosman898235c2016-04-06 07:38:23 -07002018 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.getRenderTarget());
egdaniel8dd688b2015-01-22 10:16:09 -08002019 this->flushStencil(pipeline.getStencil());
2020 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), glRT->origin());
cdaltonaf8bc7d2016-02-05 09:35:20 -08002021 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !pipeline.getStencil().isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08002022
2023 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002024 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07002025 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08002026
2027 return true;
2028}
2029
joshualitt873ad0e2015-01-20 09:08:51 -08002030void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
egdaniel0e1853c2016-03-17 11:35:45 -07002031 const GrNonInstancedMesh& mesh,
bsalomonbc3d0de2014-12-15 13:45:03 -08002032 size_t* indexOffsetInBytes) {
cdalton397536c2016-03-25 12:15:03 -07002033 const GrGLBuffer* vbuf;
2034 vbuf = static_cast<const GrGLBuffer*>(mesh.vertexBuffer());
bsalomonbc3d0de2014-12-15 13:45:03 -08002035
2036 SkASSERT(vbuf);
2037 SkASSERT(!vbuf->isMapped());
2038
cdaltone2e71c22016-04-07 18:13:29 -07002039 GrGLAttribArrayState* attribState;
egdaniel0e1853c2016-03-17 11:35:45 -07002040 if (mesh.isIndexed()) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002041 SkASSERT(indexOffsetInBytes);
2042
2043 *indexOffsetInBytes = 0;
cdaltone2e71c22016-04-07 18:13:29 -07002044 const GrGLBuffer* ibuf = static_cast<const GrGLBuffer*>(mesh.indexBuffer());
bsalomonbc3d0de2014-12-15 13:45:03 -08002045
2046 SkASSERT(ibuf);
2047 SkASSERT(!ibuf->isMapped());
2048 *indexOffsetInBytes += ibuf->baseOffset();
cdaltone2e71c22016-04-07 18:13:29 -07002049 attribState = fHWVertexArrayState.bindInternalVertexArray(this, ibuf);
2050 } else {
2051 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002052 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002053
joshualitt873ad0e2015-01-20 09:08:51 -08002054 int vaCount = primProc.numAttribs();
joshualitt71c92602015-01-14 08:12:47 -08002055 if (vaCount > 0) {
bsalomonbc3d0de2014-12-15 13:45:03 -08002056
joshualitt873ad0e2015-01-20 09:08:51 -08002057 GrGLsizei stride = static_cast<GrGLsizei>(primProc.getVertexStride());
bsalomonbc3d0de2014-12-15 13:45:03 -08002058
egdaniel0e1853c2016-03-17 11:35:45 -07002059 size_t vertexOffsetInBytes = stride * mesh.startVertex();
bsalomonbc3d0de2014-12-15 13:45:03 -08002060
2061 vertexOffsetInBytes += vbuf->baseOffset();
2062
bsalomonbc3d0de2014-12-15 13:45:03 -08002063 uint32_t usedAttribArraysMask = 0;
2064 size_t offset = 0;
2065
2066 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
joshualitt873ad0e2015-01-20 09:08:51 -08002067 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(attribIndex);
bsalomonbc3d0de2014-12-15 13:45:03 -08002068 usedAttribArraysMask |= (1 << attribIndex);
joshualitt71c92602015-01-14 08:12:47 -08002069 GrVertexAttribType attribType = attrib.fType;
bsalomonbc3d0de2014-12-15 13:45:03 -08002070 attribState->set(this,
2071 attribIndex,
cdaltone2e71c22016-04-07 18:13:29 -07002072 vbuf,
cdalton793dc262016-02-08 10:11:47 -08002073 attribType,
bsalomonbc3d0de2014-12-15 13:45:03 -08002074 stride,
2075 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
joshualitt71c92602015-01-14 08:12:47 -08002076 offset += attrib.fOffset;
bsalomonbc3d0de2014-12-15 13:45:03 -08002077 }
2078 attribState->disableUnusedArrays(this, usedAttribArraysMask);
2079 }
2080}
2081
cdaltone2e71c22016-04-07 18:13:29 -07002082GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrGLBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002083 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002084
cdaltone2e71c22016-04-07 18:13:29 -07002085 // Index buffer state is tied to the vertex array.
2086 if (kIndex_GrBufferType == type) {
2087 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002088 }
cdaltone2e71c22016-04-07 18:13:29 -07002089
2090 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
2091 auto& bufferState = fHWBufferState[type];
2092
2093 if (buffer->getUniqueID() != bufferState.fBoundBufferUniqueID) {
2094 if (!buffer->isCPUBacked() || !bufferState.fBufferZeroKnownBound) {
2095 GL_CALL(BindBuffer(bufferState.fGLTarget, buffer->bufferID()));
2096 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
2097 }
2098 bufferState.fBoundBufferUniqueID = buffer->getUniqueID();
2099 }
2100
2101 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002102}
2103
cdalton74b8d322016-04-11 14:47:28 -07002104void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
2105 if (buffer->hasAttachedToTexture()) {
2106 // Detach this buffer from any textures to ensure the underlying memory is freed.
2107 uint32_t uniqueID = buffer->getUniqueID();
2108 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
2109 auto& buffTex = fHWBufferTextures[i];
2110 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
2111 continue;
2112 }
2113 if (i == fHWMaxUsedBufferTextureUnit) {
2114 --fHWMaxUsedBufferTextureUnit;
2115 }
2116
2117 this->setTextureUnit(i);
2118 if (!buffTex.fKnownBound) {
2119 SkASSERT(buffTex.fTextureID);
2120 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
2121 buffTex.fKnownBound = true;
2122 }
2123 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
2124 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
2125 }
2126 }
2127}
2128
bsalomon861e1032014-12-16 07:33:49 -08002129void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00002130 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002131 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002132 fHWScissorSettings.fEnabled = kNo_TriState;
2133 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00002134 }
2135}
2136
egdaniel51c8d402015-08-06 10:54:13 -07002137void GrGLGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002138 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002139 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07002140 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002141
egdaniel51c8d402015-08-06 10:54:13 -07002142 this->flushRenderTarget(glRT, &rect);
bsalomon3e791242014-12-17 13:43:13 -08002143 GrScissorState scissorState;
egdaniel51c8d402015-08-06 10:54:13 -07002144 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002145 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.com74b98712011-11-11 19:46:16 +00002146
2147 GrGLfloat r, g, b, a;
2148 static const GrGLfloat scale255 = 1.f / 255.f;
2149 a = GrColorUnpackA(color) * scale255;
2150 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002151 r = GrColorUnpackR(color) * scaleRGB;
2152 g = GrColorUnpackG(color) * scaleRGB;
2153 b = GrColorUnpackB(color) * scaleRGB;
2154
2155 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002156 fHWWriteToColor = kYes_TriState;
bsalomon@google.com74b98712011-11-11 19:46:16 +00002157 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002159}
2160
bsalomon861e1032014-12-16 07:33:49 -08002161void GrGLGpu::discard(GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -08002162 SkASSERT(renderTarget);
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002163 if (!this->caps()->discardRenderTargetSupport()) {
2164 return;
2165 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002166
2167 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002168 if (renderTarget->getUniqueID() != fHWBoundRenderTargetUniqueID) {
2169 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
2170 fStats.incRenderTargetBinds();
2171 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, glRT->renderFBOID()));
2172 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002173 switch (this->glCaps().invalidateFBType()) {
joshualitt58162332014-08-01 06:44:53 -07002174 case GrGLCaps::kNone_InvalidateFBType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00002175 SkFAIL("Should never get here.");
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002176 break;
2177 case GrGLCaps::kInvalidate_InvalidateFBType:
egdanield803f272015-03-18 13:01:52 -07002178 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002179 // When rendering to the default framebuffer the legal values for attachments
2180 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2181 // types.
2182 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002183 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002184 attachments));
2185 } else {
2186 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002187 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002188 attachments));
2189 }
2190 break;
2191 case GrGLCaps::kDiscard_InvalidateFBType: {
egdanield803f272015-03-18 13:01:52 -07002192 if (0 == glRT->renderFBOID()) {
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002193 // When rendering to the default framebuffer the legal values for attachments
2194 // are GL_COLOR, GL_DEPTH, GL_STENCIL, ... rather than the various FBO attachment
2195 // types. See glDiscardFramebuffer() spec.
2196 static const GrGLenum attachments[] = { GR_GL_COLOR };
egdanield803f272015-03-18 13:01:52 -07002197 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002198 attachments));
2199 } else {
2200 static const GrGLenum attachments[] = { GR_GL_COLOR_ATTACHMENT0 };
egdanield803f272015-03-18 13:01:52 -07002201 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, SK_ARRAY_COUNT(attachments),
commit-bot@chromium.org4453e8b2014-04-16 14:33:27 +00002202 attachments));
2203 }
commit-bot@chromium.org52ffbf62014-04-02 16:19:33 +00002204 break;
2205 }
2206 }
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00002207 renderTarget->flagAsResolved();
2208}
2209
bsalomon861e1032014-12-16 07:33:49 -08002210void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002211 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002212 return;
2213 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002214 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002215 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002216
joshualitt77b13072014-10-27 14:51:01 -07002217 this->disableScissor();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002218
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002219 GL_CALL(StencilMask(0xffffffff));
2220 GL_CALL(ClearStencil(0));
2221 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002222 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002223}
2224
bsalomon861e1032014-12-16 07:33:49 -08002225void GrGLGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) {
bsalomon49f085d2014-09-05 13:34:00 -07002226 SkASSERT(target);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002227
egdaniel8dc7c3a2015-04-16 11:22:42 -07002228 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002229 // this should only be called internally when we know we have a
2230 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002231 SkASSERT(sb);
2232 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002233#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002234 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002235 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002236#else
2237 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002238 // ANGLE a partial stencil mask will cause clears to be
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002239 // turned into draws. Our contract on GrDrawTarget says that
2240 // changing the clip between stencil passes may or may not
2241 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002242 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002243#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002244 GrGLint value;
2245 if (insideClip) {
2246 value = (1 << (stencilBitCount - 1));
2247 } else {
2248 value = 0;
2249 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002250 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002251 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002252
bsalomon3e791242014-12-17 13:43:13 -08002253 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002254 scissorState.set(rect);
joshualitt77b13072014-10-27 14:51:01 -07002255 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002256
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002257 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002258 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002259 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002260 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002261}
2262
bsalomon39826022015-07-23 08:07:21 -07002263static bool read_pixels_pays_for_y_flip(GrRenderTarget* renderTarget, const GrGLCaps& caps,
2264 int width, int height, GrPixelConfig config,
2265 size_t rowBytes) {
2266 // If this render target is already TopLeft, we don't need to flip.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002267 if (kTopLeft_GrSurfaceOrigin == renderTarget->origin()) {
2268 return false;
2269 }
2270
bsalomon494aa592015-07-23 11:45:02 -07002271 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002272 static const int kMinSize = 32;
2273 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002274 return false;
2275 }
2276
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002277 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002278 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002279 return false;
2280 }
2281
2282 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002283 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002284 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2285 // clipped.
2286 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2287}
2288
bsalomon1aa20292016-01-22 08:16:09 -08002289bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
2290 auto bindRenderTarget = [this, target]() -> bool {
2291 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2292 return true;
2293 };
2294 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2295 GR_GL_GetIntegerv(this->glInterface(), query, value);
2296 };
2297 GrPixelConfig rtConfig = target->config();
2298 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2299}
2300
2301bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
2302 auto bindRenderTarget = [this, rtConfig]() -> bool {
2303 GrTextureDesc desc;
2304 desc.fConfig = rtConfig;
2305 desc.fWidth = desc.fHeight = 16;
2306 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomone699d0c2016-03-09 06:25:15 -08002307 SkAutoTUnref<GrTexture> temp(this->createTexture(desc,
2308 SkBudgeted::kNo));
bsalomon1aa20292016-01-22 08:16:09 -08002309 if (!temp) {
2310 return false;
2311 }
2312 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2313 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2314 return true;
2315 };
2316 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2317 GR_GL_GetIntegerv(this->glInterface(), query, value);
2318 };
2319 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget);
2320}
2321
2322bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2323 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2324 return this->readPixelsSupported(rt, readConfig);
2325 } else {
2326 GrPixelConfig config = surfaceForConfig->config();
2327 return this->readPixelsSupported(config, readConfig);
2328 }
2329}
2330
bsalomone9573312016-01-25 14:33:25 -08002331static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2332 if (GrPixelConfigIsSRGB(a)) {
2333 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2334 } else if (GrPixelConfigIsSRGB(b)) {
2335 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2336 }
2337 return false;
2338}
2339
bsalomonf0674512015-07-28 13:26:15 -07002340bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
2341 GrPixelConfig readConfig, DrawPreference* drawPreference,
2342 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002343 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002344
bsalomone9573312016-01-25 14:33:25 -08002345 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002346 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2347 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2348 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2349 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2350 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomon100b8f82015-10-28 08:37:44 -07002351 tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
bsalomon39826022015-07-23 08:07:21 -07002352
bsalomone9573312016-01-25 14:33:25 -08002353 // For now assume no swizzling, we may change that below.
2354 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2355
2356 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2357 // from will be srcConfig and we will read readConfig pixels from it.
2358 // Not that if we require a draw and return a non-renderable format for the temp surface the
2359 // base class will fail for us.
2360 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2361 tempDrawInfo->fReadConfig = readConfig;
2362
2363 if (requires_srgb_conversion(srcConfig, readConfig)) {
2364 if (!this->readPixelsSupported(readConfig, readConfig)) {
2365 return false;
2366 }
2367 // Draw to do srgb to linear conversion or vice versa.
2368 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2369 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2370 tempDrawInfo->fReadConfig = readConfig;
2371 return true;
2372 }
2373
2374 GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
2375 if (!srcAsRT) {
2376 // For now keep assuming the draw is not a format transformation, just a draw to get to a
2377 // RT. We may add additional transformations below.
2378 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2379 }
bsalomon1aa20292016-01-22 08:16:09 -08002380 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2381 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002382 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002383 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2384 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002385 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002386 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002387 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002388 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2389 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002390 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002391 // Better to do a draw with a R/B swap and then read as the original config.
2392 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002393 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2394 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002395 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002396 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2397 if (readConfig == kBGRA_8888_GrPixelConfig &&
2398 this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
2399 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002400 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2401 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2402 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002403 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2404 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2405 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2406 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002407 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
2408 this->glCaps().isConfigRenderable(kSRGBA_8888_GrPixelConfig, false) &&
2409 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2410 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2411 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2412 // will effectively be sBGRA).
2413 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2414 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2415 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2416 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002417 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2418 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2419 // it's unsupported, but 32bit RGBA reads are supported.
2420 // Don't attempt to do any srgb conversions since we only care about alpha.
2421 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2422 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2423 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2424 }
2425 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2426 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2427 // first and then onReadPixels will read that to a 32bit temporary buffer.
2428 if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
2429 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2430 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2431 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2432 } else {
2433 return false;
2434 }
2435 } else {
2436 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2437 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2438 }
bsalomone6d665e2016-03-10 07:22:25 -08002439 } else if (this->caps()->isConfigRenderable(readConfig, false) &&
2440 this->readPixelsSupported(readConfig, readConfig)) {
2441 // Do a draw to convert from the src config to the read config.
2442 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2443 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2444 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002445 } else {
2446 return false;
2447 }
bsalomon39826022015-07-23 08:07:21 -07002448 }
2449
bsalomon1aa20292016-01-22 08:16:09 -08002450 if (srcAsRT &&
2451 read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig, rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002452 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002453 }
2454
bsalomon39826022015-07-23 08:07:21 -07002455 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002456}
2457
bsalomon6cb3cbe2015-07-30 07:34:27 -07002458bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002459 int left, int top,
2460 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002461 GrPixelConfig config,
2462 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002463 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002464 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002465
bsalomone9573312016-01-25 14:33:25 -08002466 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
2467 if (!renderTarget) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002468 return false;
2469 }
2470
bsalomon16921ec2015-07-30 15:34:56 -07002471 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002472 if (requires_srgb_conversion(surface->config(), config)) {
2473 return false;
2474 }
2475
2476 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2477 // bit channels as RGBA and then extract A.
2478 if (!this->readPixelsSupported(renderTarget, config)) {
2479 // Don't attempt to do any srgb conversions since we only care about alpha.
2480 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
2481 if (GrPixelConfigIsSRGB(renderTarget->config())) {
2482 tempConfig = kSRGBA_8888_GrPixelConfig;
2483 }
2484 if (kAlpha_8_GrPixelConfig == config &&
2485 this->readPixelsSupported(renderTarget, tempConfig)) {
2486 SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
2487 if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, temp.get(),
2488 width*4)) {
2489 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2490 for (int j = 0; j < height; ++j) {
2491 for (int i = 0; i < width; ++i) {
2492 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2493 }
2494 }
2495 return true;
2496 }
2497 }
bsalomon16921ec2015-07-30 15:34:56 -07002498 return false;
2499 }
2500
bsalomon76148af2016-01-12 11:13:47 -08002501 GrGLenum externalFormat;
2502 GrGLenum externalType;
bsalomone9573312016-01-25 14:33:25 -08002503 if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002504 &externalType)) {
2505 return false;
2506 }
bsalomon6cb3cbe2015-07-30 07:34:27 -07002507 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002508
bsalomon@google.comc6980972011-11-02 19:57:21 +00002509 // resolve the render target if necessary
bsalomone9573312016-01-25 14:33:25 -08002510 switch (renderTarget->getResolveType()) {
egdanield803f272015-03-18 13:01:52 -07002511 case GrGLRenderTarget::kCantResolve_ResolveType:
2512 return false;
2513 case GrGLRenderTarget::kAutoResolves_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002514 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
egdanield803f272015-03-18 13:01:52 -07002515 break;
2516 case GrGLRenderTarget::kCanResolve_ResolveType:
bsalomone9573312016-01-25 14:33:25 -08002517 this->onResolveRenderTarget(renderTarget);
egdanield803f272015-03-18 13:01:52 -07002518 // we don't track the state of the READ FBO ID.
2519 fStats.incRenderTargetBinds();
bsalomone9573312016-01-25 14:33:25 -08002520 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
egdanield803f272015-03-18 13:01:52 -07002521 break;
2522 default:
2523 SkFAIL("Unknown resolve type");
reed@google.comac10a2d2010-12-22 21:39:39 +00002524 }
2525
bsalomone9573312016-01-25 14:33:25 -08002526 const GrGLIRect& glvp = renderTarget->getViewport();
bsalomon@google.comd302f142011-03-03 13:54:13 +00002527
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002528 // the read rect is viewport-relative
2529 GrGLIRect readRect;
bsalomone9573312016-01-25 14:33:25 -08002530 readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002531
bsalomon9d02b262016-02-01 12:49:30 -08002532 size_t bytesPerPixel = GrBytesPerPixel(config);
2533 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002534
bsalomon@google.comc6980972011-11-02 19:57:21 +00002535 size_t readDstRowBytes = tightRowBytes;
2536 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002537
bsalomon@google.comc6980972011-11-02 19:57:21 +00002538 // determine if GL can read using the passed rowBytes or if we need
2539 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002540 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002541 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002542 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002543 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002544 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002545 readDstRowBytes = rowBytes;
2546 } else {
2547 scratch.reset(tightRowBytes * height);
2548 readDst = scratch.get();
2549 }
2550 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002551 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002552 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2553 }
bsalomonf46a1242015-12-15 12:37:38 -08002554 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2555
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002556 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2557 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002558 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002559 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002560 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002561 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2562 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002563 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002564 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002565 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002566 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002567
2568 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002569 // API presents top-to-bottom. We must preserve the padding contents. Note
2570 // that the above readPixels did not overwrite the padding.
2571 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002572 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002573 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002574 scratch.reset(tightRowBytes);
2575 void* tmpRow = scratch.get();
2576 // flip y in-place by rows
2577 const int halfY = height >> 1;
2578 char* top = reinterpret_cast<char*>(buffer);
2579 char* bottom = top + (height - 1) * rowBytes;
2580 for (int y = 0; y < halfY; y++) {
2581 memcpy(tmpRow, top, tightRowBytes);
2582 memcpy(top, bottom, tightRowBytes);
2583 memcpy(bottom, tmpRow, tightRowBytes);
2584 top += rowBytes;
2585 bottom -= rowBytes;
2586 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002587 }
2588 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002589 SkASSERT(readDst != buffer);
2590 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002591 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002592 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002593 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002594 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002595 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002596 dst += (height-1) * rowBytes;
2597 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002598 for (int y = 0; y < height; y++) {
2599 memcpy(dst, src, tightRowBytes);
2600 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002601 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002602 dst += rowBytes;
2603 } else {
2604 dst -= rowBytes;
2605 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002606 }
2607 }
2608 return true;
2609}
2610
jvanverthd2d2eb92016-02-17 14:04:46 -08002611void GrGLGpu::finishDrawTarget() {
ethannicholas22793252016-01-30 09:59:10 -08002612 if (fPLSHasBeenUsed) {
halcanary9d524f22016-03-29 09:03:52 -07002613 /* There is an ARM driver bug where if we use PLS, and then draw a frame which does not
2614 * use PLS, it leaves garbage all over the place. As a workaround, we use PLS in a
ethannicholas22793252016-01-30 09:59:10 -08002615 * trivial way every frame. And since we use it every frame, there's never a point at which
2616 * it becomes safe to stop using this workaround once we start.
2617 */
2618 this->disableScissor();
2619 // using PLS in the presence of MSAA results in GL_INVALID_OPERATION
cdaltonaf8bc7d2016-02-05 09:35:20 -08002620 this->flushHWAAState(nullptr, false, false);
ethannicholas22793252016-01-30 09:59:10 -08002621 SkASSERT(!fHWPLSEnabled);
2622 SkASSERT(fMSAAEnabled != kYes_TriState);
2623 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
cdaltone2e71c22016-04-07 18:13:29 -07002624 this->stampPLSSetupRect(SkRect::MakeXYWH(-100.0f, -100.0f, 0.01f, 0.01f));
ethannicholas22793252016-01-30 09:59:10 -08002625 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2626 }
2627}
ethannicholas5366a092016-01-22 09:45:47 -08002628
brianosman64d094d2016-03-25 06:01:59 -07002629void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002630 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002631
egdanield803f272015-03-18 13:01:52 -07002632 uint32_t rtID = target->getUniqueID();
2633 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002634 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002635 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2636#ifdef SK_DEBUG
2637 // don't do this check in Chromium -- this is causing
2638 // lots of repeated command buffer flushes when the compositor is
2639 // rendering with Ganesh, which is really slow; even too slow for
2640 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002641 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002642 GrGLenum status;
2643 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2644 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2645 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2646 }
bsalomon160f24c2015-03-17 15:55:42 -07002647 }
egdanield803f272015-03-18 13:01:52 -07002648#endif
2649 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002650 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002651 }
2652
2653 if (this->glCaps().srgbSupport()) {
2654 bool enableSRGBWrite = GrPixelConfigIsSRGB(target->config()) && !disableSRGB;
2655 if (enableSRGBWrite && kYes_TriState != fHWSRGBFramebuffer) {
2656 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2657 fHWSRGBFramebuffer = kYes_TriState;
2658 } else if (!enableSRGBWrite && kNo_TriState != fHWSRGBFramebuffer) {
2659 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2660 fHWSRGBFramebuffer = kNo_TriState;
bsalomon16921ec2015-07-30 15:34:56 -07002661 }
bsalomon5cd020f2015-03-17 12:46:56 -07002662 }
brianosman64d094d2016-03-25 06:01:59 -07002663
bsalomon083617b2016-02-12 12:10:14 -08002664 this->didWriteToSurface(target, bounds);
2665}
bsalomona9909122016-01-23 10:41:40 -08002666
bsalomon083617b2016-02-12 12:10:14 -08002667void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2668 if (fHWViewport != viewport) {
2669 viewport.pushToGLViewport(this->glInterface());
2670 fHWViewport = viewport;
2671 }
2672}
2673
jvanverthfb591642016-04-29 09:50:25 -07002674void GrGLGpu::didWriteToSurface(GrSurface* surface, const SkIRect* bounds, int mipLevels) const {
bsalomon083617b2016-02-12 12:10:14 -08002675 SkASSERT(surface);
bsalomona9909122016-01-23 10:41:40 -08002676 // 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 -08002677 if (nullptr == bounds || !bounds->isEmpty()) {
2678 if (GrRenderTarget* target = surface->asRenderTarget()) {
2679 target->flagAsNeedingResolve(bounds);
2680 }
jvanverthfb591642016-04-29 09:50:25 -07002681 GrTexture* texture = surface->asTexture();
2682 if (texture && 1 == mipLevels) {
bsalomona9909122016-01-23 10:41:40 -08002683 texture->texturePriv().dirtyMipMaps(true);
2684 }
egdanield803f272015-03-18 13:01:52 -07002685 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002686}
2687
twiz@google.com0f31ca72011-03-18 17:38:11 +00002688GrGLenum gPrimitiveType2GLMode[] = {
2689 GR_GL_TRIANGLES,
2690 GR_GL_TRIANGLE_STRIP,
2691 GR_GL_TRIANGLE_FAN,
2692 GR_GL_POINTS,
2693 GR_GL_LINES,
2694 GR_GL_LINE_STRIP
reed@google.comac10a2d2010-12-22 21:39:39 +00002695};
2696
bsalomon@google.comd302f142011-03-03 13:54:13 +00002697#define SWAP_PER_DRAW 0
2698
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002699#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002700 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002701 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002702 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002703 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002704 void SwapBuf() {
2705 DWORD procID = GetCurrentProcessId();
2706 HWND hwnd = GetTopWindow(GetDesktopWindow());
2707 while(hwnd) {
2708 DWORD wndProcID = 0;
2709 GetWindowThreadProcessId(hwnd, &wndProcID);
2710 if(wndProcID == procID) {
2711 SwapBuffers(GetDC(hwnd));
2712 }
2713 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2714 }
2715 }
2716 #endif
2717#endif
2718
egdaniel0e1853c2016-03-17 11:35:45 -07002719void GrGLGpu::onDraw(const GrPipeline& pipeline,
2720 const GrPrimitiveProcessor& primProc,
2721 const GrMesh* meshes,
2722 int meshCount) {
2723 if (!this->flushGLState(pipeline, primProc)) {
bsalomond95263c2014-12-16 13:05:12 -08002724 return;
2725 }
egdaniel0e1853c2016-03-17 11:35:45 -07002726 GrPixelLocalStorageState plsState = primProc.getPixelLocalStorageState();
2727 if (!fHWPLSEnabled && plsState !=
ethannicholas22793252016-01-30 09:59:10 -08002728 GrPixelLocalStorageState::kDisabled_GrPixelLocalStorageState) {
2729 GL_CALL(Enable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
egdaniel0e1853c2016-03-17 11:35:45 -07002730 this->setupPixelLocalStorage(pipeline, primProc);
ethannicholas22793252016-01-30 09:59:10 -08002731 fHWPLSEnabled = true;
2732 }
2733 if (plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2734 GrStencilSettings stencil;
2735 stencil.setDisabled();
2736 this->flushStencil(stencil);
2737 }
2738
egdaniel0e1853c2016-03-17 11:35:45 -07002739 for (int i = 0; i < meshCount; ++i) {
2740 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
2741 this->xferBarrier(pipeline.getRenderTarget(), barrierType);
2742 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002743
egdaniel0e1853c2016-03-17 11:35:45 -07002744 const GrMesh& mesh = meshes[i];
2745 GrMesh::Iterator iter;
2746 const GrNonInstancedMesh* nonIdxMesh = iter.init(mesh);
2747 do {
2748 size_t indexOffsetInBytes = 0;
2749 this->setupGeometry(primProc, *nonIdxMesh, &indexOffsetInBytes);
2750 if (nonIdxMesh->isIndexed()) {
2751 GrGLvoid* indices =
2752 reinterpret_cast<GrGLvoid*>(indexOffsetInBytes + sizeof(uint16_t) *
2753 nonIdxMesh->startIndex());
2754 // info.startVertex() was accounted for by setupGeometry.
2755 GL_CALL(DrawElements(gPrimitiveType2GLMode[nonIdxMesh->primitiveType()],
2756 nonIdxMesh->indexCount(),
2757 GR_GL_UNSIGNED_SHORT,
2758 indices));
2759 } else {
2760 // Pass 0 for parameter first. We have to adjust glVertexAttribPointer() to account
2761 // for startVertex in the DrawElements case. So we always rely on setupGeometry to
2762 // have accounted for startVertex.
2763 GL_CALL(DrawArrays(gPrimitiveType2GLMode[nonIdxMesh->primitiveType()], 0,
2764 nonIdxMesh->vertexCount()));
2765 }
2766 fStats.incNumDraws();
2767 } while ((nonIdxMesh = iter.next()));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002768 }
ethannicholas22793252016-01-30 09:59:10 -08002769
2770 if (fHWPLSEnabled && plsState == GrPixelLocalStorageState::kFinish_GrPixelLocalStorageState) {
2771 // PLS draws always involve multiple draws, finishing up with a non-PLS
2772 // draw that writes to the color buffer. That draw ends up here; we wait
2773 // until after it is complete to actually disable PLS.
2774 GL_CALL(Disable(GR_GL_SHADER_PIXEL_LOCAL_STORAGE));
2775 fHWPLSEnabled = false;
2776 this->disableScissor();
2777 }
2778
bsalomon@google.comd302f142011-03-03 13:54:13 +00002779#if SWAP_PER_DRAW
2780 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002781 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002782 aglSwapBuffers(aglGetCurrentContext());
2783 int set_a_break_pt_here = 9;
2784 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002785 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002786 SwapBuf();
2787 int set_a_break_pt_here = 9;
2788 SwapBuf();
2789 #endif
2790#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002791}
2792
cdaltone2e71c22016-04-07 18:13:29 -07002793void GrGLGpu::stampPLSSetupRect(const SkRect& bounds) {
2794 SkASSERT(this->glCaps().glslCaps()->plsPathRenderingSupport())
ethannicholas22793252016-01-30 09:59:10 -08002795
cdaltone2e71c22016-04-07 18:13:29 -07002796 if (!fPLSSetupProgram.fProgram) {
2797 if (!this->createPLSSetupProgram()) {
2798 SkDebugf("Failed to create PLS setup program.\n");
2799 return;
2800 }
2801 }
2802
2803 GL_CALL(UseProgram(fPLSSetupProgram.fProgram));
2804 this->fHWVertexArrayState.setVertexArrayID(this, 0);
2805
2806 GrGLAttribArrayState* attribs = this->fHWVertexArrayState.bindInternalVertexArray(this);
2807 attribs->set(this, 0, fPLSSetupProgram.fArrayBuffer, kVec2f_GrVertexAttribType,
2808 2 * sizeof(GrGLfloat), 0);
ethannicholas22793252016-01-30 09:59:10 -08002809 attribs->disableUnusedArrays(this, 0x1);
2810
cdaltone2e71c22016-04-07 18:13:29 -07002811 GL_CALL(Uniform4f(fPLSSetupProgram.fPosXformUniform, bounds.width(), bounds.height(),
2812 bounds.left(), bounds.top()));
ethannicholas22793252016-01-30 09:59:10 -08002813
2814 GrXferProcessor::BlendInfo blendInfo;
2815 blendInfo.reset();
2816 this->flushBlend(blendInfo, GrSwizzle());
2817 this->flushColorWrite(true);
2818 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
2819 if (!fHWStencilSettings.isDisabled()) {
2820 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2821 }
2822 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
2823 GL_CALL(UseProgram(fHWProgramID));
2824 if (!fHWStencilSettings.isDisabled()) {
2825 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2826 }
2827}
2828
egdaniel0e1853c2016-03-17 11:35:45 -07002829void GrGLGpu::setupPixelLocalStorage(const GrPipeline& pipeline,
2830 const GrPrimitiveProcessor& primProc) {
ethannicholas22793252016-01-30 09:59:10 -08002831 fPLSHasBeenUsed = true;
halcanary9d524f22016-03-29 09:03:52 -07002832 const SkRect& bounds =
egdaniel0e1853c2016-03-17 11:35:45 -07002833 static_cast<const GrPLSGeometryProcessor&>(primProc).getBounds();
ethannicholas22793252016-01-30 09:59:10 -08002834 // setup pixel local storage -- this means capturing and storing the current framebuffer color
2835 // and initializing the winding counts to zero
egdaniel0e1853c2016-03-17 11:35:45 -07002836 GrRenderTarget* rt = pipeline.getRenderTarget();
ethannicholas22793252016-01-30 09:59:10 -08002837 SkScalar width = SkIntToScalar(rt->width());
2838 SkScalar height = SkIntToScalar(rt->height());
2839 // dst rect edges in NDC (-1 to 1)
2840 // having some issues with rounding, just expand the bounds by 1 and trust the scissor to keep
2841 // it contained properly
2842 GrGLfloat dx0 = 2.0f * (bounds.left() - 1) / width - 1.0f;
2843 GrGLfloat dx1 = 2.0f * (bounds.right() + 1) / width - 1.0f;
2844 GrGLfloat dy0 = -2.0f * (bounds.top() - 1) / height + 1.0f;
2845 GrGLfloat dy1 = -2.0f * (bounds.bottom() + 1) / height + 1.0f;
2846 SkRect deviceBounds = SkRect::MakeXYWH(dx0, dy0, dx1 - dx0, dy1 - dy0);
halcanary9d524f22016-03-29 09:03:52 -07002847
ethannicholas22793252016-01-30 09:59:10 -08002848 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
cdaltone2e71c22016-04-07 18:13:29 -07002849 this->stampPLSSetupRect(deviceBounds);
ethannicholas22793252016-01-30 09:59:10 -08002850}
2851
bsalomon861e1032014-12-16 07:33:49 -08002852void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002853 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002854 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002855 // Some extensions automatically resolves the texture when it is read.
2856 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002857 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2858 fStats.incRenderTargetBinds();
2859 fStats.incRenderTargetBinds();
2860 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2861 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2862 // make sure we go through flushRenderTarget() since we've modified
2863 // the bound DRAW FBO ID.
2864 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002865 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002866 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002867
bsalomon@google.com347c3822013-05-01 20:10:01 +00002868 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002869 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002870 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002871 scissorState.set(dirtyRect);
2872 this->flushScissor(scissorState, vp, rt->origin());
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002873 GL_CALL(ResolveMultisampleFramebuffer());
2874 } else {
robertphillipse85a32d2015-02-10 08:16:55 -08002875 GrGLIRect r;
2876 r.setRelativeTo(vp, dirtyRect.fLeft, dirtyRect.fTop,
2877 dirtyRect.width(), dirtyRect.height(), target->origin());
2878
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002879 int right = r.fLeft + r.fWidth;
2880 int top = r.fBottom + r.fHeight;
derekf8c8f71a2014-09-16 06:24:57 -07002881
2882 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002883 this->disableScissor();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002884 GL_CALL(BlitFramebuffer(r.fLeft, r.fBottom, right, top,
2885 r.fLeft, r.fBottom, right, top,
2886 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002887 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002888 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002889 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002890 }
2891}
2892
bsalomon@google.com411dad02012-06-05 20:24:20 +00002893namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002894
bsalomon@google.com411dad02012-06-05 20:24:20 +00002895
2896GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
2897 static const GrGLenum gTable[] = {
2898 GR_GL_KEEP, // kKeep_StencilOp
2899 GR_GL_REPLACE, // kReplace_StencilOp
2900 GR_GL_INCR_WRAP, // kIncWrap_StencilOp
2901 GR_GL_INCR, // kIncClamp_StencilOp
2902 GR_GL_DECR_WRAP, // kDecWrap_StencilOp
2903 GR_GL_DECR, // kDecClamp_StencilOp
2904 GR_GL_ZERO, // kZero_StencilOp
2905 GR_GL_INVERT, // kInvert_StencilOp
2906 };
bsalomon3de75da2016-04-29 08:44:16 -07002907 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kStencilOpCnt);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002908 GR_STATIC_ASSERT(0 == kKeep_StencilOp);
2909 GR_STATIC_ASSERT(1 == kReplace_StencilOp);
2910 GR_STATIC_ASSERT(2 == kIncWrap_StencilOp);
2911 GR_STATIC_ASSERT(3 == kIncClamp_StencilOp);
2912 GR_STATIC_ASSERT(4 == kDecWrap_StencilOp);
2913 GR_STATIC_ASSERT(5 == kDecClamp_StencilOp);
2914 GR_STATIC_ASSERT(6 == kZero_StencilOp);
2915 GR_STATIC_ASSERT(7 == kInvert_StencilOp);
bsalomon3de75da2016-04-29 08:44:16 -07002916 SkASSERT((unsigned) op < kStencilOpCnt);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002917 return gTable[op];
2918}
2919
2920void set_gl_stencil(const GrGLInterface* gl,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002921 const GrStencilSettings& settings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002922 GrGLenum glFace,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002923 GrStencilSettings::Face grFace) {
kkinnunenccdaa042014-08-20 01:36:23 -07002924 GrGLenum glFunc = GrToGLStencilFunc(settings.func(grFace));
bsalomon@google.coma3201942012-06-21 19:58:20 +00002925 GrGLenum glFailOp = gr_to_gl_stencil_op(settings.failOp(grFace));
2926 GrGLenum glPassOp = gr_to_gl_stencil_op(settings.passOp(grFace));
2927
2928 GrGLint ref = settings.funcRef(grFace);
2929 GrGLint mask = settings.funcMask(grFace);
2930 GrGLint writeMask = settings.writeMask(grFace);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002931
2932 if (GR_GL_FRONT_AND_BACK == glFace) {
2933 // we call the combined func just in case separate stencil is not
2934 // supported.
2935 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2936 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002937 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002938 } else {
2939 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2940 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002941 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002942 }
2943}
2944}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002945
bsalomon3e791242014-12-17 13:43:13 -08002946void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
2947 if (fHWStencilSettings != stencilSettings) {
joshualitta58fe352014-10-27 08:39:00 -07002948 if (stencilSettings.isDisabled()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00002949 if (kNo_TriState != fHWStencilTestEnabled) {
2950 GL_CALL(Disable(GR_GL_STENCIL_TEST));
2951 fHWStencilTestEnabled = kNo_TriState;
2952 }
2953 } else {
2954 if (kYes_TriState != fHWStencilTestEnabled) {
2955 GL_CALL(Enable(GR_GL_STENCIL_TEST));
2956 fHWStencilTestEnabled = kYes_TriState;
2957 }
bsalomon@google.coma3201942012-06-21 19:58:20 +00002958 }
joshualitta58fe352014-10-27 08:39:00 -07002959 if (!stencilSettings.isDisabled()) {
bsalomon@google.combcce8922013-03-25 15:38:39 +00002960 if (this->caps()->twoSidedStencilSupport()) {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002961 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002962 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002963 GR_GL_FRONT,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002964 GrStencilSettings::kFront_Face);
bsalomon@google.com411dad02012-06-05 20:24:20 +00002965 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002966 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002967 GR_GL_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002968 GrStencilSettings::kBack_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002969 } else {
bsalomon@google.com411dad02012-06-05 20:24:20 +00002970 set_gl_stencil(this->glInterface(),
joshualitta58fe352014-10-27 08:39:00 -07002971 stencilSettings,
bsalomon@google.com411dad02012-06-05 20:24:20 +00002972 GR_GL_FRONT_AND_BACK,
bsalomon@google.coma3201942012-06-21 19:58:20 +00002973 GrStencilSettings::kFront_Face);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002974 }
2975 }
joshualitta58fe352014-10-27 08:39:00 -07002976 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002977 }
2978}
2979
cdaltonaf8bc7d2016-02-05 09:35:20 -08002980void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002981 // rt is only optional if useHWAA is false.
2982 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002983 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002984
cdaltond4727922015-11-10 12:49:06 -08002985 if (this->glCaps().multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002986 if (useHWAA) {
2987 if (kYes_TriState != fMSAAEnabled) {
2988 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2989 fMSAAEnabled = kYes_TriState;
2990 }
2991 } else {
2992 if (kNo_TriState != fMSAAEnabled) {
2993 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2994 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002995 }
2996 }
2997 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002998
2999 if (0 != this->caps()->maxRasterSamples()) {
3000 if (useHWAA && rt->hasMixedSamples() && !stencilEnabled) {
3001 // Since stencil is disabled and we want more samples than are in the color buffer, we
3002 // need to tell the rasterizer explicitly how many to run.
3003 if (kYes_TriState != fHWRasterMultisampleEnabled) {
3004 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
3005 fHWRasterMultisampleEnabled = kYes_TriState;
3006 }
3007 if (rt->numStencilSamples() != fHWNumRasterSamples) {
3008 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
3009 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
3010 fHWNumRasterSamples = rt->numStencilSamples();
3011 }
3012 } else {
3013 if (kNo_TriState != fHWRasterMultisampleEnabled) {
3014 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
3015 fHWRasterMultisampleEnabled = kNo_TriState;
3016 }
3017 }
3018 } else {
3019 SkASSERT(!useHWAA || !rt->hasMixedSamples() || stencilEnabled);
3020 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00003021}
3022
bsalomon7f9b2e42016-01-12 13:29:26 -08003023void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07003024 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07003025 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07003026
cdalton8917d622015-05-06 13:40:21 -07003027 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08003028 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
3029 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07003030 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
3031 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07003032 if (blendOff) {
3033 if (kNo_TriState != fHWBlendState.fEnabled) {
3034 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07003035
3036 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
3037 // https://code.google.com/p/skia/issues/detail?id=3943
3038 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
3039 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
3040 SkASSERT(this->caps()->advancedBlendEquationSupport());
3041 // Set to any basic blending equation.
3042 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
3043 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
3044 fHWBlendState.fEquation = blend_equation;
3045 }
3046
egdanielb414f252014-07-29 13:15:47 -07003047 fHWBlendState.fEnabled = kNo_TriState;
3048 }
cdalton8917d622015-05-06 13:40:21 -07003049 return;
3050 }
3051
3052 if (kYes_TriState != fHWBlendState.fEnabled) {
3053 GL_CALL(Enable(GR_GL_BLEND));
3054 fHWBlendState.fEnabled = kYes_TriState;
3055 }
3056
3057 if (fHWBlendState.fEquation != equation) {
3058 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
3059 fHWBlendState.fEquation = equation;
3060 }
3061
3062 if (GrBlendEquationIsAdvanced(equation)) {
3063 SkASSERT(this->caps()->advancedBlendEquationSupport());
3064 // Advanced equations have no other blend state.
3065 return;
3066 }
3067
bsalomone63ffef2016-02-05 07:17:34 -08003068 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07003069 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
3070 gXfermodeCoeff2Blend[dstCoeff]));
3071 fHWBlendState.fSrcCoeff = srcCoeff;
3072 fHWBlendState.fDstCoeff = dstCoeff;
3073 }
3074
bsalomon7f9b2e42016-01-12 13:29:26 -08003075 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
3076 GrColor blendConst = blendInfo.fBlendConstant;
3077 blendConst = swizzle.applyTo(blendConst);
3078 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
3079 GrGLfloat c[4];
3080 GrColorToRGBAFloat(blendConst, c);
3081 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
3082 fHWBlendState.fConstColor = blendConst;
3083 fHWBlendState.fConstColorValid = true;
3084 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00003085 }
3086}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00003087
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00003088static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00003089 static const GrGLenum gWrapModes[] = {
3090 GR_GL_CLAMP_TO_EDGE,
3091 GR_GL_REPEAT,
3092 GR_GL_MIRRORED_REPEAT
3093 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00003094 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00003095 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
3096 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
3097 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
3098 return gWrapModes[tm];
3099}
3100
egdanielb7e7d572015-11-04 04:23:53 -08003101static GrGLenum get_component_enum_from_char(char component) {
3102 switch (component) {
3103 case 'r':
3104 return GR_GL_RED;
3105 case 'g':
3106 return GR_GL_GREEN;
3107 case 'b':
3108 return GR_GL_BLUE;
3109 case 'a':
3110 return GR_GL_ALPHA;
3111 default:
3112 SkFAIL("Unsupported component");
3113 return 0;
3114 }
3115}
3116
3117/** If texture swizzling is available using tex parameters then it is preferred over mangling
3118 the generated shader code. This potentially allows greater reuse of cached shaders. */
3119static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08003120 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08003121 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08003122 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08003123 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08003124 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08003125 }
3126}
3127
brianosmana6359362016-03-21 06:55:37 -07003128void GrGLGpu::bindTexture(int unitIdx, const GrTextureParams& params, bool dstConfigAllowsSRGB,
3129 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07003130 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00003131
reed856e9d92015-09-30 12:21:45 -07003132#ifdef SK_DEBUG
3133 if (!this->caps()->npotTextureTileSupport()) {
3134 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
3135 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
3136 if (tileX || tileY) {
3137 const int w = texture->width();
3138 const int h = texture->height();
3139 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
3140 }
3141 }
3142#endif
3143
bsalomon@google.comb8670992012-07-25 21:27:09 +00003144 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3145 // 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 +00003146 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08003147 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07003148 if (texRT) {
bsalomon@google.com4c883782012-06-04 19:05:11 +00003149 this->onResolveRenderTarget(texRT);
3150 }
3151
bsalomon1c63bf62014-07-22 13:09:46 -07003152 uint32_t textureID = texture->getUniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003153 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07003154 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003155 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003156 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07003157 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00003158 }
3159
bsalomon@google.com4c883782012-06-04 19:05:11 +00003160 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003161 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003162 bool setAll = timestamp < this->getResetTimestamp();
3163 GrGLTexture::TexParams newTexParams;
3164
brianosmana6359362016-03-21 06:55:37 -07003165 if (this->caps()->srgbSupport()) {
3166 // By default, the decision to allow SRGB decode is based on the destination config.
3167 // A texture can override that by specifying a value in GrTextureParams.
3168 newTexParams.fSRGBDecode =
3169 (dstConfigAllowsSRGB || GrTextureParams::kForceAllowSRGB_SRGBMode == params.srgbMode())
3170 ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3171
3172 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
3173 this->setTextureUnit(unitIdx);
3174 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
3175 }
3176 }
3177
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003178 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003179 GR_GL_NEAREST,
3180 GR_GL_LINEAR,
3181 GR_GL_LINEAR_MIPMAP_LINEAR
3182 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003183 static GrGLenum glMagFilterModes[] = {
3184 GR_GL_NEAREST,
3185 GR_GL_LINEAR,
3186 GR_GL_LINEAR
3187 };
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003188 GrTextureParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07003189
3190 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3191 if (!this->caps()->mipMapSupport() || GrPixelConfigIsCompressed(texture->config())) {
3192 filterMode = GrTextureParams::kBilerp_FilterMode;
3193 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003194 }
bsalomonefd7d452014-10-23 14:17:46 -07003195
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003196 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3197 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003198
cblume55f2d2d2016-02-26 13:20:48 -08003199 if (GrTextureParams::kMipMap_FilterMode == filterMode) {
3200 if (texture->texturePriv().mipMapsAreDirty()) {
brianosmanadf4fa62016-04-26 16:48:06 -07003201 this->setTextureUnit(unitIdx);
cblume55f2d2d2016-02-26 13:20:48 -08003202 GL_CALL(GenerateMipmap(target));
3203 texture->texturePriv().dirtyMipMaps(false);
3204 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3205 texture->width(), texture->height()));
3206 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003207 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003208
cblume55f2d2d2016-02-26 13:20:48 -08003209 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3210
bsalomon@google.comb8670992012-07-25 21:27:09 +00003211 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3212 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003213 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003214 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003215 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003216 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003217 }
3218 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3219 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003220 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003221 }
cblume55f2d2d2016-02-26 13:20:48 -08003222 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003223 // These are not supported in ES2 contexts
3224 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3225 if (newTexParams.fMaxMipMapLevel != 0) {
3226 this->setTextureUnit(unitIdx);
3227 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3228 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3229 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3230 newTexParams.fMaxMipMapLevel));
3231 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3232 newTexParams.fMaxMipMapLevel));
3233 }
cblume55f2d2d2016-02-26 13:20:48 -08003234 }
3235 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003236 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003237 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003238 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003239 }
3240 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003241 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003242 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003243 }
bsalomoncdee0092016-01-08 13:20:12 -08003244 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003245 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3246 oldTexParams.fSwizzleRGBA,
3247 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003248 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003249 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003250 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003251}
3252
cdalton74b8d322016-04-11 14:47:28 -07003253void GrGLGpu::bindTexelBuffer(int unitIdx, intptr_t offsetInBytes, GrPixelConfig texelConfig,
3254 GrGLBuffer* buffer) {
3255 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3256 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
3257 SkASSERT(offsetInBytes >= 0 && offsetInBytes < (intptr_t) buffer->glSizeInBytes());
3258
3259 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3260
3261 if (!buffTex.fKnownBound) {
3262 if (!buffTex.fTextureID) {
3263 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3264 if (!buffTex.fTextureID) {
3265 return;
3266 }
3267 }
3268
3269 this->setTextureUnit(unitIdx);
3270 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3271
3272 buffTex.fKnownBound = true;
3273 }
3274
3275 if (buffer->getUniqueID() != buffTex.fAttachedBufferUniqueID ||
3276 buffTex.fOffsetInBytes != offsetInBytes ||
3277 buffTex.fTexelConfig != texelConfig ||
3278 buffTex.fAttachedSizeInBytes != buffer->glSizeInBytes() - offsetInBytes) {
3279
3280 size_t attachmentSizeInBytes = buffer->glSizeInBytes() - offsetInBytes;
3281
3282 this->setTextureUnit(unitIdx);
3283 GL_CALL(TexBufferRange(GR_GL_TEXTURE_BUFFER,
3284 this->glCaps().configSizedInternalFormat(texelConfig),
3285 buffer->bufferID(),
3286 offsetInBytes,
3287 attachmentSizeInBytes));
3288
3289 buffTex.fOffsetInBytes = offsetInBytes;
3290 buffTex.fTexelConfig = texelConfig;
3291 buffTex.fAttachedSizeInBytes = attachmentSizeInBytes;
3292 buffTex.fAttachedBufferUniqueID = buffer->getUniqueID();
3293
3294 if (this->glCaps().textureSwizzleSupport() &&
3295 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3296 GrGLenum glSwizzle[4];
3297 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3298 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3299 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3300 }
3301
3302 buffer->setHasAttachedToTexture();
3303 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3304 }
3305}
3306
3307void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3308 this->setTextureUnit(unitIdx);
3309 if (this->glStandard() == kGLES_GrGLStandard) {
3310 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3311 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3312 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3313 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3314 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3315 } else {
3316 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3317 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3318 reinterpret_cast<const GrGLint*>(swizzle)));
3319 }
3320}
3321
egdaniel080e6732014-12-22 07:35:52 -08003322void GrGLGpu::flushColorWrite(bool writeColor) {
3323 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003324 if (kNo_TriState != fHWWriteToColor) {
3325 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3326 GR_GL_FALSE, GR_GL_FALSE));
3327 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003328 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003329 } else {
3330 if (kYes_TriState != fHWWriteToColor) {
3331 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3332 fHWWriteToColor = kYes_TriState;
3333 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003334 }
bsalomon3e791242014-12-17 13:43:13 -08003335}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003336
egdaniel8dd688b2015-01-22 10:16:09 -08003337void GrGLGpu::flushDrawFace(GrPipelineBuilder::DrawFace face) {
bsalomon3e791242014-12-17 13:43:13 -08003338 if (fHWDrawFace != face) {
3339 switch (face) {
egdaniel8dd688b2015-01-22 10:16:09 -08003340 case GrPipelineBuilder::kCCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003341 GL_CALL(Enable(GR_GL_CULL_FACE));
3342 GL_CALL(CullFace(GR_GL_BACK));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003343 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003344 case GrPipelineBuilder::kCW_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003345 GL_CALL(Enable(GR_GL_CULL_FACE));
3346 GL_CALL(CullFace(GR_GL_FRONT));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003347 break;
egdaniel8dd688b2015-01-22 10:16:09 -08003348 case GrPipelineBuilder::kBoth_DrawFace:
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003349 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.comd302f142011-03-03 13:54:13 +00003350 break;
3351 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00003352 SkFAIL("Unknown draw face.");
bsalomon@google.comd302f142011-03-03 13:54:13 +00003353 }
bsalomon3e791242014-12-17 13:43:13 -08003354 fHWDrawFace = face;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003355 }
reed@google.comac10a2d2010-12-22 21:39:39 +00003356}
3357
bsalomon861e1032014-12-16 07:33:49 -08003358void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003359 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003360 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003361 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003362 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003363 }
3364}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003365
bsalomon861e1032014-12-16 07:33:49 -08003366void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003367 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003368 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003369 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3370 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3371 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003372 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003373 // clear out the this field so that if a program does use this unit it will rebind the correct
3374 // texture.
bsalomon1c63bf62014-07-22 13:09:46 -07003375 fHWBoundTextureUniqueIDs[lastUnitIdx] = SK_InvalidUniqueID;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003376}
3377
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003378// Determines whether glBlitFramebuffer could be used between src and dst.
bsalomon7ea33f52015-11-22 14:51:00 -08003379static inline bool can_blit_framebuffer(const GrSurface* dst,
3380 const GrSurface* src,
3381 const GrGLGpu* gpu) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003382 if (gpu->glCaps().isConfigRenderable(dst->config(), dst->desc().fSampleCnt > 0) &&
bsalomon083617b2016-02-12 12:10:14 -08003383 gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0)) {
3384 switch (gpu->glCaps().blitFramebufferSupport()) {
3385 case GrGLCaps::kNone_BlitFramebufferSupport:
3386 return false;
3387 case GrGLCaps::kNoScalingNoMirroring_BlitFramebufferSupport:
3388 // Our copy surface doesn't support scaling so just check for mirroring.
3389 if (dst->origin() != src->origin()) {
3390 return false;
3391 }
3392 break;
3393 case GrGLCaps::kFull_BlitFramebufferSupport:
3394 break;
3395 }
commit-bot@chromium.orga8e5a062013-09-05 23:44:09 +00003396 // ES3 doesn't allow framebuffer blits when the src has MSAA and the configs don't match
3397 // or the rects are not the same (not just the same size but have the same edges).
3398 if (GrGLCaps::kES_3_0_MSFBOType == gpu->glCaps().msFBOType() &&
3399 (src->desc().fSampleCnt > 0 || src->config() != dst->config())) {
3400 return false;
3401 }
bsalomon7ea33f52015-11-22 14:51:00 -08003402 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3403 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3404 return false;
3405 }
3406 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3407 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3408 return false;
3409 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003410 return true;
3411 } else {
3412 return false;
3413 }
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003414}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003415
bsalomon7ea33f52015-11-22 14:51:00 -08003416static inline bool can_copy_texsubimage(const GrSurface* dst,
3417 const GrSurface* src,
3418 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003419 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3420 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3421 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003422 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003423 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3424 return false;
3425 }
3426 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3427 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3428 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003429 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003430 return false;
3431 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003432 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3433 // If the src is multisampled (and uses an extension where there is a separate MSAA
3434 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003435 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003436 return false;
3437 }
bsalomon7ea33f52015-11-22 14:51:00 -08003438
3439 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3440 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3441 // texture.
3442 if (!dstTex) {
3443 return false;
3444 }
3445
3446 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003447
bsalomon7ea33f52015-11-22 14:51:00 -08003448 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3449 // is required.
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00003450 if (gpu->glCaps().isConfigRenderable(src->config(), src->desc().fSampleCnt > 0) &&
bsalomon7ea33f52015-11-22 14:51:00 -08003451 !GrPixelConfigIsCompressed(src->config()) &&
3452 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) &&
3453 dstTex->target() == GR_GL_TEXTURE_2D &&
3454 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003455 return true;
3456 } else {
3457 return false;
3458 }
3459}
3460
3461// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3462// relative to is output.
bsalomon10528f12015-10-14 12:54:52 -07003463void GrGLGpu::bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3464 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003465 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003466 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003467 SkASSERT(surface->asTexture());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003468 GrGLuint texID = static_cast<GrGLTexture*>(surface->asTexture())->textureID();
bsalomon10528f12015-10-14 12:54:52 -07003469 GrGLenum target = static_cast<GrGLTexture*>(surface->asTexture())->target();
egdanield803f272015-03-18 13:01:52 -07003470 GrGLuint* tempFBOID;
3471 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003472
egdanield803f272015-03-18 13:01:52 -07003473 if (0 == *tempFBOID) {
3474 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003475 }
3476
egdanield803f272015-03-18 13:01:52 -07003477 fStats.incRenderTargetBinds();
3478 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3479 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003480 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003481 target,
robertphillips754f4e92014-09-18 13:52:08 -07003482 texID,
3483 0));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003484 viewport->fLeft = 0;
3485 viewport->fBottom = 0;
3486 viewport->fWidth = surface->width();
3487 viewport->fHeight = surface->height();
3488 } else {
egdanield803f272015-03-18 13:01:52 -07003489 fStats.incRenderTargetBinds();
3490 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003491 *viewport = rt->getViewport();
3492 }
egdaniel0f5f9672015-02-03 11:10:51 -08003493}
3494
bsalomon10528f12015-10-14 12:54:52 -07003495void GrGLGpu::unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface) {
cblume61214052016-01-26 09:10:48 -08003496 // bindSurfaceFBOForCopy temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003497 if (!surface->asRenderTarget()) {
3498 SkASSERT(surface->asTexture());
3499 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3500 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3501 GR_GL_COLOR_ATTACHMENT0,
3502 textureTarget,
3503 0,
3504 0));
3505 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003506}
3507
joshualitt1c735482015-07-13 08:08:25 -07003508bool GrGLGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
bsalomon6df86402015-06-01 10:41:49 -07003509 // If the src is a texture, we can implement the blit as a draw assuming the config is
3510 // renderable.
3511 if (src->asTexture() && this->caps()->isConfigRenderable(src->config(), false)) {
3512 desc->fOrigin = kDefault_GrSurfaceOrigin;
3513 desc->fFlags = kRenderTarget_GrSurfaceFlag;
3514 desc->fConfig = src->config();
3515 return true;
3516 }
3517
bsalomon7ea33f52015-11-22 14:51:00 -08003518 const GrGLTexture* srcTexture = static_cast<const GrGLTexture*>(src->asTexture());
3519 if (srcTexture && srcTexture->target() != GR_GL_TEXTURE_2D) {
3520 // Not supported for FBO blit or CopyTexSubImage
3521 return false;
3522 }
3523
bsalomon6df86402015-06-01 10:41:49 -07003524 // We look for opportunities to use CopyTexSubImage, or fbo blit. If neither are
bsalomonf90a02b2014-11-26 12:28:00 -08003525 // possible and we return false to fallback to creating a render target dst for render-to-
3526 // texture. This code prefers CopyTexSubImage to fbo blit and avoids triggering temporary fbo
3527 // creation. It isn't clear that avoiding temporary fbo creation is actually optimal.
3528
bsalomon@google.comeb851172013-04-15 13:51:00 +00003529 // Check for format issues with glCopyTexSubImage2D
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003530 if (kGLES_GrGLStandard == this->glStandard() && this->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003531 kBGRA_8888_GrPixelConfig == src->config()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003532 // glCopyTexSubImage2D doesn't work with this config. If the bgra can be used with fbo blit
3533 // then we set up for that, otherwise fail.
3534 if (this->caps()->isConfigRenderable(kBGRA_8888_GrPixelConfig, false)) {
3535 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003536 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003537 desc->fConfig = kBGRA_8888_GrPixelConfig;
3538 return true;
3539 }
3540 return false;
halcanary96fcdcc2015-08-27 07:41:13 -07003541 } else if (nullptr == src->asRenderTarget()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003542 // CopyTexSubImage2D or fbo blit would require creating a temp fbo for the src.
3543 return false;
bsalomon@google.coma2719852013-04-17 14:25:27 +00003544 }
3545
3546 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
egdanield803f272015-03-18 13:01:52 -07003547 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomonf90a02b2014-11-26 12:28:00 -08003548 // It's illegal to call CopyTexSubImage2D on a MSAA renderbuffer. Set up for FBO blit or
3549 // fail.
3550 if (this->caps()->isConfigRenderable(src->config(), false)) {
3551 desc->fOrigin = kDefault_GrSurfaceOrigin;
bsalomon6bc1b5f2015-02-23 09:06:38 -08003552 desc->fFlags = kRenderTarget_GrSurfaceFlag;
bsalomonf90a02b2014-11-26 12:28:00 -08003553 desc->fConfig = src->config();
3554 return true;
3555 }
3556 return false;
bsalomon@google.comeb851172013-04-15 13:51:00 +00003557 }
bsalomonf90a02b2014-11-26 12:28:00 -08003558
3559 // We'll do a CopyTexSubImage. Make the dst a plain old texture.
3560 desc->fConfig = src->config();
3561 desc->fOrigin = src->origin();
3562 desc->fFlags = kNone_GrSurfaceFlags;
3563 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003564}
3565
joshualitt1cbdcde2015-08-21 11:53:29 -07003566bool GrGLGpu::onCopySurface(GrSurface* dst,
3567 GrSurface* src,
3568 const SkIRect& srcRect,
3569 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003570 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3571 // swizzle.
3572 if (this->glCaps().glslCaps()->configOutputSwizzle(src->config()) !=
3573 this->glCaps().glslCaps()->configOutputSwizzle(dst->config())) {
3574 return false;
3575 }
bsalomon083617b2016-02-12 12:10:14 -08003576 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3577 bool preferCopy = SkToBool(dst->asRenderTarget());
3578 if (preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003579 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3580 return true;
3581 }
bsalomon5df6fee2015-05-18 06:26:15 -07003582 }
cblume61214052016-01-26 09:10:48 -08003583
bsalomon6df86402015-06-01 10:41:49 -07003584 if (can_copy_texsubimage(dst, src, this)) {
3585 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
3586 return true;
3587 }
3588
mtklein404b3b22015-05-18 09:29:10 -07003589 if (can_blit_framebuffer(dst, src, this)) {
bsalomon6df86402015-06-01 10:41:49 -07003590 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
3591 }
3592
bsalomon083617b2016-02-12 12:10:14 -08003593 if (!preferCopy && src->asTexture()) {
cdaltone2e71c22016-04-07 18:13:29 -07003594 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
3595 return true;
3596 }
bsalomon083617b2016-02-12 12:10:14 -08003597 }
3598
bsalomon6df86402015-06-01 10:41:49 -07003599 return false;
3600}
3601
cdaltone2e71c22016-04-07 18:13:29 -07003602bool GrGLGpu::createCopyProgram(int progIdx) {
cdaltonc08f1962016-02-12 12:14:06 -08003603 const GrGLSLCaps* glslCaps = this->glCaps().glslCaps();
bsalomone5286e02016-01-14 09:24:09 -08003604 static const GrSLType kSamplerTypes[3] = { kSampler2D_GrSLType, kSamplerExternal_GrSLType,
3605 kSampler2DRect_GrSLType };
cdaltone2e71c22016-04-07 18:13:29 -07003606 if (kSamplerExternal_GrSLType == kSamplerTypes[progIdx] &&
3607 !this->glCaps().glslCaps()->externalTextureSupport()) {
3608 return false;
cdaltondeacc972016-04-06 14:26:33 -07003609 }
cdaltone2e71c22016-04-07 18:13:29 -07003610 if (kSampler2DRect_GrSLType == kSamplerTypes[progIdx] &&
3611 !this->glCaps().rectangleTextureSupport()) {
3612 return false;
3613 }
3614
3615 if (!fCopyProgramArrayBuffer) {
3616 static const GrGLfloat vdata[] = {
3617 0, 0,
3618 0, 1,
3619 1, 0,
3620 1, 1
3621 };
3622 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3623 kStatic_GrAccessPattern, vdata));
3624 }
3625 if (!fCopyProgramArrayBuffer) {
3626 return false;
3627 }
3628
3629 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3630 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3631 if (!fCopyPrograms[progIdx].fProgram) {
3632 return false;
3633 }
3634
3635 const char* version = glslCaps->versionDeclString();
3636 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3637 GrGLSLShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3638 GrShaderVar::kUniform_TypeModifier);
3639 GrGLSLShaderVar uPosXform("u_posXform", kVec4f_GrSLType,
3640 GrShaderVar::kUniform_TypeModifier);
3641 GrGLSLShaderVar uTexture("u_texture", kSamplerTypes[progIdx],
3642 GrShaderVar::kUniform_TypeModifier);
3643 GrGLSLShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType,
3644 GrShaderVar::kVaryingOut_TypeModifier);
3645 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,
3646 GrShaderVar::kOut_TypeModifier);
3647
3648 SkString vshaderTxt(version);
3649 if (glslCaps->noperspectiveInterpolationSupport()) {
3650 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3651 vshaderTxt.appendf("#extension %s : require\n", extension);
3652 }
3653 vTexCoord.addModifier("noperspective");
3654 }
3655
3656 aVertex.appendDecl(glslCaps, &vshaderTxt);
3657 vshaderTxt.append(";");
3658 uTexCoordXform.appendDecl(glslCaps, &vshaderTxt);
3659 vshaderTxt.append(";");
3660 uPosXform.appendDecl(glslCaps, &vshaderTxt);
3661 vshaderTxt.append(";");
3662 vTexCoord.appendDecl(glslCaps, &vshaderTxt);
3663 vshaderTxt.append(";");
3664
3665 vshaderTxt.append(
3666 "// Copy Program VS\n"
3667 "void main() {"
3668 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3669 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3670 " gl_Position.zw = vec2(0, 1);"
3671 "}"
3672 );
3673
3674 SkString fshaderTxt(version);
3675 if (glslCaps->noperspectiveInterpolationSupport()) {
3676 if (const char* extension = glslCaps->noperspectiveInterpolationExtensionString()) {
3677 fshaderTxt.appendf("#extension %s : require\n", extension);
3678 }
3679 }
3680 if (kSamplerTypes[progIdx] == kSamplerExternal_GrSLType) {
3681 fshaderTxt.appendf("#extension %s : require\n",
3682 glslCaps->externalTextureExtensionString());
3683 }
3684 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps,
3685 &fshaderTxt);
3686 vTexCoord.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
3687 vTexCoord.appendDecl(glslCaps, &fshaderTxt);
3688 fshaderTxt.append(";");
3689 uTexture.appendDecl(glslCaps, &fshaderTxt);
3690 fshaderTxt.append(";");
3691 const char* fsOutName;
3692 if (glslCaps->mustDeclareFragmentShaderOutput()) {
3693 oFragColor.appendDecl(glslCaps, &fshaderTxt);
3694 fshaderTxt.append(";");
3695 fsOutName = oFragColor.c_str();
3696 } else {
3697 fsOutName = "gl_FragColor";
3698 }
3699 fshaderTxt.appendf(
3700 "// Copy Program FS\n"
3701 "void main() {"
3702 " %s = %s(u_texture, v_texCoord);"
3703 "}",
3704 fsOutName,
3705 GrGLSLTexture2DFunctionName(kVec2f_GrSLType, kSamplerTypes[progIdx], this->glslGeneration())
3706 );
3707
3708 const char* str;
3709 GrGLint length;
3710
3711 str = vshaderTxt.c_str();
3712 length = SkToInt(vshaderTxt.size());
3713 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3714 GR_GL_VERTEX_SHADER, &str, &length, 1,
3715 &fStats);
3716
3717 str = fshaderTxt.c_str();
3718 length = SkToInt(fshaderTxt.size());
3719 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3720 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3721 &fStats);
3722
3723 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3724
3725 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3726 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3727 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3728 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3729 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3730 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3731
3732 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3733
3734 GL_CALL(DeleteShader(vshader));
3735 GL_CALL(DeleteShader(fshader));
3736
3737 return true;
bsalomon6df86402015-06-01 10:41:49 -07003738}
3739
cdaltone2e71c22016-04-07 18:13:29 -07003740bool GrGLGpu::createWireRectProgram() {
3741 if (!fWireRectArrayBuffer) {
3742 static const GrGLfloat vdata[] = {
3743 0, 0,
3744 0, 1,
3745 1, 1,
3746 1, 0
3747 };
3748 fWireRectArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3749 kStatic_GrAccessPattern, vdata));
3750 if (!fWireRectArrayBuffer) {
3751 return false;
3752 }
3753 }
3754
bsalomon6dea83f2015-12-03 12:58:06 -08003755 SkASSERT(!fWireRectProgram.fProgram);
cdaltone2e71c22016-04-07 18:13:29 -07003756 GL_CALL_RET(fWireRectProgram.fProgram, CreateProgram());
3757 if (!fWireRectProgram.fProgram) {
3758 return false;
3759 }
3760
bsalomon6dea83f2015-12-03 12:58:06 -08003761 GrGLSLShaderVar uColor("u_color", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3762 GrGLSLShaderVar uRect("u_rect", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3763 GrGLSLShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
3764 const char* version = this->glCaps().glslCaps()->versionDeclString();
3765
3766 // The rect uniform specifies the rectangle in NDC space as a vec4 (left,top,right,bottom). The
3767 // program is used with a vbo containing the unit square. Vertices are computed from the rect
3768 // uniform using the 4 vbo vertices.
3769 SkString vshaderTxt(version);
3770 aVertex.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3771 vshaderTxt.append(";");
3772 uRect.appendDecl(this->glCaps().glslCaps(), &vshaderTxt);
3773 vshaderTxt.append(";");
3774 vshaderTxt.append(
3775 "// Wire Rect Program VS\n"
3776 "void main() {"
3777 " gl_Position.x = u_rect.x + a_vertex.x * (u_rect.z - u_rect.x);"
3778 " gl_Position.y = u_rect.y + a_vertex.y * (u_rect.w - u_rect.y);"
3779 " gl_Position.zw = vec2(0, 1);"
3780 "}"
3781 );
3782
3783 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
3784
3785 SkString fshaderTxt(version);
3786 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision,
3787 *this->glCaps().glslCaps(),
3788 &fshaderTxt);
3789 uColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3790 fshaderTxt.append(";");
3791 const char* fsOutName;
3792 if (this->glCaps().glslCaps()->mustDeclareFragmentShaderOutput()) {
3793 oFragColor.appendDecl(this->glCaps().glslCaps(), &fshaderTxt);
3794 fshaderTxt.append(";");
3795 fsOutName = oFragColor.c_str();
3796 } else {
3797 fsOutName = "gl_FragColor";
3798 }
3799 fshaderTxt.appendf(
3800 "// Write Rect Program FS\n"
3801 "void main() {"
3802 " %s = %s;"
3803 "}",
3804 fsOutName,
3805 uColor.c_str()
3806 );
3807
bsalomon6dea83f2015-12-03 12:58:06 -08003808 const char* str;
3809 GrGLint length;
3810
3811 str = vshaderTxt.c_str();
3812 length = SkToInt(vshaderTxt.size());
3813 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3814 GR_GL_VERTEX_SHADER, &str, &length, 1,
3815 &fStats);
3816
3817 str = fshaderTxt.c_str();
3818 length = SkToInt(fshaderTxt.size());
3819 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
3820 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
3821 &fStats);
3822
3823 GL_CALL(LinkProgram(fWireRectProgram.fProgram));
3824
3825 GL_CALL_RET(fWireRectProgram.fColorUniform,
3826 GetUniformLocation(fWireRectProgram.fProgram, "u_color"));
3827 GL_CALL_RET(fWireRectProgram.fRectUniform,
3828 GetUniformLocation(fWireRectProgram.fProgram, "u_rect"));
3829 GL_CALL(BindAttribLocation(fWireRectProgram.fProgram, 0, "a_vertex"));
3830
3831 GL_CALL(DeleteShader(vshader));
3832 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003833
3834 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003835}
3836
3837void GrGLGpu::drawDebugWireRect(GrRenderTarget* rt, const SkIRect& rect, GrColor color) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003838 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3839 // visualization.
3840
bsalomon6dea83f2015-12-03 12:58:06 -08003841 this->handleDirtyContext();
3842 if (!fWireRectProgram.fProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003843 if (!this->createWireRectProgram()) {
3844 SkDebugf("Failed to create wire rect program.\n");
3845 return;
3846 }
bsalomon6dea83f2015-12-03 12:58:06 -08003847 }
3848
3849 int w = rt->width();
3850 int h = rt->height();
3851
3852 // Compute the edges of the rectangle (top,left,right,bottom) in NDC space. Must consider
3853 // whether the render target is flipped or not.
3854 GrGLfloat edges[4];
3855 edges[0] = SkIntToScalar(rect.fLeft) + 0.5f;
3856 edges[2] = SkIntToScalar(rect.fRight) - 0.5f;
3857 if (kBottomLeft_GrSurfaceOrigin == rt->origin()) {
3858 edges[1] = h - (SkIntToScalar(rect.fTop) + 0.5f);
3859 edges[3] = h - (SkIntToScalar(rect.fBottom) - 0.5f);
3860 } else {
3861 edges[1] = SkIntToScalar(rect.fTop) + 0.5f;
3862 edges[3] = SkIntToScalar(rect.fBottom) - 0.5f;
3863 }
3864 edges[0] = 2 * edges[0] / w - 1.0f;
3865 edges[1] = 2 * edges[1] / h - 1.0f;
3866 edges[2] = 2 * edges[2] / w - 1.0f;
3867 edges[3] = 2 * edges[3] / h - 1.0f;
3868
3869 GrGLfloat channels[4];
3870 static const GrGLfloat scale255 = 1.f / 255.f;
3871 channels[0] = GrColorUnpackR(color) * scale255;
3872 channels[1] = GrColorUnpackG(color) * scale255;
3873 channels[2] = GrColorUnpackB(color) * scale255;
3874 channels[3] = GrColorUnpackA(color) * scale255;
3875
3876 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
3877 this->flushRenderTarget(glRT, &rect);
3878
3879 GL_CALL(UseProgram(fWireRectProgram.fProgram));
3880 fHWProgramID = fWireRectProgram.fProgram;
3881
cdaltone2e71c22016-04-07 18:13:29 -07003882 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003883
cdaltone2e71c22016-04-07 18:13:29 -07003884 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08003885 attribs->set(this, 0, fWireRectArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3886 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003887 attribs->disableUnusedArrays(this, 0x1);
3888
3889 GL_CALL(Uniform4fv(fWireRectProgram.fRectUniform, 1, edges));
3890 GL_CALL(Uniform4fv(fWireRectProgram.fColorUniform, 1, channels));
3891
3892 GrXferProcessor::BlendInfo blendInfo;
3893 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003894 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6dea83f2015-12-03 12:58:06 -08003895 this->flushColorWrite(true);
3896 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003897 this->flushHWAAState(glRT, false, false);
bsalomon6dea83f2015-12-03 12:58:06 -08003898 this->disableScissor();
3899 GrStencilSettings stencil;
3900 stencil.setDisabled();
3901 this->flushStencil(stencil);
3902
3903 GL_CALL(DrawArrays(GR_GL_LINE_LOOP, 0, 4));
3904}
3905
3906
cdaltone2e71c22016-04-07 18:13:29 -07003907bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -07003908 GrSurface* src,
3909 const SkIRect& srcRect,
3910 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003911 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3912 int progIdx = TextureTargetToCopyProgramIdx(srcTex->target());
3913
3914 if (!fCopyPrograms[progIdx].fProgram) {
3915 if (!this->createCopyProgram(progIdx)) {
3916 SkDebugf("Failed to create copy program.\n");
3917 return false;
3918 }
3919 }
3920
bsalomon6df86402015-06-01 10:41:49 -07003921 int w = srcRect.width();
3922 int h = srcRect.height();
3923
bsalomon6df86402015-06-01 10:41:49 -07003924 GrTextureParams params(SkShader::kClamp_TileMode, GrTextureParams::kNone_FilterMode);
brianosmana6359362016-03-21 06:55:37 -07003925 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003926
bsalomon083617b2016-02-12 12:10:14 -08003927 GrGLIRect dstVP;
3928 this->bindSurfaceFBOForCopy(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3929 this->flushViewport(dstVP);
3930 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
3931
bsalomon6df86402015-06-01 10:41:49 -07003932 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003933
bsalomon7ea33f52015-11-22 14:51:00 -08003934 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3935 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003936
cdaltone2e71c22016-04-07 18:13:29 -07003937 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003938
cdaltone2e71c22016-04-07 18:13:29 -07003939 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
cdalton793dc262016-02-08 10:11:47 -08003940 attribs->set(this, 0, fCopyProgramArrayBuffer, kVec2f_GrVertexAttribType, 2 * sizeof(GrGLfloat),
3941 0);
bsalomond6246342015-06-04 13:57:00 -07003942 attribs->disableUnusedArrays(this, 0x1);
bsalomon6df86402015-06-01 10:41:49 -07003943
3944 // dst rect edges in NDC (-1 to 1)
3945 int dw = dst->width();
3946 int dh = dst->height();
3947 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3948 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3949 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3950 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
3951 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
3952 dy0 = -dy0;
3953 dy1 = -dy1;
3954 }
3955
bsalomone5286e02016-01-14 09:24:09 -08003956 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3957 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3958 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3959 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
bsalomon6df86402015-06-01 10:41:49 -07003960 int sh = src->height();
bsalomon6df86402015-06-01 10:41:49 -07003961 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003962 sy0 = sh - sy0;
3963 sy1 = sh - sy1;
3964 }
3965 // src rect edges in normalized texture space (0 to 1) unless we're using a RECTANGLE texture.
3966 GrGLenum srcTarget = srcTex->target();
3967 if (GR_GL_TEXTURE_RECTANGLE != srcTarget) {
3968 int sw = src->width();
3969 sx0 /= sw;
3970 sx1 /= sw;
3971 sy0 /= sh;
3972 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003973 }
3974
bsalomon7ea33f52015-11-22 14:51:00 -08003975 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3976 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3977 sx1 - sx0, sy1 - sy0, sx0, sy0));
3978 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003979
3980 GrXferProcessor::BlendInfo blendInfo;
3981 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003982 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003983 this->flushColorWrite(true);
bsalomon6df86402015-06-01 10:41:49 -07003984 this->flushDrawFace(GrPipelineBuilder::kBoth_DrawFace);
bsalomon083617b2016-02-12 12:10:14 -08003985 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003986 this->disableScissor();
3987 GrStencilSettings stencil;
3988 stencil.setDisabled();
3989 this->flushStencil(stencil);
3990
3991 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon083617b2016-02-12 12:10:14 -08003992 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, dst);
3993 this->didWriteToSurface(dst, &dstRect);
3994
cdaltone2e71c22016-04-07 18:13:29 -07003995 return true;
bsalomon6df86402015-06-01 10:41:49 -07003996}
3997
3998void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3999 GrSurface* src,
4000 const SkIRect& srcRect,
4001 const SkIPoint& dstPoint) {
4002 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07004003 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004004 this->bindSurfaceFBOForCopy(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08004005 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07004006 SkASSERT(dstTex);
4007 // We modified the bound FBO
4008 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4009 GrGLIRect srcGLRect;
4010 srcGLRect.setRelativeTo(srcVP,
4011 srcRect.fLeft,
4012 srcRect.fTop,
4013 srcRect.width(),
4014 srcRect.height(),
4015 src->origin());
4016
4017 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07004018 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07004019 GrGLint dstY;
4020 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
4021 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
4022 } else {
4023 dstY = dstPoint.fY;
4024 }
bsalomon10528f12015-10-14 12:54:52 -07004025 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08004026 dstPoint.fX, dstY,
4027 srcGLRect.fLeft, srcGLRect.fBottom,
4028 srcGLRect.fWidth, srcGLRect.fHeight));
bsalomon10528f12015-10-14 12:54:52 -07004029 this->unbindTextureFBOForCopy(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004030 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4031 srcRect.width(), srcRect.height());
4032 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004033}
4034
4035bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
4036 GrSurface* src,
4037 const SkIRect& srcRect,
4038 const SkIPoint& dstPoint) {
4039 SkASSERT(can_blit_framebuffer(dst, src, this));
4040 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
4041 srcRect.width(), srcRect.height());
4042 if (dst == src) {
4043 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
4044 return false;
mtklein404b3b22015-05-18 09:29:10 -07004045 }
bsalomon5df6fee2015-05-18 06:26:15 -07004046 }
bsalomon6df86402015-06-01 10:41:49 -07004047
bsalomon6df86402015-06-01 10:41:49 -07004048 GrGLIRect dstVP;
4049 GrGLIRect srcVP;
bsalomon10528f12015-10-14 12:54:52 -07004050 this->bindSurfaceFBOForCopy(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
4051 this->bindSurfaceFBOForCopy(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07004052 // We modified the bound FBO
4053 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
4054 GrGLIRect srcGLRect;
4055 GrGLIRect dstGLRect;
4056 srcGLRect.setRelativeTo(srcVP,
4057 srcRect.fLeft,
4058 srcRect.fTop,
4059 srcRect.width(),
4060 srcRect.height(),
4061 src->origin());
4062 dstGLRect.setRelativeTo(dstVP,
4063 dstRect.fLeft,
4064 dstRect.fTop,
4065 dstRect.width(),
4066 dstRect.height(),
4067 dst->origin());
4068
4069 // BlitFrameBuffer respects the scissor, so disable it.
4070 this->disableScissor();
4071
4072 GrGLint srcY0;
4073 GrGLint srcY1;
4074 // Does the blit need to y-mirror or not?
4075 if (src->origin() == dst->origin()) {
4076 srcY0 = srcGLRect.fBottom;
4077 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4078 } else {
4079 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4080 srcY1 = srcGLRect.fBottom;
4081 }
4082 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4083 srcY0,
4084 srcGLRect.fLeft + srcGLRect.fWidth,
4085 srcY1,
4086 dstGLRect.fLeft,
4087 dstGLRect.fBottom,
4088 dstGLRect.fLeft + dstGLRect.fWidth,
4089 dstGLRect.fBottom + dstGLRect.fHeight,
4090 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon10528f12015-10-14 12:54:52 -07004091 this->unbindTextureFBOForCopy(GR_GL_DRAW_FRAMEBUFFER, dst);
4092 this->unbindTextureFBOForCopy(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004093 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004094 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004095}
4096
cdalton28f45b92016-03-07 13:58:26 -08004097void GrGLGpu::onGetMultisampleSpecs(GrRenderTarget* rt,
4098 const GrStencilSettings& stencil,
4099 int* effectiveSampleCnt,
4100 SkAutoTDeleteArray<SkPoint>* sampleLocations) {
4101 SkASSERT(!rt->hasMixedSamples() || rt->renderTargetPriv().getStencilAttachment() ||
4102 stencil.isDisabled());
4103
4104 this->flushStencil(stencil);
4105 this->flushHWAAState(rt, true, !stencil.isDisabled());
4106 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4107
4108 if (0 != this->caps()->maxRasterSamples()) {
4109 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4110 } else {
4111 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4112 }
4113
4114 SkASSERT(*effectiveSampleCnt >= rt->desc().fSampleCnt);
4115
4116 if (this->caps()->sampleLocationsSupport()) {
4117 sampleLocations->reset(new SkPoint[*effectiveSampleCnt]);
4118 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4119 GrGLfloat pos[2];
4120 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
4121 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
4122 (*sampleLocations)[i].set(pos[0], pos[1]);
4123 } else {
4124 (*sampleLocations)[i].set(pos[0], 1 - pos[1]);
4125 }
4126 }
4127 }
4128}
4129
cdalton231c5fd2015-05-13 12:35:36 -07004130void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004131 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004132 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004133 case kTexture_GrXferBarrierType: {
4134 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4135 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4136 // The render target uses separate storage so no need for glTextureBarrier.
4137 // FIXME: The render target will resolve automatically when its texture is bound,
4138 // but we could resolve only the bounds that will be read if we do it here instead.
4139 return;
4140 }
cdalton9954bc32015-04-29 14:17:00 -07004141 SkASSERT(this->caps()->textureBarrierSupport());
4142 GL_CALL(TextureBarrier());
4143 return;
cdalton231c5fd2015-05-13 12:35:36 -07004144 }
cdalton8917d622015-05-06 13:40:21 -07004145 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004146 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004147 this->caps()->blendEquationSupport());
4148 GL_CALL(BlendBarrier());
4149 return;
bsalomoncb02b382015-08-12 11:14:50 -07004150 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004151 }
4152}
4153
jvanverth88957922015-07-14 11:02:52 -07004154GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -08004155 GrPixelConfig config) {
bsalomon926cb022015-12-17 18:15:11 -08004156 if (!this->caps()->isConfigTexturable(config)) {
4157 return false;
4158 }
bsalomon091f60c2015-11-10 11:54:56 -08004159 GrGLTextureInfo* info = new GrGLTextureInfo;
4160 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004161 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004162 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004163 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4164 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004165 GL_CALL(BindTexture(info->fTarget, info->fID));
bsalomone63ffef2016-02-05 07:17:34 -08004166 fHWBoundTextureUniqueIDs[0] = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004167 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4168 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4169 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4170 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004171
bsalomon76148af2016-01-12 11:13:47 -08004172 GrGLenum internalFormat;
4173 GrGLenum externalFormat;
4174 GrGLenum externalType;
4175
4176 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4177 &externalType)) {
4178 delete info;
4179#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4180 return 0;
4181#else
4182 return reinterpret_cast<GrBackendObject>(nullptr);
4183#endif
4184 }
jvanverth672bb7f2015-07-13 07:19:57 -07004185
bsalomon091f60c2015-11-10 11:54:56 -08004186 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004187 externalType, pixels));
4188
bsalomon091f60c2015-11-10 11:54:56 -08004189#ifdef SK_IGNORE_GL_TEXTURE_TARGET
4190 GrGLuint id = info->fID;
4191 delete info;
4192 return id;
4193#else
4194 return reinterpret_cast<GrBackendObject>(info);
4195#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004196}
4197
jvanverth88957922015-07-14 11:02:52 -07004198bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004199#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004200 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004201#else
4202 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
4203#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004204
4205 GrGLboolean result;
4206 GL_CALL_RET(result, IsTexture(texID));
4207
4208 return (GR_GL_TRUE == result);
4209}
4210
bsalomone63ffef2016-02-05 07:17:34 -08004211void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
bsalomon091f60c2015-11-10 11:54:56 -08004212#ifdef SK_IGNORE_GL_TEXTURE_TARGET
jvanverth672bb7f2015-07-13 07:19:57 -07004213 GrGLuint texID = (GrGLuint)id;
bsalomon091f60c2015-11-10 11:54:56 -08004214#else
4215 const GrGLTextureInfo* info = reinterpret_cast<const GrGLTextureInfo*>(id);
4216 GrGLuint texID = info->fID;
4217#endif
4218
bsalomon67d76202015-11-11 12:40:42 -08004219 if (!abandonTexture) {
4220 GL_CALL(DeleteTextures(1, &texID));
4221 }
bsalomon091f60c2015-11-10 11:54:56 -08004222
4223#ifndef SK_IGNORE_GL_TEXTURE_TARGET
4224 delete info;
4225#endif
jvanverth672bb7f2015-07-13 07:19:57 -07004226}
4227
joshualitt8fd844f2015-12-02 13:36:47 -08004228void GrGLGpu::resetShaderCacheForTesting() const {
4229 fProgramCache->abandon();
4230}
4231
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004232///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004233
cdaltone2e71c22016-04-07 18:13:29 -07004234GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
4235 const GrGLBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004236 GrGLAttribArrayState* attribState;
4237
cdaltone2e71c22016-04-07 18:13:29 -07004238 if (gpu->glCaps().isCoreProfile()) {
4239 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004240 GrGLuint arrayID;
4241 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4242 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004243 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004244 }
cdaltone2e71c22016-04-07 18:13:29 -07004245 if (ibuf) {
4246 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004247 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004248 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004249 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004250 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004251 if (ibuf) {
4252 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4253 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004254 } else {
4255 this->setVertexArrayID(gpu, 0);
4256 }
4257 int attrCount = gpu->glCaps().maxVertexAttributes();
4258 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4259 fDefaultVertexArrayAttribState.resize(attrCount);
4260 }
4261 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004262 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004263 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004264}
bsalomone179a912016-01-20 06:18:10 -08004265
4266bool GrGLGpu::onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams& textureParams,
4267 GrTextureProducer::CopyParams* copyParams) const {
4268 if (textureParams.isTiled() ||
4269 GrTextureParams::kMipMap_FilterMode == textureParams.filterMode()) {
4270 GrGLTexture* glTexture = static_cast<GrGLTexture*>(texture);
4271 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4272 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
4273 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
4274 copyParams->fWidth = texture->width();
4275 copyParams->fHeight = texture->height();
4276 return true;
4277 }
4278 }
4279 return false;
4280}