blob: 5f53234c15bbacee322567181c6bc584861c2fef [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040022#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040024#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrFixedClip.h"
26#include "src/gpu/GrGpuResourcePriv.h"
27#include "src/gpu/GrMesh.h"
28#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040029#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrRenderTargetPriv.h"
31#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/gl/GrGLBuffer.h"
35#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040036#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/gl/GrGLSemaphore.h"
38#include "src/gpu/gl/GrGLStencilAttachment.h"
39#include "src/gpu/gl/GrGLTextureRenderTarget.h"
40#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
41#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <cmath>
44
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000046#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000047
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000048#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
49 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
50 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
51 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000053 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
54 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
55 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
56#endif
57
Jim Van Verth32ac83e2016-11-28 15:23:57 -050058//#define USE_NSIGHT
59
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000060///////////////////////////////////////////////////////////////////////////////
61
cdalton8917d622015-05-06 13:40:21 -070062static const GrGLenum gXfermodeEquation2Blend[] = {
63 // Basic OpenGL blend equations.
64 GR_GL_FUNC_ADD,
65 GR_GL_FUNC_SUBTRACT,
66 GR_GL_FUNC_REVERSE_SUBTRACT,
67
68 // GL_KHR_blend_equation_advanced.
69 GR_GL_SCREEN,
70 GR_GL_OVERLAY,
71 GR_GL_DARKEN,
72 GR_GL_LIGHTEN,
73 GR_GL_COLORDODGE,
74 GR_GL_COLORBURN,
75 GR_GL_HARDLIGHT,
76 GR_GL_SOFTLIGHT,
77 GR_GL_DIFFERENCE,
78 GR_GL_EXCLUSION,
79 GR_GL_MULTIPLY,
80 GR_GL_HSL_HUE,
81 GR_GL_HSL_SATURATION,
82 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050083 GR_GL_HSL_LUMINOSITY,
84
85 // Illegal... needs to map to something.
86 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070087};
Brian Salomon4dea72a2019-12-18 10:43:10 -050088static_assert(0 == kAdd_GrBlendEquation);
89static_assert(1 == kSubtract_GrBlendEquation);
90static_assert(2 == kReverseSubtract_GrBlendEquation);
91static_assert(3 == kScreen_GrBlendEquation);
92static_assert(4 == kOverlay_GrBlendEquation);
93static_assert(5 == kDarken_GrBlendEquation);
94static_assert(6 == kLighten_GrBlendEquation);
95static_assert(7 == kColorDodge_GrBlendEquation);
96static_assert(8 == kColorBurn_GrBlendEquation);
97static_assert(9 == kHardLight_GrBlendEquation);
98static_assert(10 == kSoftLight_GrBlendEquation);
99static_assert(11 == kDifference_GrBlendEquation);
100static_assert(12 == kExclusion_GrBlendEquation);
101static_assert(13 == kMultiply_GrBlendEquation);
102static_assert(14 == kHSLHue_GrBlendEquation);
103static_assert(15 == kHSLSaturation_GrBlendEquation);
104static_assert(16 == kHSLColor_GrBlendEquation);
105static_assert(17 == kHSLLuminosity_GrBlendEquation);
106static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700107
twiz@google.com0f31ca72011-03-18 17:38:11 +0000108static const GrGLenum gXfermodeCoeff2Blend[] = {
109 GR_GL_ZERO,
110 GR_GL_ONE,
111 GR_GL_SRC_COLOR,
112 GR_GL_ONE_MINUS_SRC_COLOR,
113 GR_GL_DST_COLOR,
114 GR_GL_ONE_MINUS_DST_COLOR,
115 GR_GL_SRC_ALPHA,
116 GR_GL_ONE_MINUS_SRC_ALPHA,
117 GR_GL_DST_ALPHA,
118 GR_GL_ONE_MINUS_DST_ALPHA,
119 GR_GL_CONSTANT_COLOR,
120 GR_GL_ONE_MINUS_CONSTANT_COLOR,
121 GR_GL_CONSTANT_ALPHA,
122 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000123
124 // extended blend coeffs
125 GR_GL_SRC1_COLOR,
126 GR_GL_ONE_MINUS_SRC1_COLOR,
127 GR_GL_SRC1_ALPHA,
128 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500129
130 // Illegal... needs to map to something.
131 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
bsalomon861e1032014-12-16 07:33:49 -0800134bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000135 static const bool gCoeffReferencesBlendConst[] = {
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 false,
146 true,
147 true,
148 true,
149 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150
151 // extended blend coeffs
152 false,
153 false,
154 false,
155 false,
Mike Klein36743362018-11-06 08:23:30 -0500156
157 // Illegal.
158 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000159 };
160 return gCoeffReferencesBlendConst[coeff];
Brian Salomon4dea72a2019-12-18 10:43:10 -0500161 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000162
Brian Salomon4dea72a2019-12-18 10:43:10 -0500163 static_assert(0 == kZero_GrBlendCoeff);
164 static_assert(1 == kOne_GrBlendCoeff);
165 static_assert(2 == kSC_GrBlendCoeff);
166 static_assert(3 == kISC_GrBlendCoeff);
167 static_assert(4 == kDC_GrBlendCoeff);
168 static_assert(5 == kIDC_GrBlendCoeff);
169 static_assert(6 == kSA_GrBlendCoeff);
170 static_assert(7 == kISA_GrBlendCoeff);
171 static_assert(8 == kDA_GrBlendCoeff);
172 static_assert(9 == kIDA_GrBlendCoeff);
173 static_assert(10 == kConstC_GrBlendCoeff);
174 static_assert(11 == kIConstC_GrBlendCoeff);
175 static_assert(12 == kConstA_GrBlendCoeff);
176 static_assert(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
Brian Salomon4dea72a2019-12-18 10:43:10 -0500178 static_assert(14 == kS2C_GrBlendCoeff);
179 static_assert(15 == kIS2C_GrBlendCoeff);
180 static_assert(16 == kS2A_GrBlendCoeff);
181 static_assert(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000182
183 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
Brian Salomon4dea72a2019-12-18 10:43:10 -0500184 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000185}
186
Brian Salomond978b902019-02-07 15:09:18 -0500187//////////////////////////////////////////////////////////////////////////////
188
189static int gl_target_to_binding_index(GrGLenum target) {
190 switch (target) {
191 case GR_GL_TEXTURE_2D:
192 return 0;
193 case GR_GL_TEXTURE_RECTANGLE:
194 return 1;
195 case GR_GL_TEXTURE_EXTERNAL:
196 return 2;
197 }
198 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500199}
200
201GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500202 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
203}
204
205bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
206 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500207}
208
209void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500210 int targetIndex = gl_target_to_binding_index(target);
211 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
212 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500213}
214
Brian Salomon1f05d452019-02-08 12:33:08 -0500215void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
216 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500217}
218
Brian Salomon1f05d452019-02-08 12:33:08 -0500219void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
220 for (auto& targetBinding : fTargetBindings) {
221 targetBinding.fBoundResourceID.makeInvalid();
222 if (markUnmodified) {
223 targetBinding.fHasBeenModified = false;
224 }
Brian Salomond978b902019-02-07 15:09:18 -0500225 }
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
Brian Salomondc829942018-10-23 16:07:24 -0400230static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
231 switch (filter) {
232 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
233 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
234 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
235 }
236 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400237}
238
239static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
240 switch (filter) {
241 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
242 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
243 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
244 }
245 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400246}
247
Michael Ludwigf23a1522018-12-10 11:36:13 -0500248static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
249 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400250 switch (wrapMode) {
251 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
252 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
253 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500254 case GrSamplerState::WrapMode::kClampToBorder:
255 // May not be supported but should have been caught earlier
256 SkASSERT(caps.clampToBorderSupport());
257 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500258 }
Brian Salomondc829942018-10-23 16:07:24 -0400259 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400260}
261
262///////////////////////////////////////////////////////////////////////////////
263
264class GrGLGpu::SamplerObjectCache {
265public:
266 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
267 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
268 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
269 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
270 std::fill_n(fSamplers, kNumSamplers, 0);
271 }
272
273 ~SamplerObjectCache() {
274 if (!fNumTextureUnits) {
275 // We've already been abandoned.
276 return;
277 }
Chris Dalton703fe682019-05-15 12:58:12 -0600278 for (GrGLuint sampler : fSamplers) {
279 // The spec states that "zero" values should be silently ignored, however they still
280 // trigger GL errors on some NVIDIA platforms.
281 if (sampler) {
282 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
283 }
284 }
Brian Salomondc829942018-10-23 16:07:24 -0400285 }
286
Brian Salomonccb61422020-01-09 10:46:36 -0500287 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400288 int index = StateToIndex(state);
289 if (!fSamplers[index]) {
290 GrGLuint s;
291 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
292 if (!s) {
293 return;
294 }
295 fSamplers[index] = s;
296 auto minFilter = filter_to_gl_min_filter(state.filter());
297 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500298 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
299 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400300 GR_GL_CALL(fGpu->glInterface(),
301 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
302 GR_GL_CALL(fGpu->glInterface(),
303 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
304 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
305 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
306 }
307 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
308 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
309 fHWBoundSamplers[unitIdx] = fSamplers[index];
310 }
311 }
312
313 void invalidateBindings() {
314 // When we have sampler support we always use samplers. So setting these to zero will cause
315 // a rebind on next usage.
316 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
317 }
318
319 void abandon() {
320 fHWBoundSamplers.reset();
321 fNumTextureUnits = 0;
322 }
323
324 void release() {
325 if (!fNumTextureUnits) {
326 // We've already been abandoned.
327 return;
328 }
329 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
330 std::fill_n(fSamplers, kNumSamplers, 0);
331 // Deleting a bound sampler implicitly binds sampler 0.
332 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
333 }
334
335private:
Brian Salomonccb61422020-01-09 10:46:36 -0500336 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400337 int filter = static_cast<int>(state.filter());
338 SkASSERT(filter >= 0 && filter < 3);
339 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500340 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400341 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500342 SkASSERT(wrapY >= 0 && wrapY < 4);
343 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400344 SkASSERT(idx < kNumSamplers);
345 return idx;
346 }
347
348 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500349 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400350 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
351 GrGLuint fSamplers[kNumSamplers];
352 int fNumTextureUnits;
353};
354
reed@google.comac10a2d2010-12-22 21:39:39 +0000355///////////////////////////////////////////////////////////////////////////////
356
Brian Salomon384fab42017-12-07 12:33:05 -0500357sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
358 GrContext* context) {
359 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500360 interface = GrGLMakeNativeInterface();
361 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
362 // to GrGLMakeNativeInterface.
363 if (!interface) {
364 interface = sk_ref_sp(GrGLCreateNativeInterface());
365 }
Brian Salomon384fab42017-12-07 12:33:05 -0500366 if (!interface) {
367 return nullptr;
368 }
bsalomon424cc262015-05-22 10:37:30 -0700369 }
Jim Van Verth76334772017-05-05 16:46:05 -0400370#ifdef USE_NSIGHT
371 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
372#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500373 auto glContext = GrGLContext::Make(std::move(interface), options);
374 if (!glContext) {
375 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700376 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500377 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700378}
379
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500380GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
381 : GrGpu(context)
382 , fGLContext(std::move(ctx))
383 , fProgramCache(new ProgramCache(this))
384 , fHWProgramID(0)
385 , fTempSrcFBOID(0)
386 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500387 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500388 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400389 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500390 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000391
Brian Salomond978b902019-02-07 15:09:18 -0500392 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000393
Brian Salomonae64c192019-02-05 09:41:37 -0500394 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
395 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500396 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500397 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
398 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
399 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
400 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700401 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500402 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
403 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700404 }
Brian Salomonae64c192019-02-05 09:41:37 -0500405 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400406 fHWBufferState[i].invalidate();
407 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500408 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700409
410 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
411 fPathRendering.reset(new GrGLPathRendering(this));
412 }
413
Brian Salomondc829942018-10-23 16:07:24 -0400414 if (this->glCaps().samplerObjectSupport()) {
415 fSamplerObjectCache.reset(new SamplerObjectCache(this));
416 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000417}
418
bsalomon861e1032014-12-16 07:33:49 -0800419GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700420 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
421 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800422 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700423 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700424 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800425
Brian Salomon802cb312018-06-08 18:05:20 -0400426 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400427 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000428 // detach the current program so there is no confusion on OpenGL's part
429 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000430 GL_CALL(UseProgram(0));
431 }
432
Brian Salomon43f8bf02017-10-18 08:33:29 -0400433 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700434 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800435 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700437 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800438 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400439 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700440 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800441 }
egdaniel0f5f9672015-02-03 11:10:51 -0800442
bsalomon7ea33f52015-11-22 14:51:00 -0800443 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
444 if (0 != fCopyPrograms[i].fProgram) {
445 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
446 }
bsalomon6df86402015-06-01 10:41:49 -0700447 }
bsalomon6dea83f2015-12-03 12:58:06 -0800448
brianosman33f6b3f2016-06-02 05:49:21 -0700449 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
450 if (0 != fMipmapPrograms[i].fProgram) {
451 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
452 }
453 }
454
Brian Salomondc829942018-10-23 16:07:24 -0400455 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700456}
457
bsalomon6e2aad42016-04-01 11:54:31 -0700458void GrGLGpu::disconnect(DisconnectType type) {
459 INHERITED::disconnect(type);
460 if (DisconnectType::kCleanup == type) {
461 if (fHWProgramID) {
462 GL_CALL(UseProgram(0));
463 }
464 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700465 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700466 }
467 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700468 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700469 }
470 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700471 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700472 }
bsalomon6e2aad42016-04-01 11:54:31 -0700473 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
474 if (fCopyPrograms[i].fProgram) {
475 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
476 }
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
479 if (fMipmapPrograms[i].fProgram) {
480 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
481 }
482 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400483
Brian Salomondc829942018-10-23 16:07:24 -0400484 if (fSamplerObjectCache) {
485 fSamplerObjectCache->release();
486 }
bsalomon6e2aad42016-04-01 11:54:31 -0700487 } else {
488 if (fProgramCache) {
489 fProgramCache->abandon();
490 }
Brian Salomondc829942018-10-23 16:07:24 -0400491 if (fSamplerObjectCache) {
492 fSamplerObjectCache->abandon();
493 }
bsalomon6e2aad42016-04-01 11:54:31 -0700494 }
495
Brian Salomon802cb312018-06-08 18:05:20 -0400496 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500497 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700498
bsalomonc8dc1f72014-08-21 13:02:13 -0700499 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700500 fTempSrcFBOID = 0;
501 fTempDstFBOID = 0;
502 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700503 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800504 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
505 fCopyPrograms[i].fProgram = 0;
506 }
brianosman33f6b3f2016-06-02 05:49:21 -0700507 fMipmapProgramArrayBuffer.reset();
508 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
509 fMipmapPrograms[i].fProgram = 0;
510 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400511
jvanverthe9c0fc62015-04-29 11:18:05 -0700512 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700513 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700514 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000515}
516
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000517///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000518
bsalomon861e1032014-12-16 07:33:49 -0800519void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000520 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400521 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000522 GL_CALL(Disable(GR_GL_DEPTH_TEST));
523 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000524
Brian Salomonf0861672017-05-08 11:10:10 -0400525 // We don't use face culling.
526 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600527 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
528 // just set this to the default for self-consistency.
529 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400530
Brian Salomonae64c192019-02-05 09:41:37 -0500531 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
532 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700533
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400534 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500535#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 // Desktop-only state that we never change
537 if (!this->glCaps().isCoreProfile()) {
538 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
539 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
540 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
541 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
542 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
543 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
544 }
545 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
546 // core profile. This seems like a bug since the core spec removes any mention of
547 // GL_ARB_imaging.
548 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
549 GL_CALL(Disable(GR_GL_COLOR_TABLE));
550 }
551 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400552
Chris Dalton1215cda2019-12-17 21:44:04 -0700553 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500554#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 // Since ES doesn't support glPointSize at all we always use the VS to
556 // set the point size
557 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
558
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000559 }
joshualitt58162332014-08-01 06:44:53 -0700560
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400561 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500562 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700563 // The arm extension requires specifically enabling MSAA fetching per sample.
564 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500565 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700566 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000567 fHWWriteToColor = kUnknown_TriState;
568 // we only ever use lines in hairline mode
569 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700570 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500571
572 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000573 }
edisonn@google.comba669992013-06-28 16:03:21 +0000574
egdanielb414f252014-07-29 13:15:47 -0700575 if (resetBits & kMSAAEnable_GrGLBackendState) {
576 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700577
Chris Dalton6ce447a2019-06-23 18:07:38 -0600578 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800579 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
580 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700581 GL_CALL(CoverageModulation(GR_GL_RGBA));
582 }
Chris Daltonce425af2019-12-16 10:39:03 -0700583
584 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000585 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000587 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400588 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000589
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000590 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500591 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500592 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000593 }
Brian Salomondc829942018-10-23 16:07:24 -0400594 if (fSamplerObjectCache) {
595 fSamplerObjectCache->invalidateBindings();
596 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000597 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000598
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 if (resetBits & kBlend_GrGLBackendState) {
600 fHWBlendState.invalidate();
601 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000602
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000603 if (resetBits & kView_GrGLBackendState) {
604 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700605 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 fHWViewport.invalidate();
607 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000608
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 if (resetBits & kStencil_GrGLBackendState) {
610 fHWStencilSettings.invalidate();
611 fHWStencilTestEnabled = kUnknown_TriState;
612 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000613
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000614 // Vertex
615 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700616 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500617 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
618 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700619 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400635 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
Brian Salomon1047a492019-07-02 12:25:21 -0400638 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400650 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000651}
652
Brian Salomonea4ad302019-08-07 13:04:55 -0400653static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
654 const GrGLCaps& caps, GrGLTexture::Desc* desc,
655 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400656 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400657 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800659 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000660
Brian Salomonea4ad302019-08-07 13:04:55 -0400661 desc->fSize = {backendTex.width(), backendTex.height()};
662 desc->fTarget = info.fTarget;
663 desc->fID = info.fID;
664 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800665
Brian Salomonea4ad302019-08-07 13:04:55 -0400666 if (desc->fFormat == GrGLFormat::kUnknown) {
667 return false;
668 }
669 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400670 if (!caps.shaderCaps()->externalTextureSupport()) {
671 return false;
672 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400673 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
674 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400675 return false;
676 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400677 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400678 return false;
679 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400680 if (backendTex.isProtected()) {
681 // Not supported in GL backend at this time.
682 return false;
683 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400684
Brian Salomond17f6582017-07-19 18:28:58 -0400685 return true;
686}
687
688sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400689 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400690 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400691 GrGLTexture::Desc desc;
692 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400693 return nullptr;
694 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400695
Brian Salomond17f6582017-07-19 18:28:58 -0400696 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400697 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400698 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400699 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400700 }
bsalomone5286e02016-01-14 09:24:09 -0800701
Greg Daniel177e6952017-10-12 12:27:11 -0400702 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
703 : GrMipMapsStatus::kNotAllocated;
704
Brian Salomonea4ad302019-08-07 13:04:55 -0400705 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400706 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400707 // We don't know what parameters are already set on wrapped textures.
708 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600709 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400710}
711
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500712static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
713 const GrGLCaps& caps, GrGLTexture::Desc* desc,
714 bool skipRectTexSupportCheck = false) {
715 GrGLTextureInfo info;
716 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
717 return false;
718 }
719
720 desc->fSize = {backendTex.width(), backendTex.height()};
721 desc->fTarget = info.fTarget;
722 desc->fID = info.fID;
723 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
724
725 if (desc->fFormat == GrGLFormat::kUnknown) {
726 return false;
727 }
728
729 if (GR_GL_TEXTURE_2D != desc->fTarget) {
730 return false;
731 }
732 if (backendTex.isProtected()) {
733 // Not supported in GL backend at this time.
734 return false;
735 }
736
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500737 return true;
738}
739
Robert Phillipsb915c942019-12-17 14:44:37 -0500740sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
741 GrWrapOwnership ownership,
742 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500743 GrGLTexture::Desc desc;
744 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
745 return nullptr;
746 }
747
748 if (kBorrow_GrWrapOwnership == ownership) {
749 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
750 } else {
751 desc.fOwnership = GrBackendObjectOwnership::kOwned;
752 }
753
754 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
755 : GrMipMapsStatus::kNotAllocated;
756
757 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
758 backendTex.getGLTextureParams(), cacheable,
759 kRead_GrIOType);
760 // We don't know what parameters are already set on wrapped textures.
761 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600762 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500763}
764
Brian Salomond17f6582017-07-19 18:28:58 -0400765sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400766 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400767 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500768 GrWrapOwnership ownership,
769 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400770 const GrGLCaps& caps = this->glCaps();
771
Brian Salomonea4ad302019-08-07 13:04:55 -0400772 GrGLTexture::Desc desc;
773 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800774 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800775 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400776 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
777 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800778
Brian Salomond17f6582017-07-19 18:28:58 -0400779 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400780 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800781 return nullptr;
782 }
bsalomon10528f12015-10-14 12:54:52 -0700783
Brian Osman766fcbb2017-03-13 09:33:09 -0400784 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400785 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400786 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400787 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800788 }
bsalomonb15b4c12014-10-29 12:41:57 -0700789
Robert Phillips0902c982019-07-16 07:47:56 -0400790
Greg Daniel6fa62e22019-08-07 15:52:37 -0400791 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
792 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000793
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 GrGLRenderTarget::IDs rtIDs;
795 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400796 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000797 }
Greg Daniel177e6952017-10-12 12:27:11 -0400798
799 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
800 : GrMipMapsStatus::kNotAllocated;
801
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500802 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400803 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400804 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400805 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400806 // We don't know what parameters are already set on wrapped textures.
807 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600808 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000809}
810
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400811sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
812 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400813 GrGLFramebufferInfo info;
814 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000815 return nullptr;
816 }
817
Brian Salomone8a766b2019-07-19 14:24:36 -0400818 if (backendRT.isProtected()) {
819 // Not supported in GL at this time.
820 return nullptr;
821 }
822
Brian Salomond4764a12019-08-08 12:08:24 -0400823 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400824 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
825 return nullptr;
826 }
827
Brian Salomonea4ad302019-08-07 13:04:55 -0400828 GrGLRenderTarget::IDs rtIDs;
829 rtIDs.fRTFBOID = info.fFBOID;
830 rtIDs.fMSColorRenderbufferID = 0;
831 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
832 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000833
Greg Daniel6fa62e22019-08-07 15:52:37 -0400834 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700835
Brian Salomona56a7462020-02-07 14:17:25 -0500836 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400837 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000838}
839
Greg Daniel7ef28f32017-04-20 16:41:55 +0000840sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400841 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400842 GrColorType colorType) {
843 GrGLTexture::Desc desc;
844 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
845 // with a texture rectangle,we assume the necessary support exists.
846 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800847 return nullptr;
848 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400849
850 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
851 return nullptr;
852 }
853
854 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400855 GrGLRenderTarget::IDs rtIDs;
856 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800857 return nullptr;
858 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500859 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800860}
861
Brian Salomonc320b152018-02-20 14:05:36 -0500862static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700863 if (!glTex) {
864 return false;
865 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000866
bsalomone5286e02016-01-14 09:24:09 -0800867 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
868 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800869 return false;
870 }
871
jvanverth17aa0472016-01-05 10:41:27 -0800872 return true;
873}
874
Brian Salomona9b04b92018-06-01 15:04:28 -0400875bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400876 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400877 const GrMipLevel texels[], int mipLevelCount,
878 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500879 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800880
Brian Salomonc320b152018-02-20 14:05:36 -0500881 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800882 return false;
883 }
884
Brian Salomond978b902019-02-07 15:09:18 -0500885 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000886
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400887 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400888 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400889 glTex->target(), left, top, width, height, srcColorType, texels,
890 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000891}
892
Brian Salomone05ba5a2019-04-08 11:59:07 -0400893bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400894 GrColorType textureColorType, GrColorType bufferColorType,
895 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897
Jim Van Verth1676cb92019-01-15 13:24:45 -0500898 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400899 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500900
Brian Salomonc320b152018-02-20 14:05:36 -0500901 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400902 return false;
903 }
904
Hal Canaryc36f7e72018-06-18 15:50:09 -0400905 static_assert(sizeof(int) == sizeof(int32_t), "");
906 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 return false;
908 }
909
Brian Salomond978b902019-02-07 15:09:18 -0500910 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911
912 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500913 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500915 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400916
Greg Daniel660cc992017-06-26 14:55:05 -0400917 SkDEBUGCODE(
918 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
919 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
920 SkASSERT(bounds.contains(subRect));
921 )
922
Brian Salomonb28cb682019-07-26 12:48:47 -0400923 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400925 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700926 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400927 return false;
928 }
929
930 bool restoreGLRowLength = false;
931 if (trimRowBytes != rowBytes) {
932 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400933 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400934 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
935 restoreGLRowLength = true;
936 }
937
Greg Danielba88ab62019-07-26 09:14:01 -0400938 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400939 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400940 GrGLenum externalFormat = 0;
941 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400942 this->glCaps().getTexSubImageExternalFormatAndType(
943 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400944 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400945 return false;
946 }
947
Brian Salomon8cbf6622019-07-30 11:03:14 -0400948 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400949 GL_CALL(TexSubImage2D(glTex->target(),
950 0,
951 left, top,
952 width,
953 height,
954 externalFormat, externalType,
955 pixels));
956
957 if (restoreGLRowLength) {
958 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
959 }
960
961 return true;
962}
963
Brian Salomon26de56e2019-04-10 12:14:26 -0400964bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400965 GrColorType surfaceColorType, GrColorType dstColorType,
966 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400967 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
968 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400969 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400970 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
971 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400972}
973
Brian Osman9b560242017-09-05 15:34:52 -0400974void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500975 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
976 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
977 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
978 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400979 }
Brian Osman9b560242017-09-05 15:34:52 -0400980}
981
Brian Salomond2a8ae22019-09-10 16:03:59 -0400982bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
983 int texHeight, GrGLenum target, int left, int top, int width,
984 int height, GrColorType srcColorType, const GrMipLevel texels[],
985 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500986 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400987 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500988
Greg Daniel7bfc9132019-08-14 14:23:53 -0400989 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400990 SkDEBUGCODE(
991 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
992 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
993 SkASSERT(bounds.contains(subRect));
994 )
Robert Phillips590533f2017-07-11 14:22:35 -0400995 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400996 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800997
Brian Osman9b560242017-09-05 15:34:52 -0400998 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400999
cblume55f2d2d2016-02-26 13:20:48 -08001000 const GrGLInterface* interface = this->glInterface();
1001 const GrGLCaps& caps = this->glCaps();
1002
Brian Salomonf77c1462019-08-01 15:19:29 -04001003 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001004
1005 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001006 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001007 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001008
bsalomon5b30c6f2015-12-17 14:17:34 -08001009 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001010 GrGLenum externalFormat;
1011 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001012 this->glCaps().getTexSubImageExternalFormatAndType(
1013 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001014 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001015 return false;
1016 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001017
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001019 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001020 * because our srcData has extra bytes past each row. If so, we need
1021 * to trim those off here, since GL ES may not let us specify
1022 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001023 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001024 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001025
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001026 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001027 *mipMapsStatus = (mipLevelCount > 1) ?
1028 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001029 }
bsalomone699d0c2016-03-09 06:25:15 -08001030
Brian Salomond2a8ae22019-09-10 16:03:59 -04001031 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001032
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1034 if (!texels[currentMipLevel].fPixels) {
1035 if (mipMapsStatus) {
1036 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001037 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001038 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001039 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001040 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001041 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1042 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 const size_t trimRowBytes = currentWidth * bpp;
1044 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1045
1046 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1047 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1048 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1049 restoreGLRowLength = true;
1050 }
1051
1052 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1053 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001055 if (restoreGLRowLength) {
1056 SkASSERT(caps.writePixelsRowBytesSupport());
1057 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1058 }
1059 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001060}
1061
Greg Daniel7bfc9132019-08-14 14:23:53 -04001062bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001063 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001064 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001066 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001067 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001068 const GrGLCaps& caps = this->glCaps();
1069
1070 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001071 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001073 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001074 }
1075
Robert Phillips9f744f72019-12-19 19:14:33 -05001076 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1077 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1078
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001081 int numMipLevels = 1;
1082 if (mipMapped == GrMipMapped::kYes) {
1083 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1084 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001085
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001086 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001088
1089 if (useTexStorage) {
1090 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001091 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001092 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001093 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001094 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1095 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001096 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001097 }
Robert Phillips42716d42019-12-16 12:19:54 -05001098
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 size_t offset = 0;
1100 for (int level = 0; level < numMipLevels; ++level) {
1101
Robert Phillips99dead92020-01-27 16:11:57 -05001102 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1103 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001104
1105 GL_CALL(CompressedTexSubImage2D(target,
1106 level,
1107 0, // left
1108 0, // top
1109 dimensions.width(),
1110 dimensions.height(),
1111 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001112 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001113 &((char*)data)[offset]));
1114
1115 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1116 if (error != GR_GL_NO_ERROR) {
1117 return false;
1118 }
1119
Robert Phillips9f744f72019-12-19 19:14:33 -05001120 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001121 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001122 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001123 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001124 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001125
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001126 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001127 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1128 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129
1130 const char* rawLevelData = &((char*)data)[offset];
1131 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1132 level,
1133 internalFormat,
1134 dimensions.width(),
1135 dimensions.height(),
1136 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001137 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001138 rawLevelData));
1139
1140 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1141 if (error != GR_GL_NO_ERROR) {
1142 return false;
1143 }
1144
Robert Phillips9f744f72019-12-19 19:14:33 -05001145 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001146 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001147 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001148 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001149 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001150}
1151
bsalomon424cc262015-05-22 10:37:30 -07001152static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001153 int sampleCount,
1154 GrGLenum format,
1155 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001156 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001157 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001158 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001159 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001160 GL_ALLOC_CALL(ctx.interface(),
1161 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1162 sampleCount,
1163 format,
1164 width, height));
1165 break;
1166 case GrGLCaps::kES_Apple_MSFBOType:
1167 GL_ALLOC_CALL(ctx.interface(),
1168 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1169 sampleCount,
1170 format,
1171 width, height));
1172 break;
1173 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1174 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1175 GL_ALLOC_CALL(ctx.interface(),
1176 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1177 sampleCount,
1178 format,
1179 width, height));
1180 break;
1181 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001182 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001183 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001184 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001185 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186}
1187
Brian Salomonea4ad302019-08-07 13:04:55 -04001188bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001189 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 GrGLRenderTarget::IDs* rtIDs) {
1191 rtIDs->fMSColorRenderbufferID = 0;
1192 rtIDs->fRTFBOID = 0;
1193 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1194 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195
bsalomona11e5fc2015-12-18 07:59:41 -08001196 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001197
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001199 goto FAILED;
1200 }
1201
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001202 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001203 goto FAILED;
1204 }
1205
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1207 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 goto FAILED;
1209 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001210
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001211 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1212 // the texture bound to the other. The exception is the IMG multisample extension. With this
1213 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1214 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001215 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001216 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1217 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1218 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 goto FAILED;
1220 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001221 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
1225
egdanield803f272015-03-18 13:01:52 -07001226 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001227 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001229 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001231 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001232 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 goto FAILED;
1234 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001237 GR_GL_COLOR_ATTACHMENT0,
1238 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001239 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001243 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1245 GR_GL_COLOR_ATTACHMENT0,
1246 desc.fTarget,
1247 desc.fID,
1248 0,
1249 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001250 } else {
egdanield803f272015-03-18 13:01:52 -07001251 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001252 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 desc.fTarget,
1254 desc.fID,
1255 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001256 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257
1258 return true;
1259
1260FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001261 if (rtIDs->fMSColorRenderbufferID) {
1262 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1265 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001267 if (rtIDs->fTexFBOID) {
1268 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 }
1270 return false;
1271}
1272
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001273// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001274static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001275// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001276 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001277}
1278
Brian Salomone2826ab2019-06-04 15:58:31 -04001279static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001280 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001281 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1282 // drivers have a bug where an FBO won't be complete if it includes a
1283 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001284 GrGLTextureParameters::SamplerOverriddenState state;
1285 state.fMinFilter = GR_GL_NEAREST;
1286 state.fMagFilter = GR_GL_NEAREST;
1287 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1288 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001289 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1290 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001293 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001294}
1295
Brian Salomona56a7462020-02-07 14:17:25 -05001296sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001297 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001298 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001299 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001300 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001301 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001302 int mipLevelCount,
1303 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001304 // We don't support protected textures in GL.
1305 if (isProtected == GrProtected::kYes) {
1306 return nullptr;
1307 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001308 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001309
Brian Salomond2a8ae22019-09-10 16:03:59 -04001310 SkASSERT(mipLevelCount > 0);
1311 GrMipMapsStatus mipMapsStatus =
1312 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001313 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001314 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001315 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001317 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001318 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001319 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1320 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001321
Brian Salomona56a7462020-02-07 14:17:25 -05001322 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1323 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001324
1325 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001326 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001327 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001328
Robert Phillips67d52cf2017-06-05 13:38:13 -04001329 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001330 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001331 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001332 GL_CALL(BindTexture(texDesc.fTarget, 0));
1333 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001334
Brian Salomonea4ad302019-08-07 13:04:55 -04001335 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1336 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 return return_null_texture();
1338 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001339 tex = sk_make_sp<GrGLTextureRenderTarget>(
1340 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001341 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001342 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001343 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001345 // The non-sampler params are still at their default values.
1346 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1347 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001348 if (levelClearMask) {
1349 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001350 GrColorType colorType;
1351 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1352 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001353 if (this->glCaps().clearTextureSupport()) {
1354 for (int i = 0; i < mipLevelCount; ++i) {
1355 if (levelClearMask & (1U << i)) {
1356 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1357 nullptr));
1358 }
1359 }
1360 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1361 !this->glCaps().performColorClearsAsDraws()) {
1362 this->disableScissor();
1363 this->disableWindowRectangles();
1364 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001365 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001366 for (int i = 0; i < mipLevelCount; ++i) {
1367 if (levelClearMask & (1U << i)) {
1368 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1369 kDst_TempFBOTarget);
1370 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1371 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1372 }
1373 }
Brian Salomonc2249852019-09-16 11:08:50 -04001374 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001375 } else {
1376 std::unique_ptr<char[]> zeros;
1377 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1378 for (int i = 0; i < mipLevelCount; ++i) {
1379 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001380 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1381 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001382 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1383 // smaller levels that need clearing.
1384 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001385 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001386 size_t size = levelWidth * levelHeight * bpp;
1387 zeros.reset(new char[size]());
1388 }
1389 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1390 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1391 externalFormat, externalType, zeros.get()));
1392 }
Brian Salomona3e29962019-07-16 11:52:08 -04001393 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001394 }
1395 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001396 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001397}
1398
Robert Phillips9f744f72019-12-19 19:14:33 -05001399sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001400 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001401 SkBudgeted budgeted,
1402 GrMipMapped mipMapped,
1403 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001404 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001405 // We don't support protected textures in GL.
1406 if (isProtected == GrProtected::kYes) {
1407 return nullptr;
1408 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001409 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001410 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001411 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001412 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001413 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001414 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001415 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001416 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001417 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001418 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001419 return nullptr;
1420 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001421
Robert Phillipsee946932019-12-18 11:16:17 -05001422 // Unbind this texture from the scratch texture unit.
1423 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1424
Robert Phillipse4720c62020-01-14 14:33:24 -05001425 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1426 ? GrMipMapsStatus::kValid
1427 : GrMipMapsStatus::kNotAllocated;
1428
1429 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001430 // The non-sampler params are still at their default values.
1431 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1432 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001433 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001434}
1435
Robert Phillipsb915c942019-12-17 14:44:37 -05001436GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1437 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001438 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001439 GrProtected isProtected,
1440 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001441 // We don't support protected textures in GL.
1442 if (isProtected == GrProtected::kYes) {
1443 return {};
1444 }
1445
1446 this->handleDirtyContext();
1447
1448 GrGLFormat glFormat = format.asGLFormat();
1449 if (glFormat == GrGLFormat::kUnknown) {
1450 return {};
1451 }
1452
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001453 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001454 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001455 SkAutoMalloc am;
1456
1457 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1458 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1459 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001460 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001461 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001462 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1463 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001464
Robert Phillips99dead92020-01-27 16:11:57 -05001465 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1466 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001467
1468 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001469
1470 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1471
1472 rawData = (const char*) am.get();
1473 }
1474
1475 GrGLTextureInfo info;
1476 GrGLTextureParameters::SamplerOverriddenState initialState;
1477
1478 info.fTarget = GR_GL_TEXTURE_2D;
1479 info.fFormat = GrGLFormatToEnum(glFormat);
1480 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001481 mipMapped, &initialState,
1482 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001483 if (!info.fID) {
1484 return {};
1485 }
1486
1487 // Unbind this texture from the scratch texture unit.
1488 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1489
1490 auto parameters = sk_make_sp<GrGLTextureParameters>();
1491 // The non-sampler params are still at their default values.
1492 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1493 fResetTimestampForTextureParameters);
1494
1495 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1496 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001497}
1498
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001499namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001500
egdaniel8dc7c3a2015-04-16 11:22:42 -07001501const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001503void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001504 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001505
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001506 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001507 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001508 (kUnknownBitCount == format->fTotalBits));
1509 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001510 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1512 (GrGLint*)&format->fStencilBits);
1513 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001514 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001515 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1516 (GrGLint*)&format->fTotalBits);
1517 format->fTotalBits += format->fStencilBits;
1518 } else {
1519 format->fTotalBits = format->fStencilBits;
1520 }
1521 }
1522}
1523}
1524
Brian Salomon5043f1f2019-07-11 21:27:54 -04001525int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001526 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001527 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1528
1529 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001530 // Default to unsupported, set this if we find a stencil format that works.
1531 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001532
Brian Salomond2a8ae22019-09-10 16:03:59 -04001533 GrGLuint colorID =
1534 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1535 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001536 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001537 }
egdanielff1d5472015-09-10 08:37:20 -07001538 // unbind the texture from the texture unit before binding it to the frame buffer
1539 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1540
1541 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001542 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001543 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001544 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001545 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001546 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1547 GR_GL_COLOR_ATTACHMENT0,
1548 GR_GL_TEXTURE_2D,
1549 colorID,
1550 0));
bsalomon30447372015-12-21 09:03:05 -08001551 GrGLuint sbRBID = 0;
1552 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001553
1554 // look over formats till I find a compatible one
1555 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001556 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001557 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001558 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1559 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1560 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1561 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1562 sFmt.fInternalFormat,
1563 kSize, kSize));
1564 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001565 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001566 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001567 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001568 if (sFmt.fPacked) {
1569 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1570 GR_GL_DEPTH_ATTACHMENT,
1571 GR_GL_RENDERBUFFER, sbRBID));
1572 } else {
1573 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1574 GR_GL_DEPTH_ATTACHMENT,
1575 GR_GL_RENDERBUFFER, 0));
1576 }
1577 GrGLenum status;
1578 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1579 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1580 firstWorkingStencilFormatIndex = i;
1581 break;
1582 }
egdanielff1d5472015-09-10 08:37:20 -07001583 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1584 GR_GL_STENCIL_ATTACHMENT,
1585 GR_GL_RENDERBUFFER, 0));
1586 if (sFmt.fPacked) {
1587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1588 GR_GL_DEPTH_ATTACHMENT,
1589 GR_GL_RENDERBUFFER, 0));
1590 }
egdanielff1d5472015-09-10 08:37:20 -07001591 }
1592 }
bsalomon30447372015-12-21 09:03:05 -08001593 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001596 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1597 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001598 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001599 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001600 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001601}
1602
Brian Salomonea4ad302019-08-07 13:04:55 -04001603GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001604 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001605 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001606 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001607 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001608 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001609 if (format == GrGLFormat::kUnknown) {
1610 return 0;
1611 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001612 GrGLuint id = 0;
1613 GL_CALL(GenTextures(1, &id));
1614 if (!id) {
1615 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001616 }
1617
Brian Salomonea4ad302019-08-07 13:04:55 -04001618 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001619
Brian Salomonea4ad302019-08-07 13:04:55 -04001620 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1621
Robert Phillips42716d42019-12-16 12:19:54 -05001622 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001623 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1624 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001625 GL_CALL(DeleteTextures(1, &id));
1626 return 0;
1627 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001628 }
Robert Phillips42716d42019-12-16 12:19:54 -05001629
Brian Salomonea4ad302019-08-07 13:04:55 -04001630 return id;
1631}
1632
Robert Phillips2716daf2020-01-23 12:53:42 -05001633GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001634 GrGLFormat format,
1635 GrRenderable renderable,
1636 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001637 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001638 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001639 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001640
Brian Salomonea4ad302019-08-07 13:04:55 -04001641 GrGLuint id = 0;
1642 GL_CALL(GenTextures(1, &id));
1643
1644 if (!id) {
1645 return 0;
1646 }
1647
1648 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001649
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001650 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001651 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001652 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001653 }
1654
Brian Salomond2a8ae22019-09-10 16:03:59 -04001655 if (initialState) {
1656 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1657 } else {
1658 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001659 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001660
1661 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1662
1663 bool success = false;
1664 if (internalFormat) {
1665 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1666 if (this->glCaps().formatSupportsTexStorage(format)) {
1667 GL_ALLOC_CALL(this->glInterface(),
Brian Osman788b9162020-02-07 10:36:46 -05001668 TexStorage2D(GR_GL_TEXTURE_2D, std::max(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001669 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001670 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1671 } else {
1672 GrGLenum externalFormat, externalType;
1673 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1674 GrGLenum error = GR_GL_NO_ERROR;
1675 if (externalFormat && externalType) {
1676 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1677 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001678 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1679 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001680 GL_ALLOC_CALL(
1681 this->glInterface(),
1682 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1683 currentHeight, 0, externalFormat, externalType, nullptr));
1684 error = CHECK_ALLOC_ERROR(this->glInterface());
1685 }
1686 success = (GR_GL_NO_ERROR == error);
1687 }
1688 }
1689 }
1690 if (success) {
1691 return id;
1692 }
1693 GL_CALL(DeleteTextures(1, &id));
1694 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001695}
1696
Chris Daltoneffee202019-07-01 22:28:03 -06001697GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1698 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001699 SkASSERT(width >= rt->width());
1700 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001701
egdaniel8dc7c3a2015-04-16 11:22:42 -07001702 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001703
Brian Salomond4764a12019-08-08 12:08:24 -04001704 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001705 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001706 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001707 }
egdanielff1d5472015-09-10 08:37:20 -07001708
1709 if (!sbDesc.fRenderbufferID) {
1710 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1711 }
1712 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001713 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001714 }
1715 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1716 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1717 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1718 // we do this "if" so that we don't call the multisample
1719 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001720 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001721 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001722 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001723 sFmt.fInternalFormat,
1724 width, height));
1725 } else {
1726 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1727 sFmt.fInternalFormat,
1728 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001729 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001730 }
1731 fStats.incStencilAttachmentCreates();
1732 // After sized formats we attempt an unsized format and take
1733 // whatever sizes GL gives us. In that case we query for the size.
1734 GrGLStencilAttachment::Format format = sFmt;
1735 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001736 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1737 sbDesc,
1738 width,
1739 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001740 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001741 format);
1742 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001743}
1744
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001745////////////////////////////////////////////////////////////////////////////////
1746
Brian Salomondbf70722019-02-07 11:31:24 -05001747sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1748 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001749 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001750}
1751
Greg Danielacd66b42019-05-22 16:29:12 -04001752void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001753 GrSurfaceOrigin rtOrigin) {
1754 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001755 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Chris Daltondc2b15e2020-02-19 11:45:21 -07001756 if (fHWScissorSettings.fRect != scissor) {
1757 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
1758 fHWScissorSettings.fRect = scissor;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001759 }
Chris Daltondc2b15e2020-02-19 11:45:21 -07001760 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1761 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1762 fHWScissorSettings.fEnabled = kYes_TriState;
1763 }
1764 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001766
1767 // See fall through note above
1768 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001769}
1770
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001771void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001772 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001773#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001774 typedef GrWindowRectsState::Mode Mode;
1775 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1776 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001777
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001778 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001779 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001780 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001781 }
1782
csmartdalton7535f412016-08-23 06:51:00 -07001783 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1784 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001785 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001786 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001787
Chris Daltond6cda8d2019-09-05 02:30:04 -06001788 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001789 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001790 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001791 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001792 }
1793
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001794 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001795 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001796
Greg Danielacd66b42019-05-22 16:29:12 -04001797 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001798#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001799}
1800
1801void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001803 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001804 return;
1805 }
1806 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001807 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001808#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001809}
1810
Robert Phillipsfcaae482019-11-07 10:17:03 -05001811bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001812 this->handleDirtyContext();
1813
1814 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1815 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1816 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001817
Stephen Whiteb1857852020-02-07 15:33:23 +00001818 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001819 if (!program) {
1820 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1821 return false;
1822 }
brianosman33f6b3f2016-06-02 05:49:21 -07001823
Brian Salomon802cb312018-06-08 18:05:20 -04001824 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001825
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001826 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001827 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1828 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001829
Chris Dalton3ffa7af2020-02-20 16:31:47 -07001830 fHWProgram->updateUniforms(renderTarget, programInfo);
1831 if (!programInfo.hasDynamicPrimProcTextures()) {
1832 auto* primProcTextures = (programInfo.hasFixedPrimProcTextures())
1833 ? programInfo.fixedPrimProcTextures() : nullptr;
1834 fHWProgram->bindTextures(programInfo.primProc(), programInfo.pipeline(), primProcTextures);
1835 }
bsalomon1f78c0a2014-12-17 09:43:13 -08001836
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001837 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001838 GrStencilSettings stencil;
1839 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001840 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1841 stencil.reset(*programInfo.pipeline().getUserStencil(),
1842 programInfo.pipeline().hasStencilClip(),
1843 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001844 }
Robert Phillips901aff02019-10-08 12:32:56 -04001845 this->flushStencil(stencil, programInfo.origin());
1846 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001847 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001848 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1849 : kBogusScissor);
1850 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001851 } else {
1852 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001853 }
Robert Phillips901aff02019-10-08 12:32:56 -04001854 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1855 glRT, programInfo.origin());
1856 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001857 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001858 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001859
1860 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001861 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001862 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001863
1864 return true;
1865}
1866
Brian Salomon802cb312018-06-08 18:05:20 -04001867void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1868 if (!program) {
1869 fHWProgram.reset();
1870 fHWProgramID = 0;
1871 return;
1872 }
1873 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1874 if (program == fHWProgram) {
1875 return;
1876 }
1877 auto id = program->programID();
1878 SkASSERT(id);
1879 GL_CALL(UseProgram(id));
1880 fHWProgram = std::move(program);
1881 fHWProgramID = id;
1882}
1883
1884void GrGLGpu::flushProgram(GrGLuint id) {
1885 SkASSERT(id);
1886 if (fHWProgramID == id) {
1887 SkASSERT(!fHWProgram);
1888 return;
1889 }
1890 fHWProgram.reset();
1891 GL_CALL(UseProgram(id));
1892 fHWProgramID = id;
1893}
1894
1895void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001896 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001897 int baseVertex,
1898 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001899 int baseInstance,
1900 GrPrimitiveRestart enablePrimitiveRestart) {
1901 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001902
cdaltone2e71c22016-04-07 18:13:29 -07001903 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001904 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001905 SkASSERT(indexBuffer->isCpuBuffer() ||
1906 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001907 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001908 } else {
1909 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001910 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001911
Brian Salomon92be2f72018-06-19 14:33:47 -04001912 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1913 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001914
Brian Salomon802cb312018-06-08 18:05:20 -04001915 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001916 SkASSERT(vertexBuffer);
1917 SkASSERT(vertexBuffer->isCpuBuffer() ||
1918 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1919 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001920 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1921 const auto& attrib = fHWProgram->vertexAttribute(i);
1922 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001923 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1924 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001925 }
Chris Dalton1d616352017-05-31 12:51:23 -06001926 }
Brian Salomon802cb312018-06-08 18:05:20 -04001927 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001928 SkASSERT(instanceBuffer);
1929 SkASSERT(instanceBuffer->isCpuBuffer() ||
1930 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1931 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001932 int attribIdx = fHWProgram->numVertexAttributes();
1933 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1934 const auto& attrib = fHWProgram->instanceAttribute(i);
1935 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001936 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1937 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1938 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001939 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001940 }
1941}
1942
Brian Salomonae64c192019-02-05 09:41:37 -05001943GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001944 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001945
cdaltone2e71c22016-04-07 18:13:29 -07001946 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001947 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001948 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001949 }
cdaltone2e71c22016-04-07 18:13:29 -07001950
Brian Salomonae64c192019-02-05 09:41:37 -05001951 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001952 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001953 if (!bufferState->fBufferZeroKnownBound) {
1954 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1955 bufferState->fBufferZeroKnownBound = true;
1956 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001957 }
Brian Salomondbf70722019-02-07 11:31:24 -05001958 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1959 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001960 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1961 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1962 bufferState->fBufferZeroKnownBound = false;
1963 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001964 }
1965
Brian Salomonae64c192019-02-05 09:41:37 -05001966 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001967}
Brian Salomond818ebf2018-07-02 14:08:49 +00001968void GrGLGpu::disableScissor() {
1969 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1970 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1971 fHWScissorSettings.fEnabled = kNo_TriState;
1972 return;
1973 }
1974}
1975
Brian Osman9a9baae2018-11-05 15:06:26 -05001976void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001977 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001978 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001979 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001980 SkASSERT(!this->caps()->performColorClearsAsDraws());
1981 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001982
Brian Salomon43f8bf02017-10-18 08:33:29 -04001983 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001984
Brian Salomon43f8bf02017-10-18 08:33:29 -04001985 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1986
Brian Salomond818ebf2018-07-02 14:08:49 +00001987 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001988 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001989 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001990 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001991 }
Greg Danielacd66b42019-05-22 16:29:12 -04001992 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001993 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001994 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001995 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001996 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001997}
1998
Robert Phillips95214472017-08-08 18:00:03 -04001999void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002000 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2001
Robert Phillips95214472017-08-08 18:00:03 -04002002 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002003 return;
2004 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002005
Chris Dalton674f77a2019-09-30 20:49:39 -06002006 // This should only be called internally when we know we have a stencil buffer.
2007 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002008
bsalomonb0bd4f62014-09-03 07:19:50 -07002009 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002010 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002011
Brian Salomond818ebf2018-07-02 14:08:49 +00002012 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002013 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002014
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002015 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002016 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002017 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002018 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002019}
2020
Chris Daltonb50cc812019-10-14 16:29:21 -06002021static bool use_tiled_rendering(const GrGLCaps& glCaps,
2022 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2023 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2024 // Otherwise it's faster to just not use it.
2025 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2026 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2027}
2028
2029void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002030 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2031 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2032 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2033
2034 this->handleDirtyContext();
2035
2036 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2037 this->flushRenderTarget(glRT);
2038 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2039
Chris Daltonb50cc812019-10-14 16:29:21 -06002040 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2041 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2042 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2043 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2044 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2045 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2046 nativeBounds.fHeight, preserveMask));
2047 }
2048
Chris Dalton674f77a2019-09-30 20:49:39 -06002049 GrGLbitfield clearMask = 0;
2050 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2051 SkASSERT(!this->caps()->performColorClearsAsDraws());
2052 this->flushClearColor(colorLoadStore.fClearColor);
2053 this->flushColorWrite(true);
2054 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002055 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002056 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2057 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2058 GL_CALL(StencilMask(0xffffffff));
2059 GL_CALL(ClearStencil(0));
2060 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2061 }
2062 if (clearMask) {
2063 this->disableScissor();
2064 this->disableWindowRectangles();
2065 GL_CALL(Clear(clearMask));
2066 }
2067}
2068
2069void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2070 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2071 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2072 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2073
2074 this->handleDirtyContext();
2075
2076 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2077 // The framebuffer binding changed in the middle of a command buffer. We should have already
2078 // printed a warning during onFBOChanged.
2079 return;
2080 }
2081
2082 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2083 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2084
2085 SkSTArray<2, GrGLenum> discardAttachments;
2086 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2087 discardAttachments.push_back(
2088 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2089 }
2090 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2091 discardAttachments.push_back(
2092 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2093 }
2094
2095 if (!discardAttachments.empty()) {
2096 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2097 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2098 discardAttachments.begin()));
2099 } else {
2100 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2101 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2102 discardAttachments.begin()));
2103 }
2104 }
2105 }
2106
Chris Daltonb50cc812019-10-14 16:29:21 -06002107 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2108 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2109 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2110 // Handled by use_tiled_rendering().
2111 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2112 GL_CALL(EndTiling(preserveMask));
2113 }
2114
Chris Dalton674f77a2019-09-30 20:49:39 -06002115 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002116}
2117
csmartdalton29df7602016-08-31 11:55:52 -07002118void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2119 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002120 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002121 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002122 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002123 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002124
egdaniel8dc7c3a2015-04-16 11:22:42 -07002125 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002126 if (!sb) {
2127 // We should only get here if we marked a proxy as requiring a SB. However,
2128 // the SB creation could later fail. Likely clipping is going to go awry now.
2129 return;
2130 }
2131
bsalomon6bc1b5f2015-02-23 09:06:38 -08002132 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002133#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002134 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002135 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002136#else
2137 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002138 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002139 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002140 // changing the clip between stencil passes may or may not
2141 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002142 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002143#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002144 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002145 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002146 value = (1 << (stencilBitCount - 1));
2147 } else {
2148 value = 0;
2149 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002150 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002151 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002152
Greg Danielacd66b42019-05-22 16:29:12 -04002153 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002154 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002155
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002156 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002157 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002158 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002159 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002160}
2161
Brian Salomone05ba5a2019-04-08 11:59:07 -04002162bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002163 GrColorType surfaceColorType, GrColorType dstColorType,
2164 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002165 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002166
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002167 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002168 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002169 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002170 return false;
2171 }
Greg Danielba88ab62019-07-26 09:14:01 -04002172 GrGLenum externalFormat = 0;
2173 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002174 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2175 surfaceColorType,
2176 dstColorType,
2177 &externalFormat,
2178 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002179 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002180 return false;
2181 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002182
Brian Salomon71d9d842016-11-03 13:42:00 -04002183 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002184 if (renderTarget->numSamples() <= 1 ||
2185 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2186 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2187 this->flushRenderTargetNoColorWrites(renderTarget);
2188 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2189 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2190 return false;
2191 } else {
2192 SkASSERT(renderTarget->requiresManualMSAAResolve());
2193 // we don't track the state of the READ FBO ID.
2194 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002195 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002196 } else {
2197 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002198 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002199 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002200 }
2201
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002202 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002203 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002204
Brian Salomona6948702018-06-01 15:33:20 -04002205 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002206 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002207 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002208 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002209 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002210 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002211
Brian Osman1348ed02018-09-12 09:08:39 -04002212 bool reattachStencil = false;
2213 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2214 renderTarget &&
2215 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002216 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002217 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2218 reattachStencil = true;
2219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2220 GR_GL_RENDERBUFFER, 0));
2221 }
2222
Chris Dalton76500e52019-09-05 02:13:05 -06002223 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002224 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002225
2226 if (reattachStencil) {
2227 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2228 renderTarget->renderTargetPriv().getStencilAttachment());
2229 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2230 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2231 }
2232
Brian Salomon26de56e2019-04-10 12:14:26 -04002233 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002234 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002235 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2236 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002237
Brian Salomone05ba5a2019-04-08 11:59:07 -04002238 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002239 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002240 }
2241 return true;
2242}
2243
2244bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002245 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2246 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002247 SkASSERT(surface);
2248
Brian Salomonb28cb682019-07-26 12:48:47 -04002249 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002250
Brian Salomon26de56e2019-04-10 12:14:26 -04002251 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2252 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002253
Brian Salomon1047a492019-07-02 12:25:21 -04002254 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002255 rowPixelWidth = width;
2256 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002257 SkASSERT(!(rowBytes % bytesPerPixel));
2258 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002259 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002260 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2261 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002262}
2263
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002264GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002265 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002266 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002267 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002268 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002269 if (!fCachedOpsRenderPass) {
2270 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002271 }
2272
Chris Daltonb50cc812019-10-14 16:29:21 -06002273 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002274 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002275}
2276
Brian Salomon1fabd512018-02-09 09:54:25 -05002277void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002278 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002279 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002280 this->didWriteToSurface(target, origin, &bounds);
2281}
bsalomon6ba6fa12015-03-04 11:57:37 -08002282
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002283void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2284 this->flushRenderTargetNoColorWrites(target);
2285 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002286}
2287
Brian Osman9aa30c62018-07-02 15:21:46 -04002288void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002289 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002290 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002291 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002292 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002293#ifdef SK_DEBUG
2294 // don't do this check in Chromium -- this is causing
2295 // lots of repeated command buffer flushes when the compositor is
2296 // rendering with Ganesh, which is really slow; even too slow for
2297 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002298 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002299 GrGLenum status;
2300 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2301 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2302 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2303 }
bsalomon160f24c2015-03-17 15:55:42 -07002304 }
egdanield803f272015-03-18 13:01:52 -07002305#endif
2306 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002307 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002308 }
2309
brianosman35b784d2016-05-05 11:52:53 -07002310 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002311 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002312 }
Brian Salomon9b553272020-01-24 14:38:37 -05002313
2314 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2315 GrGLint format;
2316 GrGLint type;
2317 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2318 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2319 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2320 }
bsalomon083617b2016-02-12 12:10:14 -08002321}
bsalomona9909122016-01-23 10:41:40 -08002322
brianosman33f6b3f2016-06-02 05:49:21 -07002323void GrGLGpu::flushFramebufferSRGB(bool enable) {
2324 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2325 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2326 fHWSRGBFramebuffer = kYes_TriState;
2327 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2328 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2329 fHWSRGBFramebuffer = kNo_TriState;
2330 }
2331}
2332
Greg Danielacd66b42019-05-22 16:29:12 -04002333void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002334 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002335 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002336 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002337 fHWViewport = viewport;
2338 }
2339}
2340
bsalomon@google.comd302f142011-03-03 13:54:13 +00002341#define SWAP_PER_DRAW 0
2342
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002343#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002344 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002345 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002346 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002347 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002348 void SwapBuf() {
2349 DWORD procID = GetCurrentProcessId();
2350 HWND hwnd = GetTopWindow(GetDesktopWindow());
2351 while(hwnd) {
2352 DWORD wndProcID = 0;
2353 GetWindowThreadProcessId(hwnd, &wndProcID);
2354 if(wndProcID == procID) {
2355 SwapBuffers(GetDC(hwnd));
2356 }
2357 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2358 }
2359 }
2360 #endif
2361#endif
2362
Chris Dalton4386ad12020-02-19 16:42:06 -07002363void GrGLGpu::drawMeshes(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo,
2364 const GrMesh meshes[], int meshCount) {
Robert Phillips2579de42019-10-09 09:51:59 -04002365 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002366
Robert Phillips901aff02019-10-08 12:32:56 -04002367 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2368 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2369
Brian Salomonf7232642018-09-19 08:58:08 -04002370 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002371 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2372 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002373 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002374 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002375
Robert Phillips901aff02019-10-08 12:32:56 -04002376 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002377 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002378 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2379 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002380 }
Robert Phillips901aff02019-10-08 12:32:56 -04002381 if (hasDynamicPrimProcTextures) {
Chris Dalton3ffa7af2020-02-20 16:31:47 -07002382 fHWProgram->bindTextures(programInfo.primProc(), programInfo.pipeline(),
2383 programInfo.dynamicPrimProcTextures(m));
Brian Salomonf7232642018-09-19 08:58:08 -04002384 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002385 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Chris Dalton79f336d2020-02-11 14:42:56 -07002386 GrIsPrimTypeLines(programInfo.primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002387 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2388 GL_CALL(Enable(GR_GL_CULL_FACE));
2389 GL_CALL(Disable(GR_GL_CULL_FACE));
2390 }
Chris Dalton79f336d2020-02-11 14:42:56 -07002391 meshes[m].sendToGpu(programInfo.primitiveType(), this);
2392 fLastPrimitiveType = programInfo.primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002393 }
ethannicholas22793252016-01-30 09:59:10 -08002394
bsalomon@google.comd302f142011-03-03 13:54:13 +00002395#if SWAP_PER_DRAW
2396 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002397 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002398 aglSwapBuffers(aglGetCurrentContext());
2399 int set_a_break_pt_here = 9;
2400 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002401 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002402 SwapBuf();
2403 int set_a_break_pt_here = 9;
2404 SwapBuf();
2405 #endif
2406#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002407}
2408
Chris Dalton3809bab2017-06-13 10:55:06 -06002409static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2410 switch (primitiveType) {
2411 case GrPrimitiveType::kTriangles:
2412 return GR_GL_TRIANGLES;
2413 case GrPrimitiveType::kTriangleStrip:
2414 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002415 case GrPrimitiveType::kPoints:
2416 return GR_GL_POINTS;
2417 case GrPrimitiveType::kLines:
2418 return GR_GL_LINES;
2419 case GrPrimitiveType::kLineStrip:
2420 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002421 case GrPrimitiveType::kPatches:
2422 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002423 case GrPrimitiveType::kPath:
2424 SK_ABORT("non-mesh-based GrPrimitiveType");
2425 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002426 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002427 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002428}
2429
Chris Dalton79f336d2020-02-11 14:42:56 -07002430void GrGLGpu::sendArrayMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh, int vertexCount,
2431 int baseVertex) {
2432 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002433 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002434 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2435 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002436 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2437 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002438 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002439 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2440 }
2441 fStats.incNumDraws();
2442}
2443
Brian Salomondbf70722019-02-07 11:31:24 -05002444static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2445 size_t baseOffset = baseIndex * sizeof(uint16_t);
2446 if (indexBuffer->isCpuBuffer()) {
2447 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2448 } else {
2449 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2450 }
2451}
2452
Chris Dalton79f336d2020-02-11 14:42:56 -07002453void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2454 int indexCount, int baseIndex, uint16_t minIndexValue,
2455 uint16_t maxIndexValue, int baseVertex) {
2456 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002457 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2458
2459 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2460 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002461
2462 if (this->glCaps().drawRangeElementsSupport()) {
2463 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002464 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002465 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002466 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002467 }
2468 fStats.incNumDraws();
2469}
2470
Chris Dalton79f336d2020-02-11 14:42:56 -07002471void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2472 int vertexCount, int baseVertex, int instanceCount,
2473 int baseInstance) {
2474 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002475 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002476 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002477 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2478 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002479 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
Brian Osman788b9162020-02-07 10:36:46 -05002480 std::min(instanceCount - i, maxInstances)));
Chris Daltoncc604e52017-10-06 16:27:32 -06002481 fStats.incNumDraws();
2482 }
Chris Dalton1d616352017-05-31 12:51:23 -06002483}
2484
Chris Dalton79f336d2020-02-11 14:42:56 -07002485void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2486 int indexCount, int baseIndex, int baseVertex,
2487 int instanceCount, int baseInstance) {
2488 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002489 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002490 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2491 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002492 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2493 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002494 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Brian Osman788b9162020-02-07 10:36:46 -05002495 std::min(instanceCount - i, maxInstances)));
Chris Dalton1b4ad762018-10-04 11:58:09 -06002496 fStats.incNumDraws();
2497 }
Chris Dalton1d616352017-05-31 12:51:23 -06002498}
2499
Chris Dalton16a33c62019-09-24 22:19:17 -06002500void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002501 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002502 // Some extensions automatically resolves the texture when it is read.
2503 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2504
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002505 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002506 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2507 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2508 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2509 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002510
Chris Daltonc139d082019-09-26 14:04:24 -06002511 // make sure we go through flushRenderTarget() since we've modified
2512 // the bound DRAW FBO ID.
2513 fHWBoundRenderTargetUniqueID.makeInvalid();
2514 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2515 // Apple's extension uses the scissor as the blit bounds.
2516 GrScissorState scissorState;
2517 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002518 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2519 // happens inside flushScissor since resolveRect is already in native device coordinates.
2520 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002521 this->disableWindowRectangles();
2522 GL_CALL(ResolveMultisampleFramebuffer());
2523 } else {
2524 int l, b, r, t;
2525 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2526 this->glCaps().blitFramebufferSupportFlags()) {
2527 l = 0;
2528 b = 0;
2529 r = target->width();
2530 t = target->height();
2531 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002532 l = resolveRect.x();
2533 b = resolveRect.y();
2534 r = resolveRect.x() + resolveRect.width();
2535 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002536 }
Chris Daltonc139d082019-09-26 14:04:24 -06002537
2538 // BlitFrameBuffer respects the scissor, so disable it.
2539 this->disableScissor();
2540 this->disableWindowRectangles();
2541 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00002542 }
2543}
2544
bsalomon@google.com411dad02012-06-05 20:24:20 +00002545namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002546
bsalomon@google.com411dad02012-06-05 20:24:20 +00002547
2548GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002549 static const GrGLenum gTable[kGrStencilOpCount] = {
2550 GR_GL_KEEP, // kKeep
2551 GR_GL_ZERO, // kZero
2552 GR_GL_REPLACE, // kReplace
2553 GR_GL_INVERT, // kInvert
2554 GR_GL_INCR_WRAP, // kIncWrap
2555 GR_GL_DECR_WRAP, // kDecWrap
2556 GR_GL_INCR, // kIncClamp
2557 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002558 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002559 static_assert(0 == (int)GrStencilOp::kKeep);
2560 static_assert(1 == (int)GrStencilOp::kZero);
2561 static_assert(2 == (int)GrStencilOp::kReplace);
2562 static_assert(3 == (int)GrStencilOp::kInvert);
2563 static_assert(4 == (int)GrStencilOp::kIncWrap);
2564 static_assert(5 == (int)GrStencilOp::kDecWrap);
2565 static_assert(6 == (int)GrStencilOp::kIncClamp);
2566 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002567 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2568 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002569}
2570
2571void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002572 const GrStencilSettings::Face& face,
2573 GrGLenum glFace) {
2574 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2575 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2576 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002577
cdalton93a379b2016-05-11 13:58:08 -07002578 GrGLint ref = face.fRef;
2579 GrGLint mask = face.fTestMask;
2580 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002581
2582 if (GR_GL_FRONT_AND_BACK == glFace) {
2583 // we call the combined func just in case separate stencil is not
2584 // supported.
2585 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2586 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002587 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002588 } else {
2589 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2590 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002591 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002592 }
2593}
2594}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002595
Chris Dalton71713f62019-04-18 12:41:03 -06002596void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002597 if (stencilSettings.isDisabled()) {
2598 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002599 } else if (fHWStencilSettings != stencilSettings ||
2600 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002601 if (kYes_TriState != fHWStencilTestEnabled) {
2602 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002603
csmartdaltonc7d85332016-10-26 10:13:46 -07002604 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002605 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002606 if (!stencilSettings.isTwoSided()) {
2607 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2608 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002609 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002610 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2611 GR_GL_FRONT);
2612 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2613 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002614 }
joshualitta58fe352014-10-27 08:39:00 -07002615 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002616 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002617 }
2618}
2619
csmartdaltonc7d85332016-10-26 10:13:46 -07002620void GrGLGpu::disableStencil() {
2621 if (kNo_TriState != fHWStencilTestEnabled) {
2622 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002623
csmartdaltonc7d85332016-10-26 10:13:46 -07002624 fHWStencilTestEnabled = kNo_TriState;
2625 fHWStencilSettings.invalidate();
2626 }
2627}
2628
Chris Dalton4c56b032019-03-04 12:28:42 -07002629void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002630 // rt is only optional if useHWAA is false.
2631 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002632#ifdef SK_DEBUG
2633 if (useHWAA && rt->numSamples() <= 1) {
2634 SkASSERT(this->caps()->mixedSamplesSupport());
2635 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2636 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2637 }
2638#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002639
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002640 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002641 if (useHWAA) {
2642 if (kYes_TriState != fMSAAEnabled) {
2643 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2644 fMSAAEnabled = kYes_TriState;
2645 }
2646 } else {
2647 if (kNo_TriState != fMSAAEnabled) {
2648 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2649 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002650 }
2651 }
2652 }
2653}
2654
Chris Daltonce425af2019-12-16 10:39:03 -07002655void GrGLGpu::flushConservativeRasterState(bool enabled) {
2656 if (this->caps()->conservativeRasterSupport()) {
2657 if (enabled) {
2658 if (kYes_TriState != fHWConservativeRasterEnabled) {
2659 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2660 fHWConservativeRasterEnabled = kYes_TriState;
2661 }
2662 } else {
2663 if (kNo_TriState != fHWConservativeRasterEnabled) {
2664 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2665 fHWConservativeRasterEnabled = kNo_TriState;
2666 }
2667 }
2668 }
2669}
2670
Chris Dalton1215cda2019-12-17 21:44:04 -07002671void GrGLGpu::flushWireframeState(bool enabled) {
2672 if (this->caps()->wireframeSupport()) {
2673 if (this->caps()->wireframeMode() || enabled) {
2674 if (kYes_TriState != fHWWireframeEnabled) {
2675 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2676 fHWWireframeEnabled = kYes_TriState;
2677 }
2678 } else {
2679 if (kNo_TriState != fHWWireframeEnabled) {
2680 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2681 fHWWireframeEnabled = kNo_TriState;
2682 }
2683 }
2684 }
2685}
2686
Chris Daltonbbb3f642019-07-24 12:25:08 -04002687void GrGLGpu::flushBlendAndColorWrite(
2688 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2689 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2690 // We need to work around a driver bug by using a blend state that preserves the dst color,
2691 // rather than disabling color writes.
2692 GrXferProcessor::BlendInfo preserveDstBlend;
2693 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2694 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2695 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2696 return;
2697 }
bsalomonf7cc8772015-05-11 11:21:14 -07002698
cdalton8917d622015-05-06 13:40:21 -07002699 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002700 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2701 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002702
2703 // Any optimization to disable blending should have already been applied and
2704 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002705 bool blendOff =
2706 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2707 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2708 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002709
egdanielb414f252014-07-29 13:15:47 -07002710 if (blendOff) {
2711 if (kNo_TriState != fHWBlendState.fEnabled) {
2712 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002713
2714 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2715 // https://code.google.com/p/skia/issues/detail?id=3943
2716 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2717 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2718 SkASSERT(this->caps()->advancedBlendEquationSupport());
2719 // Set to any basic blending equation.
2720 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2721 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2722 fHWBlendState.fEquation = blend_equation;
2723 }
2724
egdanielb414f252014-07-29 13:15:47 -07002725 fHWBlendState.fEnabled = kNo_TriState;
2726 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002727 } else {
2728 if (kYes_TriState != fHWBlendState.fEnabled) {
2729 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002730
Chris Daltonbbb3f642019-07-24 12:25:08 -04002731 fHWBlendState.fEnabled = kYes_TriState;
2732 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002733
Chris Daltonbbb3f642019-07-24 12:25:08 -04002734 if (fHWBlendState.fEquation != equation) {
2735 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2736 fHWBlendState.fEquation = equation;
2737 }
cdalton8917d622015-05-06 13:40:21 -07002738
Chris Daltonbbb3f642019-07-24 12:25:08 -04002739 if (GrBlendEquationIsAdvanced(equation)) {
2740 SkASSERT(this->caps()->advancedBlendEquationSupport());
2741 // Advanced equations have no other blend state.
2742 return;
2743 }
cdalton8917d622015-05-06 13:40:21 -07002744
Chris Daltonbbb3f642019-07-24 12:25:08 -04002745 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2746 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2747 gXfermodeCoeff2Blend[dstCoeff]));
2748 fHWBlendState.fSrcCoeff = srcCoeff;
2749 fHWBlendState.fDstCoeff = dstCoeff;
2750 }
cdalton8917d622015-05-06 13:40:21 -07002751
Chris Daltonbbb3f642019-07-24 12:25:08 -04002752 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2753 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2754 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2755 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2756 fHWBlendState.fConstColor = blendConst;
2757 fHWBlendState.fConstColorValid = true;
2758 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002759 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002760 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002761
2762 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002763}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002764
Brian Salomondc829942018-10-23 16:07:24 -04002765static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002766 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002767 switch (swizzle[i]) {
2768 case 'r': glValues[i] = GR_GL_RED; break;
2769 case 'g': glValues[i] = GR_GL_GREEN; break;
2770 case 'b': glValues[i] = GR_GL_BLUE; break;
2771 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002772 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002773 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002774 default: SK_ABORT("Unsupported component");
2775 }
Brian Salomon327955b2018-10-22 15:39:22 +00002776 }
2777}
2778
Greg Daniel2c3398d2019-06-19 11:58:01 -04002779void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2780 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002781 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002782
reed856e9d92015-09-30 12:21:45 -07002783#ifdef SK_DEBUG
2784 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002785 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002786 const int w = texture->width();
2787 const int h = texture->height();
2788 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2789 }
2790 }
2791#endif
2792
Robert Phillips294870f2016-11-11 12:38:40 -05002793 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002794 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002795 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002796 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002797 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002798 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002799 }
2800
Brian Salomondc829942018-10-23 16:07:24 -04002801 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002802 if (!this->caps()->mipMapSupport() ||
2803 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002804 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002805 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002806 }
bsalomonefd7d452014-10-23 14:17:46 -07002807
brianosman33f6b3f2016-06-02 05:49:21 -07002808#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002809 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002810 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002811 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002812 }
2813#endif
2814
Brian Salomone2826ab2019-06-04 15:58:31 -04002815 auto timestamp = texture->parameters()->resetTimestamp();
2816 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002817
Brian Salomone2826ab2019-06-04 15:58:31 -04002818 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2819 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002820 if (fSamplerObjectCache) {
2821 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2822 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002823 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2824 texture->parameters()->samplerOverriddenState();
2825 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002826
Brian Salomone2826ab2019-06-04 15:58:31 -04002827 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2828 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002829
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2831 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002832
2833 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002834 newSamplerState.fMinLOD = -1000.f;
2835 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002836
Brian Salomone2826ab2019-06-04 15:58:31 -04002837 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002838 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002839 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002840 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002841 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002842 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002843 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002844 }
Mike Klein1bccae52018-10-19 11:38:44 +00002845 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002847 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002849 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002850 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002851 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002852 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002853 }
2854 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002856 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002858 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002859 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002860 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002861 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002862 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002863 if (this->glCaps().clampToBorderSupport()) {
2864 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002865 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002866 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002867 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2868 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002869 }
2870 }
Brian Salomondc829942018-10-23 16:07:24 -04002871 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002872 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2873 newNonsamplerState.fBaseMipMapLevel = 0;
2874 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002875
Brian Salomone2826ab2019-06-04 15:58:31 -04002876 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2877 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002878 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002879 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2880 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002881 GrGLenum glValues[4];
2882 get_gl_swizzle_values(swizzle, glValues);
2883 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002884 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002885 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002886 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2887 reinterpret_cast<const GrGLint*>(glValues)));
2888 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002889 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2890 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2891 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2892 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2893 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002894 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002895 }
2896 }
Brian Salomondc829942018-10-23 16:07:24 -04002897 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002898 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2899 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2900 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002901 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002902 this->setTextureUnit(unitIdx);
2903 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002904 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002905 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002906 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002907 this->setTextureUnit(unitIdx);
2908 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002909 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002910 }
Brian Salomon327955b2018-10-22 15:39:22 +00002911 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002912 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2913 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002914}
2915
Brian Salomon1f05d452019-02-08 12:33:08 -05002916void GrGLGpu::onResetTextureBindings() {
2917 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2918 GR_GL_TEXTURE_EXTERNAL};
2919 for (int i = 0; i < this->numTextureUnits(); ++i) {
2920 this->setTextureUnit(i);
2921 for (auto target : kTargets) {
2922 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2923 GL_CALL(BindTexture(target, 0));
2924 }
2925 }
2926 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2927 }
2928}
2929
Chris Dalton5a2f9622019-12-27 14:56:38 -07002930void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2931 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2932 if (fHWPatchVertexCount != count) {
2933 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2934 fHWPatchVertexCount = count;
2935 }
2936}
2937
egdaniel080e6732014-12-22 07:35:52 -08002938void GrGLGpu::flushColorWrite(bool writeColor) {
2939 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002940 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002941 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2942 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002943 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002944 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002945 } else {
2946 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002947 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002948 fHWWriteToColor = kYes_TriState;
2949 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002950 }
bsalomon3e791242014-12-17 13:43:13 -08002951}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002952
Chris Dalton674f77a2019-09-30 20:49:39 -06002953void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2954 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2955 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2956 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2957 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2958 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2959 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2960 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002961 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2962 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2963 GL_CALL(ClearColor(r, g, b, a));
2964 fHWClearColor[0] = r;
2965 fHWClearColor[1] = g;
2966 fHWClearColor[2] = b;
2967 fHWClearColor[3] = a;
2968 }
2969}
2970
bsalomon861e1032014-12-16 07:33:49 -08002971void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002972 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002973 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002974 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002975 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002976 }
2977}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002978
Brian Salomond978b902019-02-07 15:09:18 -05002979void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002980 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002981 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002982 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2983 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2984 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002985 }
Brian Salomond978b902019-02-07 15:09:18 -05002986 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2987 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002988 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002989 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002990}
2991
Brian Salomone5e7eb12016-10-14 16:18:33 -04002992// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002993static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2994 const GrSurface* src,
2995 const SkIRect& srcRect,
2996 const SkIPoint& dstPoint,
2997 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002998 int dstSampleCnt = 0;
2999 int srcSampleCnt = 0;
3000 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003001 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003002 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003003 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003004 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003005 }
3006 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3007 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3008
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003009 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3010 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3011
Brian Salomone5e7eb12016-10-14 16:18:33 -04003012 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003013 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003014
Greg Daniel46cfbc62019-06-07 11:43:30 -04003015 GrTextureType dstTexType;
3016 GrTextureType* dstTexTypePtr = nullptr;
3017 GrTextureType srcTexType;
3018 GrTextureType* srcTexTypePtr = nullptr;
3019 if (dstTex) {
3020 dstTexType = dstTex->texturePriv().textureType();
3021 dstTexTypePtr = &dstTexType;
3022 }
3023 if (srcTex) {
3024 srcTexType = srcTex->texturePriv().textureType();
3025 srcTexTypePtr = &srcTexType;
3026 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003027
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003028 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3029 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003030 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003031}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003032
Brian Osmana9c8a052018-01-19 10:31:56 -05003033static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3034 // A RT has a separate MSAA renderbuffer if:
3035 // 1) It's multisampled
3036 // 2) We're using an extension with separate MSAA renderbuffers
3037 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003038 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003039}
3040
Greg Daniel46cfbc62019-06-07 11:43:30 -04003041static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003042 const GrGLCaps& caps) {
3043
bsalomon@google.comeb851172013-04-15 13:51:00 +00003044 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003045 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003046 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003047 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003048
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003049 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3050 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3051
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003052 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3053 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3054
Greg Daniel46cfbc62019-06-07 11:43:30 -04003055 GrTextureType dstTexType;
3056 GrTextureType* dstTexTypePtr = nullptr;
3057 GrTextureType srcTexType;
3058 GrTextureType* srcTexTypePtr = nullptr;
3059 if (dstTex) {
3060 dstTexType = dstTex->texturePriv().textureType();
3061 dstTexTypePtr = &dstTexType;
3062 }
3063 if (srcTex) {
3064 srcTexType = srcTex->texturePriv().textureType();
3065 srcTexTypePtr = &srcTexType;
3066 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003067
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003068 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3069 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003070}
3071
Greg Danielacd66b42019-05-22 16:29:12 -04003072// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003073void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003074 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003075 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003076 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003077 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003078 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3079 GrGLuint texID = texture->textureID();
3080 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003081 GrGLuint* tempFBOID;
3082 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003083
egdanield803f272015-03-18 13:01:52 -07003084 if (0 == *tempFBOID) {
3085 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003086 }
3087
Adrienne Walker4ee88512018-05-17 11:37:14 -07003088 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003089 GR_GL_CALL(
3090 this->glInterface(),
3091 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3092 if (mipLevel == 0) {
3093 texture->baseLevelWasBoundToFBO();
3094 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003095 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003096 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003097 }
egdaniel0f5f9672015-02-03 11:10:51 -08003098}
3099
Brian Salomond2a8ae22019-09-10 16:03:59 -04003100void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003101 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003102 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003103 SkASSERT(surface->asTexture());
3104 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3105 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3106 GR_GL_COLOR_ATTACHMENT0,
3107 textureTarget,
3108 0,
3109 0));
3110 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003111}
3112
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003113void GrGLGpu::onFBOChanged() {
3114 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3115 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3116 GL_CALL(Flush());
3117 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003118#ifdef SK_DEBUG
3119 if (fIsExecutingCommandBuffer_DebugOnly) {
3120 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3121 "This will severely hurt performance.\n");
3122 }
3123#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003124}
3125
Adrienne Walker4ee88512018-05-17 11:37:14 -07003126void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3127 fStats.incRenderTargetBinds();
3128 GL_CALL(BindFramebuffer(target, fboid));
3129 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3130 fBoundDrawFramebuffer = fboid;
3131 }
3132
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003133 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3134 // The driver forgets the correct scissor when modifying the FBO binding.
3135 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003136 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003137 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003138 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003139 }
3140
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003141 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003142}
3143
Adrienne Walker4ee88512018-05-17 11:37:14 -07003144void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3145 if (fboid == fBoundDrawFramebuffer &&
3146 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3147 // This workaround only applies to deleting currently bound framebuffers
3148 // on Adreno 420. Because this is a somewhat rare case, instead of
3149 // tracking all the attachments of every framebuffer instead just always
3150 // unbind all attachments.
3151 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3152 GR_GL_RENDERBUFFER, 0));
3153 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3154 GR_GL_RENDERBUFFER, 0));
3155 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3156 GR_GL_RENDERBUFFER, 0));
3157 }
3158
3159 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003160
3161 // Deleting the currently bound framebuffer rebinds to 0.
3162 if (fboid == fBoundDrawFramebuffer) {
3163 this->onFBOChanged();
3164 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003165}
3166
Greg Daniel46cfbc62019-06-07 11:43:30 -04003167bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003168 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003169 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3170 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3171 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003172 auto dstFormat = dst->backendFormat().asGLFormat();
3173 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003174 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003175 return true;
3176 }
3177 }
cblume61214052016-01-26 09:10:48 -08003178
Greg Daniel46cfbc62019-06-07 11:43:30 -04003179 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3180 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003181 return true;
3182 }
3183
Greg Daniel46cfbc62019-06-07 11:43:30 -04003184 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3185 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003186 }
3187
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003188 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003189 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003190 return true;
3191 }
bsalomon083617b2016-02-12 12:10:14 -08003192 }
3193
bsalomon6df86402015-06-01 10:41:49 -07003194 return false;
3195}
3196
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003197bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003198 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003199
3200 int progIdx = TextureToCopyProgramIdx(srcTex);
3201 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3202 GrSLType samplerType =
3203 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3204
3205 if (!fCopyProgramArrayBuffer) {
3206 static const GrGLfloat vdata[] = {
3207 0, 0,
3208 0, 1,
3209 1, 0,
3210 1, 1
3211 };
3212 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3213 kStatic_GrAccessPattern, vdata);
3214 }
3215 if (!fCopyProgramArrayBuffer) {
3216 return false;
3217 }
3218
3219 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3220 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3221 if (!fCopyPrograms[progIdx].fProgram) {
3222 return false;
3223 }
3224
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003225 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3226 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3227 GrShaderVar::kUniform_TypeModifier);
3228 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3229 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3230 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3231 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3232
Greg Daniel9e3169b2019-06-06 14:38:17 -04003233 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003234 if (shaderCaps->noperspectiveInterpolationSupport()) {
3235 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3236 vshaderTxt.appendf("#extension %s : require\n", extension);
3237 }
3238 vTexCoord.addModifier("noperspective");
3239 }
3240
3241 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3242 vshaderTxt.append(";");
3243 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3244 vshaderTxt.append(";");
3245 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3246 vshaderTxt.append(";");
3247 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3248 vshaderTxt.append(";");
3249
3250 vshaderTxt.append(
3251 "// Copy Program VS\n"
3252 "void main() {"
3253 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3254 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3255 " sk_Position.zw = half2(0, 1);"
3256 "}"
3257 );
3258
Greg Daniel9e3169b2019-06-06 14:38:17 -04003259 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003260 if (shaderCaps->noperspectiveInterpolationSupport()) {
3261 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3262 fshaderTxt.appendf("#extension %s : require\n", extension);
3263 }
3264 }
3265 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3266 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3267 fshaderTxt.append(";");
3268 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3269 fshaderTxt.append(";");
3270 fshaderTxt.appendf(
3271 "// Copy Program FS\n"
3272 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003273 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003274 "}"
3275 );
3276
3277 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3278 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3279 SkSL::Program::Settings settings;
3280 settings.fCaps = shaderCaps;
3281 SkSL::String glsl;
3282 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3283 sksl, settings, &glsl, errorHandler);
3284 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3285 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3286 SkASSERT(program->fInputs.isEmpty());
3287
3288 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3289 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3290 errorHandler);
3291 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3292 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3293 errorHandler);
3294 SkASSERT(program->fInputs.isEmpty());
3295
3296 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3297
3298 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3299 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3300 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3301 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3302 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3303 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3304
3305 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3306
3307 GL_CALL(DeleteShader(vshader));
3308 GL_CALL(DeleteShader(fshader));
3309
3310 return true;
3311}
3312
brianosman33f6b3f2016-06-02 05:49:21 -07003313bool GrGLGpu::createMipmapProgram(int progIdx) {
3314 const bool oddWidth = SkToBool(progIdx & 0x2);
3315 const bool oddHeight = SkToBool(progIdx & 0x1);
3316 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3317
Brian Salomon1edc5b92016-11-29 13:43:46 -05003318 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003319
3320 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3321 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3322 if (!fMipmapPrograms[progIdx].fProgram) {
3323 return false;
3324 }
3325
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003326 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3327 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003328 GrShaderVar::kUniform_TypeModifier);
3329 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3330 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003331 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003332 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003333 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3334 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3335 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3336 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003337 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003338 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003339
Ethan Nicholasfc994162019-06-06 10:04:27 -04003340 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003341 if (shaderCaps->noperspectiveInterpolationSupport()) {
3342 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003343 vshaderTxt.appendf("#extension %s : require\n", extension);
3344 }
3345 vTexCoords[0].addModifier("noperspective");
3346 vTexCoords[1].addModifier("noperspective");
3347 vTexCoords[2].addModifier("noperspective");
3348 vTexCoords[3].addModifier("noperspective");
3349 }
3350
Brian Salomon1edc5b92016-11-29 13:43:46 -05003351 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003352 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003353 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003354 vshaderTxt.append(";");
3355 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003356 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003357 vshaderTxt.append(";");
3358 }
3359
3360 vshaderTxt.append(
3361 "// Mipmap Program VS\n"
3362 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003363 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3364 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003365 );
3366
3367 // Insert texture coordinate computation:
3368 if (oddWidth && oddHeight) {
3369 vshaderTxt.append(
3370 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003371 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3372 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003373 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3374 );
3375 } else if (oddWidth) {
3376 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003377 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3378 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003379 );
3380 } else if (oddHeight) {
3381 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003382 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3383 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003384 );
3385 } else {
3386 vshaderTxt.append(
3387 " v_texCoord0 = a_vertex.xy;"
3388 );
3389 }
3390
3391 vshaderTxt.append("}");
3392
Ethan Nicholasfc994162019-06-06 10:04:27 -04003393 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003394 if (shaderCaps->noperspectiveInterpolationSupport()) {
3395 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003396 fshaderTxt.appendf("#extension %s : require\n", extension);
3397 }
3398 }
brianosman33f6b3f2016-06-02 05:49:21 -07003399 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003400 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003401 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003402 fshaderTxt.append(";");
3403 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003404 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003405 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003406 fshaderTxt.append(
3407 "// Mipmap Program FS\n"
3408 "void main() {"
3409 );
3410
3411 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003412 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003413 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3414 " sample(u_texture, v_texCoord1) + "
3415 " sample(u_texture, v_texCoord2) + "
3416 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003417 );
3418 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003419 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003420 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3421 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003422 );
3423 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003424 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003425 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003426 );
3427 }
3428
3429 fshaderTxt.append("}");
3430
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003431 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003432 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003433 SkSL::Program::Settings settings;
3434 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003435 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003436 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003437 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003438 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003439 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003440 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003441
Brian Osman6c431d52019-04-15 16:31:54 -04003442 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003443 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3444 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003445 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003446 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003447 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003448 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003449
3450 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3451
3452 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3453 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3454 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3455 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3456
3457 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3458
3459 GL_CALL(DeleteShader(vshader));
3460 GL_CALL(DeleteShader(fshader));
3461
3462 return true;
3463}
3464
Greg Daniel46cfbc62019-06-07 11:43:30 -04003465bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003466 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003467 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3468 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3469 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3470 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003471 return false;
3472 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003473 int progIdx = TextureToCopyProgramIdx(srcTex);
3474 if (!dstRT) {
3475 SkASSERT(dstTex);
3476 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3477 return false;
3478 }
3479 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003480 if (!fCopyPrograms[progIdx].fProgram) {
3481 if (!this->createCopyProgram(srcTex)) {
3482 SkDebugf("Failed to create copy program.\n");
3483 return false;
3484 }
3485 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003486 int w = srcRect.width();
3487 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003488 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003489 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003490 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003491 this->flushViewport(dst->width(), dst->height());
3492 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003493 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003494 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003495 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003496 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3497 attribs->enableVertexArrays(this, 1);
3498 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3499 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003500 // dst rect edges in NDC (-1 to 1)
3501 int dw = dst->width();
3502 int dh = dst->height();
3503 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3504 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3505 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3506 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003507 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3508 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3509 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3510 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3511 int sw = src->width();
3512 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003513 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3514 // src rect edges in normalized texture space (0 to 1)
3515 sx0 /= sw;
3516 sx1 /= sw;
3517 sy0 /= sh;
3518 sy1 /= sh;
3519 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003520 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3521 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3522 sx1 - sx0, sy1 - sy0, sx0, sy0));
3523 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003524 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003525 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003526 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003527 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003528 this->disableScissor();
3529 this->disableWindowRectangles();
3530 this->disableStencil();
3531 if (this->glCaps().srgbWriteControl()) {
3532 this->flushFramebufferSRGB(true);
3533 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003534 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003535 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003536 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3537 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003538 return true;
3539}
3540
Greg Daniel46cfbc62019-06-07 11:43:30 -04003541void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003542 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003543 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003544 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003545 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003546 SkASSERT(dstTex);
3547 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003548 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003549
Brian Salomond978b902019-02-07 15:09:18 -05003550 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003551 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003552 dstPoint.fX, dstPoint.fY,
3553 srcRect.fLeft, srcRect.fTop,
3554 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003555 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003556 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3557 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003558 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3559 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003560}
3561
Greg Daniel46cfbc62019-06-07 11:43:30 -04003562bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003563 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003564 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003565 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3566 srcRect.width(), srcRect.height());
3567 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003568 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003569 return false;
mtklein404b3b22015-05-18 09:29:10 -07003570 }
bsalomon5df6fee2015-05-18 06:26:15 -07003571 }
bsalomon6df86402015-06-01 10:41:49 -07003572
Brian Salomond2a8ae22019-09-10 16:03:59 -04003573 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3574 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003575 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003576 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003577
3578 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003579 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003580 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003581
Greg Daniel46cfbc62019-06-07 11:43:30 -04003582 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3583 srcRect.fTop,
3584 srcRect.fRight,
3585 srcRect.fBottom,
3586 dstRect.fLeft,
3587 dstRect.fTop,
3588 dstRect.fRight,
3589 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003590 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003591 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3592 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003593
3594 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3595 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003596 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003597}
3598
Brian Salomon930f9392018-06-20 16:25:26 -04003599bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3600 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003601 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003602 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003603 return false;
3604 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003605 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003606 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3607 // Uses draw calls to do a series of downsample operations to successive mips.
3608
3609 // The manual approach requires the ability to limit which level we're sampling and that the
3610 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003611 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003612 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003613 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003614 GL_CALL(GenerateMipmap(glTex->target()));
3615 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003616 }
3617
brianosman33f6b3f2016-06-02 05:49:21 -07003618 int width = texture->width();
3619 int height = texture->height();
3620 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003621 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003622
3623 // Create (if necessary), then bind temporary FBO:
3624 if (0 == fTempDstFBOID) {
3625 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3626 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003627 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003628 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003629
3630 // Bind the texture, to get things configured for filtering.
3631 // We'll be changing our base level further below:
3632 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003633 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003634 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003635
3636 // Vertex data:
3637 if (!fMipmapProgramArrayBuffer) {
3638 static const GrGLfloat vdata[] = {
3639 0, 0,
3640 0, 1,
3641 1, 0,
3642 1, 1
3643 };
Brian Salomonae64c192019-02-05 09:41:37 -05003644 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003645 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003646 }
3647 if (!fMipmapProgramArrayBuffer) {
3648 return false;
3649 }
3650
3651 fHWVertexArrayState.setVertexArrayID(this, 0);
3652
3653 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003654 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003655 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003656 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003657
3658 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003659 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003660 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003661 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003662 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003663 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003664
3665 // Do all the blits:
3666 width = texture->width();
3667 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003668
brianosman33f6b3f2016-06-02 05:49:21 -07003669 for (GrGLint level = 1; level < levelCount; ++level) {
3670 // Get and bind the program for this particular downsample (filter shape can vary):
3671 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3672 if (!fMipmapPrograms[progIdx].fProgram) {
3673 if (!this->createMipmapProgram(progIdx)) {
3674 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003675 // Invalidate all params to cover base level change in a previous iteration.
3676 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003677 return false;
3678 }
3679 }
Brian Salomon802cb312018-06-08 18:05:20 -04003680 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003681
3682 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3683 const float invWidth = 1.0f / width;
3684 const float invHeight = 1.0f / height;
3685 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3686 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3687 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3688
3689 // Only sample from previous mip
3690 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3691
Brian Salomon930f9392018-06-20 16:25:26 -04003692 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3693 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003694
Brian Osman788b9162020-02-07 10:36:46 -05003695 width = std::max(1, width / 2);
3696 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003697 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003698
3699 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3700 }
3701
3702 // Unbind:
3703 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3704 GR_GL_TEXTURE_2D, 0, 0));
3705
Brian Salomon930f9392018-06-20 16:25:26 -04003706 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003707 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3708 // We drew the 2nd to last level into the last level.
3709 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3710 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003711
brianosman33f6b3f2016-06-02 05:49:21 -07003712 return true;
3713}
3714
Chris Daltond7291ba2019-03-07 14:17:03 -07003715void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003716 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3717 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003718
3719 int effectiveSampleCnt;
3720 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003721 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003722
3723 sampleLocations->reset(effectiveSampleCnt);
3724 for (int i = 0; i < effectiveSampleCnt; ++i) {
3725 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3726 }
3727}
3728
cdalton231c5fd2015-05-13 12:35:36 -07003729void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003730 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003731 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003732 case kTexture_GrXferBarrierType: {
3733 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003734 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003735 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3736 // The render target uses separate storage so no need for glTextureBarrier.
3737 // FIXME: The render target will resolve automatically when its texture is bound,
3738 // but we could resolve only the bounds that will be read if we do it here instead.
3739 return;
3740 }
cdalton9954bc32015-04-29 14:17:00 -07003741 SkASSERT(this->caps()->textureBarrierSupport());
3742 GL_CALL(TextureBarrier());
3743 return;
cdalton231c5fd2015-05-13 12:35:36 -07003744 }
cdalton8917d622015-05-06 13:40:21 -07003745 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003746 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003747 this->caps()->blendEquationSupport());
3748 GL_CALL(BlendBarrier());
3749 return;
bsalomoncb02b382015-08-12 11:14:50 -07003750 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003751 }
3752}
3753
Chris Daltone1196c52019-12-28 14:31:09 -07003754void GrGLGpu::insertManualFramebufferBarrier() {
3755 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3756 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3757}
3758
Brian Salomon85c3d682019-11-04 15:04:54 -05003759GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003760 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003761 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003762 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003763 GrProtected isProtected,
3764 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003765 // We don't support protected textures in GL.
3766 if (isProtected == GrProtected::kYes) {
3767 return {};
3768 }
3769
Brian Salomon8a375832018-03-14 10:21:40 -04003770 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003771
Brian Salomond4764a12019-08-08 12:08:24 -04003772 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003773 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003774 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003775 }
3776
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003777 int numMipLevels = 1;
3778 if (mipMapped == GrMipMapped::kYes) {
3779 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3780 }
3781
Robert Phillipsd34691b2019-09-24 13:38:43 -04003782 // Compressed formats go through onCreateCompressedBackendTexture
3783 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3784
Greg Daniel15500642019-06-27 03:05:55 +00003785 GrGLTextureInfo info;
3786 GrGLTextureParameters::SamplerOverriddenState initialState;
3787
Greg Danield51fa2f2020-01-22 16:53:38 -05003788 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003789 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003790 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003791
Robert Phillipsd34691b2019-09-24 13:38:43 -04003792 info.fTarget = GR_GL_TEXTURE_2D;
3793 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003794 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003795 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003796 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003797 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003798
Robert Phillips42716d42019-12-16 12:19:54 -05003799 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003800 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3801 SkTDArray<GrMipLevel> texels;
3802 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003803 texels.append(numMipLevels);
3804 for (int i = 0; i < numMipLevels; ++i) {
3805 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3806 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003807 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3808 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3809 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003810 GL_CALL(DeleteTextures(1, &info.fID));
3811 return {};
3812 }
3813 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3814 // TODO: Unify this with the clear texture code in onCreateTexture().
3815 GrColorType colorType;
3816 GrGLenum externalFormat, externalType;
3817 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3818 &externalType, &colorType);
3819 if (colorType == GrColorType::kUnknown) {
3820 GL_CALL(DeleteTextures(1, &info.fID));
3821 return {};
3822 }
3823
3824 // Make one tight image at the base size and reuse it for smaller levels.
3825 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3826 auto rb = ii.minRowBytes();
3827 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3828 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3829 GL_CALL(DeleteTextures(1, &info.fID));
3830 return {};
3831 }
3832
3833 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3834 SkISize levelDimensions = dimensions;
3835 for (int i = 0; i < numMipLevels; ++i) {
3836 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3837 levelDimensions.height(), externalFormat, externalType,
3838 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003839 levelDimensions = {std::max(1, levelDimensions.width() /2),
3840 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003841 }
3842 }
3843 // Unbind this texture from the scratch texture unit.
3844 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003845
Brian Salomone2826ab2019-06-04 15:58:31 -04003846 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003847 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003848 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3849 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003850
Brian Salomon85c3d682019-11-04 15:04:54 -05003851 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3852 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003853}
3854
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003855void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003856 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3857
3858 GrGLTextureInfo info;
3859 if (tex.getGLTextureInfo(&info)) {
3860 GL_CALL(DeleteTextures(1, &info.fID));
3861 }
3862}
3863
3864#if GR_TEST_UTILS
3865
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003866bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003867 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003868
Greg Daniel52e16d92018-04-10 09:34:07 -04003869 GrGLTextureInfo info;
3870 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003871 return false;
3872 }
3873
3874 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003875 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003876
3877 return (GR_GL_TRUE == result);
3878}
3879
Brian Salomonf865b052018-03-09 09:01:53 -05003880GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003881 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003882 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3883 return GrBackendRenderTarget(); // invalid
3884 }
Brian Salomon8a375832018-03-14 10:21:40 -04003885 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003886 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003887 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003888 return {};
3889 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003890 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003891 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003892 if (sFormatIdx < 0) {
3893 return {};
3894 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003895 GrGLuint colorID = 0;
3896 GrGLuint stencilID = 0;
3897 auto deleteIDs = [&] {
3898 if (colorID) {
3899 if (useTexture) {
3900 GL_CALL(DeleteTextures(1, &colorID));
3901 } else {
3902 GL_CALL(DeleteRenderbuffers(1, &colorID));
3903 }
Brian Salomonf865b052018-03-09 09:01:53 -05003904 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003905 if (stencilID) {
3906 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003907 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003908 };
3909
3910 if (useTexture) {
3911 GL_CALL(GenTextures(1, &colorID));
3912 } else {
3913 GL_CALL(GenRenderbuffers(1, &colorID));
3914 }
3915 GL_CALL(GenRenderbuffers(1, &stencilID));
3916 if (!stencilID || !colorID) {
3917 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003918 return {};
3919 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003920
Brian Salomonf865b052018-03-09 09:01:53 -05003921 GrGLFramebufferInfo info;
3922 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003923 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003924 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3925 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003926 deleteIDs();
3927 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003928 }
3929
3930 this->invalidateBoundRenderTarget();
3931
Adrienne Walker4ee88512018-05-17 11:37:14 -07003932 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003933 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003934 GrGLTextureParameters::SamplerOverriddenState initialState;
3935 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3936 if (!colorID) {
3937 deleteIDs();
3938 return {};
3939 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003940 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3941 colorID, 0));
3942 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003943 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003944 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3945 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003946 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003947 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3948 GR_GL_RENDERBUFFER, colorID));
3949 }
3950 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003951 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3952 GL_ALLOC_CALL(this->glInterface(),
3953 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3954 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003955 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003956 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3957 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003958 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003959 }
3960
Brian Salomon93348dd2018-08-29 12:56:23 -04003961 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3962 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3963 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3964 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003965 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003966 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003967
Adrienne Walker4ee88512018-05-17 11:37:14 -07003968 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003969 GrGLenum status;
3970 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3971 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003972 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003973 return {};
3974 }
3975 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003976
Greg Daniel108bb232018-07-03 16:18:29 -04003977 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003978 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003979 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003980}
3981
3982void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003983 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003984 GrGLFramebufferInfo info;
3985 if (backendRT.getGLFramebufferInfo(&info)) {
3986 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003987 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003988 }
3989 }
joshualitt8fd844f2015-12-02 13:36:47 -08003990}
3991
Greg Daniel26b50a42018-03-08 09:49:58 -05003992void GrGLGpu::testingOnly_flushGpuAndSync() {
3993 GL_CALL(Finish());
3994}
Brian Salomonf865b052018-03-09 09:01:53 -05003995#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003996
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003997///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003998
cdaltone2e71c22016-04-07 18:13:29 -07003999GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004000 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004001 GrGLAttribArrayState* attribState;
4002
cdaltone2e71c22016-04-07 18:13:29 -07004003 if (gpu->glCaps().isCoreProfile()) {
4004 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004005 GrGLuint arrayID;
4006 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4007 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004008 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004009 }
cdaltone2e71c22016-04-07 18:13:29 -07004010 if (ibuf) {
4011 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004012 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004013 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004014 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004015 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004016 if (ibuf) {
4017 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004018 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004019 } else {
4020 this->setVertexArrayID(gpu, 0);
4021 }
4022 int attrCount = gpu->glCaps().maxVertexAttributes();
4023 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4024 fDefaultVertexArrayAttribState.resize(attrCount);
4025 }
4026 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004027 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004028 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004029}
bsalomone179a912016-01-20 06:18:10 -08004030
Greg Daniel30a35e82019-11-19 14:12:25 -05004031bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004032 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004033 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004034 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004035 // We call finish if the client told us to sync or if we have a finished proc but don't support
4036 // GLsync objects.
4037 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4038 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4039 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004040 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004041 // After a finish everything previously sent to GL is done.
4042 for (const auto& cb : fFinishCallbacks) {
4043 cb.fCallback(cb.fContext);
4044 this->deleteSync(cb.fSync);
4045 }
4046 fFinishCallbacks.clear();
4047 if (info.fFinishedProc) {
4048 info.fFinishedProc(info.fFinishedContext);
4049 }
4050 } else {
4051 if (info.fFinishedProc) {
4052 FinishCallback callback;
4053 callback.fCallback = info.fFinishedProc;
4054 callback.fContext = info.fFinishedContext;
4055 callback.fSync = (GrGLsync)this->insertFence();
4056 fFinishCallbacks.push_back(callback);
4057 GL_CALL(Flush());
4058 } else if (insertedSemaphore) {
4059 // Must call flush after semaphores in case they are waited on another GL context.
4060 GL_CALL(Flush());
4061 }
4062 // See if any previously inserted finish procs are good to go.
4063 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004064 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004065 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004066}
4067
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004068void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4069 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4070 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4071 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004072}
4073
Greg Daniel6be35232017-03-01 17:01:09 -05004074GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004075 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004076 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004077 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4078 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4079 GrGLuint fence = 0;
4080 GL_CALL(GenFences(1, &fence));
4081 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4082 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4083 } else {
4084 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4085 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004086 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004087 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004088}
4089
Brian Salomonb0d8b762019-05-06 16:58:22 -04004090bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004091 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4092 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4093 if (!timeout) {
4094 if (flush) {
4095 GL_CALL(Flush);
4096 }
4097 GrGLboolean result;
4098 GL_CALL_RET(result, TestFence(nvFence));
4099 return result == GR_GL_TRUE;
4100 }
4101 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4102 // If this really becomes necessary we could poll TestFence().
4103 // FinishFence always flushes so no need to check flush param.
4104 GL_CALL(FinishFence(nvFence));
4105 return true;
4106 } else {
4107 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4108 GrGLenum result;
4109 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4110 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4111 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004112}
4113
4114bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4115 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004116}
4117
4118void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004119 this->deleteSync((GrGLsync)fence);
4120}
4121
Greg Daniel301015c2019-11-18 14:06:46 -05004122std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004123 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004124 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004125}
4126
Greg Daniel301015c2019-11-18 14:06:46 -05004127std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4128 const GrBackendSemaphore& semaphore,
4129 GrResourceProvider::SemaphoreWrapType wrapType,
4130 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004131 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004132 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4133}
4134
Greg Daniel301015c2019-11-18 14:06:46 -05004135void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4136 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004137
Greg Daniel48661b82018-01-22 16:11:35 -05004138 GrGLsync sync;
4139 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4140 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004141}
4142
Greg Daniel301015c2019-11-18 14:06:46 -05004143void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4144 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004145
4146 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4147}
4148
Brian Salomonb0d8b762019-05-06 16:58:22 -04004149void GrGLGpu::checkFinishProcs() {
4150 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4151 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4152 /* timeout = */ 0, /* flush = */ false)) {
4153 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4154 this->deleteSync(fFinishCallbacks.front().fSync);
4155 fFinishCallbacks.pop_front();
4156 }
4157}
4158
Greg Daniel6be35232017-03-01 17:01:09 -05004159void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004160 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4161 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4162 GL_CALL(DeleteFences(1, &nvFence));
4163 } else {
4164 GL_CALL(DeleteSync(sync));
4165 }
jvanverth84741b32016-09-30 08:39:02 -07004166}
Brian Osman13dddce2017-05-09 13:19:50 -04004167
Greg Daniel301015c2019-11-18 14:06:46 -05004168std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004169 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004170 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004171 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004172 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004173 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4174 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004175
4176 return semaphore;
4177}
Robert Phillips646e4292017-06-13 12:44:56 -04004178
4179int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004180 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004181 case kTexture2DSampler_GrSLType:
4182 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004183 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004184 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004185 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004186 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004187 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004188 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004189 }
4190}
Brian Osman71a18892017-08-10 10:23:25 -04004191
Kevin Lubickf4def342018-10-04 12:52:50 -04004192#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004193#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004194void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4195 // We are called by the base class, which has already called beginObject(). We choose to nest
4196 // all of our caps information in a named sub-object.
4197 writer->beginObject("GL GPU");
4198
4199 const GrGLubyte* str;
4200 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4201 writer->appendString("GL_VERSION", (const char*)(str));
4202 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4203 writer->appendString("GL_RENDERER", (const char*)(str));
4204 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4205 writer->appendString("GL_VENDOR", (const char*)(str));
4206 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4207 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4208
4209 writer->appendName("extensions");
4210 glInterface()->fExtensions.dumpJSON(writer);
4211
4212 writer->endObject();
4213}
Kevin Lubickf4def342018-10-04 12:52:50 -04004214#endif