blob: 89b962c1526599ab95db1bb57d9e317f2d0aa449 [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
Brian Salomond8575452020-02-25 12:13:29 -050048#define GL_ALLOC_CALL(call) \
49 [&] { \
50 if (this->glCaps().skipErrorChecks()) { \
51 GR_GL_CALL(this->glInterface(), call); \
52 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
53 } else { \
54 GrGLClearErr(this->glInterface()); \
55 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
56 return GR_GL_GET_ERROR(this->glInterface()); \
57 } \
58 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000059
Jim Van Verth32ac83e2016-11-28 15:23:57 -050060//#define USE_NSIGHT
61
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000062///////////////////////////////////////////////////////////////////////////////
63
cdalton8917d622015-05-06 13:40:21 -070064static const GrGLenum gXfermodeEquation2Blend[] = {
65 // Basic OpenGL blend equations.
66 GR_GL_FUNC_ADD,
67 GR_GL_FUNC_SUBTRACT,
68 GR_GL_FUNC_REVERSE_SUBTRACT,
69
70 // GL_KHR_blend_equation_advanced.
71 GR_GL_SCREEN,
72 GR_GL_OVERLAY,
73 GR_GL_DARKEN,
74 GR_GL_LIGHTEN,
75 GR_GL_COLORDODGE,
76 GR_GL_COLORBURN,
77 GR_GL_HARDLIGHT,
78 GR_GL_SOFTLIGHT,
79 GR_GL_DIFFERENCE,
80 GR_GL_EXCLUSION,
81 GR_GL_MULTIPLY,
82 GR_GL_HSL_HUE,
83 GR_GL_HSL_SATURATION,
84 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050085 GR_GL_HSL_LUMINOSITY,
86
87 // Illegal... needs to map to something.
88 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070089};
Brian Salomon4dea72a2019-12-18 10:43:10 -050090static_assert(0 == kAdd_GrBlendEquation);
91static_assert(1 == kSubtract_GrBlendEquation);
92static_assert(2 == kReverseSubtract_GrBlendEquation);
93static_assert(3 == kScreen_GrBlendEquation);
94static_assert(4 == kOverlay_GrBlendEquation);
95static_assert(5 == kDarken_GrBlendEquation);
96static_assert(6 == kLighten_GrBlendEquation);
97static_assert(7 == kColorDodge_GrBlendEquation);
98static_assert(8 == kColorBurn_GrBlendEquation);
99static_assert(9 == kHardLight_GrBlendEquation);
100static_assert(10 == kSoftLight_GrBlendEquation);
101static_assert(11 == kDifference_GrBlendEquation);
102static_assert(12 == kExclusion_GrBlendEquation);
103static_assert(13 == kMultiply_GrBlendEquation);
104static_assert(14 == kHSLHue_GrBlendEquation);
105static_assert(15 == kHSLSaturation_GrBlendEquation);
106static_assert(16 == kHSLColor_GrBlendEquation);
107static_assert(17 == kHSLLuminosity_GrBlendEquation);
108static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700109
twiz@google.com0f31ca72011-03-18 17:38:11 +0000110static const GrGLenum gXfermodeCoeff2Blend[] = {
111 GR_GL_ZERO,
112 GR_GL_ONE,
113 GR_GL_SRC_COLOR,
114 GR_GL_ONE_MINUS_SRC_COLOR,
115 GR_GL_DST_COLOR,
116 GR_GL_ONE_MINUS_DST_COLOR,
117 GR_GL_SRC_ALPHA,
118 GR_GL_ONE_MINUS_SRC_ALPHA,
119 GR_GL_DST_ALPHA,
120 GR_GL_ONE_MINUS_DST_ALPHA,
121 GR_GL_CONSTANT_COLOR,
122 GR_GL_ONE_MINUS_CONSTANT_COLOR,
123 GR_GL_CONSTANT_ALPHA,
124 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000125
126 // extended blend coeffs
127 GR_GL_SRC1_COLOR,
128 GR_GL_ONE_MINUS_SRC1_COLOR,
129 GR_GL_SRC1_ALPHA,
130 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500131
132 // Illegal... needs to map to something.
133 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000134};
135
bsalomon861e1032014-12-16 07:33:49 -0800136bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000137 static const bool gCoeffReferencesBlendConst[] = {
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 false,
146 false,
147 false,
148 true,
149 true,
150 true,
151 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000152
153 // extended blend coeffs
154 false,
155 false,
156 false,
157 false,
Mike Klein36743362018-11-06 08:23:30 -0500158
159 // Illegal.
160 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000161 };
162 return gCoeffReferencesBlendConst[coeff];
Brian Salomon4dea72a2019-12-18 10:43:10 -0500163 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000164
Brian Salomon4dea72a2019-12-18 10:43:10 -0500165 static_assert(0 == kZero_GrBlendCoeff);
166 static_assert(1 == kOne_GrBlendCoeff);
167 static_assert(2 == kSC_GrBlendCoeff);
168 static_assert(3 == kISC_GrBlendCoeff);
169 static_assert(4 == kDC_GrBlendCoeff);
170 static_assert(5 == kIDC_GrBlendCoeff);
171 static_assert(6 == kSA_GrBlendCoeff);
172 static_assert(7 == kISA_GrBlendCoeff);
173 static_assert(8 == kDA_GrBlendCoeff);
174 static_assert(9 == kIDA_GrBlendCoeff);
175 static_assert(10 == kConstC_GrBlendCoeff);
176 static_assert(11 == kIConstC_GrBlendCoeff);
177 static_assert(12 == kConstA_GrBlendCoeff);
178 static_assert(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000179
Brian Salomon4dea72a2019-12-18 10:43:10 -0500180 static_assert(14 == kS2C_GrBlendCoeff);
181 static_assert(15 == kIS2C_GrBlendCoeff);
182 static_assert(16 == kS2A_GrBlendCoeff);
183 static_assert(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000184
185 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
Brian Salomon4dea72a2019-12-18 10:43:10 -0500186 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000187}
188
Brian Salomond978b902019-02-07 15:09:18 -0500189//////////////////////////////////////////////////////////////////////////////
190
191static int gl_target_to_binding_index(GrGLenum target) {
192 switch (target) {
193 case GR_GL_TEXTURE_2D:
194 return 0;
195 case GR_GL_TEXTURE_RECTANGLE:
196 return 1;
197 case GR_GL_TEXTURE_EXTERNAL:
198 return 2;
199 }
200 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500201}
202
203GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500204 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
205}
206
207bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
208 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500209}
210
211void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500212 int targetIndex = gl_target_to_binding_index(target);
213 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
214 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500215}
216
Brian Salomon1f05d452019-02-08 12:33:08 -0500217void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
218 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500219}
220
Brian Salomon1f05d452019-02-08 12:33:08 -0500221void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
222 for (auto& targetBinding : fTargetBindings) {
223 targetBinding.fBoundResourceID.makeInvalid();
224 if (markUnmodified) {
225 targetBinding.fHasBeenModified = false;
226 }
Brian Salomond978b902019-02-07 15:09:18 -0500227 }
228}
229
230//////////////////////////////////////////////////////////////////////////////
231
Brian Salomondc829942018-10-23 16:07:24 -0400232static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
233 switch (filter) {
234 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
235 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
236 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
237 }
238 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400239}
240
241static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
242 switch (filter) {
243 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
244 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
245 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
246 }
247 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400248}
249
Michael Ludwigf23a1522018-12-10 11:36:13 -0500250static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
251 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400252 switch (wrapMode) {
253 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
254 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
255 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500256 case GrSamplerState::WrapMode::kClampToBorder:
257 // May not be supported but should have been caught earlier
258 SkASSERT(caps.clampToBorderSupport());
259 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500260 }
Brian Salomondc829942018-10-23 16:07:24 -0400261 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400262}
263
264///////////////////////////////////////////////////////////////////////////////
265
266class GrGLGpu::SamplerObjectCache {
267public:
268 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
269 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
270 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
271 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
272 std::fill_n(fSamplers, kNumSamplers, 0);
273 }
274
275 ~SamplerObjectCache() {
276 if (!fNumTextureUnits) {
277 // We've already been abandoned.
278 return;
279 }
Chris Dalton703fe682019-05-15 12:58:12 -0600280 for (GrGLuint sampler : fSamplers) {
281 // The spec states that "zero" values should be silently ignored, however they still
282 // trigger GL errors on some NVIDIA platforms.
283 if (sampler) {
284 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
285 }
286 }
Brian Salomondc829942018-10-23 16:07:24 -0400287 }
288
Brian Salomonccb61422020-01-09 10:46:36 -0500289 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400290 int index = StateToIndex(state);
291 if (!fSamplers[index]) {
292 GrGLuint s;
293 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
294 if (!s) {
295 return;
296 }
297 fSamplers[index] = s;
298 auto minFilter = filter_to_gl_min_filter(state.filter());
299 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500300 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
301 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400302 GR_GL_CALL(fGpu->glInterface(),
303 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
304 GR_GL_CALL(fGpu->glInterface(),
305 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
306 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
307 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
308 }
309 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
310 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
311 fHWBoundSamplers[unitIdx] = fSamplers[index];
312 }
313 }
314
315 void invalidateBindings() {
316 // When we have sampler support we always use samplers. So setting these to zero will cause
317 // a rebind on next usage.
318 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
319 }
320
321 void abandon() {
322 fHWBoundSamplers.reset();
323 fNumTextureUnits = 0;
324 }
325
326 void release() {
327 if (!fNumTextureUnits) {
328 // We've already been abandoned.
329 return;
330 }
331 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
332 std::fill_n(fSamplers, kNumSamplers, 0);
333 // Deleting a bound sampler implicitly binds sampler 0.
334 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
335 }
336
337private:
Brian Salomonccb61422020-01-09 10:46:36 -0500338 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400339 int filter = static_cast<int>(state.filter());
340 SkASSERT(filter >= 0 && filter < 3);
341 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500342 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400343 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500344 SkASSERT(wrapY >= 0 && wrapY < 4);
345 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400346 SkASSERT(idx < kNumSamplers);
347 return idx;
348 }
349
350 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500351 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400352 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
353 GrGLuint fSamplers[kNumSamplers];
354 int fNumTextureUnits;
355};
356
reed@google.comac10a2d2010-12-22 21:39:39 +0000357///////////////////////////////////////////////////////////////////////////////
358
Brian Salomon384fab42017-12-07 12:33:05 -0500359sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
360 GrContext* context) {
361 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500362 interface = GrGLMakeNativeInterface();
363 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
364 // to GrGLMakeNativeInterface.
365 if (!interface) {
366 interface = sk_ref_sp(GrGLCreateNativeInterface());
367 }
Brian Salomon384fab42017-12-07 12:33:05 -0500368 if (!interface) {
369 return nullptr;
370 }
bsalomon424cc262015-05-22 10:37:30 -0700371 }
Jim Van Verth76334772017-05-05 16:46:05 -0400372#ifdef USE_NSIGHT
373 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
374#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500375 auto glContext = GrGLContext::Make(std::move(interface), options);
376 if (!glContext) {
377 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700378 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500379 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700380}
381
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500382GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
383 : GrGpu(context)
384 , fGLContext(std::move(ctx))
385 , fProgramCache(new ProgramCache(this))
386 , fHWProgramID(0)
387 , fTempSrcFBOID(0)
388 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500389 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500390 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400391 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500392 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000393
Brian Salomond978b902019-02-07 15:09:18 -0500394 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000395
Brian Salomonae64c192019-02-05 09:41:37 -0500396 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
397 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500398 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500399 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
400 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
401 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
402 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700403 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500404 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
405 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700406 }
Brian Salomonae64c192019-02-05 09:41:37 -0500407 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400408 fHWBufferState[i].invalidate();
409 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500410 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700411
412 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
413 fPathRendering.reset(new GrGLPathRendering(this));
414 }
415
Brian Salomondc829942018-10-23 16:07:24 -0400416 if (this->glCaps().samplerObjectSupport()) {
417 fSamplerObjectCache.reset(new SamplerObjectCache(this));
418 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000419}
420
bsalomon861e1032014-12-16 07:33:49 -0800421GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700422 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
423 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800424 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700425 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700426 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800427
Brian Salomon43f8bf02017-10-18 08:33:29 -0400428 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000429 // detach the current program so there is no confusion on OpenGL's part
430 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000431 GL_CALL(UseProgram(0));
432 }
433
Brian Salomon43f8bf02017-10-18 08:33:29 -0400434 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700435 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800436 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400437 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700438 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800439 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400440 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700441 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800442 }
egdaniel0f5f9672015-02-03 11:10:51 -0800443
bsalomon7ea33f52015-11-22 14:51:00 -0800444 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
445 if (0 != fCopyPrograms[i].fProgram) {
446 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
447 }
bsalomon6df86402015-06-01 10:41:49 -0700448 }
bsalomon6dea83f2015-12-03 12:58:06 -0800449
brianosman33f6b3f2016-06-02 05:49:21 -0700450 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
451 if (0 != fMipmapPrograms[i].fProgram) {
452 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
453 }
454 }
455
Brian Salomondc829942018-10-23 16:07:24 -0400456 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500457
458 while (!fFinishCallbacks.empty()) {
459 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
460 this->deleteSync(fFinishCallbacks.front().fSync);
461 fFinishCallbacks.pop_front();
462 }
bsalomonc8dc1f72014-08-21 13:02:13 -0700463}
464
bsalomon6e2aad42016-04-01 11:54:31 -0700465void GrGLGpu::disconnect(DisconnectType type) {
466 INHERITED::disconnect(type);
467 if (DisconnectType::kCleanup == type) {
468 if (fHWProgramID) {
469 GL_CALL(UseProgram(0));
470 }
471 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700472 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700473 }
474 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700475 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700476 }
477 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700478 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700479 }
bsalomon6e2aad42016-04-01 11:54:31 -0700480 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
481 if (fCopyPrograms[i].fProgram) {
482 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
483 }
484 }
brianosman33f6b3f2016-06-02 05:49:21 -0700485 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
486 if (fMipmapPrograms[i].fProgram) {
487 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
488 }
489 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400490
Brian Salomondc829942018-10-23 16:07:24 -0400491 if (fSamplerObjectCache) {
492 fSamplerObjectCache->release();
493 }
bsalomon6e2aad42016-04-01 11:54:31 -0700494 } else {
495 if (fProgramCache) {
496 fProgramCache->abandon();
497 }
Brian Salomondc829942018-10-23 16:07:24 -0400498 if (fSamplerObjectCache) {
499 fSamplerObjectCache->abandon();
500 }
bsalomon6e2aad42016-04-01 11:54:31 -0700501 }
502
Robert Phillips1daa2392020-02-18 14:34:36 -0500503 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700504
bsalomonc8dc1f72014-08-21 13:02:13 -0700505 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700506 fTempSrcFBOID = 0;
507 fTempDstFBOID = 0;
508 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700509 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800510 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
511 fCopyPrograms[i].fProgram = 0;
512 }
brianosman33f6b3f2016-06-02 05:49:21 -0700513 fMipmapProgramArrayBuffer.reset();
514 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
515 fMipmapPrograms[i].fProgram = 0;
516 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400517
jvanverthe9c0fc62015-04-29 11:18:05 -0700518 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700519 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700520 }
Greg Daniel822f7b22020-02-21 14:41:36 -0500521
522 while (!fFinishCallbacks.empty()) {
523 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
524 if (DisconnectType::kCleanup == type) {
525 this->deleteSync(fFinishCallbacks.front().fSync);
526 }
527 fFinishCallbacks.pop_front();
528 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000529}
530
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000531///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000532
bsalomon861e1032014-12-16 07:33:49 -0800533void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000534 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400535 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 GL_CALL(Disable(GR_GL_DEPTH_TEST));
537 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000538
Brian Salomonf0861672017-05-08 11:10:10 -0400539 // We don't use face culling.
540 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600541 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
542 // just set this to the default for self-consistency.
543 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400544
Brian Salomonae64c192019-02-05 09:41:37 -0500545 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
546 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700547
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400548 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500549#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000550 // Desktop-only state that we never change
551 if (!this->glCaps().isCoreProfile()) {
552 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
553 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
554 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
555 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
556 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
557 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
558 }
559 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
560 // core profile. This seems like a bug since the core spec removes any mention of
561 // GL_ARB_imaging.
562 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
563 GL_CALL(Disable(GR_GL_COLOR_TABLE));
564 }
565 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400566
Chris Dalton1215cda2019-12-17 21:44:04 -0700567 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500568#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 // Since ES doesn't support glPointSize at all we always use the VS to
570 // set the point size
571 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
572
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000573 }
joshualitt58162332014-08-01 06:44:53 -0700574
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400575 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500576 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700577 // The arm extension requires specifically enabling MSAA fetching per sample.
578 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500579 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700580 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000581 fHWWriteToColor = kUnknown_TriState;
582 // we only ever use lines in hairline mode
583 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700584 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500585
586 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000587 }
edisonn@google.comba669992013-06-28 16:03:21 +0000588
egdanielb414f252014-07-29 13:15:47 -0700589 if (resetBits & kMSAAEnable_GrGLBackendState) {
590 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700591
Chris Dalton6ce447a2019-06-23 18:07:38 -0600592 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800593 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
594 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700595 GL_CALL(CoverageModulation(GR_GL_RGBA));
596 }
Chris Daltonce425af2019-12-16 10:39:03 -0700597
598 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000600
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000601 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400602 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500605 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500606 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 }
Brian Salomondc829942018-10-23 16:07:24 -0400608 if (fSamplerObjectCache) {
609 fSamplerObjectCache->invalidateBindings();
610 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000611 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000612
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000613 if (resetBits & kBlend_GrGLBackendState) {
614 fHWBlendState.invalidate();
615 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000616
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000617 if (resetBits & kView_GrGLBackendState) {
618 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700619 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 fHWViewport.invalidate();
621 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000622
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 if (resetBits & kStencil_GrGLBackendState) {
624 fHWStencilSettings.invalidate();
625 fHWStencilTestEnabled = kUnknown_TriState;
626 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000627
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000628 // Vertex
629 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700630 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500631 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
632 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700633 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000635
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500637 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700638 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500639 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000640 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000641
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000642 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700643 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700644 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000645 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000646 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000647
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000648 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400650 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000651 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
652 }
Brian Salomon1047a492019-07-02 12:25:21 -0400653 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000654 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
655 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000656 if (this->glCaps().packFlipYSupport()) {
657 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
658 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000659 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000660
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000661 if (resetBits & kProgram_GrGLBackendState) {
662 fHWProgramID = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000663 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400664 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000665}
666
Brian Salomonea4ad302019-08-07 13:04:55 -0400667static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
668 const GrGLCaps& caps, GrGLTexture::Desc* desc,
669 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400670 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400671 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400672 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800673 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000674
Brian Salomonea4ad302019-08-07 13:04:55 -0400675 desc->fSize = {backendTex.width(), backendTex.height()};
676 desc->fTarget = info.fTarget;
677 desc->fID = info.fID;
678 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800679
Brian Salomonea4ad302019-08-07 13:04:55 -0400680 if (desc->fFormat == GrGLFormat::kUnknown) {
681 return false;
682 }
683 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400684 if (!caps.shaderCaps()->externalTextureSupport()) {
685 return false;
686 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400687 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
688 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400689 return false;
690 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400691 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400692 return false;
693 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400694 if (backendTex.isProtected()) {
695 // Not supported in GL backend at this time.
696 return false;
697 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400698
Brian Salomond17f6582017-07-19 18:28:58 -0400699 return true;
700}
701
702sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400703 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400704 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400705 GrGLTexture::Desc desc;
706 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400707 return nullptr;
708 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400709
Brian Salomond17f6582017-07-19 18:28:58 -0400710 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400711 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400712 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400713 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400714 }
bsalomone5286e02016-01-14 09:24:09 -0800715
Greg Daniel177e6952017-10-12 12:27:11 -0400716 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
717 : GrMipMapsStatus::kNotAllocated;
718
Brian Salomonea4ad302019-08-07 13:04:55 -0400719 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400720 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400721 // We don't know what parameters are already set on wrapped textures.
722 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600723 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400724}
725
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500726static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
727 const GrGLCaps& caps, GrGLTexture::Desc* desc,
728 bool skipRectTexSupportCheck = false) {
729 GrGLTextureInfo info;
730 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
731 return false;
732 }
733
734 desc->fSize = {backendTex.width(), backendTex.height()};
735 desc->fTarget = info.fTarget;
736 desc->fID = info.fID;
737 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
738
739 if (desc->fFormat == GrGLFormat::kUnknown) {
740 return false;
741 }
742
743 if (GR_GL_TEXTURE_2D != desc->fTarget) {
744 return false;
745 }
746 if (backendTex.isProtected()) {
747 // Not supported in GL backend at this time.
748 return false;
749 }
750
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500751 return true;
752}
753
Robert Phillipsb915c942019-12-17 14:44:37 -0500754sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
755 GrWrapOwnership ownership,
756 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500757 GrGLTexture::Desc desc;
758 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
759 return nullptr;
760 }
761
762 if (kBorrow_GrWrapOwnership == ownership) {
763 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
764 } else {
765 desc.fOwnership = GrBackendObjectOwnership::kOwned;
766 }
767
768 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
769 : GrMipMapsStatus::kNotAllocated;
770
771 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
772 backendTex.getGLTextureParams(), cacheable,
773 kRead_GrIOType);
774 // We don't know what parameters are already set on wrapped textures.
775 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600776 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500777}
778
Brian Salomond17f6582017-07-19 18:28:58 -0400779sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400780 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400781 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500782 GrWrapOwnership ownership,
783 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400784 const GrGLCaps& caps = this->glCaps();
785
Brian Salomonea4ad302019-08-07 13:04:55 -0400786 GrGLTexture::Desc desc;
787 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800788 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800789 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400790 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
791 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800792
Brian Salomond17f6582017-07-19 18:28:58 -0400793 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800795 return nullptr;
796 }
bsalomon10528f12015-10-14 12:54:52 -0700797
Brian Osman766fcbb2017-03-13 09:33:09 -0400798 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400799 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400800 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400801 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800802 }
bsalomonb15b4c12014-10-29 12:41:57 -0700803
Robert Phillips0902c982019-07-16 07:47:56 -0400804
Greg Daniel6fa62e22019-08-07 15:52:37 -0400805 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
806 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000807
Brian Salomonea4ad302019-08-07 13:04:55 -0400808 GrGLRenderTarget::IDs rtIDs;
809 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400810 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000811 }
Greg Daniel177e6952017-10-12 12:27:11 -0400812
813 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
814 : GrMipMapsStatus::kNotAllocated;
815
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500816 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400817 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400818 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400819 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400820 // We don't know what parameters are already set on wrapped textures.
821 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600822 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000823}
824
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400825sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
826 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400827 GrGLFramebufferInfo info;
828 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000829 return nullptr;
830 }
831
Brian Salomone8a766b2019-07-19 14:24:36 -0400832 if (backendRT.isProtected()) {
833 // Not supported in GL at this time.
834 return nullptr;
835 }
836
Brian Salomond4764a12019-08-08 12:08:24 -0400837 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400838 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
839 return nullptr;
840 }
841
Brian Salomonea4ad302019-08-07 13:04:55 -0400842 GrGLRenderTarget::IDs rtIDs;
843 rtIDs.fRTFBOID = info.fFBOID;
844 rtIDs.fMSColorRenderbufferID = 0;
845 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
846 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000847
Greg Daniel6fa62e22019-08-07 15:52:37 -0400848 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700849
Brian Salomona56a7462020-02-07 14:17:25 -0500850 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400851 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000852}
853
Greg Daniel7ef28f32017-04-20 16:41:55 +0000854sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400855 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400856 GrColorType colorType) {
857 GrGLTexture::Desc desc;
858 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
859 // with a texture rectangle,we assume the necessary support exists.
860 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800861 return nullptr;
862 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400863
864 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
865 return nullptr;
866 }
867
868 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400869 GrGLRenderTarget::IDs rtIDs;
870 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800871 return nullptr;
872 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500873 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800874}
875
Brian Salomonc320b152018-02-20 14:05:36 -0500876static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700877 if (!glTex) {
878 return false;
879 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000880
bsalomone5286e02016-01-14 09:24:09 -0800881 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
882 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800883 return false;
884 }
885
jvanverth17aa0472016-01-05 10:41:27 -0800886 return true;
887}
888
Brian Salomona9b04b92018-06-01 15:04:28 -0400889bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400890 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400891 const GrMipLevel texels[], int mipLevelCount,
892 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500893 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800894
Brian Salomonc320b152018-02-20 14:05:36 -0500895 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800896 return false;
897 }
898
Brian Salomond978b902019-02-07 15:09:18 -0500899 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000900
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400901 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400902 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400903 glTex->target(), left, top, width, height, srcColorType, texels,
904 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000905}
906
Brian Salomone05ba5a2019-04-08 11:59:07 -0400907bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400908 GrColorType textureColorType, GrColorType bufferColorType,
909 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911
Jim Van Verth1676cb92019-01-15 13:24:45 -0500912 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400913 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500914
Brian Salomonc320b152018-02-20 14:05:36 -0500915 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400916 return false;
917 }
918
Hal Canaryc36f7e72018-06-18 15:50:09 -0400919 static_assert(sizeof(int) == sizeof(int32_t), "");
920 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921 return false;
922 }
923
Brian Salomond978b902019-02-07 15:09:18 -0500924 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400925
926 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500927 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400928 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500929 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400930
Greg Daniel660cc992017-06-26 14:55:05 -0400931 SkDEBUGCODE(
932 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
933 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
934 SkASSERT(bounds.contains(subRect));
935 )
936
Brian Salomonb28cb682019-07-26 12:48:47 -0400937 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400938 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400939 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700940 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400941 return false;
942 }
943
944 bool restoreGLRowLength = false;
945 if (trimRowBytes != rowBytes) {
946 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400947 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400948 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
949 restoreGLRowLength = true;
950 }
951
Greg Danielba88ab62019-07-26 09:14:01 -0400952 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400953 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400954 GrGLenum externalFormat = 0;
955 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400956 this->glCaps().getTexSubImageExternalFormatAndType(
957 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400958 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400959 return false;
960 }
961
Brian Salomon8cbf6622019-07-30 11:03:14 -0400962 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400963 GL_CALL(TexSubImage2D(glTex->target(),
964 0,
965 left, top,
966 width,
967 height,
968 externalFormat, externalType,
969 pixels));
970
971 if (restoreGLRowLength) {
972 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
973 }
974
975 return true;
976}
977
Brian Salomon26de56e2019-04-10 12:14:26 -0400978bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400979 GrColorType surfaceColorType, GrColorType dstColorType,
980 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400981 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
982 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400983 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400984 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
985 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400986}
987
Brian Osman9b560242017-09-05 15:34:52 -0400988void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500989 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
990 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
991 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
992 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400993 }
Brian Osman9b560242017-09-05 15:34:52 -0400994}
995
Brian Salomond2a8ae22019-09-10 16:03:59 -0400996bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
997 int texHeight, GrGLenum target, int left, int top, int width,
998 int height, GrColorType srcColorType, const GrMipLevel texels[],
999 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001000 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -04001001 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001002
Greg Daniel7bfc9132019-08-14 14:23:53 -04001003 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -04001004 SkDEBUGCODE(
1005 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1006 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1007 SkASSERT(bounds.contains(subRect));
1008 )
Robert Phillips590533f2017-07-11 14:22:35 -04001009 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001010 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001011
Brian Osman9b560242017-09-05 15:34:52 -04001012 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001013
cblume55f2d2d2016-02-26 13:20:48 -08001014 const GrGLInterface* interface = this->glInterface();
1015 const GrGLCaps& caps = this->glCaps();
1016
Brian Salomonf77c1462019-08-01 15:19:29 -04001017 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001018
1019 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001020 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001022
bsalomon5b30c6f2015-12-17 14:17:34 -08001023 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001024 GrGLenum externalFormat;
1025 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001026 this->glCaps().getTexSubImageExternalFormatAndType(
1027 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001028 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001029 return false;
1030 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001031
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001032 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001033 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001034 * because our srcData has extra bytes past each row. If so, we need
1035 * to trim those off here, since GL ES may not let us specify
1036 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001037 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001038 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001039
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001040 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001041 *mipMapsStatus = (mipLevelCount > 1) ?
1042 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001043 }
bsalomone699d0c2016-03-09 06:25:15 -08001044
Brian Salomond2a8ae22019-09-10 16:03:59 -04001045 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001046
Brian Salomond2a8ae22019-09-10 16:03:59 -04001047 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1048 if (!texels[currentMipLevel].fPixels) {
1049 if (mipMapsStatus) {
1050 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001051 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001052 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001053 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001054 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001055 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1056 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001057 const size_t trimRowBytes = currentWidth * bpp;
1058 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1059
1060 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1061 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1062 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1063 restoreGLRowLength = true;
1064 }
1065
1066 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1067 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001068 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001069 if (restoreGLRowLength) {
1070 SkASSERT(caps.writePixelsRowBytesSupport());
1071 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1072 }
1073 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001074}
1075
Greg Daniel7bfc9132019-08-14 14:23:53 -04001076bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001077 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001078 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001080 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001081 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001082 const GrGLCaps& caps = this->glCaps();
1083
1084 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001085 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001086 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001087 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001088 }
1089
Robert Phillips9f744f72019-12-19 19:14:33 -05001090 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1091 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1092
Greg Daniel7bfc9132019-08-14 14:23:53 -04001093 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001094
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001095 int numMipLevels = 1;
1096 if (mipMapped == GrMipMapped::kYes) {
1097 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1098 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001099
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001100 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001101 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001102
1103 if (useTexStorage) {
1104 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001105 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1106 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001107 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001108 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001109 }
Robert Phillips42716d42019-12-16 12:19:54 -05001110
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001111 size_t offset = 0;
1112 for (int level = 0; level < numMipLevels; ++level) {
1113
Robert Phillips99dead92020-01-27 16:11:57 -05001114 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1115 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001116
Brian Salomond8575452020-02-25 12:13:29 -05001117 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1118 level,
1119 0, // left
1120 0, // top
1121 dimensions.width(),
1122 dimensions.height(),
1123 internalFormat,
1124 SkToInt(levelDataSize),
1125 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001126
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001127 if (error != GR_GL_NO_ERROR) {
1128 return false;
1129 }
1130
Robert Phillips9f744f72019-12-19 19:14:33 -05001131 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001132 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001133 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001134 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001135 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001136
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001137 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001138 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1139 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001140
1141 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001142 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1143 level,
1144 internalFormat,
1145 dimensions.width(),
1146 dimensions.height(),
1147 0, // border
1148 SkToInt(levelDataSize),
1149 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001150
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001151 if (error != GR_GL_NO_ERROR) {
1152 return false;
1153 }
1154
Robert Phillips9f744f72019-12-19 19:14:33 -05001155 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001156 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001157 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001158 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001159 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001160}
1161
Brian Salomond8575452020-02-25 12:13:29 -05001162bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001163 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001164 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001165 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001166 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001167 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001168 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1169 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001170 break;
1171 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001172 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1173 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001174 break;
1175 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1176 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001177 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1178 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001179 break;
1180 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001181 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001182 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001183 }
Brian Salomond8575452020-02-25 12:13:29 -05001184 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001185}
1186
Brian Salomonea4ad302019-08-07 13:04:55 -04001187bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001188 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001189 GrGLRenderTarget::IDs* rtIDs) {
1190 rtIDs->fMSColorRenderbufferID = 0;
1191 rtIDs->fRTFBOID = 0;
1192 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1193 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194
bsalomona11e5fc2015-12-18 07:59:41 -08001195 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001196
Brian Salomonea4ad302019-08-07 13:04:55 -04001197 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001198 goto FAILED;
1199 }
1200
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001201 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001202 goto FAILED;
1203 }
1204
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1206 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207 goto FAILED;
1208 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001209
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001210 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1211 // the texture bound to the other. The exception is the IMG multisample extension. With this
1212 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1213 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001214 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001215 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1216 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1217 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 goto FAILED;
1219 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001220 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001222 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 }
1224
egdanield803f272015-03-18 13:01:52 -07001225 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001226 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001227 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001228 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001229 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001230 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1231 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 goto FAILED;
1233 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001234 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001235 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001236 GR_GL_COLOR_ATTACHMENT0,
1237 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001238 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001240 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001242 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001243 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1244 GR_GL_COLOR_ATTACHMENT0,
1245 desc.fTarget,
1246 desc.fID,
1247 0,
1248 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001249 } else {
egdanield803f272015-03-18 13:01:52 -07001250 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001251 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001252 desc.fTarget,
1253 desc.fID,
1254 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001255 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001256
1257 return true;
1258
1259FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001260 if (rtIDs->fMSColorRenderbufferID) {
1261 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001262 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001263 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1264 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 if (rtIDs->fTexFBOID) {
1267 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 }
1269 return false;
1270}
1271
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001272// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001273static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001274// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001275 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001276}
1277
Brian Salomone2826ab2019-06-04 15:58:31 -04001278static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001279 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001280 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1281 // drivers have a bug where an FBO won't be complete if it includes a
1282 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001283 GrGLTextureParameters::SamplerOverriddenState state;
1284 state.fMinFilter = GR_GL_NEAREST;
1285 state.fMagFilter = GR_GL_NEAREST;
1286 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1287 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001288 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1289 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1290 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001292 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001293}
1294
Brian Salomona56a7462020-02-07 14:17:25 -05001295sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001296 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001297 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001298 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001299 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001300 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001301 int mipLevelCount,
1302 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001303 // We don't support protected textures in GL.
1304 if (isProtected == GrProtected::kYes) {
1305 return nullptr;
1306 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001307 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001308
Brian Salomond2a8ae22019-09-10 16:03:59 -04001309 SkASSERT(mipLevelCount > 0);
1310 GrMipMapsStatus mipMapsStatus =
1311 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001312 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001313 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001314 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001315 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001316 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001317 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001318 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1319 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001320
Brian Salomona56a7462020-02-07 14:17:25 -05001321 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1322 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001323
1324 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001325 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001326 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001327
Robert Phillips67d52cf2017-06-05 13:38:13 -04001328 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001329 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001330 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001331 GL_CALL(BindTexture(texDesc.fTarget, 0));
1332 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001333
Brian Salomonea4ad302019-08-07 13:04:55 -04001334 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1335 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 return return_null_texture();
1337 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001338 tex = sk_make_sp<GrGLTextureRenderTarget>(
1339 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001340 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001341 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001342 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001344 // The non-sampler params are still at their default values.
1345 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1346 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001347 if (levelClearMask) {
1348 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001349 GrColorType colorType;
1350 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1351 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001352 if (this->glCaps().clearTextureSupport()) {
1353 for (int i = 0; i < mipLevelCount; ++i) {
1354 if (levelClearMask & (1U << i)) {
1355 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1356 nullptr));
1357 }
1358 }
1359 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1360 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001361 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001362 this->disableWindowRectangles();
1363 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001364 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001365 for (int i = 0; i < mipLevelCount; ++i) {
1366 if (levelClearMask & (1U << i)) {
1367 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1368 kDst_TempFBOTarget);
1369 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1370 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1371 }
1372 }
Brian Salomonc2249852019-09-16 11:08:50 -04001373 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001374 } else {
1375 std::unique_ptr<char[]> zeros;
1376 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1377 for (int i = 0; i < mipLevelCount; ++i) {
1378 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001379 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1380 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001381 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1382 // smaller levels that need clearing.
1383 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001384 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001385 size_t size = levelWidth * levelHeight * bpp;
1386 zeros.reset(new char[size]());
1387 }
1388 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1389 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1390 externalFormat, externalType, zeros.get()));
1391 }
Brian Salomona3e29962019-07-16 11:52:08 -04001392 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001393 }
1394 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001395 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001396}
1397
Robert Phillips9f744f72019-12-19 19:14:33 -05001398sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001399 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001400 SkBudgeted budgeted,
1401 GrMipMapped mipMapped,
1402 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001403 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001404 // We don't support protected textures in GL.
1405 if (isProtected == GrProtected::kYes) {
1406 return nullptr;
1407 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001408 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001409 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001410 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001411 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001412 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001413 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001414 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001415 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001416 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001417 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001418 return nullptr;
1419 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001420
Robert Phillipsee946932019-12-18 11:16:17 -05001421 // Unbind this texture from the scratch texture unit.
1422 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1423
Robert Phillipse4720c62020-01-14 14:33:24 -05001424 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1425 ? GrMipMapsStatus::kValid
1426 : GrMipMapsStatus::kNotAllocated;
1427
1428 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001429 // The non-sampler params are still at their default values.
1430 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1431 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001432 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001433}
1434
Robert Phillipsb915c942019-12-17 14:44:37 -05001435GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1436 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001437 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001438 GrProtected isProtected,
1439 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001440 // We don't support protected textures in GL.
1441 if (isProtected == GrProtected::kYes) {
1442 return {};
1443 }
1444
1445 this->handleDirtyContext();
1446
1447 GrGLFormat glFormat = format.asGLFormat();
1448 if (glFormat == GrGLFormat::kUnknown) {
1449 return {};
1450 }
1451
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001452 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001453 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001454 SkAutoMalloc am;
1455
1456 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1457 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1458 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001459 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001460 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001461 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1462 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001463
Robert Phillips99dead92020-01-27 16:11:57 -05001464 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1465 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001466
1467 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001468
1469 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1470
1471 rawData = (const char*) am.get();
1472 }
1473
1474 GrGLTextureInfo info;
1475 GrGLTextureParameters::SamplerOverriddenState initialState;
1476
1477 info.fTarget = GR_GL_TEXTURE_2D;
1478 info.fFormat = GrGLFormatToEnum(glFormat);
1479 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001480 mipMapped, &initialState,
1481 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001482 if (!info.fID) {
1483 return {};
1484 }
1485
1486 // Unbind this texture from the scratch texture unit.
1487 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1488
1489 auto parameters = sk_make_sp<GrGLTextureParameters>();
1490 // The non-sampler params are still at their default values.
1491 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1492 fResetTimestampForTextureParameters);
1493
1494 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1495 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001496}
1497
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001498namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001499
egdaniel8dc7c3a2015-04-16 11:22:42 -07001500const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001501
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001502void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001503 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001504
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001505 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001506 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 (kUnknownBitCount == format->fTotalBits));
1508 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001509 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001510 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1511 (GrGLint*)&format->fStencilBits);
1512 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001513 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1515 (GrGLint*)&format->fTotalBits);
1516 format->fTotalBits += format->fStencilBits;
1517 } else {
1518 format->fTotalBits = format->fStencilBits;
1519 }
1520 }
1521}
1522}
1523
Brian Salomon5043f1f2019-07-11 21:27:54 -04001524int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001525 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001526 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1527
1528 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001529 // Default to unsupported, set this if we find a stencil format that works.
1530 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001531
Brian Salomond2a8ae22019-09-10 16:03:59 -04001532 GrGLuint colorID =
1533 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1534 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001535 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001536 }
egdanielff1d5472015-09-10 08:37:20 -07001537 // unbind the texture from the texture unit before binding it to the frame buffer
1538 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1539
1540 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001541 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001542 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001543 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001544 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001545 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1546 GR_GL_COLOR_ATTACHMENT0,
1547 GR_GL_TEXTURE_2D,
1548 colorID,
1549 0));
bsalomon30447372015-12-21 09:03:05 -08001550 GrGLuint sbRBID = 0;
1551 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001552
1553 // look over formats till I find a compatible one
1554 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001555 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001556 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001557 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1558 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001559 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1560 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1561 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001562 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001563 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001564 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001565 if (sFmt.fPacked) {
1566 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1567 GR_GL_DEPTH_ATTACHMENT,
1568 GR_GL_RENDERBUFFER, sbRBID));
1569 } else {
1570 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1571 GR_GL_DEPTH_ATTACHMENT,
1572 GR_GL_RENDERBUFFER, 0));
1573 }
1574 GrGLenum status;
1575 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1576 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1577 firstWorkingStencilFormatIndex = i;
1578 break;
1579 }
egdanielff1d5472015-09-10 08:37:20 -07001580 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1581 GR_GL_STENCIL_ATTACHMENT,
1582 GR_GL_RENDERBUFFER, 0));
1583 if (sFmt.fPacked) {
1584 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1585 GR_GL_DEPTH_ATTACHMENT,
1586 GR_GL_RENDERBUFFER, 0));
1587 }
egdanielff1d5472015-09-10 08:37:20 -07001588 }
1589 }
bsalomon30447372015-12-21 09:03:05 -08001590 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001591 }
1592 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001593 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1594 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001595 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001596 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001597 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001598}
1599
Brian Salomonea4ad302019-08-07 13:04:55 -04001600GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001601 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001602 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001603 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001604 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001605 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001606 if (format == GrGLFormat::kUnknown) {
1607 return 0;
1608 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001609 GrGLuint id = 0;
1610 GL_CALL(GenTextures(1, &id));
1611 if (!id) {
1612 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001613 }
1614
Brian Salomonea4ad302019-08-07 13:04:55 -04001615 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001616
Brian Salomonea4ad302019-08-07 13:04:55 -04001617 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1618
Robert Phillips42716d42019-12-16 12:19:54 -05001619 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001620 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1621 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001622 GL_CALL(DeleteTextures(1, &id));
1623 return 0;
1624 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001625 }
Robert Phillips42716d42019-12-16 12:19:54 -05001626
Brian Salomonea4ad302019-08-07 13:04:55 -04001627 return id;
1628}
1629
Robert Phillips2716daf2020-01-23 12:53:42 -05001630GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001631 GrGLFormat format,
1632 GrRenderable renderable,
1633 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001634 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001635 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001636 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001637
Brian Salomonea4ad302019-08-07 13:04:55 -04001638 GrGLuint id = 0;
1639 GL_CALL(GenTextures(1, &id));
1640
1641 if (!id) {
1642 return 0;
1643 }
1644
1645 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001646
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001647 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001648 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001649 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001650 }
1651
Brian Salomond2a8ae22019-09-10 16:03:59 -04001652 if (initialState) {
1653 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1654 } else {
1655 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001656 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001657
1658 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1659
1660 bool success = false;
1661 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001662 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001663 auto levelCount = std::max(mipLevelCount, 1);
1664 GrGLenum error =
1665 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1666 dimensions.width(), dimensions.height()));
1667 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001668 } else {
1669 GrGLenum externalFormat, externalType;
1670 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1671 GrGLenum error = GR_GL_NO_ERROR;
1672 if (externalFormat && externalType) {
1673 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1674 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001675 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1676 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001677 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1678 currentWidth, currentHeight, 0, externalFormat,
1679 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001680 }
Brian Salomond8575452020-02-25 12:13:29 -05001681 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001682 }
1683 }
1684 }
1685 if (success) {
1686 return id;
1687 }
1688 GL_CALL(DeleteTextures(1, &id));
1689 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001690}
1691
Chris Daltoneffee202019-07-01 22:28:03 -06001692GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1693 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001694 SkASSERT(width >= rt->width());
1695 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001696
egdaniel8dc7c3a2015-04-16 11:22:42 -07001697 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001698
Brian Salomond4764a12019-08-08 12:08:24 -04001699 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001700 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001701 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001702 }
egdanielff1d5472015-09-10 08:37:20 -07001703
1704 if (!sbDesc.fRenderbufferID) {
1705 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1706 }
1707 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001708 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001709 }
1710 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1711 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001712 // we do this "if" so that we don't call the multisample
1713 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001714 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001715 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1716 width, height)) {
1717 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1718 return nullptr;
1719 }
egdanielff1d5472015-09-10 08:37:20 -07001720 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001721 GrGLenum error = GL_ALLOC_CALL(
1722 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1723 if (error != GR_GL_NO_ERROR) {
1724 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1725 return nullptr;
1726 }
egdanielff1d5472015-09-10 08:37:20 -07001727 }
1728 fStats.incStencilAttachmentCreates();
1729 // After sized formats we attempt an unsized format and take
1730 // whatever sizes GL gives us. In that case we query for the size.
1731 GrGLStencilAttachment::Format format = sFmt;
1732 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001733 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1734 sbDesc,
1735 width,
1736 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001737 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001738 format);
1739 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001740}
1741
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001742////////////////////////////////////////////////////////////////////////////////
1743
Brian Salomondbf70722019-02-07 11:31:24 -05001744sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1745 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001746 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001747}
1748
Chris Dalton2e7ed262020-02-21 15:17:59 -07001749void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1750 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001751 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1752 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1753 fHWScissorSettings.fEnabled = kYes_TriState;
1754 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001755 } else {
1756 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1757 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1758 fHWScissorSettings.fEnabled = kNo_TriState;
1759 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001761}
Brian Salomond818ebf2018-07-02 14:08:49 +00001762
Chris Dalton2e7ed262020-02-21 15:17:59 -07001763void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1764 GrSurfaceOrigin rtOrigin) {
Chris Daltonb67081f2020-02-27 15:13:56 -07001765 SkASSERT(TriState::kYes_TriState == fHWScissorSettings.fEnabled);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001766 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1767 if (fHWScissorSettings.fRect != nativeScissor) {
1768 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1769 nativeScissor.fHeight));
1770 fHWScissorSettings.fRect = nativeScissor;
1771 }
joshualitt77b13072014-10-27 14:51:01 -07001772}
1773
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001774void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001775 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001776#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001777 typedef GrWindowRectsState::Mode Mode;
1778 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1779 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001780
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001781 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001782 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001783 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001784 }
1785
csmartdalton7535f412016-08-23 06:51:00 -07001786 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1787 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001788 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001789 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001790
Chris Daltond6cda8d2019-09-05 02:30:04 -06001791 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001792 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001793 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001794 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001795 }
1796
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001798 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001799
Greg Danielacd66b42019-05-22 16:29:12 -04001800 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001801#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001802}
1803
1804void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001805#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001806 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001807 return;
1808 }
1809 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001810 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001811#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001812}
1813
Chris Daltonb67081f2020-02-27 15:13:56 -07001814sk_sp<GrGLProgram> GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1815 const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001816 this->handleDirtyContext();
1817
Chris Daltonb67081f2020-02-27 15:13:56 -07001818 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
1819 if (!program) {
1820 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1821 return nullptr;
1822 }
1823
1824 this->flushProgram(program->programID());
1825
Chris Dalton4386ad12020-02-19 16:42:06 -07001826 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1827 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1828 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001829
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001830 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001831 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1832 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001833
Chris Daltonb67081f2020-02-27 15:13:56 -07001834 program->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001835
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001836 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001837 GrStencilSettings stencil;
1838 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001839 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1840 stencil.reset(*programInfo.pipeline().getUserStencil(),
1841 programInfo.pipeline().hasStencilClip(),
1842 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001843 }
Robert Phillips901aff02019-10-08 12:32:56 -04001844 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001845 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001846 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1847 glRT, programInfo.origin());
1848 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001849 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001850 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001851
1852 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001853 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001854 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001855
Chris Daltonb67081f2020-02-27 15:13:56 -07001856 return program;
Brian Salomon802cb312018-06-08 18:05:20 -04001857}
1858
1859void GrGLGpu::flushProgram(GrGLuint id) {
1860 SkASSERT(id);
1861 if (fHWProgramID == id) {
Brian Salomon802cb312018-06-08 18:05:20 -04001862 return;
1863 }
Brian Salomon802cb312018-06-08 18:05:20 -04001864 GL_CALL(UseProgram(id));
1865 fHWProgramID = id;
1866}
1867
Brian Salomonae64c192019-02-05 09:41:37 -05001868GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001869 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001870
cdaltone2e71c22016-04-07 18:13:29 -07001871 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001872 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001873 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001874 }
cdaltone2e71c22016-04-07 18:13:29 -07001875
Brian Salomonae64c192019-02-05 09:41:37 -05001876 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001877 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001878 if (!bufferState->fBufferZeroKnownBound) {
1879 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1880 bufferState->fBufferZeroKnownBound = true;
1881 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001882 }
Brian Salomondbf70722019-02-07 11:31:24 -05001883 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1884 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001885 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1886 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1887 bufferState->fBufferZeroKnownBound = false;
1888 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001889 }
1890
Brian Salomonae64c192019-02-05 09:41:37 -05001891 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001892}
Brian Salomond818ebf2018-07-02 14:08:49 +00001893
Brian Osman9a9baae2018-11-05 15:06:26 -05001894void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001895 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001896 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001897 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001898 SkASSERT(!this->caps()->performColorClearsAsDraws());
1899 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001900
Brian Salomon43f8bf02017-10-18 08:33:29 -04001901 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001902
Brian Salomon43f8bf02017-10-18 08:33:29 -04001903 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1904
Brian Salomond818ebf2018-07-02 14:08:49 +00001905 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001906 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001907 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001908 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001909 }
Greg Danielacd66b42019-05-22 16:29:12 -04001910 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001911 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001912 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001913 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001914 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001915}
1916
Robert Phillips95214472017-08-08 18:00:03 -04001917void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001918 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1919
Robert Phillips95214472017-08-08 18:00:03 -04001920 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001921 return;
1922 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001923
Chris Dalton674f77a2019-09-30 20:49:39 -06001924 // This should only be called internally when we know we have a stencil buffer.
1925 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001926
bsalomonb0bd4f62014-09-03 07:19:50 -07001927 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001928 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001929
Chris Dalton2e7ed262020-02-21 15:17:59 -07001930 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001931 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001932
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001933 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001934 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001935 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001936 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001937}
1938
Chris Daltonb50cc812019-10-14 16:29:21 -06001939static bool use_tiled_rendering(const GrGLCaps& glCaps,
1940 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1941 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1942 // Otherwise it's faster to just not use it.
1943 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1944 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1945}
1946
1947void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001948 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1949 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1950 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1951
1952 this->handleDirtyContext();
1953
1954 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1955 this->flushRenderTarget(glRT);
1956 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1957
Chris Daltonb50cc812019-10-14 16:29:21 -06001958 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1959 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1960 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1961 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1962 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1963 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1964 nativeBounds.fHeight, preserveMask));
1965 }
1966
Chris Dalton674f77a2019-09-30 20:49:39 -06001967 GrGLbitfield clearMask = 0;
1968 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1969 SkASSERT(!this->caps()->performColorClearsAsDraws());
1970 this->flushClearColor(colorLoadStore.fClearColor);
1971 this->flushColorWrite(true);
1972 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001973 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001974 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1975 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1976 GL_CALL(StencilMask(0xffffffff));
1977 GL_CALL(ClearStencil(0));
1978 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1979 }
1980 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001981 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001982 this->disableWindowRectangles();
1983 GL_CALL(Clear(clearMask));
1984 }
1985}
1986
1987void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1988 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1989 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1990 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1991
1992 this->handleDirtyContext();
1993
1994 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1995 // The framebuffer binding changed in the middle of a command buffer. We should have already
1996 // printed a warning during onFBOChanged.
1997 return;
1998 }
1999
2000 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2001 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2002
2003 SkSTArray<2, GrGLenum> discardAttachments;
2004 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2005 discardAttachments.push_back(
2006 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2007 }
2008 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2009 discardAttachments.push_back(
2010 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2011 }
2012
2013 if (!discardAttachments.empty()) {
2014 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2015 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2016 discardAttachments.begin()));
2017 } else {
2018 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2019 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2020 discardAttachments.begin()));
2021 }
2022 }
2023 }
2024
Chris Daltonb50cc812019-10-14 16:29:21 -06002025 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2026 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2027 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2028 // Handled by use_tiled_rendering().
2029 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2030 GL_CALL(EndTiling(preserveMask));
2031 }
2032
Chris Dalton674f77a2019-09-30 20:49:39 -06002033 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002034}
2035
csmartdalton29df7602016-08-31 11:55:52 -07002036void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2037 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002038 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002039 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002040 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002041 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002042
egdaniel8dc7c3a2015-04-16 11:22:42 -07002043 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002044 if (!sb) {
2045 // We should only get here if we marked a proxy as requiring a SB. However,
2046 // the SB creation could later fail. Likely clipping is going to go awry now.
2047 return;
2048 }
2049
bsalomon6bc1b5f2015-02-23 09:06:38 -08002050 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002051#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002052 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002053 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002054#else
2055 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002056 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002057 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002058 // changing the clip between stencil passes may or may not
2059 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002060 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002061#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002062 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002063 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002064 value = (1 << (stencilBitCount - 1));
2065 } else {
2066 value = 0;
2067 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002068 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002069 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002070
Greg Danielacd66b42019-05-22 16:29:12 -04002071 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002072 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002073
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002074 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002075 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002076 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002077 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002078}
2079
Brian Salomone05ba5a2019-04-08 11:59:07 -04002080bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002081 GrColorType surfaceColorType, GrColorType dstColorType,
2082 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002083 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002084
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002085 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002086 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002087 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002088 return false;
2089 }
Greg Danielba88ab62019-07-26 09:14:01 -04002090 GrGLenum externalFormat = 0;
2091 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002092 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2093 surfaceColorType,
2094 dstColorType,
2095 &externalFormat,
2096 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002097 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002098 return false;
2099 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002100
Brian Salomon71d9d842016-11-03 13:42:00 -04002101 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002102 if (renderTarget->numSamples() <= 1 ||
2103 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2104 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2105 this->flushRenderTargetNoColorWrites(renderTarget);
2106 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2107 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2108 return false;
2109 } else {
2110 SkASSERT(renderTarget->requiresManualMSAAResolve());
2111 // we don't track the state of the READ FBO ID.
2112 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002113 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002114 } else {
2115 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002116 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002117 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002118 }
2119
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002120 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002121 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002122
Brian Salomona6948702018-06-01 15:33:20 -04002123 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002124 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002125 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002126 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002127 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002128 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002129
Brian Osman1348ed02018-09-12 09:08:39 -04002130 bool reattachStencil = false;
2131 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2132 renderTarget &&
2133 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002134 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002135 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2136 reattachStencil = true;
2137 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2138 GR_GL_RENDERBUFFER, 0));
2139 }
2140
Chris Dalton76500e52019-09-05 02:13:05 -06002141 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002142 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002143
2144 if (reattachStencil) {
2145 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2146 renderTarget->renderTargetPriv().getStencilAttachment());
2147 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2148 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2149 }
2150
Brian Salomon26de56e2019-04-10 12:14:26 -04002151 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002152 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002153 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2154 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002155
Brian Salomone05ba5a2019-04-08 11:59:07 -04002156 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002157 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002158 }
2159 return true;
2160}
2161
2162bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002163 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2164 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002165 SkASSERT(surface);
2166
Brian Salomonb28cb682019-07-26 12:48:47 -04002167 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002168
Brian Salomon26de56e2019-04-10 12:14:26 -04002169 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2170 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002171
Brian Salomon1047a492019-07-02 12:25:21 -04002172 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002173 rowPixelWidth = width;
2174 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002175 SkASSERT(!(rowBytes % bytesPerPixel));
2176 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002177 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002178 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2179 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002180}
2181
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002182GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002183 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002184 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002185 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002186 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002187 if (!fCachedOpsRenderPass) {
2188 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002189 }
2190
Chris Daltonb50cc812019-10-14 16:29:21 -06002191 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002192 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002193}
2194
Brian Salomon1fabd512018-02-09 09:54:25 -05002195void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002196 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002197 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002198 this->didWriteToSurface(target, origin, &bounds);
2199}
bsalomon6ba6fa12015-03-04 11:57:37 -08002200
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002201void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2202 this->flushRenderTargetNoColorWrites(target);
2203 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002204}
2205
Brian Osman9aa30c62018-07-02 15:21:46 -04002206void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002207 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002208 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002209 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002210 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002211#ifdef SK_DEBUG
2212 // don't do this check in Chromium -- this is causing
2213 // lots of repeated command buffer flushes when the compositor is
2214 // rendering with Ganesh, which is really slow; even too slow for
2215 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002216 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002217 GrGLenum status;
2218 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2219 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2220 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2221 }
bsalomon160f24c2015-03-17 15:55:42 -07002222 }
egdanield803f272015-03-18 13:01:52 -07002223#endif
2224 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002225 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002226 }
2227
brianosman35b784d2016-05-05 11:52:53 -07002228 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002229 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002230 }
Brian Salomon9b553272020-01-24 14:38:37 -05002231
2232 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2233 GrGLint format;
2234 GrGLint type;
2235 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2236 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2237 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2238 }
bsalomon083617b2016-02-12 12:10:14 -08002239}
bsalomona9909122016-01-23 10:41:40 -08002240
brianosman33f6b3f2016-06-02 05:49:21 -07002241void GrGLGpu::flushFramebufferSRGB(bool enable) {
2242 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2243 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2244 fHWSRGBFramebuffer = kYes_TriState;
2245 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2246 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2247 fHWSRGBFramebuffer = kNo_TriState;
2248 }
2249}
2250
Greg Danielacd66b42019-05-22 16:29:12 -04002251void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002252 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002253 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002254 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002255 fHWViewport = viewport;
2256 }
2257}
2258
Chris Dalton33db9fc2020-02-25 18:52:12 -07002259GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltonb67081f2020-02-27 15:13:56 -07002260 fStats.incNumDraws();
2261
Chris Dalton2e7ed262020-02-21 15:17:59 -07002262 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2263 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2264 GL_CALL(Enable(GR_GL_CULL_FACE));
2265 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002266 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002267 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002268
Chris Dalton3809bab2017-06-13 10:55:06 -06002269 switch (primitiveType) {
2270 case GrPrimitiveType::kTriangles:
2271 return GR_GL_TRIANGLES;
2272 case GrPrimitiveType::kTriangleStrip:
2273 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002274 case GrPrimitiveType::kPoints:
2275 return GR_GL_POINTS;
2276 case GrPrimitiveType::kLines:
2277 return GR_GL_LINES;
2278 case GrPrimitiveType::kLineStrip:
2279 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002280 case GrPrimitiveType::kPatches:
2281 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002282 case GrPrimitiveType::kPath:
2283 SK_ABORT("non-mesh-based GrPrimitiveType");
2284 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002285 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002286 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002287}
2288
Chris Daltonb67081f2020-02-27 15:13:56 -07002289void GrGLGpu::drawArrays(GrPrimitiveType primitiveType, GrGLint baseVertex, GrGLsizei vertexCount) {
2290 SkASSERT(!this->glCaps().drawArraysBaseVertexIsBroken() || 0 == baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -07002291 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltonb67081f2020-02-27 15:13:56 -07002292 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
Chris Dalton114a3c02017-05-26 15:17:19 -06002293}
2294
Chris Daltonb67081f2020-02-27 15:13:56 -07002295void GrGLGpu::drawElements(GrPrimitiveType primitiveType, GrGLsizei indexCount, GrGLenum indexType,
2296 const void* indices) {
2297 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2298 GL_CALL(DrawElements(glPrimType, indexCount, indexType, indices));
Brian Salomondbf70722019-02-07 11:31:24 -05002299}
2300
Chris Daltonb67081f2020-02-27 15:13:56 -07002301void GrGLGpu::drawRangeElements(GrPrimitiveType primitiveType, GrGLuint minIndexValue,
2302 GrGLuint maxIndexValue, GrGLsizei indexCount, GrGLenum indexType,
2303 const void* indices) {
2304 SkASSERT(this->glCaps().drawRangeElementsSupport());
Chris Dalton33db9fc2020-02-25 18:52:12 -07002305 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltonb67081f2020-02-27 15:13:56 -07002306 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, indexType,
2307 indices));
Chris Dalton114a3c02017-05-26 15:17:19 -06002308}
2309
Chris Daltonb67081f2020-02-27 15:13:56 -07002310void GrGLGpu::drawArraysInstanced(GrPrimitiveType primitiveType, GrGLint baseVertex,
2311 GrGLsizei vertexCount, GrGLsizei instanceCount) {
2312 SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
Chris Dalton33db9fc2020-02-25 18:52:12 -07002313 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltonb67081f2020-02-27 15:13:56 -07002314 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
Chris Dalton1d616352017-05-31 12:51:23 -06002315}
2316
Chris Daltonb67081f2020-02-27 15:13:56 -07002317void GrGLGpu::drawElementsInstanced(GrPrimitiveType primitiveType, GrGLsizei indexCount,
2318 GrGLenum indexType, const void* indices,
2319 GrGLsizei instanceCount) {
2320 SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
Chris Dalton33db9fc2020-02-25 18:52:12 -07002321 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltonb67081f2020-02-27 15:13:56 -07002322 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, indexType, indices, instanceCount));
Chris Dalton1d616352017-05-31 12:51:23 -06002323}
2324
Chris Dalton16a33c62019-09-24 22:19:17 -06002325void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002326 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002327 // Some extensions automatically resolves the texture when it is read.
2328 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2329
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002330 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002331 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2332 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2333 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2334 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002335
Chris Daltonc139d082019-09-26 14:04:24 -06002336 // make sure we go through flushRenderTarget() since we've modified
2337 // the bound DRAW FBO ID.
2338 fHWBoundRenderTargetUniqueID.makeInvalid();
2339 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2340 // Apple's extension uses the scissor as the blit bounds.
2341 GrScissorState scissorState;
2342 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002343 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2344 // happens inside flushScissor since resolveRect is already in native device coordinates.
2345 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002346 this->disableWindowRectangles();
2347 GL_CALL(ResolveMultisampleFramebuffer());
2348 } else {
2349 int l, b, r, t;
2350 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2351 this->glCaps().blitFramebufferSupportFlags()) {
2352 l = 0;
2353 b = 0;
2354 r = target->width();
2355 t = target->height();
2356 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002357 l = resolveRect.x();
2358 b = resolveRect.y();
2359 r = resolveRect.x() + resolveRect.width();
2360 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002361 }
Chris Daltonc139d082019-09-26 14:04:24 -06002362
2363 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002364 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002365 this->disableWindowRectangles();
2366 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 +00002367 }
2368}
2369
bsalomon@google.com411dad02012-06-05 20:24:20 +00002370namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002371
bsalomon@google.com411dad02012-06-05 20:24:20 +00002372
2373GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002374 static const GrGLenum gTable[kGrStencilOpCount] = {
2375 GR_GL_KEEP, // kKeep
2376 GR_GL_ZERO, // kZero
2377 GR_GL_REPLACE, // kReplace
2378 GR_GL_INVERT, // kInvert
2379 GR_GL_INCR_WRAP, // kIncWrap
2380 GR_GL_DECR_WRAP, // kDecWrap
2381 GR_GL_INCR, // kIncClamp
2382 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002383 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002384 static_assert(0 == (int)GrStencilOp::kKeep);
2385 static_assert(1 == (int)GrStencilOp::kZero);
2386 static_assert(2 == (int)GrStencilOp::kReplace);
2387 static_assert(3 == (int)GrStencilOp::kInvert);
2388 static_assert(4 == (int)GrStencilOp::kIncWrap);
2389 static_assert(5 == (int)GrStencilOp::kDecWrap);
2390 static_assert(6 == (int)GrStencilOp::kIncClamp);
2391 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002392 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2393 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002394}
2395
2396void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002397 const GrStencilSettings::Face& face,
2398 GrGLenum glFace) {
2399 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2400 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2401 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002402
cdalton93a379b2016-05-11 13:58:08 -07002403 GrGLint ref = face.fRef;
2404 GrGLint mask = face.fTestMask;
2405 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002406
2407 if (GR_GL_FRONT_AND_BACK == glFace) {
2408 // we call the combined func just in case separate stencil is not
2409 // supported.
2410 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2411 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002412 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002413 } else {
2414 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2415 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002416 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002417 }
2418}
2419}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002420
Chris Dalton71713f62019-04-18 12:41:03 -06002421void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002422 if (stencilSettings.isDisabled()) {
2423 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002424 } else if (fHWStencilSettings != stencilSettings ||
2425 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002426 if (kYes_TriState != fHWStencilTestEnabled) {
2427 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002428
csmartdaltonc7d85332016-10-26 10:13:46 -07002429 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002430 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002431 if (!stencilSettings.isTwoSided()) {
2432 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2433 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002434 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002435 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2436 GR_GL_FRONT);
2437 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2438 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002439 }
joshualitta58fe352014-10-27 08:39:00 -07002440 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002441 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002442 }
2443}
2444
csmartdaltonc7d85332016-10-26 10:13:46 -07002445void GrGLGpu::disableStencil() {
2446 if (kNo_TriState != fHWStencilTestEnabled) {
2447 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002448
csmartdaltonc7d85332016-10-26 10:13:46 -07002449 fHWStencilTestEnabled = kNo_TriState;
2450 fHWStencilSettings.invalidate();
2451 }
2452}
2453
Chris Dalton4c56b032019-03-04 12:28:42 -07002454void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002455 // rt is only optional if useHWAA is false.
2456 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002457#ifdef SK_DEBUG
2458 if (useHWAA && rt->numSamples() <= 1) {
2459 SkASSERT(this->caps()->mixedSamplesSupport());
2460 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2461 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2462 }
2463#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002464
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002465 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002466 if (useHWAA) {
2467 if (kYes_TriState != fMSAAEnabled) {
2468 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2469 fMSAAEnabled = kYes_TriState;
2470 }
2471 } else {
2472 if (kNo_TriState != fMSAAEnabled) {
2473 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2474 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002475 }
2476 }
2477 }
2478}
2479
Chris Daltonce425af2019-12-16 10:39:03 -07002480void GrGLGpu::flushConservativeRasterState(bool enabled) {
2481 if (this->caps()->conservativeRasterSupport()) {
2482 if (enabled) {
2483 if (kYes_TriState != fHWConservativeRasterEnabled) {
2484 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2485 fHWConservativeRasterEnabled = kYes_TriState;
2486 }
2487 } else {
2488 if (kNo_TriState != fHWConservativeRasterEnabled) {
2489 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2490 fHWConservativeRasterEnabled = kNo_TriState;
2491 }
2492 }
2493 }
2494}
2495
Chris Dalton1215cda2019-12-17 21:44:04 -07002496void GrGLGpu::flushWireframeState(bool enabled) {
2497 if (this->caps()->wireframeSupport()) {
2498 if (this->caps()->wireframeMode() || enabled) {
2499 if (kYes_TriState != fHWWireframeEnabled) {
2500 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2501 fHWWireframeEnabled = kYes_TriState;
2502 }
2503 } else {
2504 if (kNo_TriState != fHWWireframeEnabled) {
2505 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2506 fHWWireframeEnabled = kNo_TriState;
2507 }
2508 }
2509 }
2510}
2511
Chris Daltonbbb3f642019-07-24 12:25:08 -04002512void GrGLGpu::flushBlendAndColorWrite(
2513 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2514 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2515 // We need to work around a driver bug by using a blend state that preserves the dst color,
2516 // rather than disabling color writes.
2517 GrXferProcessor::BlendInfo preserveDstBlend;
2518 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2519 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2520 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2521 return;
2522 }
bsalomonf7cc8772015-05-11 11:21:14 -07002523
cdalton8917d622015-05-06 13:40:21 -07002524 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002525 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2526 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002527
2528 // Any optimization to disable blending should have already been applied and
2529 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002530 bool blendOff =
2531 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2532 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2533 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002534
egdanielb414f252014-07-29 13:15:47 -07002535 if (blendOff) {
2536 if (kNo_TriState != fHWBlendState.fEnabled) {
2537 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002538
2539 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2540 // https://code.google.com/p/skia/issues/detail?id=3943
2541 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2542 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2543 SkASSERT(this->caps()->advancedBlendEquationSupport());
2544 // Set to any basic blending equation.
2545 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2546 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2547 fHWBlendState.fEquation = blend_equation;
2548 }
2549
egdanielb414f252014-07-29 13:15:47 -07002550 fHWBlendState.fEnabled = kNo_TriState;
2551 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002552 } else {
2553 if (kYes_TriState != fHWBlendState.fEnabled) {
2554 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002555
Chris Daltonbbb3f642019-07-24 12:25:08 -04002556 fHWBlendState.fEnabled = kYes_TriState;
2557 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002558
Chris Daltonbbb3f642019-07-24 12:25:08 -04002559 if (fHWBlendState.fEquation != equation) {
2560 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2561 fHWBlendState.fEquation = equation;
2562 }
cdalton8917d622015-05-06 13:40:21 -07002563
Chris Daltonbbb3f642019-07-24 12:25:08 -04002564 if (GrBlendEquationIsAdvanced(equation)) {
2565 SkASSERT(this->caps()->advancedBlendEquationSupport());
2566 // Advanced equations have no other blend state.
2567 return;
2568 }
cdalton8917d622015-05-06 13:40:21 -07002569
Chris Daltonbbb3f642019-07-24 12:25:08 -04002570 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2571 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2572 gXfermodeCoeff2Blend[dstCoeff]));
2573 fHWBlendState.fSrcCoeff = srcCoeff;
2574 fHWBlendState.fDstCoeff = dstCoeff;
2575 }
cdalton8917d622015-05-06 13:40:21 -07002576
Chris Daltonbbb3f642019-07-24 12:25:08 -04002577 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2578 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2579 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2580 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2581 fHWBlendState.fConstColor = blendConst;
2582 fHWBlendState.fConstColorValid = true;
2583 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002584 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002585 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002586
2587 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002588}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002589
Brian Salomondc829942018-10-23 16:07:24 -04002590static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002591 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002592 switch (swizzle[i]) {
2593 case 'r': glValues[i] = GR_GL_RED; break;
2594 case 'g': glValues[i] = GR_GL_GREEN; break;
2595 case 'b': glValues[i] = GR_GL_BLUE; break;
2596 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002597 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002598 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002599 default: SK_ABORT("Unsupported component");
2600 }
Brian Salomon327955b2018-10-22 15:39:22 +00002601 }
2602}
2603
Greg Daniel2c3398d2019-06-19 11:58:01 -04002604void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2605 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002606 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002607
reed856e9d92015-09-30 12:21:45 -07002608#ifdef SK_DEBUG
2609 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002610 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002611 const int w = texture->width();
2612 const int h = texture->height();
2613 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2614 }
2615 }
2616#endif
2617
Robert Phillips294870f2016-11-11 12:38:40 -05002618 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002619 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002620 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002621 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002622 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002623 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002624 }
2625
Brian Salomondc829942018-10-23 16:07:24 -04002626 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002627 if (!this->caps()->mipMapSupport() ||
2628 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002629 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002630 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002631 }
bsalomonefd7d452014-10-23 14:17:46 -07002632
brianosman33f6b3f2016-06-02 05:49:21 -07002633#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002634 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002635 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002636 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002637 }
2638#endif
2639
Brian Salomone2826ab2019-06-04 15:58:31 -04002640 auto timestamp = texture->parameters()->resetTimestamp();
2641 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002642
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2644 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002645 if (fSamplerObjectCache) {
2646 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2647 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2649 texture->parameters()->samplerOverriddenState();
2650 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002651
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2653 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002654
Brian Salomone2826ab2019-06-04 15:58:31 -04002655 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2656 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002657
2658 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002659 newSamplerState.fMinLOD = -1000.f;
2660 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002661
Brian Salomone2826ab2019-06-04 15:58:31 -04002662 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002663 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002664 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002665 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002666 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002667 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002668 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002669 }
Mike Klein1bccae52018-10-19 11:38:44 +00002670 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002671 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002672 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002673 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002674 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002675 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002676 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002677 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002678 }
2679 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002680 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002681 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002682 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002683 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002684 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002685 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002686 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002687 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002688 if (this->glCaps().clampToBorderSupport()) {
2689 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002690 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002691 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002692 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2693 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002694 }
2695 }
Brian Salomondc829942018-10-23 16:07:24 -04002696 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002697 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2698 newNonsamplerState.fBaseMipMapLevel = 0;
2699 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002700
Brian Salomone2826ab2019-06-04 15:58:31 -04002701 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2702 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002703 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002704 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2705 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002706 GrGLenum glValues[4];
2707 get_gl_swizzle_values(swizzle, glValues);
2708 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002709 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002710 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002711 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2712 reinterpret_cast<const GrGLint*>(glValues)));
2713 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002714 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2715 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2716 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2717 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2718 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002719 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002720 }
2721 }
Brian Salomondc829942018-10-23 16:07:24 -04002722 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002723 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2724 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2725 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002726 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002727 this->setTextureUnit(unitIdx);
2728 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002729 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002730 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002731 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002732 this->setTextureUnit(unitIdx);
2733 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002734 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002735 }
Brian Salomon327955b2018-10-22 15:39:22 +00002736 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002737 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2738 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002739}
2740
Brian Salomon1f05d452019-02-08 12:33:08 -05002741void GrGLGpu::onResetTextureBindings() {
2742 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2743 GR_GL_TEXTURE_EXTERNAL};
2744 for (int i = 0; i < this->numTextureUnits(); ++i) {
2745 this->setTextureUnit(i);
2746 for (auto target : kTargets) {
2747 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2748 GL_CALL(BindTexture(target, 0));
2749 }
2750 }
2751 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2752 }
2753}
2754
Chris Dalton5a2f9622019-12-27 14:56:38 -07002755void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2756 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2757 if (fHWPatchVertexCount != count) {
2758 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2759 fHWPatchVertexCount = count;
2760 }
2761}
2762
egdaniel080e6732014-12-22 07:35:52 -08002763void GrGLGpu::flushColorWrite(bool writeColor) {
2764 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002765 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002766 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2767 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002768 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002769 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002770 } else {
2771 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002772 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002773 fHWWriteToColor = kYes_TriState;
2774 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002775 }
bsalomon3e791242014-12-17 13:43:13 -08002776}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002777
Chris Dalton674f77a2019-09-30 20:49:39 -06002778void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2779 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2780 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2781 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2782 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2783 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2784 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2785 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002786 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2787 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2788 GL_CALL(ClearColor(r, g, b, a));
2789 fHWClearColor[0] = r;
2790 fHWClearColor[1] = g;
2791 fHWClearColor[2] = b;
2792 fHWClearColor[3] = a;
2793 }
2794}
2795
bsalomon861e1032014-12-16 07:33:49 -08002796void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002797 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002798 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002799 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002800 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002801 }
2802}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002803
Brian Salomond978b902019-02-07 15:09:18 -05002804void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002805 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002806 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002807 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2808 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2809 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002810 }
Brian Salomond978b902019-02-07 15:09:18 -05002811 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2812 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002813 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002814 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002815}
2816
Brian Salomone5e7eb12016-10-14 16:18:33 -04002817// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002818static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2819 const GrSurface* src,
2820 const SkIRect& srcRect,
2821 const SkIPoint& dstPoint,
2822 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002823 int dstSampleCnt = 0;
2824 int srcSampleCnt = 0;
2825 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002826 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002827 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002828 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002829 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002830 }
2831 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2832 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2833
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002834 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2835 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2836
Brian Salomone5e7eb12016-10-14 16:18:33 -04002837 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002838 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002839
Greg Daniel46cfbc62019-06-07 11:43:30 -04002840 GrTextureType dstTexType;
2841 GrTextureType* dstTexTypePtr = nullptr;
2842 GrTextureType srcTexType;
2843 GrTextureType* srcTexTypePtr = nullptr;
2844 if (dstTex) {
2845 dstTexType = dstTex->texturePriv().textureType();
2846 dstTexTypePtr = &dstTexType;
2847 }
2848 if (srcTex) {
2849 srcTexType = srcTex->texturePriv().textureType();
2850 srcTexTypePtr = &srcTexType;
2851 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002852
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002853 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2854 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002855 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002856}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002857
Brian Osmana9c8a052018-01-19 10:31:56 -05002858static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2859 // A RT has a separate MSAA renderbuffer if:
2860 // 1) It's multisampled
2861 // 2) We're using an extension with separate MSAA renderbuffers
2862 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002863 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002864}
2865
Greg Daniel46cfbc62019-06-07 11:43:30 -04002866static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002867 const GrGLCaps& caps) {
2868
bsalomon@google.comeb851172013-04-15 13:51:00 +00002869 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002870 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002871 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002872 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002873
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002874 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2875 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2876
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002877 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2878 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2879
Greg Daniel46cfbc62019-06-07 11:43:30 -04002880 GrTextureType dstTexType;
2881 GrTextureType* dstTexTypePtr = nullptr;
2882 GrTextureType srcTexType;
2883 GrTextureType* srcTexTypePtr = nullptr;
2884 if (dstTex) {
2885 dstTexType = dstTex->texturePriv().textureType();
2886 dstTexTypePtr = &dstTexType;
2887 }
2888 if (srcTex) {
2889 srcTexType = srcTex->texturePriv().textureType();
2890 srcTexTypePtr = &srcTexType;
2891 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002892
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002893 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2894 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002895}
2896
Greg Danielacd66b42019-05-22 16:29:12 -04002897// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002898void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002899 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002900 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002901 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002902 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002903 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2904 GrGLuint texID = texture->textureID();
2905 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002906 GrGLuint* tempFBOID;
2907 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002908
egdanield803f272015-03-18 13:01:52 -07002909 if (0 == *tempFBOID) {
2910 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002911 }
2912
Adrienne Walker4ee88512018-05-17 11:37:14 -07002913 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002914 GR_GL_CALL(
2915 this->glInterface(),
2916 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2917 if (mipLevel == 0) {
2918 texture->baseLevelWasBoundToFBO();
2919 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002920 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002921 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002922 }
egdaniel0f5f9672015-02-03 11:10:51 -08002923}
2924
Brian Salomond2a8ae22019-09-10 16:03:59 -04002925void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002926 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002927 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002928 SkASSERT(surface->asTexture());
2929 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2930 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2931 GR_GL_COLOR_ATTACHMENT0,
2932 textureTarget,
2933 0,
2934 0));
2935 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002936}
2937
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002938void GrGLGpu::onFBOChanged() {
2939 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2940 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2941 GL_CALL(Flush());
2942 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002943#ifdef SK_DEBUG
2944 if (fIsExecutingCommandBuffer_DebugOnly) {
2945 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2946 "This will severely hurt performance.\n");
2947 }
2948#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002949}
2950
Adrienne Walker4ee88512018-05-17 11:37:14 -07002951void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2952 fStats.incRenderTargetBinds();
2953 GL_CALL(BindFramebuffer(target, fboid));
2954 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2955 fBoundDrawFramebuffer = fboid;
2956 }
2957
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002958 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2959 // The driver forgets the correct scissor when modifying the FBO binding.
2960 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002961 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002962 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002963 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002964 }
2965
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002966 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002967}
2968
Adrienne Walker4ee88512018-05-17 11:37:14 -07002969void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002970 // We're relying on the GL state shadowing being correct in the workaround code below so we
2971 // need to handle a dirty context.
2972 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002973 if (fboid == fBoundDrawFramebuffer &&
2974 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2975 // This workaround only applies to deleting currently bound framebuffers
2976 // on Adreno 420. Because this is a somewhat rare case, instead of
2977 // tracking all the attachments of every framebuffer instead just always
2978 // unbind all attachments.
2979 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2980 GR_GL_RENDERBUFFER, 0));
2981 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2982 GR_GL_RENDERBUFFER, 0));
2983 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2984 GR_GL_RENDERBUFFER, 0));
2985 }
2986
2987 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002988
2989 // Deleting the currently bound framebuffer rebinds to 0.
2990 if (fboid == fBoundDrawFramebuffer) {
2991 this->onFBOChanged();
2992 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002993}
2994
Greg Daniel46cfbc62019-06-07 11:43:30 -04002995bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002996 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002997 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2998 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2999 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003000 auto dstFormat = dst->backendFormat().asGLFormat();
3001 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003002 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003003 return true;
3004 }
3005 }
cblume61214052016-01-26 09:10:48 -08003006
Greg Daniel46cfbc62019-06-07 11:43:30 -04003007 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3008 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003009 return true;
3010 }
3011
Greg Daniel46cfbc62019-06-07 11:43:30 -04003012 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3013 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003014 }
3015
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003016 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003017 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003018 return true;
3019 }
bsalomon083617b2016-02-12 12:10:14 -08003020 }
3021
bsalomon6df86402015-06-01 10:41:49 -07003022 return false;
3023}
3024
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003025bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003026 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003027
3028 int progIdx = TextureToCopyProgramIdx(srcTex);
3029 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3030 GrSLType samplerType =
3031 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3032
3033 if (!fCopyProgramArrayBuffer) {
3034 static const GrGLfloat vdata[] = {
3035 0, 0,
3036 0, 1,
3037 1, 0,
3038 1, 1
3039 };
3040 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3041 kStatic_GrAccessPattern, vdata);
3042 }
3043 if (!fCopyProgramArrayBuffer) {
3044 return false;
3045 }
3046
3047 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3048 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3049 if (!fCopyPrograms[progIdx].fProgram) {
3050 return false;
3051 }
3052
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003053 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3054 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3055 GrShaderVar::kUniform_TypeModifier);
3056 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3057 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3058 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3059 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3060
Greg Daniel9e3169b2019-06-06 14:38:17 -04003061 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003062 if (shaderCaps->noperspectiveInterpolationSupport()) {
3063 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3064 vshaderTxt.appendf("#extension %s : require\n", extension);
3065 }
3066 vTexCoord.addModifier("noperspective");
3067 }
3068
3069 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3070 vshaderTxt.append(";");
3071 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3072 vshaderTxt.append(";");
3073 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3074 vshaderTxt.append(";");
3075 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3076 vshaderTxt.append(";");
3077
3078 vshaderTxt.append(
3079 "// Copy Program VS\n"
3080 "void main() {"
3081 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3082 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3083 " sk_Position.zw = half2(0, 1);"
3084 "}"
3085 );
3086
Greg Daniel9e3169b2019-06-06 14:38:17 -04003087 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003088 if (shaderCaps->noperspectiveInterpolationSupport()) {
3089 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3090 fshaderTxt.appendf("#extension %s : require\n", extension);
3091 }
3092 }
3093 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3094 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3095 fshaderTxt.append(";");
3096 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3097 fshaderTxt.append(";");
3098 fshaderTxt.appendf(
3099 "// Copy Program FS\n"
3100 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003101 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003102 "}"
3103 );
3104
3105 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3106 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3107 SkSL::Program::Settings settings;
3108 settings.fCaps = shaderCaps;
3109 SkSL::String glsl;
3110 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3111 sksl, settings, &glsl, errorHandler);
3112 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3113 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3114 SkASSERT(program->fInputs.isEmpty());
3115
3116 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3117 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3118 errorHandler);
3119 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3120 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3121 errorHandler);
3122 SkASSERT(program->fInputs.isEmpty());
3123
3124 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3125
3126 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3127 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3128 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3129 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3130 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3131 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3132
3133 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3134
3135 GL_CALL(DeleteShader(vshader));
3136 GL_CALL(DeleteShader(fshader));
3137
3138 return true;
3139}
3140
brianosman33f6b3f2016-06-02 05:49:21 -07003141bool GrGLGpu::createMipmapProgram(int progIdx) {
3142 const bool oddWidth = SkToBool(progIdx & 0x2);
3143 const bool oddHeight = SkToBool(progIdx & 0x1);
3144 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3145
Brian Salomon1edc5b92016-11-29 13:43:46 -05003146 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003147
3148 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3149 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3150 if (!fMipmapPrograms[progIdx].fProgram) {
3151 return false;
3152 }
3153
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003154 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3155 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003156 GrShaderVar::kUniform_TypeModifier);
3157 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3158 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003159 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003160 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003161 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3162 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3163 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3164 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003165 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003166 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003167
Ethan Nicholasfc994162019-06-06 10:04:27 -04003168 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003169 if (shaderCaps->noperspectiveInterpolationSupport()) {
3170 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003171 vshaderTxt.appendf("#extension %s : require\n", extension);
3172 }
3173 vTexCoords[0].addModifier("noperspective");
3174 vTexCoords[1].addModifier("noperspective");
3175 vTexCoords[2].addModifier("noperspective");
3176 vTexCoords[3].addModifier("noperspective");
3177 }
3178
Brian Salomon1edc5b92016-11-29 13:43:46 -05003179 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003180 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003181 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003182 vshaderTxt.append(";");
3183 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003184 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003185 vshaderTxt.append(";");
3186 }
3187
3188 vshaderTxt.append(
3189 "// Mipmap Program VS\n"
3190 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003191 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3192 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003193 );
3194
3195 // Insert texture coordinate computation:
3196 if (oddWidth && oddHeight) {
3197 vshaderTxt.append(
3198 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003199 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3200 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003201 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3202 );
3203 } else if (oddWidth) {
3204 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003205 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3206 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003207 );
3208 } else if (oddHeight) {
3209 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003210 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3211 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003212 );
3213 } else {
3214 vshaderTxt.append(
3215 " v_texCoord0 = a_vertex.xy;"
3216 );
3217 }
3218
3219 vshaderTxt.append("}");
3220
Ethan Nicholasfc994162019-06-06 10:04:27 -04003221 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003222 if (shaderCaps->noperspectiveInterpolationSupport()) {
3223 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003224 fshaderTxt.appendf("#extension %s : require\n", extension);
3225 }
3226 }
brianosman33f6b3f2016-06-02 05:49:21 -07003227 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003228 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003229 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003230 fshaderTxt.append(";");
3231 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003232 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003233 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003234 fshaderTxt.append(
3235 "// Mipmap Program FS\n"
3236 "void main() {"
3237 );
3238
3239 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003240 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003241 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3242 " sample(u_texture, v_texCoord1) + "
3243 " sample(u_texture, v_texCoord2) + "
3244 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003245 );
3246 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003247 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003248 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3249 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003250 );
3251 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003252 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003253 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003254 );
3255 }
3256
3257 fshaderTxt.append("}");
3258
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003259 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003260 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003261 SkSL::Program::Settings settings;
3262 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003263 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003264 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003265 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003266 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003267 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003268 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003269
Brian Osman6c431d52019-04-15 16:31:54 -04003270 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003271 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3272 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003273 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003274 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003275 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003276 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003277
3278 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3279
3280 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3281 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3282 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3283 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3284
3285 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3286
3287 GL_CALL(DeleteShader(vshader));
3288 GL_CALL(DeleteShader(fshader));
3289
3290 return true;
3291}
3292
Greg Daniel46cfbc62019-06-07 11:43:30 -04003293bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003295 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3296 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3297 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3298 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003299 return false;
3300 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003301 int progIdx = TextureToCopyProgramIdx(srcTex);
3302 if (!dstRT) {
3303 SkASSERT(dstTex);
3304 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3305 return false;
3306 }
3307 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003308 if (!fCopyPrograms[progIdx].fProgram) {
3309 if (!this->createCopyProgram(srcTex)) {
3310 SkDebugf("Failed to create copy program.\n");
3311 return false;
3312 }
3313 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003314 int w = srcRect.width();
3315 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003316 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003317 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003318 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003319 this->flushViewport(dst->width(), dst->height());
3320 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003321 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003322 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003323 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003324 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3325 attribs->enableVertexArrays(this, 1);
3326 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3327 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003328 // dst rect edges in NDC (-1 to 1)
3329 int dw = dst->width();
3330 int dh = dst->height();
3331 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3332 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3333 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3334 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003335 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3336 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3337 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3338 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3339 int sw = src->width();
3340 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003341 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3342 // src rect edges in normalized texture space (0 to 1)
3343 sx0 /= sw;
3344 sx1 /= sw;
3345 sy0 /= sh;
3346 sy1 /= sh;
3347 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003348 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3349 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3350 sx1 - sx0, sy1 - sy0, sx0, sy0));
3351 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003352 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003353 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003354 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003355 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003356 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003357 this->disableWindowRectangles();
3358 this->disableStencil();
3359 if (this->glCaps().srgbWriteControl()) {
3360 this->flushFramebufferSRGB(true);
3361 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003362 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003363 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003364 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3365 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003366 return true;
3367}
3368
Greg Daniel46cfbc62019-06-07 11:43:30 -04003369void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003370 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003371 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003372 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003373 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003374 SkASSERT(dstTex);
3375 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003376 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003377
Brian Salomond978b902019-02-07 15:09:18 -05003378 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003379 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003380 dstPoint.fX, dstPoint.fY,
3381 srcRect.fLeft, srcRect.fTop,
3382 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003383 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003384 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3385 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003386 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3387 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003388}
3389
Greg Daniel46cfbc62019-06-07 11:43:30 -04003390bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003391 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003392 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003393 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3394 srcRect.width(), srcRect.height());
3395 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003396 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003397 return false;
mtklein404b3b22015-05-18 09:29:10 -07003398 }
bsalomon5df6fee2015-05-18 06:26:15 -07003399 }
bsalomon6df86402015-06-01 10:41:49 -07003400
Brian Salomond2a8ae22019-09-10 16:03:59 -04003401 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3402 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003403 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003404 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003405
3406 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003407 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003408 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003409
Greg Daniel46cfbc62019-06-07 11:43:30 -04003410 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3411 srcRect.fTop,
3412 srcRect.fRight,
3413 srcRect.fBottom,
3414 dstRect.fLeft,
3415 dstRect.fTop,
3416 dstRect.fRight,
3417 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003418 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003419 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3420 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003421
3422 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3423 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003424 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003425}
3426
Brian Salomon930f9392018-06-20 16:25:26 -04003427bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3428 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003429 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003430 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003431 return false;
3432 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003433 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003434 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3435 // Uses draw calls to do a series of downsample operations to successive mips.
3436
3437 // The manual approach requires the ability to limit which level we're sampling and that the
3438 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003439 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003440 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003441 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003442 GL_CALL(GenerateMipmap(glTex->target()));
3443 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003444 }
3445
brianosman33f6b3f2016-06-02 05:49:21 -07003446 int width = texture->width();
3447 int height = texture->height();
3448 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003449 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003450
3451 // Create (if necessary), then bind temporary FBO:
3452 if (0 == fTempDstFBOID) {
3453 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3454 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003455 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003456 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003457
3458 // Bind the texture, to get things configured for filtering.
3459 // We'll be changing our base level further below:
3460 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003461 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003462 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003463
3464 // Vertex data:
3465 if (!fMipmapProgramArrayBuffer) {
3466 static const GrGLfloat vdata[] = {
3467 0, 0,
3468 0, 1,
3469 1, 0,
3470 1, 1
3471 };
Brian Salomonae64c192019-02-05 09:41:37 -05003472 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003473 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003474 }
3475 if (!fMipmapProgramArrayBuffer) {
3476 return false;
3477 }
3478
3479 fHWVertexArrayState.setVertexArrayID(this, 0);
3480
3481 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003482 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003483 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003484 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003485
3486 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003487 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003488 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003489 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003490 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003491 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003492
3493 // Do all the blits:
3494 width = texture->width();
3495 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003496
brianosman33f6b3f2016-06-02 05:49:21 -07003497 for (GrGLint level = 1; level < levelCount; ++level) {
3498 // Get and bind the program for this particular downsample (filter shape can vary):
3499 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3500 if (!fMipmapPrograms[progIdx].fProgram) {
3501 if (!this->createMipmapProgram(progIdx)) {
3502 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003503 // Invalidate all params to cover base level change in a previous iteration.
3504 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003505 return false;
3506 }
3507 }
Brian Salomon802cb312018-06-08 18:05:20 -04003508 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003509
3510 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3511 const float invWidth = 1.0f / width;
3512 const float invHeight = 1.0f / height;
3513 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3514 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3515 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3516
3517 // Only sample from previous mip
3518 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3519
Brian Salomon930f9392018-06-20 16:25:26 -04003520 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3521 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003522
Brian Osman788b9162020-02-07 10:36:46 -05003523 width = std::max(1, width / 2);
3524 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003525 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003526
3527 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3528 }
3529
3530 // Unbind:
3531 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3532 GR_GL_TEXTURE_2D, 0, 0));
3533
Brian Salomon930f9392018-06-20 16:25:26 -04003534 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003535 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3536 // We drew the 2nd to last level into the last level.
3537 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3538 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003539
brianosman33f6b3f2016-06-02 05:49:21 -07003540 return true;
3541}
3542
Chris Daltond7291ba2019-03-07 14:17:03 -07003543void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003544 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3545 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003546
3547 int effectiveSampleCnt;
3548 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003549 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003550
3551 sampleLocations->reset(effectiveSampleCnt);
3552 for (int i = 0; i < effectiveSampleCnt; ++i) {
3553 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3554 }
3555}
3556
cdalton231c5fd2015-05-13 12:35:36 -07003557void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003558 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003559 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003560 case kTexture_GrXferBarrierType: {
3561 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003562 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003563 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3564 // The render target uses separate storage so no need for glTextureBarrier.
3565 // FIXME: The render target will resolve automatically when its texture is bound,
3566 // but we could resolve only the bounds that will be read if we do it here instead.
3567 return;
3568 }
cdalton9954bc32015-04-29 14:17:00 -07003569 SkASSERT(this->caps()->textureBarrierSupport());
3570 GL_CALL(TextureBarrier());
3571 return;
cdalton231c5fd2015-05-13 12:35:36 -07003572 }
cdalton8917d622015-05-06 13:40:21 -07003573 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003574 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003575 this->caps()->blendEquationSupport());
3576 GL_CALL(BlendBarrier());
3577 return;
bsalomoncb02b382015-08-12 11:14:50 -07003578 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003579 }
3580}
3581
Chris Daltone1196c52019-12-28 14:31:09 -07003582void GrGLGpu::insertManualFramebufferBarrier() {
3583 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3584 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3585}
3586
Brian Salomon85c3d682019-11-04 15:04:54 -05003587GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003588 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003589 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003590 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003591 GrProtected isProtected,
3592 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003593 // We don't support protected textures in GL.
3594 if (isProtected == GrProtected::kYes) {
3595 return {};
3596 }
3597
Brian Salomon8a375832018-03-14 10:21:40 -04003598 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003599
Brian Salomond4764a12019-08-08 12:08:24 -04003600 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003601 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003602 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003603 }
3604
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003605 int numMipLevels = 1;
3606 if (mipMapped == GrMipMapped::kYes) {
3607 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3608 }
3609
Robert Phillipsd34691b2019-09-24 13:38:43 -04003610 // Compressed formats go through onCreateCompressedBackendTexture
3611 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3612
Greg Daniel15500642019-06-27 03:05:55 +00003613 GrGLTextureInfo info;
3614 GrGLTextureParameters::SamplerOverriddenState initialState;
3615
Greg Danield51fa2f2020-01-22 16:53:38 -05003616 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003617 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003618 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003619
Robert Phillipsd34691b2019-09-24 13:38:43 -04003620 info.fTarget = GR_GL_TEXTURE_2D;
3621 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003622 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003623 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003624 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003625 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003626
Robert Phillips42716d42019-12-16 12:19:54 -05003627 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003628 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3629 SkTDArray<GrMipLevel> texels;
3630 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003631 texels.append(numMipLevels);
3632 for (int i = 0; i < numMipLevels; ++i) {
3633 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3634 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003635 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3636 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3637 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003638 GL_CALL(DeleteTextures(1, &info.fID));
3639 return {};
3640 }
3641 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3642 // TODO: Unify this with the clear texture code in onCreateTexture().
3643 GrColorType colorType;
3644 GrGLenum externalFormat, externalType;
3645 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3646 &externalType, &colorType);
3647 if (colorType == GrColorType::kUnknown) {
3648 GL_CALL(DeleteTextures(1, &info.fID));
3649 return {};
3650 }
3651
3652 // Make one tight image at the base size and reuse it for smaller levels.
3653 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3654 auto rb = ii.minRowBytes();
3655 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3656 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3657 GL_CALL(DeleteTextures(1, &info.fID));
3658 return {};
3659 }
3660
3661 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3662 SkISize levelDimensions = dimensions;
3663 for (int i = 0; i < numMipLevels; ++i) {
3664 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3665 levelDimensions.height(), externalFormat, externalType,
3666 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003667 levelDimensions = {std::max(1, levelDimensions.width() /2),
3668 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003669 }
3670 }
3671 // Unbind this texture from the scratch texture unit.
3672 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003673
Brian Salomone2826ab2019-06-04 15:58:31 -04003674 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003675 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003676 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3677 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003678
Brian Salomon85c3d682019-11-04 15:04:54 -05003679 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3680 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003681}
3682
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003683void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003684 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3685
3686 GrGLTextureInfo info;
3687 if (tex.getGLTextureInfo(&info)) {
3688 GL_CALL(DeleteTextures(1, &info.fID));
3689 }
3690}
3691
3692#if GR_TEST_UTILS
3693
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003694bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003695 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003696
Greg Daniel52e16d92018-04-10 09:34:07 -04003697 GrGLTextureInfo info;
3698 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003699 return false;
3700 }
3701
3702 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003703 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003704
3705 return (GR_GL_TRUE == result);
3706}
3707
Brian Salomonf865b052018-03-09 09:01:53 -05003708GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003709 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003710 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3711 return GrBackendRenderTarget(); // invalid
3712 }
Brian Salomon8a375832018-03-14 10:21:40 -04003713 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003714 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003715 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003716 return {};
3717 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003718 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003719 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003720 if (sFormatIdx < 0) {
3721 return {};
3722 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003723 GrGLuint colorID = 0;
3724 GrGLuint stencilID = 0;
3725 auto deleteIDs = [&] {
3726 if (colorID) {
3727 if (useTexture) {
3728 GL_CALL(DeleteTextures(1, &colorID));
3729 } else {
3730 GL_CALL(DeleteRenderbuffers(1, &colorID));
3731 }
Brian Salomonf865b052018-03-09 09:01:53 -05003732 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003733 if (stencilID) {
3734 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003735 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003736 };
3737
3738 if (useTexture) {
3739 GL_CALL(GenTextures(1, &colorID));
3740 } else {
3741 GL_CALL(GenRenderbuffers(1, &colorID));
3742 }
3743 GL_CALL(GenRenderbuffers(1, &stencilID));
3744 if (!stencilID || !colorID) {
3745 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003746 return {};
3747 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003748
Brian Salomonf865b052018-03-09 09:01:53 -05003749 GrGLFramebufferInfo info;
3750 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003751 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003752 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3753 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003754 deleteIDs();
3755 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003756 }
3757
3758 this->invalidateBoundRenderTarget();
3759
Adrienne Walker4ee88512018-05-17 11:37:14 -07003760 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003761 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003762 GrGLTextureParameters::SamplerOverriddenState initialState;
3763 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3764 if (!colorID) {
3765 deleteIDs();
3766 return {};
3767 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003768 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3769 colorID, 0));
3770 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003771 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003772 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003773 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003774 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3775 GR_GL_RENDERBUFFER, colorID));
3776 }
3777 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003778 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003779 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003780 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003781 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003782 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3783 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003784 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003785 }
3786
Brian Salomon93348dd2018-08-29 12:56:23 -04003787 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3788 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3789 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3790 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003791 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003792 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003793
Adrienne Walker4ee88512018-05-17 11:37:14 -07003794 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003795 GrGLenum status;
3796 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3797 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003798 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003799 return {};
3800 }
3801 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003802
Greg Daniel108bb232018-07-03 16:18:29 -04003803 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003804 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003805 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003806}
3807
3808void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003809 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003810 GrGLFramebufferInfo info;
3811 if (backendRT.getGLFramebufferInfo(&info)) {
3812 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003813 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003814 }
3815 }
joshualitt8fd844f2015-12-02 13:36:47 -08003816}
3817
Greg Daniel26b50a42018-03-08 09:49:58 -05003818void GrGLGpu::testingOnly_flushGpuAndSync() {
3819 GL_CALL(Finish());
3820}
Brian Salomonf865b052018-03-09 09:01:53 -05003821#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003822
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003823///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003824
cdaltone2e71c22016-04-07 18:13:29 -07003825GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003826 const GrBuffer* ibuf) {
Chris Daltonb67081f2020-02-27 15:13:56 -07003827 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003828 GrGLAttribArrayState* attribState;
3829
cdaltone2e71c22016-04-07 18:13:29 -07003830 if (gpu->glCaps().isCoreProfile()) {
3831 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003832 GrGLuint arrayID;
3833 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3834 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003835 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003836 }
cdaltone2e71c22016-04-07 18:13:29 -07003837 if (ibuf) {
3838 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003839 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003840 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003841 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003842 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003843 if (ibuf) {
3844 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003845 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003846 } else {
3847 this->setVertexArrayID(gpu, 0);
3848 }
3849 int attrCount = gpu->glCaps().maxVertexAttributes();
3850 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3851 fDefaultVertexArrayAttribState.resize(attrCount);
3852 }
3853 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003854 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003855 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003856}
bsalomone179a912016-01-20 06:18:10 -08003857
Greg Daniel30a35e82019-11-19 14:12:25 -05003858bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003859 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003860 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003861 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003862 // We call finish if the client told us to sync or if we have a finished proc but don't support
3863 // GLsync objects.
3864 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3865 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3866 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003867 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003868 // After a finish everything previously sent to GL is done.
3869 for (const auto& cb : fFinishCallbacks) {
3870 cb.fCallback(cb.fContext);
3871 this->deleteSync(cb.fSync);
3872 }
3873 fFinishCallbacks.clear();
3874 if (info.fFinishedProc) {
3875 info.fFinishedProc(info.fFinishedContext);
3876 }
3877 } else {
3878 if (info.fFinishedProc) {
3879 FinishCallback callback;
3880 callback.fCallback = info.fFinishedProc;
3881 callback.fContext = info.fFinishedContext;
3882 callback.fSync = (GrGLsync)this->insertFence();
3883 fFinishCallbacks.push_back(callback);
3884 GL_CALL(Flush());
3885 } else if (insertedSemaphore) {
3886 // Must call flush after semaphores in case they are waited on another GL context.
3887 GL_CALL(Flush());
3888 }
3889 // See if any previously inserted finish procs are good to go.
3890 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003891 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003892 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003893}
3894
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003895void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3896 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3897 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3898 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003899}
3900
Greg Daniel6be35232017-03-01 17:01:09 -05003901GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003902 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003903 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003904 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3905 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3906 GrGLuint fence = 0;
3907 GL_CALL(GenFences(1, &fence));
3908 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3909 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3910 } else {
3911 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3912 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05003913 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003914 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003915}
3916
Brian Salomonb0d8b762019-05-06 16:58:22 -04003917bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003918 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3919 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3920 if (!timeout) {
3921 if (flush) {
3922 GL_CALL(Flush);
3923 }
3924 GrGLboolean result;
3925 GL_CALL_RET(result, TestFence(nvFence));
3926 return result == GR_GL_TRUE;
3927 }
3928 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3929 // If this really becomes necessary we could poll TestFence().
3930 // FinishFence always flushes so no need to check flush param.
3931 GL_CALL(FinishFence(nvFence));
3932 return true;
3933 } else {
3934 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3935 GrGLenum result;
3936 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3937 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3938 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003939}
3940
3941bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3942 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003943}
3944
3945void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003946 this->deleteSync((GrGLsync)fence);
3947}
3948
Greg Daniel301015c2019-11-18 14:06:46 -05003949std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003950 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003951 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003952}
3953
Greg Daniel301015c2019-11-18 14:06:46 -05003954std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3955 const GrBackendSemaphore& semaphore,
3956 GrResourceProvider::SemaphoreWrapType wrapType,
3957 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003958 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003959 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3960}
3961
Greg Daniel301015c2019-11-18 14:06:46 -05003962void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3963 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003964
Greg Daniel48661b82018-01-22 16:11:35 -05003965 GrGLsync sync;
3966 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3967 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05003968}
3969
Greg Daniel301015c2019-11-18 14:06:46 -05003970void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
3971 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003972
3973 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3974}
3975
Brian Salomonb0d8b762019-05-06 16:58:22 -04003976void GrGLGpu::checkFinishProcs() {
3977 // Bail after the first unfinished sync since we expect they signal in the order inserted.
3978 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
3979 /* timeout = */ 0, /* flush = */ false)) {
3980 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
3981 this->deleteSync(fFinishCallbacks.front().fSync);
3982 fFinishCallbacks.pop_front();
3983 }
3984}
3985
Greg Daniel6be35232017-03-01 17:01:09 -05003986void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003987 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3988 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
3989 GL_CALL(DeleteFences(1, &nvFence));
3990 } else {
3991 GL_CALL(DeleteSync(sync));
3992 }
jvanverth84741b32016-09-30 08:39:02 -07003993}
Brian Osman13dddce2017-05-09 13:19:50 -04003994
Greg Daniel301015c2019-11-18 14:06:46 -05003995std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04003996 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05003997 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05003998 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003999 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004000 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4001 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004002
4003 return semaphore;
4004}
Robert Phillips646e4292017-06-13 12:44:56 -04004005
4006int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004007 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004008 case kTexture2DSampler_GrSLType:
4009 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004010 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004011 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004012 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004013 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004014 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004015 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004016 }
4017}
Brian Osman71a18892017-08-10 10:23:25 -04004018
Kevin Lubickf4def342018-10-04 12:52:50 -04004019#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004020#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004021void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4022 // We are called by the base class, which has already called beginObject(). We choose to nest
4023 // all of our caps information in a named sub-object.
4024 writer->beginObject("GL GPU");
4025
4026 const GrGLubyte* str;
4027 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4028 writer->appendString("GL_VERSION", (const char*)(str));
4029 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4030 writer->appendString("GL_RENDERER", (const char*)(str));
4031 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4032 writer->appendString("GL_VENDOR", (const char*)(str));
4033 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4034 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4035
4036 writer->appendName("extensions");
4037 glInterface()->fExtensions.dumpJSON(writer);
4038
4039 writer->endObject();
4040}
Kevin Lubickf4def342018-10-04 12:52:50 -04004041#endif