blob: 60454902320208bb245d34668463d2ba352ed5f0 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040028#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
30#include "src/gpu/GrShaderCaps.h"
31#include "src/gpu/GrSurfaceProxyPriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/gl/GrGLBuffer.h"
34#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
Brian Salomond8575452020-02-25 12:13:29 -050047#define GL_ALLOC_CALL(call) \
48 [&] { \
49 if (this->glCaps().skipErrorChecks()) { \
50 GR_GL_CALL(this->glInterface(), call); \
51 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
52 } else { \
53 GrGLClearErr(this->glInterface()); \
54 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
55 return GR_GL_GET_ERROR(this->glInterface()); \
56 } \
57 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000058
Jim Van Verth32ac83e2016-11-28 15:23:57 -050059//#define USE_NSIGHT
60
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000061///////////////////////////////////////////////////////////////////////////////
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050084 GR_GL_HSL_LUMINOSITY,
85
86 // Illegal... needs to map to something.
87 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070088};
Brian Salomon4dea72a2019-12-18 10:43:10 -050089static_assert(0 == kAdd_GrBlendEquation);
90static_assert(1 == kSubtract_GrBlendEquation);
91static_assert(2 == kReverseSubtract_GrBlendEquation);
92static_assert(3 == kScreen_GrBlendEquation);
93static_assert(4 == kOverlay_GrBlendEquation);
94static_assert(5 == kDarken_GrBlendEquation);
95static_assert(6 == kLighten_GrBlendEquation);
96static_assert(7 == kColorDodge_GrBlendEquation);
97static_assert(8 == kColorBurn_GrBlendEquation);
98static_assert(9 == kHardLight_GrBlendEquation);
99static_assert(10 == kSoftLight_GrBlendEquation);
100static_assert(11 == kDifference_GrBlendEquation);
101static_assert(12 == kExclusion_GrBlendEquation);
102static_assert(13 == kMultiply_GrBlendEquation);
103static_assert(14 == kHSLHue_GrBlendEquation);
104static_assert(15 == kHSLSaturation_GrBlendEquation);
105static_assert(16 == kHSLColor_GrBlendEquation);
106static_assert(17 == kHSLLuminosity_GrBlendEquation);
107static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
Brian Salomond978b902019-02-07 15:09:18 -0500133//////////////////////////////////////////////////////////////////////////////
134
135static int gl_target_to_binding_index(GrGLenum target) {
136 switch (target) {
137 case GR_GL_TEXTURE_2D:
138 return 0;
139 case GR_GL_TEXTURE_RECTANGLE:
140 return 1;
141 case GR_GL_TEXTURE_EXTERNAL:
142 return 2;
143 }
144 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500145}
146
147GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500148 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
149}
150
151bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
152 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500153}
154
155void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500156 int targetIndex = gl_target_to_binding_index(target);
157 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
158 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500159}
160
Brian Salomon1f05d452019-02-08 12:33:08 -0500161void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
162 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500163}
164
Brian Salomon1f05d452019-02-08 12:33:08 -0500165void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
166 for (auto& targetBinding : fTargetBindings) {
167 targetBinding.fBoundResourceID.makeInvalid();
168 if (markUnmodified) {
169 targetBinding.fHasBeenModified = false;
170 }
Brian Salomond978b902019-02-07 15:09:18 -0500171 }
172}
173
174//////////////////////////////////////////////////////////////////////////////
175
Brian Salomondc829942018-10-23 16:07:24 -0400176static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
177 switch (filter) {
178 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
179 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
180 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
181 }
182 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400183}
184
185static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
186 switch (filter) {
187 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
188 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
189 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
190 }
191 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400192}
193
Michael Ludwigf23a1522018-12-10 11:36:13 -0500194static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
195 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400196 switch (wrapMode) {
197 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
198 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
199 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500200 case GrSamplerState::WrapMode::kClampToBorder:
201 // May not be supported but should have been caught earlier
202 SkASSERT(caps.clampToBorderSupport());
203 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500204 }
Brian Salomondc829942018-10-23 16:07:24 -0400205 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400206}
207
208///////////////////////////////////////////////////////////////////////////////
209
210class GrGLGpu::SamplerObjectCache {
211public:
212 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
213 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
214 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
215 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
216 std::fill_n(fSamplers, kNumSamplers, 0);
217 }
218
219 ~SamplerObjectCache() {
220 if (!fNumTextureUnits) {
221 // We've already been abandoned.
222 return;
223 }
Chris Dalton703fe682019-05-15 12:58:12 -0600224 for (GrGLuint sampler : fSamplers) {
225 // The spec states that "zero" values should be silently ignored, however they still
226 // trigger GL errors on some NVIDIA platforms.
227 if (sampler) {
228 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
229 }
230 }
Brian Salomondc829942018-10-23 16:07:24 -0400231 }
232
Brian Salomonccb61422020-01-09 10:46:36 -0500233 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400234 int index = StateToIndex(state);
235 if (!fSamplers[index]) {
236 GrGLuint s;
237 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
238 if (!s) {
239 return;
240 }
241 fSamplers[index] = s;
242 auto minFilter = filter_to_gl_min_filter(state.filter());
243 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500244 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
245 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400246 GR_GL_CALL(fGpu->glInterface(),
247 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
248 GR_GL_CALL(fGpu->glInterface(),
249 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
250 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
251 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
252 }
253 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
254 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
255 fHWBoundSamplers[unitIdx] = fSamplers[index];
256 }
257 }
258
259 void invalidateBindings() {
260 // When we have sampler support we always use samplers. So setting these to zero will cause
261 // a rebind on next usage.
262 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
263 }
264
265 void abandon() {
266 fHWBoundSamplers.reset();
267 fNumTextureUnits = 0;
268 }
269
270 void release() {
271 if (!fNumTextureUnits) {
272 // We've already been abandoned.
273 return;
274 }
275 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
276 std::fill_n(fSamplers, kNumSamplers, 0);
277 // Deleting a bound sampler implicitly binds sampler 0.
278 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
279 }
280
281private:
Brian Salomonccb61422020-01-09 10:46:36 -0500282 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400283 int filter = static_cast<int>(state.filter());
284 SkASSERT(filter >= 0 && filter < 3);
285 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500286 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400287 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500288 SkASSERT(wrapY >= 0 && wrapY < 4);
289 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400290 SkASSERT(idx < kNumSamplers);
291 return idx;
292 }
293
294 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500295 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400296 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
297 GrGLuint fSamplers[kNumSamplers];
298 int fNumTextureUnits;
299};
300
reed@google.comac10a2d2010-12-22 21:39:39 +0000301///////////////////////////////////////////////////////////////////////////////
302
Brian Salomon384fab42017-12-07 12:33:05 -0500303sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
304 GrContext* context) {
305 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500306 interface = GrGLMakeNativeInterface();
307 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
308 // to GrGLMakeNativeInterface.
309 if (!interface) {
310 interface = sk_ref_sp(GrGLCreateNativeInterface());
311 }
Brian Salomon384fab42017-12-07 12:33:05 -0500312 if (!interface) {
313 return nullptr;
314 }
bsalomon424cc262015-05-22 10:37:30 -0700315 }
Jim Van Verth76334772017-05-05 16:46:05 -0400316#ifdef USE_NSIGHT
317 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
318#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500319 auto glContext = GrGLContext::Make(std::move(interface), options);
320 if (!glContext) {
321 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700322 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500323 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700324}
325
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500326GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
327 : GrGpu(context)
328 , fGLContext(std::move(ctx))
329 , fProgramCache(new ProgramCache(this))
330 , fHWProgramID(0)
331 , fTempSrcFBOID(0)
332 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500333 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500334 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400335 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500336 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000337
Brian Salomond978b902019-02-07 15:09:18 -0500338 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000339
Brian Salomonae64c192019-02-05 09:41:37 -0500340 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
341 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600342 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500343 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500344 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
345 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
346 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
347 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700348 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500349 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
350 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700351 }
Brian Salomonae64c192019-02-05 09:41:37 -0500352 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400353 fHWBufferState[i].invalidate();
354 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600355 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700356
357 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
358 fPathRendering.reset(new GrGLPathRendering(this));
359 }
360
Brian Salomondc829942018-10-23 16:07:24 -0400361 if (this->glCaps().samplerObjectSupport()) {
362 fSamplerObjectCache.reset(new SamplerObjectCache(this));
363 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000364}
365
bsalomon861e1032014-12-16 07:33:49 -0800366GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700367 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
368 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800369 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700370 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700371 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800372
Chris Dalton20e7a532020-02-28 15:37:53 +0000373 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400374 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000375 // detach the current program so there is no confusion on OpenGL's part
376 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000377 GL_CALL(UseProgram(0));
378 }
379
Brian Salomon43f8bf02017-10-18 08:33:29 -0400380 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700381 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800382 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400383 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700384 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800385 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400386 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700387 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800388 }
egdaniel0f5f9672015-02-03 11:10:51 -0800389
bsalomon7ea33f52015-11-22 14:51:00 -0800390 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
391 if (0 != fCopyPrograms[i].fProgram) {
392 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
393 }
bsalomon6df86402015-06-01 10:41:49 -0700394 }
bsalomon6dea83f2015-12-03 12:58:06 -0800395
brianosman33f6b3f2016-06-02 05:49:21 -0700396 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
397 if (0 != fMipmapPrograms[i].fProgram) {
398 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
399 }
400 }
401
Brian Salomondc829942018-10-23 16:07:24 -0400402 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500403
404 while (!fFinishCallbacks.empty()) {
405 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
406 this->deleteSync(fFinishCallbacks.front().fSync);
407 fFinishCallbacks.pop_front();
408 }
bsalomonc8dc1f72014-08-21 13:02:13 -0700409}
410
bsalomon6e2aad42016-04-01 11:54:31 -0700411void GrGLGpu::disconnect(DisconnectType type) {
412 INHERITED::disconnect(type);
413 if (DisconnectType::kCleanup == type) {
414 if (fHWProgramID) {
415 GL_CALL(UseProgram(0));
416 }
417 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700418 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700419 }
420 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700421 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700422 }
423 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700424 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700425 }
bsalomon6e2aad42016-04-01 11:54:31 -0700426 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
427 if (fCopyPrograms[i].fProgram) {
428 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
429 }
430 }
brianosman33f6b3f2016-06-02 05:49:21 -0700431 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
432 if (fMipmapPrograms[i].fProgram) {
433 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
434 }
435 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436
Brian Salomondc829942018-10-23 16:07:24 -0400437 if (fSamplerObjectCache) {
438 fSamplerObjectCache->release();
439 }
bsalomon6e2aad42016-04-01 11:54:31 -0700440 } else {
441 if (fProgramCache) {
442 fProgramCache->abandon();
443 }
Brian Salomondc829942018-10-23 16:07:24 -0400444 if (fSamplerObjectCache) {
445 fSamplerObjectCache->abandon();
446 }
bsalomon6e2aad42016-04-01 11:54:31 -0700447 }
448
Chris Dalton20e7a532020-02-28 15:37:53 +0000449 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500450 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700451
bsalomonc8dc1f72014-08-21 13:02:13 -0700452 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700453 fTempSrcFBOID = 0;
454 fTempDstFBOID = 0;
455 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700456 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800457 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
458 fCopyPrograms[i].fProgram = 0;
459 }
brianosman33f6b3f2016-06-02 05:49:21 -0700460 fMipmapProgramArrayBuffer.reset();
461 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
462 fMipmapPrograms[i].fProgram = 0;
463 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400464
jvanverthe9c0fc62015-04-29 11:18:05 -0700465 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700466 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700467 }
Greg Daniel822f7b22020-02-21 14:41:36 -0500468
469 while (!fFinishCallbacks.empty()) {
470 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
471 if (DisconnectType::kCleanup == type) {
472 this->deleteSync(fFinishCallbacks.front().fSync);
473 }
474 fFinishCallbacks.pop_front();
475 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000476}
477
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000478///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000479
bsalomon861e1032014-12-16 07:33:49 -0800480void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000481 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400482 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000483 GL_CALL(Disable(GR_GL_DEPTH_TEST));
484 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000485
Brian Salomonf0861672017-05-08 11:10:10 -0400486 // We don't use face culling.
487 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600488 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
489 // just set this to the default for self-consistency.
490 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400491
Brian Salomonae64c192019-02-05 09:41:37 -0500492 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
493 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700494
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400495 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500496#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000497 // Desktop-only state that we never change
498 if (!this->glCaps().isCoreProfile()) {
499 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
500 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
501 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
502 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
503 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
504 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
505 }
506 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
507 // core profile. This seems like a bug since the core spec removes any mention of
508 // GL_ARB_imaging.
509 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
510 GL_CALL(Disable(GR_GL_COLOR_TABLE));
511 }
512 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400513
Chris Dalton1215cda2019-12-17 21:44:04 -0700514 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500515#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000516 // Since ES doesn't support glPointSize at all we always use the VS to
517 // set the point size
518 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
519
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000520 }
joshualitt58162332014-08-01 06:44:53 -0700521
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400522 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500523 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700524 // The arm extension requires specifically enabling MSAA fetching per sample.
525 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500526 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700527 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000528 fHWWriteToColor = kUnknown_TriState;
529 // we only ever use lines in hairline mode
530 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700531 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500532
533 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000534 }
edisonn@google.comba669992013-06-28 16:03:21 +0000535
egdanielb414f252014-07-29 13:15:47 -0700536 if (resetBits & kMSAAEnable_GrGLBackendState) {
537 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700538
Chris Dalton6ce447a2019-06-23 18:07:38 -0600539 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800540 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
541 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700542 GL_CALL(CoverageModulation(GR_GL_RGBA));
543 }
Chris Daltonce425af2019-12-16 10:39:03 -0700544
545 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000546 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000548 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400549 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000550
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000551 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500552 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500553 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 }
Brian Salomondc829942018-10-23 16:07:24 -0400555 if (fSamplerObjectCache) {
556 fSamplerObjectCache->invalidateBindings();
557 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000558 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000559
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000560 if (resetBits & kBlend_GrGLBackendState) {
561 fHWBlendState.invalidate();
562 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000563
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000564 if (resetBits & kView_GrGLBackendState) {
565 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700566 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000567 fHWViewport.invalidate();
568 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000569
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 if (resetBits & kStencil_GrGLBackendState) {
571 fHWStencilSettings.invalidate();
572 fHWStencilTestEnabled = kUnknown_TriState;
573 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000574
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000575 // Vertex
576 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700577 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500578 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
579 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600580 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700581 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000584 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500585 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700586 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500587 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000588 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000589
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000590 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700591 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700592 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000593 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000594 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000595
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000596 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400598 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
600 }
Brian Salomon1047a492019-07-02 12:25:21 -0400601 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
603 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (this->glCaps().packFlipYSupport()) {
605 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
606 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000607 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000608
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 if (resetBits & kProgram_GrGLBackendState) {
610 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000611 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400613 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000614}
615
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400616static bool check_backend_texture(const GrBackendTexture& backendTex,
617 const GrGLCaps& caps,
618 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400619 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400620 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400621 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400622 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800623 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000624
Brian Salomonea4ad302019-08-07 13:04:55 -0400625 desc->fSize = {backendTex.width(), backendTex.height()};
626 desc->fTarget = info.fTarget;
627 desc->fID = info.fID;
628 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800629
Brian Salomonea4ad302019-08-07 13:04:55 -0400630 if (desc->fFormat == GrGLFormat::kUnknown) {
631 return false;
632 }
633 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400634 if (!caps.shaderCaps()->externalTextureSupport()) {
635 return false;
636 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400637 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
638 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400639 return false;
640 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400641 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400642 return false;
643 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400644 if (backendTex.isProtected()) {
645 // Not supported in GL backend at this time.
646 return false;
647 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400648
Brian Salomond17f6582017-07-19 18:28:58 -0400649 return true;
650}
651
652sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400653 GrWrapOwnership ownership,
654 GrWrapCacheable cacheable,
655 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400656 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400657 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return nullptr;
659 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400660
Brian Salomond17f6582017-07-19 18:28:58 -0400661 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400662 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400663 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400664 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400665 }
bsalomone5286e02016-01-14 09:24:09 -0800666
Greg Daniel177e6952017-10-12 12:27:11 -0400667 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
668 : GrMipMapsStatus::kNotAllocated;
669
Brian Salomonea4ad302019-08-07 13:04:55 -0400670 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400671 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400672 // We don't know what parameters are already set on wrapped textures.
673 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600674 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400675}
676
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500677static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
678 const GrGLCaps& caps, GrGLTexture::Desc* desc,
679 bool skipRectTexSupportCheck = false) {
680 GrGLTextureInfo info;
681 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
682 return false;
683 }
684
685 desc->fSize = {backendTex.width(), backendTex.height()};
686 desc->fTarget = info.fTarget;
687 desc->fID = info.fID;
688 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
689
690 if (desc->fFormat == GrGLFormat::kUnknown) {
691 return false;
692 }
693
694 if (GR_GL_TEXTURE_2D != desc->fTarget) {
695 return false;
696 }
697 if (backendTex.isProtected()) {
698 // Not supported in GL backend at this time.
699 return false;
700 }
701
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500702 return true;
703}
704
Robert Phillipsb915c942019-12-17 14:44:37 -0500705sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
706 GrWrapOwnership ownership,
707 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500708 GrGLTexture::Desc desc;
709 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
710 return nullptr;
711 }
712
713 if (kBorrow_GrWrapOwnership == ownership) {
714 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
715 } else {
716 desc.fOwnership = GrBackendObjectOwnership::kOwned;
717 }
718
719 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
720 : GrMipMapsStatus::kNotAllocated;
721
722 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
723 backendTex.getGLTextureParams(), cacheable,
724 kRead_GrIOType);
725 // We don't know what parameters are already set on wrapped textures.
726 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600727 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500728}
729
Brian Salomond17f6582017-07-19 18:28:58 -0400730sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400731 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500732 GrWrapOwnership ownership,
733 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400734 const GrGLCaps& caps = this->glCaps();
735
Brian Salomonea4ad302019-08-07 13:04:55 -0400736 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400737 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800738 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800739 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400740 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
741 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800742
Brian Salomond17f6582017-07-19 18:28:58 -0400743 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400744 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800745 return nullptr;
746 }
bsalomon10528f12015-10-14 12:54:52 -0700747
Brian Osman766fcbb2017-03-13 09:33:09 -0400748 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400749 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400750 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400751 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800752 }
bsalomonb15b4c12014-10-29 12:41:57 -0700753
Robert Phillips0902c982019-07-16 07:47:56 -0400754
Greg Daniel6fa62e22019-08-07 15:52:37 -0400755 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
756 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000757
Brian Salomonea4ad302019-08-07 13:04:55 -0400758 GrGLRenderTarget::IDs rtIDs;
759 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400760 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000761 }
Greg Daniel177e6952017-10-12 12:27:11 -0400762
763 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
764 : GrMipMapsStatus::kNotAllocated;
765
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500766 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400767 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400768 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400769 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400770 // We don't know what parameters are already set on wrapped textures.
771 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600772 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000773}
774
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400775sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400776 GrGLFramebufferInfo info;
777 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000778 return nullptr;
779 }
780
Brian Salomone8a766b2019-07-19 14:24:36 -0400781 if (backendRT.isProtected()) {
782 // Not supported in GL at this time.
783 return nullptr;
784 }
785
Brian Salomond4764a12019-08-08 12:08:24 -0400786 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400787 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
788 return nullptr;
789 }
790
Brian Salomonea4ad302019-08-07 13:04:55 -0400791 GrGLRenderTarget::IDs rtIDs;
792 rtIDs.fRTFBOID = info.fFBOID;
793 rtIDs.fMSColorRenderbufferID = 0;
794 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
795 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000796
Greg Daniel6fa62e22019-08-07 15:52:37 -0400797 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700798
Brian Salomona56a7462020-02-07 14:17:25 -0500799 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400800 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000801}
802
Greg Daniel7ef28f32017-04-20 16:41:55 +0000803sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400804 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400805 GrGLTexture::Desc desc;
806 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
807 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400808 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800809 return nullptr;
810 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400811
812 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
813 return nullptr;
814 }
815
816 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400817 GrGLRenderTarget::IDs rtIDs;
818 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800819 return nullptr;
820 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500821 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800822}
823
Brian Salomonc320b152018-02-20 14:05:36 -0500824static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700825 if (!glTex) {
826 return false;
827 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000828
bsalomone5286e02016-01-14 09:24:09 -0800829 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
830 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800831 return false;
832 }
833
jvanverth17aa0472016-01-05 10:41:27 -0800834 return true;
835}
836
Brian Salomona9b04b92018-06-01 15:04:28 -0400837bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400838 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400839 const GrMipLevel texels[], int mipLevelCount,
840 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500841 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800842
Brian Salomonc320b152018-02-20 14:05:36 -0500843 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800844 return false;
845 }
846
Brian Salomond978b902019-02-07 15:09:18 -0500847 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000848
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400849 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400850 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400851 glTex->target(), left, top, width, height, srcColorType, texels,
852 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000853}
854
Brian Salomone05ba5a2019-04-08 11:59:07 -0400855bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400856 GrColorType textureColorType, GrColorType bufferColorType,
857 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859
Jim Van Verth1676cb92019-01-15 13:24:45 -0500860 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400861 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500862
Brian Salomonc320b152018-02-20 14:05:36 -0500863 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400864 return false;
865 }
866
Hal Canaryc36f7e72018-06-18 15:50:09 -0400867 static_assert(sizeof(int) == sizeof(int32_t), "");
868 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400869 return false;
870 }
871
Brian Salomond978b902019-02-07 15:09:18 -0500872 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400873
874 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500875 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400876 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500877 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400878
Greg Daniel660cc992017-06-26 14:55:05 -0400879 SkDEBUGCODE(
880 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
881 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
882 SkASSERT(bounds.contains(subRect));
883 )
884
Brian Salomonb28cb682019-07-26 12:48:47 -0400885 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400886 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400887 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700888 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400889 return false;
890 }
891
892 bool restoreGLRowLength = false;
893 if (trimRowBytes != rowBytes) {
894 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400895 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
897 restoreGLRowLength = true;
898 }
899
Greg Danielba88ab62019-07-26 09:14:01 -0400900 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400902 GrGLenum externalFormat = 0;
903 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400904 this->glCaps().getTexSubImageExternalFormatAndType(
905 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400906 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 return false;
908 }
909
Brian Salomon8cbf6622019-07-30 11:03:14 -0400910 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911 GL_CALL(TexSubImage2D(glTex->target(),
912 0,
913 left, top,
914 width,
915 height,
916 externalFormat, externalType,
917 pixels));
918
919 if (restoreGLRowLength) {
920 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
921 }
922
923 return true;
924}
925
Brian Salomon26de56e2019-04-10 12:14:26 -0400926bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400927 GrColorType surfaceColorType, GrColorType dstColorType,
928 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400929 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
930 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400931 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400932 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
933 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400934}
935
Brian Osman9b560242017-09-05 15:34:52 -0400936void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500937 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
938 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
939 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
940 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400941 }
Brian Osman9b560242017-09-05 15:34:52 -0400942}
943
Brian Salomond2a8ae22019-09-10 16:03:59 -0400944bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
945 int texHeight, GrGLenum target, int left, int top, int width,
946 int height, GrColorType srcColorType, const GrMipLevel texels[],
947 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500948 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400949 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500950
Greg Daniel7bfc9132019-08-14 14:23:53 -0400951 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400952 SkDEBUGCODE(
953 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
954 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
955 SkASSERT(bounds.contains(subRect));
956 )
Robert Phillips590533f2017-07-11 14:22:35 -0400957 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400958 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800959
Brian Osman9b560242017-09-05 15:34:52 -0400960 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400961
cblume55f2d2d2016-02-26 13:20:48 -0800962 const GrGLInterface* interface = this->glInterface();
963 const GrGLCaps& caps = this->glCaps();
964
Brian Salomonf77c1462019-08-01 15:19:29 -0400965 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800966
967 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000968 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000969 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000970
bsalomon5b30c6f2015-12-17 14:17:34 -0800971 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800972 GrGLenum externalFormat;
973 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400974 this->glCaps().getTexSubImageExternalFormatAndType(
975 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400976 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800977 return false;
978 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400979
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000980 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800981 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000982 * because our srcData has extra bytes past each row. If so, we need
983 * to trim those off here, since GL ES may not let us specify
984 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000985 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000986 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -0800987
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400988 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600989 *mipMapsStatus = (mipLevelCount > 1) ?
990 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -0800991 }
bsalomone699d0c2016-03-09 06:25:15 -0800992
Brian Salomond2a8ae22019-09-10 16:03:59 -0400993 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -0800994
Brian Salomond2a8ae22019-09-10 16:03:59 -0400995 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
996 if (!texels[currentMipLevel].fPixels) {
997 if (mipMapsStatus) {
998 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400999 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001000 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001001 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001002 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001003 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1004 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001005 const size_t trimRowBytes = currentWidth * bpp;
1006 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1007
1008 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1009 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1010 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1011 restoreGLRowLength = true;
1012 }
1013
1014 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1015 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001016 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001017 if (restoreGLRowLength) {
1018 SkASSERT(caps.writePixelsRowBytesSupport());
1019 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1020 }
1021 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001022}
1023
Greg Daniel7bfc9132019-08-14 14:23:53 -04001024bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001025 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001026 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001027 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001028 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001029 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001030 const GrGLCaps& caps = this->glCaps();
1031
1032 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001034 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001035 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001036 }
1037
Robert Phillips9f744f72019-12-19 19:14:33 -05001038 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1039 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1040
Greg Daniel7bfc9132019-08-14 14:23:53 -04001041 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001042
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001043 int numMipLevels = 1;
1044 if (mipMapped == GrMipMapped::kYes) {
1045 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1046 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001047
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001048 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001049 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001050
1051 if (useTexStorage) {
1052 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001053 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1054 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001055 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001056 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001057 }
Robert Phillips42716d42019-12-16 12:19:54 -05001058
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001059 size_t offset = 0;
1060 for (int level = 0; level < numMipLevels; ++level) {
1061
Robert Phillips99dead92020-01-27 16:11:57 -05001062 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1063 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001064
Brian Salomond8575452020-02-25 12:13:29 -05001065 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1066 level,
1067 0, // left
1068 0, // top
1069 dimensions.width(),
1070 dimensions.height(),
1071 internalFormat,
1072 SkToInt(levelDataSize),
1073 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001074
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001075 if (error != GR_GL_NO_ERROR) {
1076 return false;
1077 }
1078
Robert Phillips9f744f72019-12-19 19:14:33 -05001079 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001080 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001081 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001082 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001083 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001084
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001085 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001086 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1087 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001088
1089 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001090 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1091 level,
1092 internalFormat,
1093 dimensions.width(),
1094 dimensions.height(),
1095 0, // border
1096 SkToInt(levelDataSize),
1097 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001098
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 if (error != GR_GL_NO_ERROR) {
1100 return false;
1101 }
1102
Robert Phillips9f744f72019-12-19 19:14:33 -05001103 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001104 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001105 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001106 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001107 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001108}
1109
Brian Salomond8575452020-02-25 12:13:29 -05001110bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001111 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001112 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001113 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001114 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001115 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001116 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1117 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001118 break;
1119 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001120 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1121 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001122 break;
1123 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1124 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001125 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1126 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001127 break;
1128 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001129 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001130 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001131 }
Brian Salomond8575452020-02-25 12:13:29 -05001132 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001133}
1134
Brian Salomonea4ad302019-08-07 13:04:55 -04001135bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001136 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001137 GrGLRenderTarget::IDs* rtIDs) {
1138 rtIDs->fMSColorRenderbufferID = 0;
1139 rtIDs->fRTFBOID = 0;
1140 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1141 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001142
bsalomona11e5fc2015-12-18 07:59:41 -08001143 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001144
Brian Salomonea4ad302019-08-07 13:04:55 -04001145 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001146 goto FAILED;
1147 }
1148
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001149 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001150 goto FAILED;
1151 }
1152
Brian Salomonea4ad302019-08-07 13:04:55 -04001153 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1154 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001155 goto FAILED;
1156 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001157
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001158 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1159 // the texture bound to the other. The exception is the IMG multisample extension. With this
1160 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1161 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001162 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001163 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1164 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1165 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 goto FAILED;
1167 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001168 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001169 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001170 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 }
1172
egdanield803f272015-03-18 13:01:52 -07001173 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001174 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001175 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001176 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001177 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001178 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1179 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001180 goto FAILED;
1181 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001182 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001183 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001184 GR_GL_COLOR_ATTACHMENT0,
1185 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001186 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001188 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001190 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001191 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1192 GR_GL_COLOR_ATTACHMENT0,
1193 desc.fTarget,
1194 desc.fID,
1195 0,
1196 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001197 } else {
egdanield803f272015-03-18 13:01:52 -07001198 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001199 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 desc.fTarget,
1201 desc.fID,
1202 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001203 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204
1205 return true;
1206
1207FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 if (rtIDs->fMSColorRenderbufferID) {
1209 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001211 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1212 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001214 if (rtIDs->fTexFBOID) {
1215 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001216 }
1217 return false;
1218}
1219
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001220// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001221static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001222// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001223 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001224}
1225
Brian Salomone2826ab2019-06-04 15:58:31 -04001226static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001227 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001228 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1229 // drivers have a bug where an FBO won't be complete if it includes a
1230 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001231 GrGLTextureParameters::SamplerOverriddenState state;
1232 state.fMinFilter = GR_GL_NEAREST;
1233 state.fMagFilter = GR_GL_NEAREST;
1234 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1235 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001236 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1237 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1238 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1239 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001240 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001241}
1242
Brian Salomona56a7462020-02-07 14:17:25 -05001243sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001244 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001245 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001246 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001247 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001248 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001249 int mipLevelCount,
1250 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001251 // We don't support protected textures in GL.
1252 if (isProtected == GrProtected::kYes) {
1253 return nullptr;
1254 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001255 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001256
Brian Salomond2a8ae22019-09-10 16:03:59 -04001257 SkASSERT(mipLevelCount > 0);
1258 GrMipMapsStatus mipMapsStatus =
1259 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001260 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001261 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001262 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001263 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001264 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001265 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001266 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1267 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001268
Brian Salomona56a7462020-02-07 14:17:25 -05001269 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1270 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001271
1272 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001273 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001274 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001275
Robert Phillips67d52cf2017-06-05 13:38:13 -04001276 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001277 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001278 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001279 GL_CALL(BindTexture(texDesc.fTarget, 0));
1280 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001281
Brian Salomonea4ad302019-08-07 13:04:55 -04001282 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1283 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 return return_null_texture();
1285 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001286 tex = sk_make_sp<GrGLTextureRenderTarget>(
1287 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001288 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001289 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001290 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001292 // The non-sampler params are still at their default values.
1293 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1294 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001295 if (levelClearMask) {
1296 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001297 GrColorType colorType;
1298 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1299 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001300 if (this->glCaps().clearTextureSupport()) {
1301 for (int i = 0; i < mipLevelCount; ++i) {
1302 if (levelClearMask & (1U << i)) {
1303 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1304 nullptr));
1305 }
1306 }
1307 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1308 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001309 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001310 this->disableWindowRectangles();
1311 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001312 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001313 for (int i = 0; i < mipLevelCount; ++i) {
1314 if (levelClearMask & (1U << i)) {
1315 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1316 kDst_TempFBOTarget);
1317 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1318 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1319 }
1320 }
Brian Salomonc2249852019-09-16 11:08:50 -04001321 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001322 } else {
1323 std::unique_ptr<char[]> zeros;
1324 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1325 for (int i = 0; i < mipLevelCount; ++i) {
1326 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001327 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1328 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001329 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1330 // smaller levels that need clearing.
1331 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001332 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001333 size_t size = levelWidth * levelHeight * bpp;
1334 zeros.reset(new char[size]());
1335 }
1336 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1337 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1338 externalFormat, externalType, zeros.get()));
1339 }
Brian Salomona3e29962019-07-16 11:52:08 -04001340 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001341 }
1342 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001343 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001344}
1345
Robert Phillips9f744f72019-12-19 19:14:33 -05001346sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001347 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001348 SkBudgeted budgeted,
1349 GrMipMapped mipMapped,
1350 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001351 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001352 // We don't support protected textures in GL.
1353 if (isProtected == GrProtected::kYes) {
1354 return nullptr;
1355 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001356 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001357 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001358 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001359 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001360 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001361 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001362 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001363 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001364 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001365 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001366 return nullptr;
1367 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001368
Robert Phillipsee946932019-12-18 11:16:17 -05001369 // Unbind this texture from the scratch texture unit.
1370 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1371
Robert Phillipse4720c62020-01-14 14:33:24 -05001372 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1373 ? GrMipMapsStatus::kValid
1374 : GrMipMapsStatus::kNotAllocated;
1375
1376 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001377 // The non-sampler params are still at their default values.
1378 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1379 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001380 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001381}
1382
Robert Phillipsb915c942019-12-17 14:44:37 -05001383GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1384 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001385 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001386 GrProtected isProtected,
1387 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001388 // We don't support protected textures in GL.
1389 if (isProtected == GrProtected::kYes) {
1390 return {};
1391 }
1392
1393 this->handleDirtyContext();
1394
1395 GrGLFormat glFormat = format.asGLFormat();
1396 if (glFormat == GrGLFormat::kUnknown) {
1397 return {};
1398 }
1399
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001400 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001401 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001402 SkAutoMalloc am;
1403
1404 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1405 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1406 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001407 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001408 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001409 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1410 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001411
Robert Phillips99dead92020-01-27 16:11:57 -05001412 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1413 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001414
1415 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001416
1417 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1418
1419 rawData = (const char*) am.get();
1420 }
1421
1422 GrGLTextureInfo info;
1423 GrGLTextureParameters::SamplerOverriddenState initialState;
1424
1425 info.fTarget = GR_GL_TEXTURE_2D;
1426 info.fFormat = GrGLFormatToEnum(glFormat);
1427 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001428 mipMapped, &initialState,
1429 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001430 if (!info.fID) {
1431 return {};
1432 }
1433
1434 // Unbind this texture from the scratch texture unit.
1435 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1436
1437 auto parameters = sk_make_sp<GrGLTextureParameters>();
1438 // The non-sampler params are still at their default values.
1439 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1440 fResetTimestampForTextureParameters);
1441
1442 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1443 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001444}
1445
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001446namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001447
egdaniel8dc7c3a2015-04-16 11:22:42 -07001448const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001449
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001450void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001451 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001452
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001453 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001454 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001455 (kUnknownBitCount == format->fTotalBits));
1456 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001457 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001458 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1459 (GrGLint*)&format->fStencilBits);
1460 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001461 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001462 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1463 (GrGLint*)&format->fTotalBits);
1464 format->fTotalBits += format->fStencilBits;
1465 } else {
1466 format->fTotalBits = format->fStencilBits;
1467 }
1468 }
1469}
1470}
1471
Brian Salomon5043f1f2019-07-11 21:27:54 -04001472int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001473 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001474 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1475
1476 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001477 // Default to unsupported, set this if we find a stencil format that works.
1478 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001479
Brian Salomond2a8ae22019-09-10 16:03:59 -04001480 GrGLuint colorID =
1481 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1482 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001483 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001484 }
egdanielff1d5472015-09-10 08:37:20 -07001485 // unbind the texture from the texture unit before binding it to the frame buffer
1486 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1487
1488 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001489 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001490 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001491 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001492 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001493 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1494 GR_GL_COLOR_ATTACHMENT0,
1495 GR_GL_TEXTURE_2D,
1496 colorID,
1497 0));
bsalomon30447372015-12-21 09:03:05 -08001498 GrGLuint sbRBID = 0;
1499 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001500
1501 // look over formats till I find a compatible one
1502 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001503 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001504 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001505 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1506 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001507 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1508 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1509 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001510 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001511 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001512 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001513 if (sFmt.fPacked) {
1514 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1515 GR_GL_DEPTH_ATTACHMENT,
1516 GR_GL_RENDERBUFFER, sbRBID));
1517 } else {
1518 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1519 GR_GL_DEPTH_ATTACHMENT,
1520 GR_GL_RENDERBUFFER, 0));
1521 }
1522 GrGLenum status;
1523 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1524 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1525 firstWorkingStencilFormatIndex = i;
1526 break;
1527 }
egdanielff1d5472015-09-10 08:37:20 -07001528 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1529 GR_GL_STENCIL_ATTACHMENT,
1530 GR_GL_RENDERBUFFER, 0));
1531 if (sFmt.fPacked) {
1532 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1533 GR_GL_DEPTH_ATTACHMENT,
1534 GR_GL_RENDERBUFFER, 0));
1535 }
egdanielff1d5472015-09-10 08:37:20 -07001536 }
1537 }
bsalomon30447372015-12-21 09:03:05 -08001538 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001539 }
1540 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001541 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1542 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001543 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001544 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001545 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001546}
1547
Brian Salomonea4ad302019-08-07 13:04:55 -04001548GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001549 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001550 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001551 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001552 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001553 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001554 if (format == GrGLFormat::kUnknown) {
1555 return 0;
1556 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001557 GrGLuint id = 0;
1558 GL_CALL(GenTextures(1, &id));
1559 if (!id) {
1560 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001561 }
1562
Brian Salomonea4ad302019-08-07 13:04:55 -04001563 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001564
Brian Salomonea4ad302019-08-07 13:04:55 -04001565 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1566
Robert Phillips42716d42019-12-16 12:19:54 -05001567 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001568 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1569 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001570 GL_CALL(DeleteTextures(1, &id));
1571 return 0;
1572 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001573 }
Robert Phillips42716d42019-12-16 12:19:54 -05001574
Brian Salomonea4ad302019-08-07 13:04:55 -04001575 return id;
1576}
1577
Robert Phillips2716daf2020-01-23 12:53:42 -05001578GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001579 GrGLFormat format,
1580 GrRenderable renderable,
1581 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001582 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001583 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001584 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001585
Brian Salomonea4ad302019-08-07 13:04:55 -04001586 GrGLuint id = 0;
1587 GL_CALL(GenTextures(1, &id));
1588
1589 if (!id) {
1590 return 0;
1591 }
1592
1593 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001594
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001595 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001596 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001597 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001598 }
1599
Brian Salomond2a8ae22019-09-10 16:03:59 -04001600 if (initialState) {
1601 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1602 } else {
1603 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001604 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001605
1606 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1607
1608 bool success = false;
1609 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001610 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001611 auto levelCount = std::max(mipLevelCount, 1);
1612 GrGLenum error =
1613 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1614 dimensions.width(), dimensions.height()));
1615 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001616 } else {
1617 GrGLenum externalFormat, externalType;
1618 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1619 GrGLenum error = GR_GL_NO_ERROR;
1620 if (externalFormat && externalType) {
1621 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1622 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001623 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1624 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001625 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1626 currentWidth, currentHeight, 0, externalFormat,
1627 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001628 }
Brian Salomond8575452020-02-25 12:13:29 -05001629 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001630 }
1631 }
1632 }
1633 if (success) {
1634 return id;
1635 }
1636 GL_CALL(DeleteTextures(1, &id));
1637 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001638}
1639
Chris Daltoneffee202019-07-01 22:28:03 -06001640GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1641 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001642 SkASSERT(width >= rt->width());
1643 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001644
egdaniel8dc7c3a2015-04-16 11:22:42 -07001645 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001646
Brian Salomond4764a12019-08-08 12:08:24 -04001647 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001648 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001649 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001650 }
egdanielff1d5472015-09-10 08:37:20 -07001651
1652 if (!sbDesc.fRenderbufferID) {
1653 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1654 }
1655 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001656 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001657 }
1658 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1659 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001660 // we do this "if" so that we don't call the multisample
1661 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001662 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001663 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1664 width, height)) {
1665 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1666 return nullptr;
1667 }
egdanielff1d5472015-09-10 08:37:20 -07001668 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001669 GrGLenum error = GL_ALLOC_CALL(
1670 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1671 if (error != GR_GL_NO_ERROR) {
1672 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1673 return nullptr;
1674 }
egdanielff1d5472015-09-10 08:37:20 -07001675 }
1676 fStats.incStencilAttachmentCreates();
1677 // After sized formats we attempt an unsized format and take
1678 // whatever sizes GL gives us. In that case we query for the size.
1679 GrGLStencilAttachment::Format format = sFmt;
1680 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001681 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1682 sbDesc,
1683 width,
1684 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001685 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001686 format);
1687 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001688}
1689
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001690////////////////////////////////////////////////////////////////////////////////
1691
Brian Salomondbf70722019-02-07 11:31:24 -05001692sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1693 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001694 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001695}
1696
Chris Dalton2e7ed262020-02-21 15:17:59 -07001697void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1698 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001699 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1700 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1701 fHWScissorSettings.fEnabled = kYes_TriState;
1702 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001703 } else {
1704 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1705 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1706 fHWScissorSettings.fEnabled = kNo_TriState;
1707 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001708 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001709}
Brian Salomond818ebf2018-07-02 14:08:49 +00001710
Chris Dalton2e7ed262020-02-21 15:17:59 -07001711void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1712 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001713 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001714 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1715 if (fHWScissorSettings.fRect != nativeScissor) {
1716 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1717 nativeScissor.fHeight));
1718 fHWScissorSettings.fRect = nativeScissor;
1719 }
joshualitt77b13072014-10-27 14:51:01 -07001720}
1721
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001722void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001723 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001724#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001725 typedef GrWindowRectsState::Mode Mode;
1726 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1727 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001728
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001729 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001730 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001731 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001732 }
1733
csmartdalton7535f412016-08-23 06:51:00 -07001734 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1735 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001736 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001737 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001738
Chris Daltond6cda8d2019-09-05 02:30:04 -06001739 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001740 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001741 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001742 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001743 }
1744
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001745 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001746 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001747
Greg Danielacd66b42019-05-22 16:29:12 -04001748 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001749#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001750}
1751
1752void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001753#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001754 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001755 return;
1756 }
1757 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001758 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001759#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001760}
1761
Chris Daltond42d2002020-02-28 12:05:04 -07001762bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1763 const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001764 this->handleDirtyContext();
1765
Chris Daltond42d2002020-02-28 12:05:04 -07001766 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001767 if (!program) {
1768 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1769 return false;
1770 }
1771
1772 this->flushProgram(std::move(program));
1773
Chris Daltond42d2002020-02-28 12:05:04 -07001774 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1775 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1776 }
1777
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001778 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001779 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001780 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001781
Chris Dalton20e7a532020-02-28 15:37:53 +00001782 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001783
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001784 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001785 GrStencilSettings stencil;
1786 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001787 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1788 stencil.reset(*programInfo.pipeline().getUserStencil(),
1789 programInfo.pipeline().hasStencilClip(),
1790 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001791 }
Robert Phillips901aff02019-10-08 12:32:56 -04001792 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001793 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001794 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1795 glRT, programInfo.origin());
1796 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001797 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001798 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001799
1800 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001801 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001802 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001803
Chris Dalton20e7a532020-02-28 15:37:53 +00001804 return true;
1805}
1806
1807void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1808 if (!program) {
1809 fHWProgram.reset();
1810 fHWProgramID = 0;
1811 return;
1812 }
1813 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1814 if (program == fHWProgram) {
1815 return;
1816 }
1817 auto id = program->programID();
1818 SkASSERT(id);
1819 GL_CALL(UseProgram(id));
1820 fHWProgram = std::move(program);
1821 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001822}
1823
1824void GrGLGpu::flushProgram(GrGLuint id) {
1825 SkASSERT(id);
1826 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001827 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001828 return;
1829 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001830 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001831 GL_CALL(UseProgram(id));
1832 fHWProgramID = id;
1833}
1834
Brian Salomonae64c192019-02-05 09:41:37 -05001835GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001836 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001837
cdaltone2e71c22016-04-07 18:13:29 -07001838 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001839 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001840 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001841 }
cdaltone2e71c22016-04-07 18:13:29 -07001842
Brian Salomonae64c192019-02-05 09:41:37 -05001843 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001844 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001845 if (!bufferState->fBufferZeroKnownBound) {
1846 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1847 bufferState->fBufferZeroKnownBound = true;
1848 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001849 }
Brian Salomondbf70722019-02-07 11:31:24 -05001850 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1851 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001852 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1853 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1854 bufferState->fBufferZeroKnownBound = false;
1855 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001856 }
1857
Brian Salomonae64c192019-02-05 09:41:37 -05001858 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001859}
Brian Salomond818ebf2018-07-02 14:08:49 +00001860
Brian Osman9a9baae2018-11-05 15:06:26 -05001861void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001862 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001863 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001864 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001865 SkASSERT(!this->caps()->performColorClearsAsDraws());
1866 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001867
Brian Salomon43f8bf02017-10-18 08:33:29 -04001868 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001869
Brian Salomon43f8bf02017-10-18 08:33:29 -04001870 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1871
Brian Salomond818ebf2018-07-02 14:08:49 +00001872 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001873 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001874 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001875 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001876 }
Greg Danielacd66b42019-05-22 16:29:12 -04001877 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001878 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001879 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001880 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001881 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001882}
1883
Robert Phillips95214472017-08-08 18:00:03 -04001884void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001885 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1886
Robert Phillips95214472017-08-08 18:00:03 -04001887 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001888 return;
1889 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001890
Chris Dalton674f77a2019-09-30 20:49:39 -06001891 // This should only be called internally when we know we have a stencil buffer.
1892 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001893
bsalomonb0bd4f62014-09-03 07:19:50 -07001894 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001895 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001896
Chris Dalton2e7ed262020-02-21 15:17:59 -07001897 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001898 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001899
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001900 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001901 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001902 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001903 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001904}
1905
Chris Daltonb50cc812019-10-14 16:29:21 -06001906static bool use_tiled_rendering(const GrGLCaps& glCaps,
1907 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1908 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1909 // Otherwise it's faster to just not use it.
1910 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1911 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1912}
1913
1914void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001915 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1916 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1917 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1918
1919 this->handleDirtyContext();
1920
1921 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1922 this->flushRenderTarget(glRT);
1923 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1924
Chris Daltonb50cc812019-10-14 16:29:21 -06001925 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1926 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1927 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1928 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1929 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1930 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1931 nativeBounds.fHeight, preserveMask));
1932 }
1933
Chris Dalton674f77a2019-09-30 20:49:39 -06001934 GrGLbitfield clearMask = 0;
1935 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1936 SkASSERT(!this->caps()->performColorClearsAsDraws());
1937 this->flushClearColor(colorLoadStore.fClearColor);
1938 this->flushColorWrite(true);
1939 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001940 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001941 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1942 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1943 GL_CALL(StencilMask(0xffffffff));
1944 GL_CALL(ClearStencil(0));
1945 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1946 }
1947 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001948 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001949 this->disableWindowRectangles();
1950 GL_CALL(Clear(clearMask));
1951 }
1952}
1953
1954void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1955 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1956 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1957 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1958
1959 this->handleDirtyContext();
1960
1961 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1962 // The framebuffer binding changed in the middle of a command buffer. We should have already
1963 // printed a warning during onFBOChanged.
1964 return;
1965 }
1966
1967 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1968 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1969
1970 SkSTArray<2, GrGLenum> discardAttachments;
1971 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1972 discardAttachments.push_back(
1973 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1974 }
1975 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1976 discardAttachments.push_back(
1977 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1978 }
1979
1980 if (!discardAttachments.empty()) {
1981 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1982 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1983 discardAttachments.begin()));
1984 } else {
1985 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1986 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1987 discardAttachments.begin()));
1988 }
1989 }
1990 }
1991
Chris Daltonb50cc812019-10-14 16:29:21 -06001992 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1993 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1994 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1995 // Handled by use_tiled_rendering().
1996 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1997 GL_CALL(EndTiling(preserveMask));
1998 }
1999
Chris Dalton674f77a2019-09-30 20:49:39 -06002000 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002001}
2002
csmartdalton29df7602016-08-31 11:55:52 -07002003void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2004 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002005 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002006 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002007 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002008 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002009
egdaniel8dc7c3a2015-04-16 11:22:42 -07002010 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002011 if (!sb) {
2012 // We should only get here if we marked a proxy as requiring a SB. However,
2013 // the SB creation could later fail. Likely clipping is going to go awry now.
2014 return;
2015 }
2016
bsalomon6bc1b5f2015-02-23 09:06:38 -08002017 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002018#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002019 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002020 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002021#else
2022 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002023 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002024 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002025 // changing the clip between stencil passes may or may not
2026 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002027 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002028#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002029 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002030 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002031 value = (1 << (stencilBitCount - 1));
2032 } else {
2033 value = 0;
2034 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002035 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002036 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002037
Greg Danielacd66b42019-05-22 16:29:12 -04002038 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002039 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002040
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002041 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002042 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002043 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002044 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002045}
2046
Brian Salomone05ba5a2019-04-08 11:59:07 -04002047bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002048 GrColorType surfaceColorType, GrColorType dstColorType,
2049 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002050 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002051
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002052 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002053 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002054 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002055 return false;
2056 }
Greg Danielba88ab62019-07-26 09:14:01 -04002057 GrGLenum externalFormat = 0;
2058 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002059 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2060 surfaceColorType,
2061 dstColorType,
2062 &externalFormat,
2063 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002064 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002065 return false;
2066 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002067
Brian Salomon71d9d842016-11-03 13:42:00 -04002068 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002069 if (renderTarget->numSamples() <= 1 ||
2070 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2071 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2072 this->flushRenderTargetNoColorWrites(renderTarget);
2073 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2074 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2075 return false;
2076 } else {
2077 SkASSERT(renderTarget->requiresManualMSAAResolve());
2078 // we don't track the state of the READ FBO ID.
2079 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002080 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002081 } else {
2082 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002083 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002084 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002085 }
2086
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002087 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002088 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002089
Brian Salomona6948702018-06-01 15:33:20 -04002090 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002091 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002092 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002093 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002094 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002095 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002096
Brian Osman1348ed02018-09-12 09:08:39 -04002097 bool reattachStencil = false;
2098 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2099 renderTarget &&
2100 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002101 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002102 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2103 reattachStencil = true;
2104 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2105 GR_GL_RENDERBUFFER, 0));
2106 }
2107
Chris Dalton76500e52019-09-05 02:13:05 -06002108 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002109 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002110
2111 if (reattachStencil) {
2112 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2113 renderTarget->renderTargetPriv().getStencilAttachment());
2114 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2115 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2116 }
2117
Brian Salomon26de56e2019-04-10 12:14:26 -04002118 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002119 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002120 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2121 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002122
Brian Salomone05ba5a2019-04-08 11:59:07 -04002123 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002124 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002125 }
2126 return true;
2127}
2128
2129bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002130 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2131 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002132 SkASSERT(surface);
2133
Brian Salomonb28cb682019-07-26 12:48:47 -04002134 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002135
Brian Salomon26de56e2019-04-10 12:14:26 -04002136 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2137 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002138
Brian Salomon1047a492019-07-02 12:25:21 -04002139 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002140 rowPixelWidth = width;
2141 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002142 SkASSERT(!(rowBytes % bytesPerPixel));
2143 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002144 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002145 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2146 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002147}
2148
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002149GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002150 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002151 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002152 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002153 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002154 if (!fCachedOpsRenderPass) {
2155 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002156 }
2157
Chris Daltonb50cc812019-10-14 16:29:21 -06002158 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002159 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002160}
2161
Brian Salomon1fabd512018-02-09 09:54:25 -05002162void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002163 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002164 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002165 this->didWriteToSurface(target, origin, &bounds);
2166}
bsalomon6ba6fa12015-03-04 11:57:37 -08002167
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002168void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2169 this->flushRenderTargetNoColorWrites(target);
2170 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002171}
2172
Brian Osman9aa30c62018-07-02 15:21:46 -04002173void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002174 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002175 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002176 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002177 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002178#ifdef SK_DEBUG
2179 // don't do this check in Chromium -- this is causing
2180 // lots of repeated command buffer flushes when the compositor is
2181 // rendering with Ganesh, which is really slow; even too slow for
2182 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002183 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002184 GrGLenum status;
2185 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2186 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2187 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2188 }
bsalomon160f24c2015-03-17 15:55:42 -07002189 }
egdanield803f272015-03-18 13:01:52 -07002190#endif
2191 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002192 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002193 }
2194
brianosman35b784d2016-05-05 11:52:53 -07002195 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002196 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002197 }
Brian Salomon9b553272020-01-24 14:38:37 -05002198
2199 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2200 GrGLint format;
2201 GrGLint type;
2202 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2203 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2204 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2205 }
bsalomon083617b2016-02-12 12:10:14 -08002206}
bsalomona9909122016-01-23 10:41:40 -08002207
brianosman33f6b3f2016-06-02 05:49:21 -07002208void GrGLGpu::flushFramebufferSRGB(bool enable) {
2209 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2210 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2211 fHWSRGBFramebuffer = kYes_TriState;
2212 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2213 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2214 fHWSRGBFramebuffer = kNo_TriState;
2215 }
2216}
2217
Greg Danielacd66b42019-05-22 16:29:12 -04002218void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002219 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002220 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002221 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002222 fHWViewport = viewport;
2223 }
2224}
2225
Chris Dalton33db9fc2020-02-25 18:52:12 -07002226GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002227 fStats.incNumDraws();
2228
Chris Dalton2e7ed262020-02-21 15:17:59 -07002229 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2230 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2231 GL_CALL(Enable(GR_GL_CULL_FACE));
2232 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002233 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002234 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002235
Chris Dalton3809bab2017-06-13 10:55:06 -06002236 switch (primitiveType) {
2237 case GrPrimitiveType::kTriangles:
2238 return GR_GL_TRIANGLES;
2239 case GrPrimitiveType::kTriangleStrip:
2240 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002241 case GrPrimitiveType::kPoints:
2242 return GR_GL_POINTS;
2243 case GrPrimitiveType::kLines:
2244 return GR_GL_LINES;
2245 case GrPrimitiveType::kLineStrip:
2246 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002247 case GrPrimitiveType::kPatches:
2248 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002249 case GrPrimitiveType::kPath:
2250 SK_ABORT("non-mesh-based GrPrimitiveType");
2251 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002252 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002253 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002254}
2255
Chris Dalton16a33c62019-09-24 22:19:17 -06002256void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002257 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002258 // Some extensions automatically resolves the texture when it is read.
2259 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2260
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002261 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002262 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2263 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2264 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2265 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002266
Chris Daltonc139d082019-09-26 14:04:24 -06002267 // make sure we go through flushRenderTarget() since we've modified
2268 // the bound DRAW FBO ID.
2269 fHWBoundRenderTargetUniqueID.makeInvalid();
2270 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2271 // Apple's extension uses the scissor as the blit bounds.
2272 GrScissorState scissorState;
2273 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002274 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2275 // happens inside flushScissor since resolveRect is already in native device coordinates.
2276 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002277 this->disableWindowRectangles();
2278 GL_CALL(ResolveMultisampleFramebuffer());
2279 } else {
2280 int l, b, r, t;
2281 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2282 this->glCaps().blitFramebufferSupportFlags()) {
2283 l = 0;
2284 b = 0;
2285 r = target->width();
2286 t = target->height();
2287 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002288 l = resolveRect.x();
2289 b = resolveRect.y();
2290 r = resolveRect.x() + resolveRect.width();
2291 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002292 }
Chris Daltonc139d082019-09-26 14:04:24 -06002293
2294 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002295 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002296 this->disableWindowRectangles();
2297 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 +00002298 }
2299}
2300
bsalomon@google.com411dad02012-06-05 20:24:20 +00002301namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002302
bsalomon@google.com411dad02012-06-05 20:24:20 +00002303
2304GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002305 static const GrGLenum gTable[kGrStencilOpCount] = {
2306 GR_GL_KEEP, // kKeep
2307 GR_GL_ZERO, // kZero
2308 GR_GL_REPLACE, // kReplace
2309 GR_GL_INVERT, // kInvert
2310 GR_GL_INCR_WRAP, // kIncWrap
2311 GR_GL_DECR_WRAP, // kDecWrap
2312 GR_GL_INCR, // kIncClamp
2313 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002314 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002315 static_assert(0 == (int)GrStencilOp::kKeep);
2316 static_assert(1 == (int)GrStencilOp::kZero);
2317 static_assert(2 == (int)GrStencilOp::kReplace);
2318 static_assert(3 == (int)GrStencilOp::kInvert);
2319 static_assert(4 == (int)GrStencilOp::kIncWrap);
2320 static_assert(5 == (int)GrStencilOp::kDecWrap);
2321 static_assert(6 == (int)GrStencilOp::kIncClamp);
2322 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002323 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2324 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002325}
2326
2327void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002328 const GrStencilSettings::Face& face,
2329 GrGLenum glFace) {
2330 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2331 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2332 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002333
cdalton93a379b2016-05-11 13:58:08 -07002334 GrGLint ref = face.fRef;
2335 GrGLint mask = face.fTestMask;
2336 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002337
2338 if (GR_GL_FRONT_AND_BACK == glFace) {
2339 // we call the combined func just in case separate stencil is not
2340 // supported.
2341 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2342 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002343 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002344 } else {
2345 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2346 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002347 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002348 }
2349}
2350}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002351
Chris Dalton71713f62019-04-18 12:41:03 -06002352void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002353 if (stencilSettings.isDisabled()) {
2354 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002355 } else if (fHWStencilSettings != stencilSettings ||
2356 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002357 if (kYes_TriState != fHWStencilTestEnabled) {
2358 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002359
csmartdaltonc7d85332016-10-26 10:13:46 -07002360 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002361 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002362 if (!stencilSettings.isTwoSided()) {
2363 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2364 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002365 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002366 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2367 GR_GL_FRONT);
2368 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2369 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002370 }
joshualitta58fe352014-10-27 08:39:00 -07002371 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002372 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002373 }
2374}
2375
csmartdaltonc7d85332016-10-26 10:13:46 -07002376void GrGLGpu::disableStencil() {
2377 if (kNo_TriState != fHWStencilTestEnabled) {
2378 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002379
csmartdaltonc7d85332016-10-26 10:13:46 -07002380 fHWStencilTestEnabled = kNo_TriState;
2381 fHWStencilSettings.invalidate();
2382 }
2383}
2384
Chris Dalton4c56b032019-03-04 12:28:42 -07002385void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002386 // rt is only optional if useHWAA is false.
2387 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002388#ifdef SK_DEBUG
2389 if (useHWAA && rt->numSamples() <= 1) {
2390 SkASSERT(this->caps()->mixedSamplesSupport());
2391 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2392 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2393 }
2394#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002395
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002396 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002397 if (useHWAA) {
2398 if (kYes_TriState != fMSAAEnabled) {
2399 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2400 fMSAAEnabled = kYes_TriState;
2401 }
2402 } else {
2403 if (kNo_TriState != fMSAAEnabled) {
2404 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2405 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002406 }
2407 }
2408 }
2409}
2410
Chris Daltonce425af2019-12-16 10:39:03 -07002411void GrGLGpu::flushConservativeRasterState(bool enabled) {
2412 if (this->caps()->conservativeRasterSupport()) {
2413 if (enabled) {
2414 if (kYes_TriState != fHWConservativeRasterEnabled) {
2415 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2416 fHWConservativeRasterEnabled = kYes_TriState;
2417 }
2418 } else {
2419 if (kNo_TriState != fHWConservativeRasterEnabled) {
2420 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2421 fHWConservativeRasterEnabled = kNo_TriState;
2422 }
2423 }
2424 }
2425}
2426
Chris Dalton1215cda2019-12-17 21:44:04 -07002427void GrGLGpu::flushWireframeState(bool enabled) {
2428 if (this->caps()->wireframeSupport()) {
2429 if (this->caps()->wireframeMode() || enabled) {
2430 if (kYes_TriState != fHWWireframeEnabled) {
2431 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2432 fHWWireframeEnabled = kYes_TriState;
2433 }
2434 } else {
2435 if (kNo_TriState != fHWWireframeEnabled) {
2436 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2437 fHWWireframeEnabled = kNo_TriState;
2438 }
2439 }
2440 }
2441}
2442
Chris Daltonbbb3f642019-07-24 12:25:08 -04002443void GrGLGpu::flushBlendAndColorWrite(
2444 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2445 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2446 // We need to work around a driver bug by using a blend state that preserves the dst color,
2447 // rather than disabling color writes.
2448 GrXferProcessor::BlendInfo preserveDstBlend;
2449 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2450 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2451 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2452 return;
2453 }
bsalomonf7cc8772015-05-11 11:21:14 -07002454
cdalton8917d622015-05-06 13:40:21 -07002455 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002456 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2457 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002458
2459 // Any optimization to disable blending should have already been applied and
2460 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002461 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2462 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002463
egdanielb414f252014-07-29 13:15:47 -07002464 if (blendOff) {
2465 if (kNo_TriState != fHWBlendState.fEnabled) {
2466 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002467
2468 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2469 // https://code.google.com/p/skia/issues/detail?id=3943
2470 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2471 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2472 SkASSERT(this->caps()->advancedBlendEquationSupport());
2473 // Set to any basic blending equation.
2474 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2475 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2476 fHWBlendState.fEquation = blend_equation;
2477 }
2478
egdanielb414f252014-07-29 13:15:47 -07002479 fHWBlendState.fEnabled = kNo_TriState;
2480 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002481 } else {
2482 if (kYes_TriState != fHWBlendState.fEnabled) {
2483 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002484
Chris Daltonbbb3f642019-07-24 12:25:08 -04002485 fHWBlendState.fEnabled = kYes_TriState;
2486 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002487
Chris Daltonbbb3f642019-07-24 12:25:08 -04002488 if (fHWBlendState.fEquation != equation) {
2489 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2490 fHWBlendState.fEquation = equation;
2491 }
cdalton8917d622015-05-06 13:40:21 -07002492
Chris Daltonbbb3f642019-07-24 12:25:08 -04002493 if (GrBlendEquationIsAdvanced(equation)) {
2494 SkASSERT(this->caps()->advancedBlendEquationSupport());
2495 // Advanced equations have no other blend state.
2496 return;
2497 }
cdalton8917d622015-05-06 13:40:21 -07002498
Chris Daltonbbb3f642019-07-24 12:25:08 -04002499 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2500 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2501 gXfermodeCoeff2Blend[dstCoeff]));
2502 fHWBlendState.fSrcCoeff = srcCoeff;
2503 fHWBlendState.fDstCoeff = dstCoeff;
2504 }
cdalton8917d622015-05-06 13:40:21 -07002505
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002506 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002507 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2508 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2509 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2510 fHWBlendState.fConstColor = blendConst;
2511 fHWBlendState.fConstColorValid = true;
2512 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002513 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002514 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002515
2516 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002517}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002518
Brian Salomondc829942018-10-23 16:07:24 -04002519static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002520 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002521 switch (swizzle[i]) {
2522 case 'r': glValues[i] = GR_GL_RED; break;
2523 case 'g': glValues[i] = GR_GL_GREEN; break;
2524 case 'b': glValues[i] = GR_GL_BLUE; break;
2525 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002526 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002527 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002528 default: SK_ABORT("Unsupported component");
2529 }
Brian Salomon327955b2018-10-22 15:39:22 +00002530 }
2531}
2532
Greg Daniel2c3398d2019-06-19 11:58:01 -04002533void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2534 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002535 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002536
reed856e9d92015-09-30 12:21:45 -07002537#ifdef SK_DEBUG
2538 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002539 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002540 const int w = texture->width();
2541 const int h = texture->height();
2542 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2543 }
2544 }
2545#endif
2546
Robert Phillips294870f2016-11-11 12:38:40 -05002547 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002548 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002549 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002550 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002551 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002552 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002553 }
2554
Brian Salomondc829942018-10-23 16:07:24 -04002555 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002556 if (!this->caps()->mipMapSupport() ||
2557 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002558 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002559 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002560 }
bsalomonefd7d452014-10-23 14:17:46 -07002561
brianosman33f6b3f2016-06-02 05:49:21 -07002562#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002563 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002564 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002565 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002566 }
2567#endif
2568
Brian Salomone2826ab2019-06-04 15:58:31 -04002569 auto timestamp = texture->parameters()->resetTimestamp();
2570 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002571
Brian Salomone2826ab2019-06-04 15:58:31 -04002572 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2573 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002574 if (fSamplerObjectCache) {
2575 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2576 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002577 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2578 texture->parameters()->samplerOverriddenState();
2579 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002580
Brian Salomone2826ab2019-06-04 15:58:31 -04002581 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2582 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002583
Brian Salomone2826ab2019-06-04 15:58:31 -04002584 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2585 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002586
2587 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002588 newSamplerState.fMinLOD = -1000.f;
2589 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002590
Brian Salomone2826ab2019-06-04 15:58:31 -04002591 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002592 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002593 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002594 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002595 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002596 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002597 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002598 }
Mike Klein1bccae52018-10-19 11:38:44 +00002599 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002600 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002601 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002602 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002603 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002604 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002605 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002606 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002607 }
2608 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002609 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002610 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002611 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002612 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002613 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002614 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002615 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002616 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002617 if (this->glCaps().clampToBorderSupport()) {
2618 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002619 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002620 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002621 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2622 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002623 }
2624 }
Brian Salomondc829942018-10-23 16:07:24 -04002625 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002626 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2627 newNonsamplerState.fBaseMipMapLevel = 0;
2628 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002629
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2631 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002632 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002633 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2634 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002635 GrGLenum glValues[4];
2636 get_gl_swizzle_values(swizzle, glValues);
2637 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002638 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002639 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002640 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2641 reinterpret_cast<const GrGLint*>(glValues)));
2642 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002643 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2644 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2645 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2646 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2647 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002648 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002649 }
2650 }
Brian Salomondc829942018-10-23 16:07:24 -04002651 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002652 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2653 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2654 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002655 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002656 this->setTextureUnit(unitIdx);
2657 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002658 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002659 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002660 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002661 this->setTextureUnit(unitIdx);
2662 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002663 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002664 }
Brian Salomon327955b2018-10-22 15:39:22 +00002665 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002666 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2667 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002668}
2669
Brian Salomon1f05d452019-02-08 12:33:08 -05002670void GrGLGpu::onResetTextureBindings() {
2671 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2672 GR_GL_TEXTURE_EXTERNAL};
2673 for (int i = 0; i < this->numTextureUnits(); ++i) {
2674 this->setTextureUnit(i);
2675 for (auto target : kTargets) {
2676 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2677 GL_CALL(BindTexture(target, 0));
2678 }
2679 }
2680 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2681 }
2682}
2683
Chris Dalton5a2f9622019-12-27 14:56:38 -07002684void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2685 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2686 if (fHWPatchVertexCount != count) {
2687 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2688 fHWPatchVertexCount = count;
2689 }
2690}
2691
egdaniel080e6732014-12-22 07:35:52 -08002692void GrGLGpu::flushColorWrite(bool writeColor) {
2693 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002694 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002695 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2696 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002697 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002698 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002699 } else {
2700 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002701 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002702 fHWWriteToColor = kYes_TriState;
2703 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002704 }
bsalomon3e791242014-12-17 13:43:13 -08002705}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002706
Chris Dalton674f77a2019-09-30 20:49:39 -06002707void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2708 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2709 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2710 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2711 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2712 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2713 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2714 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002715 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2716 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2717 GL_CALL(ClearColor(r, g, b, a));
2718 fHWClearColor[0] = r;
2719 fHWClearColor[1] = g;
2720 fHWClearColor[2] = b;
2721 fHWClearColor[3] = a;
2722 }
2723}
2724
bsalomon861e1032014-12-16 07:33:49 -08002725void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002726 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002727 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002728 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002729 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002730 }
2731}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002732
Brian Salomond978b902019-02-07 15:09:18 -05002733void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002734 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002735 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002736 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2737 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2738 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002739 }
Brian Salomond978b902019-02-07 15:09:18 -05002740 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2741 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002742 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002743 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002744}
2745
Brian Salomone5e7eb12016-10-14 16:18:33 -04002746// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002747static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2748 const GrSurface* src,
2749 const SkIRect& srcRect,
2750 const SkIPoint& dstPoint,
2751 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002752 int dstSampleCnt = 0;
2753 int srcSampleCnt = 0;
2754 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002755 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002756 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002757 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002758 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002759 }
2760 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2761 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2762
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002763 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2764 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2765
Brian Salomone5e7eb12016-10-14 16:18:33 -04002766 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002767 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002768
Greg Daniel46cfbc62019-06-07 11:43:30 -04002769 GrTextureType dstTexType;
2770 GrTextureType* dstTexTypePtr = nullptr;
2771 GrTextureType srcTexType;
2772 GrTextureType* srcTexTypePtr = nullptr;
2773 if (dstTex) {
2774 dstTexType = dstTex->texturePriv().textureType();
2775 dstTexTypePtr = &dstTexType;
2776 }
2777 if (srcTex) {
2778 srcTexType = srcTex->texturePriv().textureType();
2779 srcTexTypePtr = &srcTexType;
2780 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002781
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002782 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2783 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002784 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002785}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002786
Brian Osmana9c8a052018-01-19 10:31:56 -05002787static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2788 // A RT has a separate MSAA renderbuffer if:
2789 // 1) It's multisampled
2790 // 2) We're using an extension with separate MSAA renderbuffers
2791 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002792 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002793}
2794
Greg Daniel46cfbc62019-06-07 11:43:30 -04002795static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002796 const GrGLCaps& caps) {
2797
bsalomon@google.comeb851172013-04-15 13:51:00 +00002798 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002799 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002800 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002801 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002802
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002803 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2804 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2805
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002806 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2807 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2808
Greg Daniel46cfbc62019-06-07 11:43:30 -04002809 GrTextureType dstTexType;
2810 GrTextureType* dstTexTypePtr = nullptr;
2811 GrTextureType srcTexType;
2812 GrTextureType* srcTexTypePtr = nullptr;
2813 if (dstTex) {
2814 dstTexType = dstTex->texturePriv().textureType();
2815 dstTexTypePtr = &dstTexType;
2816 }
2817 if (srcTex) {
2818 srcTexType = srcTex->texturePriv().textureType();
2819 srcTexTypePtr = &srcTexType;
2820 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002821
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002822 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2823 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002824}
2825
Greg Danielacd66b42019-05-22 16:29:12 -04002826// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002827void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002828 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002829 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002830 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002831 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002832 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2833 GrGLuint texID = texture->textureID();
2834 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002835 GrGLuint* tempFBOID;
2836 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002837
egdanield803f272015-03-18 13:01:52 -07002838 if (0 == *tempFBOID) {
2839 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002840 }
2841
Adrienne Walker4ee88512018-05-17 11:37:14 -07002842 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002843 GR_GL_CALL(
2844 this->glInterface(),
2845 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2846 if (mipLevel == 0) {
2847 texture->baseLevelWasBoundToFBO();
2848 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002849 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002850 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002851 }
egdaniel0f5f9672015-02-03 11:10:51 -08002852}
2853
Brian Salomond2a8ae22019-09-10 16:03:59 -04002854void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002855 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002856 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002857 SkASSERT(surface->asTexture());
2858 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2859 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2860 GR_GL_COLOR_ATTACHMENT0,
2861 textureTarget,
2862 0,
2863 0));
2864 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002865}
2866
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002867void GrGLGpu::onFBOChanged() {
2868 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2869 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2870 GL_CALL(Flush());
2871 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002872#ifdef SK_DEBUG
2873 if (fIsExecutingCommandBuffer_DebugOnly) {
2874 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2875 "This will severely hurt performance.\n");
2876 }
2877#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002878}
2879
Adrienne Walker4ee88512018-05-17 11:37:14 -07002880void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2881 fStats.incRenderTargetBinds();
2882 GL_CALL(BindFramebuffer(target, fboid));
2883 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2884 fBoundDrawFramebuffer = fboid;
2885 }
2886
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002887 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2888 // The driver forgets the correct scissor when modifying the FBO binding.
2889 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002890 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002891 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002892 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002893 }
2894
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002895 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002896}
2897
Adrienne Walker4ee88512018-05-17 11:37:14 -07002898void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002899 // We're relying on the GL state shadowing being correct in the workaround code below so we
2900 // need to handle a dirty context.
2901 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002902 if (fboid == fBoundDrawFramebuffer &&
2903 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2904 // This workaround only applies to deleting currently bound framebuffers
2905 // on Adreno 420. Because this is a somewhat rare case, instead of
2906 // tracking all the attachments of every framebuffer instead just always
2907 // unbind all attachments.
2908 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2909 GR_GL_RENDERBUFFER, 0));
2910 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2911 GR_GL_RENDERBUFFER, 0));
2912 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2913 GR_GL_RENDERBUFFER, 0));
2914 }
2915
2916 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002917
2918 // Deleting the currently bound framebuffer rebinds to 0.
2919 if (fboid == fBoundDrawFramebuffer) {
2920 this->onFBOChanged();
2921 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002922}
2923
Greg Daniel46cfbc62019-06-07 11:43:30 -04002924bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002925 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002926 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2927 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2928 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002929 auto dstFormat = dst->backendFormat().asGLFormat();
2930 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002931 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002932 return true;
2933 }
2934 }
cblume61214052016-01-26 09:10:48 -08002935
Greg Daniel46cfbc62019-06-07 11:43:30 -04002936 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2937 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002938 return true;
2939 }
2940
Greg Daniel46cfbc62019-06-07 11:43:30 -04002941 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2942 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002943 }
2944
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002945 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002946 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002947 return true;
2948 }
bsalomon083617b2016-02-12 12:10:14 -08002949 }
2950
bsalomon6df86402015-06-01 10:41:49 -07002951 return false;
2952}
2953
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002954bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002955 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002956
2957 int progIdx = TextureToCopyProgramIdx(srcTex);
2958 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2959 GrSLType samplerType =
2960 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2961
2962 if (!fCopyProgramArrayBuffer) {
2963 static const GrGLfloat vdata[] = {
2964 0, 0,
2965 0, 1,
2966 1, 0,
2967 1, 1
2968 };
2969 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2970 kStatic_GrAccessPattern, vdata);
2971 }
2972 if (!fCopyProgramArrayBuffer) {
2973 return false;
2974 }
2975
2976 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2977 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
2978 if (!fCopyPrograms[progIdx].fProgram) {
2979 return false;
2980 }
2981
Ben Wagnerdabd98c2020-03-25 15:17:18 -04002982 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002983 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04002984 GrShaderVar::TypeModifier::Uniform);
2985 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
2986 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
2987 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
2988 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002989
Greg Daniel9e3169b2019-06-06 14:38:17 -04002990 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991 if (shaderCaps->noperspectiveInterpolationSupport()) {
2992 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
2993 vshaderTxt.appendf("#extension %s : require\n", extension);
2994 }
2995 vTexCoord.addModifier("noperspective");
2996 }
2997
2998 aVertex.appendDecl(shaderCaps, &vshaderTxt);
2999 vshaderTxt.append(";");
3000 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3001 vshaderTxt.append(";");
3002 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3003 vshaderTxt.append(";");
3004 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3005 vshaderTxt.append(";");
3006
3007 vshaderTxt.append(
3008 "// Copy Program VS\n"
3009 "void main() {"
3010 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3011 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3012 " sk_Position.zw = half2(0, 1);"
3013 "}"
3014 );
3015
Greg Daniel9e3169b2019-06-06 14:38:17 -04003016 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003017 if (shaderCaps->noperspectiveInterpolationSupport()) {
3018 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3019 fshaderTxt.appendf("#extension %s : require\n", extension);
3020 }
3021 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003022 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003023 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3024 fshaderTxt.append(";");
3025 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3026 fshaderTxt.append(";");
3027 fshaderTxt.appendf(
3028 "// Copy Program FS\n"
3029 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003030 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003031 "}"
3032 );
3033
3034 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3035 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3036 SkSL::Program::Settings settings;
3037 settings.fCaps = shaderCaps;
3038 SkSL::String glsl;
3039 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3040 sksl, settings, &glsl, errorHandler);
3041 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3042 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3043 SkASSERT(program->fInputs.isEmpty());
3044
3045 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3046 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3047 errorHandler);
3048 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3049 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3050 errorHandler);
3051 SkASSERT(program->fInputs.isEmpty());
3052
3053 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3054
3055 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3056 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3057 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3058 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3059 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3060 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3061
3062 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3063
3064 GL_CALL(DeleteShader(vshader));
3065 GL_CALL(DeleteShader(fshader));
3066
3067 return true;
3068}
3069
brianosman33f6b3f2016-06-02 05:49:21 -07003070bool GrGLGpu::createMipmapProgram(int progIdx) {
3071 const bool oddWidth = SkToBool(progIdx & 0x2);
3072 const bool oddHeight = SkToBool(progIdx & 0x1);
3073 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3074
Brian Salomon1edc5b92016-11-29 13:43:46 -05003075 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003076
3077 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3078 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3079 if (!fMipmapPrograms[progIdx].fProgram) {
3080 return false;
3081 }
3082
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003083 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003084 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003085 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003086 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003087 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003088 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003089 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003090 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3091 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3092 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3093 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003094 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003095 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003096
Ethan Nicholasfc994162019-06-06 10:04:27 -04003097 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003098 if (shaderCaps->noperspectiveInterpolationSupport()) {
3099 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003100 vshaderTxt.appendf("#extension %s : require\n", extension);
3101 }
3102 vTexCoords[0].addModifier("noperspective");
3103 vTexCoords[1].addModifier("noperspective");
3104 vTexCoords[2].addModifier("noperspective");
3105 vTexCoords[3].addModifier("noperspective");
3106 }
3107
Brian Salomon1edc5b92016-11-29 13:43:46 -05003108 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003109 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003110 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003111 vshaderTxt.append(";");
3112 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003113 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003114 vshaderTxt.append(";");
3115 }
3116
3117 vshaderTxt.append(
3118 "// Mipmap Program VS\n"
3119 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003120 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3121 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003122 );
3123
3124 // Insert texture coordinate computation:
3125 if (oddWidth && oddHeight) {
3126 vshaderTxt.append(
3127 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003128 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3129 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003130 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3131 );
3132 } else if (oddWidth) {
3133 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003134 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3135 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003136 );
3137 } else if (oddHeight) {
3138 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003139 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3140 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003141 );
3142 } else {
3143 vshaderTxt.append(
3144 " v_texCoord0 = a_vertex.xy;"
3145 );
3146 }
3147
3148 vshaderTxt.append("}");
3149
Ethan Nicholasfc994162019-06-06 10:04:27 -04003150 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003151 if (shaderCaps->noperspectiveInterpolationSupport()) {
3152 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003153 fshaderTxt.appendf("#extension %s : require\n", extension);
3154 }
3155 }
brianosman33f6b3f2016-06-02 05:49:21 -07003156 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003157 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003158 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003159 fshaderTxt.append(";");
3160 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003161 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003162 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003163 fshaderTxt.append(
3164 "// Mipmap Program FS\n"
3165 "void main() {"
3166 );
3167
3168 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003169 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003170 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3171 " sample(u_texture, v_texCoord1) + "
3172 " sample(u_texture, v_texCoord2) + "
3173 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003174 );
3175 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003176 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003177 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3178 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003179 );
3180 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003181 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003182 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003183 );
3184 }
3185
3186 fshaderTxt.append("}");
3187
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003188 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003189 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003190 SkSL::Program::Settings settings;
3191 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003192 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003193 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003194 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003195 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003196 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003197 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003198
Brian Osman6c431d52019-04-15 16:31:54 -04003199 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003200 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3201 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003202 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003203 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003204 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003205 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003206
3207 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3208
3209 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3210 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3211 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3212 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3213
3214 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3215
3216 GL_CALL(DeleteShader(vshader));
3217 GL_CALL(DeleteShader(fshader));
3218
3219 return true;
3220}
3221
Greg Daniel46cfbc62019-06-07 11:43:30 -04003222bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003223 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003224 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3225 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3226 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3227 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003228 return false;
3229 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003230 int progIdx = TextureToCopyProgramIdx(srcTex);
3231 if (!dstRT) {
3232 SkASSERT(dstTex);
3233 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3234 return false;
3235 }
3236 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003237 if (!fCopyPrograms[progIdx].fProgram) {
3238 if (!this->createCopyProgram(srcTex)) {
3239 SkDebugf("Failed to create copy program.\n");
3240 return false;
3241 }
3242 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003243 int w = srcRect.width();
3244 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003245 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003246 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003247 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003248 this->flushViewport(dst->width(), dst->height());
3249 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003250 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003251 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003252 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003253 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3254 attribs->enableVertexArrays(this, 1);
3255 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3256 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003257 // dst rect edges in NDC (-1 to 1)
3258 int dw = dst->width();
3259 int dh = dst->height();
3260 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3261 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3262 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3263 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003264 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3265 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3266 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3267 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3268 int sw = src->width();
3269 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003270 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3271 // src rect edges in normalized texture space (0 to 1)
3272 sx0 /= sw;
3273 sx1 /= sw;
3274 sy0 /= sh;
3275 sy1 /= sh;
3276 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003277 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3278 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3279 sx1 - sx0, sy1 - sy0, sx0, sy0));
3280 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003281 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003282 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003283 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003284 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003285 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003286 this->disableWindowRectangles();
3287 this->disableStencil();
3288 if (this->glCaps().srgbWriteControl()) {
3289 this->flushFramebufferSRGB(true);
3290 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003291 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003292 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003293 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3294 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003295 return true;
3296}
3297
Greg Daniel46cfbc62019-06-07 11:43:30 -04003298void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003299 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003300 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003301 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003302 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003303 SkASSERT(dstTex);
3304 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003305 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003306
Brian Salomond978b902019-02-07 15:09:18 -05003307 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003308 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003309 dstPoint.fX, dstPoint.fY,
3310 srcRect.fLeft, srcRect.fTop,
3311 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003312 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003313 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3314 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003315 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3316 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003317}
3318
Greg Daniel46cfbc62019-06-07 11:43:30 -04003319bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003320 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003321 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003322 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3323 srcRect.width(), srcRect.height());
3324 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003325 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003326 return false;
mtklein404b3b22015-05-18 09:29:10 -07003327 }
bsalomon5df6fee2015-05-18 06:26:15 -07003328 }
bsalomon6df86402015-06-01 10:41:49 -07003329
Brian Salomond2a8ae22019-09-10 16:03:59 -04003330 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3331 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003332 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003333 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003334
3335 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003336 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003337 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003338
Greg Daniel46cfbc62019-06-07 11:43:30 -04003339 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3340 srcRect.fTop,
3341 srcRect.fRight,
3342 srcRect.fBottom,
3343 dstRect.fLeft,
3344 dstRect.fTop,
3345 dstRect.fRight,
3346 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003347 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003348 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3349 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003350
3351 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3352 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003353 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003354}
3355
Brian Salomon930f9392018-06-20 16:25:26 -04003356bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3357 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003358 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003359 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003360 return false;
3361 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003362 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003363 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3364 // Uses draw calls to do a series of downsample operations to successive mips.
3365
3366 // The manual approach requires the ability to limit which level we're sampling and that the
3367 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003368 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003369 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003370 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003371 GL_CALL(GenerateMipmap(glTex->target()));
3372 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003373 }
3374
brianosman33f6b3f2016-06-02 05:49:21 -07003375 int width = texture->width();
3376 int height = texture->height();
3377 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003378 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003379
3380 // Create (if necessary), then bind temporary FBO:
3381 if (0 == fTempDstFBOID) {
3382 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3383 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003384 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003385 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003386
3387 // Bind the texture, to get things configured for filtering.
3388 // We'll be changing our base level further below:
3389 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003390 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003391 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003392
3393 // Vertex data:
3394 if (!fMipmapProgramArrayBuffer) {
3395 static const GrGLfloat vdata[] = {
3396 0, 0,
3397 0, 1,
3398 1, 0,
3399 1, 1
3400 };
Brian Salomonae64c192019-02-05 09:41:37 -05003401 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003402 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003403 }
3404 if (!fMipmapProgramArrayBuffer) {
3405 return false;
3406 }
3407
3408 fHWVertexArrayState.setVertexArrayID(this, 0);
3409
3410 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003411 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003412 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003413 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003414
3415 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003416 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003417 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003418 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003419 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003420 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003421
3422 // Do all the blits:
3423 width = texture->width();
3424 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003425
brianosman33f6b3f2016-06-02 05:49:21 -07003426 for (GrGLint level = 1; level < levelCount; ++level) {
3427 // Get and bind the program for this particular downsample (filter shape can vary):
3428 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3429 if (!fMipmapPrograms[progIdx].fProgram) {
3430 if (!this->createMipmapProgram(progIdx)) {
3431 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003432 // Invalidate all params to cover base level change in a previous iteration.
3433 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003434 return false;
3435 }
3436 }
Brian Salomon802cb312018-06-08 18:05:20 -04003437 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003438
3439 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3440 const float invWidth = 1.0f / width;
3441 const float invHeight = 1.0f / height;
3442 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3443 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3444 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3445
3446 // Only sample from previous mip
3447 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3448
Brian Salomon930f9392018-06-20 16:25:26 -04003449 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3450 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003451
Brian Osman788b9162020-02-07 10:36:46 -05003452 width = std::max(1, width / 2);
3453 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003454 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003455
3456 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3457 }
3458
3459 // Unbind:
3460 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3461 GR_GL_TEXTURE_2D, 0, 0));
3462
Brian Salomon930f9392018-06-20 16:25:26 -04003463 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003464 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3465 // We drew the 2nd to last level into the last level.
3466 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3467 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003468
brianosman33f6b3f2016-06-02 05:49:21 -07003469 return true;
3470}
3471
Chris Daltond7291ba2019-03-07 14:17:03 -07003472void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003473 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3474 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003475
3476 int effectiveSampleCnt;
3477 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003478 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003479
3480 sampleLocations->reset(effectiveSampleCnt);
3481 for (int i = 0; i < effectiveSampleCnt; ++i) {
3482 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3483 }
3484}
3485
cdalton231c5fd2015-05-13 12:35:36 -07003486void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003487 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003488 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003489 case kTexture_GrXferBarrierType: {
3490 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003491 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003492 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3493 // The render target uses separate storage so no need for glTextureBarrier.
3494 // FIXME: The render target will resolve automatically when its texture is bound,
3495 // but we could resolve only the bounds that will be read if we do it here instead.
3496 return;
3497 }
cdalton9954bc32015-04-29 14:17:00 -07003498 SkASSERT(this->caps()->textureBarrierSupport());
3499 GL_CALL(TextureBarrier());
3500 return;
cdalton231c5fd2015-05-13 12:35:36 -07003501 }
cdalton8917d622015-05-06 13:40:21 -07003502 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003503 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003504 this->caps()->blendEquationSupport());
3505 GL_CALL(BlendBarrier());
3506 return;
bsalomoncb02b382015-08-12 11:14:50 -07003507 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003508 }
3509}
3510
Chris Daltone1196c52019-12-28 14:31:09 -07003511void GrGLGpu::insertManualFramebufferBarrier() {
3512 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3513 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3514}
3515
Brian Salomon85c3d682019-11-04 15:04:54 -05003516GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003517 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003518 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003519 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003520 GrProtected isProtected,
3521 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003522 // We don't support protected textures in GL.
3523 if (isProtected == GrProtected::kYes) {
3524 return {};
3525 }
3526
Brian Salomon8a375832018-03-14 10:21:40 -04003527 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003528
Brian Salomond4764a12019-08-08 12:08:24 -04003529 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003530 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003531 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003532 }
3533
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003534 int numMipLevels = 1;
3535 if (mipMapped == GrMipMapped::kYes) {
3536 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3537 }
3538
Robert Phillipsd34691b2019-09-24 13:38:43 -04003539 // Compressed formats go through onCreateCompressedBackendTexture
3540 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3541
Greg Daniel15500642019-06-27 03:05:55 +00003542 GrGLTextureInfo info;
3543 GrGLTextureParameters::SamplerOverriddenState initialState;
3544
Greg Danield51fa2f2020-01-22 16:53:38 -05003545 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003546 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003547 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003548
Robert Phillipsd34691b2019-09-24 13:38:43 -04003549 info.fTarget = GR_GL_TEXTURE_2D;
3550 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003551 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003552 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003553 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003554 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003555
Robert Phillips42716d42019-12-16 12:19:54 -05003556 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003557 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3558 SkTDArray<GrMipLevel> texels;
3559 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003560 texels.append(numMipLevels);
3561 for (int i = 0; i < numMipLevels; ++i) {
3562 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3563 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003564 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3565 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3566 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003567 GL_CALL(DeleteTextures(1, &info.fID));
3568 return {};
3569 }
3570 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3571 // TODO: Unify this with the clear texture code in onCreateTexture().
3572 GrColorType colorType;
3573 GrGLenum externalFormat, externalType;
3574 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3575 &externalType, &colorType);
3576 if (colorType == GrColorType::kUnknown) {
3577 GL_CALL(DeleteTextures(1, &info.fID));
3578 return {};
3579 }
3580
3581 // Make one tight image at the base size and reuse it for smaller levels.
3582 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3583 auto rb = ii.minRowBytes();
3584 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3585 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3586 GL_CALL(DeleteTextures(1, &info.fID));
3587 return {};
3588 }
3589
3590 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3591 SkISize levelDimensions = dimensions;
3592 for (int i = 0; i < numMipLevels; ++i) {
3593 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3594 levelDimensions.height(), externalFormat, externalType,
3595 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003596 levelDimensions = {std::max(1, levelDimensions.width() /2),
3597 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003598 }
3599 }
3600 // Unbind this texture from the scratch texture unit.
3601 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003602
Brian Salomone2826ab2019-06-04 15:58:31 -04003603 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003604 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003605 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3606 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003607
Brian Salomon85c3d682019-11-04 15:04:54 -05003608 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3609 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003610}
3611
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003612void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003613 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3614
3615 GrGLTextureInfo info;
3616 if (tex.getGLTextureInfo(&info)) {
3617 GL_CALL(DeleteTextures(1, &info.fID));
3618 }
3619}
3620
3621#if GR_TEST_UTILS
3622
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003623bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003624 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003625
Greg Daniel52e16d92018-04-10 09:34:07 -04003626 GrGLTextureInfo info;
3627 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003628 return false;
3629 }
3630
3631 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003632 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003633
3634 return (GR_GL_TRUE == result);
3635}
3636
Brian Salomonf865b052018-03-09 09:01:53 -05003637GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003638 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003639 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3640 return GrBackendRenderTarget(); // invalid
3641 }
Brian Salomon8a375832018-03-14 10:21:40 -04003642 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003643 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003644 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003645 return {};
3646 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003647 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003648 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003649 if (sFormatIdx < 0) {
3650 return {};
3651 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003652 GrGLuint colorID = 0;
3653 GrGLuint stencilID = 0;
3654 auto deleteIDs = [&] {
3655 if (colorID) {
3656 if (useTexture) {
3657 GL_CALL(DeleteTextures(1, &colorID));
3658 } else {
3659 GL_CALL(DeleteRenderbuffers(1, &colorID));
3660 }
Brian Salomonf865b052018-03-09 09:01:53 -05003661 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003662 if (stencilID) {
3663 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003664 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003665 };
3666
3667 if (useTexture) {
3668 GL_CALL(GenTextures(1, &colorID));
3669 } else {
3670 GL_CALL(GenRenderbuffers(1, &colorID));
3671 }
3672 GL_CALL(GenRenderbuffers(1, &stencilID));
3673 if (!stencilID || !colorID) {
3674 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003675 return {};
3676 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003677
Brian Salomonf865b052018-03-09 09:01:53 -05003678 GrGLFramebufferInfo info;
3679 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003680 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003681 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3682 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003683 deleteIDs();
3684 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003685 }
3686
3687 this->invalidateBoundRenderTarget();
3688
Adrienne Walker4ee88512018-05-17 11:37:14 -07003689 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003690 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003691 GrGLTextureParameters::SamplerOverriddenState initialState;
3692 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3693 if (!colorID) {
3694 deleteIDs();
3695 return {};
3696 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003697 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3698 colorID, 0));
3699 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003700 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003701 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003702 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003703 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3704 GR_GL_RENDERBUFFER, colorID));
3705 }
3706 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003707 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003708 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003709 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003710 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003711 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3712 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003713 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003714 }
3715
Brian Salomon93348dd2018-08-29 12:56:23 -04003716 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3717 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3718 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3719 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003720 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003721 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003722
Adrienne Walker4ee88512018-05-17 11:37:14 -07003723 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003724 GrGLenum status;
3725 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3726 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003727 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003728 return {};
3729 }
3730 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003731
Greg Daniel108bb232018-07-03 16:18:29 -04003732 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003733 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003734 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003735}
3736
3737void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003738 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003739 GrGLFramebufferInfo info;
3740 if (backendRT.getGLFramebufferInfo(&info)) {
3741 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003742 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003743 }
3744 }
joshualitt8fd844f2015-12-02 13:36:47 -08003745}
3746
Greg Daniel26b50a42018-03-08 09:49:58 -05003747void GrGLGpu::testingOnly_flushGpuAndSync() {
3748 GL_CALL(Finish());
3749}
Brian Salomonf865b052018-03-09 09:01:53 -05003750#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003751
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003752///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003753
cdaltone2e71c22016-04-07 18:13:29 -07003754GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003755 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003756 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003757 GrGLAttribArrayState* attribState;
3758
cdaltone2e71c22016-04-07 18:13:29 -07003759 if (gpu->glCaps().isCoreProfile()) {
3760 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003761 GrGLuint arrayID;
3762 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3763 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003764 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003765 }
cdaltone2e71c22016-04-07 18:13:29 -07003766 if (ibuf) {
3767 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003768 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003769 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003770 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003771 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003772 if (ibuf) {
3773 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003774 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003775 } else {
3776 this->setVertexArrayID(gpu, 0);
3777 }
3778 int attrCount = gpu->glCaps().maxVertexAttributes();
3779 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3780 fDefaultVertexArrayAttribState.resize(attrCount);
3781 }
3782 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003783 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003784 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003785}
bsalomone179a912016-01-20 06:18:10 -08003786
Greg Daniel30a35e82019-11-19 14:12:25 -05003787bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003788 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003789 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003790 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003791 // We call finish if the client told us to sync or if we have a finished proc but don't support
3792 // GLsync objects.
3793 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3794 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3795 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003796 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003797 // After a finish everything previously sent to GL is done.
3798 for (const auto& cb : fFinishCallbacks) {
3799 cb.fCallback(cb.fContext);
3800 this->deleteSync(cb.fSync);
3801 }
3802 fFinishCallbacks.clear();
3803 if (info.fFinishedProc) {
3804 info.fFinishedProc(info.fFinishedContext);
3805 }
3806 } else {
3807 if (info.fFinishedProc) {
3808 FinishCallback callback;
3809 callback.fCallback = info.fFinishedProc;
3810 callback.fContext = info.fFinishedContext;
3811 callback.fSync = (GrGLsync)this->insertFence();
3812 fFinishCallbacks.push_back(callback);
3813 GL_CALL(Flush());
3814 } else if (insertedSemaphore) {
3815 // Must call flush after semaphores in case they are waited on another GL context.
3816 GL_CALL(Flush());
3817 }
3818 // See if any previously inserted finish procs are good to go.
3819 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003820 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003821 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003822}
3823
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003824void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3825 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3826 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3827 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003828}
3829
Greg Daniel6be35232017-03-01 17:01:09 -05003830GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003831 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003832 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003833 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3834 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3835 GrGLuint fence = 0;
3836 GL_CALL(GenFences(1, &fence));
3837 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3838 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3839 } else {
3840 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3841 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05003842 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003843 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003844}
3845
Brian Salomonb0d8b762019-05-06 16:58:22 -04003846bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003847 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3848 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3849 if (!timeout) {
3850 if (flush) {
3851 GL_CALL(Flush);
3852 }
3853 GrGLboolean result;
3854 GL_CALL_RET(result, TestFence(nvFence));
3855 return result == GR_GL_TRUE;
3856 }
3857 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3858 // If this really becomes necessary we could poll TestFence().
3859 // FinishFence always flushes so no need to check flush param.
3860 GL_CALL(FinishFence(nvFence));
3861 return true;
3862 } else {
3863 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3864 GrGLenum result;
3865 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3866 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3867 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003868}
3869
3870bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3871 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003872}
3873
3874void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003875 this->deleteSync((GrGLsync)fence);
3876}
3877
Greg Daniel301015c2019-11-18 14:06:46 -05003878std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003879 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003880 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003881}
3882
Greg Daniel301015c2019-11-18 14:06:46 -05003883std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3884 const GrBackendSemaphore& semaphore,
3885 GrResourceProvider::SemaphoreWrapType wrapType,
3886 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003887 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003888 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3889}
3890
Greg Daniel301015c2019-11-18 14:06:46 -05003891void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3892 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003893
Greg Daniel48661b82018-01-22 16:11:35 -05003894 GrGLsync sync;
3895 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3896 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05003897}
3898
Greg Daniel301015c2019-11-18 14:06:46 -05003899void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
3900 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003901
3902 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3903}
3904
Brian Salomonb0d8b762019-05-06 16:58:22 -04003905void GrGLGpu::checkFinishProcs() {
3906 // Bail after the first unfinished sync since we expect they signal in the order inserted.
3907 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
3908 /* timeout = */ 0, /* flush = */ false)) {
3909 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
3910 this->deleteSync(fFinishCallbacks.front().fSync);
3911 fFinishCallbacks.pop_front();
3912 }
3913}
3914
Greg Daniel6be35232017-03-01 17:01:09 -05003915void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003916 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3917 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
3918 GL_CALL(DeleteFences(1, &nvFence));
3919 } else {
3920 GL_CALL(DeleteSync(sync));
3921 }
jvanverth84741b32016-09-30 08:39:02 -07003922}
Brian Osman13dddce2017-05-09 13:19:50 -04003923
Greg Daniel301015c2019-11-18 14:06:46 -05003924std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04003925 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05003926 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05003927 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003928 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05003929 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
3930 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04003931
3932 return semaphore;
3933}
Robert Phillips646e4292017-06-13 12:44:56 -04003934
3935int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04003936 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04003937 case kTexture2DSampler_GrSLType:
3938 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04003939 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003940 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04003941 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003942 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04003943 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04003944 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04003945 }
3946}
Brian Osman71a18892017-08-10 10:23:25 -04003947
Kevin Lubickf4def342018-10-04 12:52:50 -04003948#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003949#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04003950void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
3951 // We are called by the base class, which has already called beginObject(). We choose to nest
3952 // all of our caps information in a named sub-object.
3953 writer->beginObject("GL GPU");
3954
3955 const GrGLubyte* str;
3956 GL_CALL_RET(str, GetString(GR_GL_VERSION));
3957 writer->appendString("GL_VERSION", (const char*)(str));
3958 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
3959 writer->appendString("GL_RENDERER", (const char*)(str));
3960 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
3961 writer->appendString("GL_VENDOR", (const char*)(str));
3962 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
3963 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
3964
3965 writer->appendName("extensions");
3966 glInterface()->fExtensions.dumpJSON(writer);
3967
3968 writer->endObject();
3969}
Kevin Lubickf4def342018-10-04 12:52:50 -04003970#endif