blob: 55d77192af4a0e373bbf276bd6e824a9b23cc4bc [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 Salomon802cb312018-06-08 18:05:20 -0400428 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400429 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000430 // detach the current program so there is no confusion on OpenGL's part
431 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000432 GL_CALL(UseProgram(0));
433 }
434
Brian Salomon43f8bf02017-10-18 08:33:29 -0400435 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700436 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800437 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400438 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700439 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800440 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400441 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700442 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800443 }
egdaniel0f5f9672015-02-03 11:10:51 -0800444
bsalomon7ea33f52015-11-22 14:51:00 -0800445 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
446 if (0 != fCopyPrograms[i].fProgram) {
447 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
448 }
bsalomon6df86402015-06-01 10:41:49 -0700449 }
bsalomon6dea83f2015-12-03 12:58:06 -0800450
brianosman33f6b3f2016-06-02 05:49:21 -0700451 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
452 if (0 != fMipmapPrograms[i].fProgram) {
453 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
454 }
455 }
456
Brian Salomondc829942018-10-23 16:07:24 -0400457 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500458
459 while (!fFinishCallbacks.empty()) {
460 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
461 this->deleteSync(fFinishCallbacks.front().fSync);
462 fFinishCallbacks.pop_front();
463 }
bsalomonc8dc1f72014-08-21 13:02:13 -0700464}
465
bsalomon6e2aad42016-04-01 11:54:31 -0700466void GrGLGpu::disconnect(DisconnectType type) {
467 INHERITED::disconnect(type);
468 if (DisconnectType::kCleanup == type) {
469 if (fHWProgramID) {
470 GL_CALL(UseProgram(0));
471 }
472 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700473 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700474 }
475 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700476 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700477 }
478 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700479 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700480 }
bsalomon6e2aad42016-04-01 11:54:31 -0700481 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
482 if (fCopyPrograms[i].fProgram) {
483 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
484 }
485 }
brianosman33f6b3f2016-06-02 05:49:21 -0700486 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
487 if (fMipmapPrograms[i].fProgram) {
488 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
489 }
490 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400491
Brian Salomondc829942018-10-23 16:07:24 -0400492 if (fSamplerObjectCache) {
493 fSamplerObjectCache->release();
494 }
bsalomon6e2aad42016-04-01 11:54:31 -0700495 } else {
496 if (fProgramCache) {
497 fProgramCache->abandon();
498 }
Brian Salomondc829942018-10-23 16:07:24 -0400499 if (fSamplerObjectCache) {
500 fSamplerObjectCache->abandon();
501 }
bsalomon6e2aad42016-04-01 11:54:31 -0700502 }
503
Brian Salomon802cb312018-06-08 18:05:20 -0400504 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500505 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700506
bsalomonc8dc1f72014-08-21 13:02:13 -0700507 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700508 fTempSrcFBOID = 0;
509 fTempDstFBOID = 0;
510 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700511 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800512 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
513 fCopyPrograms[i].fProgram = 0;
514 }
brianosman33f6b3f2016-06-02 05:49:21 -0700515 fMipmapProgramArrayBuffer.reset();
516 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
517 fMipmapPrograms[i].fProgram = 0;
518 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400519
jvanverthe9c0fc62015-04-29 11:18:05 -0700520 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700521 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700522 }
Greg Daniel822f7b22020-02-21 14:41:36 -0500523
524 while (!fFinishCallbacks.empty()) {
525 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
526 if (DisconnectType::kCleanup == type) {
527 this->deleteSync(fFinishCallbacks.front().fSync);
528 }
529 fFinishCallbacks.pop_front();
530 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000531}
532
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000533///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000534
bsalomon861e1032014-12-16 07:33:49 -0800535void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400537 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000538 GL_CALL(Disable(GR_GL_DEPTH_TEST));
539 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000540
Brian Salomonf0861672017-05-08 11:10:10 -0400541 // We don't use face culling.
542 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600543 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
544 // just set this to the default for self-consistency.
545 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400546
Brian Salomonae64c192019-02-05 09:41:37 -0500547 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
548 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700549
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400550 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500551#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000552 // Desktop-only state that we never change
553 if (!this->glCaps().isCoreProfile()) {
554 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
555 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
556 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
557 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
558 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
559 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
560 }
561 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
562 // core profile. This seems like a bug since the core spec removes any mention of
563 // GL_ARB_imaging.
564 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
565 GL_CALL(Disable(GR_GL_COLOR_TABLE));
566 }
567 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400568
Chris Dalton1215cda2019-12-17 21:44:04 -0700569 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500570#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 // Since ES doesn't support glPointSize at all we always use the VS to
572 // set the point size
573 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
574
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000575 }
joshualitt58162332014-08-01 06:44:53 -0700576
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400577 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500578 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700579 // The arm extension requires specifically enabling MSAA fetching per sample.
580 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500581 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700582 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 fHWWriteToColor = kUnknown_TriState;
584 // we only ever use lines in hairline mode
585 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700586 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500587
588 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000589 }
edisonn@google.comba669992013-06-28 16:03:21 +0000590
egdanielb414f252014-07-29 13:15:47 -0700591 if (resetBits & kMSAAEnable_GrGLBackendState) {
592 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700593
Chris Dalton6ce447a2019-06-23 18:07:38 -0600594 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800595 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
596 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700597 GL_CALL(CoverageModulation(GR_GL_RGBA));
598 }
Chris Daltonce425af2019-12-16 10:39:03 -0700599
600 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000601 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000603 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400604 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000605
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500607 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500608 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 }
Brian Salomondc829942018-10-23 16:07:24 -0400610 if (fSamplerObjectCache) {
611 fSamplerObjectCache->invalidateBindings();
612 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000613 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000614
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 if (resetBits & kBlend_GrGLBackendState) {
616 fHWBlendState.invalidate();
617 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000618
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000619 if (resetBits & kView_GrGLBackendState) {
620 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700621 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 fHWViewport.invalidate();
623 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 if (resetBits & kStencil_GrGLBackendState) {
626 fHWStencilSettings.invalidate();
627 fHWStencilTestEnabled = kUnknown_TriState;
628 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000629
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 // Vertex
631 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700632 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500633 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
634 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700635 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000637
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000638 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500639 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700640 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000642
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000643 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700644 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700645 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000647 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000648
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000649 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000650 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400651 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000652 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
653 }
Brian Salomon1047a492019-07-02 12:25:21 -0400654 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000655 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
656 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000657 if (this->glCaps().packFlipYSupport()) {
658 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
659 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000660 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000661
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000662 if (resetBits & kProgram_GrGLBackendState) {
663 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400664 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000665 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400666 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000667}
668
Brian Salomonea4ad302019-08-07 13:04:55 -0400669static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
670 const GrGLCaps& caps, GrGLTexture::Desc* desc,
671 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400672 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400673 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400674 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800675 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000676
Brian Salomonea4ad302019-08-07 13:04:55 -0400677 desc->fSize = {backendTex.width(), backendTex.height()};
678 desc->fTarget = info.fTarget;
679 desc->fID = info.fID;
680 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800681
Brian Salomonea4ad302019-08-07 13:04:55 -0400682 if (desc->fFormat == GrGLFormat::kUnknown) {
683 return false;
684 }
685 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400686 if (!caps.shaderCaps()->externalTextureSupport()) {
687 return false;
688 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400689 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
690 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400691 return false;
692 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400693 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400694 return false;
695 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400696 if (backendTex.isProtected()) {
697 // Not supported in GL backend at this time.
698 return false;
699 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400700
Brian Salomond17f6582017-07-19 18:28:58 -0400701 return true;
702}
703
704sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400705 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400706 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400707 GrGLTexture::Desc desc;
708 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400709 return nullptr;
710 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400711
Brian Salomond17f6582017-07-19 18:28:58 -0400712 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400713 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400714 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400715 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400716 }
bsalomone5286e02016-01-14 09:24:09 -0800717
Greg Daniel177e6952017-10-12 12:27:11 -0400718 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
719 : GrMipMapsStatus::kNotAllocated;
720
Brian Salomonea4ad302019-08-07 13:04:55 -0400721 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400722 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400723 // We don't know what parameters are already set on wrapped textures.
724 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600725 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400726}
727
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500728static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
729 const GrGLCaps& caps, GrGLTexture::Desc* desc,
730 bool skipRectTexSupportCheck = false) {
731 GrGLTextureInfo info;
732 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
733 return false;
734 }
735
736 desc->fSize = {backendTex.width(), backendTex.height()};
737 desc->fTarget = info.fTarget;
738 desc->fID = info.fID;
739 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
740
741 if (desc->fFormat == GrGLFormat::kUnknown) {
742 return false;
743 }
744
745 if (GR_GL_TEXTURE_2D != desc->fTarget) {
746 return false;
747 }
748 if (backendTex.isProtected()) {
749 // Not supported in GL backend at this time.
750 return false;
751 }
752
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500753 return true;
754}
755
Robert Phillipsb915c942019-12-17 14:44:37 -0500756sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
757 GrWrapOwnership ownership,
758 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500759 GrGLTexture::Desc desc;
760 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
761 return nullptr;
762 }
763
764 if (kBorrow_GrWrapOwnership == ownership) {
765 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
766 } else {
767 desc.fOwnership = GrBackendObjectOwnership::kOwned;
768 }
769
770 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
771 : GrMipMapsStatus::kNotAllocated;
772
773 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
774 backendTex.getGLTextureParams(), cacheable,
775 kRead_GrIOType);
776 // We don't know what parameters are already set on wrapped textures.
777 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600778 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500779}
780
Brian Salomond17f6582017-07-19 18:28:58 -0400781sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400782 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400783 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500784 GrWrapOwnership ownership,
785 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400786 const GrGLCaps& caps = this->glCaps();
787
Brian Salomonea4ad302019-08-07 13:04:55 -0400788 GrGLTexture::Desc desc;
789 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800790 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800791 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400792 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
793 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800794
Brian Salomond17f6582017-07-19 18:28:58 -0400795 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400796 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800797 return nullptr;
798 }
bsalomon10528f12015-10-14 12:54:52 -0700799
Brian Osman766fcbb2017-03-13 09:33:09 -0400800 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400801 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400802 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400803 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800804 }
bsalomonb15b4c12014-10-29 12:41:57 -0700805
Robert Phillips0902c982019-07-16 07:47:56 -0400806
Greg Daniel6fa62e22019-08-07 15:52:37 -0400807 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
808 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000809
Brian Salomonea4ad302019-08-07 13:04:55 -0400810 GrGLRenderTarget::IDs rtIDs;
811 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400812 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000813 }
Greg Daniel177e6952017-10-12 12:27:11 -0400814
815 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
816 : GrMipMapsStatus::kNotAllocated;
817
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500818 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400819 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400820 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400821 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400822 // We don't know what parameters are already set on wrapped textures.
823 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600824 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000825}
826
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400827sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
828 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400829 GrGLFramebufferInfo info;
830 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000831 return nullptr;
832 }
833
Brian Salomone8a766b2019-07-19 14:24:36 -0400834 if (backendRT.isProtected()) {
835 // Not supported in GL at this time.
836 return nullptr;
837 }
838
Brian Salomond4764a12019-08-08 12:08:24 -0400839 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400840 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
841 return nullptr;
842 }
843
Brian Salomonea4ad302019-08-07 13:04:55 -0400844 GrGLRenderTarget::IDs rtIDs;
845 rtIDs.fRTFBOID = info.fFBOID;
846 rtIDs.fMSColorRenderbufferID = 0;
847 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
848 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000849
Greg Daniel6fa62e22019-08-07 15:52:37 -0400850 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700851
Brian Salomona56a7462020-02-07 14:17:25 -0500852 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400853 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000854}
855
Greg Daniel7ef28f32017-04-20 16:41:55 +0000856sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400857 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400858 GrColorType colorType) {
859 GrGLTexture::Desc desc;
860 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
861 // with a texture rectangle,we assume the necessary support exists.
862 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800863 return nullptr;
864 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400865
866 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
867 return nullptr;
868 }
869
870 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400871 GrGLRenderTarget::IDs rtIDs;
872 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800873 return nullptr;
874 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500875 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800876}
877
Brian Salomonc320b152018-02-20 14:05:36 -0500878static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700879 if (!glTex) {
880 return false;
881 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000882
bsalomone5286e02016-01-14 09:24:09 -0800883 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
884 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800885 return false;
886 }
887
jvanverth17aa0472016-01-05 10:41:27 -0800888 return true;
889}
890
Brian Salomona9b04b92018-06-01 15:04:28 -0400891bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400892 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400893 const GrMipLevel texels[], int mipLevelCount,
894 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500895 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800896
Brian Salomonc320b152018-02-20 14:05:36 -0500897 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800898 return false;
899 }
900
Brian Salomond978b902019-02-07 15:09:18 -0500901 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000902
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400903 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400904 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400905 glTex->target(), left, top, width, height, srcColorType, texels,
906 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000907}
908
Brian Salomone05ba5a2019-04-08 11:59:07 -0400909bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400910 GrColorType textureColorType, GrColorType bufferColorType,
911 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400912 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400913
Jim Van Verth1676cb92019-01-15 13:24:45 -0500914 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400915 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500916
Brian Salomonc320b152018-02-20 14:05:36 -0500917 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400918 return false;
919 }
920
Hal Canaryc36f7e72018-06-18 15:50:09 -0400921 static_assert(sizeof(int) == sizeof(int32_t), "");
922 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400923 return false;
924 }
925
Brian Salomond978b902019-02-07 15:09:18 -0500926 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400927
928 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500929 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400930 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500931 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400932
Greg Daniel660cc992017-06-26 14:55:05 -0400933 SkDEBUGCODE(
934 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
935 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
936 SkASSERT(bounds.contains(subRect));
937 )
938
Brian Salomonb28cb682019-07-26 12:48:47 -0400939 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400940 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400941 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700942 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400943 return false;
944 }
945
946 bool restoreGLRowLength = false;
947 if (trimRowBytes != rowBytes) {
948 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400949 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400950 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
951 restoreGLRowLength = true;
952 }
953
Greg Danielba88ab62019-07-26 09:14:01 -0400954 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400955 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400956 GrGLenum externalFormat = 0;
957 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400958 this->glCaps().getTexSubImageExternalFormatAndType(
959 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400960 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400961 return false;
962 }
963
Brian Salomon8cbf6622019-07-30 11:03:14 -0400964 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400965 GL_CALL(TexSubImage2D(glTex->target(),
966 0,
967 left, top,
968 width,
969 height,
970 externalFormat, externalType,
971 pixels));
972
973 if (restoreGLRowLength) {
974 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
975 }
976
977 return true;
978}
979
Brian Salomon26de56e2019-04-10 12:14:26 -0400980bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400981 GrColorType surfaceColorType, GrColorType dstColorType,
982 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400983 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
984 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400985 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400986 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
987 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400988}
989
Brian Osman9b560242017-09-05 15:34:52 -0400990void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500991 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
992 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
993 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
994 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400995 }
Brian Osman9b560242017-09-05 15:34:52 -0400996}
997
Brian Salomond2a8ae22019-09-10 16:03:59 -0400998bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
999 int texHeight, GrGLenum target, int left, int top, int width,
1000 int height, GrColorType srcColorType, const GrMipLevel texels[],
1001 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001002 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -04001003 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001004
Greg Daniel7bfc9132019-08-14 14:23:53 -04001005 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -04001006 SkDEBUGCODE(
1007 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1008 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1009 SkASSERT(bounds.contains(subRect));
1010 )
Robert Phillips590533f2017-07-11 14:22:35 -04001011 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001012 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001013
Brian Osman9b560242017-09-05 15:34:52 -04001014 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001015
cblume55f2d2d2016-02-26 13:20:48 -08001016 const GrGLInterface* interface = this->glInterface();
1017 const GrGLCaps& caps = this->glCaps();
1018
Brian Salomonf77c1462019-08-01 15:19:29 -04001019 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001020
1021 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001022 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001023 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001024
bsalomon5b30c6f2015-12-17 14:17:34 -08001025 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001026 GrGLenum externalFormat;
1027 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001028 this->glCaps().getTexSubImageExternalFormatAndType(
1029 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001030 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001031 return false;
1032 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001033
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001034 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001035 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001036 * because our srcData has extra bytes past each row. If so, we need
1037 * to trim those off here, since GL ES may not let us specify
1038 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001039 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001040 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001041
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001042 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001043 *mipMapsStatus = (mipLevelCount > 1) ?
1044 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001045 }
bsalomone699d0c2016-03-09 06:25:15 -08001046
Brian Salomond2a8ae22019-09-10 16:03:59 -04001047 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001048
Brian Salomond2a8ae22019-09-10 16:03:59 -04001049 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1050 if (!texels[currentMipLevel].fPixels) {
1051 if (mipMapsStatus) {
1052 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001053 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001054 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001055 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001056 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001057 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1058 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001059 const size_t trimRowBytes = currentWidth * bpp;
1060 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1061
1062 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1063 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1064 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1065 restoreGLRowLength = true;
1066 }
1067
1068 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1069 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001070 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001071 if (restoreGLRowLength) {
1072 SkASSERT(caps.writePixelsRowBytesSupport());
1073 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1074 }
1075 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001076}
1077
Greg Daniel7bfc9132019-08-14 14:23:53 -04001078bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001079 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001080 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001081 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001082 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001083 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001084 const GrGLCaps& caps = this->glCaps();
1085
1086 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001087 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001088 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001089 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001090 }
1091
Robert Phillips9f744f72019-12-19 19:14:33 -05001092 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1093 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1094
Greg Daniel7bfc9132019-08-14 14:23:53 -04001095 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001096
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001097 int numMipLevels = 1;
1098 if (mipMapped == GrMipMapped::kYes) {
1099 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1100 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001101
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001102 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001103 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001104
1105 if (useTexStorage) {
1106 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001107 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1108 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001109 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001110 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001111 }
Robert Phillips42716d42019-12-16 12:19:54 -05001112
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001113 size_t offset = 0;
1114 for (int level = 0; level < numMipLevels; ++level) {
1115
Robert Phillips99dead92020-01-27 16:11:57 -05001116 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1117 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001118
Brian Salomond8575452020-02-25 12:13:29 -05001119 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1120 level,
1121 0, // left
1122 0, // top
1123 dimensions.width(),
1124 dimensions.height(),
1125 internalFormat,
1126 SkToInt(levelDataSize),
1127 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001128
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129 if (error != GR_GL_NO_ERROR) {
1130 return false;
1131 }
1132
Robert Phillips9f744f72019-12-19 19:14:33 -05001133 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001134 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001135 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001136 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001137 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001138
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001139 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001140 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1141 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001142
1143 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001144 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1145 level,
1146 internalFormat,
1147 dimensions.width(),
1148 dimensions.height(),
1149 0, // border
1150 SkToInt(levelDataSize),
1151 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001152
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001153 if (error != GR_GL_NO_ERROR) {
1154 return false;
1155 }
1156
Robert Phillips9f744f72019-12-19 19:14:33 -05001157 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001158 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001159 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001160 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001161 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001162}
1163
Brian Salomond8575452020-02-25 12:13:29 -05001164bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001165 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001166 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001167 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001168 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001169 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001170 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1171 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001172 break;
1173 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001174 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1175 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001176 break;
1177 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1178 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001179 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1180 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001181 break;
1182 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001183 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001184 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001185 }
Brian Salomond8575452020-02-25 12:13:29 -05001186 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001187}
1188
Brian Salomonea4ad302019-08-07 13:04:55 -04001189bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001190 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001191 GrGLRenderTarget::IDs* rtIDs) {
1192 rtIDs->fMSColorRenderbufferID = 0;
1193 rtIDs->fRTFBOID = 0;
1194 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1195 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196
bsalomona11e5fc2015-12-18 07:59:41 -08001197 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001198
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001200 goto FAILED;
1201 }
1202
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001203 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001204 goto FAILED;
1205 }
1206
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1208 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 goto FAILED;
1210 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001211
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001212 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1213 // the texture bound to the other. The exception is the IMG multisample extension. With this
1214 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1215 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001216 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1218 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1219 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 goto FAILED;
1221 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001222 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001223 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001224 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 }
1226
egdanield803f272015-03-18 13:01:52 -07001227 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001228 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001229 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001230 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001231 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001232 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1233 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 goto FAILED;
1235 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001236 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001237 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001238 GR_GL_COLOR_ATTACHMENT0,
1239 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001240 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001242 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001244 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001245 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1246 GR_GL_COLOR_ATTACHMENT0,
1247 desc.fTarget,
1248 desc.fID,
1249 0,
1250 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001251 } else {
egdanield803f272015-03-18 13:01:52 -07001252 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001253 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001254 desc.fTarget,
1255 desc.fID,
1256 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001257 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001258
1259 return true;
1260
1261FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001262 if (rtIDs->fMSColorRenderbufferID) {
1263 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001264 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001265 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1266 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001267 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001268 if (rtIDs->fTexFBOID) {
1269 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001270 }
1271 return false;
1272}
1273
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001274// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001275static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001276// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001277 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001278}
1279
Brian Salomone2826ab2019-06-04 15:58:31 -04001280static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001281 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001282 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1283 // drivers have a bug where an FBO won't be complete if it includes a
1284 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001285 GrGLTextureParameters::SamplerOverriddenState state;
1286 state.fMinFilter = GR_GL_NEAREST;
1287 state.fMagFilter = GR_GL_NEAREST;
1288 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1289 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001290 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1293 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001294 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001295}
1296
Brian Salomona56a7462020-02-07 14:17:25 -05001297sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001298 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001299 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001300 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001301 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001302 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001303 int mipLevelCount,
1304 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001305 // We don't support protected textures in GL.
1306 if (isProtected == GrProtected::kYes) {
1307 return nullptr;
1308 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001309 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001310
Brian Salomond2a8ae22019-09-10 16:03:59 -04001311 SkASSERT(mipLevelCount > 0);
1312 GrMipMapsStatus mipMapsStatus =
1313 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001314 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001315 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001316 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001317 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001318 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001319 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001320 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1321 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001322
Brian Salomona56a7462020-02-07 14:17:25 -05001323 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1324 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001325
1326 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001327 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001328 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001329
Robert Phillips67d52cf2017-06-05 13:38:13 -04001330 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001331 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001332 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001333 GL_CALL(BindTexture(texDesc.fTarget, 0));
1334 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001335
Brian Salomonea4ad302019-08-07 13:04:55 -04001336 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1337 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 return return_null_texture();
1339 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001340 tex = sk_make_sp<GrGLTextureRenderTarget>(
1341 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001342 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001343 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001344 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001346 // The non-sampler params are still at their default values.
1347 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1348 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001349 if (levelClearMask) {
1350 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001351 GrColorType colorType;
1352 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1353 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001354 if (this->glCaps().clearTextureSupport()) {
1355 for (int i = 0; i < mipLevelCount; ++i) {
1356 if (levelClearMask & (1U << i)) {
1357 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1358 nullptr));
1359 }
1360 }
1361 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1362 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001363 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001364 this->disableWindowRectangles();
1365 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001366 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001367 for (int i = 0; i < mipLevelCount; ++i) {
1368 if (levelClearMask & (1U << i)) {
1369 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1370 kDst_TempFBOTarget);
1371 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1372 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1373 }
1374 }
Brian Salomonc2249852019-09-16 11:08:50 -04001375 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001376 } else {
1377 std::unique_ptr<char[]> zeros;
1378 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1379 for (int i = 0; i < mipLevelCount; ++i) {
1380 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001381 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1382 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001383 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1384 // smaller levels that need clearing.
1385 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001386 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001387 size_t size = levelWidth * levelHeight * bpp;
1388 zeros.reset(new char[size]());
1389 }
1390 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1391 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1392 externalFormat, externalType, zeros.get()));
1393 }
Brian Salomona3e29962019-07-16 11:52:08 -04001394 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001395 }
1396 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001397 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001398}
1399
Robert Phillips9f744f72019-12-19 19:14:33 -05001400sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001401 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001402 SkBudgeted budgeted,
1403 GrMipMapped mipMapped,
1404 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001405 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001406 // We don't support protected textures in GL.
1407 if (isProtected == GrProtected::kYes) {
1408 return nullptr;
1409 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001410 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001411 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001412 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001413 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001414 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001415 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001416 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001417 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001418 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001419 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001420 return nullptr;
1421 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001422
Robert Phillipsee946932019-12-18 11:16:17 -05001423 // Unbind this texture from the scratch texture unit.
1424 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1425
Robert Phillipse4720c62020-01-14 14:33:24 -05001426 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1427 ? GrMipMapsStatus::kValid
1428 : GrMipMapsStatus::kNotAllocated;
1429
1430 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001431 // The non-sampler params are still at their default values.
1432 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1433 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001434 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001435}
1436
Robert Phillipsb915c942019-12-17 14:44:37 -05001437GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1438 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001439 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001440 GrProtected isProtected,
1441 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001442 // We don't support protected textures in GL.
1443 if (isProtected == GrProtected::kYes) {
1444 return {};
1445 }
1446
1447 this->handleDirtyContext();
1448
1449 GrGLFormat glFormat = format.asGLFormat();
1450 if (glFormat == GrGLFormat::kUnknown) {
1451 return {};
1452 }
1453
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001454 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001455 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001456 SkAutoMalloc am;
1457
1458 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1459 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1460 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001461 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001462 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001463 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1464 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001465
Robert Phillips99dead92020-01-27 16:11:57 -05001466 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1467 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001468
1469 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001470
1471 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1472
1473 rawData = (const char*) am.get();
1474 }
1475
1476 GrGLTextureInfo info;
1477 GrGLTextureParameters::SamplerOverriddenState initialState;
1478
1479 info.fTarget = GR_GL_TEXTURE_2D;
1480 info.fFormat = GrGLFormatToEnum(glFormat);
1481 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001482 mipMapped, &initialState,
1483 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001484 if (!info.fID) {
1485 return {};
1486 }
1487
1488 // Unbind this texture from the scratch texture unit.
1489 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1490
1491 auto parameters = sk_make_sp<GrGLTextureParameters>();
1492 // The non-sampler params are still at their default values.
1493 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1494 fResetTimestampForTextureParameters);
1495
1496 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1497 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001498}
1499
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001500namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001501
egdaniel8dc7c3a2015-04-16 11:22:42 -07001502const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001503
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001504void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001505 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001506
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001508 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 (kUnknownBitCount == format->fTotalBits));
1510 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001511 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001512 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1513 (GrGLint*)&format->fStencilBits);
1514 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001515 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001516 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1517 (GrGLint*)&format->fTotalBits);
1518 format->fTotalBits += format->fStencilBits;
1519 } else {
1520 format->fTotalBits = format->fStencilBits;
1521 }
1522 }
1523}
1524}
1525
Brian Salomon5043f1f2019-07-11 21:27:54 -04001526int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001527 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001528 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1529
1530 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001531 // Default to unsupported, set this if we find a stencil format that works.
1532 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001533
Brian Salomond2a8ae22019-09-10 16:03:59 -04001534 GrGLuint colorID =
1535 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1536 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001537 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001538 }
egdanielff1d5472015-09-10 08:37:20 -07001539 // unbind the texture from the texture unit before binding it to the frame buffer
1540 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1541
1542 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001543 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001544 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001545 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001546 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001547 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1548 GR_GL_COLOR_ATTACHMENT0,
1549 GR_GL_TEXTURE_2D,
1550 colorID,
1551 0));
bsalomon30447372015-12-21 09:03:05 -08001552 GrGLuint sbRBID = 0;
1553 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001554
1555 // look over formats till I find a compatible one
1556 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001557 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001558 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001559 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1560 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001561 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1562 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1563 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001564 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001565 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001566 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001567 if (sFmt.fPacked) {
1568 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1569 GR_GL_DEPTH_ATTACHMENT,
1570 GR_GL_RENDERBUFFER, sbRBID));
1571 } else {
1572 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1573 GR_GL_DEPTH_ATTACHMENT,
1574 GR_GL_RENDERBUFFER, 0));
1575 }
1576 GrGLenum status;
1577 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1578 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1579 firstWorkingStencilFormatIndex = i;
1580 break;
1581 }
egdanielff1d5472015-09-10 08:37:20 -07001582 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1583 GR_GL_STENCIL_ATTACHMENT,
1584 GR_GL_RENDERBUFFER, 0));
1585 if (sFmt.fPacked) {
1586 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1587 GR_GL_DEPTH_ATTACHMENT,
1588 GR_GL_RENDERBUFFER, 0));
1589 }
egdanielff1d5472015-09-10 08:37:20 -07001590 }
1591 }
bsalomon30447372015-12-21 09:03:05 -08001592 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001593 }
1594 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001595 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1596 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001597 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001598 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001599 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001600}
1601
Brian Salomonea4ad302019-08-07 13:04:55 -04001602GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001603 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001604 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001605 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001606 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001607 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001608 if (format == GrGLFormat::kUnknown) {
1609 return 0;
1610 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001611 GrGLuint id = 0;
1612 GL_CALL(GenTextures(1, &id));
1613 if (!id) {
1614 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001615 }
1616
Brian Salomonea4ad302019-08-07 13:04:55 -04001617 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001618
Brian Salomonea4ad302019-08-07 13:04:55 -04001619 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1620
Robert Phillips42716d42019-12-16 12:19:54 -05001621 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001622 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1623 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001624 GL_CALL(DeleteTextures(1, &id));
1625 return 0;
1626 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001627 }
Robert Phillips42716d42019-12-16 12:19:54 -05001628
Brian Salomonea4ad302019-08-07 13:04:55 -04001629 return id;
1630}
1631
Robert Phillips2716daf2020-01-23 12:53:42 -05001632GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001633 GrGLFormat format,
1634 GrRenderable renderable,
1635 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001636 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001637 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001638 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001639
Brian Salomonea4ad302019-08-07 13:04:55 -04001640 GrGLuint id = 0;
1641 GL_CALL(GenTextures(1, &id));
1642
1643 if (!id) {
1644 return 0;
1645 }
1646
1647 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001648
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001649 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001650 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001651 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001652 }
1653
Brian Salomond2a8ae22019-09-10 16:03:59 -04001654 if (initialState) {
1655 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1656 } else {
1657 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001658 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001659
1660 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1661
1662 bool success = false;
1663 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001664 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001665 auto levelCount = std::max(mipLevelCount, 1);
1666 GrGLenum error =
1667 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1668 dimensions.width(), dimensions.height()));
1669 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001670 } else {
1671 GrGLenum externalFormat, externalType;
1672 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1673 GrGLenum error = GR_GL_NO_ERROR;
1674 if (externalFormat && externalType) {
1675 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1676 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001677 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1678 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001679 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1680 currentWidth, currentHeight, 0, externalFormat,
1681 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001682 }
Brian Salomond8575452020-02-25 12:13:29 -05001683 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001684 }
1685 }
1686 }
1687 if (success) {
1688 return id;
1689 }
1690 GL_CALL(DeleteTextures(1, &id));
1691 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001692}
1693
Chris Daltoneffee202019-07-01 22:28:03 -06001694GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1695 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001696 SkASSERT(width >= rt->width());
1697 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001698
egdaniel8dc7c3a2015-04-16 11:22:42 -07001699 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001700
Brian Salomond4764a12019-08-08 12:08:24 -04001701 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001702 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001703 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001704 }
egdanielff1d5472015-09-10 08:37:20 -07001705
1706 if (!sbDesc.fRenderbufferID) {
1707 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1708 }
1709 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001710 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001711 }
1712 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1713 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001714 // we do this "if" so that we don't call the multisample
1715 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001716 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001717 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1718 width, height)) {
1719 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1720 return nullptr;
1721 }
egdanielff1d5472015-09-10 08:37:20 -07001722 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001723 GrGLenum error = GL_ALLOC_CALL(
1724 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1725 if (error != GR_GL_NO_ERROR) {
1726 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1727 return nullptr;
1728 }
egdanielff1d5472015-09-10 08:37:20 -07001729 }
1730 fStats.incStencilAttachmentCreates();
1731 // After sized formats we attempt an unsized format and take
1732 // whatever sizes GL gives us. In that case we query for the size.
1733 GrGLStencilAttachment::Format format = sFmt;
1734 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001735 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1736 sbDesc,
1737 width,
1738 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001739 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001740 format);
1741 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001742}
1743
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001744////////////////////////////////////////////////////////////////////////////////
1745
Brian Salomondbf70722019-02-07 11:31:24 -05001746sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1747 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001748 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001749}
1750
Chris Dalton2e7ed262020-02-21 15:17:59 -07001751void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1752 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001753 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1754 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1755 fHWScissorSettings.fEnabled = kYes_TriState;
1756 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001757 } else {
1758 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1759 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1760 fHWScissorSettings.fEnabled = kNo_TriState;
1761 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001763}
Brian Salomond818ebf2018-07-02 14:08:49 +00001764
Chris Dalton2e7ed262020-02-21 15:17:59 -07001765void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1766 GrSurfaceOrigin rtOrigin) {
1767 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1768 if (fHWScissorSettings.fRect != nativeScissor) {
1769 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1770 nativeScissor.fHeight));
1771 fHWScissorSettings.fRect = nativeScissor;
1772 }
joshualitt77b13072014-10-27 14:51:01 -07001773}
1774
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001775void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001776 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001777#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001778 typedef GrWindowRectsState::Mode Mode;
1779 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1780 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001781
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001782 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001783 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001784 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001785 }
1786
csmartdalton7535f412016-08-23 06:51:00 -07001787 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1788 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001789 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001791
Chris Daltond6cda8d2019-09-05 02:30:04 -06001792 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001794 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001795 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001796 }
1797
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001798 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001799 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001800
Greg Danielacd66b42019-05-22 16:29:12 -04001801 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001803}
1804
1805void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001806#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001807 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001808 return;
1809 }
1810 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001811 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001812#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001813}
1814
Robert Phillipsfcaae482019-11-07 10:17:03 -05001815bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001816 this->handleDirtyContext();
1817
1818 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1819 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1820 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001821
Stephen Whiteb1857852020-02-07 15:33:23 +00001822 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001823 if (!program) {
1824 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1825 return false;
1826 }
brianosman33f6b3f2016-06-02 05:49:21 -07001827
Brian Salomon802cb312018-06-08 18:05:20 -04001828 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001829
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 Dalton3ffa7af2020-02-20 16:31:47 -07001834 fHWProgram->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
1856 return true;
1857}
1858
Brian Salomon802cb312018-06-08 18:05:20 -04001859void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1860 if (!program) {
1861 fHWProgram.reset();
1862 fHWProgramID = 0;
1863 return;
1864 }
1865 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1866 if (program == fHWProgram) {
1867 return;
1868 }
1869 auto id = program->programID();
1870 SkASSERT(id);
1871 GL_CALL(UseProgram(id));
1872 fHWProgram = std::move(program);
1873 fHWProgramID = id;
1874}
1875
1876void GrGLGpu::flushProgram(GrGLuint id) {
1877 SkASSERT(id);
1878 if (fHWProgramID == id) {
1879 SkASSERT(!fHWProgram);
1880 return;
1881 }
1882 fHWProgram.reset();
1883 GL_CALL(UseProgram(id));
1884 fHWProgramID = id;
1885}
1886
1887void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001888 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001889 int baseVertex,
1890 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001891 int baseInstance,
1892 GrPrimitiveRestart enablePrimitiveRestart) {
1893 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001894
cdaltone2e71c22016-04-07 18:13:29 -07001895 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001896 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001897 SkASSERT(indexBuffer->isCpuBuffer() ||
1898 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001899 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001900 } else {
1901 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001902 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001903
Brian Salomon92be2f72018-06-19 14:33:47 -04001904 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1905 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001906
Brian Salomon802cb312018-06-08 18:05:20 -04001907 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001908 SkASSERT(vertexBuffer);
1909 SkASSERT(vertexBuffer->isCpuBuffer() ||
1910 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1911 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001912 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1913 const auto& attrib = fHWProgram->vertexAttribute(i);
1914 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001915 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1916 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001917 }
Chris Dalton1d616352017-05-31 12:51:23 -06001918 }
Brian Salomon802cb312018-06-08 18:05:20 -04001919 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001920 SkASSERT(instanceBuffer);
1921 SkASSERT(instanceBuffer->isCpuBuffer() ||
1922 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1923 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001924 int attribIdx = fHWProgram->numVertexAttributes();
1925 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1926 const auto& attrib = fHWProgram->instanceAttribute(i);
1927 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001928 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1929 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1930 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001931 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001932 }
1933}
1934
Brian Salomonae64c192019-02-05 09:41:37 -05001935GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001936 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001937
cdaltone2e71c22016-04-07 18:13:29 -07001938 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001939 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001940 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001941 }
cdaltone2e71c22016-04-07 18:13:29 -07001942
Brian Salomonae64c192019-02-05 09:41:37 -05001943 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001944 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001945 if (!bufferState->fBufferZeroKnownBound) {
1946 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1947 bufferState->fBufferZeroKnownBound = true;
1948 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001949 }
Brian Salomondbf70722019-02-07 11:31:24 -05001950 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1951 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001952 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1953 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1954 bufferState->fBufferZeroKnownBound = false;
1955 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001956 }
1957
Brian Salomonae64c192019-02-05 09:41:37 -05001958 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001959}
Brian Salomond818ebf2018-07-02 14:08:49 +00001960
Brian Osman9a9baae2018-11-05 15:06:26 -05001961void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001962 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001963 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001964 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001965 SkASSERT(!this->caps()->performColorClearsAsDraws());
1966 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001967
Brian Salomon43f8bf02017-10-18 08:33:29 -04001968 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001969
Brian Salomon43f8bf02017-10-18 08:33:29 -04001970 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1971
Brian Salomond818ebf2018-07-02 14:08:49 +00001972 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001973 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001974 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001975 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001976 }
Greg Danielacd66b42019-05-22 16:29:12 -04001977 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001978 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001979 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001980 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001981 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001982}
1983
Robert Phillips95214472017-08-08 18:00:03 -04001984void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001985 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1986
Robert Phillips95214472017-08-08 18:00:03 -04001987 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001988 return;
1989 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001990
Chris Dalton674f77a2019-09-30 20:49:39 -06001991 // This should only be called internally when we know we have a stencil buffer.
1992 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001993
bsalomonb0bd4f62014-09-03 07:19:50 -07001994 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001995 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001996
Chris Dalton2e7ed262020-02-21 15:17:59 -07001997 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001998 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001999
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002000 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002001 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002002 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002003 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002004}
2005
Chris Daltonb50cc812019-10-14 16:29:21 -06002006static bool use_tiled_rendering(const GrGLCaps& glCaps,
2007 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2008 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2009 // Otherwise it's faster to just not use it.
2010 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2011 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2012}
2013
2014void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002015 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2016 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2017 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2018
2019 this->handleDirtyContext();
2020
2021 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2022 this->flushRenderTarget(glRT);
2023 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2024
Chris Daltonb50cc812019-10-14 16:29:21 -06002025 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2026 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2027 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2028 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2029 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2030 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2031 nativeBounds.fHeight, preserveMask));
2032 }
2033
Chris Dalton674f77a2019-09-30 20:49:39 -06002034 GrGLbitfield clearMask = 0;
2035 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2036 SkASSERT(!this->caps()->performColorClearsAsDraws());
2037 this->flushClearColor(colorLoadStore.fClearColor);
2038 this->flushColorWrite(true);
2039 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002040 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002041 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2042 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2043 GL_CALL(StencilMask(0xffffffff));
2044 GL_CALL(ClearStencil(0));
2045 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2046 }
2047 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07002048 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06002049 this->disableWindowRectangles();
2050 GL_CALL(Clear(clearMask));
2051 }
2052}
2053
2054void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2055 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2056 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2057 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2058
2059 this->handleDirtyContext();
2060
2061 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2062 // The framebuffer binding changed in the middle of a command buffer. We should have already
2063 // printed a warning during onFBOChanged.
2064 return;
2065 }
2066
2067 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2068 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2069
2070 SkSTArray<2, GrGLenum> discardAttachments;
2071 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2072 discardAttachments.push_back(
2073 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2074 }
2075 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2076 discardAttachments.push_back(
2077 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2078 }
2079
2080 if (!discardAttachments.empty()) {
2081 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2082 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2083 discardAttachments.begin()));
2084 } else {
2085 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2086 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2087 discardAttachments.begin()));
2088 }
2089 }
2090 }
2091
Chris Daltonb50cc812019-10-14 16:29:21 -06002092 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2093 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2094 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2095 // Handled by use_tiled_rendering().
2096 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2097 GL_CALL(EndTiling(preserveMask));
2098 }
2099
Chris Dalton674f77a2019-09-30 20:49:39 -06002100 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002101}
2102
csmartdalton29df7602016-08-31 11:55:52 -07002103void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2104 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002105 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002106 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002107 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002108 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002109
egdaniel8dc7c3a2015-04-16 11:22:42 -07002110 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002111 if (!sb) {
2112 // We should only get here if we marked a proxy as requiring a SB. However,
2113 // the SB creation could later fail. Likely clipping is going to go awry now.
2114 return;
2115 }
2116
bsalomon6bc1b5f2015-02-23 09:06:38 -08002117 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002118#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002119 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002120 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002121#else
2122 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002123 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002124 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002125 // changing the clip between stencil passes may or may not
2126 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002127 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002128#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002129 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002130 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002131 value = (1 << (stencilBitCount - 1));
2132 } else {
2133 value = 0;
2134 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002135 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002136 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002137
Greg Danielacd66b42019-05-22 16:29:12 -04002138 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002139 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002140
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002141 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002142 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002143 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002144 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002145}
2146
Brian Salomone05ba5a2019-04-08 11:59:07 -04002147bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002148 GrColorType surfaceColorType, GrColorType dstColorType,
2149 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002150 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002151
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002152 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002153 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002154 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002155 return false;
2156 }
Greg Danielba88ab62019-07-26 09:14:01 -04002157 GrGLenum externalFormat = 0;
2158 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002159 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2160 surfaceColorType,
2161 dstColorType,
2162 &externalFormat,
2163 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002164 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002165 return false;
2166 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002167
Brian Salomon71d9d842016-11-03 13:42:00 -04002168 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002169 if (renderTarget->numSamples() <= 1 ||
2170 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2171 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2172 this->flushRenderTargetNoColorWrites(renderTarget);
2173 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2174 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2175 return false;
2176 } else {
2177 SkASSERT(renderTarget->requiresManualMSAAResolve());
2178 // we don't track the state of the READ FBO ID.
2179 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002180 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002181 } else {
2182 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002183 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002184 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002185 }
2186
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002187 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002188 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002189
Brian Salomona6948702018-06-01 15:33:20 -04002190 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002191 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002192 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002193 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002194 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002195 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002196
Brian Osman1348ed02018-09-12 09:08:39 -04002197 bool reattachStencil = false;
2198 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2199 renderTarget &&
2200 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002201 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002202 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2203 reattachStencil = true;
2204 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2205 GR_GL_RENDERBUFFER, 0));
2206 }
2207
Chris Dalton76500e52019-09-05 02:13:05 -06002208 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002209 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002210
2211 if (reattachStencil) {
2212 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2213 renderTarget->renderTargetPriv().getStencilAttachment());
2214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2215 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2216 }
2217
Brian Salomon26de56e2019-04-10 12:14:26 -04002218 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002219 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002220 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2221 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002222
Brian Salomone05ba5a2019-04-08 11:59:07 -04002223 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002224 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002225 }
2226 return true;
2227}
2228
2229bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002230 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2231 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002232 SkASSERT(surface);
2233
Brian Salomonb28cb682019-07-26 12:48:47 -04002234 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002235
Brian Salomon26de56e2019-04-10 12:14:26 -04002236 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2237 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002238
Brian Salomon1047a492019-07-02 12:25:21 -04002239 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002240 rowPixelWidth = width;
2241 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002242 SkASSERT(!(rowBytes % bytesPerPixel));
2243 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002244 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002245 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2246 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002247}
2248
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002249GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002250 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002251 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002252 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002253 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002254 if (!fCachedOpsRenderPass) {
2255 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002256 }
2257
Chris Daltonb50cc812019-10-14 16:29:21 -06002258 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002259 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002260}
2261
Brian Salomon1fabd512018-02-09 09:54:25 -05002262void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002263 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002264 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002265 this->didWriteToSurface(target, origin, &bounds);
2266}
bsalomon6ba6fa12015-03-04 11:57:37 -08002267
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002268void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2269 this->flushRenderTargetNoColorWrites(target);
2270 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002271}
2272
Brian Osman9aa30c62018-07-02 15:21:46 -04002273void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002274 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002275 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002276 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002277 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002278#ifdef SK_DEBUG
2279 // don't do this check in Chromium -- this is causing
2280 // lots of repeated command buffer flushes when the compositor is
2281 // rendering with Ganesh, which is really slow; even too slow for
2282 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002283 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002284 GrGLenum status;
2285 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2286 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2287 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2288 }
bsalomon160f24c2015-03-17 15:55:42 -07002289 }
egdanield803f272015-03-18 13:01:52 -07002290#endif
2291 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002292 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002293 }
2294
brianosman35b784d2016-05-05 11:52:53 -07002295 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002296 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002297 }
Brian Salomon9b553272020-01-24 14:38:37 -05002298
2299 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2300 GrGLint format;
2301 GrGLint type;
2302 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2303 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2304 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2305 }
bsalomon083617b2016-02-12 12:10:14 -08002306}
bsalomona9909122016-01-23 10:41:40 -08002307
brianosman33f6b3f2016-06-02 05:49:21 -07002308void GrGLGpu::flushFramebufferSRGB(bool enable) {
2309 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2310 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2311 fHWSRGBFramebuffer = kYes_TriState;
2312 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2313 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2314 fHWSRGBFramebuffer = kNo_TriState;
2315 }
2316}
2317
Greg Danielacd66b42019-05-22 16:29:12 -04002318void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002319 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002320 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002321 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002322 fHWViewport = viewport;
2323 }
2324}
2325
Chris Dalton2e7ed262020-02-21 15:17:59 -07002326void GrGLGpu::drawMesh(GrRenderTarget* renderTarget, GrPrimitiveType primitiveType,
2327 const GrMesh& mesh) {
2328 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2329 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2330 GL_CALL(Enable(GR_GL_CULL_FACE));
2331 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002332 }
ethannicholas22793252016-01-30 09:59:10 -08002333
Chris Dalton2e7ed262020-02-21 15:17:59 -07002334 mesh.sendToGpu(primitiveType, this);
2335 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002336}
2337
Chris Dalton3809bab2017-06-13 10:55:06 -06002338static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2339 switch (primitiveType) {
2340 case GrPrimitiveType::kTriangles:
2341 return GR_GL_TRIANGLES;
2342 case GrPrimitiveType::kTriangleStrip:
2343 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002344 case GrPrimitiveType::kPoints:
2345 return GR_GL_POINTS;
2346 case GrPrimitiveType::kLines:
2347 return GR_GL_LINES;
2348 case GrPrimitiveType::kLineStrip:
2349 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002350 case GrPrimitiveType::kPatches:
2351 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002352 case GrPrimitiveType::kPath:
2353 SK_ABORT("non-mesh-based GrPrimitiveType");
2354 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002355 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002356 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002357}
2358
Chris Dalton79f336d2020-02-11 14:42:56 -07002359void GrGLGpu::sendArrayMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh, int vertexCount,
2360 int baseVertex) {
2361 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002362 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002363 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2364 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002365 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2366 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002367 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002368 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2369 }
2370 fStats.incNumDraws();
2371}
2372
Brian Salomondbf70722019-02-07 11:31:24 -05002373static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2374 size_t baseOffset = baseIndex * sizeof(uint16_t);
2375 if (indexBuffer->isCpuBuffer()) {
2376 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2377 } else {
2378 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2379 }
2380}
2381
Chris Dalton79f336d2020-02-11 14:42:56 -07002382void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2383 int indexCount, int baseIndex, uint16_t minIndexValue,
2384 uint16_t maxIndexValue, int baseVertex) {
2385 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002386 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2387
2388 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2389 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002390
2391 if (this->glCaps().drawRangeElementsSupport()) {
2392 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002393 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002394 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002395 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002396 }
2397 fStats.incNumDraws();
2398}
2399
Chris Dalton79f336d2020-02-11 14:42:56 -07002400void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2401 int vertexCount, int baseVertex, int instanceCount,
2402 int baseInstance) {
2403 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002404 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002405 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002406 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2407 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002408 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
Brian Osman788b9162020-02-07 10:36:46 -05002409 std::min(instanceCount - i, maxInstances)));
Chris Daltoncc604e52017-10-06 16:27:32 -06002410 fStats.incNumDraws();
2411 }
Chris Dalton1d616352017-05-31 12:51:23 -06002412}
2413
Chris Dalton79f336d2020-02-11 14:42:56 -07002414void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2415 int indexCount, int baseIndex, int baseVertex,
2416 int instanceCount, int baseInstance) {
2417 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002418 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002419 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2420 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002421 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2422 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002423 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Brian Osman788b9162020-02-07 10:36:46 -05002424 std::min(instanceCount - i, maxInstances)));
Chris Dalton1b4ad762018-10-04 11:58:09 -06002425 fStats.incNumDraws();
2426 }
Chris Dalton1d616352017-05-31 12:51:23 -06002427}
2428
Chris Dalton16a33c62019-09-24 22:19:17 -06002429void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002430 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002431 // Some extensions automatically resolves the texture when it is read.
2432 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2433
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002434 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002435 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2436 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2437 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2438 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002439
Chris Daltonc139d082019-09-26 14:04:24 -06002440 // make sure we go through flushRenderTarget() since we've modified
2441 // the bound DRAW FBO ID.
2442 fHWBoundRenderTargetUniqueID.makeInvalid();
2443 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2444 // Apple's extension uses the scissor as the blit bounds.
2445 GrScissorState scissorState;
2446 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002447 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2448 // happens inside flushScissor since resolveRect is already in native device coordinates.
2449 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002450 this->disableWindowRectangles();
2451 GL_CALL(ResolveMultisampleFramebuffer());
2452 } else {
2453 int l, b, r, t;
2454 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2455 this->glCaps().blitFramebufferSupportFlags()) {
2456 l = 0;
2457 b = 0;
2458 r = target->width();
2459 t = target->height();
2460 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002461 l = resolveRect.x();
2462 b = resolveRect.y();
2463 r = resolveRect.x() + resolveRect.width();
2464 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002465 }
Chris Daltonc139d082019-09-26 14:04:24 -06002466
2467 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002468 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002469 this->disableWindowRectangles();
2470 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 +00002471 }
2472}
2473
bsalomon@google.com411dad02012-06-05 20:24:20 +00002474namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002475
bsalomon@google.com411dad02012-06-05 20:24:20 +00002476
2477GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002478 static const GrGLenum gTable[kGrStencilOpCount] = {
2479 GR_GL_KEEP, // kKeep
2480 GR_GL_ZERO, // kZero
2481 GR_GL_REPLACE, // kReplace
2482 GR_GL_INVERT, // kInvert
2483 GR_GL_INCR_WRAP, // kIncWrap
2484 GR_GL_DECR_WRAP, // kDecWrap
2485 GR_GL_INCR, // kIncClamp
2486 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002487 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002488 static_assert(0 == (int)GrStencilOp::kKeep);
2489 static_assert(1 == (int)GrStencilOp::kZero);
2490 static_assert(2 == (int)GrStencilOp::kReplace);
2491 static_assert(3 == (int)GrStencilOp::kInvert);
2492 static_assert(4 == (int)GrStencilOp::kIncWrap);
2493 static_assert(5 == (int)GrStencilOp::kDecWrap);
2494 static_assert(6 == (int)GrStencilOp::kIncClamp);
2495 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002496 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2497 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002498}
2499
2500void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002501 const GrStencilSettings::Face& face,
2502 GrGLenum glFace) {
2503 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2504 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2505 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002506
cdalton93a379b2016-05-11 13:58:08 -07002507 GrGLint ref = face.fRef;
2508 GrGLint mask = face.fTestMask;
2509 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002510
2511 if (GR_GL_FRONT_AND_BACK == glFace) {
2512 // we call the combined func just in case separate stencil is not
2513 // supported.
2514 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2515 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002516 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002517 } else {
2518 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2519 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002520 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002521 }
2522}
2523}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002524
Chris Dalton71713f62019-04-18 12:41:03 -06002525void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002526 if (stencilSettings.isDisabled()) {
2527 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002528 } else if (fHWStencilSettings != stencilSettings ||
2529 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002530 if (kYes_TriState != fHWStencilTestEnabled) {
2531 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002532
csmartdaltonc7d85332016-10-26 10:13:46 -07002533 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002534 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002535 if (!stencilSettings.isTwoSided()) {
2536 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2537 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002538 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002539 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2540 GR_GL_FRONT);
2541 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2542 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002543 }
joshualitta58fe352014-10-27 08:39:00 -07002544 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002545 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002546 }
2547}
2548
csmartdaltonc7d85332016-10-26 10:13:46 -07002549void GrGLGpu::disableStencil() {
2550 if (kNo_TriState != fHWStencilTestEnabled) {
2551 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002552
csmartdaltonc7d85332016-10-26 10:13:46 -07002553 fHWStencilTestEnabled = kNo_TriState;
2554 fHWStencilSettings.invalidate();
2555 }
2556}
2557
Chris Dalton4c56b032019-03-04 12:28:42 -07002558void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002559 // rt is only optional if useHWAA is false.
2560 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002561#ifdef SK_DEBUG
2562 if (useHWAA && rt->numSamples() <= 1) {
2563 SkASSERT(this->caps()->mixedSamplesSupport());
2564 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2565 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2566 }
2567#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002568
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002569 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002570 if (useHWAA) {
2571 if (kYes_TriState != fMSAAEnabled) {
2572 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2573 fMSAAEnabled = kYes_TriState;
2574 }
2575 } else {
2576 if (kNo_TriState != fMSAAEnabled) {
2577 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2578 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002579 }
2580 }
2581 }
2582}
2583
Chris Daltonce425af2019-12-16 10:39:03 -07002584void GrGLGpu::flushConservativeRasterState(bool enabled) {
2585 if (this->caps()->conservativeRasterSupport()) {
2586 if (enabled) {
2587 if (kYes_TriState != fHWConservativeRasterEnabled) {
2588 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2589 fHWConservativeRasterEnabled = kYes_TriState;
2590 }
2591 } else {
2592 if (kNo_TriState != fHWConservativeRasterEnabled) {
2593 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2594 fHWConservativeRasterEnabled = kNo_TriState;
2595 }
2596 }
2597 }
2598}
2599
Chris Dalton1215cda2019-12-17 21:44:04 -07002600void GrGLGpu::flushWireframeState(bool enabled) {
2601 if (this->caps()->wireframeSupport()) {
2602 if (this->caps()->wireframeMode() || enabled) {
2603 if (kYes_TriState != fHWWireframeEnabled) {
2604 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2605 fHWWireframeEnabled = kYes_TriState;
2606 }
2607 } else {
2608 if (kNo_TriState != fHWWireframeEnabled) {
2609 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2610 fHWWireframeEnabled = kNo_TriState;
2611 }
2612 }
2613 }
2614}
2615
Chris Daltonbbb3f642019-07-24 12:25:08 -04002616void GrGLGpu::flushBlendAndColorWrite(
2617 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2618 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2619 // We need to work around a driver bug by using a blend state that preserves the dst color,
2620 // rather than disabling color writes.
2621 GrXferProcessor::BlendInfo preserveDstBlend;
2622 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2623 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2624 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2625 return;
2626 }
bsalomonf7cc8772015-05-11 11:21:14 -07002627
cdalton8917d622015-05-06 13:40:21 -07002628 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002629 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2630 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002631
2632 // Any optimization to disable blending should have already been applied and
2633 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002634 bool blendOff =
2635 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2636 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2637 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002638
egdanielb414f252014-07-29 13:15:47 -07002639 if (blendOff) {
2640 if (kNo_TriState != fHWBlendState.fEnabled) {
2641 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002642
2643 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2644 // https://code.google.com/p/skia/issues/detail?id=3943
2645 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2646 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2647 SkASSERT(this->caps()->advancedBlendEquationSupport());
2648 // Set to any basic blending equation.
2649 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2650 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2651 fHWBlendState.fEquation = blend_equation;
2652 }
2653
egdanielb414f252014-07-29 13:15:47 -07002654 fHWBlendState.fEnabled = kNo_TriState;
2655 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002656 } else {
2657 if (kYes_TriState != fHWBlendState.fEnabled) {
2658 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002659
Chris Daltonbbb3f642019-07-24 12:25:08 -04002660 fHWBlendState.fEnabled = kYes_TriState;
2661 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002662
Chris Daltonbbb3f642019-07-24 12:25:08 -04002663 if (fHWBlendState.fEquation != equation) {
2664 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2665 fHWBlendState.fEquation = equation;
2666 }
cdalton8917d622015-05-06 13:40:21 -07002667
Chris Daltonbbb3f642019-07-24 12:25:08 -04002668 if (GrBlendEquationIsAdvanced(equation)) {
2669 SkASSERT(this->caps()->advancedBlendEquationSupport());
2670 // Advanced equations have no other blend state.
2671 return;
2672 }
cdalton8917d622015-05-06 13:40:21 -07002673
Chris Daltonbbb3f642019-07-24 12:25:08 -04002674 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2675 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2676 gXfermodeCoeff2Blend[dstCoeff]));
2677 fHWBlendState.fSrcCoeff = srcCoeff;
2678 fHWBlendState.fDstCoeff = dstCoeff;
2679 }
cdalton8917d622015-05-06 13:40:21 -07002680
Chris Daltonbbb3f642019-07-24 12:25:08 -04002681 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2682 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2683 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2684 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2685 fHWBlendState.fConstColor = blendConst;
2686 fHWBlendState.fConstColorValid = true;
2687 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002688 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002689 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002690
2691 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002692}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002693
Brian Salomondc829942018-10-23 16:07:24 -04002694static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002695 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002696 switch (swizzle[i]) {
2697 case 'r': glValues[i] = GR_GL_RED; break;
2698 case 'g': glValues[i] = GR_GL_GREEN; break;
2699 case 'b': glValues[i] = GR_GL_BLUE; break;
2700 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002701 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002702 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002703 default: SK_ABORT("Unsupported component");
2704 }
Brian Salomon327955b2018-10-22 15:39:22 +00002705 }
2706}
2707
Greg Daniel2c3398d2019-06-19 11:58:01 -04002708void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2709 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002710 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002711
reed856e9d92015-09-30 12:21:45 -07002712#ifdef SK_DEBUG
2713 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002714 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002715 const int w = texture->width();
2716 const int h = texture->height();
2717 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2718 }
2719 }
2720#endif
2721
Robert Phillips294870f2016-11-11 12:38:40 -05002722 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002723 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002724 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002725 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002726 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002727 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002728 }
2729
Brian Salomondc829942018-10-23 16:07:24 -04002730 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002731 if (!this->caps()->mipMapSupport() ||
2732 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002733 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002734 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002735 }
bsalomonefd7d452014-10-23 14:17:46 -07002736
brianosman33f6b3f2016-06-02 05:49:21 -07002737#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002738 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002739 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002740 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002741 }
2742#endif
2743
Brian Salomone2826ab2019-06-04 15:58:31 -04002744 auto timestamp = texture->parameters()->resetTimestamp();
2745 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002746
Brian Salomone2826ab2019-06-04 15:58:31 -04002747 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2748 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002749 if (fSamplerObjectCache) {
2750 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2751 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002752 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2753 texture->parameters()->samplerOverriddenState();
2754 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002755
Brian Salomone2826ab2019-06-04 15:58:31 -04002756 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2757 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002758
Brian Salomone2826ab2019-06-04 15:58:31 -04002759 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2760 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002761
2762 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002763 newSamplerState.fMinLOD = -1000.f;
2764 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002765
Brian Salomone2826ab2019-06-04 15:58:31 -04002766 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002767 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002768 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002769 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002770 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002771 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002772 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002773 }
Mike Klein1bccae52018-10-19 11:38:44 +00002774 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002775 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002776 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002777 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002778 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002779 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002780 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002781 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002782 }
2783 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002784 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002785 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002786 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002787 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002788 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002789 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002790 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002791 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002792 if (this->glCaps().clampToBorderSupport()) {
2793 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002794 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002795 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002796 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2797 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002798 }
2799 }
Brian Salomondc829942018-10-23 16:07:24 -04002800 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002801 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2802 newNonsamplerState.fBaseMipMapLevel = 0;
2803 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002804
Brian Salomone2826ab2019-06-04 15:58:31 -04002805 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2806 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002807 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002808 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2809 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002810 GrGLenum glValues[4];
2811 get_gl_swizzle_values(swizzle, glValues);
2812 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002813 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002814 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002815 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2816 reinterpret_cast<const GrGLint*>(glValues)));
2817 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002818 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2819 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2820 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2821 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2822 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002823 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002824 }
2825 }
Brian Salomondc829942018-10-23 16:07:24 -04002826 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002827 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2828 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2829 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002831 this->setTextureUnit(unitIdx);
2832 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002833 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002834 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002835 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002836 this->setTextureUnit(unitIdx);
2837 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002838 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002839 }
Brian Salomon327955b2018-10-22 15:39:22 +00002840 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002841 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2842 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002843}
2844
Brian Salomon1f05d452019-02-08 12:33:08 -05002845void GrGLGpu::onResetTextureBindings() {
2846 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2847 GR_GL_TEXTURE_EXTERNAL};
2848 for (int i = 0; i < this->numTextureUnits(); ++i) {
2849 this->setTextureUnit(i);
2850 for (auto target : kTargets) {
2851 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2852 GL_CALL(BindTexture(target, 0));
2853 }
2854 }
2855 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2856 }
2857}
2858
Chris Dalton5a2f9622019-12-27 14:56:38 -07002859void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2860 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2861 if (fHWPatchVertexCount != count) {
2862 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2863 fHWPatchVertexCount = count;
2864 }
2865}
2866
egdaniel080e6732014-12-22 07:35:52 -08002867void GrGLGpu::flushColorWrite(bool writeColor) {
2868 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002869 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002870 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2871 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002872 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002873 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002874 } else {
2875 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002876 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002877 fHWWriteToColor = kYes_TriState;
2878 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002879 }
bsalomon3e791242014-12-17 13:43:13 -08002880}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002881
Chris Dalton674f77a2019-09-30 20:49:39 -06002882void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2883 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2884 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2885 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2886 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2887 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2888 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2889 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002890 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2891 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2892 GL_CALL(ClearColor(r, g, b, a));
2893 fHWClearColor[0] = r;
2894 fHWClearColor[1] = g;
2895 fHWClearColor[2] = b;
2896 fHWClearColor[3] = a;
2897 }
2898}
2899
bsalomon861e1032014-12-16 07:33:49 -08002900void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002901 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002902 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002903 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002904 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002905 }
2906}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002907
Brian Salomond978b902019-02-07 15:09:18 -05002908void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002909 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002910 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002911 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2912 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2913 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002914 }
Brian Salomond978b902019-02-07 15:09:18 -05002915 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2916 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002917 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002918 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002919}
2920
Brian Salomone5e7eb12016-10-14 16:18:33 -04002921// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002922static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2923 const GrSurface* src,
2924 const SkIRect& srcRect,
2925 const SkIPoint& dstPoint,
2926 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002927 int dstSampleCnt = 0;
2928 int srcSampleCnt = 0;
2929 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002930 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002931 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002932 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002933 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002934 }
2935 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2936 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2937
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002938 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2939 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2940
Brian Salomone5e7eb12016-10-14 16:18:33 -04002941 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002942 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002943
Greg Daniel46cfbc62019-06-07 11:43:30 -04002944 GrTextureType dstTexType;
2945 GrTextureType* dstTexTypePtr = nullptr;
2946 GrTextureType srcTexType;
2947 GrTextureType* srcTexTypePtr = nullptr;
2948 if (dstTex) {
2949 dstTexType = dstTex->texturePriv().textureType();
2950 dstTexTypePtr = &dstTexType;
2951 }
2952 if (srcTex) {
2953 srcTexType = srcTex->texturePriv().textureType();
2954 srcTexTypePtr = &srcTexType;
2955 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002956
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002957 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2958 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002959 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002960}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002961
Brian Osmana9c8a052018-01-19 10:31:56 -05002962static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2963 // A RT has a separate MSAA renderbuffer if:
2964 // 1) It's multisampled
2965 // 2) We're using an extension with separate MSAA renderbuffers
2966 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002967 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002968}
2969
Greg Daniel46cfbc62019-06-07 11:43:30 -04002970static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002971 const GrGLCaps& caps) {
2972
bsalomon@google.comeb851172013-04-15 13:51:00 +00002973 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002974 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002975 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002976 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002977
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002978 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2979 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2980
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002981 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2982 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2983
Greg Daniel46cfbc62019-06-07 11:43:30 -04002984 GrTextureType dstTexType;
2985 GrTextureType* dstTexTypePtr = nullptr;
2986 GrTextureType srcTexType;
2987 GrTextureType* srcTexTypePtr = nullptr;
2988 if (dstTex) {
2989 dstTexType = dstTex->texturePriv().textureType();
2990 dstTexTypePtr = &dstTexType;
2991 }
2992 if (srcTex) {
2993 srcTexType = srcTex->texturePriv().textureType();
2994 srcTexTypePtr = &srcTexType;
2995 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002996
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002997 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2998 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002999}
3000
Greg Danielacd66b42019-05-22 16:29:12 -04003001// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003002void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003003 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003004 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003005 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003006 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003007 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3008 GrGLuint texID = texture->textureID();
3009 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003010 GrGLuint* tempFBOID;
3011 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003012
egdanield803f272015-03-18 13:01:52 -07003013 if (0 == *tempFBOID) {
3014 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003015 }
3016
Adrienne Walker4ee88512018-05-17 11:37:14 -07003017 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003018 GR_GL_CALL(
3019 this->glInterface(),
3020 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3021 if (mipLevel == 0) {
3022 texture->baseLevelWasBoundToFBO();
3023 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003024 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003025 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003026 }
egdaniel0f5f9672015-02-03 11:10:51 -08003027}
3028
Brian Salomond2a8ae22019-09-10 16:03:59 -04003029void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003030 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003031 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003032 SkASSERT(surface->asTexture());
3033 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3034 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3035 GR_GL_COLOR_ATTACHMENT0,
3036 textureTarget,
3037 0,
3038 0));
3039 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003040}
3041
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003042void GrGLGpu::onFBOChanged() {
3043 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3044 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3045 GL_CALL(Flush());
3046 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003047#ifdef SK_DEBUG
3048 if (fIsExecutingCommandBuffer_DebugOnly) {
3049 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3050 "This will severely hurt performance.\n");
3051 }
3052#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003053}
3054
Adrienne Walker4ee88512018-05-17 11:37:14 -07003055void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3056 fStats.incRenderTargetBinds();
3057 GL_CALL(BindFramebuffer(target, fboid));
3058 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3059 fBoundDrawFramebuffer = fboid;
3060 }
3061
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003062 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3063 // The driver forgets the correct scissor when modifying the FBO binding.
3064 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003065 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003066 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003067 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003068 }
3069
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003070 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003071}
3072
Adrienne Walker4ee88512018-05-17 11:37:14 -07003073void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3074 if (fboid == fBoundDrawFramebuffer &&
3075 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3076 // This workaround only applies to deleting currently bound framebuffers
3077 // on Adreno 420. Because this is a somewhat rare case, instead of
3078 // tracking all the attachments of every framebuffer instead just always
3079 // unbind all attachments.
3080 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3081 GR_GL_RENDERBUFFER, 0));
3082 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3083 GR_GL_RENDERBUFFER, 0));
3084 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3085 GR_GL_RENDERBUFFER, 0));
3086 }
3087
3088 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003089
3090 // Deleting the currently bound framebuffer rebinds to 0.
3091 if (fboid == fBoundDrawFramebuffer) {
3092 this->onFBOChanged();
3093 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003094}
3095
Greg Daniel46cfbc62019-06-07 11:43:30 -04003096bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003097 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003098 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3099 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3100 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003101 auto dstFormat = dst->backendFormat().asGLFormat();
3102 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003103 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003104 return true;
3105 }
3106 }
cblume61214052016-01-26 09:10:48 -08003107
Greg Daniel46cfbc62019-06-07 11:43:30 -04003108 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3109 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003110 return true;
3111 }
3112
Greg Daniel46cfbc62019-06-07 11:43:30 -04003113 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3114 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003115 }
3116
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003117 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003118 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003119 return true;
3120 }
bsalomon083617b2016-02-12 12:10:14 -08003121 }
3122
bsalomon6df86402015-06-01 10:41:49 -07003123 return false;
3124}
3125
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003126bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003127 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003128
3129 int progIdx = TextureToCopyProgramIdx(srcTex);
3130 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3131 GrSLType samplerType =
3132 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3133
3134 if (!fCopyProgramArrayBuffer) {
3135 static const GrGLfloat vdata[] = {
3136 0, 0,
3137 0, 1,
3138 1, 0,
3139 1, 1
3140 };
3141 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3142 kStatic_GrAccessPattern, vdata);
3143 }
3144 if (!fCopyProgramArrayBuffer) {
3145 return false;
3146 }
3147
3148 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3149 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3150 if (!fCopyPrograms[progIdx].fProgram) {
3151 return false;
3152 }
3153
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003154 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3155 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3156 GrShaderVar::kUniform_TypeModifier);
3157 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3158 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3159 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3160 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3161
Greg Daniel9e3169b2019-06-06 14:38:17 -04003162 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003163 if (shaderCaps->noperspectiveInterpolationSupport()) {
3164 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3165 vshaderTxt.appendf("#extension %s : require\n", extension);
3166 }
3167 vTexCoord.addModifier("noperspective");
3168 }
3169
3170 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3171 vshaderTxt.append(";");
3172 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3173 vshaderTxt.append(";");
3174 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3175 vshaderTxt.append(";");
3176 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3177 vshaderTxt.append(";");
3178
3179 vshaderTxt.append(
3180 "// Copy Program VS\n"
3181 "void main() {"
3182 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3183 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3184 " sk_Position.zw = half2(0, 1);"
3185 "}"
3186 );
3187
Greg Daniel9e3169b2019-06-06 14:38:17 -04003188 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003189 if (shaderCaps->noperspectiveInterpolationSupport()) {
3190 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3191 fshaderTxt.appendf("#extension %s : require\n", extension);
3192 }
3193 }
3194 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3195 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3196 fshaderTxt.append(";");
3197 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3198 fshaderTxt.append(";");
3199 fshaderTxt.appendf(
3200 "// Copy Program FS\n"
3201 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003202 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003203 "}"
3204 );
3205
3206 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3207 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3208 SkSL::Program::Settings settings;
3209 settings.fCaps = shaderCaps;
3210 SkSL::String glsl;
3211 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3212 sksl, settings, &glsl, errorHandler);
3213 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3214 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3215 SkASSERT(program->fInputs.isEmpty());
3216
3217 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3218 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3219 errorHandler);
3220 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3221 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3222 errorHandler);
3223 SkASSERT(program->fInputs.isEmpty());
3224
3225 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3226
3227 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3228 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3229 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3230 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3231 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3232 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3233
3234 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3235
3236 GL_CALL(DeleteShader(vshader));
3237 GL_CALL(DeleteShader(fshader));
3238
3239 return true;
3240}
3241
brianosman33f6b3f2016-06-02 05:49:21 -07003242bool GrGLGpu::createMipmapProgram(int progIdx) {
3243 const bool oddWidth = SkToBool(progIdx & 0x2);
3244 const bool oddHeight = SkToBool(progIdx & 0x1);
3245 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3246
Brian Salomon1edc5b92016-11-29 13:43:46 -05003247 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003248
3249 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3250 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3251 if (!fMipmapPrograms[progIdx].fProgram) {
3252 return false;
3253 }
3254
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003255 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3256 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003257 GrShaderVar::kUniform_TypeModifier);
3258 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3259 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003260 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003261 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003262 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3263 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3264 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3265 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003266 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003267 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003268
Ethan Nicholasfc994162019-06-06 10:04:27 -04003269 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003270 if (shaderCaps->noperspectiveInterpolationSupport()) {
3271 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003272 vshaderTxt.appendf("#extension %s : require\n", extension);
3273 }
3274 vTexCoords[0].addModifier("noperspective");
3275 vTexCoords[1].addModifier("noperspective");
3276 vTexCoords[2].addModifier("noperspective");
3277 vTexCoords[3].addModifier("noperspective");
3278 }
3279
Brian Salomon1edc5b92016-11-29 13:43:46 -05003280 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003281 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003282 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003283 vshaderTxt.append(";");
3284 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003285 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003286 vshaderTxt.append(";");
3287 }
3288
3289 vshaderTxt.append(
3290 "// Mipmap Program VS\n"
3291 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003292 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3293 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003294 );
3295
3296 // Insert texture coordinate computation:
3297 if (oddWidth && oddHeight) {
3298 vshaderTxt.append(
3299 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003300 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3301 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003302 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3303 );
3304 } else if (oddWidth) {
3305 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003306 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3307 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003308 );
3309 } else if (oddHeight) {
3310 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003311 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3312 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003313 );
3314 } else {
3315 vshaderTxt.append(
3316 " v_texCoord0 = a_vertex.xy;"
3317 );
3318 }
3319
3320 vshaderTxt.append("}");
3321
Ethan Nicholasfc994162019-06-06 10:04:27 -04003322 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003323 if (shaderCaps->noperspectiveInterpolationSupport()) {
3324 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003325 fshaderTxt.appendf("#extension %s : require\n", extension);
3326 }
3327 }
brianosman33f6b3f2016-06-02 05:49:21 -07003328 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003329 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003330 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003331 fshaderTxt.append(";");
3332 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003333 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003334 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003335 fshaderTxt.append(
3336 "// Mipmap Program FS\n"
3337 "void main() {"
3338 );
3339
3340 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003341 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003342 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3343 " sample(u_texture, v_texCoord1) + "
3344 " sample(u_texture, v_texCoord2) + "
3345 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003346 );
3347 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003348 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003349 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3350 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003351 );
3352 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003353 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003354 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003355 );
3356 }
3357
3358 fshaderTxt.append("}");
3359
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003360 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003361 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003362 SkSL::Program::Settings settings;
3363 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003364 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003365 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003366 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003367 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003368 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003369 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003370
Brian Osman6c431d52019-04-15 16:31:54 -04003371 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003372 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3373 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003374 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003375 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003376 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003377 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003378
3379 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3380
3381 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3382 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3383 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3384 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3385
3386 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3387
3388 GL_CALL(DeleteShader(vshader));
3389 GL_CALL(DeleteShader(fshader));
3390
3391 return true;
3392}
3393
Greg Daniel46cfbc62019-06-07 11:43:30 -04003394bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003395 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003396 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3397 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3398 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3399 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003400 return false;
3401 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003402 int progIdx = TextureToCopyProgramIdx(srcTex);
3403 if (!dstRT) {
3404 SkASSERT(dstTex);
3405 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3406 return false;
3407 }
3408 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003409 if (!fCopyPrograms[progIdx].fProgram) {
3410 if (!this->createCopyProgram(srcTex)) {
3411 SkDebugf("Failed to create copy program.\n");
3412 return false;
3413 }
3414 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003415 int w = srcRect.width();
3416 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003417 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003418 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003419 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003420 this->flushViewport(dst->width(), dst->height());
3421 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003422 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003423 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003424 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003425 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3426 attribs->enableVertexArrays(this, 1);
3427 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3428 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003429 // dst rect edges in NDC (-1 to 1)
3430 int dw = dst->width();
3431 int dh = dst->height();
3432 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3433 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3434 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3435 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003436 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3437 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3438 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3439 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3440 int sw = src->width();
3441 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003442 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3443 // src rect edges in normalized texture space (0 to 1)
3444 sx0 /= sw;
3445 sx1 /= sw;
3446 sy0 /= sh;
3447 sy1 /= sh;
3448 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003449 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3450 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3451 sx1 - sx0, sy1 - sy0, sx0, sy0));
3452 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003453 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003454 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003455 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003456 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003457 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003458 this->disableWindowRectangles();
3459 this->disableStencil();
3460 if (this->glCaps().srgbWriteControl()) {
3461 this->flushFramebufferSRGB(true);
3462 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003463 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003464 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003465 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3466 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003467 return true;
3468}
3469
Greg Daniel46cfbc62019-06-07 11:43:30 -04003470void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003471 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003472 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003473 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003474 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003475 SkASSERT(dstTex);
3476 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003477 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003478
Brian Salomond978b902019-02-07 15:09:18 -05003479 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003480 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003481 dstPoint.fX, dstPoint.fY,
3482 srcRect.fLeft, srcRect.fTop,
3483 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003484 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003485 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3486 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003487 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3488 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003489}
3490
Greg Daniel46cfbc62019-06-07 11:43:30 -04003491bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003492 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003493 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003494 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3495 srcRect.width(), srcRect.height());
3496 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003497 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003498 return false;
mtklein404b3b22015-05-18 09:29:10 -07003499 }
bsalomon5df6fee2015-05-18 06:26:15 -07003500 }
bsalomon6df86402015-06-01 10:41:49 -07003501
Brian Salomond2a8ae22019-09-10 16:03:59 -04003502 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3503 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003504 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003505 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003506
3507 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003508 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003509 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003510
Greg Daniel46cfbc62019-06-07 11:43:30 -04003511 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3512 srcRect.fTop,
3513 srcRect.fRight,
3514 srcRect.fBottom,
3515 dstRect.fLeft,
3516 dstRect.fTop,
3517 dstRect.fRight,
3518 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003519 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003520 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3521 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003522
3523 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3524 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003525 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003526}
3527
Brian Salomon930f9392018-06-20 16:25:26 -04003528bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3529 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003530 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003531 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003532 return false;
3533 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003534 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003535 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3536 // Uses draw calls to do a series of downsample operations to successive mips.
3537
3538 // The manual approach requires the ability to limit which level we're sampling and that the
3539 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003540 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003541 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003542 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003543 GL_CALL(GenerateMipmap(glTex->target()));
3544 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003545 }
3546
brianosman33f6b3f2016-06-02 05:49:21 -07003547 int width = texture->width();
3548 int height = texture->height();
3549 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003550 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003551
3552 // Create (if necessary), then bind temporary FBO:
3553 if (0 == fTempDstFBOID) {
3554 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3555 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003556 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003557 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003558
3559 // Bind the texture, to get things configured for filtering.
3560 // We'll be changing our base level further below:
3561 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003562 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003563 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003564
3565 // Vertex data:
3566 if (!fMipmapProgramArrayBuffer) {
3567 static const GrGLfloat vdata[] = {
3568 0, 0,
3569 0, 1,
3570 1, 0,
3571 1, 1
3572 };
Brian Salomonae64c192019-02-05 09:41:37 -05003573 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003574 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003575 }
3576 if (!fMipmapProgramArrayBuffer) {
3577 return false;
3578 }
3579
3580 fHWVertexArrayState.setVertexArrayID(this, 0);
3581
3582 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003583 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003584 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003585 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003586
3587 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003588 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003589 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003590 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003591 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003592 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003593
3594 // Do all the blits:
3595 width = texture->width();
3596 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003597
brianosman33f6b3f2016-06-02 05:49:21 -07003598 for (GrGLint level = 1; level < levelCount; ++level) {
3599 // Get and bind the program for this particular downsample (filter shape can vary):
3600 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3601 if (!fMipmapPrograms[progIdx].fProgram) {
3602 if (!this->createMipmapProgram(progIdx)) {
3603 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003604 // Invalidate all params to cover base level change in a previous iteration.
3605 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003606 return false;
3607 }
3608 }
Brian Salomon802cb312018-06-08 18:05:20 -04003609 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003610
3611 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3612 const float invWidth = 1.0f / width;
3613 const float invHeight = 1.0f / height;
3614 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3615 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3616 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3617
3618 // Only sample from previous mip
3619 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3620
Brian Salomon930f9392018-06-20 16:25:26 -04003621 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3622 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003623
Brian Osman788b9162020-02-07 10:36:46 -05003624 width = std::max(1, width / 2);
3625 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003626 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003627
3628 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3629 }
3630
3631 // Unbind:
3632 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3633 GR_GL_TEXTURE_2D, 0, 0));
3634
Brian Salomon930f9392018-06-20 16:25:26 -04003635 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003636 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3637 // We drew the 2nd to last level into the last level.
3638 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3639 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003640
brianosman33f6b3f2016-06-02 05:49:21 -07003641 return true;
3642}
3643
Chris Daltond7291ba2019-03-07 14:17:03 -07003644void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003645 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3646 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003647
3648 int effectiveSampleCnt;
3649 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003650 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003651
3652 sampleLocations->reset(effectiveSampleCnt);
3653 for (int i = 0; i < effectiveSampleCnt; ++i) {
3654 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3655 }
3656}
3657
cdalton231c5fd2015-05-13 12:35:36 -07003658void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003659 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003660 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003661 case kTexture_GrXferBarrierType: {
3662 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003663 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003664 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3665 // The render target uses separate storage so no need for glTextureBarrier.
3666 // FIXME: The render target will resolve automatically when its texture is bound,
3667 // but we could resolve only the bounds that will be read if we do it here instead.
3668 return;
3669 }
cdalton9954bc32015-04-29 14:17:00 -07003670 SkASSERT(this->caps()->textureBarrierSupport());
3671 GL_CALL(TextureBarrier());
3672 return;
cdalton231c5fd2015-05-13 12:35:36 -07003673 }
cdalton8917d622015-05-06 13:40:21 -07003674 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003675 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003676 this->caps()->blendEquationSupport());
3677 GL_CALL(BlendBarrier());
3678 return;
bsalomoncb02b382015-08-12 11:14:50 -07003679 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003680 }
3681}
3682
Chris Daltone1196c52019-12-28 14:31:09 -07003683void GrGLGpu::insertManualFramebufferBarrier() {
3684 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3685 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3686}
3687
Brian Salomon85c3d682019-11-04 15:04:54 -05003688GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003689 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003690 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003691 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003692 GrProtected isProtected,
3693 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003694 // We don't support protected textures in GL.
3695 if (isProtected == GrProtected::kYes) {
3696 return {};
3697 }
3698
Brian Salomon8a375832018-03-14 10:21:40 -04003699 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003700
Brian Salomond4764a12019-08-08 12:08:24 -04003701 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003702 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003703 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003704 }
3705
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003706 int numMipLevels = 1;
3707 if (mipMapped == GrMipMapped::kYes) {
3708 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3709 }
3710
Robert Phillipsd34691b2019-09-24 13:38:43 -04003711 // Compressed formats go through onCreateCompressedBackendTexture
3712 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3713
Greg Daniel15500642019-06-27 03:05:55 +00003714 GrGLTextureInfo info;
3715 GrGLTextureParameters::SamplerOverriddenState initialState;
3716
Greg Danield51fa2f2020-01-22 16:53:38 -05003717 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003718 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003719 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003720
Robert Phillipsd34691b2019-09-24 13:38:43 -04003721 info.fTarget = GR_GL_TEXTURE_2D;
3722 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003723 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003724 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003725 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003726 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003727
Robert Phillips42716d42019-12-16 12:19:54 -05003728 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003729 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3730 SkTDArray<GrMipLevel> texels;
3731 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003732 texels.append(numMipLevels);
3733 for (int i = 0; i < numMipLevels; ++i) {
3734 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3735 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003736 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3737 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3738 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003739 GL_CALL(DeleteTextures(1, &info.fID));
3740 return {};
3741 }
3742 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3743 // TODO: Unify this with the clear texture code in onCreateTexture().
3744 GrColorType colorType;
3745 GrGLenum externalFormat, externalType;
3746 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3747 &externalType, &colorType);
3748 if (colorType == GrColorType::kUnknown) {
3749 GL_CALL(DeleteTextures(1, &info.fID));
3750 return {};
3751 }
3752
3753 // Make one tight image at the base size and reuse it for smaller levels.
3754 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3755 auto rb = ii.minRowBytes();
3756 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3757 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3758 GL_CALL(DeleteTextures(1, &info.fID));
3759 return {};
3760 }
3761
3762 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3763 SkISize levelDimensions = dimensions;
3764 for (int i = 0; i < numMipLevels; ++i) {
3765 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3766 levelDimensions.height(), externalFormat, externalType,
3767 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003768 levelDimensions = {std::max(1, levelDimensions.width() /2),
3769 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003770 }
3771 }
3772 // Unbind this texture from the scratch texture unit.
3773 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003774
Brian Salomone2826ab2019-06-04 15:58:31 -04003775 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003776 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003777 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3778 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003779
Brian Salomon85c3d682019-11-04 15:04:54 -05003780 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3781 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003782}
3783
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003784void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003785 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3786
3787 GrGLTextureInfo info;
3788 if (tex.getGLTextureInfo(&info)) {
3789 GL_CALL(DeleteTextures(1, &info.fID));
3790 }
3791}
3792
3793#if GR_TEST_UTILS
3794
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003795bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003796 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003797
Greg Daniel52e16d92018-04-10 09:34:07 -04003798 GrGLTextureInfo info;
3799 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003800 return false;
3801 }
3802
3803 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003804 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003805
3806 return (GR_GL_TRUE == result);
3807}
3808
Brian Salomonf865b052018-03-09 09:01:53 -05003809GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003810 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003811 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3812 return GrBackendRenderTarget(); // invalid
3813 }
Brian Salomon8a375832018-03-14 10:21:40 -04003814 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003815 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003816 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003817 return {};
3818 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003819 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003820 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003821 if (sFormatIdx < 0) {
3822 return {};
3823 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003824 GrGLuint colorID = 0;
3825 GrGLuint stencilID = 0;
3826 auto deleteIDs = [&] {
3827 if (colorID) {
3828 if (useTexture) {
3829 GL_CALL(DeleteTextures(1, &colorID));
3830 } else {
3831 GL_CALL(DeleteRenderbuffers(1, &colorID));
3832 }
Brian Salomonf865b052018-03-09 09:01:53 -05003833 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003834 if (stencilID) {
3835 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003836 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003837 };
3838
3839 if (useTexture) {
3840 GL_CALL(GenTextures(1, &colorID));
3841 } else {
3842 GL_CALL(GenRenderbuffers(1, &colorID));
3843 }
3844 GL_CALL(GenRenderbuffers(1, &stencilID));
3845 if (!stencilID || !colorID) {
3846 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003847 return {};
3848 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003849
Brian Salomonf865b052018-03-09 09:01:53 -05003850 GrGLFramebufferInfo info;
3851 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003852 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003853 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3854 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003855 deleteIDs();
3856 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003857 }
3858
3859 this->invalidateBoundRenderTarget();
3860
Adrienne Walker4ee88512018-05-17 11:37:14 -07003861 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003862 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003863 GrGLTextureParameters::SamplerOverriddenState initialState;
3864 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3865 if (!colorID) {
3866 deleteIDs();
3867 return {};
3868 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003869 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3870 colorID, 0));
3871 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003872 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003873 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003874 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003875 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3876 GR_GL_RENDERBUFFER, colorID));
3877 }
3878 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003879 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003880 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003881 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003882 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003883 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3884 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003885 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003886 }
3887
Brian Salomon93348dd2018-08-29 12:56:23 -04003888 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3889 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3890 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3891 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003892 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003893 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003894
Adrienne Walker4ee88512018-05-17 11:37:14 -07003895 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003896 GrGLenum status;
3897 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3898 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003899 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003900 return {};
3901 }
3902 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003903
Greg Daniel108bb232018-07-03 16:18:29 -04003904 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003905 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003906 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003907}
3908
3909void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003910 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003911 GrGLFramebufferInfo info;
3912 if (backendRT.getGLFramebufferInfo(&info)) {
3913 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003914 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003915 }
3916 }
joshualitt8fd844f2015-12-02 13:36:47 -08003917}
3918
Greg Daniel26b50a42018-03-08 09:49:58 -05003919void GrGLGpu::testingOnly_flushGpuAndSync() {
3920 GL_CALL(Finish());
3921}
Brian Salomonf865b052018-03-09 09:01:53 -05003922#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003923
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003924///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003925
cdaltone2e71c22016-04-07 18:13:29 -07003926GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003927 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003928 GrGLAttribArrayState* attribState;
3929
cdaltone2e71c22016-04-07 18:13:29 -07003930 if (gpu->glCaps().isCoreProfile()) {
3931 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003932 GrGLuint arrayID;
3933 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3934 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003935 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003936 }
cdaltone2e71c22016-04-07 18:13:29 -07003937 if (ibuf) {
3938 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003939 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003940 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003941 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003942 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003943 if (ibuf) {
3944 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003945 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003946 } else {
3947 this->setVertexArrayID(gpu, 0);
3948 }
3949 int attrCount = gpu->glCaps().maxVertexAttributes();
3950 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3951 fDefaultVertexArrayAttribState.resize(attrCount);
3952 }
3953 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003954 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003955 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003956}
bsalomone179a912016-01-20 06:18:10 -08003957
Greg Daniel30a35e82019-11-19 14:12:25 -05003958bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003959 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003960 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003961 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003962 // We call finish if the client told us to sync or if we have a finished proc but don't support
3963 // GLsync objects.
3964 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3965 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3966 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003967 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003968 // After a finish everything previously sent to GL is done.
3969 for (const auto& cb : fFinishCallbacks) {
3970 cb.fCallback(cb.fContext);
3971 this->deleteSync(cb.fSync);
3972 }
3973 fFinishCallbacks.clear();
3974 if (info.fFinishedProc) {
3975 info.fFinishedProc(info.fFinishedContext);
3976 }
3977 } else {
3978 if (info.fFinishedProc) {
3979 FinishCallback callback;
3980 callback.fCallback = info.fFinishedProc;
3981 callback.fContext = info.fFinishedContext;
3982 callback.fSync = (GrGLsync)this->insertFence();
3983 fFinishCallbacks.push_back(callback);
3984 GL_CALL(Flush());
3985 } else if (insertedSemaphore) {
3986 // Must call flush after semaphores in case they are waited on another GL context.
3987 GL_CALL(Flush());
3988 }
3989 // See if any previously inserted finish procs are good to go.
3990 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003991 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003992 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003993}
3994
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003995void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3996 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3997 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3998 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003999}
4000
Greg Daniel6be35232017-03-01 17:01:09 -05004001GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004002 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004003 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004004 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4005 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4006 GrGLuint fence = 0;
4007 GL_CALL(GenFences(1, &fence));
4008 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4009 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4010 } else {
4011 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4012 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004013 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004014 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004015}
4016
Brian Salomonb0d8b762019-05-06 16:58:22 -04004017bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004018 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4019 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4020 if (!timeout) {
4021 if (flush) {
4022 GL_CALL(Flush);
4023 }
4024 GrGLboolean result;
4025 GL_CALL_RET(result, TestFence(nvFence));
4026 return result == GR_GL_TRUE;
4027 }
4028 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4029 // If this really becomes necessary we could poll TestFence().
4030 // FinishFence always flushes so no need to check flush param.
4031 GL_CALL(FinishFence(nvFence));
4032 return true;
4033 } else {
4034 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4035 GrGLenum result;
4036 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4037 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4038 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004039}
4040
4041bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4042 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004043}
4044
4045void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004046 this->deleteSync((GrGLsync)fence);
4047}
4048
Greg Daniel301015c2019-11-18 14:06:46 -05004049std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004050 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004051 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004052}
4053
Greg Daniel301015c2019-11-18 14:06:46 -05004054std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4055 const GrBackendSemaphore& semaphore,
4056 GrResourceProvider::SemaphoreWrapType wrapType,
4057 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004058 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004059 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4060}
4061
Greg Daniel301015c2019-11-18 14:06:46 -05004062void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4063 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004064
Greg Daniel48661b82018-01-22 16:11:35 -05004065 GrGLsync sync;
4066 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4067 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004068}
4069
Greg Daniel301015c2019-11-18 14:06:46 -05004070void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4071 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004072
4073 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4074}
4075
Brian Salomonb0d8b762019-05-06 16:58:22 -04004076void GrGLGpu::checkFinishProcs() {
4077 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4078 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4079 /* timeout = */ 0, /* flush = */ false)) {
4080 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4081 this->deleteSync(fFinishCallbacks.front().fSync);
4082 fFinishCallbacks.pop_front();
4083 }
4084}
4085
Greg Daniel6be35232017-03-01 17:01:09 -05004086void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004087 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4088 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4089 GL_CALL(DeleteFences(1, &nvFence));
4090 } else {
4091 GL_CALL(DeleteSync(sync));
4092 }
jvanverth84741b32016-09-30 08:39:02 -07004093}
Brian Osman13dddce2017-05-09 13:19:50 -04004094
Greg Daniel301015c2019-11-18 14:06:46 -05004095std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004096 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004097 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004098 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004099 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004100 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4101 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004102
4103 return semaphore;
4104}
Robert Phillips646e4292017-06-13 12:44:56 -04004105
4106int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004107 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004108 case kTexture2DSampler_GrSLType:
4109 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004110 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004111 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004112 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004113 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004114 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004115 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004116 }
4117}
Brian Osman71a18892017-08-10 10:23:25 -04004118
Kevin Lubickf4def342018-10-04 12:52:50 -04004119#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004120#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004121void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4122 // We are called by the base class, which has already called beginObject(). We choose to nest
4123 // all of our caps information in a named sub-object.
4124 writer->beginObject("GL GPU");
4125
4126 const GrGLubyte* str;
4127 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4128 writer->appendString("GL_VERSION", (const char*)(str));
4129 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4130 writer->appendString("GL_RENDERER", (const char*)(str));
4131 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4132 writer->appendString("GL_VENDOR", (const char*)(str));
4133 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4134 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4135
4136 writer->appendName("extensions");
4137 glInterface()->fExtensions.dumpJSON(writer);
4138
4139 writer->endObject();
4140}
Kevin Lubickf4def342018-10-04 12:52:50 -04004141#endif