blob: fbc6ff528bd49ef9a274b8b3b2edd4c4d6e3ee87 [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
Chris Dalton20e7a532020-02-28 15:37:53 +0000428 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
Chris Dalton20e7a532020-02-28 15:37:53 +0000504 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;
Brian Salomon3aef3012020-02-27 15:35:02 -0500641 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000642 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000643
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000644 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700645 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700646 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000647 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000648 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000649
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000650 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000651 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400652 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000653 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
654 }
Brian Salomon1047a492019-07-02 12:25:21 -0400655 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000656 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
657 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000658 if (this->glCaps().packFlipYSupport()) {
659 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
660 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000661 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000662
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000663 if (resetBits & kProgram_GrGLBackendState) {
664 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000665 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000666 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400667 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000668}
669
Brian Salomonea4ad302019-08-07 13:04:55 -0400670static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
671 const GrGLCaps& caps, GrGLTexture::Desc* desc,
672 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400673 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400674 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400675 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800676 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000677
Brian Salomonea4ad302019-08-07 13:04:55 -0400678 desc->fSize = {backendTex.width(), backendTex.height()};
679 desc->fTarget = info.fTarget;
680 desc->fID = info.fID;
681 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800682
Brian Salomonea4ad302019-08-07 13:04:55 -0400683 if (desc->fFormat == GrGLFormat::kUnknown) {
684 return false;
685 }
686 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400687 if (!caps.shaderCaps()->externalTextureSupport()) {
688 return false;
689 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400690 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
691 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400692 return false;
693 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400694 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400695 return false;
696 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400697 if (backendTex.isProtected()) {
698 // Not supported in GL backend at this time.
699 return false;
700 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400701
Brian Salomond17f6582017-07-19 18:28:58 -0400702 return true;
703}
704
705sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400706 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400707 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400708 GrGLTexture::Desc desc;
709 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400710 return nullptr;
711 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400712
Brian Salomond17f6582017-07-19 18:28:58 -0400713 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400714 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400715 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400716 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400717 }
bsalomone5286e02016-01-14 09:24:09 -0800718
Greg Daniel177e6952017-10-12 12:27:11 -0400719 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
720 : GrMipMapsStatus::kNotAllocated;
721
Brian Salomonea4ad302019-08-07 13:04:55 -0400722 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400723 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400724 // We don't know what parameters are already set on wrapped textures.
725 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600726 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400727}
728
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500729static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
730 const GrGLCaps& caps, GrGLTexture::Desc* desc,
731 bool skipRectTexSupportCheck = false) {
732 GrGLTextureInfo info;
733 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
734 return false;
735 }
736
737 desc->fSize = {backendTex.width(), backendTex.height()};
738 desc->fTarget = info.fTarget;
739 desc->fID = info.fID;
740 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
741
742 if (desc->fFormat == GrGLFormat::kUnknown) {
743 return false;
744 }
745
746 if (GR_GL_TEXTURE_2D != desc->fTarget) {
747 return false;
748 }
749 if (backendTex.isProtected()) {
750 // Not supported in GL backend at this time.
751 return false;
752 }
753
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500754 return true;
755}
756
Robert Phillipsb915c942019-12-17 14:44:37 -0500757sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
758 GrWrapOwnership ownership,
759 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500760 GrGLTexture::Desc desc;
761 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
762 return nullptr;
763 }
764
765 if (kBorrow_GrWrapOwnership == ownership) {
766 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
767 } else {
768 desc.fOwnership = GrBackendObjectOwnership::kOwned;
769 }
770
771 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
772 : GrMipMapsStatus::kNotAllocated;
773
774 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
775 backendTex.getGLTextureParams(), cacheable,
776 kRead_GrIOType);
777 // We don't know what parameters are already set on wrapped textures.
778 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600779 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500780}
781
Brian Salomond17f6582017-07-19 18:28:58 -0400782sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400783 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400784 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500785 GrWrapOwnership ownership,
786 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400787 const GrGLCaps& caps = this->glCaps();
788
Brian Salomonea4ad302019-08-07 13:04:55 -0400789 GrGLTexture::Desc desc;
790 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800791 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800792 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400793 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
794 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800795
Brian Salomond17f6582017-07-19 18:28:58 -0400796 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400797 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800798 return nullptr;
799 }
bsalomon10528f12015-10-14 12:54:52 -0700800
Brian Osman766fcbb2017-03-13 09:33:09 -0400801 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400802 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400803 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400804 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800805 }
bsalomonb15b4c12014-10-29 12:41:57 -0700806
Robert Phillips0902c982019-07-16 07:47:56 -0400807
Greg Daniel6fa62e22019-08-07 15:52:37 -0400808 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
809 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000810
Brian Salomonea4ad302019-08-07 13:04:55 -0400811 GrGLRenderTarget::IDs rtIDs;
812 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400813 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000814 }
Greg Daniel177e6952017-10-12 12:27:11 -0400815
816 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
817 : GrMipMapsStatus::kNotAllocated;
818
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500819 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400820 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400821 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400822 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400823 // We don't know what parameters are already set on wrapped textures.
824 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600825 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000826}
827
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400828sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
829 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400830 GrGLFramebufferInfo info;
831 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000832 return nullptr;
833 }
834
Brian Salomone8a766b2019-07-19 14:24:36 -0400835 if (backendRT.isProtected()) {
836 // Not supported in GL at this time.
837 return nullptr;
838 }
839
Brian Salomond4764a12019-08-08 12:08:24 -0400840 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400841 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
842 return nullptr;
843 }
844
Brian Salomonea4ad302019-08-07 13:04:55 -0400845 GrGLRenderTarget::IDs rtIDs;
846 rtIDs.fRTFBOID = info.fFBOID;
847 rtIDs.fMSColorRenderbufferID = 0;
848 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
849 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000850
Greg Daniel6fa62e22019-08-07 15:52:37 -0400851 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700852
Brian Salomona56a7462020-02-07 14:17:25 -0500853 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400854 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000855}
856
Greg Daniel7ef28f32017-04-20 16:41:55 +0000857sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400858 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400859 GrColorType colorType) {
860 GrGLTexture::Desc desc;
861 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
862 // with a texture rectangle,we assume the necessary support exists.
863 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800864 return nullptr;
865 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400866
867 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
868 return nullptr;
869 }
870
871 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400872 GrGLRenderTarget::IDs rtIDs;
873 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800874 return nullptr;
875 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500876 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800877}
878
Brian Salomonc320b152018-02-20 14:05:36 -0500879static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700880 if (!glTex) {
881 return false;
882 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000883
bsalomone5286e02016-01-14 09:24:09 -0800884 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
885 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800886 return false;
887 }
888
jvanverth17aa0472016-01-05 10:41:27 -0800889 return true;
890}
891
Brian Salomona9b04b92018-06-01 15:04:28 -0400892bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400893 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400894 const GrMipLevel texels[], int mipLevelCount,
895 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500896 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800897
Brian Salomonc320b152018-02-20 14:05:36 -0500898 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800899 return false;
900 }
901
Brian Salomond978b902019-02-07 15:09:18 -0500902 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000903
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400904 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400905 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400906 glTex->target(), left, top, width, height, srcColorType, texels,
907 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000908}
909
Brian Salomone05ba5a2019-04-08 11:59:07 -0400910bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400911 GrColorType textureColorType, GrColorType bufferColorType,
912 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400913 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914
Jim Van Verth1676cb92019-01-15 13:24:45 -0500915 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400916 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500917
Brian Salomonc320b152018-02-20 14:05:36 -0500918 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400919 return false;
920 }
921
Hal Canaryc36f7e72018-06-18 15:50:09 -0400922 static_assert(sizeof(int) == sizeof(int32_t), "");
923 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 return false;
925 }
926
Brian Salomond978b902019-02-07 15:09:18 -0500927 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400928
929 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500930 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400931 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500932 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400933
Greg Daniel660cc992017-06-26 14:55:05 -0400934 SkDEBUGCODE(
935 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
936 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
937 SkASSERT(bounds.contains(subRect));
938 )
939
Brian Salomonb28cb682019-07-26 12:48:47 -0400940 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400941 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400942 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700943 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400944 return false;
945 }
946
947 bool restoreGLRowLength = false;
948 if (trimRowBytes != rowBytes) {
949 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400950 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400951 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
952 restoreGLRowLength = true;
953 }
954
Greg Danielba88ab62019-07-26 09:14:01 -0400955 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400956 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400957 GrGLenum externalFormat = 0;
958 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400959 this->glCaps().getTexSubImageExternalFormatAndType(
960 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400961 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400962 return false;
963 }
964
Brian Salomon8cbf6622019-07-30 11:03:14 -0400965 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400966 GL_CALL(TexSubImage2D(glTex->target(),
967 0,
968 left, top,
969 width,
970 height,
971 externalFormat, externalType,
972 pixels));
973
974 if (restoreGLRowLength) {
975 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
976 }
977
978 return true;
979}
980
Brian Salomon26de56e2019-04-10 12:14:26 -0400981bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400982 GrColorType surfaceColorType, GrColorType dstColorType,
983 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400984 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
985 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400986 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400987 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
988 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400989}
990
Brian Osman9b560242017-09-05 15:34:52 -0400991void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500992 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
993 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
994 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
995 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400996 }
Brian Osman9b560242017-09-05 15:34:52 -0400997}
998
Brian Salomond2a8ae22019-09-10 16:03:59 -0400999bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
1000 int texHeight, GrGLenum target, int left, int top, int width,
1001 int height, GrColorType srcColorType, const GrMipLevel texels[],
1002 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001003 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -04001004 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001005
Greg Daniel7bfc9132019-08-14 14:23:53 -04001006 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -04001007 SkDEBUGCODE(
1008 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1009 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1010 SkASSERT(bounds.contains(subRect));
1011 )
Robert Phillips590533f2017-07-11 14:22:35 -04001012 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001013 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001014
Brian Osman9b560242017-09-05 15:34:52 -04001015 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001016
cblume55f2d2d2016-02-26 13:20:48 -08001017 const GrGLInterface* interface = this->glInterface();
1018 const GrGLCaps& caps = this->glCaps();
1019
Brian Salomonf77c1462019-08-01 15:19:29 -04001020 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001021
1022 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001023 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001024 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001025
bsalomon5b30c6f2015-12-17 14:17:34 -08001026 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001027 GrGLenum externalFormat;
1028 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001029 this->glCaps().getTexSubImageExternalFormatAndType(
1030 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001031 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001032 return false;
1033 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001034
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001035 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001036 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001037 * because our srcData has extra bytes past each row. If so, we need
1038 * to trim those off here, since GL ES may not let us specify
1039 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001040 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001041 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001042
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001043 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001044 *mipMapsStatus = (mipLevelCount > 1) ?
1045 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001046 }
bsalomone699d0c2016-03-09 06:25:15 -08001047
Brian Salomond2a8ae22019-09-10 16:03:59 -04001048 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001049
Brian Salomond2a8ae22019-09-10 16:03:59 -04001050 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1051 if (!texels[currentMipLevel].fPixels) {
1052 if (mipMapsStatus) {
1053 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001054 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001055 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001056 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001057 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001058 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1059 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001060 const size_t trimRowBytes = currentWidth * bpp;
1061 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1062
1063 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1064 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1065 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1066 restoreGLRowLength = true;
1067 }
1068
1069 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1070 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001071 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001072 if (restoreGLRowLength) {
1073 SkASSERT(caps.writePixelsRowBytesSupport());
1074 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1075 }
1076 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001077}
1078
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001080 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001081 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001082 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001083 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001084 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001085 const GrGLCaps& caps = this->glCaps();
1086
1087 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001088 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001089 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001090 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001091 }
1092
Robert Phillips9f744f72019-12-19 19:14:33 -05001093 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1094 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1095
Greg Daniel7bfc9132019-08-14 14:23:53 -04001096 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001097
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001098 int numMipLevels = 1;
1099 if (mipMapped == GrMipMapped::kYes) {
1100 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1101 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001102
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001103 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001104 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001105
1106 if (useTexStorage) {
1107 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001108 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1109 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001110 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001111 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001112 }
Robert Phillips42716d42019-12-16 12:19:54 -05001113
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001114 size_t offset = 0;
1115 for (int level = 0; level < numMipLevels; ++level) {
1116
Robert Phillips99dead92020-01-27 16:11:57 -05001117 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1118 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001119
Brian Salomond8575452020-02-25 12:13:29 -05001120 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1121 level,
1122 0, // left
1123 0, // top
1124 dimensions.width(),
1125 dimensions.height(),
1126 internalFormat,
1127 SkToInt(levelDataSize),
1128 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001130 if (error != GR_GL_NO_ERROR) {
1131 return false;
1132 }
1133
Robert Phillips9f744f72019-12-19 19:14:33 -05001134 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001135 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001136 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001137 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001138 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001139
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001140 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001141 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1142 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001143
1144 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001145 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1146 level,
1147 internalFormat,
1148 dimensions.width(),
1149 dimensions.height(),
1150 0, // border
1151 SkToInt(levelDataSize),
1152 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001153
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001154 if (error != GR_GL_NO_ERROR) {
1155 return false;
1156 }
1157
Robert Phillips9f744f72019-12-19 19:14:33 -05001158 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001159 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001160 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001161 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001162 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001163}
1164
Brian Salomond8575452020-02-25 12:13:29 -05001165bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001166 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001167 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001168 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001169 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001170 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001171 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1172 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001173 break;
1174 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001175 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1176 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001177 break;
1178 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1179 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001180 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1181 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001182 break;
1183 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001184 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001185 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186 }
Brian Salomond8575452020-02-25 12:13:29 -05001187 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001188}
1189
Brian Salomonea4ad302019-08-07 13:04:55 -04001190bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001191 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 GrGLRenderTarget::IDs* rtIDs) {
1193 rtIDs->fMSColorRenderbufferID = 0;
1194 rtIDs->fRTFBOID = 0;
1195 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1196 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197
bsalomona11e5fc2015-12-18 07:59:41 -08001198 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001199
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001201 goto FAILED;
1202 }
1203
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001204 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001205 goto FAILED;
1206 }
1207
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1209 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 goto FAILED;
1211 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001212
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001213 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1214 // the texture bound to the other. The exception is the IMG multisample extension. With this
1215 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1216 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001217 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001218 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1219 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1220 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 goto FAILED;
1222 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001225 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 }
1227
egdanield803f272015-03-18 13:01:52 -07001228 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001229 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001231 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001232 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001233 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1234 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 goto FAILED;
1236 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001237 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001239 GR_GL_COLOR_ATTACHMENT0,
1240 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001243 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001245 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001246 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1247 GR_GL_COLOR_ATTACHMENT0,
1248 desc.fTarget,
1249 desc.fID,
1250 0,
1251 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001252 } else {
egdanield803f272015-03-18 13:01:52 -07001253 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001254 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001255 desc.fTarget,
1256 desc.fID,
1257 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001258 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259
1260 return true;
1261
1262FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001263 if (rtIDs->fMSColorRenderbufferID) {
1264 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1267 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001269 if (rtIDs->fTexFBOID) {
1270 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 }
1272 return false;
1273}
1274
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001275// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001276static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001277// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001278 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001279}
1280
Brian Salomone2826ab2019-06-04 15:58:31 -04001281static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001282 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001283 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1284 // drivers have a bug where an FBO won't be complete if it includes a
1285 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001286 GrGLTextureParameters::SamplerOverriddenState state;
1287 state.fMinFilter = GR_GL_NEAREST;
1288 state.fMagFilter = GR_GL_NEAREST;
1289 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1290 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1293 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1294 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001295 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001296}
1297
Brian Salomona56a7462020-02-07 14:17:25 -05001298sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001299 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001300 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001301 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001302 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001303 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001304 int mipLevelCount,
1305 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001306 // We don't support protected textures in GL.
1307 if (isProtected == GrProtected::kYes) {
1308 return nullptr;
1309 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001310 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001311
Brian Salomond2a8ae22019-09-10 16:03:59 -04001312 SkASSERT(mipLevelCount > 0);
1313 GrMipMapsStatus mipMapsStatus =
1314 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001315 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001317 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001318 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001319 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001320 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001321 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1322 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001323
Brian Salomona56a7462020-02-07 14:17:25 -05001324 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1325 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001326
1327 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001328 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001329 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001330
Robert Phillips67d52cf2017-06-05 13:38:13 -04001331 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001332 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001333 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001334 GL_CALL(BindTexture(texDesc.fTarget, 0));
1335 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001336
Brian Salomonea4ad302019-08-07 13:04:55 -04001337 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1338 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 return return_null_texture();
1340 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001341 tex = sk_make_sp<GrGLTextureRenderTarget>(
1342 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001343 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001344 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001345 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001347 // The non-sampler params are still at their default values.
1348 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1349 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001350 if (levelClearMask) {
1351 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001352 GrColorType colorType;
1353 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1354 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001355 if (this->glCaps().clearTextureSupport()) {
1356 for (int i = 0; i < mipLevelCount; ++i) {
1357 if (levelClearMask & (1U << i)) {
1358 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1359 nullptr));
1360 }
1361 }
1362 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1363 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001364 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001365 this->disableWindowRectangles();
1366 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001367 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001368 for (int i = 0; i < mipLevelCount; ++i) {
1369 if (levelClearMask & (1U << i)) {
1370 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1371 kDst_TempFBOTarget);
1372 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1373 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1374 }
1375 }
Brian Salomonc2249852019-09-16 11:08:50 -04001376 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001377 } else {
1378 std::unique_ptr<char[]> zeros;
1379 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1380 for (int i = 0; i < mipLevelCount; ++i) {
1381 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001382 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1383 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001384 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1385 // smaller levels that need clearing.
1386 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001387 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001388 size_t size = levelWidth * levelHeight * bpp;
1389 zeros.reset(new char[size]());
1390 }
1391 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1392 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1393 externalFormat, externalType, zeros.get()));
1394 }
Brian Salomona3e29962019-07-16 11:52:08 -04001395 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001396 }
1397 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001398 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399}
1400
Robert Phillips9f744f72019-12-19 19:14:33 -05001401sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001402 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001403 SkBudgeted budgeted,
1404 GrMipMapped mipMapped,
1405 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001406 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001407 // We don't support protected textures in GL.
1408 if (isProtected == GrProtected::kYes) {
1409 return nullptr;
1410 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001411 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001412 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001413 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001414 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001415 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001416 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001417 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001418 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001419 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001420 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001421 return nullptr;
1422 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001423
Robert Phillipsee946932019-12-18 11:16:17 -05001424 // Unbind this texture from the scratch texture unit.
1425 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1426
Robert Phillipse4720c62020-01-14 14:33:24 -05001427 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1428 ? GrMipMapsStatus::kValid
1429 : GrMipMapsStatus::kNotAllocated;
1430
1431 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001432 // The non-sampler params are still at their default values.
1433 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1434 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001435 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001436}
1437
Robert Phillipsb915c942019-12-17 14:44:37 -05001438GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1439 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001440 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001441 GrProtected isProtected,
1442 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001443 // We don't support protected textures in GL.
1444 if (isProtected == GrProtected::kYes) {
1445 return {};
1446 }
1447
1448 this->handleDirtyContext();
1449
1450 GrGLFormat glFormat = format.asGLFormat();
1451 if (glFormat == GrGLFormat::kUnknown) {
1452 return {};
1453 }
1454
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001455 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001456 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001457 SkAutoMalloc am;
1458
1459 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1460 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1461 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001462 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001463 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001464 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1465 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001466
Robert Phillips99dead92020-01-27 16:11:57 -05001467 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1468 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001469
1470 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001471
1472 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1473
1474 rawData = (const char*) am.get();
1475 }
1476
1477 GrGLTextureInfo info;
1478 GrGLTextureParameters::SamplerOverriddenState initialState;
1479
1480 info.fTarget = GR_GL_TEXTURE_2D;
1481 info.fFormat = GrGLFormatToEnum(glFormat);
1482 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001483 mipMapped, &initialState,
1484 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001485 if (!info.fID) {
1486 return {};
1487 }
1488
1489 // Unbind this texture from the scratch texture unit.
1490 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1491
1492 auto parameters = sk_make_sp<GrGLTextureParameters>();
1493 // The non-sampler params are still at their default values.
1494 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1495 fResetTimestampForTextureParameters);
1496
1497 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1498 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001499}
1500
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001501namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502
egdaniel8dc7c3a2015-04-16 11:22:42 -07001503const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001504
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001505void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001506 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001507
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001508 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001509 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001510 (kUnknownBitCount == format->fTotalBits));
1511 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001512 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001513 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1514 (GrGLint*)&format->fStencilBits);
1515 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001516 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001517 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1518 (GrGLint*)&format->fTotalBits);
1519 format->fTotalBits += format->fStencilBits;
1520 } else {
1521 format->fTotalBits = format->fStencilBits;
1522 }
1523 }
1524}
1525}
1526
Brian Salomon5043f1f2019-07-11 21:27:54 -04001527int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001528 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001529 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1530
1531 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001532 // Default to unsupported, set this if we find a stencil format that works.
1533 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001534
Brian Salomond2a8ae22019-09-10 16:03:59 -04001535 GrGLuint colorID =
1536 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1537 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001538 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001539 }
egdanielff1d5472015-09-10 08:37:20 -07001540 // unbind the texture from the texture unit before binding it to the frame buffer
1541 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1542
1543 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001544 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001545 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001546 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001547 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001548 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1549 GR_GL_COLOR_ATTACHMENT0,
1550 GR_GL_TEXTURE_2D,
1551 colorID,
1552 0));
bsalomon30447372015-12-21 09:03:05 -08001553 GrGLuint sbRBID = 0;
1554 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001555
1556 // look over formats till I find a compatible one
1557 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001558 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001559 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001560 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1561 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001562 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1563 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1564 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001565 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001566 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001567 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001568 if (sFmt.fPacked) {
1569 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1570 GR_GL_DEPTH_ATTACHMENT,
1571 GR_GL_RENDERBUFFER, sbRBID));
1572 } else {
1573 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1574 GR_GL_DEPTH_ATTACHMENT,
1575 GR_GL_RENDERBUFFER, 0));
1576 }
1577 GrGLenum status;
1578 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1579 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1580 firstWorkingStencilFormatIndex = i;
1581 break;
1582 }
egdanielff1d5472015-09-10 08:37:20 -07001583 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1584 GR_GL_STENCIL_ATTACHMENT,
1585 GR_GL_RENDERBUFFER, 0));
1586 if (sFmt.fPacked) {
1587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1588 GR_GL_DEPTH_ATTACHMENT,
1589 GR_GL_RENDERBUFFER, 0));
1590 }
egdanielff1d5472015-09-10 08:37:20 -07001591 }
1592 }
bsalomon30447372015-12-21 09:03:05 -08001593 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001596 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1597 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001598 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001599 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001600 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001601}
1602
Brian Salomonea4ad302019-08-07 13:04:55 -04001603GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001604 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001605 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001606 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001607 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001608 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001609 if (format == GrGLFormat::kUnknown) {
1610 return 0;
1611 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001612 GrGLuint id = 0;
1613 GL_CALL(GenTextures(1, &id));
1614 if (!id) {
1615 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001616 }
1617
Brian Salomonea4ad302019-08-07 13:04:55 -04001618 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001619
Brian Salomonea4ad302019-08-07 13:04:55 -04001620 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1621
Robert Phillips42716d42019-12-16 12:19:54 -05001622 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001623 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1624 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001625 GL_CALL(DeleteTextures(1, &id));
1626 return 0;
1627 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001628 }
Robert Phillips42716d42019-12-16 12:19:54 -05001629
Brian Salomonea4ad302019-08-07 13:04:55 -04001630 return id;
1631}
1632
Robert Phillips2716daf2020-01-23 12:53:42 -05001633GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001634 GrGLFormat format,
1635 GrRenderable renderable,
1636 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001637 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001638 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001639 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001640
Brian Salomonea4ad302019-08-07 13:04:55 -04001641 GrGLuint id = 0;
1642 GL_CALL(GenTextures(1, &id));
1643
1644 if (!id) {
1645 return 0;
1646 }
1647
1648 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001649
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001650 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001651 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001652 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001653 }
1654
Brian Salomond2a8ae22019-09-10 16:03:59 -04001655 if (initialState) {
1656 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1657 } else {
1658 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001659 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001660
1661 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1662
1663 bool success = false;
1664 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001665 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001666 auto levelCount = std::max(mipLevelCount, 1);
1667 GrGLenum error =
1668 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1669 dimensions.width(), dimensions.height()));
1670 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001671 } else {
1672 GrGLenum externalFormat, externalType;
1673 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1674 GrGLenum error = GR_GL_NO_ERROR;
1675 if (externalFormat && externalType) {
1676 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1677 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001678 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1679 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001680 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1681 currentWidth, currentHeight, 0, externalFormat,
1682 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001683 }
Brian Salomond8575452020-02-25 12:13:29 -05001684 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001685 }
1686 }
1687 }
1688 if (success) {
1689 return id;
1690 }
1691 GL_CALL(DeleteTextures(1, &id));
1692 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001693}
1694
Chris Daltoneffee202019-07-01 22:28:03 -06001695GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1696 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001697 SkASSERT(width >= rt->width());
1698 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001699
egdaniel8dc7c3a2015-04-16 11:22:42 -07001700 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001701
Brian Salomond4764a12019-08-08 12:08:24 -04001702 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001703 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001704 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001705 }
egdanielff1d5472015-09-10 08:37:20 -07001706
1707 if (!sbDesc.fRenderbufferID) {
1708 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1709 }
1710 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001711 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001712 }
1713 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1714 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001715 // we do this "if" so that we don't call the multisample
1716 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001717 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001718 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1719 width, height)) {
1720 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1721 return nullptr;
1722 }
egdanielff1d5472015-09-10 08:37:20 -07001723 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001724 GrGLenum error = GL_ALLOC_CALL(
1725 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1726 if (error != GR_GL_NO_ERROR) {
1727 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1728 return nullptr;
1729 }
egdanielff1d5472015-09-10 08:37:20 -07001730 }
1731 fStats.incStencilAttachmentCreates();
1732 // After sized formats we attempt an unsized format and take
1733 // whatever sizes GL gives us. In that case we query for the size.
1734 GrGLStencilAttachment::Format format = sFmt;
1735 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001736 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1737 sbDesc,
1738 width,
1739 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001740 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001741 format);
1742 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001743}
1744
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001745////////////////////////////////////////////////////////////////////////////////
1746
Brian Salomondbf70722019-02-07 11:31:24 -05001747sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1748 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001749 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001750}
1751
Chris Dalton2e7ed262020-02-21 15:17:59 -07001752void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1753 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001754 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1755 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1756 fHWScissorSettings.fEnabled = kYes_TriState;
1757 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001758 } else {
1759 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1760 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1761 fHWScissorSettings.fEnabled = kNo_TriState;
1762 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001764}
Brian Salomond818ebf2018-07-02 14:08:49 +00001765
Chris Dalton2e7ed262020-02-21 15:17:59 -07001766void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1767 GrSurfaceOrigin rtOrigin) {
1768 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1769 if (fHWScissorSettings.fRect != nativeScissor) {
1770 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1771 nativeScissor.fHeight));
1772 fHWScissorSettings.fRect = nativeScissor;
1773 }
joshualitt77b13072014-10-27 14:51:01 -07001774}
1775
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001776void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001777 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001778#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779 typedef GrWindowRectsState::Mode Mode;
1780 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1781 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001782
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001783 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001784 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001785 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001786 }
1787
csmartdalton7535f412016-08-23 06:51:00 -07001788 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1789 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001790 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001791 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001792
Chris Daltond6cda8d2019-09-05 02:30:04 -06001793 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001794 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001795 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001796 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001797 }
1798
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001799 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001800 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001801
Greg Danielacd66b42019-05-22 16:29:12 -04001802 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001803#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001804}
1805
1806void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001807#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001808 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001809 return;
1810 }
1811 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001812 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001813#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001814}
1815
Chris Dalton20e7a532020-02-28 15:37:53 +00001816bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001817 this->handleDirtyContext();
1818
1819 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1820 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1821 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001822
Chris Dalton20e7a532020-02-28 15:37:53 +00001823 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
1824 if (!program) {
1825 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1826 return false;
1827 }
1828
1829 this->flushProgram(std::move(program));
1830
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001831 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001832 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1833 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001834
Chris Dalton20e7a532020-02-28 15:37:53 +00001835 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001836
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001837 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001838 GrStencilSettings stencil;
1839 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001840 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1841 stencil.reset(*programInfo.pipeline().getUserStencil(),
1842 programInfo.pipeline().hasStencilClip(),
1843 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001844 }
Robert Phillips901aff02019-10-08 12:32:56 -04001845 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001846 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001847 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1848 glRT, programInfo.origin());
1849 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001850 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001851 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001852
1853 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001854 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001855 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001856
Chris Dalton20e7a532020-02-28 15:37:53 +00001857 return true;
1858}
1859
1860void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1861 if (!program) {
1862 fHWProgram.reset();
1863 fHWProgramID = 0;
1864 return;
1865 }
1866 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1867 if (program == fHWProgram) {
1868 return;
1869 }
1870 auto id = program->programID();
1871 SkASSERT(id);
1872 GL_CALL(UseProgram(id));
1873 fHWProgram = std::move(program);
1874 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001875}
1876
1877void GrGLGpu::flushProgram(GrGLuint id) {
1878 SkASSERT(id);
1879 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001880 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001881 return;
1882 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001883 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001884 GL_CALL(UseProgram(id));
1885 fHWProgramID = id;
1886}
1887
Chris Dalton20e7a532020-02-28 15:37:53 +00001888void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
1889 const GrBuffer* vertexBuffer,
1890 int baseVertex,
1891 const GrBuffer* instanceBuffer,
1892 int baseInstance,
1893 GrPrimitiveRestart enablePrimitiveRestart) {
1894 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
1895
1896 GrGLAttribArrayState* attribState;
1897 if (indexBuffer) {
1898 SkASSERT(indexBuffer->isCpuBuffer() ||
1899 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
1900 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
1901 } else {
1902 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
1903 }
1904
1905 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1906 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
1907
1908 if (int vertexStride = fHWProgram->vertexStride()) {
1909 SkASSERT(vertexBuffer);
1910 SkASSERT(vertexBuffer->isCpuBuffer() ||
1911 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1912 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
1913 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1914 const auto& attrib = fHWProgram->vertexAttribute(i);
1915 static constexpr int kDivisor = 0;
1916 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1917 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
1918 }
1919 }
1920 if (int instanceStride = fHWProgram->instanceStride()) {
1921 SkASSERT(instanceBuffer);
1922 SkASSERT(instanceBuffer->isCpuBuffer() ||
1923 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1924 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
1925 int attribIdx = fHWProgram->numVertexAttributes();
1926 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1927 const auto& attrib = fHWProgram->instanceAttribute(i);
1928 static constexpr int kDivisor = 1;
1929 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1930 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1931 kDivisor);
1932 }
1933 }
1934}
1935
Brian Salomonae64c192019-02-05 09:41:37 -05001936GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001937 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001938
cdaltone2e71c22016-04-07 18:13:29 -07001939 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001940 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001941 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001942 }
cdaltone2e71c22016-04-07 18:13:29 -07001943
Brian Salomonae64c192019-02-05 09:41:37 -05001944 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001945 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001946 if (!bufferState->fBufferZeroKnownBound) {
1947 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1948 bufferState->fBufferZeroKnownBound = true;
1949 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001950 }
Brian Salomondbf70722019-02-07 11:31:24 -05001951 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1952 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001953 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1954 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1955 bufferState->fBufferZeroKnownBound = false;
1956 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001957 }
1958
Brian Salomonae64c192019-02-05 09:41:37 -05001959 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001960}
Brian Salomond818ebf2018-07-02 14:08:49 +00001961
Brian Osman9a9baae2018-11-05 15:06:26 -05001962void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001963 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001964 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001965 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001966 SkASSERT(!this->caps()->performColorClearsAsDraws());
1967 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001968
Brian Salomon43f8bf02017-10-18 08:33:29 -04001969 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001970
Brian Salomon43f8bf02017-10-18 08:33:29 -04001971 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1972
Brian Salomond818ebf2018-07-02 14:08:49 +00001973 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001974 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001975 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001976 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001977 }
Greg Danielacd66b42019-05-22 16:29:12 -04001978 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001979 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001980 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001981 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001982 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001983}
1984
Robert Phillips95214472017-08-08 18:00:03 -04001985void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001986 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1987
Robert Phillips95214472017-08-08 18:00:03 -04001988 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001989 return;
1990 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001991
Chris Dalton674f77a2019-09-30 20:49:39 -06001992 // This should only be called internally when we know we have a stencil buffer.
1993 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001994
bsalomonb0bd4f62014-09-03 07:19:50 -07001995 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001996 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001997
Chris Dalton2e7ed262020-02-21 15:17:59 -07001998 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001999 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002000
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002001 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002002 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002003 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002004 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002005}
2006
Chris Daltonb50cc812019-10-14 16:29:21 -06002007static bool use_tiled_rendering(const GrGLCaps& glCaps,
2008 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2009 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2010 // Otherwise it's faster to just not use it.
2011 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2012 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2013}
2014
2015void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002016 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2017 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2018 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2019
2020 this->handleDirtyContext();
2021
2022 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2023 this->flushRenderTarget(glRT);
2024 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2025
Chris Daltonb50cc812019-10-14 16:29:21 -06002026 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2027 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2028 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2029 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2030 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2031 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2032 nativeBounds.fHeight, preserveMask));
2033 }
2034
Chris Dalton674f77a2019-09-30 20:49:39 -06002035 GrGLbitfield clearMask = 0;
2036 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2037 SkASSERT(!this->caps()->performColorClearsAsDraws());
2038 this->flushClearColor(colorLoadStore.fClearColor);
2039 this->flushColorWrite(true);
2040 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002041 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002042 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2043 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2044 GL_CALL(StencilMask(0xffffffff));
2045 GL_CALL(ClearStencil(0));
2046 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2047 }
2048 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07002049 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06002050 this->disableWindowRectangles();
2051 GL_CALL(Clear(clearMask));
2052 }
2053}
2054
2055void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2056 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2057 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2058 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2059
2060 this->handleDirtyContext();
2061
2062 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2063 // The framebuffer binding changed in the middle of a command buffer. We should have already
2064 // printed a warning during onFBOChanged.
2065 return;
2066 }
2067
2068 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2069 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2070
2071 SkSTArray<2, GrGLenum> discardAttachments;
2072 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2073 discardAttachments.push_back(
2074 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2075 }
2076 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2077 discardAttachments.push_back(
2078 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2079 }
2080
2081 if (!discardAttachments.empty()) {
2082 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2083 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2084 discardAttachments.begin()));
2085 } else {
2086 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2087 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2088 discardAttachments.begin()));
2089 }
2090 }
2091 }
2092
Chris Daltonb50cc812019-10-14 16:29:21 -06002093 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2094 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2095 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2096 // Handled by use_tiled_rendering().
2097 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2098 GL_CALL(EndTiling(preserveMask));
2099 }
2100
Chris Dalton674f77a2019-09-30 20:49:39 -06002101 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002102}
2103
csmartdalton29df7602016-08-31 11:55:52 -07002104void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2105 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002106 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002107 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002108 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002109 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002110
egdaniel8dc7c3a2015-04-16 11:22:42 -07002111 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002112 if (!sb) {
2113 // We should only get here if we marked a proxy as requiring a SB. However,
2114 // the SB creation could later fail. Likely clipping is going to go awry now.
2115 return;
2116 }
2117
bsalomon6bc1b5f2015-02-23 09:06:38 -08002118 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002119#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002120 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002121 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002122#else
2123 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002124 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002125 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002126 // changing the clip between stencil passes may or may not
2127 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002128 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002129#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002130 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002131 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002132 value = (1 << (stencilBitCount - 1));
2133 } else {
2134 value = 0;
2135 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002136 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002137 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002138
Greg Danielacd66b42019-05-22 16:29:12 -04002139 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002140 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002141
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002142 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002143 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002144 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002145 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002146}
2147
Brian Salomone05ba5a2019-04-08 11:59:07 -04002148bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002149 GrColorType surfaceColorType, GrColorType dstColorType,
2150 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002151 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002152
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002153 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002154 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002155 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002156 return false;
2157 }
Greg Danielba88ab62019-07-26 09:14:01 -04002158 GrGLenum externalFormat = 0;
2159 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002160 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2161 surfaceColorType,
2162 dstColorType,
2163 &externalFormat,
2164 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002165 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002166 return false;
2167 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002168
Brian Salomon71d9d842016-11-03 13:42:00 -04002169 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002170 if (renderTarget->numSamples() <= 1 ||
2171 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2172 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2173 this->flushRenderTargetNoColorWrites(renderTarget);
2174 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2175 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2176 return false;
2177 } else {
2178 SkASSERT(renderTarget->requiresManualMSAAResolve());
2179 // we don't track the state of the READ FBO ID.
2180 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002181 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002182 } else {
2183 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002184 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002185 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002186 }
2187
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002188 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002189 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002190
Brian Salomona6948702018-06-01 15:33:20 -04002191 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002192 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002193 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002194 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002195 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002196 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002197
Brian Osman1348ed02018-09-12 09:08:39 -04002198 bool reattachStencil = false;
2199 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2200 renderTarget &&
2201 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002202 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002203 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2204 reattachStencil = true;
2205 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2206 GR_GL_RENDERBUFFER, 0));
2207 }
2208
Chris Dalton76500e52019-09-05 02:13:05 -06002209 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002210 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002211
2212 if (reattachStencil) {
2213 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2214 renderTarget->renderTargetPriv().getStencilAttachment());
2215 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2216 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2217 }
2218
Brian Salomon26de56e2019-04-10 12:14:26 -04002219 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002220 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002221 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2222 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002223
Brian Salomone05ba5a2019-04-08 11:59:07 -04002224 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002225 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002226 }
2227 return true;
2228}
2229
2230bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002231 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2232 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002233 SkASSERT(surface);
2234
Brian Salomonb28cb682019-07-26 12:48:47 -04002235 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002236
Brian Salomon26de56e2019-04-10 12:14:26 -04002237 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2238 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002239
Brian Salomon1047a492019-07-02 12:25:21 -04002240 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002241 rowPixelWidth = width;
2242 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002243 SkASSERT(!(rowBytes % bytesPerPixel));
2244 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002245 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002246 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2247 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002248}
2249
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002250GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002251 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002252 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002253 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002254 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002255 if (!fCachedOpsRenderPass) {
2256 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002257 }
2258
Chris Daltonb50cc812019-10-14 16:29:21 -06002259 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002260 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002261}
2262
Brian Salomon1fabd512018-02-09 09:54:25 -05002263void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002264 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002265 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002266 this->didWriteToSurface(target, origin, &bounds);
2267}
bsalomon6ba6fa12015-03-04 11:57:37 -08002268
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002269void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2270 this->flushRenderTargetNoColorWrites(target);
2271 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002272}
2273
Brian Osman9aa30c62018-07-02 15:21:46 -04002274void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002275 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002276 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002277 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002278 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002279#ifdef SK_DEBUG
2280 // don't do this check in Chromium -- this is causing
2281 // lots of repeated command buffer flushes when the compositor is
2282 // rendering with Ganesh, which is really slow; even too slow for
2283 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002284 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002285 GrGLenum status;
2286 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2287 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2288 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2289 }
bsalomon160f24c2015-03-17 15:55:42 -07002290 }
egdanield803f272015-03-18 13:01:52 -07002291#endif
2292 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002293 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002294 }
2295
brianosman35b784d2016-05-05 11:52:53 -07002296 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002297 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002298 }
Brian Salomon9b553272020-01-24 14:38:37 -05002299
2300 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2301 GrGLint format;
2302 GrGLint type;
2303 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2304 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2305 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2306 }
bsalomon083617b2016-02-12 12:10:14 -08002307}
bsalomona9909122016-01-23 10:41:40 -08002308
brianosman33f6b3f2016-06-02 05:49:21 -07002309void GrGLGpu::flushFramebufferSRGB(bool enable) {
2310 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2311 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2312 fHWSRGBFramebuffer = kYes_TriState;
2313 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2314 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2315 fHWSRGBFramebuffer = kNo_TriState;
2316 }
2317}
2318
Greg Danielacd66b42019-05-22 16:29:12 -04002319void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002320 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002321 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002322 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002323 fHWViewport = viewport;
2324 }
2325}
2326
Chris Dalton33db9fc2020-02-25 18:52:12 -07002327GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07002328 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 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002333 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002334
Chris Dalton3809bab2017-06-13 10:55:06 -06002335 switch (primitiveType) {
2336 case GrPrimitiveType::kTriangles:
2337 return GR_GL_TRIANGLES;
2338 case GrPrimitiveType::kTriangleStrip:
2339 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002340 case GrPrimitiveType::kPoints:
2341 return GR_GL_POINTS;
2342 case GrPrimitiveType::kLines:
2343 return GR_GL_LINES;
2344 case GrPrimitiveType::kLineStrip:
2345 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002346 case GrPrimitiveType::kPatches:
2347 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002348 case GrPrimitiveType::kPath:
2349 SK_ABORT("non-mesh-based GrPrimitiveType");
2350 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002351 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002352 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002353}
2354
Chris Dalton20e7a532020-02-28 15:37:53 +00002355void GrGLGpu::draw(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer, int vertexCount,
2356 int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -07002357 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Dalton20e7a532020-02-28 15:37:53 +00002358 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
2359 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
2360 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2361 } else {
2362 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
2363 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2364 }
2365 fStats.incNumDraws();
Chris Dalton114a3c02017-05-26 15:17:19 -06002366}
2367
Chris Dalton20e7a532020-02-28 15:37:53 +00002368static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2369 size_t baseOffset = baseIndex * sizeof(uint16_t);
2370 if (indexBuffer->isCpuBuffer()) {
2371 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2372 } else {
2373 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2374 }
Brian Salomondbf70722019-02-07 11:31:24 -05002375}
2376
Chris Dalton20e7a532020-02-28 15:37:53 +00002377void GrGLGpu::drawIndexed(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2378 int indexCount, int baseIndex, GrPrimitiveRestart primitiveRestart,
2379 uint16_t minIndexValue, uint16_t maxIndexValue,
2380 const GrBuffer* vertexBuffer, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -07002381 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Dalton20e7a532020-02-28 15:37:53 +00002382 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
2383 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, primitiveRestart);
2384 if (this->glCaps().drawRangeElementsSupport()) {
2385 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2386 GR_GL_UNSIGNED_SHORT, elementPtr));
2387 } else {
2388 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
2389 }
2390 fStats.incNumDraws();
Chris Dalton114a3c02017-05-26 15:17:19 -06002391}
2392
Chris Dalton20e7a532020-02-28 15:37:53 +00002393void GrGLGpu::drawInstanced(GrPrimitiveType primitiveType, const GrBuffer* instanceBuffer,
2394 int instanceCount, int baseInstance, const GrBuffer* vertexBuffer,
2395 int vertexCount, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -07002396 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Dalton20e7a532020-02-28 15:37:53 +00002397 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2398 for (int i = 0; i < instanceCount; i += maxInstances) {
2399 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2400 GrPrimitiveRestart::kNo);
2401 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2402 std::min(instanceCount - i, maxInstances)));
2403 fStats.incNumDraws();
2404 }
Chris Dalton1d616352017-05-31 12:51:23 -06002405}
2406
Chris Dalton20e7a532020-02-28 15:37:53 +00002407void GrGLGpu::drawIndexedInstanced(
2408 GrPrimitiveType primitiveType, const GrBuffer* indexBuffer, int indexCount, int baseIndex,
2409 GrPrimitiveRestart primitiveRestart, const GrBuffer* instanceBuffer, int instanceCount,
2410 int baseInstance, const GrBuffer* vertexBuffer, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -07002411 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Dalton20e7a532020-02-28 15:37:53 +00002412 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
2413 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2414 for (int i = 0; i < instanceCount; i += maxInstances) {
2415 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex,
2416 instanceBuffer, baseInstance + i, primitiveRestart);
2417 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
2418 std::min(instanceCount - i, maxInstances)));
2419 fStats.incNumDraws();
2420 }
Chris Dalton1d616352017-05-31 12:51:23 -06002421}
2422
Chris Dalton16a33c62019-09-24 22:19:17 -06002423void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002424 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002425 // Some extensions automatically resolves the texture when it is read.
2426 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2427
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002428 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002429 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2430 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2431 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2432 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002433
Chris Daltonc139d082019-09-26 14:04:24 -06002434 // make sure we go through flushRenderTarget() since we've modified
2435 // the bound DRAW FBO ID.
2436 fHWBoundRenderTargetUniqueID.makeInvalid();
2437 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2438 // Apple's extension uses the scissor as the blit bounds.
2439 GrScissorState scissorState;
2440 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002441 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2442 // happens inside flushScissor since resolveRect is already in native device coordinates.
2443 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002444 this->disableWindowRectangles();
2445 GL_CALL(ResolveMultisampleFramebuffer());
2446 } else {
2447 int l, b, r, t;
2448 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2449 this->glCaps().blitFramebufferSupportFlags()) {
2450 l = 0;
2451 b = 0;
2452 r = target->width();
2453 t = target->height();
2454 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002455 l = resolveRect.x();
2456 b = resolveRect.y();
2457 r = resolveRect.x() + resolveRect.width();
2458 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002459 }
Chris Daltonc139d082019-09-26 14:04:24 -06002460
2461 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002462 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002463 this->disableWindowRectangles();
2464 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 +00002465 }
2466}
2467
bsalomon@google.com411dad02012-06-05 20:24:20 +00002468namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002469
bsalomon@google.com411dad02012-06-05 20:24:20 +00002470
2471GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002472 static const GrGLenum gTable[kGrStencilOpCount] = {
2473 GR_GL_KEEP, // kKeep
2474 GR_GL_ZERO, // kZero
2475 GR_GL_REPLACE, // kReplace
2476 GR_GL_INVERT, // kInvert
2477 GR_GL_INCR_WRAP, // kIncWrap
2478 GR_GL_DECR_WRAP, // kDecWrap
2479 GR_GL_INCR, // kIncClamp
2480 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002481 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002482 static_assert(0 == (int)GrStencilOp::kKeep);
2483 static_assert(1 == (int)GrStencilOp::kZero);
2484 static_assert(2 == (int)GrStencilOp::kReplace);
2485 static_assert(3 == (int)GrStencilOp::kInvert);
2486 static_assert(4 == (int)GrStencilOp::kIncWrap);
2487 static_assert(5 == (int)GrStencilOp::kDecWrap);
2488 static_assert(6 == (int)GrStencilOp::kIncClamp);
2489 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002490 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2491 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002492}
2493
2494void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002495 const GrStencilSettings::Face& face,
2496 GrGLenum glFace) {
2497 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2498 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2499 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002500
cdalton93a379b2016-05-11 13:58:08 -07002501 GrGLint ref = face.fRef;
2502 GrGLint mask = face.fTestMask;
2503 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002504
2505 if (GR_GL_FRONT_AND_BACK == glFace) {
2506 // we call the combined func just in case separate stencil is not
2507 // supported.
2508 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2509 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002510 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002511 } else {
2512 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2513 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002514 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002515 }
2516}
2517}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002518
Chris Dalton71713f62019-04-18 12:41:03 -06002519void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002520 if (stencilSettings.isDisabled()) {
2521 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002522 } else if (fHWStencilSettings != stencilSettings ||
2523 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002524 if (kYes_TriState != fHWStencilTestEnabled) {
2525 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002526
csmartdaltonc7d85332016-10-26 10:13:46 -07002527 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002528 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002529 if (!stencilSettings.isTwoSided()) {
2530 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2531 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002532 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002533 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2534 GR_GL_FRONT);
2535 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2536 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002537 }
joshualitta58fe352014-10-27 08:39:00 -07002538 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002539 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002540 }
2541}
2542
csmartdaltonc7d85332016-10-26 10:13:46 -07002543void GrGLGpu::disableStencil() {
2544 if (kNo_TriState != fHWStencilTestEnabled) {
2545 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002546
csmartdaltonc7d85332016-10-26 10:13:46 -07002547 fHWStencilTestEnabled = kNo_TriState;
2548 fHWStencilSettings.invalidate();
2549 }
2550}
2551
Chris Dalton4c56b032019-03-04 12:28:42 -07002552void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002553 // rt is only optional if useHWAA is false.
2554 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002555#ifdef SK_DEBUG
2556 if (useHWAA && rt->numSamples() <= 1) {
2557 SkASSERT(this->caps()->mixedSamplesSupport());
2558 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2559 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2560 }
2561#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002562
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002563 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002564 if (useHWAA) {
2565 if (kYes_TriState != fMSAAEnabled) {
2566 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2567 fMSAAEnabled = kYes_TriState;
2568 }
2569 } else {
2570 if (kNo_TriState != fMSAAEnabled) {
2571 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2572 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002573 }
2574 }
2575 }
2576}
2577
Chris Daltonce425af2019-12-16 10:39:03 -07002578void GrGLGpu::flushConservativeRasterState(bool enabled) {
2579 if (this->caps()->conservativeRasterSupport()) {
2580 if (enabled) {
2581 if (kYes_TriState != fHWConservativeRasterEnabled) {
2582 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2583 fHWConservativeRasterEnabled = kYes_TriState;
2584 }
2585 } else {
2586 if (kNo_TriState != fHWConservativeRasterEnabled) {
2587 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2588 fHWConservativeRasterEnabled = kNo_TriState;
2589 }
2590 }
2591 }
2592}
2593
Chris Dalton1215cda2019-12-17 21:44:04 -07002594void GrGLGpu::flushWireframeState(bool enabled) {
2595 if (this->caps()->wireframeSupport()) {
2596 if (this->caps()->wireframeMode() || enabled) {
2597 if (kYes_TriState != fHWWireframeEnabled) {
2598 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2599 fHWWireframeEnabled = kYes_TriState;
2600 }
2601 } else {
2602 if (kNo_TriState != fHWWireframeEnabled) {
2603 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2604 fHWWireframeEnabled = kNo_TriState;
2605 }
2606 }
2607 }
2608}
2609
Chris Daltonbbb3f642019-07-24 12:25:08 -04002610void GrGLGpu::flushBlendAndColorWrite(
2611 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2612 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2613 // We need to work around a driver bug by using a blend state that preserves the dst color,
2614 // rather than disabling color writes.
2615 GrXferProcessor::BlendInfo preserveDstBlend;
2616 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2617 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2618 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2619 return;
2620 }
bsalomonf7cc8772015-05-11 11:21:14 -07002621
cdalton8917d622015-05-06 13:40:21 -07002622 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002623 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2624 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002625
2626 // Any optimization to disable blending should have already been applied and
2627 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002628 bool blendOff =
2629 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2630 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2631 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002632
egdanielb414f252014-07-29 13:15:47 -07002633 if (blendOff) {
2634 if (kNo_TriState != fHWBlendState.fEnabled) {
2635 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002636
2637 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2638 // https://code.google.com/p/skia/issues/detail?id=3943
2639 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2640 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2641 SkASSERT(this->caps()->advancedBlendEquationSupport());
2642 // Set to any basic blending equation.
2643 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2644 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2645 fHWBlendState.fEquation = blend_equation;
2646 }
2647
egdanielb414f252014-07-29 13:15:47 -07002648 fHWBlendState.fEnabled = kNo_TriState;
2649 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002650 } else {
2651 if (kYes_TriState != fHWBlendState.fEnabled) {
2652 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002653
Chris Daltonbbb3f642019-07-24 12:25:08 -04002654 fHWBlendState.fEnabled = kYes_TriState;
2655 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002656
Chris Daltonbbb3f642019-07-24 12:25:08 -04002657 if (fHWBlendState.fEquation != equation) {
2658 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2659 fHWBlendState.fEquation = equation;
2660 }
cdalton8917d622015-05-06 13:40:21 -07002661
Chris Daltonbbb3f642019-07-24 12:25:08 -04002662 if (GrBlendEquationIsAdvanced(equation)) {
2663 SkASSERT(this->caps()->advancedBlendEquationSupport());
2664 // Advanced equations have no other blend state.
2665 return;
2666 }
cdalton8917d622015-05-06 13:40:21 -07002667
Chris Daltonbbb3f642019-07-24 12:25:08 -04002668 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2669 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2670 gXfermodeCoeff2Blend[dstCoeff]));
2671 fHWBlendState.fSrcCoeff = srcCoeff;
2672 fHWBlendState.fDstCoeff = dstCoeff;
2673 }
cdalton8917d622015-05-06 13:40:21 -07002674
Chris Daltonbbb3f642019-07-24 12:25:08 -04002675 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2676 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2677 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2678 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2679 fHWBlendState.fConstColor = blendConst;
2680 fHWBlendState.fConstColorValid = true;
2681 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002682 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002683 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002684
2685 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002686}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002687
Brian Salomondc829942018-10-23 16:07:24 -04002688static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002689 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002690 switch (swizzle[i]) {
2691 case 'r': glValues[i] = GR_GL_RED; break;
2692 case 'g': glValues[i] = GR_GL_GREEN; break;
2693 case 'b': glValues[i] = GR_GL_BLUE; break;
2694 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002695 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002696 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002697 default: SK_ABORT("Unsupported component");
2698 }
Brian Salomon327955b2018-10-22 15:39:22 +00002699 }
2700}
2701
Greg Daniel2c3398d2019-06-19 11:58:01 -04002702void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2703 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002704 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002705
reed856e9d92015-09-30 12:21:45 -07002706#ifdef SK_DEBUG
2707 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002708 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002709 const int w = texture->width();
2710 const int h = texture->height();
2711 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2712 }
2713 }
2714#endif
2715
Robert Phillips294870f2016-11-11 12:38:40 -05002716 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002717 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002718 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002719 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002720 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002721 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002722 }
2723
Brian Salomondc829942018-10-23 16:07:24 -04002724 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002725 if (!this->caps()->mipMapSupport() ||
2726 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002727 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002728 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002729 }
bsalomonefd7d452014-10-23 14:17:46 -07002730
brianosman33f6b3f2016-06-02 05:49:21 -07002731#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002732 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002733 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002734 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002735 }
2736#endif
2737
Brian Salomone2826ab2019-06-04 15:58:31 -04002738 auto timestamp = texture->parameters()->resetTimestamp();
2739 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002740
Brian Salomone2826ab2019-06-04 15:58:31 -04002741 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2742 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002743 if (fSamplerObjectCache) {
2744 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2745 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002746 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2747 texture->parameters()->samplerOverriddenState();
2748 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002749
Brian Salomone2826ab2019-06-04 15:58:31 -04002750 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2751 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002752
Brian Salomone2826ab2019-06-04 15:58:31 -04002753 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2754 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002755
2756 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002757 newSamplerState.fMinLOD = -1000.f;
2758 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002759
Brian Salomone2826ab2019-06-04 15:58:31 -04002760 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002761 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002762 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002763 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002764 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002765 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002766 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002767 }
Mike Klein1bccae52018-10-19 11:38:44 +00002768 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002769 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002770 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002771 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002772 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002773 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002774 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002775 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002776 }
2777 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002778 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002779 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002780 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002781 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002782 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002783 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002784 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002785 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002786 if (this->glCaps().clampToBorderSupport()) {
2787 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002788 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002789 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002790 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2791 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002792 }
2793 }
Brian Salomondc829942018-10-23 16:07:24 -04002794 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002795 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2796 newNonsamplerState.fBaseMipMapLevel = 0;
2797 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002798
Brian Salomone2826ab2019-06-04 15:58:31 -04002799 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2800 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002801 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002802 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2803 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002804 GrGLenum glValues[4];
2805 get_gl_swizzle_values(swizzle, glValues);
2806 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002807 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002808 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002809 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2810 reinterpret_cast<const GrGLint*>(glValues)));
2811 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002812 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2813 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2814 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2815 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2816 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002817 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002818 }
2819 }
Brian Salomondc829942018-10-23 16:07:24 -04002820 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002821 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2822 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2823 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002824 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002825 this->setTextureUnit(unitIdx);
2826 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002827 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002828 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002829 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002830 this->setTextureUnit(unitIdx);
2831 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002832 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002833 }
Brian Salomon327955b2018-10-22 15:39:22 +00002834 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002835 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2836 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002837}
2838
Brian Salomon1f05d452019-02-08 12:33:08 -05002839void GrGLGpu::onResetTextureBindings() {
2840 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2841 GR_GL_TEXTURE_EXTERNAL};
2842 for (int i = 0; i < this->numTextureUnits(); ++i) {
2843 this->setTextureUnit(i);
2844 for (auto target : kTargets) {
2845 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2846 GL_CALL(BindTexture(target, 0));
2847 }
2848 }
2849 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2850 }
2851}
2852
Chris Dalton5a2f9622019-12-27 14:56:38 -07002853void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2854 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2855 if (fHWPatchVertexCount != count) {
2856 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2857 fHWPatchVertexCount = count;
2858 }
2859}
2860
egdaniel080e6732014-12-22 07:35:52 -08002861void GrGLGpu::flushColorWrite(bool writeColor) {
2862 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002863 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002864 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2865 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002866 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002867 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002868 } else {
2869 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002870 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002871 fHWWriteToColor = kYes_TriState;
2872 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002873 }
bsalomon3e791242014-12-17 13:43:13 -08002874}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002875
Chris Dalton674f77a2019-09-30 20:49:39 -06002876void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2877 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2878 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2879 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2880 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2881 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2882 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2883 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002884 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2885 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2886 GL_CALL(ClearColor(r, g, b, a));
2887 fHWClearColor[0] = r;
2888 fHWClearColor[1] = g;
2889 fHWClearColor[2] = b;
2890 fHWClearColor[3] = a;
2891 }
2892}
2893
bsalomon861e1032014-12-16 07:33:49 -08002894void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002895 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002896 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002897 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002898 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002899 }
2900}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002901
Brian Salomond978b902019-02-07 15:09:18 -05002902void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002903 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002904 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002905 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2906 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2907 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002908 }
Brian Salomond978b902019-02-07 15:09:18 -05002909 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2910 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002911 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002912 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002913}
2914
Brian Salomone5e7eb12016-10-14 16:18:33 -04002915// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002916static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2917 const GrSurface* src,
2918 const SkIRect& srcRect,
2919 const SkIPoint& dstPoint,
2920 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002921 int dstSampleCnt = 0;
2922 int srcSampleCnt = 0;
2923 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002924 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002925 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002926 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002927 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002928 }
2929 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2930 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2931
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002932 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2933 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2934
Brian Salomone5e7eb12016-10-14 16:18:33 -04002935 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002936 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002937
Greg Daniel46cfbc62019-06-07 11:43:30 -04002938 GrTextureType dstTexType;
2939 GrTextureType* dstTexTypePtr = nullptr;
2940 GrTextureType srcTexType;
2941 GrTextureType* srcTexTypePtr = nullptr;
2942 if (dstTex) {
2943 dstTexType = dstTex->texturePriv().textureType();
2944 dstTexTypePtr = &dstTexType;
2945 }
2946 if (srcTex) {
2947 srcTexType = srcTex->texturePriv().textureType();
2948 srcTexTypePtr = &srcTexType;
2949 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002950
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002951 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2952 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002953 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002954}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002955
Brian Osmana9c8a052018-01-19 10:31:56 -05002956static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2957 // A RT has a separate MSAA renderbuffer if:
2958 // 1) It's multisampled
2959 // 2) We're using an extension with separate MSAA renderbuffers
2960 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002961 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002962}
2963
Greg Daniel46cfbc62019-06-07 11:43:30 -04002964static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002965 const GrGLCaps& caps) {
2966
bsalomon@google.comeb851172013-04-15 13:51:00 +00002967 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002968 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002969 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002970 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002971
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002972 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2973 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2974
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002975 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2976 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2977
Greg Daniel46cfbc62019-06-07 11:43:30 -04002978 GrTextureType dstTexType;
2979 GrTextureType* dstTexTypePtr = nullptr;
2980 GrTextureType srcTexType;
2981 GrTextureType* srcTexTypePtr = nullptr;
2982 if (dstTex) {
2983 dstTexType = dstTex->texturePriv().textureType();
2984 dstTexTypePtr = &dstTexType;
2985 }
2986 if (srcTex) {
2987 srcTexType = srcTex->texturePriv().textureType();
2988 srcTexTypePtr = &srcTexType;
2989 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002990
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002991 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2992 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002993}
2994
Greg Danielacd66b42019-05-22 16:29:12 -04002995// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002996void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002997 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002998 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002999 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003000 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003001 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3002 GrGLuint texID = texture->textureID();
3003 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003004 GrGLuint* tempFBOID;
3005 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003006
egdanield803f272015-03-18 13:01:52 -07003007 if (0 == *tempFBOID) {
3008 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003009 }
3010
Adrienne Walker4ee88512018-05-17 11:37:14 -07003011 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003012 GR_GL_CALL(
3013 this->glInterface(),
3014 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3015 if (mipLevel == 0) {
3016 texture->baseLevelWasBoundToFBO();
3017 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003018 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003019 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003020 }
egdaniel0f5f9672015-02-03 11:10:51 -08003021}
3022
Brian Salomond2a8ae22019-09-10 16:03:59 -04003023void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003024 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003025 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003026 SkASSERT(surface->asTexture());
3027 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3028 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3029 GR_GL_COLOR_ATTACHMENT0,
3030 textureTarget,
3031 0,
3032 0));
3033 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003034}
3035
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003036void GrGLGpu::onFBOChanged() {
3037 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3038 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3039 GL_CALL(Flush());
3040 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003041#ifdef SK_DEBUG
3042 if (fIsExecutingCommandBuffer_DebugOnly) {
3043 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3044 "This will severely hurt performance.\n");
3045 }
3046#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003047}
3048
Adrienne Walker4ee88512018-05-17 11:37:14 -07003049void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3050 fStats.incRenderTargetBinds();
3051 GL_CALL(BindFramebuffer(target, fboid));
3052 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3053 fBoundDrawFramebuffer = fboid;
3054 }
3055
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003056 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3057 // The driver forgets the correct scissor when modifying the FBO binding.
3058 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003059 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003060 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003061 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003062 }
3063
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003064 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003065}
3066
Adrienne Walker4ee88512018-05-17 11:37:14 -07003067void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05003068 // We're relying on the GL state shadowing being correct in the workaround code below so we
3069 // need to handle a dirty context.
3070 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07003071 if (fboid == fBoundDrawFramebuffer &&
3072 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3073 // This workaround only applies to deleting currently bound framebuffers
3074 // on Adreno 420. Because this is a somewhat rare case, instead of
3075 // tracking all the attachments of every framebuffer instead just always
3076 // unbind all attachments.
3077 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3078 GR_GL_RENDERBUFFER, 0));
3079 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3080 GR_GL_RENDERBUFFER, 0));
3081 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3082 GR_GL_RENDERBUFFER, 0));
3083 }
3084
3085 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003086
3087 // Deleting the currently bound framebuffer rebinds to 0.
3088 if (fboid == fBoundDrawFramebuffer) {
3089 this->onFBOChanged();
3090 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003091}
3092
Greg Daniel46cfbc62019-06-07 11:43:30 -04003093bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003094 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003095 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3096 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3097 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003098 auto dstFormat = dst->backendFormat().asGLFormat();
3099 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003100 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003101 return true;
3102 }
3103 }
cblume61214052016-01-26 09:10:48 -08003104
Greg Daniel46cfbc62019-06-07 11:43:30 -04003105 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3106 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003107 return true;
3108 }
3109
Greg Daniel46cfbc62019-06-07 11:43:30 -04003110 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3111 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003112 }
3113
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003114 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003115 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003116 return true;
3117 }
bsalomon083617b2016-02-12 12:10:14 -08003118 }
3119
bsalomon6df86402015-06-01 10:41:49 -07003120 return false;
3121}
3122
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003123bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003124 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003125
3126 int progIdx = TextureToCopyProgramIdx(srcTex);
3127 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3128 GrSLType samplerType =
3129 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3130
3131 if (!fCopyProgramArrayBuffer) {
3132 static const GrGLfloat vdata[] = {
3133 0, 0,
3134 0, 1,
3135 1, 0,
3136 1, 1
3137 };
3138 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3139 kStatic_GrAccessPattern, vdata);
3140 }
3141 if (!fCopyProgramArrayBuffer) {
3142 return false;
3143 }
3144
3145 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3146 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3147 if (!fCopyPrograms[progIdx].fProgram) {
3148 return false;
3149 }
3150
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003151 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3152 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3153 GrShaderVar::kUniform_TypeModifier);
3154 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3155 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3156 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3157 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3158
Greg Daniel9e3169b2019-06-06 14:38:17 -04003159 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003160 if (shaderCaps->noperspectiveInterpolationSupport()) {
3161 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3162 vshaderTxt.appendf("#extension %s : require\n", extension);
3163 }
3164 vTexCoord.addModifier("noperspective");
3165 }
3166
3167 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3168 vshaderTxt.append(";");
3169 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3170 vshaderTxt.append(";");
3171 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3172 vshaderTxt.append(";");
3173 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3174 vshaderTxt.append(";");
3175
3176 vshaderTxt.append(
3177 "// Copy Program VS\n"
3178 "void main() {"
3179 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3180 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3181 " sk_Position.zw = half2(0, 1);"
3182 "}"
3183 );
3184
Greg Daniel9e3169b2019-06-06 14:38:17 -04003185 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003186 if (shaderCaps->noperspectiveInterpolationSupport()) {
3187 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3188 fshaderTxt.appendf("#extension %s : require\n", extension);
3189 }
3190 }
3191 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3192 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3193 fshaderTxt.append(";");
3194 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3195 fshaderTxt.append(";");
3196 fshaderTxt.appendf(
3197 "// Copy Program FS\n"
3198 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003199 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003200 "}"
3201 );
3202
3203 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3204 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3205 SkSL::Program::Settings settings;
3206 settings.fCaps = shaderCaps;
3207 SkSL::String glsl;
3208 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3209 sksl, settings, &glsl, errorHandler);
3210 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3211 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3212 SkASSERT(program->fInputs.isEmpty());
3213
3214 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3215 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3216 errorHandler);
3217 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3218 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3219 errorHandler);
3220 SkASSERT(program->fInputs.isEmpty());
3221
3222 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3223
3224 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3225 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3226 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3227 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3228 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3229 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3230
3231 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3232
3233 GL_CALL(DeleteShader(vshader));
3234 GL_CALL(DeleteShader(fshader));
3235
3236 return true;
3237}
3238
brianosman33f6b3f2016-06-02 05:49:21 -07003239bool GrGLGpu::createMipmapProgram(int progIdx) {
3240 const bool oddWidth = SkToBool(progIdx & 0x2);
3241 const bool oddHeight = SkToBool(progIdx & 0x1);
3242 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3243
Brian Salomon1edc5b92016-11-29 13:43:46 -05003244 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003245
3246 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3247 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3248 if (!fMipmapPrograms[progIdx].fProgram) {
3249 return false;
3250 }
3251
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003252 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3253 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003254 GrShaderVar::kUniform_TypeModifier);
3255 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3256 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003257 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003258 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003259 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3260 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3261 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3262 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003263 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003264 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003265
Ethan Nicholasfc994162019-06-06 10:04:27 -04003266 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003267 if (shaderCaps->noperspectiveInterpolationSupport()) {
3268 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003269 vshaderTxt.appendf("#extension %s : require\n", extension);
3270 }
3271 vTexCoords[0].addModifier("noperspective");
3272 vTexCoords[1].addModifier("noperspective");
3273 vTexCoords[2].addModifier("noperspective");
3274 vTexCoords[3].addModifier("noperspective");
3275 }
3276
Brian Salomon1edc5b92016-11-29 13:43:46 -05003277 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003278 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003279 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003280 vshaderTxt.append(";");
3281 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003282 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003283 vshaderTxt.append(";");
3284 }
3285
3286 vshaderTxt.append(
3287 "// Mipmap Program VS\n"
3288 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003289 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3290 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003291 );
3292
3293 // Insert texture coordinate computation:
3294 if (oddWidth && oddHeight) {
3295 vshaderTxt.append(
3296 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003297 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3298 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003299 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3300 );
3301 } else if (oddWidth) {
3302 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003303 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3304 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003305 );
3306 } else if (oddHeight) {
3307 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003308 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3309 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003310 );
3311 } else {
3312 vshaderTxt.append(
3313 " v_texCoord0 = a_vertex.xy;"
3314 );
3315 }
3316
3317 vshaderTxt.append("}");
3318
Ethan Nicholasfc994162019-06-06 10:04:27 -04003319 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003320 if (shaderCaps->noperspectiveInterpolationSupport()) {
3321 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003322 fshaderTxt.appendf("#extension %s : require\n", extension);
3323 }
3324 }
brianosman33f6b3f2016-06-02 05:49:21 -07003325 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003326 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003327 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003328 fshaderTxt.append(";");
3329 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003330 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003331 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003332 fshaderTxt.append(
3333 "// Mipmap Program FS\n"
3334 "void main() {"
3335 );
3336
3337 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003338 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003339 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3340 " sample(u_texture, v_texCoord1) + "
3341 " sample(u_texture, v_texCoord2) + "
3342 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003343 );
3344 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003345 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003346 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3347 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003348 );
3349 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003350 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003351 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003352 );
3353 }
3354
3355 fshaderTxt.append("}");
3356
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003357 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003358 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003359 SkSL::Program::Settings settings;
3360 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003361 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003362 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003363 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003364 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003365 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003366 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003367
Brian Osman6c431d52019-04-15 16:31:54 -04003368 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003369 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3370 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003371 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003372 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003373 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003374 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003375
3376 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3377
3378 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3379 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3380 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3381 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3382
3383 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3384
3385 GL_CALL(DeleteShader(vshader));
3386 GL_CALL(DeleteShader(fshader));
3387
3388 return true;
3389}
3390
Greg Daniel46cfbc62019-06-07 11:43:30 -04003391bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003392 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003393 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3394 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3395 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3396 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003397 return false;
3398 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003399 int progIdx = TextureToCopyProgramIdx(srcTex);
3400 if (!dstRT) {
3401 SkASSERT(dstTex);
3402 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3403 return false;
3404 }
3405 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003406 if (!fCopyPrograms[progIdx].fProgram) {
3407 if (!this->createCopyProgram(srcTex)) {
3408 SkDebugf("Failed to create copy program.\n");
3409 return false;
3410 }
3411 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003412 int w = srcRect.width();
3413 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003414 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003415 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003416 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003417 this->flushViewport(dst->width(), dst->height());
3418 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003419 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003420 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003421 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003422 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3423 attribs->enableVertexArrays(this, 1);
3424 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3425 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003426 // dst rect edges in NDC (-1 to 1)
3427 int dw = dst->width();
3428 int dh = dst->height();
3429 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3430 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3431 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3432 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003433 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3434 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3435 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3436 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3437 int sw = src->width();
3438 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003439 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3440 // src rect edges in normalized texture space (0 to 1)
3441 sx0 /= sw;
3442 sx1 /= sw;
3443 sy0 /= sh;
3444 sy1 /= sh;
3445 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003446 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3447 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3448 sx1 - sx0, sy1 - sy0, sx0, sy0));
3449 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003450 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003451 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003452 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003453 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003454 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003455 this->disableWindowRectangles();
3456 this->disableStencil();
3457 if (this->glCaps().srgbWriteControl()) {
3458 this->flushFramebufferSRGB(true);
3459 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003460 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003461 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003462 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3463 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003464 return true;
3465}
3466
Greg Daniel46cfbc62019-06-07 11:43:30 -04003467void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003468 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003469 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003470 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003471 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003472 SkASSERT(dstTex);
3473 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003474 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003475
Brian Salomond978b902019-02-07 15:09:18 -05003476 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003477 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003478 dstPoint.fX, dstPoint.fY,
3479 srcRect.fLeft, srcRect.fTop,
3480 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003481 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003482 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3483 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003484 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3485 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003486}
3487
Greg Daniel46cfbc62019-06-07 11:43:30 -04003488bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003489 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003490 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003491 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3492 srcRect.width(), srcRect.height());
3493 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003494 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003495 return false;
mtklein404b3b22015-05-18 09:29:10 -07003496 }
bsalomon5df6fee2015-05-18 06:26:15 -07003497 }
bsalomon6df86402015-06-01 10:41:49 -07003498
Brian Salomond2a8ae22019-09-10 16:03:59 -04003499 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3500 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003501 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003502 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003503
3504 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003505 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003506 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003507
Greg Daniel46cfbc62019-06-07 11:43:30 -04003508 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3509 srcRect.fTop,
3510 srcRect.fRight,
3511 srcRect.fBottom,
3512 dstRect.fLeft,
3513 dstRect.fTop,
3514 dstRect.fRight,
3515 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003516 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003517 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3518 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003519
3520 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3521 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003522 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003523}
3524
Brian Salomon930f9392018-06-20 16:25:26 -04003525bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3526 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003527 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003528 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003529 return false;
3530 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003531 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003532 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3533 // Uses draw calls to do a series of downsample operations to successive mips.
3534
3535 // The manual approach requires the ability to limit which level we're sampling and that the
3536 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003537 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003538 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003539 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003540 GL_CALL(GenerateMipmap(glTex->target()));
3541 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003542 }
3543
brianosman33f6b3f2016-06-02 05:49:21 -07003544 int width = texture->width();
3545 int height = texture->height();
3546 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003547 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003548
3549 // Create (if necessary), then bind temporary FBO:
3550 if (0 == fTempDstFBOID) {
3551 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3552 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003553 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003554 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003555
3556 // Bind the texture, to get things configured for filtering.
3557 // We'll be changing our base level further below:
3558 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003559 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003560 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003561
3562 // Vertex data:
3563 if (!fMipmapProgramArrayBuffer) {
3564 static const GrGLfloat vdata[] = {
3565 0, 0,
3566 0, 1,
3567 1, 0,
3568 1, 1
3569 };
Brian Salomonae64c192019-02-05 09:41:37 -05003570 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003571 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003572 }
3573 if (!fMipmapProgramArrayBuffer) {
3574 return false;
3575 }
3576
3577 fHWVertexArrayState.setVertexArrayID(this, 0);
3578
3579 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003580 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003581 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003582 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003583
3584 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003585 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003586 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003587 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003588 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003589 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003590
3591 // Do all the blits:
3592 width = texture->width();
3593 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003594
brianosman33f6b3f2016-06-02 05:49:21 -07003595 for (GrGLint level = 1; level < levelCount; ++level) {
3596 // Get and bind the program for this particular downsample (filter shape can vary):
3597 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3598 if (!fMipmapPrograms[progIdx].fProgram) {
3599 if (!this->createMipmapProgram(progIdx)) {
3600 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003601 // Invalidate all params to cover base level change in a previous iteration.
3602 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003603 return false;
3604 }
3605 }
Brian Salomon802cb312018-06-08 18:05:20 -04003606 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003607
3608 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3609 const float invWidth = 1.0f / width;
3610 const float invHeight = 1.0f / height;
3611 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3612 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3613 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3614
3615 // Only sample from previous mip
3616 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3617
Brian Salomon930f9392018-06-20 16:25:26 -04003618 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3619 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003620
Brian Osman788b9162020-02-07 10:36:46 -05003621 width = std::max(1, width / 2);
3622 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003623 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003624
3625 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3626 }
3627
3628 // Unbind:
3629 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3630 GR_GL_TEXTURE_2D, 0, 0));
3631
Brian Salomon930f9392018-06-20 16:25:26 -04003632 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003633 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3634 // We drew the 2nd to last level into the last level.
3635 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3636 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003637
brianosman33f6b3f2016-06-02 05:49:21 -07003638 return true;
3639}
3640
Chris Daltond7291ba2019-03-07 14:17:03 -07003641void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003642 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3643 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003644
3645 int effectiveSampleCnt;
3646 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003647 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003648
3649 sampleLocations->reset(effectiveSampleCnt);
3650 for (int i = 0; i < effectiveSampleCnt; ++i) {
3651 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3652 }
3653}
3654
cdalton231c5fd2015-05-13 12:35:36 -07003655void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003656 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003657 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003658 case kTexture_GrXferBarrierType: {
3659 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003660 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003661 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3662 // The render target uses separate storage so no need for glTextureBarrier.
3663 // FIXME: The render target will resolve automatically when its texture is bound,
3664 // but we could resolve only the bounds that will be read if we do it here instead.
3665 return;
3666 }
cdalton9954bc32015-04-29 14:17:00 -07003667 SkASSERT(this->caps()->textureBarrierSupport());
3668 GL_CALL(TextureBarrier());
3669 return;
cdalton231c5fd2015-05-13 12:35:36 -07003670 }
cdalton8917d622015-05-06 13:40:21 -07003671 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003672 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003673 this->caps()->blendEquationSupport());
3674 GL_CALL(BlendBarrier());
3675 return;
bsalomoncb02b382015-08-12 11:14:50 -07003676 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003677 }
3678}
3679
Chris Daltone1196c52019-12-28 14:31:09 -07003680void GrGLGpu::insertManualFramebufferBarrier() {
3681 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3682 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3683}
3684
Brian Salomon85c3d682019-11-04 15:04:54 -05003685GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003686 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003687 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003688 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003689 GrProtected isProtected,
3690 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003691 // We don't support protected textures in GL.
3692 if (isProtected == GrProtected::kYes) {
3693 return {};
3694 }
3695
Brian Salomon8a375832018-03-14 10:21:40 -04003696 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003697
Brian Salomond4764a12019-08-08 12:08:24 -04003698 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003699 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003700 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003701 }
3702
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003703 int numMipLevels = 1;
3704 if (mipMapped == GrMipMapped::kYes) {
3705 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3706 }
3707
Robert Phillipsd34691b2019-09-24 13:38:43 -04003708 // Compressed formats go through onCreateCompressedBackendTexture
3709 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3710
Greg Daniel15500642019-06-27 03:05:55 +00003711 GrGLTextureInfo info;
3712 GrGLTextureParameters::SamplerOverriddenState initialState;
3713
Greg Danield51fa2f2020-01-22 16:53:38 -05003714 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003715 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003716 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003717
Robert Phillipsd34691b2019-09-24 13:38:43 -04003718 info.fTarget = GR_GL_TEXTURE_2D;
3719 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003720 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003721 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003722 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003723 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003724
Robert Phillips42716d42019-12-16 12:19:54 -05003725 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003726 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3727 SkTDArray<GrMipLevel> texels;
3728 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003729 texels.append(numMipLevels);
3730 for (int i = 0; i < numMipLevels; ++i) {
3731 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3732 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003733 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3734 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3735 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003736 GL_CALL(DeleteTextures(1, &info.fID));
3737 return {};
3738 }
3739 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3740 // TODO: Unify this with the clear texture code in onCreateTexture().
3741 GrColorType colorType;
3742 GrGLenum externalFormat, externalType;
3743 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3744 &externalType, &colorType);
3745 if (colorType == GrColorType::kUnknown) {
3746 GL_CALL(DeleteTextures(1, &info.fID));
3747 return {};
3748 }
3749
3750 // Make one tight image at the base size and reuse it for smaller levels.
3751 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3752 auto rb = ii.minRowBytes();
3753 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3754 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3755 GL_CALL(DeleteTextures(1, &info.fID));
3756 return {};
3757 }
3758
3759 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3760 SkISize levelDimensions = dimensions;
3761 for (int i = 0; i < numMipLevels; ++i) {
3762 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3763 levelDimensions.height(), externalFormat, externalType,
3764 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003765 levelDimensions = {std::max(1, levelDimensions.width() /2),
3766 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003767 }
3768 }
3769 // Unbind this texture from the scratch texture unit.
3770 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003771
Brian Salomone2826ab2019-06-04 15:58:31 -04003772 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003773 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003774 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3775 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003776
Brian Salomon85c3d682019-11-04 15:04:54 -05003777 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3778 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003779}
3780
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003781void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003782 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3783
3784 GrGLTextureInfo info;
3785 if (tex.getGLTextureInfo(&info)) {
3786 GL_CALL(DeleteTextures(1, &info.fID));
3787 }
3788}
3789
3790#if GR_TEST_UTILS
3791
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003792bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003793 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003794
Greg Daniel52e16d92018-04-10 09:34:07 -04003795 GrGLTextureInfo info;
3796 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003797 return false;
3798 }
3799
3800 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003801 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003802
3803 return (GR_GL_TRUE == result);
3804}
3805
Brian Salomonf865b052018-03-09 09:01:53 -05003806GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003807 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003808 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3809 return GrBackendRenderTarget(); // invalid
3810 }
Brian Salomon8a375832018-03-14 10:21:40 -04003811 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003812 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003813 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003814 return {};
3815 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003816 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003817 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003818 if (sFormatIdx < 0) {
3819 return {};
3820 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003821 GrGLuint colorID = 0;
3822 GrGLuint stencilID = 0;
3823 auto deleteIDs = [&] {
3824 if (colorID) {
3825 if (useTexture) {
3826 GL_CALL(DeleteTextures(1, &colorID));
3827 } else {
3828 GL_CALL(DeleteRenderbuffers(1, &colorID));
3829 }
Brian Salomonf865b052018-03-09 09:01:53 -05003830 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003831 if (stencilID) {
3832 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003833 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003834 };
3835
3836 if (useTexture) {
3837 GL_CALL(GenTextures(1, &colorID));
3838 } else {
3839 GL_CALL(GenRenderbuffers(1, &colorID));
3840 }
3841 GL_CALL(GenRenderbuffers(1, &stencilID));
3842 if (!stencilID || !colorID) {
3843 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003844 return {};
3845 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003846
Brian Salomonf865b052018-03-09 09:01:53 -05003847 GrGLFramebufferInfo info;
3848 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003849 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003850 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3851 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003852 deleteIDs();
3853 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003854 }
3855
3856 this->invalidateBoundRenderTarget();
3857
Adrienne Walker4ee88512018-05-17 11:37:14 -07003858 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003859 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003860 GrGLTextureParameters::SamplerOverriddenState initialState;
3861 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3862 if (!colorID) {
3863 deleteIDs();
3864 return {};
3865 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003866 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3867 colorID, 0));
3868 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003869 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003870 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003871 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003872 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3873 GR_GL_RENDERBUFFER, colorID));
3874 }
3875 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003876 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003877 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003878 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003879 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003880 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3881 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003882 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003883 }
3884
Brian Salomon93348dd2018-08-29 12:56:23 -04003885 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3886 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3887 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3888 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003889 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003890 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003891
Adrienne Walker4ee88512018-05-17 11:37:14 -07003892 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003893 GrGLenum status;
3894 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3895 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003896 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003897 return {};
3898 }
3899 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003900
Greg Daniel108bb232018-07-03 16:18:29 -04003901 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003902 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003903 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003904}
3905
3906void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003907 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003908 GrGLFramebufferInfo info;
3909 if (backendRT.getGLFramebufferInfo(&info)) {
3910 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003911 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003912 }
3913 }
joshualitt8fd844f2015-12-02 13:36:47 -08003914}
3915
Greg Daniel26b50a42018-03-08 09:49:58 -05003916void GrGLGpu::testingOnly_flushGpuAndSync() {
3917 GL_CALL(Finish());
3918}
Brian Salomonf865b052018-03-09 09:01:53 -05003919#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003920
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003921///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003922
cdaltone2e71c22016-04-07 18:13:29 -07003923GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003924 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003925 GrGLAttribArrayState* attribState;
3926
cdaltone2e71c22016-04-07 18:13:29 -07003927 if (gpu->glCaps().isCoreProfile()) {
3928 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003929 GrGLuint arrayID;
3930 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3931 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003932 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003933 }
cdaltone2e71c22016-04-07 18:13:29 -07003934 if (ibuf) {
3935 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003936 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003937 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003938 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003939 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003940 if (ibuf) {
3941 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003942 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003943 } else {
3944 this->setVertexArrayID(gpu, 0);
3945 }
3946 int attrCount = gpu->glCaps().maxVertexAttributes();
3947 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3948 fDefaultVertexArrayAttribState.resize(attrCount);
3949 }
3950 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003951 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003952 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003953}
bsalomone179a912016-01-20 06:18:10 -08003954
Greg Daniel30a35e82019-11-19 14:12:25 -05003955bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003956 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003957 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003958 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003959 // We call finish if the client told us to sync or if we have a finished proc but don't support
3960 // GLsync objects.
3961 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3962 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3963 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003964 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003965 // After a finish everything previously sent to GL is done.
3966 for (const auto& cb : fFinishCallbacks) {
3967 cb.fCallback(cb.fContext);
3968 this->deleteSync(cb.fSync);
3969 }
3970 fFinishCallbacks.clear();
3971 if (info.fFinishedProc) {
3972 info.fFinishedProc(info.fFinishedContext);
3973 }
3974 } else {
3975 if (info.fFinishedProc) {
3976 FinishCallback callback;
3977 callback.fCallback = info.fFinishedProc;
3978 callback.fContext = info.fFinishedContext;
3979 callback.fSync = (GrGLsync)this->insertFence();
3980 fFinishCallbacks.push_back(callback);
3981 GL_CALL(Flush());
3982 } else if (insertedSemaphore) {
3983 // Must call flush after semaphores in case they are waited on another GL context.
3984 GL_CALL(Flush());
3985 }
3986 // See if any previously inserted finish procs are good to go.
3987 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003988 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003989 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003990}
3991
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003992void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3993 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3994 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3995 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003996}
3997
Greg Daniel6be35232017-03-01 17:01:09 -05003998GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003999 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004000 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004001 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4002 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4003 GrGLuint fence = 0;
4004 GL_CALL(GenFences(1, &fence));
4005 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4006 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4007 } else {
4008 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4009 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004010 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004011 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004012}
4013
Brian Salomonb0d8b762019-05-06 16:58:22 -04004014bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004015 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4016 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4017 if (!timeout) {
4018 if (flush) {
4019 GL_CALL(Flush);
4020 }
4021 GrGLboolean result;
4022 GL_CALL_RET(result, TestFence(nvFence));
4023 return result == GR_GL_TRUE;
4024 }
4025 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4026 // If this really becomes necessary we could poll TestFence().
4027 // FinishFence always flushes so no need to check flush param.
4028 GL_CALL(FinishFence(nvFence));
4029 return true;
4030 } else {
4031 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4032 GrGLenum result;
4033 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4034 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4035 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004036}
4037
4038bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4039 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004040}
4041
4042void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004043 this->deleteSync((GrGLsync)fence);
4044}
4045
Greg Daniel301015c2019-11-18 14:06:46 -05004046std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004047 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004048 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004049}
4050
Greg Daniel301015c2019-11-18 14:06:46 -05004051std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4052 const GrBackendSemaphore& semaphore,
4053 GrResourceProvider::SemaphoreWrapType wrapType,
4054 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004055 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004056 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4057}
4058
Greg Daniel301015c2019-11-18 14:06:46 -05004059void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4060 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004061
Greg Daniel48661b82018-01-22 16:11:35 -05004062 GrGLsync sync;
4063 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4064 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004065}
4066
Greg Daniel301015c2019-11-18 14:06:46 -05004067void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4068 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004069
4070 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4071}
4072
Brian Salomonb0d8b762019-05-06 16:58:22 -04004073void GrGLGpu::checkFinishProcs() {
4074 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4075 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4076 /* timeout = */ 0, /* flush = */ false)) {
4077 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4078 this->deleteSync(fFinishCallbacks.front().fSync);
4079 fFinishCallbacks.pop_front();
4080 }
4081}
4082
Greg Daniel6be35232017-03-01 17:01:09 -05004083void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004084 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4085 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4086 GL_CALL(DeleteFences(1, &nvFence));
4087 } else {
4088 GL_CALL(DeleteSync(sync));
4089 }
jvanverth84741b32016-09-30 08:39:02 -07004090}
Brian Osman13dddce2017-05-09 13:19:50 -04004091
Greg Daniel301015c2019-11-18 14:06:46 -05004092std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004093 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004094 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004095 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004096 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004097 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4098 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004099
4100 return semaphore;
4101}
Robert Phillips646e4292017-06-13 12:44:56 -04004102
4103int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004104 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004105 case kTexture2DSampler_GrSLType:
4106 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004107 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004108 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004109 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004110 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004111 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004112 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004113 }
4114}
Brian Osman71a18892017-08-10 10:23:25 -04004115
Kevin Lubickf4def342018-10-04 12:52:50 -04004116#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004117#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004118void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4119 // We are called by the base class, which has already called beginObject(). We choose to nest
4120 // all of our caps information in a named sub-object.
4121 writer->beginObject("GL GPU");
4122
4123 const GrGLubyte* str;
4124 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4125 writer->appendString("GL_VERSION", (const char*)(str));
4126 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4127 writer->appendString("GL_RENDERER", (const char*)(str));
4128 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4129 writer->appendString("GL_VENDOR", (const char*)(str));
4130 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4131 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4132
4133 writer->appendName("extensions");
4134 glInterface()->fExtensions.dumpJSON(writer);
4135
4136 writer->endObject();
4137}
Kevin Lubickf4def342018-10-04 12:52:50 -04004138#endif