blob: c20cdf048f4a69804e70808edf18089ff55e5660 [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 Phillips19e51dc2017-08-09 09:30:51 -04001975void GrGLGpu::clear(const GrFixedClip& clip, GrColor color,
1976 GrRenderTarget* target, GrSurfaceOrigin origin) {
egdaniel9cb63402016-06-23 08:37:05 -07001977 this->handleDirtyContext();
1978
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001979 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001980 SkASSERT(target);
bsalomonb0bd4f62014-09-03 07:19:50 -07001981 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001982
csmartdalton29df7602016-08-31 11:55:52 -07001983 this->flushRenderTarget(glRT, clip.scissorEnabled() ? &clip.scissorRect() : nullptr);
Robert Phillips19e51dc2017-08-09 09:30:51 -04001984 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillips7294b852017-08-01 13:51:44 +00001985 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.com74b98712011-11-11 19:46:16 +00001986
1987 GrGLfloat r, g, b, a;
1988 static const GrGLfloat scale255 = 1.f / 255.f;
1989 a = GrColorUnpackA(color) * scale255;
1990 GrGLfloat scaleRGB = scale255;
bsalomon@google.com74b98712011-11-11 19:46:16 +00001991 r = GrColorUnpackR(color) * scaleRGB;
1992 g = GrColorUnpackG(color) * scaleRGB;
1993 b = GrColorUnpackB(color) * scaleRGB;
1994
1995 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00001996 fHWWriteToColor = kYes_TriState;
Brian Salomon028a9a52017-05-11 11:39:08 -04001997
Eric Karlaeaf22b2017-05-18 15:08:09 -07001998 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1999 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
Eric Karlaeaf22b2017-05-18 15:08:09 -07002000 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2001 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
Eric Karlaeaf22b2017-05-18 15:08:09 -07002002 a = (1 == a) ? safeAlpha1 : safeAlpha0;
Brian Salomon028a9a52017-05-11 11:39:08 -04002003 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002004 GL_CALL(ClearColor(r, g, b, a));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002005 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002006}
2007
Robert Phillips95214472017-08-08 18:00:03 -04002008void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
2009 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002010 return;
2011 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002012 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002013 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002014
joshualitt77b13072014-10-27 14:51:01 -07002015 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002016 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002017
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002018 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002019 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002021 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002022}
2023
csmartdalton29df7602016-08-31 11:55:52 -07002024void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2025 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002026 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002027 SkASSERT(target);
egdaniel9cb63402016-06-23 08:37:05 -07002028 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002029
Mike Klein31550db2017-06-06 23:29:53 +00002030 if (this->glCaps().useDrawToClearStencilClip()) {
Robert Phillips7294b852017-08-01 13:51:44 +00002031 this->clearStencilClipAsDraw(clip, insideStencilMask, target);
Mike Klein31550db2017-06-06 23:29:53 +00002032 return;
2033 }
2034
egdaniel8dc7c3a2015-04-16 11:22:42 -07002035 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002036 // this should only be called internally when we know we have a
2037 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002038 SkASSERT(sb);
2039 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002040#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002041 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002042 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002043#else
2044 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002045 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002046 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002047 // changing the clip between stencil passes may or may not
2048 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002049 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002050#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002051 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002052 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002053 value = (1 << (stencilBitCount - 1));
2054 } else {
2055 value = 0;
2056 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002057 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
egdanield803f272015-03-18 13:01:52 -07002058 this->flushRenderTarget(glRT, &SkIRect::EmptyIRect());
bsalomon@google.coma3201942012-06-21 19:58:20 +00002059
Robert Phillips19e51dc2017-08-09 09:30:51 -04002060 this->flushScissor(clip.scissorState(), glRT->getViewport(), origin);
Robert Phillips7294b852017-08-01 13:51:44 +00002061 this->flushWindowRectangles(clip.windowRectsState(), glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002062
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002063 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002064 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002065 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002066 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002067}
2068
Brian Salomon71d9d842016-11-03 13:42:00 -04002069static bool read_pixels_pays_for_y_flip(GrSurfaceOrigin origin, const GrGLCaps& caps,
bsalomon39826022015-07-23 08:07:21 -07002070 int width, int height, GrPixelConfig config,
2071 size_t rowBytes) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002072 // If the surface is already TopLeft, we don't need to flip.
2073 if (kTopLeft_GrSurfaceOrigin == origin) {
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002074 return false;
2075 }
2076
bsalomon494aa592015-07-23 11:45:02 -07002077 // If the read is really small or smaller than the min texture size, don't force a draw.
bsalomon100b8f82015-10-28 08:37:44 -07002078 static const int kMinSize = 32;
2079 if (width < kMinSize || height < kMinSize) {
bsalomon494aa592015-07-23 11:45:02 -07002080 return false;
2081 }
2082
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002083 // if GL can do the flip then we'll never pay for it.
bsalomon39826022015-07-23 08:07:21 -07002084 if (caps.packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002085 return false;
2086 }
2087
2088 // If we have to do memcpy to handle non-trim rowBytes then we
bsalomon@google.com7107fa72011-11-10 14:54:14 +00002089 // get the flip for free. Otherwise it costs.
bsalomon39826022015-07-23 08:07:21 -07002090 // Note that we're assuming that 0 rowBytes has already been handled and that the width has been
2091 // clipped.
2092 return caps.packRowLengthSupport() || GrBytesPerPixel(config) * width == rowBytes;
2093}
2094
bsalomon1aa20292016-01-22 08:16:09 -08002095bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002096#ifdef SK_BUILD_FOR_MAC
2097 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2098 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2099 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2100 // we always use a temporary surface to test for read pixels support.
2101 // https://www.crbug.com/662802
2102 if (this->glContext().driver() == kChromium_GrGLDriver) {
2103 return this->readPixelsSupported(target->config(), readConfig);
2104 }
2105#endif
bsalomon1aa20292016-01-22 08:16:09 -08002106 auto bindRenderTarget = [this, target]() -> bool {
2107 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(target), &SkIRect::EmptyIRect());
2108 return true;
2109 };
bsalomon2c3db322016-11-08 13:26:24 -08002110 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002111 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2112 GR_GL_GetIntegerv(this->glInterface(), query, value);
2113 };
2114 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002115 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2116 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002117}
2118
2119bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002120 sk_sp<GrTexture> temp;
2121 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002122 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002123 desc.fConfig = rtConfig;
2124 desc.fWidth = desc.fHeight = 16;
bsalomon2c3db322016-11-08 13:26:24 -08002125 if (this->glCaps().isConfigRenderable(rtConfig, false)) {
2126 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04002127 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002128 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002129 if (!temp) {
2130 return false;
2131 }
2132 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
2133 this->flushRenderTarget(glrt, &SkIRect::EmptyIRect());
2134 return true;
2135 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002136 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002137 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002138 if (!temp) {
2139 return false;
2140 }
2141 GrGLIRect vp;
2142 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, &vp, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002143 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002144 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002145 }
bsalomon2c3db322016-11-08 13:26:24 -08002146 return false;
2147 };
2148 auto unbindRenderTarget = [this, &temp]() {
2149 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002150 };
2151 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2152 GR_GL_GetIntegerv(this->glInterface(), query, value);
2153 };
bsalomon2c3db322016-11-08 13:26:24 -08002154 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2155 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002156}
2157
2158bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2159 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2160 return this->readPixelsSupported(rt, readConfig);
2161 } else {
2162 GrPixelConfig config = surfaceForConfig->config();
2163 return this->readPixelsSupported(config, readConfig);
2164 }
2165}
2166
bsalomone9573312016-01-25 14:33:25 -08002167static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
2168 if (GrPixelConfigIsSRGB(a)) {
2169 return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
2170 } else if (GrPixelConfigIsSRGB(b)) {
2171 return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
2172 }
2173 return false;
2174}
2175
Robert Phillips7294b852017-08-01 13:51:44 +00002176bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
bsalomonf0674512015-07-28 13:26:15 -07002177 GrPixelConfig readConfig, DrawPreference* drawPreference,
2178 ReadPixelTempDrawInfo* tempDrawInfo) {
bsalomone9573312016-01-25 14:33:25 -08002179 GrPixelConfig srcConfig = srcSurface->config();
bsalomon1aa20292016-01-22 08:16:09 -08002180
bsalomone9573312016-01-25 14:33:25 -08002181 // These settings we will always want if a temp draw is performed.
bsalomon39826022015-07-23 08:07:21 -07002182 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2183 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
2184 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
2185 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
2186 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07002187 tempDrawInfo->fTempSurfaceFit = this->glCaps().partialFBOReadIsSlow() ? SkBackingFit::kExact
2188 : SkBackingFit::kApprox;
bsalomone9573312016-01-25 14:33:25 -08002189 // For now assume no swizzling, we may change that below.
2190 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
2191
2192 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
2193 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04002194 // Note that if we require a draw and return a non-renderable format for the temp surface the
bsalomone9573312016-01-25 14:33:25 -08002195 // base class will fail for us.
2196 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
2197 tempDrawInfo->fReadConfig = readConfig;
2198
2199 if (requires_srgb_conversion(srcConfig, readConfig)) {
2200 if (!this->readPixelsSupported(readConfig, readConfig)) {
2201 return false;
2202 }
2203 // Draw to do srgb to linear conversion or vice versa.
2204 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2205 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2206 tempDrawInfo->fReadConfig = readConfig;
2207 return true;
2208 }
2209
bsalomon1aa20292016-01-22 08:16:09 -08002210 if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
2211 this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
bsalomon39826022015-07-23 08:07:21 -07002212 tempDrawInfo->fTempSurfaceDesc.fConfig = kBGRA_8888_GrPixelConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002213 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2214 tempDrawInfo->fReadConfig = kBGRA_8888_GrPixelConfig;
bsalomonb411b3b2015-07-31 09:34:24 -07002215 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
ericrkb4ecabd2016-03-11 15:18:20 -08002216 } else if (this->glCaps().rgbaToBgraReadbackConversionsAreSlow() &&
bsalomon39826022015-07-23 08:07:21 -07002217 GrBytesPerPixel(readConfig) == 4 &&
bsalomon1aa20292016-01-22 08:16:09 -08002218 GrPixelConfigSwapRAndB(readConfig) == srcConfig &&
2219 this->readPixelsSupported(srcSurface, srcConfig)) {
bsalomon6c9cd552016-01-22 07:17:34 -08002220 // Mesa 3D takes a slow path on when reading back BGRA from an RGBA surface and vice-versa.
bsalomon39826022015-07-23 08:07:21 -07002221 // Better to do a draw with a R/B swap and then read as the original config.
2222 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
bsalomon6c9cd552016-01-22 07:17:34 -08002223 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2224 tempDrawInfo->fReadConfig = srcConfig;
bsalomonf0674512015-07-28 13:26:15 -07002225 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon1aa20292016-01-22 08:16:09 -08002226 } else if (!this->readPixelsSupported(srcSurface, readConfig)) {
2227 if (readConfig == kBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002228 this->glCaps().canConfigBeFBOColorAttachment(kRGBA_8888_GrPixelConfig) &&
bsalomon1aa20292016-01-22 08:16:09 -08002229 this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002230 // We're trying to read BGRA but it's not supported. If RGBA is renderable and
2231 // we can read it back, then do a swizzling draw to a RGBA and read it back (which
2232 // will effectively be BGRA).
bsalomon1aa20292016-01-22 08:16:09 -08002233 tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
2234 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2235 tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
2236 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
brianosmana6359362016-03-21 06:55:37 -07002237 } else if (readConfig == kSBGRA_8888_GrPixelConfig &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002238 this->glCaps().canConfigBeFBOColorAttachment(kSRGBA_8888_GrPixelConfig) &&
brianosmana6359362016-03-21 06:55:37 -07002239 this->readPixelsSupported(kSRGBA_8888_GrPixelConfig, kSRGBA_8888_GrPixelConfig)) {
2240 // We're trying to read sBGRA but it's not supported. If sRGBA is renderable and
2241 // we can read it back, then do a swizzling draw to a sRGBA and read it back (which
2242 // will effectively be sBGRA).
2243 tempDrawInfo->fTempSurfaceDesc.fConfig = kSRGBA_8888_GrPixelConfig;
2244 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
2245 tempDrawInfo->fReadConfig = kSRGBA_8888_GrPixelConfig;
2246 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
bsalomone9573312016-01-25 14:33:25 -08002247 } else if (readConfig == kAlpha_8_GrPixelConfig) {
2248 // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
2249 // it's unsupported, but 32bit RGBA reads are supported.
2250 // Don't attempt to do any srgb conversions since we only care about alpha.
2251 GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
2252 if (GrPixelConfigIsSRGB(srcSurface->config())) {
2253 cpuTempConfig = kSRGBA_8888_GrPixelConfig;
2254 }
2255 if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
2256 // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
2257 // first and then onReadPixels will read that to a 32bit temporary buffer.
Brian Salomon71d9d842016-11-03 13:42:00 -04002258 if (this->glCaps().canConfigBeFBOColorAttachment(cpuTempConfig)) {
bsalomone9573312016-01-25 14:33:25 -08002259 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2260 tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
2261 tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
2262 } else {
2263 return false;
2264 }
2265 } else {
2266 SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
2267 SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
2268 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002269 } else if (this->glCaps().canConfigBeFBOColorAttachment(readConfig) &&
bsalomone6d665e2016-03-10 07:22:25 -08002270 this->readPixelsSupported(readConfig, readConfig)) {
2271 // Do a draw to convert from the src config to the read config.
2272 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
2273 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
2274 tempDrawInfo->fReadConfig = readConfig;
bsalomon1aa20292016-01-22 08:16:09 -08002275 } else {
2276 return false;
2277 }
bsalomon39826022015-07-23 08:07:21 -07002278 }
2279
Brian Salomon71d9d842016-11-03 13:42:00 -04002280 if ((srcSurface->asRenderTarget() || this->glCaps().canConfigBeFBOColorAttachment(srcConfig)) &&
Robert Phillips7294b852017-08-01 13:51:44 +00002281 read_pixels_pays_for_y_flip(srcSurface->origin(), this->glCaps(), width, height, readConfig,
Brian Salomon71d9d842016-11-03 13:42:00 -04002282 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -07002283 ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
bsalomon39826022015-07-23 08:07:21 -07002284 }
2285
bsalomon39826022015-07-23 08:07:21 -07002286 return true;
bsalomon@google.comc4364992011-11-07 15:54:49 +00002287}
2288
Robert Phillips7294b852017-08-01 13:51:44 +00002289bool GrGLGpu::onReadPixels(GrSurface* surface,
bsalomon@google.comc6980972011-11-02 19:57:21 +00002290 int left, int top,
2291 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +00002292 GrPixelConfig config,
2293 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002294 size_t rowBytes) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002295 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002296
bsalomone9573312016-01-25 14:33:25 -08002297 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002298 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002299 return false;
2300 }
2301
bsalomon16921ec2015-07-30 15:34:56 -07002302 // OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
bsalomone9573312016-01-25 14:33:25 -08002303 if (requires_srgb_conversion(surface->config(), config)) {
2304 return false;
2305 }
2306
2307 // We have a special case fallback for reading eight bit alpha. We will read back all four 8
2308 // bit channels as RGBA and then extract A.
Brian Salomon71d9d842016-11-03 13:42:00 -04002309 if (!this->readPixelsSupported(surface, config)) {
bsalomone9573312016-01-25 14:33:25 -08002310 // Don't attempt to do any srgb conversions since we only care about alpha.
2311 GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon71d9d842016-11-03 13:42:00 -04002312 if (GrPixelConfigIsSRGB(surface->config())) {
bsalomone9573312016-01-25 14:33:25 -08002313 tempConfig = kSRGBA_8888_GrPixelConfig;
2314 }
2315 if (kAlpha_8_GrPixelConfig == config &&
Brian Salomon71d9d842016-11-03 13:42:00 -04002316 this->readPixelsSupported(surface, tempConfig)) {
Ben Wagner7ecc5962016-11-02 17:07:33 -04002317 std::unique_ptr<uint32_t[]> temp(new uint32_t[width * height * 4]);
Robert Phillips7294b852017-08-01 13:51:44 +00002318 if (this->onReadPixels(surface, left, top, width, height, tempConfig, temp.get(),
2319 width*4)) {
bsalomone9573312016-01-25 14:33:25 -08002320 uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
2321 for (int j = 0; j < height; ++j) {
2322 for (int i = 0; i < width; ++i) {
2323 dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
2324 }
2325 }
2326 return true;
2327 }
2328 }
bsalomon16921ec2015-07-30 15:34:56 -07002329 return false;
2330 }
2331
bsalomon76148af2016-01-12 11:13:47 -08002332 GrGLenum externalFormat;
2333 GrGLenum externalType;
Brian Salomon71d9d842016-11-03 13:42:00 -04002334 if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002335 &externalType)) {
2336 return false;
2337 }
Robert Phillips7294b852017-08-01 13:51:44 +00002338 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
bsalomon@google.comc4364992011-11-07 15:54:49 +00002339
Brian Salomon71d9d842016-11-03 13:42:00 -04002340 GrGLIRect glvp;
2341 if (renderTarget) {
2342 // resolve the render target if necessary
2343 switch (renderTarget->getResolveType()) {
2344 case GrGLRenderTarget::kCantResolve_ResolveType:
2345 return false;
2346 case GrGLRenderTarget::kAutoResolves_ResolveType:
2347 this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
2348 break;
2349 case GrGLRenderTarget::kCanResolve_ResolveType:
Robert Phillips7294b852017-08-01 13:51:44 +00002350 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002351 // we don't track the state of the READ FBO ID.
2352 fStats.incRenderTargetBinds();
2353 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
2354 break;
2355 default:
2356 SkFAIL("Unknown resolve type");
2357 }
2358 glvp = renderTarget->getViewport();
2359 } else {
2360 // Use a temporary FBO.
2361 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, &glvp, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002362 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002363 }
2364
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002365 // the read rect is viewport-relative
2366 GrGLIRect readRect;
Robert Phillips7294b852017-08-01 13:51:44 +00002367 readRect.setRelativeTo(glvp, left, top, width, height, surface->origin());
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002368
bsalomon9d02b262016-02-01 12:49:30 -08002369 size_t bytesPerPixel = GrBytesPerPixel(config);
2370 size_t tightRowBytes = bytesPerPixel * width;
egdaniel6d901da2015-07-30 12:02:15 -07002371
bsalomon@google.comc6980972011-11-02 19:57:21 +00002372 size_t readDstRowBytes = tightRowBytes;
2373 void* readDst = buffer;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002374
bsalomon@google.comc6980972011-11-02 19:57:21 +00002375 // determine if GL can read using the passed rowBytes or if we need
2376 // a scratch buffer.
joshualitt29f86792015-05-29 08:06:48 -07002377 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002378 if (rowBytes != tightRowBytes) {
bsalomon9d02b262016-02-01 12:49:30 -08002379 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
skia.committer@gmail.com4677acc2013-10-17 07:02:33 +00002380 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH,
bsalomon9d02b262016-02-01 12:49:30 -08002381 static_cast<GrGLint>(rowBytes / bytesPerPixel)));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002382 readDstRowBytes = rowBytes;
2383 } else {
2384 scratch.reset(tightRowBytes * height);
2385 readDst = scratch.get();
2386 }
2387 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002388 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002389 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 1));
2390 }
bsalomonf46a1242015-12-15 12:37:38 -08002391 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(config)));
2392
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002393 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom,
2394 readRect.fWidth, readRect.fHeight,
bsalomon76148af2016-01-12 11:13:47 -08002395 externalFormat, externalType, readDst));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002396 if (readDstRowBytes != tightRowBytes) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002397 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002398 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2399 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002400 if (flipY && this->glCaps().packFlipYSupport()) {
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002401 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, 0));
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002402 flipY = false;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00002403 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002404
2405 // now reverse the order of the rows, since GL's are bottom-to-top, but our
bsalomon@google.comc6980972011-11-02 19:57:21 +00002406 // API presents top-to-bottom. We must preserve the padding contents. Note
2407 // that the above readPixels did not overwrite the padding.
2408 if (readDst == buffer) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002409 SkASSERT(rowBytes == readDstRowBytes);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002410 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002411 scratch.reset(tightRowBytes);
2412 void* tmpRow = scratch.get();
2413 // flip y in-place by rows
2414 const int halfY = height >> 1;
2415 char* top = reinterpret_cast<char*>(buffer);
2416 char* bottom = top + (height - 1) * rowBytes;
2417 for (int y = 0; y < halfY; y++) {
2418 memcpy(tmpRow, top, tightRowBytes);
2419 memcpy(top, bottom, tightRowBytes);
2420 memcpy(bottom, tmpRow, tightRowBytes);
2421 top += rowBytes;
2422 bottom -= rowBytes;
2423 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002424 }
2425 } else {
bsalomon9d02b262016-02-01 12:49:30 -08002426 SkASSERT(readDst != buffer);
2427 SkASSERT(rowBytes != tightRowBytes);
bsalomon@google.comc6980972011-11-02 19:57:21 +00002428 // copy from readDst to buffer while flipping y
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002429 // const int halfY = height >> 1;
bsalomon@google.comc6980972011-11-02 19:57:21 +00002430 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002431 char* dst = reinterpret_cast<char*>(buffer);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002432 if (flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002433 dst += (height-1) * rowBytes;
2434 }
bsalomon@google.comc6980972011-11-02 19:57:21 +00002435 for (int y = 0; y < height; y++) {
2436 memcpy(dst, src, tightRowBytes);
2437 src += readDstRowBytes;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00002438 if (!flipY) {
bsalomon@google.comc4364992011-11-07 15:54:49 +00002439 dst += rowBytes;
2440 } else {
2441 dst -= rowBytes;
2442 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002443 }
2444 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002445 if (!renderTarget) {
2446 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2447 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002448 return true;
2449}
2450
egdaniel9cb63402016-06-23 08:37:05 -07002451GrGpuCommandBuffer* GrGLGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -04002452 GrRenderTarget* rt, GrSurfaceOrigin origin,
Robert Phillips95214472017-08-08 18:00:03 -04002453 const GrGpuCommandBuffer::LoadAndStoreInfo&,
2454 const GrGpuCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips19e51dc2017-08-09 09:30:51 -04002455 return new GrGLGpuCommandBuffer(this, rt, origin, stencilInfo);
egdaniel066df7c2016-06-08 14:02:27 -07002456}
2457
brianosman64d094d2016-03-25 06:01:59 -07002458void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, const SkIRect* bounds, bool disableSRGB) {
egdanield803f272015-03-18 13:01:52 -07002459 SkASSERT(target);
bsalomon6ba6fa12015-03-04 11:57:37 -08002460
Robert Phillips294870f2016-11-11 12:38:40 -05002461 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002462 if (fHWBoundRenderTargetUniqueID != rtID) {
bsalomon1e0bf7e2015-03-14 12:08:51 -07002463 fStats.incRenderTargetBinds();
egdanield803f272015-03-18 13:01:52 -07002464 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()));
2465#ifdef SK_DEBUG
2466 // don't do this check in Chromium -- this is causing
2467 // lots of repeated command buffer flushes when the compositor is
2468 // rendering with Ganesh, which is really slow; even too slow for
2469 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002470 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002471 GrGLenum status;
2472 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2473 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2474 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2475 }
bsalomon160f24c2015-03-17 15:55:42 -07002476 }
egdanield803f272015-03-18 13:01:52 -07002477#endif
2478 fHWBoundRenderTargetUniqueID = rtID;
bsalomon083617b2016-02-12 12:10:14 -08002479 this->flushViewport(target->getViewport());
brianosman64d094d2016-03-25 06:01:59 -07002480 }
2481
brianosman35b784d2016-05-05 11:52:53 -07002482 if (this->glCaps().srgbWriteControl()) {
brianosman33f6b3f2016-06-02 05:49:21 -07002483 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()) && !disableSRGB);
bsalomon5cd020f2015-03-17 12:46:56 -07002484 }
brianosman64d094d2016-03-25 06:01:59 -07002485
bsalomon083617b2016-02-12 12:10:14 -08002486 this->didWriteToSurface(target, bounds);
2487}
bsalomona9909122016-01-23 10:41:40 -08002488
brianosman33f6b3f2016-06-02 05:49:21 -07002489void GrGLGpu::flushFramebufferSRGB(bool enable) {
2490 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2491 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2492 fHWSRGBFramebuffer = kYes_TriState;
2493 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2494 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2495 fHWSRGBFramebuffer = kNo_TriState;
2496 }
2497}
2498
bsalomon083617b2016-02-12 12:10:14 -08002499void GrGLGpu::flushViewport(const GrGLIRect& viewport) {
2500 if (fHWViewport != viewport) {
2501 viewport.pushToGLViewport(this->glInterface());
2502 fHWViewport = viewport;
2503 }
2504}
2505
bsalomon@google.comd302f142011-03-03 13:54:13 +00002506#define SWAP_PER_DRAW 0
2507
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002508#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002509 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002510 #include <AGL/agl.h>
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002511 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002512 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002513 void SwapBuf() {
2514 DWORD procID = GetCurrentProcessId();
2515 HWND hwnd = GetTopWindow(GetDesktopWindow());
2516 while(hwnd) {
2517 DWORD wndProcID = 0;
2518 GetWindowThreadProcessId(hwnd, &wndProcID);
2519 if(wndProcID == procID) {
2520 SwapBuffers(GetDC(hwnd));
2521 }
2522 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2523 }
2524 }
2525 #endif
2526#endif
2527
egdaniel9cb63402016-06-23 08:37:05 -07002528void GrGLGpu::draw(const GrPipeline& pipeline,
2529 const GrPrimitiveProcessor& primProc,
bsalomon2eda5b32016-09-21 10:53:24 -07002530 const GrMesh meshes[],
Chris Dalton46983b72017-06-06 12:27:16 -06002531 const GrPipeline::DynamicState dynamicStates[],
egdaniel9cb63402016-06-23 08:37:05 -07002532 int meshCount) {
2533 this->handleDirtyContext();
2534
bsalomon2eda5b32016-09-21 10:53:24 -07002535 bool hasPoints = false;
2536 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002537 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002538 hasPoints = true;
2539 break;
2540 }
2541 }
2542 if (!this->flushGLState(pipeline, primProc, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002543 return;
2544 }
ethannicholas22793252016-01-30 09:59:10 -08002545
egdaniel0e1853c2016-03-17 11:35:45 -07002546 for (int i = 0; i < meshCount; ++i) {
2547 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002548 this->xferBarrier(pipeline.renderTarget(), barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002549 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002550
Chris Dalton46983b72017-06-06 12:27:16 -06002551 if (dynamicStates) {
2552 if (pipeline.getScissorState().enabled()) {
Robert Phillips2890fbf2017-07-26 15:48:41 -04002553 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(pipeline.renderTarget());
Chris Dalton46983b72017-06-06 12:27:16 -06002554 this->flushScissor(dynamicStates[i].fScissorRect,
Robert Phillips16d8ec62017-07-27 16:16:25 -04002555 glRT->getViewport(), pipeline.proxy()->origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002556 }
2557 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002558 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2559 GrIsPrimTypeLines(meshes[i].primitiveType()) &&
2560 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2561 GL_CALL(Enable(GR_GL_CULL_FACE));
2562 GL_CALL(Disable(GR_GL_CULL_FACE));
2563 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002564 meshes[i].sendToGpu(primProc, this);
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002565 fLastPrimitiveType = meshes[i].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002566 }
ethannicholas22793252016-01-30 09:59:10 -08002567
bsalomon@google.comd302f142011-03-03 13:54:13 +00002568#if SWAP_PER_DRAW
2569 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002570 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002571 aglSwapBuffers(aglGetCurrentContext());
2572 int set_a_break_pt_here = 9;
2573 aglSwapBuffers(aglGetCurrentContext());
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002574 #elif defined(SK_BUILD_FOR_WIN32)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002575 SwapBuf();
2576 int set_a_break_pt_here = 9;
2577 SwapBuf();
2578 #endif
2579#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002580}
2581
Chris Dalton3809bab2017-06-13 10:55:06 -06002582static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2583 switch (primitiveType) {
2584 case GrPrimitiveType::kTriangles:
2585 return GR_GL_TRIANGLES;
2586 case GrPrimitiveType::kTriangleStrip:
2587 return GR_GL_TRIANGLE_STRIP;
2588 case GrPrimitiveType::kTriangleFan:
2589 return GR_GL_TRIANGLE_FAN;
2590 case GrPrimitiveType::kPoints:
2591 return GR_GL_POINTS;
2592 case GrPrimitiveType::kLines:
2593 return GR_GL_LINES;
2594 case GrPrimitiveType::kLineStrip:
2595 return GR_GL_LINE_STRIP;
2596 case GrPrimitiveType::kLinesAdjacency:
2597 return GR_GL_LINES_ADJACENCY;
2598 }
2599 SkFAIL("invalid GrPrimitiveType");
2600 return GR_GL_TRIANGLES;
2601}
2602
Chris Dalton114a3c02017-05-26 15:17:19 -06002603void GrGLGpu::sendMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType primitiveType,
2604 const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002605 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002606 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton1d616352017-05-31 12:51:23 -06002607 this->setupGeometry(primProc, nullptr, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002608 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2609 } else {
Chris Dalton1d616352017-05-31 12:51:23 -06002610 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002611 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2612 }
2613 fStats.incNumDraws();
2614}
2615
2616void GrGLGpu::sendIndexedMeshToGpu(const GrPrimitiveProcessor& primProc,
2617 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2618 int indexCount, int baseIndex, uint16_t minIndexValue,
2619 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
2620 int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002621 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002622 GrGLvoid* const indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2623 sizeof(uint16_t) * baseIndex);
2624
Chris Dalton1d616352017-05-31 12:51:23 -06002625 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex, nullptr, 0);
Chris Dalton114a3c02017-05-26 15:17:19 -06002626
2627 if (this->glCaps().drawRangeElementsSupport()) {
2628 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2629 GR_GL_UNSIGNED_SHORT, indices));
2630 } else {
2631 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices));
2632 }
2633 fStats.incNumDraws();
2634}
2635
Chris Dalton1d616352017-05-31 12:51:23 -06002636void GrGLGpu::sendInstancedMeshToGpu(const GrPrimitiveProcessor& primProc, GrPrimitiveType
2637 primitiveType, const GrBuffer* vertexBuffer,
2638 int vertexCount, int baseVertex,
2639 const GrBuffer* instanceBuffer, int instanceCount,
2640 int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002641 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002642 this->setupGeometry(primProc, nullptr, vertexBuffer, 0, instanceBuffer, baseInstance);
2643 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
2644 fStats.incNumDraws();
2645}
2646
2647void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrPrimitiveProcessor& primProc,
2648 GrPrimitiveType primitiveType,
2649 const GrBuffer* indexBuffer, int indexCount,
2650 int baseIndex, const GrBuffer* vertexBuffer,
2651 int baseVertex, const GrBuffer* instanceBuffer,
2652 int instanceCount, int baseInstance) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002653 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1d616352017-05-31 12:51:23 -06002654 GrGLvoid* indices = reinterpret_cast<void*>(indexBuffer->baseOffset() +
2655 sizeof(uint16_t) * baseIndex);
2656 this->setupGeometry(primProc, indexBuffer, vertexBuffer, baseVertex,
2657 instanceBuffer, baseInstance);
2658 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, indices,
2659 instanceCount));
2660 fStats.incNumDraws();
2661}
2662
Robert Phillips7294b852017-08-01 13:51:44 +00002663void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002664 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002665 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002666 // Some extensions automatically resolves the texture when it is read.
2667 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002668 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2669 fStats.incRenderTargetBinds();
2670 fStats.incRenderTargetBinds();
2671 GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()));
2672 GL_CALL(BindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()));
2673 // make sure we go through flushRenderTarget() since we've modified
2674 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002675 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002676 const GrGLIRect& vp = rt->getViewport();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002677 const SkIRect dirtyRect = rt->getResolveRect();
reed@google.comac10a2d2010-12-22 21:39:39 +00002678
bsalomon@google.com347c3822013-05-01 20:10:01 +00002679 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002680 // Apple's extension uses the scissor as the blit bounds.
bsalomon3e791242014-12-17 13:43:13 -08002681 GrScissorState scissorState;
robertphillipse85a32d2015-02-10 08:16:55 -08002682 scissorState.set(dirtyRect);
Robert Phillips7294b852017-08-01 13:51:44 +00002683 this->flushScissor(scissorState, vp, rt->origin());
csmartdalton28341fa2016-08-17 10:00:21 -07002684 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002685 GL_CALL(ResolveMultisampleFramebuffer());
2686 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002687 int l, b, r, t;
2688 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2689 this->glCaps().blitFramebufferSupportFlags()) {
2690 l = 0;
2691 b = 0;
2692 r = target->width();
2693 t = target->height();
2694 } else {
2695 GrGLIRect rect;
Robert Phillips7294b852017-08-01 13:51:44 +00002696 rect.setRelativeTo(vp, dirtyRect, target->origin());
Brian Salomone5e7eb12016-10-14 16:18:33 -04002697 l = rect.fLeft;
2698 b = rect.fBottom;
2699 r = rect.fLeft + rect.fWidth;
2700 t = rect.fBottom + rect.fHeight;
2701 }
derekf8c8f71a2014-09-16 06:24:57 -07002702
2703 // BlitFrameBuffer respects the scissor, so disable it.
joshualitt77b13072014-10-27 14:51:01 -07002704 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002705 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002706 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002707 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002708 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002709 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002710 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002711 }
2712}
2713
bsalomon@google.com411dad02012-06-05 20:24:20 +00002714namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002715
bsalomon@google.com411dad02012-06-05 20:24:20 +00002716
2717GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002718 static const GrGLenum gTable[kGrStencilOpCount] = {
2719 GR_GL_KEEP, // kKeep
2720 GR_GL_ZERO, // kZero
2721 GR_GL_REPLACE, // kReplace
2722 GR_GL_INVERT, // kInvert
2723 GR_GL_INCR_WRAP, // kIncWrap
2724 GR_GL_DECR_WRAP, // kDecWrap
2725 GR_GL_INCR, // kIncClamp
2726 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002727 };
cdalton93a379b2016-05-11 13:58:08 -07002728 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2729 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2730 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2731 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2732 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2733 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2734 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2735 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2736 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2737 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002738}
2739
2740void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002741 const GrStencilSettings::Face& face,
2742 GrGLenum glFace) {
2743 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2744 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2745 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002746
cdalton93a379b2016-05-11 13:58:08 -07002747 GrGLint ref = face.fRef;
2748 GrGLint mask = face.fTestMask;
2749 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002750
2751 if (GR_GL_FRONT_AND_BACK == glFace) {
2752 // we call the combined func just in case separate stencil is not
2753 // supported.
2754 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2755 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002756 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002757 } else {
2758 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2759 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002760 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002761 }
2762}
2763}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002764
bsalomon3e791242014-12-17 13:43:13 -08002765void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002766 if (stencilSettings.isDisabled()) {
2767 this->disableStencil();
2768 } else if (fHWStencilSettings != stencilSettings) {
2769 if (kYes_TriState != fHWStencilTestEnabled) {
2770 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002771
csmartdaltonc7d85332016-10-26 10:13:46 -07002772 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002773 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002774 if (stencilSettings.isTwoSided()) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002775 set_gl_stencil(this->glInterface(),
2776 stencilSettings.front(),
2777 GR_GL_FRONT);
2778 set_gl_stencil(this->glInterface(),
2779 stencilSettings.back(),
2780 GR_GL_BACK);
2781 } else {
2782 set_gl_stencil(this->glInterface(),
2783 stencilSettings.front(),
2784 GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002785 }
joshualitta58fe352014-10-27 08:39:00 -07002786 fHWStencilSettings = stencilSettings;
reed@google.comac10a2d2010-12-22 21:39:39 +00002787 }
2788}
2789
csmartdaltonc7d85332016-10-26 10:13:46 -07002790void GrGLGpu::disableStencil() {
2791 if (kNo_TriState != fHWStencilTestEnabled) {
2792 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002793
csmartdaltonc7d85332016-10-26 10:13:46 -07002794 fHWStencilTestEnabled = kNo_TriState;
2795 fHWStencilSettings.invalidate();
2796 }
2797}
2798
cdaltonaf8bc7d2016-02-05 09:35:20 -08002799void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled) {
bsalomon083617b2016-02-12 12:10:14 -08002800 // rt is only optional if useHWAA is false.
2801 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002802 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002803
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002804 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002805 if (useHWAA) {
2806 if (kYes_TriState != fMSAAEnabled) {
2807 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2808 fMSAAEnabled = kYes_TriState;
2809 }
2810 } else {
2811 if (kNo_TriState != fMSAAEnabled) {
2812 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2813 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002814 }
2815 }
2816 }
cdaltonaf8bc7d2016-02-05 09:35:20 -08002817
2818 if (0 != this->caps()->maxRasterSamples()) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002819 if (useHWAA && GrFSAAType::kMixedSamples == rt->fsaaType() && !stencilEnabled) {
cdaltonaf8bc7d2016-02-05 09:35:20 -08002820 // Since stencil is disabled and we want more samples than are in the color buffer, we
2821 // need to tell the rasterizer explicitly how many to run.
2822 if (kYes_TriState != fHWRasterMultisampleEnabled) {
2823 GL_CALL(Enable(GR_GL_RASTER_MULTISAMPLE));
2824 fHWRasterMultisampleEnabled = kYes_TriState;
2825 }
2826 if (rt->numStencilSamples() != fHWNumRasterSamples) {
2827 SkASSERT(rt->numStencilSamples() <= this->caps()->maxRasterSamples());
2828 GL_CALL(RasterSamples(rt->numStencilSamples(), GR_GL_TRUE));
2829 fHWNumRasterSamples = rt->numStencilSamples();
2830 }
2831 } else {
2832 if (kNo_TriState != fHWRasterMultisampleEnabled) {
2833 GL_CALL(Disable(GR_GL_RASTER_MULTISAMPLE));
2834 fHWRasterMultisampleEnabled = kNo_TriState;
2835 }
2836 }
2837 } else {
Brian Salomon7c8460e2017-05-12 11:36:10 -04002838 SkASSERT(!useHWAA || GrFSAAType::kMixedSamples != rt->fsaaType() || stencilEnabled);
cdaltonaf8bc7d2016-02-05 09:35:20 -08002839 }
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002840}
2841
bsalomon7f9b2e42016-01-12 13:29:26 -08002842void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002843 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002844 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002845
cdalton8917d622015-05-06 13:40:21 -07002846 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002847 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2848 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
cdalton8917d622015-05-06 13:40:21 -07002849 bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2850 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
egdanielb414f252014-07-29 13:15:47 -07002851 if (blendOff) {
2852 if (kNo_TriState != fHWBlendState.fEnabled) {
2853 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002854
2855 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2856 // https://code.google.com/p/skia/issues/detail?id=3943
2857 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2858 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2859 SkASSERT(this->caps()->advancedBlendEquationSupport());
2860 // Set to any basic blending equation.
2861 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2862 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2863 fHWBlendState.fEquation = blend_equation;
2864 }
2865
egdanielb414f252014-07-29 13:15:47 -07002866 fHWBlendState.fEnabled = kNo_TriState;
2867 }
cdalton8917d622015-05-06 13:40:21 -07002868 return;
2869 }
2870
2871 if (kYes_TriState != fHWBlendState.fEnabled) {
2872 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002873
cdalton8917d622015-05-06 13:40:21 -07002874 fHWBlendState.fEnabled = kYes_TriState;
2875 }
2876
2877 if (fHWBlendState.fEquation != equation) {
2878 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2879 fHWBlendState.fEquation = equation;
2880 }
2881
2882 if (GrBlendEquationIsAdvanced(equation)) {
2883 SkASSERT(this->caps()->advancedBlendEquationSupport());
2884 // Advanced equations have no other blend state.
2885 return;
2886 }
2887
bsalomone63ffef2016-02-05 07:17:34 -08002888 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002889 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2890 gXfermodeCoeff2Blend[dstCoeff]));
2891 fHWBlendState.fSrcCoeff = srcCoeff;
2892 fHWBlendState.fDstCoeff = dstCoeff;
2893 }
2894
bsalomon7f9b2e42016-01-12 13:29:26 -08002895 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2896 GrColor blendConst = blendInfo.fBlendConstant;
2897 blendConst = swizzle.applyTo(blendConst);
2898 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2899 GrGLfloat c[4];
2900 GrColorToRGBAFloat(blendConst, c);
2901 GL_CALL(BlendColor(c[0], c[1], c[2], c[3]));
2902 fHWBlendState.fConstColor = blendConst;
2903 fHWBlendState.fConstColorValid = true;
2904 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002905 }
2906}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002907
commit-bot@chromium.org6364b5e2013-08-20 20:22:52 +00002908static inline GrGLenum tile_to_gl_wrap(SkShader::TileMode tm) {
bsalomon@google.comb8670992012-07-25 21:27:09 +00002909 static const GrGLenum gWrapModes[] = {
2910 GR_GL_CLAMP_TO_EDGE,
2911 GR_GL_REPEAT,
2912 GR_GL_MIRRORED_REPEAT
2913 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +00002914 GR_STATIC_ASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gWrapModes));
bsalomon@google.comb8670992012-07-25 21:27:09 +00002915 GR_STATIC_ASSERT(0 == SkShader::kClamp_TileMode);
2916 GR_STATIC_ASSERT(1 == SkShader::kRepeat_TileMode);
2917 GR_STATIC_ASSERT(2 == SkShader::kMirror_TileMode);
2918 return gWrapModes[tm];
2919}
2920
egdanielb7e7d572015-11-04 04:23:53 -08002921static GrGLenum get_component_enum_from_char(char component) {
2922 switch (component) {
2923 case 'r':
2924 return GR_GL_RED;
2925 case 'g':
2926 return GR_GL_GREEN;
2927 case 'b':
2928 return GR_GL_BLUE;
2929 case 'a':
2930 return GR_GL_ALPHA;
2931 default:
2932 SkFAIL("Unsupported component");
2933 return 0;
2934 }
2935}
2936
2937/** If texture swizzling is available using tex parameters then it is preferred over mangling
2938 the generated shader code. This potentially allows greater reuse of cached shaders. */
2939static void get_tex_param_swizzle(GrPixelConfig config,
bsalomoncdee0092016-01-08 13:20:12 -08002940 const GrGLCaps& caps,
egdanielb7e7d572015-11-04 04:23:53 -08002941 GrGLenum* glSwizzle) {
bsalomoncdee0092016-01-08 13:20:12 -08002942 const GrSwizzle& swizzle = caps.configSwizzle(config);
egdanielb7e7d572015-11-04 04:23:53 -08002943 for (int i = 0; i < 4; ++i) {
bsalomoncdee0092016-01-08 13:20:12 -08002944 glSwizzle[i] = get_component_enum_from_char(swizzle.c_str()[i]);
egdaniel574a4c12015-11-02 06:22:44 -08002945 }
2946}
2947
Brian Salomon514baff2016-11-17 15:17:07 -05002948void GrGLGpu::bindTexture(int unitIdx, const GrSamplerParams& params, bool allowSRGBInputs,
Robert Phillips7294b852017-08-01 13:51:44 +00002949 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002950 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002951
reed856e9d92015-09-30 12:21:45 -07002952#ifdef SK_DEBUG
2953 if (!this->caps()->npotTextureTileSupport()) {
2954 const bool tileX = SkShader::kClamp_TileMode != params.getTileModeX();
2955 const bool tileY = SkShader::kClamp_TileMode != params.getTileModeY();
2956 if (tileX || tileY) {
2957 const int w = texture->width();
2958 const int h = texture->height();
2959 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2960 }
2961 }
2962#endif
2963
bsalomon@google.comb8670992012-07-25 21:27:09 +00002964 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2965 // 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 +00002966 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002967 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002968 if (texRT) {
Robert Phillips7294b852017-08-01 13:51:44 +00002969 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002970 }
2971
Robert Phillips294870f2016-11-11 12:38:40 -05002972 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002973 GrGLenum target = texture->target();
bsalomon1c63bf62014-07-22 13:09:46 -07002974 if (fHWBoundTextureUniqueIDs[unitIdx] != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002975 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002976 GL_CALL(BindTexture(target, texture->textureID()));
bsalomon1c63bf62014-07-22 13:09:46 -07002977 fHWBoundTextureUniqueIDs[unitIdx] = textureID;
bsalomon@google.com4c883782012-06-04 19:05:11 +00002978 }
2979
bsalomon@google.com4c883782012-06-04 19:05:11 +00002980 ResetTimestamp timestamp;
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002981 const GrGLTexture::TexParams& oldTexParams = texture->getCachedTexParams(&timestamp);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002982 bool setAll = timestamp < this->getResetTimestamp();
2983 GrGLTexture::TexParams newTexParams;
2984
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002985 static GrGLenum glMinFilterModes[] = {
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00002986 GR_GL_NEAREST,
2987 GR_GL_LINEAR,
2988 GR_GL_LINEAR_MIPMAP_LINEAR
2989 };
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00002990 static GrGLenum glMagFilterModes[] = {
2991 GR_GL_NEAREST,
2992 GR_GL_LINEAR,
2993 GR_GL_LINEAR
2994 };
Brian Salomon514baff2016-11-17 15:17:07 -05002995 GrSamplerParams::FilterMode filterMode = params.filterMode();
bsalomonefd7d452014-10-23 14:17:46 -07002996
Brian Salomon514baff2016-11-17 15:17:07 -05002997 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04002998 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05002999 filterMode = GrSamplerParams::kBilerp_FilterMode;
bsalomonefd7d452014-10-23 14:17:46 -07003000 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003001 }
bsalomonefd7d452014-10-23 14:17:46 -07003002
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003003 newTexParams.fMinFilter = glMinFilterModes[filterMode];
3004 newTexParams.fMagFilter = glMagFilterModes[filterMode];
skia.committer@gmail.comaeefb2a2013-07-27 07:01:06 +00003005
brianosman20471892016-12-02 06:43:32 -08003006 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
brianosmanfe199872016-06-13 07:59:48 -07003007 newTexParams.fSRGBDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
brianosman33f6b3f2016-06-02 05:49:21 -07003008 if (setAll || newTexParams.fSRGBDecode != oldTexParams.fSRGBDecode) {
brianosmanadf4fa62016-04-26 16:48:06 -07003009 this->setTextureUnit(unitIdx);
brianosman33f6b3f2016-06-02 05:49:21 -07003010 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, newTexParams.fSRGBDecode));
cblume55f2d2d2016-02-26 13:20:48 -08003011 }
commit-bot@chromium.orgcffff792013-07-26 16:36:04 +00003012 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003013
brianosman33f6b3f2016-06-02 05:49:21 -07003014#ifdef SK_DEBUG
3015 // We were supposed to ensure MipMaps were up-to-date and built correctly before getting here.
Brian Salomon514baff2016-11-17 15:17:07 -05003016 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003017 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
3018 if (GrPixelConfigIsSRGB(texture->config())) {
Brian Osman7b8400d2016-11-08 17:08:54 -05003019 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3020 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3021 : SkDestinationSurfaceColorMode::kLegacy;
3022 SkASSERT(texture->texturePriv().mipColorMode() == colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003023 }
3024 }
3025#endif
3026
cblume55f2d2d2016-02-26 13:20:48 -08003027 newTexParams.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
3028
bsalomon@google.comb8670992012-07-25 21:27:09 +00003029 newTexParams.fWrapS = tile_to_gl_wrap(params.getTileModeX());
3030 newTexParams.fWrapT = tile_to_gl_wrap(params.getTileModeY());
bsalomoncdee0092016-01-08 13:20:12 -08003031 get_tex_param_swizzle(texture->config(), this->glCaps(), newTexParams.fSwizzleRGBA);
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003032 if (setAll || newTexParams.fMagFilter != oldTexParams.fMagFilter) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003033 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003034 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newTexParams.fMagFilter));
commit-bot@chromium.org149f4f52013-07-26 20:40:06 +00003035 }
3036 if (setAll || newTexParams.fMinFilter != oldTexParams.fMinFilter) {
3037 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003038 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newTexParams.fMinFilter));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003039 }
cblume55f2d2d2016-02-26 13:20:48 -08003040 if (setAll || newTexParams.fMaxMipMapLevel != oldTexParams.fMaxMipMapLevel) {
cblume09bd2c02016-03-01 14:08:28 -08003041 // These are not supported in ES2 contexts
3042 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
3043 if (newTexParams.fMaxMipMapLevel != 0) {
3044 this->setTextureUnit(unitIdx);
3045 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_LOD, 0));
3046 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, 0));
3047 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LOD,
3048 newTexParams.fMaxMipMapLevel));
3049 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
3050 newTexParams.fMaxMipMapLevel));
3051 }
cblume55f2d2d2016-02-26 13:20:48 -08003052 }
3053 }
bsalomon@google.com4c883782012-06-04 19:05:11 +00003054 if (setAll || newTexParams.fWrapS != oldTexParams.fWrapS) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003055 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003056 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newTexParams.fWrapS));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003057 }
3058 if (setAll || newTexParams.fWrapT != oldTexParams.fWrapT) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003059 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003060 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newTexParams.fWrapT));
bsalomon@google.com4c883782012-06-04 19:05:11 +00003061 }
bsalomoncdee0092016-01-08 13:20:12 -08003062 if (this->glCaps().textureSwizzleSupport() &&
bsalomon@google.com4c883782012-06-04 19:05:11 +00003063 (setAll || memcmp(newTexParams.fSwizzleRGBA,
3064 oldTexParams.fSwizzleRGBA,
3065 sizeof(newTexParams.fSwizzleRGBA)))) {
cdalton74b8d322016-04-11 14:47:28 -07003066 this->setTextureSwizzle(unitIdx, target, newTexParams.fSwizzleRGBA);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003067 }
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003068 texture->setCachedTexParams(newTexParams, this->getResetTimestamp());
bsalomon@google.com4c883782012-06-04 19:05:11 +00003069}
3070
csmartdalton1897cfd2016-06-03 08:50:54 -07003071void GrGLGpu::bindTexelBuffer(int unitIdx, GrPixelConfig texelConfig, GrGLBuffer* buffer) {
cdalton74b8d322016-04-11 14:47:28 -07003072 SkASSERT(this->glCaps().canUseConfigWithTexelBuffer(texelConfig));
3073 SkASSERT(unitIdx >= 0 && unitIdx < fHWBufferTextures.count());
cdalton74b8d322016-04-11 14:47:28 -07003074
3075 BufferTexture& buffTex = fHWBufferTextures[unitIdx];
3076
3077 if (!buffTex.fKnownBound) {
3078 if (!buffTex.fTextureID) {
3079 GL_CALL(GenTextures(1, &buffTex.fTextureID));
3080 if (!buffTex.fTextureID) {
3081 return;
3082 }
3083 }
3084
3085 this->setTextureUnit(unitIdx);
3086 GL_CALL(BindTexture(GR_GL_TEXTURE_BUFFER, buffTex.fTextureID));
3087
3088 buffTex.fKnownBound = true;
3089 }
3090
robertphillips8abb3702016-08-31 14:04:06 -07003091 if (buffer->uniqueID() != buffTex.fAttachedBufferUniqueID ||
csmartdalton1897cfd2016-06-03 08:50:54 -07003092 buffTex.fTexelConfig != texelConfig) {
cdalton74b8d322016-04-11 14:47:28 -07003093
3094 this->setTextureUnit(unitIdx);
csmartdalton1897cfd2016-06-03 08:50:54 -07003095 GL_CALL(TexBuffer(GR_GL_TEXTURE_BUFFER,
3096 this->glCaps().configSizedInternalFormat(texelConfig),
3097 buffer->bufferID()));
cdalton74b8d322016-04-11 14:47:28 -07003098
cdalton74b8d322016-04-11 14:47:28 -07003099 buffTex.fTexelConfig = texelConfig;
robertphillips8abb3702016-08-31 14:04:06 -07003100 buffTex.fAttachedBufferUniqueID = buffer->uniqueID();
cdalton74b8d322016-04-11 14:47:28 -07003101
3102 if (this->glCaps().textureSwizzleSupport() &&
3103 this->glCaps().configSwizzle(texelConfig) != buffTex.fSwizzle) {
3104 GrGLenum glSwizzle[4];
3105 get_tex_param_swizzle(texelConfig, this->glCaps(), glSwizzle);
3106 this->setTextureSwizzle(unitIdx, GR_GL_TEXTURE_BUFFER, glSwizzle);
3107 buffTex.fSwizzle = this->glCaps().configSwizzle(texelConfig);
3108 }
3109
3110 buffer->setHasAttachedToTexture();
3111 fHWMaxUsedBufferTextureUnit = SkTMax(unitIdx, fHWMaxUsedBufferTextureUnit);
3112 }
3113}
3114
Brian Salomonf9f45122016-11-29 11:59:17 -05003115void GrGLGpu::bindImageStorage(int unitIdx, GrIOType ioType, GrGLTexture *texture) {
3116 SkASSERT(texture);
3117 if (texture->uniqueID() != fHWBoundImageStorages[unitIdx].fTextureUniqueID ||
3118 ioType != fHWBoundImageStorages[unitIdx].fIOType) {
Joe Gregorio94e4ab12016-11-30 14:33:42 -05003119 GrGLenum access = GR_GL_READ_ONLY;
Brian Salomonf9f45122016-11-29 11:59:17 -05003120 switch (ioType) {
3121 case kRead_GrIOType:
3122 access = GR_GL_READ_ONLY;
3123 break;
3124 case kWrite_GrIOType:
3125 access = GR_GL_WRITE_ONLY;
3126 break;
3127 case kRW_GrIOType:
3128 access = GR_GL_READ_WRITE;
3129 break;
3130 }
3131 GrGLenum format = this->glCaps().getImageFormat(texture->config());
3132 GL_CALL(BindImageTexture(unitIdx, texture->textureID(), 0, GR_GL_FALSE, 0, access, format));
3133 }
3134}
3135
Brian Salomon514baff2016-11-17 15:17:07 -05003136void GrGLGpu::generateMipmaps(const GrSamplerParams& params, bool allowSRGBInputs,
Robert Phillips7294b852017-08-01 13:51:44 +00003137 GrGLTexture* texture) {
brianosman33f6b3f2016-06-02 05:49:21 -07003138 SkASSERT(texture);
3139
3140 // First, figure out if we need mips for this texture at all:
Brian Salomon514baff2016-11-17 15:17:07 -05003141 GrSamplerParams::FilterMode filterMode = params.filterMode();
brianosman33f6b3f2016-06-02 05:49:21 -07003142
Brian Salomon514baff2016-11-17 15:17:07 -05003143 if (GrSamplerParams::kMipMap_FilterMode == filterMode) {
Robert Phillips92de6312017-05-23 07:43:48 -04003144 if (!this->caps()->mipMapSupport()) {
Brian Salomon514baff2016-11-17 15:17:07 -05003145 filterMode = GrSamplerParams::kBilerp_FilterMode;
brianosman33f6b3f2016-06-02 05:49:21 -07003146 }
3147 }
3148
Brian Salomon514baff2016-11-17 15:17:07 -05003149 if (GrSamplerParams::kMipMap_FilterMode != filterMode) {
brianosman33f6b3f2016-06-02 05:49:21 -07003150 return;
3151 }
3152
3153 // If this is an sRGB texture and the mips were previously built the "other" way
3154 // (gamma-correct vs. not), then we need to rebuild them. We don't need to check for
3155 // srgbSupport - we'll *never* get an sRGB pixel config if we don't support it.
Brian Osman7b8400d2016-11-08 17:08:54 -05003156 SkDestinationSurfaceColorMode colorMode = allowSRGBInputs
3157 ? SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware
3158 : SkDestinationSurfaceColorMode::kLegacy;
brianosman33f6b3f2016-06-02 05:49:21 -07003159 if (GrPixelConfigIsSRGB(texture->config()) &&
Brian Osman7b8400d2016-11-08 17:08:54 -05003160 colorMode != texture->texturePriv().mipColorMode()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003161 texture->texturePriv().dirtyMipMaps(true);
3162 }
3163
3164 // If the mips aren't dirty, we're done:
3165 if (!texture->texturePriv().mipMapsAreDirty()) {
3166 return;
3167 }
3168
3169 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
3170 // from the rt it will still be the last bound texture, but it needs resolving.
3171 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
3172 if (texRT) {
Robert Phillips7294b852017-08-01 13:51:44 +00003173 this->onResolveRenderTarget(texRT);
brianosman33f6b3f2016-06-02 05:49:21 -07003174 }
3175
3176 GrGLenum target = texture->target();
3177 this->setScratchTextureUnit();
3178 GL_CALL(BindTexture(target, texture->textureID()));
3179
3180 // Configure sRGB decode, if necessary. This state is the only thing needed for the driver
3181 // call (glGenerateMipmap) to work correctly. Our manual method dirties other state, too.
brianosman20471892016-12-02 06:43:32 -08003182 if (this->glCaps().srgbDecodeDisableSupport() && GrPixelConfigIsSRGB(texture->config())) {
Brian Osmanb0403202016-12-09 10:55:23 -05003183 GrGLenum srgbDecode = allowSRGBInputs ? GR_GL_DECODE_EXT : GR_GL_SKIP_DECODE_EXT;
3184 // Command buffer's sRGB decode extension doesn't influence mipmap generation correctly.
3185 // If we set this to skip_decode, it appears to suppress sRGB -> Linear for each downsample,
3186 // but not the Linear -> sRGB when writing the next level. The result is that mip-chains
3187 // get progressively brighter as you go down. Forcing this to 'decode' gives predictable
3188 // (and only slightly incorrect) results. See crbug.com/655247 (~comment 28)
3189 if (!this->glCaps().srgbDecodeDisableAffectsMipmaps()) {
3190 srgbDecode = GR_GL_DECODE_EXT;
3191 }
3192 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SRGB_DECODE_EXT, srgbDecode));
brianosman33f6b3f2016-06-02 05:49:21 -07003193 }
3194
3195 // Either do manual mipmap generation or (if that fails), just rely on the driver:
Robert Phillips7294b852017-08-01 13:51:44 +00003196 if (!this->generateMipmap(texture, allowSRGBInputs)) {
brianosman33f6b3f2016-06-02 05:49:21 -07003197 GL_CALL(GenerateMipmap(target));
3198 }
3199
brianosmanfe199872016-06-13 07:59:48 -07003200 texture->texturePriv().dirtyMipMaps(false);
brianosman33f6b3f2016-06-02 05:49:21 -07003201 texture->texturePriv().setMaxMipMapLevel(SkMipMap::ComputeLevelCount(
3202 texture->width(), texture->height()));
Brian Osman7b8400d2016-11-08 17:08:54 -05003203 texture->texturePriv().setMipColorMode(colorMode);
brianosman33f6b3f2016-06-02 05:49:21 -07003204
3205 // We have potentially set lots of state on the texture. Easiest to dirty it all:
3206 texture->textureParamsModified();
3207}
3208
cdalton74b8d322016-04-11 14:47:28 -07003209void GrGLGpu::setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]) {
3210 this->setTextureUnit(unitIdx);
3211 if (this->glStandard() == kGLES_GrGLStandard) {
3212 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3213 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, swizzle[0]));
3214 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, swizzle[1]));
3215 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, swizzle[2]));
3216 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, swizzle[3]));
3217 } else {
3218 GR_STATIC_ASSERT(sizeof(swizzle[0]) == sizeof(GrGLint));
3219 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3220 reinterpret_cast<const GrGLint*>(swizzle)));
3221 }
3222}
3223
egdaniel080e6732014-12-22 07:35:52 -08003224void GrGLGpu::flushColorWrite(bool writeColor) {
3225 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003226 if (kNo_TriState != fHWWriteToColor) {
3227 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3228 GR_GL_FALSE, GR_GL_FALSE));
3229 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003230 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003231 } else {
3232 if (kYes_TriState != fHWWriteToColor) {
3233 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
3234 fHWWriteToColor = kYes_TriState;
3235 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003236 }
bsalomon3e791242014-12-17 13:43:13 -08003237}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003238
bsalomon861e1032014-12-16 07:33:49 -08003239void GrGLGpu::setTextureUnit(int unit) {
bsalomon1c63bf62014-07-22 13:09:46 -07003240 SkASSERT(unit >= 0 && unit < fHWBoundTextureUniqueIDs.count());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003241 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003242 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003243 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003244 }
3245}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003246
bsalomon861e1032014-12-16 07:33:49 -08003247void GrGLGpu::setScratchTextureUnit() {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003248 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
bsalomon1c63bf62014-07-22 13:09:46 -07003249 int lastUnitIdx = fHWBoundTextureUniqueIDs.count() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003250 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3251 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3252 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003253 }
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003254 // clear out the this field so that if a program does use this unit it will rebind the correct
3255 // texture.
Robert Phillips294870f2016-11-11 12:38:40 -05003256 fHWBoundTextureUniqueIDs[lastUnitIdx].makeInvalid();
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003257}
3258
Brian Salomone5e7eb12016-10-14 16:18:33 -04003259// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Robert Phillips7294b852017-08-01 13:51:44 +00003260static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3261 const GrSurface* src,
3262 const SkIRect& srcRect,
3263 const SkIPoint& dstPoint,
3264 const GrGLGpu* gpu) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003265 auto blitFramebufferFlags = gpu->glCaps().blitFramebufferSupportFlags();
Brian Salomon71d9d842016-11-03 13:42:00 -04003266 if (!gpu->glCaps().canConfigBeFBOColorAttachment(dst->config()) ||
3267 !gpu->glCaps().canConfigBeFBOColorAttachment(src->config())) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003268 return false;
3269 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003270 // Blits are not allowed between int color buffers and float/fixed color buffers. GrGpu should
3271 // have filtered such cases out.
3272 SkASSERT(GrPixelConfigIsSint(dst->config()) == GrPixelConfigIsSint(src->config()));
Brian Salomone5e7eb12016-10-14 16:18:33 -04003273 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3274 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(dst->asTexture());
3275 const GrRenderTarget* dstRT = dst->asRenderTarget();
3276 const GrRenderTarget* srcRT = src->asRenderTarget();
3277 if (dstTex && dstTex->target() != GR_GL_TEXTURE_2D) {
3278 return false;
3279 }
3280 if (srcTex && srcTex->target() != GR_GL_TEXTURE_2D) {
3281 return false;
3282 }
3283 if (GrGLCaps::kNoSupport_BlitFramebufferFlag & blitFramebufferFlags) {
3284 return false;
3285 }
3286 if (GrGLCaps::kNoScalingOrMirroring_BlitFramebufferFlag & blitFramebufferFlags) {
3287 // We would mirror to compensate for origin changes. Note that copySurface is
3288 // specified such that the src and dst rects are the same.
Robert Phillips7294b852017-08-01 13:51:44 +00003289 if (dst->origin() != src->origin()) {
Brian Salomone5e7eb12016-10-14 16:18:33 -04003290 return false;
3291 }
3292 }
3293 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003294 if (srcRT && srcRT->numColorSamples()) {
3295 if (dstRT && !dstRT->numColorSamples()) {
3296 return false;
3297 }
3298 if (SkRect::Make(srcRect) != srcRT->getBoundsRect()) {
3299 return false;
3300 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003301 }
3302 }
3303 if (GrGLCaps::kNoMSAADst_BlitFramebufferFlag & blitFramebufferFlags) {
3304 if (dstRT && dstRT->numColorSamples() > 0) {
3305 return false;
3306 }
3307 }
3308 if (GrGLCaps::kNoFormatConversion_BlitFramebufferFlag & blitFramebufferFlags) {
3309 if (dst->config() != src->config()) {
3310 return false;
3311 }
3312 } else if (GrGLCaps::kNoFormatConversionForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
3313 const GrRenderTarget* srcRT = src->asRenderTarget();
3314 if (srcRT && srcRT->numColorSamples() && dst->config() != src->config()) {
3315 return false;
3316 }
3317 }
3318 if (GrGLCaps::kRectsMustMatchForMSAASrc_BlitFramebufferFlag & blitFramebufferFlags) {
Eric Karl74480882017-04-03 14:49:05 -07003319 if (srcRT && srcRT->numColorSamples()) {
3320 if (dstPoint.fX != srcRect.fLeft || dstPoint.fY != srcRect.fTop) {
3321 return false;
3322 }
Robert Phillips7294b852017-08-01 13:51:44 +00003323 if (dst->origin() != src->origin()) {
Eric Karl74480882017-04-03 14:49:05 -07003324 return false;
3325 }
Brian Salomone5e7eb12016-10-14 16:18:33 -04003326 }
3327 }
3328 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003329}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003330
Robert Phillips7294b852017-08-01 13:51:44 +00003331static inline bool can_copy_texsubimage(const GrSurface* dst,
3332 const GrSurface* src,
bsalomon7ea33f52015-11-22 14:51:00 -08003333 const GrGLGpu* gpu) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003334 // Table 3.9 of the ES2 spec indicates the supported formats with CopyTexSubImage
3335 // and BGRA isn't in the spec. There doesn't appear to be any extension that adds it. Perhaps
3336 // many drivers would allow it to work, but ANGLE does not.
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +00003337 if (kGLES_GrGLStandard == gpu->glStandard() && gpu->glCaps().bgraIsInternalFormat() &&
bsalomon@google.comeb851172013-04-15 13:51:00 +00003338 (kBGRA_8888_GrPixelConfig == dst->config() || kBGRA_8888_GrPixelConfig == src->config())) {
3339 return false;
3340 }
3341 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
3342 // If dst is multisampled (and uses an extension where there is a separate MSAA renderbuffer)
3343 // then we don't want to copy to the texture but to the MSAA buffer.
egdanield803f272015-03-18 13:01:52 -07003344 if (dstRT && dstRT->renderFBOID() != dstRT->textureFBOID()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003345 return false;
3346 }
bsalomon@google.coma2719852013-04-17 14:25:27 +00003347 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
3348 // If the src is multisampled (and uses an extension where there is a separate MSAA
3349 // renderbuffer) then it is an invalid operation to call CopyTexSubImage
egdanield803f272015-03-18 13:01:52 -07003350 if (srcRT && srcRT->renderFBOID() != srcRT->textureFBOID()) {
bsalomon@google.coma2719852013-04-17 14:25:27 +00003351 return false;
3352 }
bsalomon7ea33f52015-11-22 14:51:00 -08003353
3354 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
3355 // CopyTex(Sub)Image writes to a texture and we have no way of dynamically wrapping a RT in a
3356 // texture.
3357 if (!dstTex) {
3358 return false;
3359 }
3360
3361 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003362
bsalomon7ea33f52015-11-22 14:51:00 -08003363 // Check that we could wrap the source in an FBO, that the dst is TEXTURE_2D, that no mirroring
3364 // is required.
Brian Salomon71d9d842016-11-03 13:42:00 -04003365 if (gpu->glCaps().canConfigBeFBOColorAttachment(src->config()) &&
Brian Salomon71d9d842016-11-03 13:42:00 -04003366 (!srcTex || srcTex->target() == GR_GL_TEXTURE_2D) && dstTex->target() == GR_GL_TEXTURE_2D &&
Robert Phillips7294b852017-08-01 13:51:44 +00003367 dst->origin() == src->origin()) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003368 return true;
3369 } else {
3370 return false;
3371 }
3372}
3373
3374// If a temporary FBO was created, its non-zero ID is returned. The viewport that the copy rect is
3375// relative to is output.
Brian Salomon71d9d842016-11-03 13:42:00 -04003376void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
3377 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003378 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003379 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003380 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003381 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3382 GrGLuint texID = texture->textureID();
3383 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003384 GrGLuint* tempFBOID;
3385 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003386
egdanield803f272015-03-18 13:01:52 -07003387 if (0 == *tempFBOID) {
3388 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003389 }
3390
egdanield803f272015-03-18 13:01:52 -07003391 fStats.incRenderTargetBinds();
3392 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, *tempFBOID));
3393 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003394 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003395 target,
robertphillips754f4e92014-09-18 13:52:08 -07003396 texID,
3397 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003398 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003399 viewport->fLeft = 0;
3400 viewport->fBottom = 0;
3401 viewport->fWidth = surface->width();
3402 viewport->fHeight = surface->height();
3403 } else {
egdanield803f272015-03-18 13:01:52 -07003404 fStats.incRenderTargetBinds();
3405 GR_GL_CALL(this->glInterface(), BindFramebuffer(fboTarget, rt->renderFBOID()));
bsalomon@google.comeb851172013-04-15 13:51:00 +00003406 *viewport = rt->getViewport();
3407 }
egdaniel0f5f9672015-02-03 11:10:51 -08003408}
3409
Brian Salomon71d9d842016-11-03 13:42:00 -04003410void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3411 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003412 if (!surface->asRenderTarget()) {
3413 SkASSERT(surface->asTexture());
3414 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3415 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3416 GR_GL_COLOR_ATTACHMENT0,
3417 textureTarget,
3418 0,
3419 0));
3420 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003421}
3422
Robert Phillips7294b852017-08-01 13:51:44 +00003423bool GrGLGpu::onCopySurface(GrSurface* dst,
3424 GrSurface* src,
3425 const SkIRect& srcRect,
3426 const SkIPoint& dstPoint) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003427 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
3428 // swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003429 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3430 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003431 return false;
3432 }
bsalomon083617b2016-02-12 12:10:14 -08003433 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3434 bool preferCopy = SkToBool(dst->asRenderTarget());
3435 if (preferCopy && src->asTexture()) {
Robert Phillips7294b852017-08-01 13:51:44 +00003436 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003437 return true;
3438 }
bsalomon5df6fee2015-05-18 06:26:15 -07003439 }
cblume61214052016-01-26 09:10:48 -08003440
Robert Phillips7294b852017-08-01 13:51:44 +00003441 if (can_copy_texsubimage(dst, src, this)) {
3442 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003443 return true;
3444 }
3445
Robert Phillips7294b852017-08-01 13:51:44 +00003446 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this)) {
3447 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003448 }
3449
bsalomon083617b2016-02-12 12:10:14 -08003450 if (!preferCopy && src->asTexture()) {
Robert Phillips7294b852017-08-01 13:51:44 +00003451 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
cdaltone2e71c22016-04-07 18:13:29 -07003452 return true;
3453 }
bsalomon083617b2016-02-12 12:10:14 -08003454 }
3455
bsalomon6df86402015-06-01 10:41:49 -07003456 return false;
3457}
3458
Brian Salomonbf7b6202016-11-11 16:08:03 -05003459bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Osman39c08ac2017-07-26 09:36:09 -04003460 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003461
Brian Salomonbf7b6202016-11-11 16:08:03 -05003462 int progIdx = TextureToCopyProgramIdx(srcTex);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003463 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomonbf7b6202016-11-11 16:08:03 -05003464 GrSLType samplerType = srcTex->texturePriv().samplerType();
cdaltone2e71c22016-04-07 18:13:29 -07003465
3466 if (!fCopyProgramArrayBuffer) {
3467 static const GrGLfloat vdata[] = {
3468 0, 0,
3469 0, 1,
3470 1, 0,
3471 1, 1
3472 };
3473 fCopyProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata), kVertex_GrBufferType,
3474 kStatic_GrAccessPattern, vdata));
3475 }
3476 if (!fCopyProgramArrayBuffer) {
3477 return false;
3478 }
3479
3480 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3481 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3482 if (!fCopyPrograms[progIdx].fProgram) {
3483 return false;
3484 }
3485
Brian Salomon1edc5b92016-11-29 13:43:46 -05003486 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003487 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3488 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3489 GrShaderVar::kUniform_TypeModifier);
3490 GrShaderVar uPosXform("u_posXform", kVec4f_GrSLType, GrShaderVar::kUniform_TypeModifier);
3491 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3492 GrShaderVar vTexCoord("v_texCoord", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier);
3493 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
cdaltone2e71c22016-04-07 18:13:29 -07003494
3495 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003496 if (shaderCaps->noperspectiveInterpolationSupport()) {
3497 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003498 vshaderTxt.appendf("#extension %s : require\n", extension);
3499 }
3500 vTexCoord.addModifier("noperspective");
3501 }
3502
Brian Salomon1edc5b92016-11-29 13:43:46 -05003503 aVertex.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003504 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003505 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003506 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003507 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003508 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003509 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003510 vshaderTxt.append(";");
3511
3512 vshaderTxt.append(
3513 "// Copy Program VS\n"
3514 "void main() {"
3515 " v_texCoord = a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw;"
3516 " gl_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003517 " gl_Position.zw = float2(0, 1);"
cdaltone2e71c22016-04-07 18:13:29 -07003518 "}"
3519 );
3520
3521 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003522 if (shaderCaps->noperspectiveInterpolationSupport()) {
3523 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
cdaltone2e71c22016-04-07 18:13:29 -07003524 fshaderTxt.appendf("#extension %s : require\n", extension);
3525 }
3526 }
Brian Salomonbf7b6202016-11-11 16:08:03 -05003527 if (samplerType == kTextureExternalSampler_GrSLType) {
cdaltone2e71c22016-04-07 18:13:29 -07003528 fshaderTxt.appendf("#extension %s : require\n",
Brian Salomon1edc5b92016-11-29 13:43:46 -05003529 shaderCaps->externalTextureExtensionString());
cdaltone2e71c22016-04-07 18:13:29 -07003530 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003531 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
cdaltone2e71c22016-04-07 18:13:29 -07003532 &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -05003533 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003534 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003535 fshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003536 uTexture.appendDecl(shaderCaps, &fshaderTxt);
cdaltone2e71c22016-04-07 18:13:29 -07003537 fshaderTxt.append(";");
cdaltone2e71c22016-04-07 18:13:29 -07003538 fshaderTxt.appendf(
3539 "// Copy Program FS\n"
3540 "void main() {"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003541 " sk_FragColor = texture(u_texture, v_texCoord);"
3542 "}"
cdaltone2e71c22016-04-07 18:13:29 -07003543 );
3544
3545 const char* str;
3546 GrGLint length;
3547
3548 str = vshaderTxt.c_str();
3549 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003550 SkSL::Program::Settings settings;
3551 settings.fCaps = shaderCaps;
3552 SkSL::Program::Inputs inputs;
cdaltone2e71c22016-04-07 18:13:29 -07003553 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3554 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003555 &fStats, settings, &inputs);
3556 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003557
3558 str = fshaderTxt.c_str();
3559 length = SkToInt(fshaderTxt.size());
3560 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3561 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003562 &fStats, settings, &inputs);
3563 SkASSERT(inputs.isEmpty());
cdaltone2e71c22016-04-07 18:13:29 -07003564
3565 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3566
3567 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3568 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3569 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3570 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3571 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3572 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3573
3574 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3575
3576 GL_CALL(DeleteShader(vshader));
3577 GL_CALL(DeleteShader(fshader));
3578
3579 return true;
bsalomon6df86402015-06-01 10:41:49 -07003580}
3581
brianosman33f6b3f2016-06-02 05:49:21 -07003582bool GrGLGpu::createMipmapProgram(int progIdx) {
3583 const bool oddWidth = SkToBool(progIdx & 0x2);
3584 const bool oddHeight = SkToBool(progIdx & 0x1);
3585 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3586
Brian Salomon1edc5b92016-11-29 13:43:46 -05003587 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003588
3589 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3590 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3591 if (!fMipmapPrograms[progIdx].fProgram) {
3592 return false;
3593 }
3594
Brian Salomon1edc5b92016-11-29 13:43:46 -05003595 const char* version = shaderCaps->versionDeclString();
Brian Salomon99938a82016-11-21 13:41:08 -05003596 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
3597 GrShaderVar uTexCoordXform("u_texCoordXform", kVec4f_GrSLType,
3598 GrShaderVar::kUniform_TypeModifier);
3599 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3600 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003601 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003602 GrShaderVar vTexCoords[] = {
3603 GrShaderVar("v_texCoord0", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3604 GrShaderVar("v_texCoord1", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3605 GrShaderVar("v_texCoord2", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
3606 GrShaderVar("v_texCoord3", kVec2f_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003607 };
Brian Salomon99938a82016-11-21 13:41:08 -05003608 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003609
3610 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003611 if (shaderCaps->noperspectiveInterpolationSupport()) {
3612 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003613 vshaderTxt.appendf("#extension %s : require\n", extension);
3614 }
3615 vTexCoords[0].addModifier("noperspective");
3616 vTexCoords[1].addModifier("noperspective");
3617 vTexCoords[2].addModifier("noperspective");
3618 vTexCoords[3].addModifier("noperspective");
3619 }
3620
Brian Salomon1edc5b92016-11-29 13:43:46 -05003621 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003622 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003623 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003624 vshaderTxt.append(";");
3625 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003626 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003627 vshaderTxt.append(";");
3628 }
3629
3630 vshaderTxt.append(
3631 "// Mipmap Program VS\n"
3632 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003633 " gl_Position.xy = a_vertex * float2(2, 2) - float2(1, 1);"
3634 " gl_Position.zw = float2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003635 );
3636
3637 // Insert texture coordinate computation:
3638 if (oddWidth && oddHeight) {
3639 vshaderTxt.append(
3640 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003641 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + float2(u_texCoordXform.x, 0);"
3642 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + float2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003643 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3644 );
3645 } else if (oddWidth) {
3646 vshaderTxt.append(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003647 " v_texCoord0 = a_vertex.xy * float2(u_texCoordXform.y, 1);"
3648 " v_texCoord1 = a_vertex.xy * float2(u_texCoordXform.y, 1) + float2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003649 );
3650 } else if (oddHeight) {
3651 vshaderTxt.append(
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003652 " v_texCoord0 = a_vertex.xy * float2(1, u_texCoordXform.w);"
3653 " v_texCoord1 = a_vertex.xy * float2(1, u_texCoordXform.w) + float2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003654 );
3655 } else {
3656 vshaderTxt.append(
3657 " v_texCoord0 = a_vertex.xy;"
3658 );
3659 }
3660
3661 vshaderTxt.append("}");
3662
3663 SkString fshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003664 if (shaderCaps->noperspectiveInterpolationSupport()) {
3665 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003666 fshaderTxt.appendf("#extension %s : require\n", extension);
3667 }
3668 }
Brian Osman33aa2c72017-04-05 09:26:15 -04003669 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps,
brianosman33f6b3f2016-06-02 05:49:21 -07003670 &fshaderTxt);
3671 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003672 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003673 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003674 fshaderTxt.append(";");
3675 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003676 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003677 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003678 fshaderTxt.append(
3679 "// Mipmap Program FS\n"
3680 "void main() {"
3681 );
3682
3683 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003684 fshaderTxt.append(
3685 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3686 " texture(u_texture, v_texCoord1) + "
3687 " texture(u_texture, v_texCoord2) + "
3688 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003689 );
3690 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003691 fshaderTxt.append(
3692 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3693 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003694 );
3695 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003696 fshaderTxt.append(
3697 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003698 );
3699 }
3700
3701 fshaderTxt.append("}");
3702
3703 const char* str;
3704 GrGLint length;
3705
3706 str = vshaderTxt.c_str();
3707 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003708 SkSL::Program::Settings settings;
3709 settings.fCaps = shaderCaps;
3710 SkSL::Program::Inputs inputs;
brianosman33f6b3f2016-06-02 05:49:21 -07003711 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3712 GR_GL_VERTEX_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003713 &fStats, settings, &inputs);
3714 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003715
3716 str = fshaderTxt.c_str();
3717 length = SkToInt(fshaderTxt.size());
3718 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
3719 GR_GL_FRAGMENT_SHADER, &str, &length, 1,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003720 &fStats, settings, &inputs);
3721 SkASSERT(inputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003722
3723 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3724
3725 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3726 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3727 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3728 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3729
3730 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3731
3732 GL_CALL(DeleteShader(vshader));
3733 GL_CALL(DeleteShader(fshader));
3734
3735 return true;
3736}
3737
Mike Klein31550db2017-06-06 23:29:53 +00003738bool GrGLGpu::createStencilClipClearProgram() {
Brian Osman39c08ac2017-07-26 09:36:09 -04003739 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -07003740
Mike Klein31550db2017-06-06 23:29:53 +00003741 if (!fStencilClipClearArrayBuffer) {
3742 static const GrGLfloat vdata[] = {-1, -1, 1, -1, -1, 1, 1, 1};
3743 fStencilClipClearArrayBuffer.reset(GrGLBuffer::Create(
3744 this, sizeof(vdata), kVertex_GrBufferType, kStatic_GrAccessPattern, vdata));
3745 if (!fStencilClipClearArrayBuffer) {
cdaltone2e71c22016-04-07 18:13:29 -07003746 return false;
3747 }
3748 }
3749
Mike Klein31550db2017-06-06 23:29:53 +00003750 SkASSERT(!fStencilClipClearProgram);
3751 GL_CALL_RET(fStencilClipClearProgram, CreateProgram());
3752 if (!fStencilClipClearProgram) {
cdaltone2e71c22016-04-07 18:13:29 -07003753 return false;
3754 }
3755
Brian Salomon99938a82016-11-21 13:41:08 -05003756 GrShaderVar aVertex("a_vertex", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003757 const char* version = this->caps()->shaderCaps()->versionDeclString();
bsalomon6dea83f2015-12-03 12:58:06 -08003758
bsalomon6dea83f2015-12-03 12:58:06 -08003759 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003760 aVertex.appendDecl(this->caps()->shaderCaps(), &vshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003761 vshaderTxt.append(";");
bsalomon6dea83f2015-12-03 12:58:06 -08003762 vshaderTxt.append(
Mike Klein31550db2017-06-06 23:29:53 +00003763 "// Stencil Clip Clear Program VS\n"
3764 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003765 " gl_Position = float4(a_vertex.x, a_vertex.y, 0, 1);"
Mike Klein31550db2017-06-06 23:29:53 +00003766 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003767
3768 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -04003769 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision,
Brian Salomon1edc5b92016-11-29 13:43:46 -05003770 *this->caps()->shaderCaps(),
bsalomon6dea83f2015-12-03 12:58:06 -08003771 &fshaderTxt);
bsalomon6dea83f2015-12-03 12:58:06 -08003772 fshaderTxt.appendf(
Mike Klein31550db2017-06-06 23:29:53 +00003773 "// Stencil Clip Clear Program FS\n"
3774 "void main() {"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04003775 " sk_FragColor = float4(0);"
Mike Klein31550db2017-06-06 23:29:53 +00003776 "}");
bsalomon6dea83f2015-12-03 12:58:06 -08003777
bsalomon6dea83f2015-12-03 12:58:06 -08003778 const char* str;
3779 GrGLint length;
3780
3781 str = vshaderTxt.c_str();
3782 length = SkToInt(vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003783 SkSL::Program::Settings settings;
3784 settings.fCaps = this->caps()->shaderCaps();
3785 SkSL::Program::Inputs inputs;
Mike Klein31550db2017-06-06 23:29:53 +00003786 GrGLuint vshader =
3787 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_VERTEX_SHADER,
3788 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003789 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003790
3791 str = fshaderTxt.c_str();
3792 length = SkToInt(fshaderTxt.size());
Mike Klein31550db2017-06-06 23:29:53 +00003793 GrGLuint fshader =
3794 GrGLCompileAndAttachShader(*fGLContext, fStencilClipClearProgram, GR_GL_FRAGMENT_SHADER,
3795 &str, &length, 1, &fStats, settings, &inputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003796 SkASSERT(inputs.isEmpty());
bsalomon6dea83f2015-12-03 12:58:06 -08003797
Mike Klein31550db2017-06-06 23:29:53 +00003798 GL_CALL(LinkProgram(fStencilClipClearProgram));
bsalomon6dea83f2015-12-03 12:58:06 -08003799
Mike Klein31550db2017-06-06 23:29:53 +00003800 GL_CALL(BindAttribLocation(fStencilClipClearProgram, 0, "a_vertex"));
bsalomon6dea83f2015-12-03 12:58:06 -08003801
3802 GL_CALL(DeleteShader(vshader));
3803 GL_CALL(DeleteShader(fshader));
cdaltone2e71c22016-04-07 18:13:29 -07003804
3805 return true;
bsalomon6dea83f2015-12-03 12:58:06 -08003806}
3807
Mike Klein31550db2017-06-06 23:29:53 +00003808void GrGLGpu::clearStencilClipAsDraw(const GrFixedClip& clip, bool insideStencilMask,
Robert Phillips7294b852017-08-01 13:51:44 +00003809 GrRenderTarget* rt) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003810 // TODO: This should swizzle the output to match dst's config, though it is a debugging
3811 // visualization.
3812
bsalomon6dea83f2015-12-03 12:58:06 -08003813 this->handleDirtyContext();
Mike Klein31550db2017-06-06 23:29:53 +00003814 if (!fStencilClipClearProgram) {
3815 if (!this->createStencilClipClearProgram()) {
3816 SkDebugf("Failed to create stencil clip clear program.\n");
cdaltone2e71c22016-04-07 18:13:29 -07003817 return;
3818 }
bsalomon6dea83f2015-12-03 12:58:06 -08003819 }
3820
Mike Klein8b06ed72017-06-06 21:47:32 +00003821 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(rt->asRenderTarget());
Mike Klein31550db2017-06-06 23:29:53 +00003822 this->flushRenderTarget(glRT, nullptr);
Mike Klein8b06ed72017-06-06 21:47:32 +00003823
Mike Klein31550db2017-06-06 23:29:53 +00003824 GL_CALL(UseProgram(fStencilClipClearProgram));
3825 fHWProgramID = fStencilClipClearProgram;
bsalomon6dea83f2015-12-03 12:58:06 -08003826
cdaltone2e71c22016-04-07 18:13:29 -07003827 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003828
cdaltone2e71c22016-04-07 18:13:29 -07003829 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003830 attribs->enableVertexArrays(this, 1);
Mike Klein31550db2017-06-06 23:29:53 +00003831 attribs->set(this, 0, fStencilClipClearArrayBuffer.get(), kVec2f_GrVertexAttribType,
Hal Canary144caf52016-11-07 17:57:18 -05003832 2 * sizeof(GrGLfloat), 0);
bsalomon6dea83f2015-12-03 12:58:06 -08003833
bsalomon6dea83f2015-12-03 12:58:06 -08003834 GrXferProcessor::BlendInfo blendInfo;
3835 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003836 this->flushBlend(blendInfo, GrSwizzle::RGBA());
Mike Klein31550db2017-06-06 23:29:53 +00003837 this->flushColorWrite(false);
cdaltonaf8bc7d2016-02-05 09:35:20 -08003838 this->flushHWAAState(glRT, false, false);
Robert Phillips7294b852017-08-01 13:51:44 +00003839 this->flushScissor(clip.scissorState(), glRT->getViewport(), glRT->origin());
3840 this->flushWindowRectangles(clip.windowRectsState(), glRT);
Mike Klein31550db2017-06-06 23:29:53 +00003841 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
3842 // This should only be called internally when we know we have a stencil buffer.
3843 SkASSERT(sb);
3844 GrStencilSettings settings = GrStencilSettings(
3845 *GrStencilSettings::SetClipBitSettings(insideStencilMask), false, sb->bits());
3846 this->flushStencil(settings);
3847 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
bsalomon6dea83f2015-12-03 12:58:06 -08003848}
3849
3850
Robert Phillips7294b852017-08-01 13:51:44 +00003851bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst,
3852 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003853 const SkIRect& srcRect,
3854 const SkIPoint& dstPoint) {
cdaltone2e71c22016-04-07 18:13:29 -07003855 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
Brian Salomonbf7b6202016-11-11 16:08:03 -05003856 int progIdx = TextureToCopyProgramIdx(srcTex);
cdaltone2e71c22016-04-07 18:13:29 -07003857
3858 if (!fCopyPrograms[progIdx].fProgram) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05003859 if (!this->createCopyProgram(srcTex)) {
cdaltone2e71c22016-04-07 18:13:29 -07003860 SkDebugf("Failed to create copy program.\n");
3861 return false;
3862 }
3863 }
3864
bsalomon6df86402015-06-01 10:41:49 -07003865 int w = srcRect.width();
3866 int h = srcRect.height();
3867
Brian Salomon514baff2016-11-17 15:17:07 -05003868 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
Robert Phillips7294b852017-08-01 13:51:44 +00003869 this->bindTexture(0, params, true, srcTex);
bsalomon6df86402015-06-01 10:41:49 -07003870
bsalomon083617b2016-02-12 12:10:14 -08003871 GrGLIRect dstVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003872 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003873 this->flushViewport(dstVP);
Robert Phillips294870f2016-11-11 12:38:40 -05003874 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon083617b2016-02-12 12:10:14 -08003875
bsalomon6df86402015-06-01 10:41:49 -07003876 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
bsalomon6df86402015-06-01 10:41:49 -07003877
bsalomon7ea33f52015-11-22 14:51:00 -08003878 GL_CALL(UseProgram(fCopyPrograms[progIdx].fProgram));
3879 fHWProgramID = fCopyPrograms[progIdx].fProgram;
bsalomon6df86402015-06-01 10:41:49 -07003880
cdaltone2e71c22016-04-07 18:13:29 -07003881 fHWVertexArrayState.setVertexArrayID(this, 0);
bsalomon6df86402015-06-01 10:41:49 -07003882
cdaltone2e71c22016-04-07 18:13:29 -07003883 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003884 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05003885 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
3886 2 * sizeof(GrGLfloat), 0);
bsalomon6df86402015-06-01 10:41:49 -07003887
3888 // dst rect edges in NDC (-1 to 1)
3889 int dw = dst->width();
3890 int dh = dst->height();
3891 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3892 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3893 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3894 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillips7294b852017-08-01 13:51:44 +00003895 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07003896 dy0 = -dy0;
3897 dy1 = -dy1;
3898 }
3899
bsalomone5286e02016-01-14 09:24:09 -08003900 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3901 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3902 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3903 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
Ethan Nicholas5338f992017-04-19 15:54:07 -04003904 int sw = src->width();
bsalomon6df86402015-06-01 10:41:49 -07003905 int sh = src->height();
Robert Phillips7294b852017-08-01 13:51:44 +00003906 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
bsalomone5286e02016-01-14 09:24:09 -08003907 sy0 = sh - sy0;
3908 sy1 = sh - sy1;
3909 }
Ethan Nicholas5338f992017-04-19 15:54:07 -04003910 // src rect edges in normalized texture space (0 to 1)
3911 sx0 /= sw;
3912 sx1 /= sw;
3913 sy0 /= sh;
3914 sy1 /= sh;
bsalomon6df86402015-06-01 10:41:49 -07003915
bsalomon7ea33f52015-11-22 14:51:00 -08003916 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3917 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3918 sx1 - sx0, sy1 - sy0, sx0, sy0));
3919 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
bsalomon6df86402015-06-01 10:41:49 -07003920
3921 GrXferProcessor::BlendInfo blendInfo;
3922 blendInfo.reset();
bsalomon7f9b2e42016-01-12 13:29:26 -08003923 this->flushBlend(blendInfo, GrSwizzle::RGBA());
bsalomon6df86402015-06-01 10:41:49 -07003924 this->flushColorWrite(true);
bsalomon083617b2016-02-12 12:10:14 -08003925 this->flushHWAAState(nullptr, false, false);
bsalomon6df86402015-06-01 10:41:49 -07003926 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003927 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003928 this->disableStencil();
Brian Osmanf02fa6f2017-07-11 11:17:47 -04003929 if (this->glCaps().srgbWriteControl()) {
3930 this->flushFramebufferSRGB(true);
3931 }
bsalomon6df86402015-06-01 10:41:49 -07003932
3933 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomon71d9d842016-11-03 13:42:00 -04003934 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
bsalomon083617b2016-02-12 12:10:14 -08003935 this->didWriteToSurface(dst, &dstRect);
3936
cdaltone2e71c22016-04-07 18:13:29 -07003937 return true;
bsalomon6df86402015-06-01 10:41:49 -07003938}
3939
Robert Phillips7294b852017-08-01 13:51:44 +00003940void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst,
3941 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003942 const SkIRect& srcRect,
3943 const SkIPoint& dstPoint) {
Robert Phillips7294b852017-08-01 13:51:44 +00003944 SkASSERT(can_copy_texsubimage(dst, src, this));
bsalomon6df86402015-06-01 10:41:49 -07003945 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003946 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003947 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003948 SkASSERT(dstTex);
3949 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003950 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003951 GrGLIRect srcGLRect;
Robert Phillips7294b852017-08-01 13:51:44 +00003952 srcGLRect.setRelativeTo(srcVP, srcRect, src->origin());
bsalomon6df86402015-06-01 10:41:49 -07003953
3954 this->setScratchTextureUnit();
bsalomon10528f12015-10-14 12:54:52 -07003955 GL_CALL(BindTexture(dstTex->target(), dstTex->textureID()));
bsalomon6df86402015-06-01 10:41:49 -07003956 GrGLint dstY;
Robert Phillips7294b852017-08-01 13:51:44 +00003957 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07003958 dstY = dst->height() - (dstPoint.fY + srcGLRect.fHeight);
3959 } else {
3960 dstY = dstPoint.fY;
3961 }
bsalomon10528f12015-10-14 12:54:52 -07003962 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
bsalomon083617b2016-02-12 12:10:14 -08003963 dstPoint.fX, dstY,
3964 srcGLRect.fLeft, srcGLRect.fBottom,
3965 srcGLRect.fWidth, srcGLRect.fHeight));
Brian Salomon71d9d842016-11-03 13:42:00 -04003966 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003967 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3968 srcRect.width(), srcRect.height());
3969 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003970}
3971
Robert Phillips7294b852017-08-01 13:51:44 +00003972bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst,
3973 GrSurface* src,
bsalomon6df86402015-06-01 10:41:49 -07003974 const SkIRect& srcRect,
3975 const SkIPoint& dstPoint) {
Robert Phillips7294b852017-08-01 13:51:44 +00003976 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this));
bsalomon6df86402015-06-01 10:41:49 -07003977 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3978 srcRect.width(), srcRect.height());
3979 if (dst == src) {
3980 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3981 return false;
mtklein404b3b22015-05-18 09:29:10 -07003982 }
bsalomon5df6fee2015-05-18 06:26:15 -07003983 }
bsalomon6df86402015-06-01 10:41:49 -07003984
bsalomon6df86402015-06-01 10:41:49 -07003985 GrGLIRect dstVP;
3986 GrGLIRect srcVP;
Brian Salomon71d9d842016-11-03 13:42:00 -04003987 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, &dstVP, kDst_TempFBOTarget);
3988 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, &srcVP, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003989 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003990 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003991 GrGLIRect srcGLRect;
3992 GrGLIRect dstGLRect;
Robert Phillips7294b852017-08-01 13:51:44 +00003993 srcGLRect.setRelativeTo(srcVP, srcRect, src->origin());
3994 dstGLRect.setRelativeTo(dstVP, dstRect, dst->origin());
bsalomon6df86402015-06-01 10:41:49 -07003995
3996 // BlitFrameBuffer respects the scissor, so disable it.
3997 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003998 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003999
4000 GrGLint srcY0;
4001 GrGLint srcY1;
4002 // Does the blit need to y-mirror or not?
Robert Phillips7294b852017-08-01 13:51:44 +00004003 if (src->origin() == dst->origin()) {
bsalomon6df86402015-06-01 10:41:49 -07004004 srcY0 = srcGLRect.fBottom;
4005 srcY1 = srcGLRect.fBottom + srcGLRect.fHeight;
4006 } else {
4007 srcY0 = srcGLRect.fBottom + srcGLRect.fHeight;
4008 srcY1 = srcGLRect.fBottom;
4009 }
4010 GL_CALL(BlitFramebuffer(srcGLRect.fLeft,
4011 srcY0,
4012 srcGLRect.fLeft + srcGLRect.fWidth,
4013 srcY1,
4014 dstGLRect.fLeft,
4015 dstGLRect.fBottom,
4016 dstGLRect.fLeft + dstGLRect.fWidth,
4017 dstGLRect.fBottom + dstGLRect.fHeight,
4018 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04004019 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
4020 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08004021 this->didWriteToSurface(dst, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07004022 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00004023}
4024
brianosman33f6b3f2016-06-02 05:49:21 -07004025// Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
4026// Uses draw calls to do a series of downsample operations to successive mips.
4027// If this returns false, then the calling code falls back to using glGenerateMipmap.
Robert Phillips7294b852017-08-01 13:51:44 +00004028bool GrGLGpu::generateMipmap(GrGLTexture* texture, bool gammaCorrect) {
Brian Salomonbf7b6202016-11-11 16:08:03 -05004029 SkASSERT(!GrPixelConfigIsSint(texture->config()));
brianosman09563ce2016-06-02 08:59:34 -07004030 // Our iterative downsample requires the ability to limit which level we're sampling:
4031 if (!this->glCaps().doManualMipmapping()) {
brianosman33f6b3f2016-06-02 05:49:21 -07004032 return false;
4033 }
4034
4035 // Mipmaps are only supported on 2D textures:
4036 if (GR_GL_TEXTURE_2D != texture->target()) {
4037 return false;
4038 }
4039
4040 // We need to be able to render to the texture for this to work:
Brian Salomon71d9d842016-11-03 13:42:00 -04004041 if (!this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
brianosman33f6b3f2016-06-02 05:49:21 -07004042 return false;
4043 }
4044
brianosman33f6b3f2016-06-02 05:49:21 -07004045 // If we're mipping an sRGB texture, we need to ensure FB sRGB is correct:
4046 if (GrPixelConfigIsSRGB(texture->config())) {
4047 // If we have write-control, just set the state that we want:
4048 if (this->glCaps().srgbWriteControl()) {
4049 this->flushFramebufferSRGB(gammaCorrect);
4050 } else if (!gammaCorrect) {
4051 // If we don't have write-control we can't do non-gamma-correct mipmapping:
4052 return false;
4053 }
4054 }
4055
4056 int width = texture->width();
4057 int height = texture->height();
4058 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
4059
4060 // Define all mips, if we haven't previously done so:
4061 if (0 == texture->texturePriv().maxMipMapLevel()) {
4062 GrGLenum internalFormat;
4063 GrGLenum externalFormat;
4064 GrGLenum externalType;
4065 if (!this->glCaps().getTexImageFormats(texture->config(), texture->config(),
4066 &internalFormat, &externalFormat, &externalType)) {
4067 return false;
4068 }
4069
4070 for (GrGLint level = 1; level < levelCount; ++level) {
4071 // Define the next mip:
4072 width = SkTMax(1, width / 2);
4073 height = SkTMax(1, height / 2);
4074 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
4075 width, height, 0,
4076 externalFormat, externalType, nullptr));
4077 }
4078 }
4079
4080 // Create (if necessary), then bind temporary FBO:
4081 if (0 == fTempDstFBOID) {
4082 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
4083 }
4084 GL_CALL(BindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID));
Robert Phillips294870f2016-11-11 12:38:40 -05004085 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07004086
4087 // Bind the texture, to get things configured for filtering.
4088 // We'll be changing our base level further below:
4089 this->setTextureUnit(0);
Brian Salomon514baff2016-11-17 15:17:07 -05004090 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kBilerp_FilterMode);
Robert Phillips7294b852017-08-01 13:51:44 +00004091 this->bindTexture(0, params, gammaCorrect, texture);
brianosman33f6b3f2016-06-02 05:49:21 -07004092
4093 // Vertex data:
4094 if (!fMipmapProgramArrayBuffer) {
4095 static const GrGLfloat vdata[] = {
4096 0, 0,
4097 0, 1,
4098 1, 0,
4099 1, 1
4100 };
4101 fMipmapProgramArrayBuffer.reset(GrGLBuffer::Create(this, sizeof(vdata),
4102 kVertex_GrBufferType,
4103 kStatic_GrAccessPattern, vdata));
4104 }
4105 if (!fMipmapProgramArrayBuffer) {
4106 return false;
4107 }
4108
4109 fHWVertexArrayState.setVertexArrayID(this, 0);
4110
4111 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04004112 attribs->enableVertexArrays(this, 1);
Hal Canary144caf52016-11-07 17:57:18 -05004113 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kVec2f_GrVertexAttribType,
brianosman33f6b3f2016-06-02 05:49:21 -07004114 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07004115
4116 // Set "simple" state once:
4117 GrXferProcessor::BlendInfo blendInfo;
4118 blendInfo.reset();
4119 this->flushBlend(blendInfo, GrSwizzle::RGBA());
4120 this->flushColorWrite(true);
brianosman33f6b3f2016-06-02 05:49:21 -07004121 this->flushHWAAState(nullptr, false, false);
4122 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07004123 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07004124 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07004125
4126 // Do all the blits:
4127 width = texture->width();
4128 height = texture->height();
4129 GrGLIRect viewport;
4130 viewport.fLeft = 0;
4131 viewport.fBottom = 0;
4132 for (GrGLint level = 1; level < levelCount; ++level) {
4133 // Get and bind the program for this particular downsample (filter shape can vary):
4134 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
4135 if (!fMipmapPrograms[progIdx].fProgram) {
4136 if (!this->createMipmapProgram(progIdx)) {
4137 SkDebugf("Failed to create mipmap program.\n");
4138 return false;
4139 }
4140 }
4141 GL_CALL(UseProgram(fMipmapPrograms[progIdx].fProgram));
4142 fHWProgramID = fMipmapPrograms[progIdx].fProgram;
4143
4144 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
4145 const float invWidth = 1.0f / width;
4146 const float invHeight = 1.0f / height;
4147 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
4148 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
4149 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
4150
4151 // Only sample from previous mip
4152 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
4153
4154 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4155 GR_GL_TEXTURE_2D, texture->textureID(), level));
4156
4157 width = SkTMax(1, width / 2);
4158 height = SkTMax(1, height / 2);
4159 viewport.fWidth = width;
4160 viewport.fHeight = height;
4161 this->flushViewport(viewport);
4162
4163 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
4164 }
4165
4166 // Unbind:
4167 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4168 GR_GL_TEXTURE_2D, 0, 0));
4169
4170 return true;
4171}
4172
Robert Phillips7294b852017-08-01 13:51:44 +00004173void GrGLGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings& stencil,
csmartdaltonc25c5d72016-11-01 07:03:59 -07004174 int* effectiveSampleCnt, SamplePattern* samplePattern) {
Brian Salomon7c8460e2017-05-12 11:36:10 -04004175 SkASSERT(GrFSAAType::kMixedSamples != rt->fsaaType() ||
4176 rt->renderTargetPriv().getStencilAttachment() || stencil.isDisabled());
cdalton28f45b92016-03-07 13:58:26 -08004177
4178 this->flushStencil(stencil);
4179 this->flushHWAAState(rt, true, !stencil.isDisabled());
4180 this->flushRenderTarget(static_cast<GrGLRenderTarget*>(rt), &SkIRect::EmptyIRect());
4181
4182 if (0 != this->caps()->maxRasterSamples()) {
4183 GR_GL_GetIntegerv(this->glInterface(), GR_GL_EFFECTIVE_RASTER_SAMPLES, effectiveSampleCnt);
4184 } else {
4185 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, effectiveSampleCnt);
4186 }
4187
Brian Salomon154ce912017-05-17 10:40:02 -04004188 SkASSERT(*effectiveSampleCnt >= rt->numStencilSamples());
cdalton28f45b92016-03-07 13:58:26 -08004189
4190 if (this->caps()->sampleLocationsSupport()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004191 samplePattern->reset(*effectiveSampleCnt);
cdalton28f45b92016-03-07 13:58:26 -08004192 for (int i = 0; i < *effectiveSampleCnt; ++i) {
4193 GrGLfloat pos[2];
4194 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, pos));
Robert Phillips7294b852017-08-01 13:51:44 +00004195 if (kTopLeft_GrSurfaceOrigin == rt->origin()) {
csmartdalton0d28e572016-07-06 09:59:43 -07004196 (*samplePattern)[i].set(pos[0], pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004197 } else {
csmartdalton0d28e572016-07-06 09:59:43 -07004198 (*samplePattern)[i].set(pos[0], 1 - pos[1]);
cdalton28f45b92016-03-07 13:58:26 -08004199 }
4200 }
4201 }
4202}
4203
cdalton231c5fd2015-05-13 12:35:36 -07004204void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07004205 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07004206 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07004207 case kTexture_GrXferBarrierType: {
4208 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
4209 if (glrt->textureFBOID() != glrt->renderFBOID()) {
4210 // The render target uses separate storage so no need for glTextureBarrier.
4211 // FIXME: The render target will resolve automatically when its texture is bound,
4212 // but we could resolve only the bounds that will be read if we do it here instead.
4213 return;
4214 }
cdalton9954bc32015-04-29 14:17:00 -07004215 SkASSERT(this->caps()->textureBarrierSupport());
4216 GL_CALL(TextureBarrier());
4217 return;
cdalton231c5fd2015-05-13 12:35:36 -07004218 }
cdalton8917d622015-05-06 13:40:21 -07004219 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07004220 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07004221 this->caps()->blendEquationSupport());
4222 GL_CALL(BlendBarrier());
4223 return;
bsalomoncb02b382015-08-12 11:14:50 -07004224 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07004225 }
4226}
4227
jvanverth88957922015-07-14 11:02:52 -07004228GrBackendObject GrGLGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07004229 GrPixelConfig config, bool /*isRT*/) {
bsalomon926cb022015-12-17 18:15:11 -08004230 if (!this->caps()->isConfigTexturable(config)) {
4231 return false;
4232 }
Ben Wagner18b61f92016-10-25 10:44:02 -04004233 std::unique_ptr<GrGLTextureInfo> info = skstd::make_unique<GrGLTextureInfo>();
bsalomon091f60c2015-11-10 11:54:56 -08004234 info->fTarget = GR_GL_TEXTURE_2D;
kkinnunen546eb5c2015-12-11 00:05:33 -08004235 info->fID = 0;
bsalomon091f60c2015-11-10 11:54:56 -08004236 GL_CALL(GenTextures(1, &info->fID));
jvanverth672bb7f2015-07-13 07:19:57 -07004237 GL_CALL(ActiveTexture(GR_GL_TEXTURE0));
4238 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
bsalomon091f60c2015-11-10 11:54:56 -08004239 GL_CALL(BindTexture(info->fTarget, info->fID));
Robert Phillips294870f2016-11-11 12:38:40 -05004240 fHWBoundTextureUniqueIDs[0].makeInvalid();
bsalomon091f60c2015-11-10 11:54:56 -08004241 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST));
4242 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST));
4243 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE));
4244 GL_CALL(TexParameteri(info->fTarget, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE));
jvanverth672bb7f2015-07-13 07:19:57 -07004245
bsalomon76148af2016-01-12 11:13:47 -08004246 GrGLenum internalFormat;
4247 GrGLenum externalFormat;
4248 GrGLenum externalType;
4249
4250 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
4251 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08004252 return reinterpret_cast<GrBackendObject>(nullptr);
bsalomon76148af2016-01-12 11:13:47 -08004253 }
jvanverth672bb7f2015-07-13 07:19:57 -07004254
bsalomon091f60c2015-11-10 11:54:56 -08004255 GL_CALL(TexImage2D(info->fTarget, 0, internalFormat, w, h, 0, externalFormat,
jvanverth672bb7f2015-07-13 07:19:57 -07004256 externalType, pixels));
4257
Ben Wagner18b61f92016-10-25 10:44:02 -04004258 return reinterpret_cast<GrBackendObject>(info.release());
jvanverth672bb7f2015-07-13 07:19:57 -07004259}
4260
jvanverth88957922015-07-14 11:02:52 -07004261bool GrGLGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
bsalomon091f60c2015-11-10 11:54:56 -08004262 GrGLuint texID = reinterpret_cast<const GrGLTextureInfo*>(id)->fID;
jvanverth672bb7f2015-07-13 07:19:57 -07004263
4264 GrGLboolean result;
4265 GL_CALL_RET(result, IsTexture(texID));
4266
4267 return (GR_GL_TRUE == result);
4268}
4269
bsalomone63ffef2016-02-05 07:17:34 -08004270void GrGLGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) {
Ben Wagner18b61f92016-10-25 10:44:02 -04004271 std::unique_ptr<const GrGLTextureInfo> info(reinterpret_cast<const GrGLTextureInfo*>(id));
bsalomon091f60c2015-11-10 11:54:56 -08004272 GrGLuint texID = info->fID;
bsalomon091f60c2015-11-10 11:54:56 -08004273
bsalomon67d76202015-11-11 12:40:42 -08004274 if (!abandonTexture) {
4275 GL_CALL(DeleteTextures(1, &texID));
4276 }
jvanverth672bb7f2015-07-13 07:19:57 -07004277}
4278
joshualitt8fd844f2015-12-02 13:36:47 -08004279void GrGLGpu::resetShaderCacheForTesting() const {
4280 fProgramCache->abandon();
4281}
4282
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004283///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004284
cdaltone2e71c22016-04-07 18:13:29 -07004285GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004286 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004287 GrGLAttribArrayState* attribState;
4288
cdaltone2e71c22016-04-07 18:13:29 -07004289 if (gpu->glCaps().isCoreProfile()) {
4290 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004291 GrGLuint arrayID;
4292 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4293 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004294 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004295 }
cdaltone2e71c22016-04-07 18:13:29 -07004296 if (ibuf) {
4297 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004298 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004299 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004300 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004301 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004302 if (ibuf) {
4303 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
4304 gpu->bindBuffer(kIndex_GrBufferType, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004305 } else {
4306 this->setVertexArrayID(gpu, 0);
4307 }
4308 int attrCount = gpu->glCaps().maxVertexAttributes();
4309 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4310 fDefaultVertexArrayAttribState.resize(attrCount);
4311 }
4312 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004313 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004314 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004315}
bsalomone179a912016-01-20 06:18:10 -08004316
Robert Phillips3798c862017-03-27 11:08:16 -04004317bool GrGLGpu::onIsACopyNeededForTextureParams(GrTextureProxy* proxy,
Robert Phillips81444fb2017-03-21 09:14:35 -04004318 const GrSamplerParams& textureParams,
4319 GrTextureProducer::CopyParams* copyParams,
4320 SkScalar scaleAdjust[2]) const {
Robert Phillips3798c862017-03-27 11:08:16 -04004321 const GrTexture* texture = proxy->priv().peekTexture();
4322 if (!texture) {
4323 // The only way to get and EXTERNAL or RECTANGLE texture in Ganesh is to wrap them.
4324 // In that case the proxy should already be instantiated.
4325 return false;
4326 }
4327
bsalomone179a912016-01-20 06:18:10 -08004328 if (textureParams.isTiled() ||
Brian Salomon514baff2016-11-17 15:17:07 -05004329 GrSamplerParams::kMipMap_FilterMode == textureParams.filterMode()) {
Robert Phillips3798c862017-03-27 11:08:16 -04004330 const GrGLTexture* glTexture = static_cast<const GrGLTexture*>(texture);
bsalomone179a912016-01-20 06:18:10 -08004331 if (GR_GL_TEXTURE_EXTERNAL == glTexture->target() ||
4332 GR_GL_TEXTURE_RECTANGLE == glTexture->target()) {
Brian Salomon514baff2016-11-17 15:17:07 -05004333 copyParams->fFilter = GrSamplerParams::kNone_FilterMode;
bsalomone179a912016-01-20 06:18:10 -08004334 copyParams->fWidth = texture->width();
4335 copyParams->fHeight = texture->height();
4336 return true;
4337 }
4338 }
4339 return false;
4340}
jvanverth84741b32016-09-30 08:39:02 -07004341
Greg Daniel51316782017-08-02 15:10:09 +00004342void GrGLGpu::onFinishFlush(bool insertedSemaphore) {
4343 // If we inserted semaphores during the flush, we need to call GLFlush.
4344 if (insertedSemaphore) {
4345 GL_CALL(Flush());
4346 }
4347}
4348
Greg Daniel6be35232017-03-01 17:01:09 -05004349GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004350 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004351 GrGLsync sync;
4352 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4353 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4354 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004355}
4356
Greg Daniel6be35232017-03-01 17:01:09 -05004357bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
jvanverth84741b32016-09-30 08:39:02 -07004358 GrGLenum result;
4359 GL_CALL_RET(result, ClientWaitSync((GrGLsync)fence, GR_GL_SYNC_FLUSH_COMMANDS_BIT, timeout));
4360 return (GR_GL_CONDITION_SATISFIED == result);
4361}
4362
4363void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004364 this->deleteSync((GrGLsync)fence);
4365}
4366
Greg Daniela5cb7812017-06-16 09:45:32 -04004367sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Greg Danielc64ee462017-06-15 16:59:49 -04004368 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004369 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004370}
4371
Greg Daniela5cb7812017-06-16 09:45:32 -04004372sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4373 GrWrapOwnership ownership) {
Greg Danielc64ee462017-06-15 16:59:49 -04004374 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004375 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4376}
4377
Brian Osmandc87c952017-04-28 13:57:38 -04004378void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05004379 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4380
4381 GrGLsync sync;
4382 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4383 glSem->setSync(sync);
Brian Osmandc87c952017-04-28 13:57:38 -04004384
4385 if (flush) {
4386 GL_CALL(Flush());
4387 }
Greg Daniel6be35232017-03-01 17:01:09 -05004388}
4389
4390void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
4391 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4392
4393 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4394}
4395
4396void GrGLGpu::deleteSync(GrGLsync sync) const {
4397 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004398}
Brian Osman13dddce2017-05-09 13:19:50 -04004399
Robert Phillips65a88fa2017-08-08 08:36:22 -04004400void GrGLGpu::insertEventMarker(const char* msg) {
4401 GL_CALL(InsertEventMarker(strlen(msg), msg));
4402}
4403
Brian Osman13dddce2017-05-09 13:19:50 -04004404sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4405 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004406 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Brian Osman13dddce2017-05-09 13:19:50 -04004407 this->insertSemaphore(semaphore, true);
4408
4409 return semaphore;
4410}
Robert Phillips646e4292017-06-13 12:44:56 -04004411
4412int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
4413 switch (texture->texturePriv().samplerType()) {
4414 case kTexture2DSampler_GrSLType:
4415 return 0;
4416 case kITexture2DSampler_GrSLType:
4417 return 1;
4418 case kTexture2DRectSampler_GrSLType:
4419 return 2;
4420 case kTextureExternalSampler_GrSLType:
4421 return 3;
4422 default:
4423 SkFAIL("Unexpected samper type");
4424 return 0;
4425 }
4426}