blob: a126e1a08259fc739ea1536fbbf850e59bcd6a55 [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"
Hal Canary95e3c052017-01-11 12:44:43 -05009
Brian Salomon028a9a52017-05-11 11:39:08 -040010#include <cmath>
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "../private/GrGLSL.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040012#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000013#include "GrBackendSurface.h"
Hal Canary95e3c052017-01-11 12:44:43 -050014#include "GrFixedClip.h"
cdalton397536c2016-03-25 12:15:03 -070015#include "GrGLBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070016#include "GrGLGpuCommandBuffer.h"
Greg Daniel6be35232017-03-01 17:01:09 -050017#include "GrGLSemaphore.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrGLStencilAttachment.h"
bsalomon37dd3312014-11-03 08:47:23 -080019#include "GrGLTextureRenderTarget.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070021#include "GrMesh.h"
Hal Canary95e3c052017-01-11 12:44:43 -050022#include "GrPipeline.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080023#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050024#include "GrShaderCaps.h"
bsalomonafbf2d62014-09-30 12:18:44 -070025#include "GrSurfacePriv.h"
Robert Phillips3798c862017-03-27 11:08:16 -040026#include "GrSurfaceProxyPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070027#include "GrTexturePriv.h"
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +000028#include "GrTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050029#include "SkAutoMalloc.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000030#include "SkMakeUnique.h"
31#include "SkMipMap.h"
32#include "SkPixmap.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000033#include "SkSLCompiler.h"
Hal Canary95e3c052017-01-11 12:44:43 -050034#include "SkStrokeRec.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000035#include "SkTemplates.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070036#include "SkTraceEvent.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000037#include "SkTypes.h"
Hal Canary95e3c052017-01-11 12:44:43 -050038#include "builders/GrGLShaderStringBuilder.h"
Hal Canary95e3c052017-01-11 12:44:43 -050039#include "instanced/GLInstancedRendering.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon@google.com0b77d682011-08-19 13:28:54 +000041#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000042#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044#define SKIP_CACHE_CHECK true
45
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000046#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
47 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
48 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
49 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000051 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
52 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
53 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
54#endif
55
Jim Van Verth32ac83e2016-11-28 15:23:57 -050056//#define USE_NSIGHT
57
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000058///////////////////////////////////////////////////////////////////////////////
59
csmartdaltona7f29642016-07-07 08:49:11 -070060using gr_instanced::InstancedRendering;
61using gr_instanced::GLInstancedRendering;
cdaltonb85a0aa2014-07-21 15:32:44 -070062
Robert Phillipse3302df2017-04-24 07:31:02 -040063using gr_instanced::OpAllocator;
64using gr_instanced::GLOpAllocator;
65
cdalton8917d622015-05-06 13:40:21 -070066static const GrGLenum gXfermodeEquation2Blend[] = {
67 // Basic OpenGL blend equations.
68 GR_GL_FUNC_ADD,
69 GR_GL_FUNC_SUBTRACT,
70 GR_GL_FUNC_REVERSE_SUBTRACT,
71
72 // GL_KHR_blend_equation_advanced.
73 GR_GL_SCREEN,
74 GR_GL_OVERLAY,
75 GR_GL_DARKEN,
76 GR_GL_LIGHTEN,
77 GR_GL_COLORDODGE,
78 GR_GL_COLORBURN,
79 GR_GL_HARDLIGHT,
80 GR_GL_SOFTLIGHT,
81 GR_GL_DIFFERENCE,
82 GR_GL_EXCLUSION,
83 GR_GL_MULTIPLY,
84 GR_GL_HSL_HUE,
85 GR_GL_HSL_SATURATION,
86 GR_GL_HSL_COLOR,
87 GR_GL_HSL_LUMINOSITY
88};
89GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
90GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
91GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
92GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
93GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
94GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
95GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
96GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
97GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
98GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
99GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
100GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
101GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
102GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
103GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
104GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
105GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
106GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700107GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
122 GR_GL_CONSTANT_ALPHA,
123 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124
125 // extended blend coeffs
126 GR_GL_SRC1_COLOR,
127 GR_GL_ONE_MINUS_SRC1_COLOR,
128 GR_GL_SRC1_ALPHA,
129 GR_GL_ONE_MINUS_SRC1_ALPHA,
reed@google.comac10a2d2010-12-22 21:39:39 +0000130};
131
bsalomon861e1032014-12-16 07:33:49 -0800132bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000133 static const bool gCoeffReferencesBlendConst[] = {
134 false,
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 true,
145 true,
146 true,
147 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148
149 // extended blend coeffs
150 false,
151 false,
152 false,
153 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000154 };
155 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700156 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000157
bsalomon@google.com47059542012-06-06 20:51:20 +0000158 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
159 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
160 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
161 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
162 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
163 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
164 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
165 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
166 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
167 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
168 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
169 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
170 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
171 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000172
bsalomon@google.com47059542012-06-06 20:51:20 +0000173 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
174 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
175 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
176 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
178 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700179 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000180}
181
reed@google.comac10a2d2010-12-22 21:39:39 +0000182///////////////////////////////////////////////////////////////////////////////
183
egdanielff1d5472015-09-10 08:37:20 -0700184
bsalomon682c2692015-05-22 14:01:46 -0700185GrGpu* GrGLGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
186 GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -0400187 return Create(reinterpret_cast<const GrGLInterface*>(backendContext), options, context);
188}
189
190GrGpu* GrGLGpu::Create(const GrGLInterface* interface, const GrContextOptions& options,
191 GrContext* context) {
192 sk_sp<const GrGLInterface> glInterface(interface);
bsalomon424cc262015-05-22 10:37:30 -0700193 if (!glInterface) {
194 glInterface.reset(GrGLDefaultInterface());
195 } else {
196 glInterface->ref();
197 }
198 if (!glInterface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700200 }
Jim Van Verth76334772017-05-05 16:46:05 -0400201#ifdef USE_NSIGHT
202 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
203#endif
Hal Canary144caf52016-11-07 17:57:18 -0500204 GrGLContext* glContext = GrGLContext::Create(glInterface.get(), options);
bsalomon424cc262015-05-22 10:37:30 -0700205 if (glContext) {
halcanary385fe4d2015-08-26 13:07:48 -0700206 return new GrGLGpu(glContext, context);
bsalomon424cc262015-05-22 10:37:30 -0700207 }
halcanary96fcdcc2015-08-27 07:41:13 -0700208 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700209}
210
rileya@google.come38160c2012-07-03 18:03:04 +0000211static bool gPrintStartupSpew;
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000212
bsalomon424cc262015-05-22 10:37:30 -0700213GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000214 : GrGpu(context)
cdaltone2e71c22016-04-07 18:13:29 -0700215 , fGLContext(ctx)
216 , fProgramCache(new ProgramCache(this))
217 , fHWProgramID(0)
218 , fTempSrcFBOID(0)
219 , fTempDstFBOID(0)
220 , fStencilClearFBOID(0)
cdalton74b8d322016-04-11 14:47:28 -0700221 , fHWMaxUsedBufferTextureUnit(-1)
cdaltone2e71c22016-04-07 18:13:29 -0700222 , fHWMinSampleShading(0.0) {
223 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
224 fCopyPrograms[i].fProgram = 0;
225 }
brianosman33f6b3f2016-06-02 05:49:21 -0700226 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
227 fMipmapPrograms[i].fProgram = 0;
228 }
Mike Klein31550db2017-06-06 23:29:53 +0000229 fStencilClipClearProgram = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700230
bsalomon424cc262015-05-22 10:37:30 -0700231 SkASSERT(ctx);
232 fCaps.reset(SkRef(ctx->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000233
Brian Salomon1edc5b92016-11-29 13:43:46 -0500234 fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
235 fHWBoundImageStorages.reset(this->caps()->shaderCaps()->maxCombinedImageStorages());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000236
cdaltone2e71c22016-04-07 18:13:29 -0700237 fHWBufferState[kVertex_GrBufferType].fGLTarget = GR_GL_ARRAY_BUFFER;
238 fHWBufferState[kIndex_GrBufferType].fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
239 fHWBufferState[kTexel_GrBufferType].fGLTarget = GR_GL_TEXTURE_BUFFER;
240 fHWBufferState[kDrawIndirect_GrBufferType].fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
241 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
242 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget =
243 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
244 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget =
245 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
246 } else {
247 fHWBufferState[kXferCpuToGpu_GrBufferType].fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
248 fHWBufferState[kXferGpuToCpu_GrBufferType].fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
249 }
250 GR_STATIC_ASSERT(6 == SK_ARRAY_COUNT(fHWBufferState));
251
cdalton74b8d322016-04-11 14:47:28 -0700252 if (this->caps()->shaderCaps()->texelBufferSupport()) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500253 fHWBufferTextures.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
cdalton74b8d322016-04-11 14:47:28 -0700254 }
255
cdaltone2e71c22016-04-07 18:13:29 -0700256 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
257 fPathRendering.reset(new GrGLPathRendering(this));
258 }
259
bsalomon424cc262015-05-22 10:37:30 -0700260 GrGLClearErr(this->glInterface());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000261 if (gPrintStartupSpew) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000262 const GrGLubyte* vendor;
263 const GrGLubyte* renderer;
264 const GrGLubyte* version;
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400265 const GrGLubyte* glslVersion;
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000266 GL_CALL_RET(vendor, GetString(GR_GL_VENDOR));
267 GL_CALL_RET(renderer, GetString(GR_GL_RENDERER));
268 GL_CALL_RET(version, GetString(GR_GL_VERSION));
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400269 GL_CALL_RET(glslVersion, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
bsalomon861e1032014-12-16 07:33:49 -0800270 SkDebugf("------------------------- create GrGLGpu %p --------------\n",
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271 this);
tfarina38406c82014-10-31 07:11:12 -0700272 SkDebugf("------ VENDOR %s\n", vendor);
273 SkDebugf("------ RENDERER %s\n", renderer);
274 SkDebugf("------ VERSION %s\n", version);
Jim Van Verth7f7b9022017-06-26 16:33:54 -0400275 SkDebugf("------ SHADING LANGUAGE VERSION %s\n", glslVersion);
tfarina38406c82014-10-31 07:11:12 -0700276 SkDebugf("------ EXTENSIONS\n");
bsalomon424cc262015-05-22 10:37:30 -0700277 this->glContext().extensions().print();
tfarina38406c82014-10-31 07:11:12 -0700278 SkDebugf("\n");
kkinnunen297aaf92015-02-19 06:32:12 -0800279 SkDebugf("%s", this->glCaps().dump().c_str());
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000280 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000281}
282
bsalomon861e1032014-12-16 07:33:49 -0800283GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700284 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
285 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800286 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700287 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700288 fMipmapProgramArrayBuffer.reset();
Mike Klein31550db2017-06-06 23:29:53 +0000289 fStencilClipClearArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800290
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000291 if (0 != fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000292 // detach the current program so there is no confusion on OpenGL's part
293 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000294 GL_CALL(UseProgram(0));
295 }
296
egdanield803f272015-03-18 13:01:52 -0700297 if (0 != fTempSrcFBOID) {
298 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800299 }
egdanield803f272015-03-18 13:01:52 -0700300 if (0 != fTempDstFBOID) {
301 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -0800302 }
egdanield803f272015-03-18 13:01:52 -0700303 if (0 != fStencilClearFBOID) {
304 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
bsalomondd3143b2015-02-23 09:27:45 -0800305 }
egdaniel0f5f9672015-02-03 11:10:51 -0800306
bsalomon7ea33f52015-11-22 14:51:00 -0800307 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
308 if (0 != fCopyPrograms[i].fProgram) {
309 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
310 }
bsalomon6df86402015-06-01 10:41:49 -0700311 }
bsalomon6dea83f2015-12-03 12:58:06 -0800312
brianosman33f6b3f2016-06-02 05:49:21 -0700313 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
314 if (0 != fMipmapPrograms[i].fProgram) {
315 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
316 }
317 }
318
Mike Klein31550db2017-06-06 23:29:53 +0000319 if (0 != fStencilClipClearProgram) {
320 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -0800321 }
322
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000323 delete fProgramCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700324}
325
bsalomon6e2aad42016-04-01 11:54:31 -0700326void GrGLGpu::disconnect(DisconnectType type) {
327 INHERITED::disconnect(type);
328 if (DisconnectType::kCleanup == type) {
329 if (fHWProgramID) {
330 GL_CALL(UseProgram(0));
331 }
332 if (fTempSrcFBOID) {
333 GL_CALL(DeleteFramebuffers(1, &fTempSrcFBOID));
334 }
335 if (fTempDstFBOID) {
336 GL_CALL(DeleteFramebuffers(1, &fTempDstFBOID));
337 }
338 if (fStencilClearFBOID) {
339 GL_CALL(DeleteFramebuffers(1, &fStencilClearFBOID));
340 }
bsalomon6e2aad42016-04-01 11:54:31 -0700341 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
342 if (fCopyPrograms[i].fProgram) {
343 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
344 }
345 }
brianosman33f6b3f2016-06-02 05:49:21 -0700346 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
347 if (fMipmapPrograms[i].fProgram) {
348 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
349 }
350 }
Mike Klein31550db2017-06-06 23:29:53 +0000351 if (fStencilClipClearProgram) {
352 GL_CALL(DeleteProgram(fStencilClipClearProgram));
bsalomon6e2aad42016-04-01 11:54:31 -0700353 }
bsalomon6e2aad42016-04-01 11:54:31 -0700354 } else {
355 if (fProgramCache) {
356 fProgramCache->abandon();
357 }
358 }
359
360 delete fProgramCache;
361 fProgramCache = nullptr;
362
bsalomonc8dc1f72014-08-21 13:02:13 -0700363 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700364 fTempSrcFBOID = 0;
365 fTempDstFBOID = 0;
366 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700367 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800368 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
369 fCopyPrograms[i].fProgram = 0;
370 }
brianosman33f6b3f2016-06-02 05:49:21 -0700371 fMipmapProgramArrayBuffer.reset();
372 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
373 fMipmapPrograms[i].fProgram = 0;
374 }
Mike Klein31550db2017-06-06 23:29:53 +0000375 fStencilClipClearProgram = 0;
376 fStencilClipClearArrayBuffer.reset();
jvanverthe9c0fc62015-04-29 11:18:05 -0700377 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700378 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700379 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000380}
381
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000382///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000383
bsalomon861e1032014-12-16 07:33:49 -0800384void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000385 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400386 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000387 GL_CALL(Disable(GR_GL_DEPTH_TEST));
388 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000389
Brian Salomonf0861672017-05-08 11:10:10 -0400390 // We don't use face culling.
391 GL_CALL(Disable(GR_GL_CULL_FACE));
392 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
393 // just set this to the default for self-consistency.
394 GL_CALL(FrontFace(GR_GL_CCW));
395
cdaltone2e71c22016-04-07 18:13:29 -0700396 fHWBufferState[kTexel_GrBufferType].invalidate();
397 fHWBufferState[kDrawIndirect_GrBufferType].invalidate();
398 fHWBufferState[kXferCpuToGpu_GrBufferType].invalidate();
399 fHWBufferState[kXferGpuToCpu_GrBufferType].invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700400
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000401 if (kGL_GrGLStandard == this->glStandard()) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500402#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000403 // Desktop-only state that we never change
404 if (!this->glCaps().isCoreProfile()) {
405 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
406 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
407 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
408 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
409 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
410 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
411 }
412 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
413 // core profile. This seems like a bug since the core spec removes any mention of
414 // GL_ARB_imaging.
415 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
416 GL_CALL(Disable(GR_GL_COLOR_TABLE));
417 }
418 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400419
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400420 if (this->caps()->wireframeMode()) {
421 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
422 } else {
423 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
424 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500425#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000426 // Since ES doesn't support glPointSize at all we always use the VS to
427 // set the point size
428 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
429
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000430 }
joshualitt58162332014-08-01 06:44:53 -0700431
432 if (kGLES_GrGLStandard == this->glStandard() &&
bsalomon424cc262015-05-22 10:37:30 -0700433 this->hasExtension("GL_ARM_shader_framebuffer_fetch")) {
joshualitt58162332014-08-01 06:44:53 -0700434 // The arm extension requires specifically enabling MSAA fetching per sample.
435 // On some devices this may have a perf hit. Also multiple render targets are disabled
436 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE_ARM));
437 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000438 fHWWriteToColor = kUnknown_TriState;
439 // we only ever use lines in hairline mode
440 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700441 GL_CALL(Disable(GR_GL_DITHER));
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000442 }
edisonn@google.comba669992013-06-28 16:03:21 +0000443
egdanielb414f252014-07-29 13:15:47 -0700444 if (resetBits & kMSAAEnable_GrGLBackendState) {
445 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700446
egdanieleed519e2016-01-15 11:36:18 -0800447 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800448 if (0 != this->caps()->maxRasterSamples()) {
449 fHWRasterMultisampleEnabled = kUnknown_TriState;
450 fHWNumRasterSamples = 0;
451 }
452
453 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
454 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700455 GL_CALL(CoverageModulation(GR_GL_RGBA));
456 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000457 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000458
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000459 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400460 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000461
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000462 if (resetBits & kTextureBinding_GrGLBackendState) {
bsalomon1c63bf62014-07-22 13:09:46 -0700463 for (int s = 0; s < fHWBoundTextureUniqueIDs.count(); ++s) {
Robert Phillips294870f2016-11-11 12:38:40 -0500464 fHWBoundTextureUniqueIDs[s].makeInvalid();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000465 }
cdalton74b8d322016-04-11 14:47:28 -0700466 for (int b = 0; b < fHWBufferTextures.count(); ++b) {
467 SkASSERT(this->caps()->shaderCaps()->texelBufferSupport());
468 fHWBufferTextures[b].fKnownBound = false;
469 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500470 for (int i = 0; i < fHWBoundImageStorages.count(); ++i) {
Brian Salomon8b7a7dd2016-11-29 15:29:41 -0500471 SkASSERT(this->caps()->shaderCaps()->imageLoadStoreSupport());
Brian Salomonf9f45122016-11-29 11:59:17 -0500472 fHWBoundImageStorages[i].fTextureUniqueID.makeInvalid();
473 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000474 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000475
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000476 if (resetBits & kBlend_GrGLBackendState) {
477 fHWBlendState.invalidate();
478 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000479
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000480 if (resetBits & kView_GrGLBackendState) {
481 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700482 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000483 fHWViewport.invalidate();
484 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000486 if (resetBits & kStencil_GrGLBackendState) {
487 fHWStencilSettings.invalidate();
488 fHWStencilTestEnabled = kUnknown_TriState;
489 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000490
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000491 // Vertex
492 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700493 fHWVertexArrayState.invalidate();
494 fHWBufferState[kVertex_GrBufferType].invalidate();
495 fHWBufferState[kIndex_GrBufferType].invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000498 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500499 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700500 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000501 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000502
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000503 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700504 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700505 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000506 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000507 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000508
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000509 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000510 if (resetBits & kPixelStore_GrGLBackendState) {
511 if (this->glCaps().unpackRowLengthSupport()) {
512 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
513 }
514 if (this->glCaps().packRowLengthSupport()) {
515 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
516 }
517 if (this->glCaps().unpackFlipYSupport()) {
518 GL_CALL(PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
519 }
520 if (this->glCaps().packFlipYSupport()) {
521 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
522 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000523 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000524
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000525 if (resetBits & kProgram_GrGLBackendState) {
526 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000527 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000528}
529
Brian Salomond17f6582017-07-19 18:28:58 -0400530static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
531 GrGLTexture::IDDesc* idDesc) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000532 const GrGLTextureInfo* info = backendTex.getGLTextureInfo();
bsalomon091f60c2015-11-10 11:54:56 -0800533 if (!info || !info->fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400534 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800535 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000536
Brian Salomond17f6582017-07-19 18:28:58 -0400537 idDesc->fInfo = *info;
bsalomon7ea33f52015-11-22 14:51:00 -0800538
Brian Salomond17f6582017-07-19 18:28:58 -0400539 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
540 if (!caps.shaderCaps()->externalTextureSupport()) {
541 return false;
542 }
543 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
544 if (!caps.rectangleTextureSupport()) {
545 return false;
546 }
547 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
548 return false;
549 }
550 return true;
551}
552
553sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips7294b852017-08-01 13:51:44 +0000554 GrSurfaceOrigin origin,
Brian Salomond17f6582017-07-19 18:28:58 -0400555 GrWrapOwnership ownership) {
bsalomonb15b4c12014-10-29 12:41:57 -0700556 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400557 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
558 return nullptr;
559 }
560 if (kBorrow_GrWrapOwnership == ownership) {
561 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
562 } else {
563 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
564 }
bsalomone5286e02016-01-14 09:24:09 -0800565
Brian Salomond17f6582017-07-19 18:28:58 -0400566 GrSurfaceDesc surfDesc;
567 surfDesc.fFlags = kNone_GrSurfaceFlags;
568 surfDesc.fWidth = backendTex.width();
569 surfDesc.fHeight = backendTex.height();
570 surfDesc.fConfig = backendTex.config();
571 surfDesc.fSampleCnt = 0;
Robert Phillips7294b852017-08-01 13:51:44 +0000572 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
573 // assuming the old behaviour, which is that backend textures are always
574 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
575 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
576 if (kDefault_GrSurfaceOrigin == origin) {
577 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
578 } else {
579 surfDesc.fOrigin = origin;
580 }
Brian Salomond17f6582017-07-19 18:28:58 -0400581 return GrGLTexture::MakeWrapped(this, surfDesc, idDesc);
582}
583
584sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Robert Phillips7294b852017-08-01 13:51:44 +0000585 GrSurfaceOrigin origin,
Brian Salomond17f6582017-07-19 18:28:58 -0400586 int sampleCnt,
587 GrWrapOwnership ownership) {
588 GrGLTexture::IDDesc idDesc;
589 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800590 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800591 }
bsalomone5286e02016-01-14 09:24:09 -0800592
Brian Salomond17f6582017-07-19 18:28:58 -0400593 // We don't support rendering to a EXTERNAL texture.
594 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800595 return nullptr;
596 }
bsalomon10528f12015-10-14 12:54:52 -0700597
Brian Osman766fcbb2017-03-13 09:33:09 -0400598 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000599 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400600 } else {
601 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800602 }
bsalomonb15b4c12014-10-29 12:41:57 -0700603
Ben Wagner18b61f92016-10-25 10:44:02 -0400604 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400605 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000606 surfDesc.fWidth = backendTex.width();
607 surfDesc.fHeight = backendTex.height();
608 surfDesc.fConfig = backendTex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400609 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, backendTex.config());
Robert Phillips7294b852017-08-01 13:51:44 +0000610 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
611 // assuming the old behaviour, which is that backend textures are always
612 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
613 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
614 if (kDefault_GrSurfaceOrigin == origin) {
615 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
616 } else {
617 surfDesc.fOrigin = origin;
618 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000619
Brian Salomond17f6582017-07-19 18:28:58 -0400620 GrGLRenderTarget::IDDesc rtIDDesc;
621 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
622 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000623 }
Brian Salomond17f6582017-07-19 18:28:58 -0400624 sk_sp<GrGLTextureRenderTarget> texRT(
625 GrGLTextureRenderTarget::MakeWrapped(this, surfDesc, idDesc, rtIDDesc));
626 texRT->baseLevelWasBoundToFBO();
627 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000628}
629
Robert Phillips7294b852017-08-01 13:51:44 +0000630sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
631 GrSurfaceOrigin origin) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000632 const GrGLFramebufferInfo* info = backendRT.getGLFramebufferInfo();
633 if (!info) {
634 return nullptr;
635 }
636
bsalomonb15b4c12014-10-29 12:41:57 -0700637 GrGLRenderTarget::IDDesc idDesc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000638 idDesc.fRTFBOID = info->fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700639 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700640 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500641 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
csmartdaltonf9635992016-08-10 11:09:07 -0700642 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000643
bsalomonb15b4c12014-10-29 12:41:57 -0700644 GrSurfaceDesc desc;
Robert Phillips7294b852017-08-01 13:51:44 +0000645 desc.fConfig = backendRT.config();
Brian Salomon0ec981b2017-05-15 13:48:50 -0400646 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000647 desc.fWidth = backendRT.width();
648 desc.fHeight = backendRT.height();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400649 desc.fSampleCnt = this->caps()->getSampleCount(backendRT.sampleCnt(), backendRT.config());
Robert Phillips7294b852017-08-01 13:51:44 +0000650 SkASSERT(kDefault_GrSurfaceOrigin != origin);
651 desc.fOrigin = origin;
bsalomonb15b4c12014-10-29 12:41:57 -0700652
Greg Danielbcf612b2017-05-01 13:50:58 +0000653 return GrGLRenderTarget::MakeWrapped(this, desc, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000654}
655
Greg Daniel7ef28f32017-04-20 16:41:55 +0000656sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillips7294b852017-08-01 13:51:44 +0000657 GrSurfaceOrigin origin,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000658 int sampleCnt) {
659 const GrGLTextureInfo* info = tex.getGLTextureInfo();
ericrkf7b8b8a2016-02-24 14:49:51 -0800660 if (!info || !info->fID) {
661 return nullptr;
662 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800663
jvanverthdb1adf52016-05-03 05:37:45 -0700664 GrGLTextureInfo texInfo;
jvanverthdb1adf52016-05-03 05:37:45 -0700665 texInfo = *info;
ericrkf7b8b8a2016-02-24 14:49:51 -0800666
jvanverthdb1adf52016-05-03 05:37:45 -0700667 if (GR_GL_TEXTURE_RECTANGLE != texInfo.fTarget &&
668 GR_GL_TEXTURE_2D != texInfo.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800669 // Only texture rectangle and texture 2d are supported. We do not check whether texture
670 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
671 // we assume the necessary support exists.
672 return nullptr;
673 }
674
Ben Wagner18b61f92016-10-25 10:44:02 -0400675 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000676 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
677 surfDesc.fWidth = tex.width();
678 surfDesc.fHeight = tex.height();
679 surfDesc.fConfig = tex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400680 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, tex.config());
Robert Phillips7294b852017-08-01 13:51:44 +0000681 // FIXME: this should be calling resolve_origin(), but Chrome code is currently
682 // assuming the old behaviour, which is that backend textures are always
683 // BottomLeft, even for non-RT's. Once Chrome is fixed, change this to:
684 // glTexDesc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
685 if (kDefault_GrSurfaceOrigin == origin) {
686 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
687 } else {
688 surfDesc.fOrigin = origin;
689 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800690
691 GrGLRenderTarget::IDDesc rtIDDesc;
jvanverthdb1adf52016-05-03 05:37:45 -0700692 if (!this->createRenderTargetObjects(surfDesc, texInfo, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800693 return nullptr;
694 }
bungeman6bd52842016-10-27 09:30:08 -0700695 return GrGLRenderTarget::MakeWrapped(this, surfDesc, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800696}
697
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000698////////////////////////////////////////////////////////////////////////////////
ericrkf7b8b8a2016-02-24 14:49:51 -0800699
Robert Phillips7294b852017-08-01 13:51:44 +0000700bool GrGLGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -0800701 GrPixelConfig srcConfig,
bsalomonf0674512015-07-28 13:26:15 -0700702 DrawPreference* drawPreference,
703 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Salomon9bada542017-06-12 12:09:30 -0400704 if (SkToBool(dstSurface->asRenderTarget())) {
705 if (this->glCaps().useDrawInsteadOfAllRenderTargetWrites()) {
706 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
707 }
708 }
709
710 GrGLTexture* texture = static_cast<GrGLTexture*>(dstSurface->asTexture());
711
712 if (texture) {
bsalomone5286e02016-01-14 09:24:09 -0800713 if (GR_GL_TEXTURE_EXTERNAL == texture->target()) {
714 // We don't currently support writing pixels to EXTERNAL textures.
bsalomon7ea33f52015-11-22 14:51:00 -0800715 return false;
716 }
Brian Salomon9bada542017-06-12 12:09:30 -0400717 if (GrPixelConfigIsUnorm(texture->config()) && texture->hasBaseLevelBeenBoundToFBO() &&
718 this->glCaps().disallowTexSubImageForUnormConfigTexturesEverBoundToFBO() &&
719 (width < dstSurface->width() || height < dstSurface->height())) {
720 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
721 }
722 } else {
723 // This subclass only allows writes to textures. If the dst is not a texture we have to draw
724 // into it. We could use glDrawPixels on GLs that have it, but we don't today.
725 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700726 }
727
Brian Osman33910292017-04-18 14:38:53 -0400728 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
729 if (dstSurface->asRenderTarget() && dstSurface->asRenderTarget()->numColorSamples() > 0) {
730 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
731 }
732
bsalomon16921ec2015-07-30 15:34:56 -0700733 if (GrPixelConfigIsSRGB(dstSurface->config()) != GrPixelConfigIsSRGB(srcConfig)) {
734 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
735 }
736
bsalomon6c9cd552016-01-22 07:17:34 -0800737 // Start off assuming no swizzling
738 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
739 tempDrawInfo->fWriteConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -0700740
741 // These settings we will always want if a temp draw is performed. Initially set the config
742 // to srcConfig, though that may be modified if we decide to do a R/G swap.
743 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
744 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
745 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
746 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
747 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
748 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
749
750 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
751
752 if (configsAreRBSwaps) {
753 if (!this->caps()->isConfigTexturable(srcConfig)) {
754 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
755 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800756 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
757 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomon88c7b982015-07-31 11:20:16 -0700758 } else if (this->glCaps().rgba8888PixelsOpsAreSlow() &&
759 kRGBA_8888_GrPixelConfig == srcConfig) {
bsalomonf0674512015-07-28 13:26:15 -0700760 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
761 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800762 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
763 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700764 } else if (kGLES_GrGLStandard == this->glStandard() &&
765 this->glCaps().bgraIsInternalFormat()) {
766 // The internal format and external formats must match texture uploads so we can't
767 // swizzle while uploading when BGRA is a distinct internal format.
768 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
769 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
bsalomon6c9cd552016-01-22 07:17:34 -0800770 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
771 tempDrawInfo->fWriteConfig = dstSurface->config();
bsalomonf0674512015-07-28 13:26:15 -0700772 }
773 }
774
Robert Phillips7294b852017-08-01 13:51:44 +0000775 if (!this->glCaps().unpackFlipYSupport() &&
776 kBottomLeft_GrSurfaceOrigin == dstSurface->origin()) {
bsalomonf0674512015-07-28 13:26:15 -0700777 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
778 }
779
780 return true;
781}
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000782
jvanverth17aa0472016-01-05 10:41:27 -0800783static bool check_write_and_transfer_input(GrGLTexture* glTex, GrSurface* surface,
784 GrPixelConfig config) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700785 if (!glTex) {
786 return false;
787 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000788
bsalomon16921ec2015-07-30 15:34:56 -0700789 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
790 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
791 return false;
792 }
793
bsalomone5286e02016-01-14 09:24:09 -0800794 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
795 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800796 return false;
797 }
798
jvanverth17aa0472016-01-05 10:41:27 -0800799 return true;
800}
801
Robert Phillips7294b852017-08-01 13:51:44 +0000802bool GrGLGpu::onWritePixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800803 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800804 GrPixelConfig config,
Robert Phillips590533f2017-07-11 14:22:35 -0400805 const GrMipLevel texels[],
806 int mipLevelCount) {
jvanverth17aa0472016-01-05 10:41:27 -0800807 GrGLTexture* glTex = static_cast<GrGLTexture*>(surface->asTexture());
808
809 if (!check_write_and_transfer_input(glTex, surface, config)) {
810 return false;
811 }
812
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000813 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -0700814 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000815
Robert Phillips92de6312017-05-23 07:43:48 -0400816 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(),
Robert Phillips7294b852017-08-01 13:51:44 +0000817 glTex->origin(), glTex->target(), kWrite_UploadType,
Robert Phillips590533f2017-07-11 14:22:35 -0400818 left, top, width, height, config, texels, mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000819}
820
bsalomonf46a1242015-12-15 12:37:38 -0800821// For GL_[UN]PACK_ALIGNMENT.
822static inline GrGLint config_alignment(GrPixelConfig config) {
bsalomonf46a1242015-12-15 12:37:38 -0800823 switch (config) {
824 case kAlpha_8_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500825 case kGray_8_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800826 return 1;
827 case kRGB_565_GrPixelConfig:
828 case kRGBA_4444_GrPixelConfig:
829 case kAlpha_half_GrPixelConfig:
830 case kRGBA_half_GrPixelConfig:
831 return 2;
832 case kRGBA_8888_GrPixelConfig:
833 case kBGRA_8888_GrPixelConfig:
834 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700835 case kSBGRA_8888_GrPixelConfig:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500836 case kRGBA_8888_sint_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800837 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500838 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800839 return 4;
csmartdalton6aa0e112017-02-08 16:14:11 -0500840 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800841 return 0;
842 }
csmartdalton6aa0e112017-02-08 16:14:11 -0500843 SkFAIL("Invalid pixel config");
844 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800845}
846
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400847bool GrGLGpu::onTransferPixels(GrTexture* texture,
848 int left, int top, int width, int height,
849 GrPixelConfig config, GrBuffer* transferBuffer,
850 size_t offset, size_t rowBytes) {
851 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
852 GrPixelConfig texConfig = glTex->config();
853 SkASSERT(this->caps()->isConfigTexturable(texConfig));
854
855 if (!check_write_and_transfer_input(glTex, texture, config)) {
856 return false;
857 }
858
859 if (width <= 0 || width > SK_MaxS32 || height <= 0 || height > SK_MaxS32) {
860 return false;
861 }
862
863 this->setScratchTextureUnit();
864 GL_CALL(BindTexture(glTex->target(), glTex->textureID()));
865
866 SkASSERT(!transferBuffer->isMapped());
867 SkASSERT(!transferBuffer->isCPUBacked());
868 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
869 this->bindBuffer(kXferCpuToGpu_GrBufferType, glBuffer);
870
Greg Daniel660cc992017-06-26 14:55:05 -0400871 SkDEBUGCODE(
872 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
873 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
874 SkASSERT(bounds.contains(subRect));
875 )
876
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400877 size_t bpp = GrBytesPerPixel(config);
878 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400879 if (!rowBytes) {
880 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400881 }
Greg Daniel660cc992017-06-26 14:55:05 -0400882 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700883 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400884 return false;
885 }
886
887 bool restoreGLRowLength = false;
888 if (trimRowBytes != rowBytes) {
889 // we should have checked for this support already
890 SkASSERT(this->glCaps().unpackRowLengthSupport());
891 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
892 restoreGLRowLength = true;
893 }
894
895 // Internal format comes from the texture desc.
896 GrGLenum internalFormat;
897 // External format and type come from the upload data.
898 GrGLenum externalFormat;
899 GrGLenum externalType;
900 if (!this->glCaps().getTexImageFormats(texConfig, config, &internalFormat,
901 &externalFormat, &externalType)) {
902 return false;
903 }
904
905 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
906 GL_CALL(TexSubImage2D(glTex->target(),
907 0,
908 left, top,
909 width,
910 height,
911 externalFormat, externalType,
912 pixels));
913
914 if (restoreGLRowLength) {
915 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
916 }
917
918 return true;
919}
920
cblume55f2d2d2016-02-26 13:20:48 -0800921/**
922 * Creates storage space for the texture and fills it with texels.
923 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400924 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800925 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800926 * @param caps The capabilities of the GL device.
brianosman81a84852016-09-12 09:05:14 -0700927 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
928 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800929 * @param externalFormat The data format used for the external storage of the texture.
930 * @param externalType The type of the data used for the external storage of the texture.
931 * @param texels The texel data of the texture being created.
932 * @param baseWidth The width of the texture's base mipmap level
933 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800934 */
Robert Phillips92de6312017-05-23 07:43:48 -0400935static bool allocate_and_populate_texture(GrPixelConfig config,
936 const GrGLInterface& interface,
937 const GrGLCaps& caps,
938 GrGLenum target,
939 GrGLenum internalFormat,
940 GrGLenum internalFormatForTexStorage,
941 GrGLenum externalFormat,
942 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -0400943 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -0400944 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -0800945 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -0800946
Brian Salomond1eaf492017-05-18 10:02:08 -0400947 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
cblume790d5132016-02-29 11:13:29 -0800948 // We can only use TexStorage if we know we will not later change the storage requirements.
949 // This means if we may later want to add mipmaps, we cannot use TexStorage.
950 // Right now, we cannot know if we will later add mipmaps or not.
951 // The only time we can use TexStorage is when we already have the
Brian Salomonbf7b6202016-11-11 16:08:03 -0500952 // mipmaps or are using a format incompatible with MIP maps.
Robert Phillips590533f2017-07-11 14:22:35 -0400953 useTexStorage &= mipLevelCount > 1 || GrPixelConfigIsSint(config);
cblume790d5132016-02-29 11:13:29 -0800954
955 if (useTexStorage) {
956 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -0800957 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -0400958 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -0400959 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400960 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -0800961 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800962 return false;
cblume790d5132016-02-29 11:13:29 -0800963 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400964 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -0800965 const void* currentMipData = texels[currentMipLevel].fPixels;
966 if (currentMipData == nullptr) {
967 continue;
968 }
969 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -0400970 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
971 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -0800972
973 GR_GL_CALL(&interface,
974 TexSubImage2D(target,
975 currentMipLevel,
976 0, // left
977 0, // top
978 currentWidth,
979 currentHeight,
980 externalFormat, externalType,
981 currentMipData));
982 }
bsalomone699d0c2016-03-09 06:25:15 -0800983 return true;
cblume790d5132016-02-29 11:13:29 -0800984 }
985 } else {
Robert Phillips590533f2017-07-11 14:22:35 -0400986 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -0800987 GL_ALLOC_CALL(&interface,
988 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -0800989 0,
cblume790d5132016-02-29 11:13:29 -0800990 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -0800991 baseWidth,
992 baseHeight,
cblume790d5132016-02-29 11:13:29 -0800993 0, // border
994 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -0800995 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -0400996 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -0800997 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -0800998 return false;
999 }
1000 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001001 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001002 int twoToTheMipLevel = 1 << currentMipLevel;
1003 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1004 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1005 const void* currentMipData = texels[currentMipLevel].fPixels;
1006 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1007 // This will allocate texture memory which we can later populate.
1008 GL_ALLOC_CALL(&interface,
1009 TexImage2D(target,
1010 currentMipLevel,
1011 internalFormat,
1012 currentWidth,
1013 currentHeight,
1014 0, // border
1015 externalFormat, externalType,
1016 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001017 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001018 if (error != GR_GL_NO_ERROR) {
1019 return false;
1020 }
cblume790d5132016-02-29 11:13:29 -08001021 }
cblume55f2d2d2016-02-26 13:20:48 -08001022 }
1023 }
bsalomone699d0c2016-03-09 06:25:15 -08001024 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001025}
1026
1027/**
cblume55f2d2d2016-02-26 13:20:48 -08001028 * After a texture is created, any state which was altered during its creation
1029 * needs to be restored.
1030 *
1031 * @param interface The GL interface to use.
1032 * @param caps The capabilities of the GL device.
1033 * @param restoreGLRowLength Should the row length unpacking be restored?
1034 * @param glFlipY Did GL flip the texture vertically?
1035 */
1036static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
1037 bool restoreGLRowLength, bool glFlipY) {
1038 if (restoreGLRowLength) {
1039 SkASSERT(caps.unpackRowLengthSupport());
1040 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1041 }
1042 if (glFlipY) {
1043 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_FALSE));
1044 }
1045}
1046
Brian Salomond1eaf492017-05-18 10:02:08 -04001047bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
1048 GrSurfaceOrigin texOrigin, GrGLenum target, UploadType uploadType,
1049 int left, int top, int width, int height, GrPixelConfig dataConfig,
Robert Phillips590533f2017-07-11 14:22:35 -04001050 const GrMipLevel texels[], int mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001051 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001052 SkDEBUGCODE(
1053 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1054 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1055 SkASSERT(bounds.contains(subRect));
1056 )
Robert Phillips590533f2017-07-11 14:22:35 -04001057 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001058 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001059
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001060 // unbind any previous transfer buffer
1061 auto& xferBufferState = fHWBufferState[kXferCpuToGpu_GrBufferType];
1062 if (!xferBufferState.fBoundBufferUniqueID.isInvalid()) {
1063 GL_CALL(BindBuffer(xferBufferState.fGLTarget, 0));
1064 xferBufferState.invalidate();
1065 }
1066
cblume55f2d2d2016-02-26 13:20:48 -08001067 // texels is const.
1068 // But we may need to flip the texture vertically to prepare it.
1069 // Rather than flip in place and alter the incoming data,
1070 // we allocate a new buffer to flip into.
1071 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001072 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1073
1074 if (mipLevelCount) {
1075 texelsShallowCopy.reset(mipLevelCount);
1076 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1077 }
cblume55f2d2d2016-02-26 13:20:48 -08001078
Robert Phillips590533f2017-07-11 14:22:35 -04001079 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001080 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels);
cblume55f2d2d2016-02-26 13:20:48 -08001081 }
1082
1083 const GrGLInterface* interface = this->glInterface();
1084 const GrGLCaps& caps = this->glCaps();
1085
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001086 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001087
1088 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001089 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001090 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001091
bsalomon5b30c6f2015-12-17 14:17:34 -08001092 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001093 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001094 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001095 GrGLenum externalFormat;
1096 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001097 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1098 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001099 return false;
1100 }
brianosman81a84852016-09-12 09:05:14 -07001101 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001102 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001103
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001104 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001105 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001106 * because our srcData has extra bytes past each row. If so, we need
1107 * to trim those off here, since GL ES may not let us specify
1108 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001109 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001110 bool restoreGLRowLength = false;
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001111 bool swFlipY = false;
1112 bool glFlipY = false;
cblume55f2d2d2016-02-26 13:20:48 -08001113
Robert Phillips590533f2017-07-11 14:22:35 -04001114 if (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount) {
cblume55f2d2d2016-02-26 13:20:48 -08001115 if (caps.unpackFlipYSupport()) {
1116 glFlipY = true;
1117 } else {
1118 swFlipY = true;
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001119 }
cblume55f2d2d2016-02-26 13:20:48 -08001120 }
1121
1122 // in case we need a temporary, trimmed copy of the src pixels
1123 SkAutoSMalloc<128 * 128> tempStorage;
1124
1125 // find the combined size of all the mip levels and the relative offset of
1126 // each into the collective buffer
1127 size_t combined_buffer_size = 0;
Robert Phillips590533f2017-07-11 14:22:35 -04001128 SkTArray<size_t> individual_mip_offsets(mipLevelCount);
1129 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001130 int twoToTheMipLevel = 1 << currentMipLevel;
1131 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1132 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1133 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1134 individual_mip_offsets.push_back(combined_buffer_size);
1135 combined_buffer_size += trimmedSize;
1136 }
1137 char* buffer = (char*)tempStorage.reset(combined_buffer_size);
1138
Robert Phillips590533f2017-07-11 14:22:35 -04001139 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001140 int twoToTheMipLevel = 1 << currentMipLevel;
1141 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1142 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1143 const size_t trimRowBytes = currentWidth * bpp;
1144
1145 /*
1146 * check whether to allocate a temporary buffer for flipping y or
1147 * because our srcData has extra bytes past each row. If so, we need
1148 * to trim those off here, since GL ES may not let us specify
1149 * GL_UNPACK_ROW_LENGTH.
1150 */
1151 restoreGLRowLength = false;
1152
Greg Daniel660cc992017-06-26 14:55:05 -04001153 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes ?
1154 texelsShallowCopy[currentMipLevel].fRowBytes :
1155 trimRowBytes;
1156
ericrk154349b2016-05-10 14:36:53 -07001157 // TODO: This optimization should be enabled with or without mips.
1158 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1159 // mip level, before calling glTexImage2D.
Robert Phillips590533f2017-07-11 14:22:35 -04001160 const bool usesMips = mipLevelCount > 1;
ericrk154349b2016-05-10 14:36:53 -07001161 if (caps.unpackRowLengthSupport() && !swFlipY && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001162 // can't use this for flipping, only non-neg values allowed. :(
1163 if (rowBytes != trimRowBytes) {
1164 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001165 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001166 restoreGLRowLength = true;
1167 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001168 } else if (trimRowBytes != rowBytes || swFlipY) {
1169 // copy data into our new storage, skipping the trailing bytes
1170 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
1171 if (swFlipY && currentHeight >= 1) {
1172 src += (currentHeight - 1) * rowBytes;
Jim Van Verthf7037d02017-06-21 10:52:43 -04001173 }
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001174 char* dst = buffer + individual_mip_offsets[currentMipLevel];
1175 for (int y = 0; y < currentHeight; y++) {
1176 memcpy(dst, src, trimRowBytes);
1177 if (swFlipY) {
1178 src -= rowBytes;
1179 } else {
1180 src += rowBytes;
1181 }
1182 dst += trimRowBytes;
1183 }
1184 // now point data to our copied version
1185 texelsShallowCopy[currentMipLevel].fPixels = buffer +
1186 individual_mip_offsets[currentMipLevel];
1187 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001188 }
bsalomone699d0c2016-03-09 06:25:15 -08001189 }
1190
Robert Phillips590533f2017-07-11 14:22:35 -04001191 if (mipLevelCount) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001192 if (glFlipY) {
cblume55f2d2d2016-02-26 13:20:48 -08001193 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_FLIP_Y, GR_GL_TRUE));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001194 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001195 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001196 }
cblume55f2d2d2016-02-26 13:20:48 -08001197
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001198 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001199 if (kNewTexture_UploadType == uploadType) {
1200 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001201 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001202 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001203 internalFormatForTexStorage, externalFormat, externalType,
1204 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001205 } else {
1206 succeeded = false;
1207 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001208 } else {
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +00001209 if (swFlipY || glFlipY) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001210 top = texHeight - (top + height);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001211 }
Robert Phillips590533f2017-07-11 14:22:35 -04001212 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume55f2d2d2016-02-26 13:20:48 -08001213 int twoToTheMipLevel = 1 << currentMipLevel;
1214 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1215 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001216
1217 GL_CALL(TexSubImage2D(target,
1218 currentMipLevel,
1219 left, top,
1220 currentWidth,
1221 currentHeight,
1222 externalFormat, externalType,
1223 texelsShallowCopy[currentMipLevel].fPixels));
1224 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001225 }
1226
cblume55f2d2d2016-02-26 13:20:48 -08001227 restore_pixelstore_state(*interface, caps, restoreGLRowLength, glFlipY);
1228
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001229 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001230}
1231
bsalomon424cc262015-05-22 10:37:30 -07001232static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001233 int sampleCount,
1234 GrGLenum format,
1235 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001236 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001237 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001238 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001239 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001240 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001241 GL_ALLOC_CALL(ctx.interface(),
1242 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1243 sampleCount,
1244 format,
1245 width, height));
1246 break;
1247 case GrGLCaps::kES_Apple_MSFBOType:
1248 GL_ALLOC_CALL(ctx.interface(),
1249 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1250 sampleCount,
1251 format,
1252 width, height));
1253 break;
1254 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1255 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1256 GL_ALLOC_CALL(ctx.interface(),
1257 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1258 sampleCount,
1259 format,
1260 width, height));
1261 break;
1262 case GrGLCaps::kNone_MSFBOType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +00001263 SkFAIL("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001264 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001265 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001266 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001267}
1268
egdanielb0e1be22015-04-22 13:27:39 -07001269bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001270 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001271 GrGLRenderTarget::IDDesc* idDesc) {
1272 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001273 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001274 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001275 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001276 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1277 this->caps()->usesMixedSamples());
csmartdaltonf9635992016-08-10 11:09:07 -07001278 idDesc->fIsMixedSampled = desc.fSampleCnt > 0 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279
1280 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281
bsalomona11e5fc2015-12-18 07:59:41 -08001282 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001283
bsalomonb15b4c12014-10-29 12:41:57 -07001284 if (desc.fSampleCnt > 0 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001285 goto FAILED;
1286 }
1287
egdanield803f272015-03-18 13:01:52 -07001288 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1289 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001290 goto FAILED;
1291 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001292
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001293 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1294 // the texture bound to the other. The exception is the IMG multisample extension. With this
1295 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1296 // rendered from.
bsalomonb15b4c12014-10-29 12:41:57 -07001297 if (desc.fSampleCnt > 0 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001298 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001299 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001300 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001301 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001302 goto FAILED;
1303 }
bsalomon76148af2016-01-12 11:13:47 -08001304 if (!this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat)) {
1305 return false;
1306 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001307 } else {
egdanield803f272015-03-18 13:01:52 -07001308 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001309 }
1310
egdanield803f272015-03-18 13:01:52 -07001311 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001312 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001313 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
bsalomonb15b4c12014-10-29 12:41:57 -07001314 SkASSERT(desc.fSampleCnt > 0);
1315 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001316 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001317 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001318 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001319 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001320 goto FAILED;
1321 }
egdanield803f272015-03-18 13:01:52 -07001322 fStats.incRenderTargetBinds();
1323 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID));
1324 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001325 GR_GL_COLOR_ATTACHMENT0,
1326 GR_GL_RENDERBUFFER,
1327 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001328 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001329 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1330 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1331 goto FAILED;
1332 }
bsalomon424cc262015-05-22 10:37:30 -07001333 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001334 }
1335 }
egdanield803f272015-03-18 13:01:52 -07001336 fStats.incRenderTargetBinds();
1337 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001338
bsalomonb15b4c12014-10-29 12:41:57 -07001339 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 0) {
egdanield803f272015-03-18 13:01:52 -07001340 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001341 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001342 texInfo.fTarget,
1343 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001344 } else {
egdanield803f272015-03-18 13:01:52 -07001345 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001346 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001347 texInfo.fTarget,
1348 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001349 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001350 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001351 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001352 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1353 goto FAILED;
1354 }
bsalomon424cc262015-05-22 10:37:30 -07001355 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001356 }
1357
1358 return true;
1359
1360FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001361 if (idDesc->fMSColorRenderbufferID) {
1362 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001363 }
egdanield803f272015-03-18 13:01:52 -07001364 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
1365 GL_CALL(DeleteFramebuffers(1, &idDesc->fRTFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366 }
egdanield803f272015-03-18 13:01:52 -07001367 if (idDesc->fTexFBOID) {
1368 GL_CALL(DeleteFramebuffers(1, &idDesc->fTexFBOID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001369 }
1370 return false;
1371}
1372
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001373// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001374static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001375// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001376 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001377}
1378
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001379#if 0 && defined(SK_DEBUG)
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001380static size_t as_size_t(int x) {
1381 return x;
1382}
1383#endif
1384
cblume55f2d2d2016-02-26 13:20:48 -08001385static void set_initial_texture_params(const GrGLInterface* interface,
1386 const GrGLTextureInfo& info,
1387 GrGLTexture::TexParams* initialTexParams) {
1388 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1389 // drivers have a bug where an FBO won't be complete if it includes a
1390 // texture that is not mipmap complete (considering the filter in use).
1391 // we only set a subset here so invalidate first
1392 initialTexParams->invalidate();
1393 initialTexParams->fMinFilter = GR_GL_NEAREST;
1394 initialTexParams->fMagFilter = GR_GL_NEAREST;
1395 initialTexParams->fWrapS = GR_GL_CLAMP_TO_EDGE;
1396 initialTexParams->fWrapT = GR_GL_CLAMP_TO_EDGE;
1397 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1398 GR_GL_TEXTURE_MAG_FILTER,
1399 initialTexParams->fMagFilter));
1400 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1401 GR_GL_TEXTURE_MIN_FILTER,
1402 initialTexParams->fMinFilter));
1403 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1404 GR_GL_TEXTURE_WRAP_S,
1405 initialTexParams->fWrapS));
1406 GR_GL_CALL(interface, TexParameteri(info.fTarget,
1407 GR_GL_TEXTURE_WRAP_T,
1408 initialTexParams->fWrapT));
1409}
1410
Robert Phillips67d52cf2017-06-05 13:38:13 -04001411sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1412 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001413 const GrMipLevel texels[],
1414 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001415 // We fail if the MSAA was requested and is not available.
1416 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
tfarina38406c82014-10-31 07:11:12 -07001417 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001418 return return_null_texture();
1419 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001420
Brian Salomond17b4a62017-05-23 16:53:47 -04001421 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag);
Brian Salomond17b4a62017-05-23 16:53:47 -04001422
Robert Phillips590533f2017-07-11 14:22:35 -04001423 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001424 std::unique_ptr<uint8_t[]> zeros;
1425 // TODO: remove the GrPixelConfigIsSint test. This is here because we have yet to add support
1426 // for glClearBuffer* which must be used instead of glClearColor/glClear for integer FBO
1427 // attachments.
1428 if (performClear && !this->glCaps().clearTextureSupport() &&
1429 (!this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig) ||
1430 GrPixelConfigIsSint(desc.fConfig))) {
1431 size_t rowSize = GrBytesPerPixel(desc.fConfig) * desc.fWidth;
1432 size_t size = rowSize * desc.fHeight;
1433 zeros.reset(new uint8_t[size]);
1434 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001435 zeroLevel.fPixels = zeros.get();
1436 zeroLevel.fRowBytes = 0;
1437 texels = &zeroLevel;
1438 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001439 performClear = false;
1440 }
1441
Brian Salomond34edf32017-05-19 15:45:48 -04001442 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
bsalomonb15b4c12014-10-29 12:41:57 -07001444 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001445 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001446 GrGLTexture::TexParams initialTexParams;
Robert Phillips590533f2017-07-11 14:22:35 -04001447 if (!this->createTextureImpl(desc, &idDesc.fInfo, isRenderTarget, &initialTexParams,
1448 texels, mipLevelCount)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001449 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001450 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001451
Robert Phillipsd6214d42016-11-07 08:23:48 -05001452 bool wasMipMapDataProvided = false;
Robert Phillips590533f2017-07-11 14:22:35 -04001453 if (mipLevelCount > 1) {
Robert Phillipsd6214d42016-11-07 08:23:48 -05001454 wasMipMapDataProvided = true;
1455 }
1456
Robert Phillips67d52cf2017-06-05 13:38:13 -04001457 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001458 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001459 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001460 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001461 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001462
kkinnunen2e6055b2016-04-22 01:48:29 -07001463 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001464 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 return return_null_texture();
1466 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001467 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
1468 wasMipMapDataProvided);
Brian Salomon9bada542017-06-12 12:09:30 -04001469 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001470 } else {
Robert Phillips67d52cf2017-06-05 13:38:13 -04001471 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, wasMipMapDataProvided);
reed@google.comac10a2d2010-12-22 21:39:39 +00001472 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001473 tex->setCachedTexParams(initialTexParams, this->getResetTimestamp());
reed@google.comac10a2d2010-12-22 21:39:39 +00001474#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001475 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001476 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001477#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001478 if (tex && performClear) {
1479 if (this->glCaps().clearTextureSupport()) {
1480 GrGLenum format = GrPixelConfigIsSint(tex->config()) ? GR_GL_RGBA_INTEGER : GR_GL_RGBA;
1481 static constexpr uint32_t kZero = 0;
1482 GL_CALL(ClearTexImage(tex->textureID(), 0, format, GR_GL_UNSIGNED_BYTE, &kZero));
1483 } else {
1484 SkASSERT(!GrPixelConfigIsSint(desc.fConfig));
1485 GrGLIRect viewport;
Robert Phillips67d52cf2017-06-05 13:38:13 -04001486 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, &viewport,
1487 kDst_TempFBOTarget);
Brian Salomond17b4a62017-05-23 16:53:47 -04001488 this->disableScissor();
1489 this->disableWindowRectangles();
1490 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
1491 fHWWriteToColor = kYes_TriState;
1492 GL_CALL(ClearColor(0, 0, 0, 0));
1493 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001494 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001495 fHWBoundRenderTargetUniqueID.makeInvalid();
1496 }
1497 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001498 return tex;
1499}
1500
1501namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502
egdaniel8dc7c3a2015-04-16 11:22:42 -07001503const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001504
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001505void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001506 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001507
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001508 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001509 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001510 (kUnknownBitCount == format->fTotalBits));
1511 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001512 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001513 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1514 (GrGLint*)&format->fStencilBits);
1515 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001516 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001517 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1518 (GrGLint*)&format->fTotalBits);
1519 format->fTotalBits += format->fStencilBits;
1520 } else {
1521 format->fTotalBits = format->fStencilBits;
1522 }
1523 }
1524}
1525}
1526
egdanielff1d5472015-09-10 08:37:20 -07001527int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001528 static const int kSize = 16;
bsalomon926cb022015-12-17 18:15:11 -08001529 SkASSERT(this->caps()->isConfigRenderable(config, false));
bsalomon30447372015-12-21 09:03:05 -08001530 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1531 // Default to unsupported, set this if we find a stencil format that works.
1532 int firstWorkingStencilFormatIndex = -1;
egdanielff1d5472015-09-10 08:37:20 -07001533 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001534 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001535 GL_CALL(GenTextures(1, &colorID));
1536 this->setScratchTextureUnit();
1537 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, colorID));
1538 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1539 GR_GL_TEXTURE_MAG_FILTER,
1540 GR_GL_NEAREST));
1541 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1542 GR_GL_TEXTURE_MIN_FILTER,
1543 GR_GL_NEAREST));
1544 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1545 GR_GL_TEXTURE_WRAP_S,
1546 GR_GL_CLAMP_TO_EDGE));
1547 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1548 GR_GL_TEXTURE_WRAP_T,
1549 GR_GL_CLAMP_TO_EDGE));
1550
bsalomon76148af2016-01-12 11:13:47 -08001551 GrGLenum internalFormat;
1552 GrGLenum externalFormat;
1553 GrGLenum externalType;
1554 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1555 &externalType)) {
1556 return false;
1557 }
egdanielff1d5472015-09-10 08:37:20 -07001558 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1559 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001560 0,
bsalomon76148af2016-01-12 11:13:47 -08001561 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001562 kSize,
1563 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001564 0,
bsalomon76148af2016-01-12 11:13:47 -08001565 externalFormat,
1566 externalType,
egdanielff1d5472015-09-10 08:37:20 -07001567 NULL));
bsalomon30447372015-12-21 09:03:05 -08001568 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001570 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001571 }
1572
1573 // unbind the texture from the texture unit before binding it to the frame buffer
1574 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1575
1576 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001577 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001578 GL_CALL(GenFramebuffers(1, &fb));
egdanielec00d942015-09-14 12:56:10 -07001579 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fb));
Robert Phillips294870f2016-11-11 12:38:40 -05001580 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001581 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1582 GR_GL_COLOR_ATTACHMENT0,
1583 GR_GL_TEXTURE_2D,
1584 colorID,
1585 0));
bsalomon30447372015-12-21 09:03:05 -08001586 GrGLuint sbRBID = 0;
1587 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001588
1589 // look over formats till I find a compatible one
1590 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001591 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001592 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001593 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1594 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1595 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1596 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1597 sFmt.fInternalFormat,
1598 kSize, kSize));
1599 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001600 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001601 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001602 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001603 if (sFmt.fPacked) {
1604 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1605 GR_GL_DEPTH_ATTACHMENT,
1606 GR_GL_RENDERBUFFER, sbRBID));
1607 } else {
1608 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1609 GR_GL_DEPTH_ATTACHMENT,
1610 GR_GL_RENDERBUFFER, 0));
1611 }
1612 GrGLenum status;
1613 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1614 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1615 firstWorkingStencilFormatIndex = i;
1616 break;
1617 }
egdanielff1d5472015-09-10 08:37:20 -07001618 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1619 GR_GL_STENCIL_ATTACHMENT,
1620 GR_GL_RENDERBUFFER, 0));
1621 if (sFmt.fPacked) {
1622 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1623 GR_GL_DEPTH_ATTACHMENT,
1624 GR_GL_RENDERBUFFER, 0));
1625 }
egdanielff1d5472015-09-10 08:37:20 -07001626 }
1627 }
bsalomon30447372015-12-21 09:03:05 -08001628 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001629 }
1630 GL_CALL(DeleteTextures(1, &colorID));
egdanielff1d5472015-09-10 08:37:20 -07001631 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
1632 GL_CALL(DeleteFramebuffers(1, &fb));
bsalomon30447372015-12-21 09:03:05 -08001633 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001634 }
bsalomon30447372015-12-21 09:03:05 -08001635 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001636}
1637
erikchen9a1ed5d2016-02-10 16:32:34 -08001638bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -08001639 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
Robert Phillips590533f2017-07-11 14:22:35 -04001640 const GrMipLevel texels[], int mipLevelCount) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001641 info->fID = 0;
1642 info->fTarget = GR_GL_TEXTURE_2D;
1643 GL_CALL(GenTextures(1, &(info->fID)));
1644
1645 if (!info->fID) {
1646 return false;
1647 }
1648
1649 this->setScratchTextureUnit();
1650 GL_CALL(BindTexture(info->fTarget, info->fID));
1651
1652 if (renderTarget && this->glCaps().textureUsageSupport()) {
1653 // provides a hint about how this texture will be used
1654 GL_CALL(TexParameteri(info->fTarget,
1655 GR_GL_TEXTURE_USAGE,
1656 GR_GL_FRAMEBUFFER_ATTACHMENT));
1657 }
1658
cblume55f2d2d2016-02-26 13:20:48 -08001659 if (info) {
1660 set_initial_texture_params(this->glInterface(), *info, initialTexParams);
1661 }
Brian Salomond1eaf492017-05-18 10:02:08 -04001662 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, desc.fOrigin, info->fTarget,
1663 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
Robert Phillips590533f2017-07-11 14:22:35 -04001664 texels, mipLevelCount)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001665 GL_CALL(DeleteTextures(1, &(info->fID)));
1666 return false;
1667 }
1668 return true;
1669}
1670
egdanielec00d942015-09-14 12:56:10 -07001671GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001672 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001673 SkASSERT(width >= rt->width());
1674 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001675
vbuzinovdded6962015-06-12 08:59:45 -07001676 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001677 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001678
egdanielff1d5472015-09-10 08:37:20 -07001679 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001680 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001681 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001682 }
egdanielff1d5472015-09-10 08:37:20 -07001683
1684 if (!sbDesc.fRenderbufferID) {
1685 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1686 }
1687 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001688 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001689 }
1690 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1691 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1692 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1693 // we do this "if" so that we don't call the multisample
1694 // version on a GL that doesn't have an MSAA extension.
1695 if (samples > 0) {
1696 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1697 samples,
1698 sFmt.fInternalFormat,
1699 width, height));
1700 } else {
1701 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1702 sFmt.fInternalFormat,
1703 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001704 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001705 }
1706 fStats.incStencilAttachmentCreates();
1707 // After sized formats we attempt an unsized format and take
1708 // whatever sizes GL gives us. In that case we query for the size.
1709 GrGLStencilAttachment::Format format = sFmt;
1710 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001711 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1712 sbDesc,
1713 width,
1714 height,
1715 samples,
1716 format);
1717 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001718}
1719
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001720////////////////////////////////////////////////////////////////////////////////
1721
jvanverth73063dc2015-12-03 09:15:47 -08001722// GL_STREAM_DRAW triggers an optimization in Chromium's GPU process where a client's vertex buffer
1723// objects are implemented as client-side-arrays on tile-deferred architectures.
1724#define DYNAMIC_USAGE_PARAM GR_GL_STREAM_DRAW
1725
cdaltone2e71c22016-04-07 18:13:29 -07001726GrBuffer* GrGLGpu::onCreateBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -07001727 GrAccessPattern accessPattern, const void* data) {
1728 return GrGLBuffer::Create(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001729}
1730
Robert Phillipse3302df2017-04-24 07:31:02 -04001731
1732std::unique_ptr<OpAllocator> GrGLGpu::onCreateInstancedRenderingAllocator() {
1733 return std::unique_ptr<OpAllocator>(new GLOpAllocator(this->caps()));
1734}
1735
csmartdaltone0d36292016-07-29 08:14:20 -07001736InstancedRendering* GrGLGpu::onCreateInstancedRendering() {
1737 return new GLInstancedRendering(this);
1738}
1739
bsalomon3e791242014-12-17 13:43:13 -08001740void GrGLGpu::flushScissor(const GrScissorState& scissorState,
joshualitt77b13072014-10-27 14:51:01 -07001741 const GrGLIRect& rtViewport,
1742 GrSurfaceOrigin rtOrigin) {
robertphillipse85a32d2015-02-10 08:16:55 -08001743 if (scissorState.enabled()) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001744 GrGLIRect scissor;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001745 scissor.setRelativeTo(rtViewport, scissorState.rect(), rtOrigin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001746 // if the scissor fully contains the viewport then we fall through and
1747 // disable the scissor test.
bsalomonb0bd4f62014-09-03 07:19:50 -07001748 if (!scissor.contains(rtViewport)) {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001749 if (fHWScissorSettings.fRect != scissor) {
1750 scissor.pushToGLScissor(this->glInterface());
1751 fHWScissorSettings.fRect = scissor;
1752 }
1753 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1754 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1755 fHWScissorSettings.fEnabled = kYes_TriState;
1756 }
1757 return;
1758 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 }
joshualitt77b13072014-10-27 14:51:01 -07001760
1761 // See fall through note above
1762 this->disableScissor();
1763}
1764
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001765void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillips7294b852017-08-01 13:51:44 +00001766 const GrGLRenderTarget* rt) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001767#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001768 typedef GrWindowRectsState::Mode Mode;
1769 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1770 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001771
1772 if (!this->caps()->maxWindowRectangles() ||
Robert Phillips7294b852017-08-01 13:51:44 +00001773 fHWWindowRectsState.knownEqualTo(rt->origin(), rt->getViewport(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001774 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001775 }
1776
csmartdalton7535f412016-08-23 06:51:00 -07001777 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1778 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1780 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001781
csmartdalton28341fa2016-08-17 10:00:21 -07001782 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001783 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001784 for (int i = 0; i < numWindows; ++i) {
Robert Phillips7294b852017-08-01 13:51:44 +00001785 glwindows[i].setRelativeTo(rt->getViewport(), skwindows[i], rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07001786 }
1787
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001788 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001789 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001790
Robert Phillips7294b852017-08-01 13:51:44 +00001791 fHWWindowRectsState.set(rt->origin(), rt->getViewport(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001792#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001793}
1794
1795void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001796#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001798 return;
1799 }
1800 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001801 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001803}
1804
ethannicholas28ef4452016-03-25 09:26:03 -07001805void GrGLGpu::flushMinSampleShading(float minSampleShading) {
1806 if (fHWMinSampleShading != minSampleShading) {
1807 if (minSampleShading > 0.0) {
1808 GL_CALL(Enable(GR_GL_SAMPLE_SHADING));
1809 GL_CALL(MinSampleShading(minSampleShading));
1810 }
1811 else {
1812 GL_CALL(Disable(GR_GL_SAMPLE_SHADING));
1813 }
1814 fHWMinSampleShading = minSampleShading;
1815 }
1816}
1817
bsalomon2eda5b32016-09-21 10:53:24 -07001818bool GrGLGpu::flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc,
1819 bool willDrawPoints) {
Hal Canary144caf52016-11-07 17:57:18 -05001820 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, pipeline, primProc, willDrawPoints));
brianosman33f6b3f2016-06-02 05:49:21 -07001821 if (!program) {
1822 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1823 return false;
1824 }
1825
1826 program->generateMipmaps(primProc, pipeline);
1827
egdaniel080e6732014-12-22 07:35:52 -08001828 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001829 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001830
egdaniel080e6732014-12-22 07:35:52 -08001831 this->flushColorWrite(blendInfo.fWriteColor);
ethannicholas28ef4452016-03-25 09:26:03 -07001832 this->flushMinSampleShading(primProc.getSampleShading());
bsalomonbc3d0de2014-12-15 13:45:03 -08001833
bsalomon6df86402015-06-01 10:41:49 -07001834 GrGLuint programID = program->programID();
bsalomon1f78c0a2014-12-17 09:43:13 -08001835 if (fHWProgramID != programID) {
1836 GL_CALL(UseProgram(programID));
1837 fHWProgramID = programID;
1838 }
1839
egdanield803f272015-03-18 13:01:52 -07001840 if (blendInfo.fWriteColor) {
bsalomon7f9b2e42016-01-12 13:29:26 -08001841 // Swizzle the blend to match what the shader will output.
Brian Salomon1edc5b92016-11-29 13:43:46 -05001842 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillips2890fbf2017-07-26 15:48:41 -04001843 pipeline.proxy()->config());
bsalomon7f9b2e42016-01-12 13:29:26 -08001844 this->flushBlend(blendInfo, swizzle);
egdanield803f272015-03-18 13:01:52 -07001845 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001846
cdalton74b8d322016-04-11 14:47:28 -07001847 program->setData(primProc, pipeline);
bsalomon1f78c0a2014-12-17 09:43:13 -08001848
Robert Phillips2890fbf2017-07-26 15:48:41 -04001849 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
csmartdaltonc633abb2016-11-01 08:55:55 -07001850 GrStencilSettings stencil;
1851 if (pipeline.isStencilEnabled()) {
1852 // TODO: attach stencil and create settings during render target flush.
1853 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1854 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1855 glRT->renderTargetPriv().numStencilBits());
1856 }
1857 this->flushStencil(stencil);
Robert Phillips16d8ec62017-07-27 16:16:25 -04001858 this->flushScissor(pipeline.getScissorState(), glRT->getViewport(), pipeline.proxy()->origin());
Robert Phillips7294b852017-08-01 13:51:44 +00001859 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT);
csmartdaltondabc91b2016-11-15 14:26:27 -07001860 this->flushHWAAState(glRT, pipeline.isHWAntialiasState(), !stencil.isDisabled());
bsalomonbc3d0de2014-12-15 13:45:03 -08001861
1862 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001863 // to be msaa-resolved (which will modify bound FBO state).
brianosman64d094d2016-03-25 06:01:59 -07001864 this->flushRenderTarget(glRT, nullptr, pipeline.getDisableOutputConversionToSRGB());
bsalomonbc3d0de2014-12-15 13:45:03 -08001865
1866 return true;
1867}
1868
joshualitt873ad0e2015-01-20 09:08:51 -08001869void GrGLGpu::setupGeometry(const GrPrimitiveProcessor& primProc,
Chris Daltonff926502017-05-03 14:36:54 -04001870 const GrBuffer* indexBuffer,
1871 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001872 int baseVertex,
1873 const GrBuffer* instanceBuffer,
1874 int baseInstance) {
cdaltone2e71c22016-04-07 18:13:29 -07001875 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001876 if (indexBuffer) {
Chris Dalton1d616352017-05-31 12:51:23 -06001877 SkASSERT(indexBuffer && !indexBuffer->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001878 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001879 } else {
1880 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001881 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001882
Chris Dalton1d616352017-05-31 12:51:23 -06001883 struct {
1884 const GrBuffer* fBuffer;
1885 int fStride;
1886 size_t fBufferOffset;
1887 } bindings[2];
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001888
Chris Dalton1d616352017-05-31 12:51:23 -06001889 if (int vertexStride = primProc.getVertexStride()) {
1890 SkASSERT(vertexBuffer && !vertexBuffer->isMapped());
1891 bindings[0].fBuffer = vertexBuffer;
1892 bindings[0].fStride = vertexStride;
1893 bindings[0].fBufferOffset = vertexBuffer->baseOffset() + baseVertex * vertexStride;
1894 }
1895 if (int instanceStride = primProc.getInstanceStride()) {
1896 SkASSERT(instanceBuffer && !instanceBuffer->isMapped());
1897 bindings[1].fBuffer = instanceBuffer;
1898 bindings[1].fStride = instanceStride;
1899 bindings[1].fBufferOffset = instanceBuffer->baseOffset() + baseInstance * instanceStride;
1900 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001901
Chris Dalton1d616352017-05-31 12:51:23 -06001902 int numAttribs = primProc.numAttribs();
1903 attribState->enableVertexArrays(this, numAttribs);
bsalomonbc3d0de2014-12-15 13:45:03 -08001904
Chris Dalton1d616352017-05-31 12:51:23 -06001905 for (int i = 0; i < numAttribs; ++i) {
1906 using InputRate = GrPrimitiveProcessor::Attribute::InputRate;
1907 const GrGeometryProcessor::Attribute& attrib = primProc.getAttrib(i);
1908 const int divisor = InputRate::kPerInstance == attrib.fInputRate ? 1 : 0;
1909 const auto& binding = bindings[divisor];
1910 attribState->set(this, i, binding.fBuffer, attrib.fType, binding.fStride,
1911 binding.fBufferOffset + attrib.fOffsetInRecord, divisor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001912 }
1913}
1914
csmartdalton485a1202016-07-13 10:16:32 -07001915GrGLenum GrGLGpu::bindBuffer(GrBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001916 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001917
cdaltone2e71c22016-04-07 18:13:29 -07001918 // Index buffer state is tied to the vertex array.
1919 if (kIndex_GrBufferType == type) {
1920 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001921 }
cdaltone2e71c22016-04-07 18:13:29 -07001922
1923 SkASSERT(type >= 0 && type <= kLast_GrBufferType);
1924 auto& bufferState = fHWBufferState[type];
1925
robertphillips8abb3702016-08-31 14:04:06 -07001926 if (buffer->uniqueID() != bufferState.fBoundBufferUniqueID) {
csmartdalton485a1202016-07-13 10:16:32 -07001927 if (buffer->isCPUBacked()) {
1928 if (!bufferState.fBufferZeroKnownBound) {
1929 GL_CALL(BindBuffer(bufferState.fGLTarget, 0));
1930 }
1931 } else {
1932 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1933 GL_CALL(BindBuffer(bufferState.fGLTarget, glBuffer->bufferID()));
cdaltone2e71c22016-04-07 18:13:29 -07001934 }
csmartdalton485a1202016-07-13 10:16:32 -07001935 bufferState.fBufferZeroKnownBound = buffer->isCPUBacked();
robertphillips8abb3702016-08-31 14:04:06 -07001936 bufferState.fBoundBufferUniqueID = buffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001937 }
1938
1939 return bufferState.fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001940}
1941
cdalton74b8d322016-04-11 14:47:28 -07001942void GrGLGpu::notifyBufferReleased(const GrGLBuffer* buffer) {
1943 if (buffer->hasAttachedToTexture()) {
1944 // Detach this buffer from any textures to ensure the underlying memory is freed.
Robert Phillips294870f2016-11-11 12:38:40 -05001945 GrGpuResource::UniqueID uniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07001946 for (int i = fHWMaxUsedBufferTextureUnit; i >= 0; --i) {
1947 auto& buffTex = fHWBufferTextures[i];
1948 if (uniqueID != buffTex.fAttachedBufferUniqueID) {
1949 continue;
1950 }
1951 if (i == fHWMaxUsedBufferTextureUnit) {
1952 --fHWMaxUsedBufferTextureUnit;
1953 }
1954
1955 this->setTextureUnit(i);
1956 if (!buffTex.fKnownBound) {
1957 SkASSERT(buffTex.fTextureID);
1958 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
1959 buffTex.fKnownBound = true;
1960 }
1961 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
1962 this->glCaps().configSizedInternalFormat(buffTex.fTexelConfig), 0));
1963 }
1964 }
1965}
1966
bsalomon861e1032014-12-16 07:33:49 -08001967void GrGLGpu::disableScissor() {
bsalomon@google.coma3201942012-06-21 19:58:20 +00001968 if (kNo_TriState != fHWScissorSettings.fEnabled) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001969 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
bsalomon@google.coma3201942012-06-21 19:58:20 +00001970 fHWScissorSettings.fEnabled = kNo_TriState;
1971 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001972 }
1973}
1974
Robert Phillips7294b852017-08-01 13:51:44 +00001975void GrGLGpu::clear(const GrFixedClip& clip, GrColor color, GrRenderTarget* target) {
egdaniel9cb63402016-06-23 08:37:05 -07001976 this->handleDirtyContext();
1977
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001978 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001979 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001980 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001981
csmartdalton29df7602016-08-31 11:55:52 -07001982 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Robert Phillips7294b852017-08-01 13:51:44 +00001983 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
1984 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001985
1986 GrGLfloat r, g, b, a;
1987 static const GrGLfloat scale255 = 1.f / 255.f;
1988 a = GrColorUnpackA(color) * scale255;
1989 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001990 r = GrColorUnpackR(color) * scaleRGB;
1991 g = GrColorUnpackG(color) * scaleRGB;
1992 b = GrColorUnpackB(color) * scaleRGB;
1993
1994 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001995 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001996
Eric Karlaeaf22b2017-05-18 15:08:09 -07001997 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1998 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07001999 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2000 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002001 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002002 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002003 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002004 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002005}
2006
bsalomon861e1032014-12-16 07:33:49 -08002007void GrGLGpu::clearStencil(GrRenderTarget* target) {
halcanary96fcdcc2015-08-27 07:41:13 -07002008 if (nullptr == target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002009 return;
2010 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002011 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002012 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002013
joshualitt77b13072014-10-27 14:51:01 -07002014 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002015 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002016
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(StencilMask(0xffffffff));
2018 GL_CALL(ClearStencil(0));
2019 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002020 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002021}
2022
csmartdalton29df7602016-08-31 11:55:52 -07002023void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2024 bool insideStencilMask,
Robert Phillips7294b852017-08-01 13:51:44 +00002025 GrRenderTarget* target) {
bsalomon49f085d2014-09-05 13:34:00 -07002026 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002027 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002028
Mike Klein31550db2017-06-06 23:29:53 +00002029 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillips7294b852017-08-01 13:51:44 +00002030 this->clearStencilClipAsDraw(clip, insideStencilMask, target);
Mike Klein31550db2017-06-06 23:29:53 +00002031 return;
2032 }
2033
egdaniel8dc7c3a2015-04-16 11:22:42 -07002034 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002035 // this should only be called internally when we know we have a
2036 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002037 SkASSERT(sb);
2038 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002039#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002040 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002041 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002042#else
2043 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002044 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002045 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002046 // changing the clip between stencil passes may or may not
2047 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002048 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002049#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002050 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002051 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002052 value = (1 << (stencilBitCount - 1));
2053 } else {
2054 value = 0;
2055 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002056 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002057 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002058
Robert Phillips7294b852017-08-01 13:51:44 +00002059 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
2060 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002061
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002062 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002063 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002064 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002065 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002066}
2067
Brian Salomon71d9d842016-11-03 13:42:00 -04002068static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002069 int width, int height, GrPixelConfig config,
2070 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002071 // If the surface is already TopLeft, we don't need to flip.
2072 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002073 return false;
2074 }
2075
bsalomon494aa592015-07-23 11:45:02 -07002076 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002077 static const int kMinSize = 32;
2078 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002079 return false;
2080 }
2081
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002082 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002083 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002084 return false;
2085 }
2086
2087 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002088 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002089 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2090 // clipped.
2091 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2092}
2093
bsalomon1aa20292016-01-22 08:16:09 -08002094bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002095#ifdef SK_BUILD_FOR_MAC
2096 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2097 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2098 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2099 // we always use a temporary surface to test for read pixels support.
2100 // https://www.crbug.com/662802
2101 if (this->glContext().driver() == kChromium_GrGLDriver) {
2102 return this->readPixelsSupported(target->config(), readConfig);
2103 }
2104#endif
bsalomon1aa20292016-01-22 08:16:09 -08002105 auto bindRenderTarget = [this, target]() -> bool {
2106 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2107 return true;
2108 };
bsalomon2c3db322016-11-08 13:26:24 -08002109 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002110 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2111 GR_GL_GetIntegerv(this->glInterface(), query, value);
2112 };
2113 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002114 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2115 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002116}
2117
2118bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002119 sk_sp<GrTexture> temp;
2120 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002121 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002122 desc.fConfig = rtConfig;
2123 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002124 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2125 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04002126 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002127 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002128 if (!temp) {
2129 return false;
2130 }
2131 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2132 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2133 return true;
2134 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002135 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002136 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002137 if (!temp) {
2138 return false;
2139 }
2140 GrGLIRect vp;
2141 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002142 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002143 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002144 }
bsalomon2c3db322016-11-08 13:26:24 -08002145 return false;
2146 };
2147 auto unbindRenderTarget = [this, &temp]() {
2148 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002149 };
2150 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2151 GR_GL_GetIntegerv(this->glInterface(), query, value);
2152 };
bsalomon2c3db322016-11-08 13:26:24 -08002153 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2154 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002155}
2156
2157bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2158 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2159 return this->readPixelsSupported(rt, readConfig);
2160 } else {
2161 GrPixelConfig config = surfaceForConfig->config();
2162 return this->readPixelsSupported(config, readConfig);
2163 }
2164}
2165
bsalomone9573312016-01-25 14:33:25 -08002166static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2167 if (GrPixelConfigIsSRGB(a)) {
2168 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2169 } else if (GrPixelConfigIsSRGB(b)) {
2170 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2171 }
2172 return false;
2173}
2174
Robert Phillips7294b852017-08-01 13:51:44 +00002175bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
bsalomonf0674512015-07-28 13:26:15 -07002176 GrPixelConfig readConfig, DrawPreference* drawPreference,
2177 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002178 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002179
bsalomone9573312016-01-25 14:33:25 -08002180 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002181 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2182 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2183 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2184 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2185 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002186 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2187 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002188 // For now assume no swizzling, we may change that below.
2189 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2190
2191 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2192 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002193 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002194 // base class will fail for us.
2195 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2196 tempDrawInfo->fReadConfig = readConfig;
2197
2198 if (requires_srgb_conversion(srcConfig, readConfig)) {
2199 if (!this->readPixelsSupported(readConfig, readConfig)) {
2200 return false;
2201 }
2202 // Draw to do srgb to linear conversion or vice versa.
2203 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2204 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2205 tempDrawInfo->fReadConfig = readConfig;
2206 return true;
2207 }
2208
bsalomon1aa20292016-01-22 08:16:09 -08002209 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2210 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002211 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002212 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2213 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002214 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002215 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002216 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002217 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2218 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002219 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002220 // Better to do a draw with a R/B swap and then read as the original config.
2221 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002222 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2223 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002224 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002225 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2226 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002227 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002228 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002229 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2230 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2231 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002232 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2233 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2234 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2235 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002236 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002237 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002238 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2239 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2240 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2241 // will effectively be sBGRA).
2242 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2243 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2244 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2245 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002246 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2247 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2248 // it's unsupported, but 32bit RGBA reads are supported.
2249 // Don't attempt to do any srgb conversions since we only care about alpha.
2250 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2251 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2252 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2253 }
2254 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2255 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2256 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002257 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002258 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2259 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2260 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2261 } else {
2262 return false;
2263 }
2264 } else {
2265 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2266 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2267 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002268 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002269 this->readPixelsSupported(readConfig, readConfig)) {
2270 // Do a draw to convert from the src config to the read config.
2271 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2272 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2273 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002274 } else {
2275 return false;
2276 }
bsalomon39826022015-07-23 08:07:21 -07002277 }
2278
Brian Salomon71d9d842016-11-03 13:42:00 -04002279 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Robert Phillips7294b852017-08-01 13:51:44 +00002280 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002281 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002282 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002283 }
2284
bsalomon39826022015-07-23 08:07:21 -07002285 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002286}
2287
Robert Phillips7294b852017-08-01 13:51:44 +00002288bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002289 int left, int top,
2290 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002291 GrPixelConfig config,
2292 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002293 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002294 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002295
bsalomone9573312016-01-25 14:33:25 -08002296 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002297 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002298 return false;
2299 }
2300
bsalomon16921ec2015-07-30 15:34:56 -07002301 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002302 if (requires_srgb_conversion(surface->config(), config)) {
2303 return false;
2304 }
2305
2306 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2307 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002308 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002309 // Don't attempt to do any srgb conversions since we only care about alpha.
2310 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002311 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002312 tempConfig = kSRGBA_8888_GrPixelConfig;
2313 }
2314 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002315 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002316 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillips7294b852017-08-01 13:51:44 +00002317 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
2318 width*4)) {
bsalomone9573312016-01-25 14:33:25 -08002319 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2320 for (int j = 0; j < height; ++j) {
2321 for (int i = 0; i < width; ++i) {
2322 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2323 }
2324 }
2325 return true;
2326 }
2327 }
bsalomon16921ec2015-07-30 15:34:56 -07002328 return false;
2329 }
2330
bsalomon76148af2016-01-12 11:13:47 -08002331 GrGLenum externalFormat;
2332 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002333 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002334 &externalType)) {
2335 return false;
2336 }
Robert Phillips7294b852017-08-01 13:51:44 +00002337 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002338
Brian Salomon71d9d842016-11-03 13:42:00 -04002339 GrGLIRect glvp;
2340 if (renderTarget) {
2341 // resolve the render target if necessary
2342 switch (renderTarget->getResolveType()) {
2343 case GrGLRenderTarget::kCantResolve_ResolveType:
2344 return false;
2345 case GrGLRenderTarget::kAutoResolves_ResolveType:
2346 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2347 break;
2348 case GrGLRenderTarget::kCanResolve_ResolveType:
Robert Phillips7294b852017-08-01 13:51:44 +00002349 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002350 // we don't track the state of the READ FBO ID.
2351 fStats.incRenderTargetBinds();
2352 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2353 break;
2354 default:
2355 SkFAIL("Unknown resolve type");
2356 }
2357 glvp = renderTarget->getViewport();
2358 } else {
2359 // Use a temporary FBO.
2360 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002361 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002362 }
2363
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002364 // the read rect is viewport-relative
2365 GrGLIRect readRect;
Robert Phillips7294b852017-08-01 13:51:44 +00002366 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002367
bsalomon9d02b262016-02-01 12:49:30 -08002368 size_t bytesPerPixel = GrBytesPerPixel(config);
2369 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002370
bsalomon@google.comc6980972011-11-02 19:57:21 +00002371 size_t readDstRowBytes = tightRowBytes;
2372 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002373
bsalomon@google.comc6980972011-11-02 19:57:21 +00002374 // determine if GL can read using the passed rowBytes or if we need
2375 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002376 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002377 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002378 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002379 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002380 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002381 readDstRowBytes = rowBytes;
2382 } else {
2383 scratch.reset(tightRowBytes * height);
2384 readDst = scratch.get();
2385 }
2386 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002387 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002388 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2389 }
bsalomonf46a1242015-12-15 12:37:38 -08002390 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2391
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002392 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2393 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002394 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002395 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002396 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002397 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2398 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002399 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002400 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002401 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002402 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002403
2404 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002405 // API presents top-to-bottom. We must preserve the padding contents. Note
2406 // that the above readPixels did not overwrite the padding.
2407 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002408 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002409 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002410 scratch.reset(tightRowBytes);
2411 void* tmpRow = scratch.get();
2412 // flip y in-place by rows
2413 const int halfY = height >> 1;
2414 char* top = reinterpret_cast<char*>(buffer);
2415 char* bottom = top + (height - 1) * rowBytes;
2416 for (int y = 0; y < halfY; y++) {
2417 memcpy(tmpRow, top, tightRowBytes);
2418 memcpy(top, bottom, tightRowBytes);
2419 memcpy(bottom, tmpRow, tightRowBytes);
2420 top += rowBytes;
2421 bottom -= rowBytes;
2422 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002423 }
2424 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002425 SkASSERT(readDst != buffer);
2426 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002427 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002428 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002429 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002430 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002431 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002432 dst += (height-1) * rowBytes;
2433 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002434 for (int y = 0; y < height; y++) {
2435 memcpy(dst, src, tightRowBytes);
2436 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002437 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002438 dst += rowBytes;
2439 } else {
2440 dst -= rowBytes;
2441 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002442 }
2443 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002444 if (!renderTarget) {
2445 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2446 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002447 return true;
2448}
2449
egdaniel9cb63402016-06-23 08:37:05 -07002450GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -07002451 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
2452 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -05002453 return new GrGLGpuCommandBuffer(this);
egdaniel066df7c2016-06-08 14:02:27 -07002454}
2455
brianosman64d094d2016-03-25 06:01:59 -07002456void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002457 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002458
Robert Phillips294870f2016-11-11 12:38:40 -05002459 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002460 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002461 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002462 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2463#ifdef SK_DEBUG
2464 // don't do this check in Chromium -- this is causing
2465 // lots of repeated command buffer flushes when the compositor is
2466 // rendering with Ganesh, which is really slow; even too slow for
2467 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002468 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002469 GrGLenum status;
2470 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2471 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2472 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2473 }
bsalomon160f24c2015-03-17 15:55:42 -07002474 }
egdanield803f272015-03-18 13:01:52 -07002475#endif
2476 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002477 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002478 }
2479
brianosman35b784d2016-05-05 11:52:53 -07002480 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002481 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002482 }
brianosman64d094d2016-03-25 06:01:59 -07002483
bsalomon083617b2016-02-12 12:10:14 -08002484 this->didWriteToSurface(target, bounds);
2485}
bsalomona9909122016-01-23 10:41:40 -08002486
brianosman33f6b3f2016-06-02 05:49:21 -07002487void GrGLGpu::flushFramebufferSRGB(bool enable) {
2488 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2489 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2490 fHWSRGBFramebuffer = kYes_TriState;
2491 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2492 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2493 fHWSRGBFramebuffer = kNo_TriState;
2494 }
2495}
2496
bsalomon083617b2016-02-12 12:10:14 -08002497void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2498 if (fHWViewport != viewport) {
2499 viewport.pushToGLViewport(this->glInterface());
2500 fHWViewport = viewport;
2501 }
2502}
2503
bsalomon@google.comd302f142011-03-03 13:54:13 +00002504#define SWAP_PER_DRAW 0
2505
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002506#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002507 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002508 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002509 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002510 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002511 void SwapBuf() {
2512 DWORD procID = GetCurrentProcessId();
2513 HWND hwnd = GetTopWindow(GetDesktopWindow());
2514 while(hwnd) {
2515 DWORD wndProcID = 0;
2516 GetWindowThreadProcessId(hwnd, &wndProcID);
2517 if(wndProcID == procID) {
2518 SwapBuffers(GetDC(hwnd));
2519 }
2520 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2521 }
2522 }
2523 #endif
2524#endif
2525
egdaniel9cb63402016-06-23 08:37:05 -07002526void GrGLGpu::draw(const GrPipeline& pipeline,
2527 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002528 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002529 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002530 int meshCount) {
2531 this->handleDirtyContext();
2532
bsalomon2eda5b32016-09-21 10:53:24 -07002533 bool hasPoints = false;
2534 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002535 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002536 hasPoints = true;
2537 break;
2538 }
2539 }
2540 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002541 return;
2542 }
ethannicholas22793252016-01-30 09:59:10 -08002543
egdaniel0e1853c2016-03-17 11:35:45 -07002544 for (int i = 0; i < meshCount; ++i) {
2545 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002546 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002547 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002548
Chris Dalton46983b72017-06-06 12:27:16 -06002549 if (dynamicStates) {
2550 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002551 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002552 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002553 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002554 }
2555 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002556 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2557 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2558 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2559 GL_CALL(Enable(GR_GL_CULL_FACE));
2560 GL_CALL(Disable(GR_GL_CULL_FACE));
2561 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002562 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002563 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002564 }
ethannicholas22793252016-01-30 09:59:10 -08002565
bsalomon@google.comd302f142011-03-03 13:54:13 +00002566#if SWAP_PER_DRAW
2567 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002568 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002569 aglSwapBuffers(aglGetCurrentContext());
2570 int set_a_break_pt_here = 9;
2571 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002572 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002573 SwapBuf();
2574 int set_a_break_pt_here = 9;
2575 SwapBuf();
2576 #endif
2577#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002578}
2579
Chris Dalton3809bab2017-06-13 10:55:06 -06002580static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2581 switch (primitiveType) {
2582 case GrPrimitiveType::kTriangles:
2583 return GR_GL_TRIANGLES;
2584 case GrPrimitiveType::kTriangleStrip:
2585 return GR_GL_TRIANGLE_STRIP;
2586 case GrPrimitiveType::kTriangleFan:
2587 return GR_GL_TRIANGLE_FAN;
2588 case GrPrimitiveType::kPoints:
2589 return GR_GL_POINTS;
2590 case GrPrimitiveType::kLines:
2591 return GR_GL_LINES;
2592 case GrPrimitiveType::kLineStrip:
2593 return GR_GL_LINE_STRIP;
2594 case GrPrimitiveType::kLinesAdjacency:
2595 return GR_GL_LINES_ADJACENCY;
2596 }
2597 SkFAIL("invalid GrPrimitiveType");
2598 return GR_GL_TRIANGLES;
2599}
2600
Chris Dalton114a3c02017-05-26 15:17:19 -06002601void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2602 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002603 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002604 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002605 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002606 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2607 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002608 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002609 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2610 }
2611 fStats.incNumDraws();
2612}
2613
2614void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2615 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2616 int indexCount, int baseIndex, uint16_t minIndexValue,
2617 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2618 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002619 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002620 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2621 sizeof(uint16_t) * baseIndex);
2622
Chris Dalton1d616352017-05-31 12:51:23 -06002623 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002624
2625 if (this->glCaps().drawRangeElementsSupport()) {
2626 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2627 GR_GL_UNSIGNED_SHORT, indices));
2628 } else {
2629 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2630 }
2631 fStats.incNumDraws();
2632}
2633
Chris Dalton1d616352017-05-31 12:51:23 -06002634void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2635 primitiveType, const GrBuffer* vertexBuffer,
2636 int vertexCount, int baseVertex,
2637 const GrBuffer* instanceBuffer, int instanceCount,
2638 int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002639 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002640 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance);
2641 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
2642 fStats.incNumDraws();
2643}
2644
2645void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2646 GrPrimitiveType primitiveType,
2647 const GrBuffer* indexBuffer, int indexCount,
2648 int baseIndex, const GrBuffer* vertexBuffer,
2649 int baseVertex, const GrBuffer* instanceBuffer,
2650 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002651 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002652 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2653 sizeof(uint16_t) * baseIndex);
2654 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2655 instanceBuffer, baseInstance);
2656 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2657 instanceCount));
2658 fStats.incNumDraws();
2659}
2660
Robert Phillips7294b852017-08-01 13:51:44 +00002661void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002662 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002663 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002664 // Some extensions automatically resolves the texture when it is read.
2665 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002666 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2667 fStats.incRenderTargetBinds();
2668 fStats.incRenderTargetBinds();
2669 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2670 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2671 // make sure we go through flushRenderTarget() since we've modified
2672 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002673 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002674 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002675 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002676
bsalomon@google.com347c3822013-05-01 20:10:01 +00002677 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002678 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002679 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002680 scissorState.set(dirtyRect);
Robert Phillips7294b852017-08-01 13:51:44 +00002681 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002682 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002683 GL_CALL(ResolveMultisampleFramebuffer());
2684 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002685 int l, b, r, t;
2686 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2687 this->glCaps().blitFramebufferSupportFlags()) {
2688 l = 0;
2689 b = 0;
2690 r = target->width();
2691 t = target->height();
2692 } else {
2693 GrGLIRect rect;
Robert Phillips7294b852017-08-01 13:51:44 +00002694 rect.setRelativeTo(vp, dirtyRect, target->origin());
Brian Salomone5e7eb12016-10-14 16:18:33 -04002695 l = rect.fLeft;
2696 b = rect.fBottom;
2697 r = rect.fLeft + rect.fWidth;
2698 t = rect.fBottom + rect.fHeight;
2699 }
derekf8c8f71a2014-09-16 06:24:57 -07002700
2701 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002702 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002703 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002704 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002705 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002706 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002707 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002708 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002709 }
2710}
2711
bsalomon@google.com411dad02012-06-05 20:24:20 +00002712namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002713
bsalomon@google.com411dad02012-06-05 20:24:20 +00002714
2715GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002716 static const GrGLenum gTable[kGrStencilOpCount] = {
2717 GR_GL_KEEP, // kKeep
2718 GR_GL_ZERO, // kZero
2719 GR_GL_REPLACE, // kReplace
2720 GR_GL_INVERT, // kInvert
2721 GR_GL_INCR_WRAP, // kIncWrap
2722 GR_GL_DECR_WRAP, // kDecWrap
2723 GR_GL_INCR, // kIncClamp
2724 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002725 };
cdalton93a379b2016-05-11 13:58:08 -07002726 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2727 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2728 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2729 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2730 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2731 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2732 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2733 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2734 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2735 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002736}
2737
2738void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002739 const GrStencilSettings::Face& face,
2740 GrGLenum glFace) {
2741 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2742 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2743 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002744
cdalton93a379b2016-05-11 13:58:08 -07002745 GrGLint ref = face.fRef;
2746 GrGLint mask = face.fTestMask;
2747 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002748
2749 if (GR_GL_FRONT_AND_BACK == glFace) {
2750 // we call the combined func just in case separate stencil is not
2751 // supported.
2752 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2753 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002754 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002755 } else {
2756 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2757 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002758 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002759 }
2760}
2761}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002762
bsalomon3e791242014-12-17 13:43:13 -08002763void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002764 if (stencilSettings.isDisabled()) {
2765 this->disableStencil();
2766 } else if (fHWStencilSettings != stencilSettings) {
2767 if (kYes_TriState != fHWStencilTestEnabled) {
2768 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002769
csmartdaltonc7d85332016-10-26 10:13:46 -07002770 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002771 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002772 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002773 set_gl_stencil(this->glInterface(),
2774 stencilSettings.front(),
2775 GR_GL_FRONT);
2776 set_gl_stencil(this->glInterface(),
2777 stencilSettings.back(),
2778 GR_GL_BACK);
2779 } else {
2780 set_gl_stencil(this->glInterface(),
2781 stencilSettings.front(),
2782 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002783 }
joshualitta58fe352014-10-27 08:39:00 -07002784 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002785 }
2786}
2787
csmartdaltonc7d85332016-10-26 10:13:46 -07002788void GrGLGpu::disableStencil() {
2789 if (kNo_TriState != fHWStencilTestEnabled) {
2790 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002791
csmartdaltonc7d85332016-10-26 10:13:46 -07002792 fHWStencilTestEnabled = kNo_TriState;
2793 fHWStencilSettings.invalidate();
2794 }
2795}
2796
cdaltonaf8bc7d2016-02-05 09:35:20 -08002797void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002798 // rt is only optional if useHWAA is false.
2799 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002800 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002801
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002802 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002803 if (useHWAA) {
2804 if (kYes_TriState != fMSAAEnabled) {
2805 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2806 fMSAAEnabled = kYes_TriState;
2807 }
2808 } else {
2809 if (kNo_TriState != fMSAAEnabled) {
2810 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2811 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002812 }
2813 }
2814 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002815
2816 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002817 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002818 // Since stencil is disabled and we want more samples than are in the color buffer, we
2819 // need to tell the rasterizer explicitly how many to run.
2820 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2821 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2822 fHWRasterMultisampleEnabled = kYes_TriState;
2823 }
2824 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2825 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2826 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2827 fHWNumRasterSamples = rt->numStencilSamples();
2828 }
2829 } else {
2830 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2831 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2832 fHWRasterMultisampleEnabled = kNo_TriState;
2833 }
2834 }
2835 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002836 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002837 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002838}
2839
bsalomon7f9b2e42016-01-12 13:29:26 -08002840void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002841 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002842 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002843
cdalton8917d622015-05-06 13:40:21 -07002844 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002845 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2846 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002847 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2848 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002849 if (blendOff) {
2850 if (kNo_TriState != fHWBlendState.fEnabled) {
2851 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002852
2853 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2854 // https://code.google.com/p/skia/issues/detail?id=3943
2855 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2856 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2857 SkASSERT(this->caps()->advancedBlendEquationSupport());
2858 // Set to any basic blending equation.
2859 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2860 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2861 fHWBlendState.fEquation = blend_equation;
2862 }
2863
egdanielb414f252014-07-29 13:15:47 -07002864 fHWBlendState.fEnabled = kNo_TriState;
2865 }
cdalton8917d622015-05-06 13:40:21 -07002866 return;
2867 }
2868
2869 if (kYes_TriState != fHWBlendState.fEnabled) {
2870 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002871
cdalton8917d622015-05-06 13:40:21 -07002872 fHWBlendState.fEnabled = kYes_TriState;
2873 }
2874
2875 if (fHWBlendState.fEquation != equation) {
2876 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2877 fHWBlendState.fEquation = equation;
2878 }
2879
2880 if (GrBlendEquationIsAdvanced(equation)) {
2881 SkASSERT(this->caps()->advancedBlendEquationSupport());
2882 // Advanced equations have no other blend state.
2883 return;
2884 }
2885
bsalomone63ffef2016-02-05 07:17:34 -08002886 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002887 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2888 gXfermodeCoeff2Blend[dstCoeff]));
2889 fHWBlendState.fSrcCoeff = srcCoeff;
2890 fHWBlendState.fDstCoeff = dstCoeff;
2891 }
2892
bsalomon7f9b2e42016-01-12 13:29:26 -08002893 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2894 GrColor blendConst = blendInfo.fBlendConstant;
2895 blendConst = swizzle.applyTo(blendConst);
2896 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2897 GrGLfloat c[4];
2898 GrColorToRGBAFloat(blendConst, c);
2899 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2900 fHWBlendState.fConstColor = blendConst;
2901 fHWBlendState.fConstColorValid = true;
2902 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002903 }
2904}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002905
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002906static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002907 static const GrGLenum gWrapModes[] = {
2908 GR_GL_CLAMP_TO_EDGE,
2909 GR_GL_REPEAT,
2910 GR_GL_MIRRORED_REPEAT
2911 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002912 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002913 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2914 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2915 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2916 return gWrapModes[tm];
2917}
2918
egdanielb7e7d572015-11-04 04:23:53 -08002919static GrGLenum get_component_enum_from_char(char component) {
2920 switch (component) {
2921 case 'r':
2922 return GR_GL_RED;
2923 case 'g':
2924 return GR_GL_GREEN;
2925 case 'b':
2926 return GR_GL_BLUE;
2927 case 'a':
2928 return GR_GL_ALPHA;
2929 default:
2930 SkFAIL("Unsupported component");
2931 return 0;
2932 }
2933}
2934
2935/** If texture swizzling is available using tex parameters then it is preferred over mangling
2936 the generated shader code. This potentially allows greater reuse of cached shaders. */
2937static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002938 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002939 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002940 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002941 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002942 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002943 }
2944}
2945
Brian Salomon514baff2016-11-17 15:17:07 -05002946void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
Robert Phillips7294b852017-08-01 13:51:44 +00002947 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002948 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002949
reed856e9d92015-09-30 12:21:45 -07002950#ifdef SK_DEBUG
2951 if (!this->caps()->npotTextureTileSupport()) {
2952 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2953 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2954 if (tileX || tileY) {
2955 const int w = texture->width();
2956 const int h = texture->height();
2957 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2958 }
2959 }
2960#endif
2961
bsalomon@google.comb8670992012-07-25 21:27:09 +00002962 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2963 // 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 +00002964 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002965 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002966 if (texRT) {
Robert Phillips7294b852017-08-01 13:51:44 +00002967 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002968 }
2969
Robert Phillips294870f2016-11-11 12:38:40 -05002970 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002971 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002972 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002973 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002974 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002975 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002976 }
2977
bsalomon@google.com4c883782012-06-04 19:05:11 +00002978 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002979 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002980 bool setAll = timestamp < this->getResetTimestamp();
2981 GrGLTexture::TexParams newTexParams;
2982
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002983 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002984 GR_GL_NEAREST,
2985 GR_GL_LINEAR,
2986 GR_GL_LINEAR_MIPMAP_LINEAR
2987 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002988 static GrGLenum glMagFilterModes[] = {
2989 GR_GL_NEAREST,
2990 GR_GL_LINEAR,
2991 GR_GL_LINEAR
2992 };
Brian Salomon514baff2016-11-17 15:17:07 -05002993 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002994
Brian Salomon514baff2016-11-17 15:17:07 -05002995 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04002996 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05002997 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07002998 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002999 }
bsalomonefd7d452014-10-23 14:17:46 -07003000
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003001 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3002 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003003
brianosman20471892016-12-02 06:43:32 -08003004 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003005 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003006 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003007 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003008 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003009 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003010 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003011
brianosman33f6b3f2016-06-02 05:49:21 -07003012#ifdef SK_DEBUG
3013 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05003014 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003015 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3016 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003017 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3018 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3019 : SkDestinationSurfaceColorMode::kLegacy;
3020 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003021 }
3022 }
3023#endif
3024
cblume55f2d2d2016-02-26 13:20:48 -08003025 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3026
bsalomon@google.comb8670992012-07-25 21:27:09 +00003027 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3028 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003029 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003030 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003031 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003032 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003033 }
3034 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3035 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003036 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003037 }
cblume55f2d2d2016-02-26 13:20:48 -08003038 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003039 // These are not supported in ES2 contexts
3040 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3041 if (newTexParams.fMaxMipMapLevel != 0) {
3042 this->setTextureUnit(unitIdx);
3043 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3044 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3045 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3046 newTexParams.fMaxMipMapLevel));
3047 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3048 newTexParams.fMaxMipMapLevel));
3049 }
cblume55f2d2d2016-02-26 13:20:48 -08003050 }
3051 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003052 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003053 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003054 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003055 }
3056 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003057 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003058 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003059 }
bsalomoncdee0092016-01-08 13:20:12 -08003060 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003061 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3062 oldTexParams.fSwizzleRGBA,
3063 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003064 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003065 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003066 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003067}
3068
csmartdalton1897cfd2016-06-03 08:50:54 -07003069void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003070 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3071 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003072
3073 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3074
3075 if (!buffTex.fKnownBound) {
3076 if (!buffTex.fTextureID) {
3077 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3078 if (!buffTex.fTextureID) {
3079 return;
3080 }
3081 }
3082
3083 this->setTextureUnit(unitIdx);
3084 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3085
3086 buffTex.fKnownBound = true;
3087 }
3088
robertphillips8abb3702016-08-31 14:04:06 -07003089 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003090 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003091
3092 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003093 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3094 this->glCaps().configSizedInternalFormat(texelConfig),
3095 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003096
cdalton74b8d322016-04-11 14:47:28 -07003097 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003098 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003099
3100 if (this->glCaps().textureSwizzleSupport() &&
3101 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3102 GrGLenum glSwizzle[4];
3103 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3104 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3105 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3106 }
3107
3108 buffer->setHasAttachedToTexture();
3109 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3110 }
3111}
3112
Brian Salomonf9f45122016-11-29 11:59:17 -05003113void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3114 SkASSERT(texture);
3115 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3116 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003117 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003118 switch (ioType) {
3119 case kRead_GrIOType:
3120 access = GR_GL_READ_ONLY;
3121 break;
3122 case kWrite_GrIOType:
3123 access = GR_GL_WRITE_ONLY;
3124 break;
3125 case kRW_GrIOType:
3126 access = GR_GL_READ_WRITE;
3127 break;
3128 }
3129 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3130 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3131 }
3132}
3133
Brian Salomon514baff2016-11-17 15:17:07 -05003134void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
Robert Phillips7294b852017-08-01 13:51:44 +00003135 GrGLTexture* texture) {
brianosman33f6b3f2016-06-02 05:49:21 -07003136 SkASSERT(texture);
3137
3138 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003139 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003140
Brian Salomon514baff2016-11-17 15:17:07 -05003141 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003142 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05003143 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003144 }
3145 }
3146
Brian Salomon514baff2016-11-17 15:17:07 -05003147 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003148 return;
3149 }
3150
3151 // If this is an sRGB texture and the mips were previously built the "other" way
3152 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3153 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003154 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3155 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3156 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003157 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003158 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003159 texture->texturePriv().dirtyMipMaps(true);
3160 }
3161
3162 // If the mips aren't dirty, we're done:
3163 if (!texture->texturePriv().mipMapsAreDirty()) {
3164 return;
3165 }
3166
3167 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3168 // from the rt it will still be the last bound texture, but it needs resolving.
3169 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3170 if (texRT) {
Robert Phillips7294b852017-08-01 13:51:44 +00003171 this->onResolveRenderTarget(texRT);
brianosman33f6b3f2016-06-02 05:49:21 -07003172 }
3173
3174 GrGLenum target = texture->target();
3175 this->setScratchTextureUnit();
3176 GL_CALL(BindTexture(target, texture->textureID()));
3177
3178 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3179 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003180 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003181 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3182 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3183 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3184 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3185 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3186 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3187 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3188 srgbDecode = GR_GL_DECODE_EXT;
3189 }
3190 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003191 }
3192
3193 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillips7294b852017-08-01 13:51:44 +00003194 if (!this->generateMipmap(texture, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003195 GL_CALL(GenerateMipmap(target));
3196 }
3197
brianosmanfe199872016-06-13 07:59:48 -07003198 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003199 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3200 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003201 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003202
3203 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3204 texture->textureParamsModified();
3205}
3206
cdalton74b8d322016-04-11 14:47:28 -07003207void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3208 this->setTextureUnit(unitIdx);
3209 if (this->glStandard() == kGLES_GrGLStandard) {
3210 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3211 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3212 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3213 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3214 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3215 } else {
3216 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3217 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3218 reinterpret_cast<const GrGLint*>(swizzle)));
3219 }
3220}
3221
egdaniel080e6732014-12-22 07:35:52 -08003222void GrGLGpu::flushColorWrite(bool writeColor) {
3223 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003224 if (kNo_TriState != fHWWriteToColor) {
3225 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3226 GR_GL_FALSE, GR_GL_FALSE));
3227 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003228 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003229 } else {
3230 if (kYes_TriState != fHWWriteToColor) {
3231 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3232 fHWWriteToColor = kYes_TriState;
3233 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003234 }
bsalomon3e791242014-12-17 13:43:13 -08003235}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003236
bsalomon861e1032014-12-16 07:33:49 -08003237void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003238 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003239 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003240 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003241 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003242 }
3243}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003244
bsalomon861e1032014-12-16 07:33:49 -08003245void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003246 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003247 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003248 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3249 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3250 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003251 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003252 // clear out the this field so that if a program does use this unit it will rebind the correct
3253 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003254 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003255}
3256
Brian Salomone5e7eb12016-10-14 16:18:33 -04003257// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillips7294b852017-08-01 13:51:44 +00003258static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3259 const GrSurface* src,
3260 const SkIRect& srcRect,
3261 const SkIPoint& dstPoint,
3262 const GrGLGpu* gpu) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003263 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003264 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3265 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003266 return false;
3267 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003268 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3269 // have filtered such cases out.
3270 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003271 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3272 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3273 const GrRenderTarget* dstRT = dst->asRenderTarget();
3274 const GrRenderTarget* srcRT = src->asRenderTarget();
3275 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3276 return false;
3277 }
3278 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3279 return false;
3280 }
3281 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3282 return false;
3283 }
3284 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3285 // We would mirror to compensate for origin changes. Note that copySurface is
3286 // specified such that the src and dst rects are the same.
Robert Phillips7294b852017-08-01 13:51:44 +00003287 if (dst->origin() != src->origin()) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003288 return false;
3289 }
3290 }
3291 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003292 if (srcRT && srcRT->numColorSamples()) {
3293 if (dstRT && !dstRT->numColorSamples()) {
3294 return false;
3295 }
3296 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3297 return false;
3298 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003299 }
3300 }
3301 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3302 if (dstRT && dstRT->numColorSamples() > 0) {
3303 return false;
3304 }
3305 }
3306 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3307 if (dst->config() != src->config()) {
3308 return false;
3309 }
3310 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3311 const GrRenderTarget* srcRT = src->asRenderTarget();
3312 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3313 return false;
3314 }
3315 }
3316 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003317 if (srcRT && srcRT->numColorSamples()) {
3318 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3319 return false;
3320 }
Robert Phillips7294b852017-08-01 13:51:44 +00003321 if (dst->origin() != src->origin()) {
Eric Karl74480882017-04-03 14:49:05 -07003322 return false;
3323 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003324 }
3325 }
3326 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003327}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003328
Robert Phillips7294b852017-08-01 13:51:44 +00003329static inline bool can_copy_texsubimage(const GrSurface* dst,
3330 const GrSurface* src,
bsalomon7ea33f52015-11-22 14:51:00 -08003331 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003332 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3333 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3334 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003335 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003336 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3337 return false;
3338 }
3339 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3340 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3341 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003342 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003343 return false;
3344 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003345 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3346 // If the src is multisampled (and uses an extension where there is a separate MSAA
3347 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003348 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003349 return false;
3350 }
bsalomon7ea33f52015-11-22 14:51:00 -08003351
3352 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3353 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3354 // texture.
3355 if (!dstTex) {
3356 return false;
3357 }
3358
3359 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003360
bsalomon7ea33f52015-11-22 14:51:00 -08003361 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3362 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003363 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003364 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
Robert Phillips7294b852017-08-01 13:51:44 +00003365 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003366 return true;
3367 } else {
3368 return false;
3369 }
3370}
3371
3372// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3373// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003374void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3375 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003376 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003377 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003378 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003379 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3380 GrGLuint texID = texture->textureID();
3381 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003382 GrGLuint* tempFBOID;
3383 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003384
egdanield803f272015-03-18 13:01:52 -07003385 if (0 == *tempFBOID) {
3386 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003387 }
3388
egdanield803f272015-03-18 13:01:52 -07003389 fStats.incRenderTargetBinds();
3390 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3391 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003392 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003393 target,
robertphillips754f4e92014-09-18 13:52:08 -07003394 texID,
3395 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003396 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003397 viewport->fLeft = 0;
3398 viewport->fBottom = 0;
3399 viewport->fWidth = surface->width();
3400 viewport->fHeight = surface->height();
3401 } else {
egdanield803f272015-03-18 13:01:52 -07003402 fStats.incRenderTargetBinds();
3403 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003404 *viewport = rt->getViewport();
3405 }
egdaniel0f5f9672015-02-03 11:10:51 -08003406}
3407
Brian Salomon71d9d842016-11-03 13:42:00 -04003408void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3409 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003410 if (!surface->asRenderTarget()) {
3411 SkASSERT(surface->asTexture());
3412 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3413 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3414 GR_GL_COLOR_ATTACHMENT0,
3415 textureTarget,
3416 0,
3417 0));
3418 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003419}
3420
Robert Phillips7294b852017-08-01 13:51:44 +00003421bool GrGLGpu::onCopySurface(GrSurface* dst,
3422 GrSurface* src,
3423 const SkIRect& srcRect,
3424 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003425 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3426 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003427 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3428 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003429 return false;
3430 }
bsalomon083617b2016-02-12 12:10:14 -08003431 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3432 bool preferCopy = SkToBool(dst->asRenderTarget());
3433 if (preferCopy && src->asTexture()) {
Robert Phillips7294b852017-08-01 13:51:44 +00003434 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003435 return true;
3436 }
bsalomon5df6fee2015-05-18 06:26:15 -07003437 }
cblume61214052016-01-26 09:10:48 -08003438
Robert Phillips7294b852017-08-01 13:51:44 +00003439 if (can_copy_texsubimage(dst, src, this)) {
3440 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003441 return true;
3442 }
3443
Robert Phillips7294b852017-08-01 13:51:44 +00003444 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
3445 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003446 }
3447
bsalomon083617b2016-02-12 12:10:14 -08003448 if (!preferCopy && src->asTexture()) {
Robert Phillips7294b852017-08-01 13:51:44 +00003449 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003450 return true;
3451 }
bsalomon083617b2016-02-12 12:10:14 -08003452 }
3453
bsalomon6df86402015-06-01 10:41:49 -07003454 return false;
3455}
3456
Brian Salomonbf7b6202016-11-11 16:08:03 -05003457bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003458 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003459
Brian Salomonbf7b6202016-11-11 16:08:03 -05003460 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003461 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003462 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003463
3464 if (!fCopyProgramArrayBuffer) {
3465 static const GrGLfloat vdata[] = {
3466 0, 0,
3467 0, 1,
3468 1, 0,
3469 1, 1
3470 };
3471 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3472 kStatic_GrAccessPattern, vdata));
3473 }
3474 if (!fCopyProgramArrayBuffer) {
3475 return false;
3476 }
3477
3478 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3479 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3480 if (!fCopyPrograms[progIdx].fProgram) {
3481 return false;
3482 }
3483
Brian Salomon1edc5b92016-11-29 13:43:46 -05003484 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003485 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3486 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3487 GrShaderVar::kUniform_TypeModifier);
3488 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3489 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3490 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3491 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003492
3493 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003494 if (shaderCaps->noperspectiveInterpolationSupport()) {
3495 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003496 vshaderTxt.appendf("#extension %s : require\n", extension);
3497 }
3498 vTexCoord.addModifier("noperspective");
3499 }
3500
Brian Salomon1edc5b92016-11-29 13:43:46 -05003501 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003502 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003503 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003504 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003505 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003506 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003507 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003508 vshaderTxt.append(";");
3509
3510 vshaderTxt.append(
3511 "// Copy Program VS\n"
3512 "void main() {"
3513 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3514 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003515 " gl_Position.zw = float2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003516 "}"
3517 );
3518
3519 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003520 if (shaderCaps->noperspectiveInterpolationSupport()) {
3521 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003522 fshaderTxt.appendf("#extension %s : require\n", extension);
3523 }
3524 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003525 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003526 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003527 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003528 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003529 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003530 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003531 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003532 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003533 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003534 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003535 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003536 fshaderTxt.appendf(
3537 "// Copy Program FS\n"
3538 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003539 " sk_FragColor = texture(u_texture, v_texCoord);"
3540 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003541 );
3542
3543 const char* str;
3544 GrGLint length;
3545
3546 str = vshaderTxt.c_str();
3547 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003548 SkSL::Program::Settings settings;
3549 settings.fCaps = shaderCaps;
3550 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003551 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3552 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003553 &fStats, settings, &inputs);
3554 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003555
3556 str = fshaderTxt.c_str();
3557 length = SkToInt(fshaderTxt.size());
3558 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3559 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003560 &fStats, settings, &inputs);
3561 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003562
3563 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3564
3565 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3566 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3567 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3568 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3569 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3570 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3571
3572 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3573
3574 GL_CALL(DeleteShader(vshader));
3575 GL_CALL(DeleteShader(fshader));
3576
3577 return true;
bsalomon6df86402015-06-01 10:41:49 -07003578}
3579
brianosman33f6b3f2016-06-02 05:49:21 -07003580bool GrGLGpu::createMipmapProgram(int progIdx) {
3581 const bool oddWidth = SkToBool(progIdx & 0x2);
3582 const bool oddHeight = SkToBool(progIdx & 0x1);
3583 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3584
Brian Salomon1edc5b92016-11-29 13:43:46 -05003585 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003586
3587 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3588 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3589 if (!fMipmapPrograms[progIdx].fProgram) {
3590 return false;
3591 }
3592
Brian Salomon1edc5b92016-11-29 13:43:46 -05003593 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003594 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3595 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3596 GrShaderVar::kUniform_TypeModifier);
3597 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3598 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003599 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003600 GrShaderVar vTexCoords[] = {
3601 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3602 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3603 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3604 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003605 };
Brian Salomon99938a82016-11-21 13:41:08 -05003606 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003607
3608 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003609 if (shaderCaps->noperspectiveInterpolationSupport()) {
3610 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003611 vshaderTxt.appendf("#extension %s : require\n", extension);
3612 }
3613 vTexCoords[0].addModifier("noperspective");
3614 vTexCoords[1].addModifier("noperspective");
3615 vTexCoords[2].addModifier("noperspective");
3616 vTexCoords[3].addModifier("noperspective");
3617 }
3618
Brian Salomon1edc5b92016-11-29 13:43:46 -05003619 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003620 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003621 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003622 vshaderTxt.append(";");
3623 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003624 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003625 vshaderTxt.append(";");
3626 }
3627
3628 vshaderTxt.append(
3629 "// Mipmap Program VS\n"
3630 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003631 " gl_Position.xy = a_vertex * float2(2, 2) - float2(1, 1);"
3632 " gl_Position.zw = float2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003633 );
3634
3635 // Insert texture coordinate computation:
3636 if (oddWidth && oddHeight) {
3637 vshaderTxt.append(
3638 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003639 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + float2(u_texCoordXform.x, 0);"
3640 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + float2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003641 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3642 );
3643 } else if (oddWidth) {
3644 vshaderTxt.append(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003645 " v_texCoord0 = a_vertex.xy * float2(u_texCoordXform.y, 1);"
3646 " v_texCoord1 = a_vertex.xy * float2(u_texCoordXform.y, 1) + float2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003647 );
3648 } else if (oddHeight) {
3649 vshaderTxt.append(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003650 " v_texCoord0 = a_vertex.xy * float2(1, u_texCoordXform.w);"
3651 " v_texCoord1 = a_vertex.xy * float2(1, u_texCoordXform.w) + float2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003652 );
3653 } else {
3654 vshaderTxt.append(
3655 " v_texCoord0 = a_vertex.xy;"
3656 );
3657 }
3658
3659 vshaderTxt.append("}");
3660
3661 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003662 if (shaderCaps->noperspectiveInterpolationSupport()) {
3663 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003664 fshaderTxt.appendf("#extension %s : require\n", extension);
3665 }
3666 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003667 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003668 &fshaderTxt);
3669 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003670 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003671 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003672 fshaderTxt.append(";");
3673 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003674 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003675 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003676 fshaderTxt.append(
3677 "// Mipmap Program FS\n"
3678 "void main() {"
3679 );
3680
3681 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003682 fshaderTxt.append(
3683 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3684 " texture(u_texture, v_texCoord1) + "
3685 " texture(u_texture, v_texCoord2) + "
3686 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003687 );
3688 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003689 fshaderTxt.append(
3690 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3691 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003692 );
3693 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003694 fshaderTxt.append(
3695 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003696 );
3697 }
3698
3699 fshaderTxt.append("}");
3700
3701 const char* str;
3702 GrGLint length;
3703
3704 str = vshaderTxt.c_str();
3705 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003706 SkSL::Program::Settings settings;
3707 settings.fCaps = shaderCaps;
3708 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003709 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3710 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003711 &fStats, settings, &inputs);
3712 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003713
3714 str = fshaderTxt.c_str();
3715 length = SkToInt(fshaderTxt.size());
3716 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3717 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003718 &fStats, settings, &inputs);
3719 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003720
3721 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3722
3723 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3724 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3725 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3726 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3727
3728 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3729
3730 GL_CALL(DeleteShader(vshader));
3731 GL_CALL(DeleteShader(fshader));
3732
3733 return true;
3734}
3735
Mike Klein31550db2017-06-06 23:29:53 +00003736bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003737 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003738
Mike Klein31550db2017-06-06 23:29:53 +00003739 if (!fStencilClipClearArrayBuffer) {
3740 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3741 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3742 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3743 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003744 return false;
3745 }
3746 }
3747
Mike Klein31550db2017-06-06 23:29:53 +00003748 SkASSERT(!fStencilClipClearProgram);
3749 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3750 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003751 return false;
3752 }
3753
Brian Salomon99938a82016-11-21 13:41:08 -05003754 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003755 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003756
bsalomon6dea83f2015-12-03 12:58:06 -08003757 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003758 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003759 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003760 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003761 "// Stencil Clip Clear Program VS\n"
3762 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003763 " gl_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003764 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003765
3766 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003767 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003768 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003769 &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003770 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003771 "// Stencil Clip Clear Program FS\n"
3772 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003773 " sk_FragColor = float4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003774 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003775
bsalomon6dea83f2015-12-03 12:58:06 -08003776 const char* str;
3777 GrGLint length;
3778
3779 str = vshaderTxt.c_str();
3780 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003781 SkSL::Program::Settings settings;
3782 settings.fCaps = this->caps()->shaderCaps();
3783 SkSL::Program::Inputs inputs;
Mike Klein31550db2017-06-06 23:29:53 +00003784 GrGLuint vshader =
3785 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_VERTEX_SHADER,
3786 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003787 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003788
3789 str = fshaderTxt.c_str();
3790 length = SkToInt(fshaderTxt.size());
Mike Klein31550db2017-06-06 23:29:53 +00003791 GrGLuint fshader =
3792 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_FRAGMENT_SHADER,
3793 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003794 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003795
Mike Klein31550db2017-06-06 23:29:53 +00003796 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003797
Mike Klein31550db2017-06-06 23:29:53 +00003798 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003799
3800 GL_CALL(DeleteShader(vshader));
3801 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003802
3803 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003804}
3805
Mike Klein31550db2017-06-06 23:29:53 +00003806void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillips7294b852017-08-01 13:51:44 +00003807 GrRenderTarget* rt) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003808 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3809 // visualization.
3810
bsalomon6dea83f2015-12-03 12:58:06 -08003811 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003812 if (!fStencilClipClearProgram) {
3813 if (!this->createStencilClipClearProgram()) {
3814 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003815 return;
3816 }
bsalomon6dea83f2015-12-03 12:58:06 -08003817 }
3818
Mike Klein8b06ed72017-06-06 21:47:32 +00003819 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003820 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003821
Mike Klein31550db2017-06-06 23:29:53 +00003822 GL_CALL(UseProgram(fStencilClipClearProgram));
3823 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003824
cdaltone2e71c22016-04-07 18:13:29 -07003825 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003826
cdaltone2e71c22016-04-07 18:13:29 -07003827 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003828 attribs->enableVertexArrays(this, 1);
Mike Klein31550db2017-06-06 23:29:53 +00003829 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kVec2f_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003830 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003831
bsalomon6dea83f2015-12-03 12:58:06 -08003832 GrXferProcessor::BlendInfo blendInfo;
3833 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003834 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003835 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003836 this->flushHWAAState(glRT, false, false);
Robert Phillips7294b852017-08-01 13:51:44 +00003837 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
3838 this->flushWindowRectangles(clip.windowRectsState(), glRT);
Mike Klein31550db2017-06-06 23:29:53 +00003839 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3840 // This should only be called internally when we know we have a stencil buffer.
3841 SkASSERT(sb);
3842 GrStencilSettings settings = GrStencilSettings(
3843 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3844 this->flushStencil(settings);
3845 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003846}
3847
3848
Robert Phillips7294b852017-08-01 13:51:44 +00003849bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3850 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003851 const SkIRect& srcRect,
3852 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003853 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003854 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003855
3856 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003857 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003858 SkDebugf("Failed to create copy program.\n");
3859 return false;
3860 }
3861 }
3862
bsalomon6df86402015-06-01 10:41:49 -07003863 int w = srcRect.width();
3864 int h = srcRect.height();
3865
Brian Salomon514baff2016-11-17 15:17:07 -05003866 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
Robert Phillips7294b852017-08-01 13:51:44 +00003867 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003868
bsalomon083617b2016-02-12 12:10:14 -08003869 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003870 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003871 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003872 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003873
bsalomon6df86402015-06-01 10:41:49 -07003874 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003875
bsalomon7ea33f52015-11-22 14:51:00 -08003876 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3877 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003878
cdaltone2e71c22016-04-07 18:13:29 -07003879 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003880
cdaltone2e71c22016-04-07 18:13:29 -07003881 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003882 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003883 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3884 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003885
3886 // dst rect edges in NDC (-1 to 1)
3887 int dw = dst->width();
3888 int dh = dst->height();
3889 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3890 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3891 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3892 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillips7294b852017-08-01 13:51:44 +00003893 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07003894 dy0 = -dy0;
3895 dy1 = -dy1;
3896 }
3897
bsalomone5286e02016-01-14 09:24:09 -08003898 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3899 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3900 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3901 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003902 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003903 int sh = src->height();
Robert Phillips7294b852017-08-01 13:51:44 +00003904 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003905 sy0 = sh - sy0;
3906 sy1 = sh - sy1;
3907 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04003908 // src rect edges in normalized texture space (0 to 1)
3909 sx0 /= sw;
3910 sx1 /= sw;
3911 sy0 /= sh;
3912 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003913
bsalomon7ea33f52015-11-22 14:51:00 -08003914 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3915 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3916 sx1 - sx0, sy1 - sy0, sx0, sy0));
3917 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003918
3919 GrXferProcessor::BlendInfo blendInfo;
3920 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003921 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003922 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08003923 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003924 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003925 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003926 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003927 if (this->glCaps().srgbWriteControl()) {
3928 this->flushFramebufferSRGB(true);
3929 }
bsalomon6df86402015-06-01 10:41:49 -07003930
3931 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003932 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08003933 this->didWriteToSurface(dst, &dstRect);
3934
cdaltone2e71c22016-04-07 18:13:29 -07003935 return true;
bsalomon6df86402015-06-01 10:41:49 -07003936}
3937
Robert Phillips7294b852017-08-01 13:51:44 +00003938void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3939 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003940 const SkIRect& srcRect,
3941 const SkIPoint& dstPoint) {
Robert Phillips7294b852017-08-01 13:51:44 +00003942 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003943 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003944 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003945 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003946 SkASSERT(dstTex);
3947 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003948 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003949 GrGLIRect srcGLRect;
Robert Phillips7294b852017-08-01 13:51:44 +00003950 srcGLRect.setRelativeTo(srcVP, srcRect, src->origin());
bsalomon6df86402015-06-01 10:41:49 -07003951
3952 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003953 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003954 GrGLint dstY;
Robert Phillips7294b852017-08-01 13:51:44 +00003955 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07003956 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3957 } else {
3958 dstY = dstPoint.fY;
3959 }
bsalomon10528f12015-10-14 12:54:52 -07003960 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003961 dstPoint.fX, dstY,
3962 srcGLRect.fLeft, srcGLRect.fBottom,
3963 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003964 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003965 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3966 srcRect.width(), srcRect.height());
3967 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003968}
3969
Robert Phillips7294b852017-08-01 13:51:44 +00003970bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3971 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003972 const SkIRect& srcRect,
3973 const SkIPoint& dstPoint) {
Robert Phillips7294b852017-08-01 13:51:44 +00003974 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07003975 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3976 srcRect.width(), srcRect.height());
3977 if (dst == src) {
3978 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3979 return false;
mtklein404b3b22015-05-18 09:29:10 -07003980 }
bsalomon5df6fee2015-05-18 06:26:15 -07003981 }
bsalomon6df86402015-06-01 10:41:49 -07003982
bsalomon6df86402015-06-01 10:41:49 -07003983 GrGLIRect dstVP;
3984 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003985 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3986 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003987 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003988 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003989 GrGLIRect srcGLRect;
3990 GrGLIRect dstGLRect;
Robert Phillips7294b852017-08-01 13:51:44 +00003991 srcGLRect.setRelativeTo(srcVP, srcRect, src->origin());
3992 dstGLRect.setRelativeTo(dstVP, dstRect, dst->origin());
bsalomon6df86402015-06-01 10:41:49 -07003993
3994 // BlitFrameBuffer respects the scissor, so disable it.
3995 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003996 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003997
3998 GrGLint srcY0;
3999 GrGLint srcY1;
4000 // Does the blit need to y-mirror or not?
Robert Phillips7294b852017-08-01 13:51:44 +00004001 if (src->origin() == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07004002 srcY0 = srcGLRect.fBottom;
4003 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4004 } else {
4005 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4006 srcY1 = srcGLRect.fBottom;
4007 }
4008 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4009 srcY0,
4010 srcGLRect.fLeft + srcGLRect.fWidth,
4011 srcY1,
4012 dstGLRect.fLeft,
4013 dstGLRect.fBottom,
4014 dstGLRect.fLeft + dstGLRect.fWidth,
4015 dstGLRect.fBottom + dstGLRect.fHeight,
4016 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004017 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4018 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004019 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004020 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004021}
4022
brianosman33f6b3f2016-06-02 05:49:21 -07004023// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4024// Uses draw calls to do a series of downsample operations to successive mips.
4025// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillips7294b852017-08-01 13:51:44 +00004026bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004027 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004028 // Our iterative downsample requires the ability to limit which level we're sampling:
4029 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004030 return false;
4031 }
4032
4033 // Mipmaps are only supported on 2D textures:
4034 if (GR_GL_TEXTURE_2D != texture->target()) {
4035 return false;
4036 }
4037
4038 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004039 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004040 return false;
4041 }
4042
brianosman33f6b3f2016-06-02 05:49:21 -07004043 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4044 if (GrPixelConfigIsSRGB(texture->config())) {
4045 // If we have write-control, just set the state that we want:
4046 if (this->glCaps().srgbWriteControl()) {
4047 this->flushFramebufferSRGB(gammaCorrect);
4048 } else if (!gammaCorrect) {
4049 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4050 return false;
4051 }
4052 }
4053
4054 int width = texture->width();
4055 int height = texture->height();
4056 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4057
4058 // Define all mips, if we haven't previously done so:
4059 if (0 == texture->texturePriv().maxMipMapLevel()) {
4060 GrGLenum internalFormat;
4061 GrGLenum externalFormat;
4062 GrGLenum externalType;
4063 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4064 &internalFormat, &externalFormat, &externalType)) {
4065 return false;
4066 }
4067
4068 for (GrGLint level = 1; level < levelCount; ++level) {
4069 // Define the next mip:
4070 width = SkTMax(1, width / 2);
4071 height = SkTMax(1, height / 2);
4072 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4073 width, height, 0,
4074 externalFormat, externalType, nullptr));
4075 }
4076 }
4077
4078 // Create (if necessary), then bind temporary FBO:
4079 if (0 == fTempDstFBOID) {
4080 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4081 }
4082 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004083 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004084
4085 // Bind the texture, to get things configured for filtering.
4086 // We'll be changing our base level further below:
4087 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004088 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
Robert Phillips7294b852017-08-01 13:51:44 +00004089 this->bindTexture(0, params, gammaCorrect, texture);
brianosman33f6b3f2016-06-02 05:49:21 -07004090
4091 // Vertex data:
4092 if (!fMipmapProgramArrayBuffer) {
4093 static const GrGLfloat vdata[] = {
4094 0, 0,
4095 0, 1,
4096 1, 0,
4097 1, 1
4098 };
4099 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4100 kVertex_GrBufferType,
4101 kStatic_GrAccessPattern, vdata));
4102 }
4103 if (!fMipmapProgramArrayBuffer) {
4104 return false;
4105 }
4106
4107 fHWVertexArrayState.setVertexArrayID(this, 0);
4108
4109 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004110 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05004111 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004112 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004113
4114 // Set "simple" state once:
4115 GrXferProcessor::BlendInfo blendInfo;
4116 blendInfo.reset();
4117 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4118 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004119 this->flushHWAAState(nullptr, false, false);
4120 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004121 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004122 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004123
4124 // Do all the blits:
4125 width = texture->width();
4126 height = texture->height();
4127 GrGLIRect viewport;
4128 viewport.fLeft = 0;
4129 viewport.fBottom = 0;
4130 for (GrGLint level = 1; level < levelCount; ++level) {
4131 // Get and bind the program for this particular downsample (filter shape can vary):
4132 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4133 if (!fMipmapPrograms[progIdx].fProgram) {
4134 if (!this->createMipmapProgram(progIdx)) {
4135 SkDebugf("Failed to create mipmap program.\n");
4136 return false;
4137 }
4138 }
4139 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4140 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4141
4142 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4143 const float invWidth = 1.0f / width;
4144 const float invHeight = 1.0f / height;
4145 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4146 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4147 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4148
4149 // Only sample from previous mip
4150 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4151
4152 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4153 GR_GL_TEXTURE_2D, texture->textureID(), level));
4154
4155 width = SkTMax(1, width / 2);
4156 height = SkTMax(1, height / 2);
4157 viewport.fWidth = width;
4158 viewport.fHeight = height;
4159 this->flushViewport(viewport);
4160
4161 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4162 }
4163
4164 // Unbind:
4165 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4166 GR_GL_TEXTURE_2D, 0, 0));
4167
4168 return true;
4169}
4170
Robert Phillips7294b852017-08-01 13:51:44 +00004171void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
csmartdaltonc25c5d72016-11-01 07:03:59 -07004172 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004173 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4174 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004175
4176 this->flushStencil(stencil);
4177 this->flushHWAAState(rt, true, !stencil.isDisabled());
4178 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4179
4180 if (0 != this->caps()->maxRasterSamples()) {
4181 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4182 } else {
4183 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4184 }
4185
Brian Salomon154ce912017-05-17 10:40:02 -04004186 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004187
4188 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004189 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004190 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4191 GrGLfloat pos[2];
4192 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
Robert Phillips7294b852017-08-01 13:51:44 +00004193 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004194 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004195 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004196 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004197 }
4198 }
4199 }
4200}
4201
cdalton231c5fd2015-05-13 12:35:36 -07004202void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004203 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004204 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004205 case kTexture_GrXferBarrierType: {
4206 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4207 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4208 // The render target uses separate storage so no need for glTextureBarrier.
4209 // FIXME: The render target will resolve automatically when its texture is bound,
4210 // but we could resolve only the bounds that will be read if we do it here instead.
4211 return;
4212 }
cdalton9954bc32015-04-29 14:17:00 -07004213 SkASSERT(this->caps()->textureBarrierSupport());
4214 GL_CALL(TextureBarrier());
4215 return;
cdalton231c5fd2015-05-13 12:35:36 -07004216 }
cdalton8917d622015-05-06 13:40:21 -07004217 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004218 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004219 this->caps()->blendEquationSupport());
4220 GL_CALL(BlendBarrier());
4221 return;
bsalomoncb02b382015-08-12 11:14:50 -07004222 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004223 }
4224}
4225
jvanverth88957922015-07-14 11:02:52 -07004226GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004227 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004228 if (!this->caps()->isConfigTexturable(config)) {
4229 return false;
4230 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004231 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004232 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004233 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004234 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004235 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4236 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004237 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004238 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004239 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4240 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4241 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4242 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004243
bsalomon76148af2016-01-12 11:13:47 -08004244 GrGLenum internalFormat;
4245 GrGLenum externalFormat;
4246 GrGLenum externalType;
4247
4248 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4249 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004250 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004251 }
jvanverth672bb7f2015-07-13 07:19:57 -07004252
bsalomon091f60c2015-11-10 11:54:56 -08004253 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004254 externalType, pixels));
4255
Ben Wagner18b61f92016-10-25 10:44:02 -04004256 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004257}
4258
jvanverth88957922015-07-14 11:02:52 -07004259bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004260 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004261
4262 GrGLboolean result;
4263 GL_CALL_RET(result, IsTexture(texID));
4264
4265 return (GR_GL_TRUE == result);
4266}
4267
bsalomone63ffef2016-02-05 07:17:34 -08004268void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004269 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004270 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004271
bsalomon67d76202015-11-11 12:40:42 -08004272 if (!abandonTexture) {
4273 GL_CALL(DeleteTextures(1, &texID));
4274 }
jvanverth672bb7f2015-07-13 07:19:57 -07004275}
4276
joshualitt8fd844f2015-12-02 13:36:47 -08004277void GrGLGpu::resetShaderCacheForTesting() const {
4278 fProgramCache->abandon();
4279}
4280
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004281///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004282
cdaltone2e71c22016-04-07 18:13:29 -07004283GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004284 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004285 GrGLAttribArrayState* attribState;
4286
cdaltone2e71c22016-04-07 18:13:29 -07004287 if (gpu->glCaps().isCoreProfile()) {
4288 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004289 GrGLuint arrayID;
4290 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4291 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004292 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004293 }
cdaltone2e71c22016-04-07 18:13:29 -07004294 if (ibuf) {
4295 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004296 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004297 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004298 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004299 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004300 if (ibuf) {
4301 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4302 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004303 } else {
4304 this->setVertexArrayID(gpu, 0);
4305 }
4306 int attrCount = gpu->glCaps().maxVertexAttributes();
4307 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4308 fDefaultVertexArrayAttribState.resize(attrCount);
4309 }
4310 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004311 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004312 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004313}
bsalomone179a912016-01-20 06:18:10 -08004314
Robert Phillips3798c862017-03-27 11:08:16 -04004315bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004316 const GrSamplerParams& textureParams,
4317 GrTextureProducer::CopyParams* copyParams,
4318 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004319 const GrTexture* texture = proxy->priv().peekTexture();
4320 if (!texture) {
4321 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4322 // In that case the proxy should already be instantiated.
4323 return false;
4324 }
4325
bsalomone179a912016-01-20 06:18:10 -08004326 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004327 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004328 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004329 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4330 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004331 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004332 copyParams->fWidth = texture->width();
4333 copyParams->fHeight = texture->height();
4334 return true;
4335 }
4336 }
4337 return false;
4338}
jvanverth84741b32016-09-30 08:39:02 -07004339
Greg Daniel51316782017-08-02 15:10:09 +00004340void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4341 // If we inserted semaphores during the flush, we need to call GLFlush.
4342 if (insertedSemaphore) {
4343 GL_CALL(Flush());
4344 }
4345}
4346
Greg Daniel6be35232017-03-01 17:01:09 -05004347GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004348 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004349 GrGLsync sync;
4350 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4351 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4352 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004353}
4354
Greg Daniel6be35232017-03-01 17:01:09 -05004355bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004356 GrGLenum result;
4357 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4358 return (GR_GL_CONDITION_SATISFIED == result);
4359}
4360
4361void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004362 this->deleteSync((GrGLsync)fence);
4363}
4364
Greg Daniela5cb7812017-06-16 09:45:32 -04004365sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004366 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004367 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004368}
4369
Greg Daniela5cb7812017-06-16 09:45:32 -04004370sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4371 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004372 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004373 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4374}
4375
Brian Osmandc87c952017-04-28 13:57:38 -04004376void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004377 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4378
4379 GrGLsync sync;
4380 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4381 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004382
4383 if (flush) {
4384 GL_CALL(Flush());
4385 }
Greg Daniel6be35232017-03-01 17:01:09 -05004386}
4387
4388void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4389 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4390
4391 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4392}
4393
4394void GrGLGpu::deleteSync(GrGLsync sync) const {
4395 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004396}
Brian Osman13dddce2017-05-09 13:19:50 -04004397
4398sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4399 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004400 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004401 this->insertSemaphore(semaphore, true);
4402
4403 return semaphore;
4404}
Robert Phillips646e4292017-06-13 12:44:56 -04004405
4406int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4407 switch (texture->texturePriv().samplerType()) {
4408 case kTexture2DSampler_GrSLType:
4409 return 0;
4410 case kITexture2DSampler_GrSLType:
4411 return 1;
4412 case kTexture2DRectSampler_GrSLType:
4413 return 2;
4414 case kTextureExternalSampler_GrSLType:
4415 return 3;
4416 default:
4417 SkFAIL("Unexpected samper type");
4418 return 0;
4419 }
4420}