blob: b00601193bf497b903adafb701d38f0ad42be316 [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"
Greg Daniel01f278c2020-06-12 16:58:17 -040022#include "src/gpu/GrBackendUtils.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040023#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040025#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#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)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400333 , fStencilClearFBOID(0)
334 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500335 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400336 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500337 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000338
Brian Salomond978b902019-02-07 15:09:18 -0500339 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000340
Brian Salomonae64c192019-02-05 09:41:37 -0500341 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
342 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600343 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500344 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500345 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
346 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
347 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
348 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700349 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500350 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
351 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700352 }
Brian Salomonae64c192019-02-05 09:41:37 -0500353 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400354 fHWBufferState[i].invalidate();
355 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600356 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700357
358 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
359 fPathRendering.reset(new GrGLPathRendering(this));
360 }
361
Brian Salomondc829942018-10-23 16:07:24 -0400362 if (this->glCaps().samplerObjectSupport()) {
363 fSamplerObjectCache.reset(new SamplerObjectCache(this));
364 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000365}
366
bsalomon861e1032014-12-16 07:33:49 -0800367GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700368 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
369 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800370 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700371 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700372 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800373
Chris Dalton20e7a532020-02-28 15:37:53 +0000374 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400375 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000376 // detach the current program so there is no confusion on OpenGL's part
377 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000378 GL_CALL(UseProgram(0));
379 }
380
Brian Salomon43f8bf02017-10-18 08:33:29 -0400381 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700382 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800383 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400384 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700385 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800386 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400387 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700388 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800389 }
egdaniel0f5f9672015-02-03 11:10:51 -0800390
bsalomon7ea33f52015-11-22 14:51:00 -0800391 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
392 if (0 != fCopyPrograms[i].fProgram) {
393 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
394 }
bsalomon6df86402015-06-01 10:41:49 -0700395 }
bsalomon6dea83f2015-12-03 12:58:06 -0800396
brianosman33f6b3f2016-06-02 05:49:21 -0700397 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
398 if (0 != fMipmapPrograms[i].fProgram) {
399 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
400 }
401 }
402
Brian Salomondc829942018-10-23 16:07:24 -0400403 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500404
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400405 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700406}
407
bsalomon6e2aad42016-04-01 11:54:31 -0700408void GrGLGpu::disconnect(DisconnectType type) {
409 INHERITED::disconnect(type);
410 if (DisconnectType::kCleanup == type) {
411 if (fHWProgramID) {
412 GL_CALL(UseProgram(0));
413 }
414 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700415 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700416 }
417 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700418 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700419 }
420 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700421 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700422 }
bsalomon6e2aad42016-04-01 11:54:31 -0700423 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
424 if (fCopyPrograms[i].fProgram) {
425 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
426 }
427 }
brianosman33f6b3f2016-06-02 05:49:21 -0700428 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
429 if (fMipmapPrograms[i].fProgram) {
430 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
431 }
432 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400433
Brian Salomondc829942018-10-23 16:07:24 -0400434 if (fSamplerObjectCache) {
435 fSamplerObjectCache->release();
436 }
bsalomon6e2aad42016-04-01 11:54:31 -0700437 } else {
438 if (fProgramCache) {
439 fProgramCache->abandon();
440 }
Brian Salomondc829942018-10-23 16:07:24 -0400441 if (fSamplerObjectCache) {
442 fSamplerObjectCache->abandon();
443 }
bsalomon6e2aad42016-04-01 11:54:31 -0700444 }
445
Chris Dalton20e7a532020-02-28 15:37:53 +0000446 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500447 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700448
bsalomonc8dc1f72014-08-21 13:02:13 -0700449 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700450 fTempSrcFBOID = 0;
451 fTempDstFBOID = 0;
452 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700453 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800454 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
455 fCopyPrograms[i].fProgram = 0;
456 }
brianosman33f6b3f2016-06-02 05:49:21 -0700457 fMipmapProgramArrayBuffer.reset();
458 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
459 fMipmapPrograms[i].fProgram = 0;
460 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400461
jvanverthe9c0fc62015-04-29 11:18:05 -0700462 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700463 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700464 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400465 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000466}
467
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000468///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000469
bsalomon861e1032014-12-16 07:33:49 -0800470void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000471 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400472 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000473 GL_CALL(Disable(GR_GL_DEPTH_TEST));
474 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000475
Brian Salomonf0861672017-05-08 11:10:10 -0400476 // We don't use face culling.
477 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600478 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
479 // just set this to the default for self-consistency.
480 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400481
Brian Salomonae64c192019-02-05 09:41:37 -0500482 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
483 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700484
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400485 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500486#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000487 // Desktop-only state that we never change
488 if (!this->glCaps().isCoreProfile()) {
489 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
490 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
491 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
492 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
493 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
494 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
495 }
496 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
497 // core profile. This seems like a bug since the core spec removes any mention of
498 // GL_ARB_imaging.
499 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
500 GL_CALL(Disable(GR_GL_COLOR_TABLE));
501 }
502 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400503
Chris Dalton1215cda2019-12-17 21:44:04 -0700504 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500505#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000506 // Since ES doesn't support glPointSize at all we always use the VS to
507 // set the point size
508 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
509
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000510 }
joshualitt58162332014-08-01 06:44:53 -0700511
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400512 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500513 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700514 // The arm extension requires specifically enabling MSAA fetching per sample.
515 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500516 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700517 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000518 fHWWriteToColor = kUnknown_TriState;
519 // we only ever use lines in hairline mode
520 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700521 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500522
523 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000524 }
edisonn@google.comba669992013-06-28 16:03:21 +0000525
egdanielb414f252014-07-29 13:15:47 -0700526 if (resetBits & kMSAAEnable_GrGLBackendState) {
527 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700528
Chris Dalton6ce447a2019-06-23 18:07:38 -0600529 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800530 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
531 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700532 GL_CALL(CoverageModulation(GR_GL_RGBA));
533 }
Chris Daltonce425af2019-12-16 10:39:03 -0700534
535 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000538 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400539 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000540
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000541 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500542 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500543 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000544 }
Brian Salomondc829942018-10-23 16:07:24 -0400545 if (fSamplerObjectCache) {
546 fSamplerObjectCache->invalidateBindings();
547 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000548 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000549
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000550 if (resetBits & kBlend_GrGLBackendState) {
551 fHWBlendState.invalidate();
552 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000553
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 if (resetBits & kView_GrGLBackendState) {
555 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700556 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000557 fHWViewport.invalidate();
558 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000560 if (resetBits & kStencil_GrGLBackendState) {
561 fHWStencilSettings.invalidate();
562 fHWStencilTestEnabled = kUnknown_TriState;
563 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000564
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000565 // Vertex
566 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700567 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500568 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
569 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600570 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700571 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000572 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000574 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500575 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700576 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500577 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000578 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000579
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000580 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700581 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700582 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000584 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000585
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000586 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400588 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
590 }
Brian Salomon1047a492019-07-02 12:25:21 -0400591 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
593 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 if (this->glCaps().packFlipYSupport()) {
595 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
596 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000597 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000598
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 if (resetBits & kProgram_GrGLBackendState) {
600 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000601 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400603 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000604}
605
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400606static bool check_backend_texture(const GrBackendTexture& backendTex,
607 const GrGLCaps& caps,
608 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400609 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400610 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400611 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400612 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800613 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000614
Brian Salomonea4ad302019-08-07 13:04:55 -0400615 desc->fSize = {backendTex.width(), backendTex.height()};
616 desc->fTarget = info.fTarget;
617 desc->fID = info.fID;
618 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800619
Brian Salomonea4ad302019-08-07 13:04:55 -0400620 if (desc->fFormat == GrGLFormat::kUnknown) {
621 return false;
622 }
623 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400624 if (!caps.shaderCaps()->externalTextureSupport()) {
625 return false;
626 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400627 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
628 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400629 return false;
630 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400631 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400632 return false;
633 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400634 if (backendTex.isProtected()) {
635 // Not supported in GL backend at this time.
636 return false;
637 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400638
Brian Salomond17f6582017-07-19 18:28:58 -0400639 return true;
640}
641
642sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400643 GrWrapOwnership ownership,
644 GrWrapCacheable cacheable,
645 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400646 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400647 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400648 return nullptr;
649 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400650
Brian Salomond17f6582017-07-19 18:28:58 -0400651 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400652 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400653 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400654 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400655 }
bsalomone5286e02016-01-14 09:24:09 -0800656
Greg Daniel177e6952017-10-12 12:27:11 -0400657 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
658 : GrMipMapsStatus::kNotAllocated;
659
Brian Salomonea4ad302019-08-07 13:04:55 -0400660 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400661 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400662 // We don't know what parameters are already set on wrapped textures.
663 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600664 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400665}
666
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500667static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
668 const GrGLCaps& caps, GrGLTexture::Desc* desc,
669 bool skipRectTexSupportCheck = false) {
670 GrGLTextureInfo info;
671 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
672 return false;
673 }
674
675 desc->fSize = {backendTex.width(), backendTex.height()};
676 desc->fTarget = info.fTarget;
677 desc->fID = info.fID;
678 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
679
680 if (desc->fFormat == GrGLFormat::kUnknown) {
681 return false;
682 }
683
684 if (GR_GL_TEXTURE_2D != desc->fTarget) {
685 return false;
686 }
687 if (backendTex.isProtected()) {
688 // Not supported in GL backend at this time.
689 return false;
690 }
691
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500692 return true;
693}
694
Robert Phillipsb915c942019-12-17 14:44:37 -0500695sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
696 GrWrapOwnership ownership,
697 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500698 GrGLTexture::Desc desc;
699 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
700 return nullptr;
701 }
702
703 if (kBorrow_GrWrapOwnership == ownership) {
704 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
705 } else {
706 desc.fOwnership = GrBackendObjectOwnership::kOwned;
707 }
708
709 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
710 : GrMipMapsStatus::kNotAllocated;
711
712 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
713 backendTex.getGLTextureParams(), cacheable,
714 kRead_GrIOType);
715 // We don't know what parameters are already set on wrapped textures.
716 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600717 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500718}
719
Brian Salomond17f6582017-07-19 18:28:58 -0400720sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400721 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500722 GrWrapOwnership ownership,
723 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400724 const GrGLCaps& caps = this->glCaps();
725
Brian Salomonea4ad302019-08-07 13:04:55 -0400726 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400727 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800728 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800729 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400730 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
731 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800732
Brian Salomond17f6582017-07-19 18:28:58 -0400733 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400734 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800735 return nullptr;
736 }
bsalomon10528f12015-10-14 12:54:52 -0700737
Brian Osman766fcbb2017-03-13 09:33:09 -0400738 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400740 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400741 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800742 }
bsalomonb15b4c12014-10-29 12:41:57 -0700743
Robert Phillips0902c982019-07-16 07:47:56 -0400744
Greg Daniel6fa62e22019-08-07 15:52:37 -0400745 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
746 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000747
Brian Salomonea4ad302019-08-07 13:04:55 -0400748 GrGLRenderTarget::IDs rtIDs;
749 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400750 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000751 }
Greg Daniel177e6952017-10-12 12:27:11 -0400752
753 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
754 : GrMipMapsStatus::kNotAllocated;
755
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500756 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400757 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400758 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400759 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400760 // We don't know what parameters are already set on wrapped textures.
761 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600762 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000763}
764
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400765sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400766 GrGLFramebufferInfo info;
767 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000768 return nullptr;
769 }
770
Brian Salomone8a766b2019-07-19 14:24:36 -0400771 if (backendRT.isProtected()) {
772 // Not supported in GL at this time.
773 return nullptr;
774 }
775
Brian Salomond4764a12019-08-08 12:08:24 -0400776 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400777 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
778 return nullptr;
779 }
780
Brian Salomonea4ad302019-08-07 13:04:55 -0400781 GrGLRenderTarget::IDs rtIDs;
782 rtIDs.fRTFBOID = info.fFBOID;
783 rtIDs.fMSColorRenderbufferID = 0;
784 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
785 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000786
Greg Daniel6fa62e22019-08-07 15:52:37 -0400787 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700788
Brian Salomona56a7462020-02-07 14:17:25 -0500789 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400790 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000791}
792
Greg Daniel7ef28f32017-04-20 16:41:55 +0000793sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400794 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400795 GrGLTexture::Desc desc;
796 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
797 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400798 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800799 return nullptr;
800 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400801
802 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
803 return nullptr;
804 }
805
806 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400807 GrGLRenderTarget::IDs rtIDs;
808 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800809 return nullptr;
810 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500811 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800812}
813
Brian Salomonc320b152018-02-20 14:05:36 -0500814static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700815 if (!glTex) {
816 return false;
817 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000818
bsalomone5286e02016-01-14 09:24:09 -0800819 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
820 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800821 return false;
822 }
823
jvanverth17aa0472016-01-05 10:41:27 -0800824 return true;
825}
826
Brian Salomona9b04b92018-06-01 15:04:28 -0400827bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400828 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400829 const GrMipLevel texels[], int mipLevelCount,
830 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500831 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800832
Brian Salomonc320b152018-02-20 14:05:36 -0500833 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800834 return false;
835 }
836
Brian Salomond978b902019-02-07 15:09:18 -0500837 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000838
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400839 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400840 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
841 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
842 glTex->target(), dstRect, srcColorType, texels,
843 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000844}
845
Brian Salomone05ba5a2019-04-08 11:59:07 -0400846bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400847 GrColorType textureColorType, GrColorType bufferColorType,
848 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400849 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400850
Jim Van Verth1676cb92019-01-15 13:24:45 -0500851 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400852 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500853
Brian Salomonc320b152018-02-20 14:05:36 -0500854 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400855 return false;
856 }
857
Hal Canaryc36f7e72018-06-18 15:50:09 -0400858 static_assert(sizeof(int) == sizeof(int32_t), "");
859 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400860 return false;
861 }
862
Brian Salomond978b902019-02-07 15:09:18 -0500863 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400864
865 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500866 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400867 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500868 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400869
Greg Daniel660cc992017-06-26 14:55:05 -0400870 SkDEBUGCODE(
871 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
872 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
873 SkASSERT(bounds.contains(subRect));
874 )
875
Brian Salomonb28cb682019-07-26 12:48:47 -0400876 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400877 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400878 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700879 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400880 return false;
881 }
882
883 bool restoreGLRowLength = false;
884 if (trimRowBytes != rowBytes) {
885 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400886 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400887 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
888 restoreGLRowLength = true;
889 }
890
Greg Danielba88ab62019-07-26 09:14:01 -0400891 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400892 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400893 GrGLenum externalFormat = 0;
894 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400895 this->glCaps().getTexSubImageExternalFormatAndType(
896 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400897 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400898 return false;
899 }
900
Brian Salomon8cbf6622019-07-30 11:03:14 -0400901 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400902 GL_CALL(TexSubImage2D(glTex->target(),
903 0,
904 left, top,
905 width,
906 height,
907 externalFormat, externalType,
908 pixels));
909
910 if (restoreGLRowLength) {
911 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
912 }
913
914 return true;
915}
916
Brian Salomon26de56e2019-04-10 12:14:26 -0400917bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400918 GrColorType surfaceColorType, GrColorType dstColorType,
919 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400920 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
921 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400922 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400923 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
924 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400925}
926
Brian Osman9b560242017-09-05 15:34:52 -0400927void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500928 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
929 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
930 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
931 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400932 }
Brian Osman9b560242017-09-05 15:34:52 -0400933}
934
Brian Salomon22558932020-05-15 14:46:34 -0400935bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
936 GrColorType textureColorType,
937 SkISize texDims,
938 GrGLenum target,
939 SkIRect dstRect,
940 GrColorType srcColorType,
941 const GrMipLevel texels[],
942 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500943 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400944 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500945
Greg Daniel7bfc9132019-08-14 14:23:53 -0400946 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800947
Brian Salomonf77c1462019-08-01 15:19:29 -0400948 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800949
bsalomon5b30c6f2015-12-17 14:17:34 -0800950 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800951 GrGLenum externalFormat;
952 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400953 this->glCaps().getTexSubImageExternalFormatAndType(
954 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400955 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800956 return false;
957 }
Brian Salomon22558932020-05-15 14:46:34 -0400958 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
959 mipLevelCount);
960 return true;
961}
Brian Salomonf77c1462019-08-01 15:19:29 -0400962
Brian Salomon22558932020-05-15 14:46:34 -0400963bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
964 SkISize texDims,
965 GrGLenum target,
966 SkColor4f color,
967 uint32_t levelMask) {
968 GrColorType colorType;
969 GrGLenum externalFormat, externalType;
970 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
971 &externalType, &colorType);
972 if (colorType == GrColorType::kUnknown) {
973 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000974 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400975
Brian Salomon22558932020-05-15 14:46:34 -0400976 std::unique_ptr<char[]> pixelStorage;
977 size_t bpp = 0;
978 int numLevels = SkMipMap::ComputeLevelCount(texDims) + 1;
979 SkSTArray<16, GrMipLevel> levels;
980 levels.resize(numLevels);
981 SkISize levelDims = texDims;
982 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
983 std::max(levelDims.height() >> 1, 1)}) {
984 if (levelMask & (1 << i)) {
985 if (!pixelStorage) {
986 // Make one tight image at the first size and reuse it for smaller levels.
987 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
988 size_t rb = ii.minRowBytes();
989 pixelStorage.reset(new char[rb * levelDims.height()]);
990 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
991 return false;
992 }
993 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000994 }
Brian Salomon22558932020-05-15 14:46:34 -0400995 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
996 }
997 }
998 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
999 bpp, levels.begin(), levels.count());
1000 return true;
1001}
1002
1003void GrGLGpu::uploadTexData(SkISize texDims,
1004 GrGLenum target,
1005 SkIRect dstRect,
1006 GrGLenum externalFormat,
1007 GrGLenum externalType,
1008 size_t bpp,
1009 const GrMipLevel texels[],
1010 int mipLevelCount) {
1011 SkASSERT(!texDims.isEmpty());
1012 SkASSERT(!dstRect.isEmpty());
1013 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
1014 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipMap::ComputeLevelCount(texDims) + 1);
1015 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1016
1017 const GrGLCaps& caps = this->glCaps();
1018
1019 bool restoreGLRowLength = false;
1020
1021 this->unbindCpuToGpuXferBuffer();
1022 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1023
1024 SkISize dims = dstRect.size();
1025 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1026 std::max(dims.height() >> 1, 1)}) {
1027 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001028 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001029 }
Brian Salomon22558932020-05-15 14:46:34 -04001030 const size_t trimRowBytes = dims.width() * bpp;
1031 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001032
1033 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1034 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001035 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001036 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001037 } else {
1038 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001039 }
1040
Brian Salomon22558932020-05-15 14:46:34 -04001041 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1042 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001043 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001044 if (restoreGLRowLength) {
1045 SkASSERT(caps.writePixelsRowBytesSupport());
1046 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1047 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001048}
1049
Greg Daniel01f278c2020-06-12 16:58:17 -04001050bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1051 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001052 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001053 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001054 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001055 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001056 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001057 const GrGLCaps& caps = this->glCaps();
1058
1059 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001060 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001061 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001062 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001063 }
1064
Robert Phillips9f744f72019-12-19 19:14:33 -05001065 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1066
Greg Daniel7bfc9132019-08-14 14:23:53 -04001067 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001068
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001069 int numMipLevels = 1;
1070 if (mipMapped == GrMipMapped::kYes) {
1071 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1072 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001074 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001075 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001076
1077 if (useTexStorage) {
1078 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001079 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1080 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001081 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001082 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001083 }
Robert Phillips42716d42019-12-16 12:19:54 -05001084
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001085 size_t offset = 0;
1086 for (int level = 0; level < numMipLevels; ++level) {
1087
Robert Phillips99dead92020-01-27 16:11:57 -05001088 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1089 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001090
Brian Salomond8575452020-02-25 12:13:29 -05001091 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1092 level,
1093 0, // left
1094 0, // top
1095 dimensions.width(),
1096 dimensions.height(),
1097 internalFormat,
1098 SkToInt(levelDataSize),
1099 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001100
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001101 if (error != GR_GL_NO_ERROR) {
1102 return false;
1103 }
1104
Robert Phillips9f744f72019-12-19 19:14:33 -05001105 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001106 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001107 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001108 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001109 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001110
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001111 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001112 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1113 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001114
1115 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001116 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1117 level,
1118 internalFormat,
1119 dimensions.width(),
1120 dimensions.height(),
1121 0, // border
1122 SkToInt(levelDataSize),
1123 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001124
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001125 if (error != GR_GL_NO_ERROR) {
1126 return false;
1127 }
1128
Robert Phillips9f744f72019-12-19 19:14:33 -05001129 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001130 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001131 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001132 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001133 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001134}
1135
Brian Salomond8575452020-02-25 12:13:29 -05001136bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001137 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001138 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001139 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001140 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001141 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001142 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1143 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001144 break;
1145 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001146 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1147 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001148 break;
1149 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1150 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001151 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1152 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001153 break;
1154 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001155 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001156 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001157 }
Brian Salomond8575452020-02-25 12:13:29 -05001158 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001159}
1160
Brian Salomonea4ad302019-08-07 13:04:55 -04001161bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001162 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001163 GrGLRenderTarget::IDs* rtIDs) {
1164 rtIDs->fMSColorRenderbufferID = 0;
1165 rtIDs->fRTFBOID = 0;
1166 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1167 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168
bsalomona11e5fc2015-12-18 07:59:41 -08001169 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001170
Brian Salomonea4ad302019-08-07 13:04:55 -04001171 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001172 goto FAILED;
1173 }
1174
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001175 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001176 goto FAILED;
1177 }
1178
Brian Salomonea4ad302019-08-07 13:04:55 -04001179 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1180 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181 goto FAILED;
1182 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001183
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001184 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1185 // the texture bound to the other. The exception is the IMG multisample extension. With this
1186 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1187 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001188 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001189 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1190 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1191 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 goto FAILED;
1193 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001194 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001196 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 }
1198
egdanield803f272015-03-18 13:01:52 -07001199 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001200 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001201 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001202 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001203 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001204 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1205 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 goto FAILED;
1207 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001210 GR_GL_COLOR_ATTACHMENT0,
1211 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001212 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001214 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001216 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1218 GR_GL_COLOR_ATTACHMENT0,
1219 desc.fTarget,
1220 desc.fID,
1221 0,
1222 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001223 } else {
egdanield803f272015-03-18 13:01:52 -07001224 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001225 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001226 desc.fTarget,
1227 desc.fID,
1228 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001229 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230
1231 return true;
1232
1233FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001234 if (rtIDs->fMSColorRenderbufferID) {
1235 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001237 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1238 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001240 if (rtIDs->fTexFBOID) {
1241 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 }
1243 return false;
1244}
1245
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001246// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001247static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001248// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001249 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001250}
1251
Brian Salomone2826ab2019-06-04 15:58:31 -04001252static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001254 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1255 // drivers have a bug where an FBO won't be complete if it includes a
1256 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001257 GrGLTextureParameters::SamplerOverriddenState state;
1258 state.fMinFilter = GR_GL_NEAREST;
1259 state.fMagFilter = GR_GL_NEAREST;
1260 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1261 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001262 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1263 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1264 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1265 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001266 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001267}
1268
Brian Salomona56a7462020-02-07 14:17:25 -05001269sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001270 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001271 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001272 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001273 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001274 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001275 int mipLevelCount,
1276 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001277 // We don't support protected textures in GL.
1278 if (isProtected == GrProtected::kYes) {
1279 return nullptr;
1280 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001281 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001282
Brian Salomond2a8ae22019-09-10 16:03:59 -04001283 SkASSERT(mipLevelCount > 0);
1284 GrMipMapsStatus mipMapsStatus =
1285 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001286 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001287 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001288 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001289 switch (format.textureType()) {
1290 case GrTextureType::kExternal:
1291 case GrTextureType::kNone:
1292 return nullptr;
1293 case GrTextureType::k2D:
1294 texDesc.fTarget = GR_GL_TEXTURE_2D;
1295 break;
1296 case GrTextureType::kRectangle:
1297 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1298 return nullptr;
1299 }
1300 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1301 break;
1302 }
Brian Salomon81536f22019-08-08 16:30:49 -04001303 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001304 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001305 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1306 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001307
Brian Salomon0f396992020-06-19 19:51:21 -04001308 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1309 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001310
1311 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001312 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001313 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001314
Robert Phillips67d52cf2017-06-05 13:38:13 -04001315 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001316 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001317 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001318 GL_CALL(BindTexture(texDesc.fTarget, 0));
1319 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001320
Brian Salomonea4ad302019-08-07 13:04:55 -04001321 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1322 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 return return_null_texture();
1324 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001325 tex = sk_make_sp<GrGLTextureRenderTarget>(
1326 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001327 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001328 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001329 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001331 // The non-sampler params are still at their default values.
1332 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1333 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001334 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001335 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001336 GrGLenum externalFormat, externalType;
1337 GrColorType colorType;
1338 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1339 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001340 for (int i = 0; i < mipLevelCount; ++i) {
1341 if (levelClearMask & (1U << i)) {
1342 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1343 nullptr));
1344 }
1345 }
1346 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1347 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001348 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001349 this->disableWindowRectangles();
1350 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001351 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001352 for (int i = 0; i < mipLevelCount; ++i) {
1353 if (levelClearMask & (1U << i)) {
1354 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1355 kDst_TempFBOTarget);
1356 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1357 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1358 }
1359 }
Brian Salomonc2249852019-09-16 11:08:50 -04001360 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001361 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001362 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001363 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001364 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001365 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001366 }
1367 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001368 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001369}
1370
Robert Phillips9f744f72019-12-19 19:14:33 -05001371sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001372 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001373 SkBudgeted budgeted,
1374 GrMipMapped mipMapped,
1375 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001376 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001377 // We don't support protected textures in GL.
1378 if (isProtected == GrProtected::kYes) {
1379 return nullptr;
1380 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001381 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1382
Brian Salomonbb8dde82019-06-27 10:52:13 -04001383 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001384 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001385 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001386 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001387 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001388 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001389 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001390 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001391 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001392 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001393 return nullptr;
1394 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001395
Robert Phillipsee946932019-12-18 11:16:17 -05001396 // Unbind this texture from the scratch texture unit.
1397 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1398
Robert Phillipse4720c62020-01-14 14:33:24 -05001399 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1400 ? GrMipMapsStatus::kValid
1401 : GrMipMapsStatus::kNotAllocated;
1402
1403 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001404 // The non-sampler params are still at their default values.
1405 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1406 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001407 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001408}
1409
Greg Danielc1ad77c2020-05-06 11:40:03 -04001410GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
1411 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
1412 GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback,
1413 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001414 // We don't support protected textures in GL.
1415 if (isProtected == GrProtected::kYes) {
1416 return {};
1417 }
1418
1419 this->handleDirtyContext();
1420
1421 GrGLFormat glFormat = format.asGLFormat();
1422 if (glFormat == GrGLFormat::kUnknown) {
1423 return {};
1424 }
1425
Greg Daniel01f278c2020-06-12 16:58:17 -04001426 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1427
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001428 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001429 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001430 SkAutoMalloc am;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001431 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1432 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1433 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001434 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001435 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001436 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001437
Robert Phillips99dead92020-01-27 16:11:57 -05001438 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1439 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001440
1441 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001442
1443 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1444
1445 rawData = (const char*) am.get();
1446 }
1447
1448 GrGLTextureInfo info;
1449 GrGLTextureParameters::SamplerOverriddenState initialState;
1450
1451 info.fTarget = GR_GL_TEXTURE_2D;
1452 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001453 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001454 mipMapped, &initialState,
1455 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001456 if (!info.fID) {
1457 return {};
1458 }
1459
1460 // Unbind this texture from the scratch texture unit.
1461 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1462
1463 auto parameters = sk_make_sp<GrGLTextureParameters>();
1464 // The non-sampler params are still at their default values.
1465 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1466 fResetTimestampForTextureParameters);
1467
1468 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1469 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001470}
1471
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001472namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001473
egdaniel8dc7c3a2015-04-16 11:22:42 -07001474const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001475
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001476void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001477 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001478
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001479 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001480 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001481 (kUnknownBitCount == format->fTotalBits));
1482 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001483 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001484 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1485 (GrGLint*)&format->fStencilBits);
1486 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001487 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001488 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1489 (GrGLint*)&format->fTotalBits);
1490 format->fTotalBits += format->fStencilBits;
1491 } else {
1492 format->fTotalBits = format->fStencilBits;
1493 }
1494 }
1495}
1496}
1497
Brian Salomon5043f1f2019-07-11 21:27:54 -04001498int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001499 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001500 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1501
1502 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001503 // Default to unsupported, set this if we find a stencil format that works.
1504 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001505
Brian Salomon0f396992020-06-19 19:51:21 -04001506 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1507 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001508 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001509 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001510 }
egdanielff1d5472015-09-10 08:37:20 -07001511 // unbind the texture from the texture unit before binding it to the frame buffer
1512 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1513
1514 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001515 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001516 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001517 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001518 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001519 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1520 GR_GL_COLOR_ATTACHMENT0,
1521 GR_GL_TEXTURE_2D,
1522 colorID,
1523 0));
bsalomon30447372015-12-21 09:03:05 -08001524 GrGLuint sbRBID = 0;
1525 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001526
1527 // look over formats till I find a compatible one
1528 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001529 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001530 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001531 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1532 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001533 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1534 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1535 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001536 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001537 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001538 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001539 if (sFmt.fPacked) {
1540 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1541 GR_GL_DEPTH_ATTACHMENT,
1542 GR_GL_RENDERBUFFER, sbRBID));
1543 } else {
1544 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1545 GR_GL_DEPTH_ATTACHMENT,
1546 GR_GL_RENDERBUFFER, 0));
1547 }
1548 GrGLenum status;
1549 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1550 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1551 firstWorkingStencilFormatIndex = i;
1552 break;
1553 }
egdanielff1d5472015-09-10 08:37:20 -07001554 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1555 GR_GL_STENCIL_ATTACHMENT,
1556 GR_GL_RENDERBUFFER, 0));
1557 if (sFmt.fPacked) {
1558 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1559 GR_GL_DEPTH_ATTACHMENT,
1560 GR_GL_RENDERBUFFER, 0));
1561 }
egdanielff1d5472015-09-10 08:37:20 -07001562 }
1563 }
bsalomon30447372015-12-21 09:03:05 -08001564 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001565 }
1566 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001567 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1568 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001569 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001570 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001571 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001572}
1573
Brian Salomonea4ad302019-08-07 13:04:55 -04001574GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001575 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001576 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001577 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001578 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001579 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001580 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001581 if (format == GrGLFormat::kUnknown) {
1582 return 0;
1583 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001584 GrGLuint id = 0;
1585 GL_CALL(GenTextures(1, &id));
1586 if (!id) {
1587 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001588 }
1589
Brian Salomonea4ad302019-08-07 13:04:55 -04001590 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001591
Brian Salomonea4ad302019-08-07 13:04:55 -04001592 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1593
Robert Phillips42716d42019-12-16 12:19:54 -05001594 if (data) {
Greg Daniel01f278c2020-06-12 16:58:17 -04001595 if (!this->uploadCompressedTexData(compression, format, dimensions, mipMapped,
Robert Phillips9f744f72019-12-19 19:14:33 -05001596 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001597 GL_CALL(DeleteTextures(1, &id));
1598 return 0;
1599 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001600 }
Robert Phillips42716d42019-12-16 12:19:54 -05001601
Brian Salomonea4ad302019-08-07 13:04:55 -04001602 return id;
1603}
1604
Brian Salomon0f396992020-06-19 19:51:21 -04001605GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1606 GrGLFormat format,
1607 GrGLenum target,
1608 GrRenderable renderable,
1609 GrGLTextureParameters::SamplerOverriddenState* initialState,
1610 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001611 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001612 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001613
Brian Salomonea4ad302019-08-07 13:04:55 -04001614 GrGLuint id = 0;
1615 GL_CALL(GenTextures(1, &id));
1616
1617 if (!id) {
1618 return 0;
1619 }
1620
Brian Salomon0f396992020-06-19 19:51:21 -04001621 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001622
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001623 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001624 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001625 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001626 }
1627
Brian Salomond2a8ae22019-09-10 16:03:59 -04001628 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001629 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001630 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001631 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001632 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001633
1634 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1635
1636 bool success = false;
1637 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001638 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001639 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001640 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1641 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001642 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001643 } else {
1644 GrGLenum externalFormat, externalType;
1645 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1646 GrGLenum error = GR_GL_NO_ERROR;
1647 if (externalFormat && externalType) {
1648 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1649 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001650 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1651 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001652 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1653 currentHeight, 0, externalFormat, externalType,
1654 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001655 }
Brian Salomond8575452020-02-25 12:13:29 -05001656 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001657 }
1658 }
1659 }
1660 if (success) {
1661 return id;
1662 }
1663 GL_CALL(DeleteTextures(1, &id));
1664 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001665}
1666
Chris Daltoneffee202019-07-01 22:28:03 -06001667GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1668 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001669 SkASSERT(width >= rt->width());
1670 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001671
egdaniel8dc7c3a2015-04-16 11:22:42 -07001672 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001673
Brian Salomond4764a12019-08-08 12:08:24 -04001674 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001675 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001676 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001677 }
egdanielff1d5472015-09-10 08:37:20 -07001678
1679 if (!sbDesc.fRenderbufferID) {
1680 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1681 }
1682 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001683 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001684 }
1685 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1686 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001687 // we do this "if" so that we don't call the multisample
1688 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001689 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001690 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1691 width, height)) {
1692 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1693 return nullptr;
1694 }
egdanielff1d5472015-09-10 08:37:20 -07001695 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001696 GrGLenum error = GL_ALLOC_CALL(
1697 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1698 if (error != GR_GL_NO_ERROR) {
1699 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1700 return nullptr;
1701 }
egdanielff1d5472015-09-10 08:37:20 -07001702 }
1703 fStats.incStencilAttachmentCreates();
1704 // After sized formats we attempt an unsized format and take
1705 // whatever sizes GL gives us. In that case we query for the size.
1706 GrGLStencilAttachment::Format format = sFmt;
1707 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001708 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1709 sbDesc,
1710 width,
1711 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001712 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001713 format);
1714 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001715}
1716
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001717////////////////////////////////////////////////////////////////////////////////
1718
Brian Salomondbf70722019-02-07 11:31:24 -05001719sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1720 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001721 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001722}
1723
Chris Dalton2e7ed262020-02-21 15:17:59 -07001724void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1725 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001726 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1727 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1728 fHWScissorSettings.fEnabled = kYes_TriState;
1729 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001730 } else {
1731 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1732 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1733 fHWScissorSettings.fEnabled = kNo_TriState;
1734 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001736}
Brian Salomond818ebf2018-07-02 14:08:49 +00001737
Chris Dalton2e7ed262020-02-21 15:17:59 -07001738void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1739 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001740 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001741 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1742 if (fHWScissorSettings.fRect != nativeScissor) {
1743 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1744 nativeScissor.fHeight));
1745 fHWScissorSettings.fRect = nativeScissor;
1746 }
joshualitt77b13072014-10-27 14:51:01 -07001747}
1748
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001749void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001750 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001751#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001752 typedef GrWindowRectsState::Mode Mode;
1753 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1754 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001755
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001756 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001757 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001758 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001759 }
1760
csmartdalton7535f412016-08-23 06:51:00 -07001761 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1762 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001763 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001764 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001765
Chris Daltond6cda8d2019-09-05 02:30:04 -06001766 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001767 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001768 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001769 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001770 }
1771
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001772 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001773 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001774
Greg Danielacd66b42019-05-22 16:29:12 -04001775 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001776#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001777}
1778
1779void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001780#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001781 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001782 return;
1783 }
1784 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001785 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001786#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001787}
1788
Robert Phillipsdf546db2020-05-29 09:42:54 -04001789bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001790 this->handleDirtyContext();
1791
Chris Daltond42d2002020-02-28 12:05:04 -07001792 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001793 if (!program) {
1794 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1795 return false;
1796 }
1797
1798 this->flushProgram(std::move(program));
1799
Chris Daltond42d2002020-02-28 12:05:04 -07001800 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1801 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1802 }
1803
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001804 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001805 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001806 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001807
Chris Dalton20e7a532020-02-28 15:37:53 +00001808 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001809
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001810 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001811 GrStencilSettings stencil;
1812 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001813 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1814 stencil.reset(*programInfo.pipeline().getUserStencil(),
1815 programInfo.pipeline().hasStencilClip(),
1816 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001817 }
Robert Phillips901aff02019-10-08 12:32:56 -04001818 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001819 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001820 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1821 glRT, programInfo.origin());
1822 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001823 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001824 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001825
1826 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001827 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001828 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001829
Chris Dalton20e7a532020-02-28 15:37:53 +00001830 return true;
1831}
1832
1833void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1834 if (!program) {
1835 fHWProgram.reset();
1836 fHWProgramID = 0;
1837 return;
1838 }
1839 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1840 if (program == fHWProgram) {
1841 return;
1842 }
1843 auto id = program->programID();
1844 SkASSERT(id);
1845 GL_CALL(UseProgram(id));
1846 fHWProgram = std::move(program);
1847 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001848}
1849
1850void GrGLGpu::flushProgram(GrGLuint id) {
1851 SkASSERT(id);
1852 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001853 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001854 return;
1855 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001856 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001857 GL_CALL(UseProgram(id));
1858 fHWProgramID = id;
1859}
1860
Brian Salomonae64c192019-02-05 09:41:37 -05001861GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001862 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001863
cdaltone2e71c22016-04-07 18:13:29 -07001864 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001865 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001866 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001867 }
cdaltone2e71c22016-04-07 18:13:29 -07001868
Brian Salomonae64c192019-02-05 09:41:37 -05001869 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001870 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001871 if (!bufferState->fBufferZeroKnownBound) {
1872 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1873 bufferState->fBufferZeroKnownBound = true;
1874 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001875 }
Brian Salomondbf70722019-02-07 11:31:24 -05001876 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1877 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001878 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1879 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1880 bufferState->fBufferZeroKnownBound = false;
1881 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001882 }
1883
Brian Salomonae64c192019-02-05 09:41:37 -05001884 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001885}
Brian Salomond818ebf2018-07-02 14:08:49 +00001886
Michael Ludwig1e632792020-05-21 12:45:31 -04001887void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001888 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001889 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001890 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001891 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001892 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001893
Brian Salomon43f8bf02017-10-18 08:33:29 -04001894 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001895
Brian Salomon43f8bf02017-10-18 08:33:29 -04001896 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1897
Michael Ludwig1e632792020-05-21 12:45:31 -04001898 if (scissor.enabled()) {
1899 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001900 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001901 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001902 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001903 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1904 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001905 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001906 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001907 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001908}
1909
Chris Daltonb50cc812019-10-14 16:29:21 -06001910static bool use_tiled_rendering(const GrGLCaps& glCaps,
1911 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1912 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1913 // Otherwise it's faster to just not use it.
1914 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1915 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1916}
1917
1918void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001919 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1920 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1921 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1922
1923 this->handleDirtyContext();
1924
1925 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1926 this->flushRenderTarget(glRT);
1927 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1928
Chris Daltonb50cc812019-10-14 16:29:21 -06001929 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1930 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1931 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1932 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1933 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1934 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1935 nativeBounds.fHeight, preserveMask));
1936 }
1937
Chris Dalton674f77a2019-09-30 20:49:39 -06001938 GrGLbitfield clearMask = 0;
1939 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1940 SkASSERT(!this->caps()->performColorClearsAsDraws());
1941 this->flushClearColor(colorLoadStore.fClearColor);
1942 this->flushColorWrite(true);
1943 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001944 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001945 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1946 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1947 GL_CALL(StencilMask(0xffffffff));
1948 GL_CALL(ClearStencil(0));
1949 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1950 }
1951 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001952 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001953 this->disableWindowRectangles();
1954 GL_CALL(Clear(clearMask));
1955 }
1956}
1957
1958void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1959 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1960 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1961 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1962
1963 this->handleDirtyContext();
1964
1965 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1966 // The framebuffer binding changed in the middle of a command buffer. We should have already
1967 // printed a warning during onFBOChanged.
1968 return;
1969 }
1970
1971 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1972 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1973
1974 SkSTArray<2, GrGLenum> discardAttachments;
1975 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1976 discardAttachments.push_back(
1977 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1978 }
1979 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1980 discardAttachments.push_back(
1981 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1982 }
1983
1984 if (!discardAttachments.empty()) {
1985 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1986 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1987 discardAttachments.begin()));
1988 } else {
1989 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1990 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1991 discardAttachments.begin()));
1992 }
1993 }
1994 }
1995
Chris Daltonb50cc812019-10-14 16:29:21 -06001996 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1997 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1998 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1999 // Handled by use_tiled_rendering().
2000 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2001 GL_CALL(EndTiling(preserveMask));
2002 }
2003
Chris Dalton674f77a2019-09-30 20:49:39 -06002004 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002005}
2006
Michael Ludwig1e632792020-05-21 12:45:31 -04002007void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002008 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002009 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002010 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002011 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002012 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002013
egdaniel8dc7c3a2015-04-16 11:22:42 -07002014 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002015 if (!sb) {
2016 // We should only get here if we marked a proxy as requiring a SB. However,
2017 // the SB creation could later fail. Likely clipping is going to go awry now.
2018 return;
2019 }
2020
bsalomon6bc1b5f2015-02-23 09:06:38 -08002021 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002022#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002023 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002024 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002025#else
2026 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002027 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002028 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002029 // changing the clip between stencil passes may or may not
2030 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002031 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002032#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002033 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002034 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002035 value = (1 << (stencilBitCount - 1));
2036 } else {
2037 value = 0;
2038 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002039 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002040 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002041
Michael Ludwig1e632792020-05-21 12:45:31 -04002042 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2043 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002044
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002045 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002046 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002047 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002048 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002049}
2050
Brian Salomone05ba5a2019-04-08 11:59:07 -04002051bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002052 GrColorType surfaceColorType, GrColorType dstColorType,
2053 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002054 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002055
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002056 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002057 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002058 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002059 return false;
2060 }
Greg Danielba88ab62019-07-26 09:14:01 -04002061 GrGLenum externalFormat = 0;
2062 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002063 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2064 surfaceColorType,
2065 dstColorType,
2066 &externalFormat,
2067 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002068 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002069 return false;
2070 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002071
Brian Salomon71d9d842016-11-03 13:42:00 -04002072 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002073 if (renderTarget->numSamples() <= 1 ||
2074 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2075 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2076 this->flushRenderTargetNoColorWrites(renderTarget);
2077 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2078 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2079 return false;
2080 } else {
2081 SkASSERT(renderTarget->requiresManualMSAAResolve());
2082 // we don't track the state of the READ FBO ID.
2083 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002084 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002085 } else {
2086 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002087 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002088 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002089 }
2090
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002091 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002092 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002093
Brian Salomona6948702018-06-01 15:33:20 -04002094 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002095 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002096 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002097 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002098 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002099 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002100
Brian Osman1348ed02018-09-12 09:08:39 -04002101 bool reattachStencil = false;
2102 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2103 renderTarget &&
2104 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002105 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002106 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2107 reattachStencil = true;
2108 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2109 GR_GL_RENDERBUFFER, 0));
2110 }
2111
Chris Dalton76500e52019-09-05 02:13:05 -06002112 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002113 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002114
2115 if (reattachStencil) {
2116 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2117 renderTarget->renderTargetPriv().getStencilAttachment());
2118 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2119 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2120 }
2121
Brian Salomon26de56e2019-04-10 12:14:26 -04002122 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002123 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002124 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2125 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002126
Brian Salomone05ba5a2019-04-08 11:59:07 -04002127 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002128 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002129 }
2130 return true;
2131}
2132
2133bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002134 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2135 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002136 SkASSERT(surface);
2137
Brian Salomonb28cb682019-07-26 12:48:47 -04002138 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002139
Brian Salomon26de56e2019-04-10 12:14:26 -04002140 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2141 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002142
Brian Salomon1047a492019-07-02 12:25:21 -04002143 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002144 rowPixelWidth = width;
2145 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002146 SkASSERT(!(rowBytes % bytesPerPixel));
2147 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002148 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002149 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2150 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002151}
2152
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002153GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002154 GrRenderTarget* rt, GrStencilAttachment*,
2155 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002156 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002157 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002158 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002159 if (!fCachedOpsRenderPass) {
2160 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002161 }
2162
Chris Daltonb50cc812019-10-14 16:29:21 -06002163 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002164 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002165}
2166
Brian Salomon1fabd512018-02-09 09:54:25 -05002167void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002168 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002169 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002170 this->didWriteToSurface(target, origin, &bounds);
2171}
bsalomon6ba6fa12015-03-04 11:57:37 -08002172
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002173void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2174 this->flushRenderTargetNoColorWrites(target);
2175 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002176}
2177
Brian Osman9aa30c62018-07-02 15:21:46 -04002178void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002179 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002180 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002181 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002182 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002183#ifdef SK_DEBUG
2184 // don't do this check in Chromium -- this is causing
2185 // lots of repeated command buffer flushes when the compositor is
2186 // rendering with Ganesh, which is really slow; even too slow for
2187 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002188 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002189 GrGLenum status;
2190 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2191 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2192 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2193 }
bsalomon160f24c2015-03-17 15:55:42 -07002194 }
egdanield803f272015-03-18 13:01:52 -07002195#endif
2196 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002197 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002198 }
2199
brianosman35b784d2016-05-05 11:52:53 -07002200 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002201 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002202 }
Brian Salomon9b553272020-01-24 14:38:37 -05002203
2204 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2205 GrGLint format;
2206 GrGLint type;
2207 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2208 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2209 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2210 }
bsalomon083617b2016-02-12 12:10:14 -08002211}
bsalomona9909122016-01-23 10:41:40 -08002212
brianosman33f6b3f2016-06-02 05:49:21 -07002213void GrGLGpu::flushFramebufferSRGB(bool enable) {
2214 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2215 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2216 fHWSRGBFramebuffer = kYes_TriState;
2217 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2218 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2219 fHWSRGBFramebuffer = kNo_TriState;
2220 }
2221}
2222
Greg Danielacd66b42019-05-22 16:29:12 -04002223void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002224 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002225 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002226 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002227 fHWViewport = viewport;
2228 }
2229}
2230
Chris Dalton33db9fc2020-02-25 18:52:12 -07002231GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002232 fStats.incNumDraws();
2233
Chris Dalton2e7ed262020-02-21 15:17:59 -07002234 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2235 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2236 GL_CALL(Enable(GR_GL_CULL_FACE));
2237 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002238 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002239 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002240
Chris Dalton3809bab2017-06-13 10:55:06 -06002241 switch (primitiveType) {
2242 case GrPrimitiveType::kTriangles:
2243 return GR_GL_TRIANGLES;
2244 case GrPrimitiveType::kTriangleStrip:
2245 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002246 case GrPrimitiveType::kPoints:
2247 return GR_GL_POINTS;
2248 case GrPrimitiveType::kLines:
2249 return GR_GL_LINES;
2250 case GrPrimitiveType::kLineStrip:
2251 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002252 case GrPrimitiveType::kPatches:
2253 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002254 case GrPrimitiveType::kPath:
2255 SK_ABORT("non-mesh-based GrPrimitiveType");
2256 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002257 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002258 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002259}
2260
Chris Dalton16a33c62019-09-24 22:19:17 -06002261void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002262 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002263 // Some extensions automatically resolves the texture when it is read.
2264 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2265
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002266 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002267 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2268 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2269 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2270 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002271
Chris Daltonc139d082019-09-26 14:04:24 -06002272 // make sure we go through flushRenderTarget() since we've modified
2273 // the bound DRAW FBO ID.
2274 fHWBoundRenderTargetUniqueID.makeInvalid();
2275 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2276 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002277 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2278 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002279 GrScissorState scissor(rt->dimensions());
2280 SkAssertResult(scissor.set(resolveRect));
2281 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002282 this->disableWindowRectangles();
2283 GL_CALL(ResolveMultisampleFramebuffer());
2284 } else {
2285 int l, b, r, t;
2286 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2287 this->glCaps().blitFramebufferSupportFlags()) {
2288 l = 0;
2289 b = 0;
2290 r = target->width();
2291 t = target->height();
2292 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002293 l = resolveRect.x();
2294 b = resolveRect.y();
2295 r = resolveRect.x() + resolveRect.width();
2296 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002297 }
Chris Daltonc139d082019-09-26 14:04:24 -06002298
2299 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002300 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002301 this->disableWindowRectangles();
2302 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 +00002303 }
2304}
2305
bsalomon@google.com411dad02012-06-05 20:24:20 +00002306namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002307
bsalomon@google.com411dad02012-06-05 20:24:20 +00002308
2309GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002310 static const GrGLenum gTable[kGrStencilOpCount] = {
2311 GR_GL_KEEP, // kKeep
2312 GR_GL_ZERO, // kZero
2313 GR_GL_REPLACE, // kReplace
2314 GR_GL_INVERT, // kInvert
2315 GR_GL_INCR_WRAP, // kIncWrap
2316 GR_GL_DECR_WRAP, // kDecWrap
2317 GR_GL_INCR, // kIncClamp
2318 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002319 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002320 static_assert(0 == (int)GrStencilOp::kKeep);
2321 static_assert(1 == (int)GrStencilOp::kZero);
2322 static_assert(2 == (int)GrStencilOp::kReplace);
2323 static_assert(3 == (int)GrStencilOp::kInvert);
2324 static_assert(4 == (int)GrStencilOp::kIncWrap);
2325 static_assert(5 == (int)GrStencilOp::kDecWrap);
2326 static_assert(6 == (int)GrStencilOp::kIncClamp);
2327 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002328 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2329 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002330}
2331
2332void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002333 const GrStencilSettings::Face& face,
2334 GrGLenum glFace) {
2335 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2336 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2337 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002338
cdalton93a379b2016-05-11 13:58:08 -07002339 GrGLint ref = face.fRef;
2340 GrGLint mask = face.fTestMask;
2341 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002342
2343 if (GR_GL_FRONT_AND_BACK == glFace) {
2344 // we call the combined func just in case separate stencil is not
2345 // supported.
2346 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2347 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002348 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002349 } else {
2350 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2351 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002352 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002353 }
2354}
2355}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002356
Chris Dalton71713f62019-04-18 12:41:03 -06002357void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002358 if (stencilSettings.isDisabled()) {
2359 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002360 } else if (fHWStencilSettings != stencilSettings ||
2361 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002362 if (kYes_TriState != fHWStencilTestEnabled) {
2363 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002364
csmartdaltonc7d85332016-10-26 10:13:46 -07002365 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002366 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002367 if (!stencilSettings.isTwoSided()) {
2368 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2369 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002370 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002371 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2372 GR_GL_FRONT);
2373 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2374 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002375 }
joshualitta58fe352014-10-27 08:39:00 -07002376 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002377 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002378 }
2379}
2380
csmartdaltonc7d85332016-10-26 10:13:46 -07002381void GrGLGpu::disableStencil() {
2382 if (kNo_TriState != fHWStencilTestEnabled) {
2383 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002384
csmartdaltonc7d85332016-10-26 10:13:46 -07002385 fHWStencilTestEnabled = kNo_TriState;
2386 fHWStencilSettings.invalidate();
2387 }
2388}
2389
Chris Dalton4c56b032019-03-04 12:28:42 -07002390void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002391 // rt is only optional if useHWAA is false.
2392 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002393#ifdef SK_DEBUG
2394 if (useHWAA && rt->numSamples() <= 1) {
2395 SkASSERT(this->caps()->mixedSamplesSupport());
2396 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2397 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2398 }
2399#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002400
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002401 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002402 if (useHWAA) {
2403 if (kYes_TriState != fMSAAEnabled) {
2404 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2405 fMSAAEnabled = kYes_TriState;
2406 }
2407 } else {
2408 if (kNo_TriState != fMSAAEnabled) {
2409 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2410 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002411 }
2412 }
2413 }
2414}
2415
Chris Daltonce425af2019-12-16 10:39:03 -07002416void GrGLGpu::flushConservativeRasterState(bool enabled) {
2417 if (this->caps()->conservativeRasterSupport()) {
2418 if (enabled) {
2419 if (kYes_TriState != fHWConservativeRasterEnabled) {
2420 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2421 fHWConservativeRasterEnabled = kYes_TriState;
2422 }
2423 } else {
2424 if (kNo_TriState != fHWConservativeRasterEnabled) {
2425 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2426 fHWConservativeRasterEnabled = kNo_TriState;
2427 }
2428 }
2429 }
2430}
2431
Chris Dalton1215cda2019-12-17 21:44:04 -07002432void GrGLGpu::flushWireframeState(bool enabled) {
2433 if (this->caps()->wireframeSupport()) {
2434 if (this->caps()->wireframeMode() || enabled) {
2435 if (kYes_TriState != fHWWireframeEnabled) {
2436 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2437 fHWWireframeEnabled = kYes_TriState;
2438 }
2439 } else {
2440 if (kNo_TriState != fHWWireframeEnabled) {
2441 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2442 fHWWireframeEnabled = kNo_TriState;
2443 }
2444 }
2445 }
2446}
2447
Chris Daltonbbb3f642019-07-24 12:25:08 -04002448void GrGLGpu::flushBlendAndColorWrite(
2449 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2450 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2451 // We need to work around a driver bug by using a blend state that preserves the dst color,
2452 // rather than disabling color writes.
2453 GrXferProcessor::BlendInfo preserveDstBlend;
2454 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2455 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2456 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2457 return;
2458 }
bsalomonf7cc8772015-05-11 11:21:14 -07002459
cdalton8917d622015-05-06 13:40:21 -07002460 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002461 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2462 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002463
2464 // Any optimization to disable blending should have already been applied and
2465 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002466 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2467 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002468
egdanielb414f252014-07-29 13:15:47 -07002469 if (blendOff) {
2470 if (kNo_TriState != fHWBlendState.fEnabled) {
2471 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002472
2473 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2474 // https://code.google.com/p/skia/issues/detail?id=3943
2475 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2476 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2477 SkASSERT(this->caps()->advancedBlendEquationSupport());
2478 // Set to any basic blending equation.
2479 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2480 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2481 fHWBlendState.fEquation = blend_equation;
2482 }
2483
egdanielb414f252014-07-29 13:15:47 -07002484 fHWBlendState.fEnabled = kNo_TriState;
2485 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002486 } else {
2487 if (kYes_TriState != fHWBlendState.fEnabled) {
2488 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002489
Chris Daltonbbb3f642019-07-24 12:25:08 -04002490 fHWBlendState.fEnabled = kYes_TriState;
2491 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002492
Chris Daltonbbb3f642019-07-24 12:25:08 -04002493 if (fHWBlendState.fEquation != equation) {
2494 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2495 fHWBlendState.fEquation = equation;
2496 }
cdalton8917d622015-05-06 13:40:21 -07002497
Chris Daltonbbb3f642019-07-24 12:25:08 -04002498 if (GrBlendEquationIsAdvanced(equation)) {
2499 SkASSERT(this->caps()->advancedBlendEquationSupport());
2500 // Advanced equations have no other blend state.
2501 return;
2502 }
cdalton8917d622015-05-06 13:40:21 -07002503
Chris Daltonbbb3f642019-07-24 12:25:08 -04002504 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2505 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2506 gXfermodeCoeff2Blend[dstCoeff]));
2507 fHWBlendState.fSrcCoeff = srcCoeff;
2508 fHWBlendState.fDstCoeff = dstCoeff;
2509 }
cdalton8917d622015-05-06 13:40:21 -07002510
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002511 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002512 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2513 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2514 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2515 fHWBlendState.fConstColor = blendConst;
2516 fHWBlendState.fConstColorValid = true;
2517 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002518 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002519 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002520
2521 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002522}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002523
Brian Salomondc829942018-10-23 16:07:24 -04002524static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002525 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002526 switch (swizzle[i]) {
2527 case 'r': glValues[i] = GR_GL_RED; break;
2528 case 'g': glValues[i] = GR_GL_GREEN; break;
2529 case 'b': glValues[i] = GR_GL_BLUE; break;
2530 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002531 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002532 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002533 default: SK_ABORT("Unsupported component");
2534 }
Brian Salomon327955b2018-10-22 15:39:22 +00002535 }
2536}
2537
Greg Daniel2c3398d2019-06-19 11:58:01 -04002538void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2539 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002540 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002541
reed856e9d92015-09-30 12:21:45 -07002542#ifdef SK_DEBUG
2543 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002544 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002545 const int w = texture->width();
2546 const int h = texture->height();
2547 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2548 }
2549 }
2550#endif
2551
Robert Phillips294870f2016-11-11 12:38:40 -05002552 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002553 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002554 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002555 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002556 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002557 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002558 }
2559
Brian Salomondc829942018-10-23 16:07:24 -04002560 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002561 if (!this->caps()->mipMapSupport() ||
2562 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002563 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002564 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002565 }
bsalomonefd7d452014-10-23 14:17:46 -07002566
brianosman33f6b3f2016-06-02 05:49:21 -07002567#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002568 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002569 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002570 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002571 }
2572#endif
2573
Brian Salomone2826ab2019-06-04 15:58:31 -04002574 auto timestamp = texture->parameters()->resetTimestamp();
2575 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002576
Brian Salomone2826ab2019-06-04 15:58:31 -04002577 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2578 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002579 if (fSamplerObjectCache) {
2580 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Brian Salomon1908bb82020-05-13 12:22:54 -04002581 if (this->glCaps().mustSetTexParameterMinFilterToEnableMipMapping()) {
2582 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
2583 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2584 texture->parameters()->samplerOverriddenState();
2585 if (setAll || oldSamplerState.fMinFilter != GR_GL_LINEAR_MIPMAP_LINEAR) {
2586 this->setTextureUnit(unitIdx);
2587 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER,
2588 GR_GL_LINEAR_MIPMAP_LINEAR));
2589 newSamplerState = oldSamplerState;
2590 newSamplerState.fMinFilter = GR_GL_LINEAR_MIPMAP_LINEAR;
2591 samplerStateToRecord = &newSamplerState;
2592 }
2593 }
2594 }
Brian Salomondc829942018-10-23 16:07:24 -04002595 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002596 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2597 texture->parameters()->samplerOverriddenState();
2598 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002599
Brian Salomone2826ab2019-06-04 15:58:31 -04002600 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2601 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002602
Brian Salomone2826ab2019-06-04 15:58:31 -04002603 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2604 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002605
2606 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002607 newSamplerState.fMinLOD = -1000.f;
2608 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002609
Brian Salomone2826ab2019-06-04 15:58:31 -04002610 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002611 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002612 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002613 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002614 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002615 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002616 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002617 }
Mike Klein1bccae52018-10-19 11:38:44 +00002618 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002619 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002620 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002621 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002622 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002623 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002624 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002625 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002626 }
2627 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002629 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002631 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002632 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002633 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002634 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002635 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002636 if (this->glCaps().clampToBorderSupport()) {
2637 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002638 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002639 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002640 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2641 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002642 }
2643 }
Brian Salomondc829942018-10-23 16:07:24 -04002644 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002645 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2646 newNonsamplerState.fBaseMipMapLevel = 0;
2647 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002648
Brian Salomone2826ab2019-06-04 15:58:31 -04002649 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2650 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002651 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2653 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002654 GrGLenum glValues[4];
2655 get_gl_swizzle_values(swizzle, glValues);
2656 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002657 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002658 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002659 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2660 reinterpret_cast<const GrGLint*>(glValues)));
2661 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002662 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2663 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2664 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2665 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2666 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002667 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002668 }
2669 }
Brian Salomondc829942018-10-23 16:07:24 -04002670 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002671 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2672 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2673 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002674 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002675 this->setTextureUnit(unitIdx);
2676 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002677 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002678 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002679 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002680 this->setTextureUnit(unitIdx);
2681 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002682 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002683 }
Brian Salomon327955b2018-10-22 15:39:22 +00002684 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002685 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2686 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002687}
2688
Brian Salomon1f05d452019-02-08 12:33:08 -05002689void GrGLGpu::onResetTextureBindings() {
2690 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2691 GR_GL_TEXTURE_EXTERNAL};
2692 for (int i = 0; i < this->numTextureUnits(); ++i) {
2693 this->setTextureUnit(i);
2694 for (auto target : kTargets) {
2695 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2696 GL_CALL(BindTexture(target, 0));
2697 }
2698 }
2699 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2700 }
2701}
2702
Chris Dalton5a2f9622019-12-27 14:56:38 -07002703void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2704 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2705 if (fHWPatchVertexCount != count) {
2706 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2707 fHWPatchVertexCount = count;
2708 }
2709}
2710
egdaniel080e6732014-12-22 07:35:52 -08002711void GrGLGpu::flushColorWrite(bool writeColor) {
2712 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002713 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002714 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2715 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002716 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002717 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002718 } else {
2719 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002720 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002721 fHWWriteToColor = kYes_TriState;
2722 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002723 }
bsalomon3e791242014-12-17 13:43:13 -08002724}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002725
Chris Dalton674f77a2019-09-30 20:49:39 -06002726void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2727 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2728 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2729 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2730 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2731 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2732 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2733 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002734 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2735 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2736 GL_CALL(ClearColor(r, g, b, a));
2737 fHWClearColor[0] = r;
2738 fHWClearColor[1] = g;
2739 fHWClearColor[2] = b;
2740 fHWClearColor[3] = a;
2741 }
2742}
2743
bsalomon861e1032014-12-16 07:33:49 -08002744void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002745 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002746 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002747 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002748 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002749 }
2750}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002751
Brian Salomond978b902019-02-07 15:09:18 -05002752void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002753 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002754 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002755 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2756 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2757 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002758 }
Brian Salomond978b902019-02-07 15:09:18 -05002759 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2760 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002761 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002762 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002763}
2764
Brian Salomone5e7eb12016-10-14 16:18:33 -04002765// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002766static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2767 const GrSurface* src,
2768 const SkIRect& srcRect,
2769 const SkIPoint& dstPoint,
2770 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002771 int dstSampleCnt = 0;
2772 int srcSampleCnt = 0;
2773 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002774 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002775 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002776 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002777 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002778 }
2779 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2780 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2781
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002782 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2783 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2784
Brian Salomone5e7eb12016-10-14 16:18:33 -04002785 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002786 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002787
Greg Daniel46cfbc62019-06-07 11:43:30 -04002788 GrTextureType dstTexType;
2789 GrTextureType* dstTexTypePtr = nullptr;
2790 GrTextureType srcTexType;
2791 GrTextureType* srcTexTypePtr = nullptr;
2792 if (dstTex) {
2793 dstTexType = dstTex->texturePriv().textureType();
2794 dstTexTypePtr = &dstTexType;
2795 }
2796 if (srcTex) {
2797 srcTexType = srcTex->texturePriv().textureType();
2798 srcTexTypePtr = &srcTexType;
2799 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002800
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002801 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2802 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002803 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002804}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002805
Brian Osmana9c8a052018-01-19 10:31:56 -05002806static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2807 // A RT has a separate MSAA renderbuffer if:
2808 // 1) It's multisampled
2809 // 2) We're using an extension with separate MSAA renderbuffers
2810 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002811 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002812}
2813
Greg Daniel46cfbc62019-06-07 11:43:30 -04002814static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002815 const GrGLCaps& caps) {
2816
bsalomon@google.comeb851172013-04-15 13:51:00 +00002817 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002818 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002819 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002820 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002821
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002822 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2823 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2824
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002825 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2826 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2827
Greg Daniel46cfbc62019-06-07 11:43:30 -04002828 GrTextureType dstTexType;
2829 GrTextureType* dstTexTypePtr = nullptr;
2830 GrTextureType srcTexType;
2831 GrTextureType* srcTexTypePtr = nullptr;
2832 if (dstTex) {
2833 dstTexType = dstTex->texturePriv().textureType();
2834 dstTexTypePtr = &dstTexType;
2835 }
2836 if (srcTex) {
2837 srcTexType = srcTex->texturePriv().textureType();
2838 srcTexTypePtr = &srcTexType;
2839 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002840
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002841 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2842 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002843}
2844
Greg Danielacd66b42019-05-22 16:29:12 -04002845// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002846void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002847 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002848 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002849 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002850 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002851 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2852 GrGLuint texID = texture->textureID();
2853 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002854 GrGLuint* tempFBOID;
2855 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002856
egdanield803f272015-03-18 13:01:52 -07002857 if (0 == *tempFBOID) {
2858 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002859 }
2860
Adrienne Walker4ee88512018-05-17 11:37:14 -07002861 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002862 GR_GL_CALL(
2863 this->glInterface(),
2864 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2865 if (mipLevel == 0) {
2866 texture->baseLevelWasBoundToFBO();
2867 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002868 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002869 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002870 }
egdaniel0f5f9672015-02-03 11:10:51 -08002871}
2872
Brian Salomond2a8ae22019-09-10 16:03:59 -04002873void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002874 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002875 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002876 SkASSERT(surface->asTexture());
2877 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2878 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2879 GR_GL_COLOR_ATTACHMENT0,
2880 textureTarget,
2881 0,
2882 0));
2883 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002884}
2885
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002886void GrGLGpu::onFBOChanged() {
2887 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2888 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002889 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002890 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002891#ifdef SK_DEBUG
2892 if (fIsExecutingCommandBuffer_DebugOnly) {
2893 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2894 "This will severely hurt performance.\n");
2895 }
2896#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002897}
2898
Adrienne Walker4ee88512018-05-17 11:37:14 -07002899void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2900 fStats.incRenderTargetBinds();
2901 GL_CALL(BindFramebuffer(target, fboid));
2902 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2903 fBoundDrawFramebuffer = fboid;
2904 }
2905
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002906 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2907 // The driver forgets the correct scissor when modifying the FBO binding.
2908 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002909 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002910 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002911 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002912 }
2913
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002914 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002915}
2916
Adrienne Walker4ee88512018-05-17 11:37:14 -07002917void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002918 // We're relying on the GL state shadowing being correct in the workaround code below so we
2919 // need to handle a dirty context.
2920 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002921 if (fboid == fBoundDrawFramebuffer &&
2922 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2923 // This workaround only applies to deleting currently bound framebuffers
2924 // on Adreno 420. Because this is a somewhat rare case, instead of
2925 // tracking all the attachments of every framebuffer instead just always
2926 // unbind all attachments.
2927 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2928 GR_GL_RENDERBUFFER, 0));
2929 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2930 GR_GL_RENDERBUFFER, 0));
2931 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2932 GR_GL_RENDERBUFFER, 0));
2933 }
2934
2935 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002936
2937 // Deleting the currently bound framebuffer rebinds to 0.
2938 if (fboid == fBoundDrawFramebuffer) {
2939 this->onFBOChanged();
2940 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002941}
2942
Greg Daniel46cfbc62019-06-07 11:43:30 -04002943bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002944 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002945 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2946 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2947 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002948 auto dstFormat = dst->backendFormat().asGLFormat();
2949 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002950 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002951 return true;
2952 }
2953 }
cblume61214052016-01-26 09:10:48 -08002954
Greg Daniel46cfbc62019-06-07 11:43:30 -04002955 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2956 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002957 return true;
2958 }
2959
Greg Daniel46cfbc62019-06-07 11:43:30 -04002960 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2961 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002962 }
2963
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002964 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002965 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002966 return true;
2967 }
bsalomon083617b2016-02-12 12:10:14 -08002968 }
2969
bsalomon6df86402015-06-01 10:41:49 -07002970 return false;
2971}
2972
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002973bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002974 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002975
2976 int progIdx = TextureToCopyProgramIdx(srcTex);
2977 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2978 GrSLType samplerType =
2979 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2980
2981 if (!fCopyProgramArrayBuffer) {
2982 static const GrGLfloat vdata[] = {
2983 0, 0,
2984 0, 1,
2985 1, 0,
2986 1, 1
2987 };
2988 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2989 kStatic_GrAccessPattern, vdata);
2990 }
2991 if (!fCopyProgramArrayBuffer) {
2992 return false;
2993 }
2994
2995 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2996 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
2997 if (!fCopyPrograms[progIdx].fProgram) {
2998 return false;
2999 }
3000
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003001 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003002 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003003 GrShaderVar::TypeModifier::Uniform);
3004 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3005 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3006 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3007 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003008
Greg Daniel9e3169b2019-06-06 14:38:17 -04003009 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003010 if (shaderCaps->noperspectiveInterpolationSupport()) {
3011 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3012 vshaderTxt.appendf("#extension %s : require\n", extension);
3013 }
3014 vTexCoord.addModifier("noperspective");
3015 }
3016
3017 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3018 vshaderTxt.append(";");
3019 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3020 vshaderTxt.append(";");
3021 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3022 vshaderTxt.append(";");
3023 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3024 vshaderTxt.append(";");
3025
3026 vshaderTxt.append(
3027 "// Copy Program VS\n"
3028 "void main() {"
3029 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3030 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3031 " sk_Position.zw = half2(0, 1);"
3032 "}"
3033 );
3034
Greg Daniel9e3169b2019-06-06 14:38:17 -04003035 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003036 if (shaderCaps->noperspectiveInterpolationSupport()) {
3037 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3038 fshaderTxt.appendf("#extension %s : require\n", extension);
3039 }
3040 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003041 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003042 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3043 fshaderTxt.append(";");
3044 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3045 fshaderTxt.append(";");
3046 fshaderTxt.appendf(
3047 "// Copy Program FS\n"
3048 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003049 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003050 "}"
3051 );
3052
3053 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3054 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3055 SkSL::Program::Settings settings;
3056 settings.fCaps = shaderCaps;
3057 SkSL::String glsl;
3058 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3059 sksl, settings, &glsl, errorHandler);
3060 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3061 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3062 SkASSERT(program->fInputs.isEmpty());
3063
3064 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3065 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3066 errorHandler);
3067 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3068 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3069 errorHandler);
3070 SkASSERT(program->fInputs.isEmpty());
3071
3072 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3073
3074 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3075 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3076 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3077 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3078 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3079 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3080
3081 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3082
3083 GL_CALL(DeleteShader(vshader));
3084 GL_CALL(DeleteShader(fshader));
3085
3086 return true;
3087}
3088
brianosman33f6b3f2016-06-02 05:49:21 -07003089bool GrGLGpu::createMipmapProgram(int progIdx) {
3090 const bool oddWidth = SkToBool(progIdx & 0x2);
3091 const bool oddHeight = SkToBool(progIdx & 0x1);
3092 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3093
Brian Salomon1edc5b92016-11-29 13:43:46 -05003094 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003095
3096 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3097 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3098 if (!fMipmapPrograms[progIdx].fProgram) {
3099 return false;
3100 }
3101
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003102 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003103 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003104 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003105 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003106 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003107 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003108 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003109 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3110 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3111 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3112 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003113 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003114 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003115
Ethan Nicholasfc994162019-06-06 10:04:27 -04003116 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003117 if (shaderCaps->noperspectiveInterpolationSupport()) {
3118 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003119 vshaderTxt.appendf("#extension %s : require\n", extension);
3120 }
3121 vTexCoords[0].addModifier("noperspective");
3122 vTexCoords[1].addModifier("noperspective");
3123 vTexCoords[2].addModifier("noperspective");
3124 vTexCoords[3].addModifier("noperspective");
3125 }
3126
Brian Salomon1edc5b92016-11-29 13:43:46 -05003127 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003128 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003129 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003130 vshaderTxt.append(";");
3131 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003132 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003133 vshaderTxt.append(";");
3134 }
3135
3136 vshaderTxt.append(
3137 "// Mipmap Program VS\n"
3138 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003139 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3140 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003141 );
3142
3143 // Insert texture coordinate computation:
3144 if (oddWidth && oddHeight) {
3145 vshaderTxt.append(
3146 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003147 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3148 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003149 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3150 );
3151 } else if (oddWidth) {
3152 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003153 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3154 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003155 );
3156 } else if (oddHeight) {
3157 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003158 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3159 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003160 );
3161 } else {
3162 vshaderTxt.append(
3163 " v_texCoord0 = a_vertex.xy;"
3164 );
3165 }
3166
3167 vshaderTxt.append("}");
3168
Ethan Nicholasfc994162019-06-06 10:04:27 -04003169 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003170 if (shaderCaps->noperspectiveInterpolationSupport()) {
3171 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003172 fshaderTxt.appendf("#extension %s : require\n", extension);
3173 }
3174 }
brianosman33f6b3f2016-06-02 05:49:21 -07003175 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003176 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003177 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003178 fshaderTxt.append(";");
3179 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003180 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003181 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003182 fshaderTxt.append(
3183 "// Mipmap Program FS\n"
3184 "void main() {"
3185 );
3186
3187 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003188 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003189 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3190 " sample(u_texture, v_texCoord1) + "
3191 " sample(u_texture, v_texCoord2) + "
3192 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003193 );
3194 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003195 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003196 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3197 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003198 );
3199 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003200 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003201 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003202 );
3203 }
3204
3205 fshaderTxt.append("}");
3206
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003207 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003208 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003209 SkSL::Program::Settings settings;
3210 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003211 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003212 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003213 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003214 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003215 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003216 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003217
Brian Osman6c431d52019-04-15 16:31:54 -04003218 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003219 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3220 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003221 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003222 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003223 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003224 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003225
3226 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3227
3228 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3229 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3230 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3231 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3232
3233 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3234
3235 GL_CALL(DeleteShader(vshader));
3236 GL_CALL(DeleteShader(fshader));
3237
3238 return true;
3239}
3240
Greg Daniel46cfbc62019-06-07 11:43:30 -04003241bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003242 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003243 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3244 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3245 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3246 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003247 return false;
3248 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003249 int progIdx = TextureToCopyProgramIdx(srcTex);
3250 if (!dstRT) {
3251 SkASSERT(dstTex);
3252 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3253 return false;
3254 }
3255 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003256 if (!fCopyPrograms[progIdx].fProgram) {
3257 if (!this->createCopyProgram(srcTex)) {
3258 SkDebugf("Failed to create copy program.\n");
3259 return false;
3260 }
3261 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003262 int w = srcRect.width();
3263 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003264 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003265 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003266 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 this->flushViewport(dst->width(), dst->height());
3268 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003269 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003270 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003271 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003272 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3273 attribs->enableVertexArrays(this, 1);
3274 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3275 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 // dst rect edges in NDC (-1 to 1)
3277 int dw = dst->width();
3278 int dh = dst->height();
3279 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3280 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3281 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3282 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003283 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3284 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3285 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3286 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3287 int sw = src->width();
3288 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003289 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3290 // src rect edges in normalized texture space (0 to 1)
3291 sx0 /= sw;
3292 sx1 /= sw;
3293 sy0 /= sh;
3294 sy1 /= sh;
3295 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003296 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3297 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3298 sx1 - sx0, sy1 - sy0, sx0, sy0));
3299 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003300 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003302 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003303 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003304 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003305 this->disableWindowRectangles();
3306 this->disableStencil();
3307 if (this->glCaps().srgbWriteControl()) {
3308 this->flushFramebufferSRGB(true);
3309 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003310 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003311 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003312 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3313 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003314 return true;
3315}
3316
Greg Daniel46cfbc62019-06-07 11:43:30 -04003317void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003318 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003319 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003320 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003321 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003322 SkASSERT(dstTex);
3323 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003324 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003325
Brian Salomond978b902019-02-07 15:09:18 -05003326 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003327 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003328 dstPoint.fX, dstPoint.fY,
3329 srcRect.fLeft, srcRect.fTop,
3330 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003331 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003332 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3333 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003334 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3335 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003336}
3337
Greg Daniel46cfbc62019-06-07 11:43:30 -04003338bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003339 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003340 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003341 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3342 srcRect.width(), srcRect.height());
3343 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003344 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003345 return false;
mtklein404b3b22015-05-18 09:29:10 -07003346 }
bsalomon5df6fee2015-05-18 06:26:15 -07003347 }
bsalomon6df86402015-06-01 10:41:49 -07003348
Brian Salomond2a8ae22019-09-10 16:03:59 -04003349 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3350 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003351 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003352 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003353
3354 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003355 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003356 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003357
Greg Daniel46cfbc62019-06-07 11:43:30 -04003358 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3359 srcRect.fTop,
3360 srcRect.fRight,
3361 srcRect.fBottom,
3362 dstRect.fLeft,
3363 dstRect.fTop,
3364 dstRect.fRight,
3365 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003366 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003367 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3368 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003369
3370 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3371 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003372 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003373}
3374
Brian Salomon930f9392018-06-20 16:25:26 -04003375bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3376 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003377 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003378 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003379 return false;
3380 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003381 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003382 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3383 // Uses draw calls to do a series of downsample operations to successive mips.
3384
3385 // The manual approach requires the ability to limit which level we're sampling and that the
3386 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003387 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003388 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003389 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003390 GL_CALL(GenerateMipmap(glTex->target()));
3391 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003392 }
3393
brianosman33f6b3f2016-06-02 05:49:21 -07003394 int width = texture->width();
3395 int height = texture->height();
3396 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003397 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003398
3399 // Create (if necessary), then bind temporary FBO:
3400 if (0 == fTempDstFBOID) {
3401 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3402 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003403 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003404 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003405
3406 // Bind the texture, to get things configured for filtering.
3407 // We'll be changing our base level further below:
3408 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003409 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003410 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003411
3412 // Vertex data:
3413 if (!fMipmapProgramArrayBuffer) {
3414 static const GrGLfloat vdata[] = {
3415 0, 0,
3416 0, 1,
3417 1, 0,
3418 1, 1
3419 };
Brian Salomonae64c192019-02-05 09:41:37 -05003420 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003421 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003422 }
3423 if (!fMipmapProgramArrayBuffer) {
3424 return false;
3425 }
3426
3427 fHWVertexArrayState.setVertexArrayID(this, 0);
3428
3429 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003430 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003431 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003432 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003433
3434 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003435 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003436 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003437 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003438 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003439 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003440
3441 // Do all the blits:
3442 width = texture->width();
3443 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003444
brianosman33f6b3f2016-06-02 05:49:21 -07003445 for (GrGLint level = 1; level < levelCount; ++level) {
3446 // Get and bind the program for this particular downsample (filter shape can vary):
3447 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3448 if (!fMipmapPrograms[progIdx].fProgram) {
3449 if (!this->createMipmapProgram(progIdx)) {
3450 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003451 // Invalidate all params to cover base level change in a previous iteration.
3452 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003453 return false;
3454 }
3455 }
Brian Salomon802cb312018-06-08 18:05:20 -04003456 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003457
3458 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3459 const float invWidth = 1.0f / width;
3460 const float invHeight = 1.0f / height;
3461 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3462 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3463 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3464
3465 // Only sample from previous mip
3466 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3467
Brian Salomon930f9392018-06-20 16:25:26 -04003468 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3469 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003470
Brian Osman788b9162020-02-07 10:36:46 -05003471 width = std::max(1, width / 2);
3472 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003473 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003474
3475 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3476 }
3477
3478 // Unbind:
3479 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3480 GR_GL_TEXTURE_2D, 0, 0));
3481
Brian Salomon930f9392018-06-20 16:25:26 -04003482 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003483 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3484 // We drew the 2nd to last level into the last level.
3485 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3486 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003487
brianosman33f6b3f2016-06-02 05:49:21 -07003488 return true;
3489}
3490
Chris Daltond7291ba2019-03-07 14:17:03 -07003491void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003492 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3493 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003494
3495 int effectiveSampleCnt;
3496 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003497 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003498
3499 sampleLocations->reset(effectiveSampleCnt);
3500 for (int i = 0; i < effectiveSampleCnt; ++i) {
3501 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3502 }
3503}
3504
cdalton231c5fd2015-05-13 12:35:36 -07003505void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003506 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003507 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003508 case kTexture_GrXferBarrierType: {
3509 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003510 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003511 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3512 // The render target uses separate storage so no need for glTextureBarrier.
3513 // FIXME: The render target will resolve automatically when its texture is bound,
3514 // but we could resolve only the bounds that will be read if we do it here instead.
3515 return;
3516 }
cdalton9954bc32015-04-29 14:17:00 -07003517 SkASSERT(this->caps()->textureBarrierSupport());
3518 GL_CALL(TextureBarrier());
3519 return;
cdalton231c5fd2015-05-13 12:35:36 -07003520 }
cdalton8917d622015-05-06 13:40:21 -07003521 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003522 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003523 this->caps()->blendEquationSupport());
3524 GL_CALL(BlendBarrier());
3525 return;
bsalomoncb02b382015-08-12 11:14:50 -07003526 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003527 }
3528}
3529
Chris Daltone1196c52019-12-28 14:31:09 -07003530void GrGLGpu::insertManualFramebufferBarrier() {
3531 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3532 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3533}
3534
Brian Salomon85c3d682019-11-04 15:04:54 -05003535GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003536 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003537 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003538 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003539 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003540 // We don't support protected textures in GL.
3541 if (isProtected == GrProtected::kYes) {
3542 return {};
3543 }
3544
Brian Salomon8a375832018-03-14 10:21:40 -04003545 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003546
Brian Salomond4764a12019-08-08 12:08:24 -04003547 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003548 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003549 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003550 }
3551
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003552 int numMipLevels = 1;
3553 if (mipMapped == GrMipMapped::kYes) {
3554 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3555 }
3556
Robert Phillipsd34691b2019-09-24 13:38:43 -04003557 // Compressed formats go through onCreateCompressedBackendTexture
3558 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3559
Greg Daniel15500642019-06-27 03:05:55 +00003560 GrGLTextureInfo info;
3561 GrGLTextureParameters::SamplerOverriddenState initialState;
3562
Greg Danield51fa2f2020-01-22 16:53:38 -05003563 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003564 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003565 }
Brian Salomon0f396992020-06-19 19:51:21 -04003566 switch (format.textureType()) {
3567 case GrTextureType::kNone:
3568 case GrTextureType::kExternal:
3569 return {};
3570 case GrTextureType::k2D:
3571 info.fTarget = GR_GL_TEXTURE_2D;
3572 break;
3573 case GrTextureType::kRectangle:
3574 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipMapped::kYes) {
3575 return {};
3576 }
3577 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3578 break;
3579 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003580 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003581 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3582 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003583 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003584 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003585 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003586
Brian Salomon85c3d682019-11-04 15:04:54 -05003587 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003588 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003589
Brian Salomone2826ab2019-06-04 15:58:31 -04003590 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003591 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003592 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3593 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003594
Brian Salomon85c3d682019-11-04 15:04:54 -05003595 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3596 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003597}
3598
Greg Daniel16032b32020-05-06 15:31:10 -04003599bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3600 sk_sp<GrRefCntedCallback> finishedCallback,
3601 const BackendTextureData* data) {
3602 GrGLTextureInfo info;
3603 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3604
3605 int numMipLevels = 1;
3606 if (backendTexture.hasMipMaps()) {
3607 numMipLevels =
3608 SkMipMap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
3609 }
3610
3611 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3612
Brian Salomon22558932020-05-15 14:46:34 -04003613 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003614
Brian Salomonc3f49432020-06-03 10:21:50 -04003615 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
Greg Danielb2365d82020-05-13 15:32:04 -04003616 // so that the uploads go to the right levels.
Brian Salomonc3f49432020-06-03 10:21:50 -04003617 if (numMipLevels && this->glCaps().mipMapLevelAndLodControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003618 auto params = backendTexture.getGLTextureParams();
3619 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3620 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003621 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003622 nonsamplerState.fBaseMipMapLevel = 0;
3623 }
3624 if (params->nonsamplerState().fMaxMipMapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003625 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003626 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3627 }
3628 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3629 }
3630
Greg Daniel16032b32020-05-06 15:31:10 -04003631 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003632 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003633 if (data->type() == BackendTextureData::Type::kPixmaps) {
3634 SkTDArray<GrMipLevel> texels;
3635 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3636 texels.append(numMipLevels);
3637 for (int i = 0; i < numMipLevels; ++i) {
3638 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3639 }
Brian Salomon22558932020-05-15 14:46:34 -04003640 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3641 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3642 info.fTarget, dstRect, colorType, texels.begin(),
3643 texels.count());
3644 } else if (data->type() == BackendTextureData::Type::kColor) {
3645 uint32_t levelMask = (1 << numMipLevels) - 1;
3646 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3647 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003648 }
3649
3650 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003651 this->bindTextureToScratchUnit(info.fTarget, 0);
3652 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003653}
3654
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003655void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003656 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3657
3658 GrGLTextureInfo info;
3659 if (tex.getGLTextureInfo(&info)) {
3660 GL_CALL(DeleteTextures(1, &info.fID));
3661 }
3662}
3663
Robert Phillips3bce1f72020-05-12 12:41:58 -04003664bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3665 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3666
3667 Stats::ProgramCacheResult stat;
3668
3669 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3670 if (!tmp) {
3671 return false;
3672 }
3673
3674 return stat != Stats::ProgramCacheResult::kHit;
3675}
3676
Robert Phillipsf0ced622019-05-16 09:06:25 -04003677#if GR_TEST_UTILS
3678
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003679bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003680 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003681
Greg Daniel52e16d92018-04-10 09:34:07 -04003682 GrGLTextureInfo info;
3683 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003684 return false;
3685 }
3686
3687 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003688 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003689
3690 return (GR_GL_TRUE == result);
3691}
3692
Brian Salomonf865b052018-03-09 09:01:53 -05003693GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003694 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003695 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3696 return GrBackendRenderTarget(); // invalid
3697 }
Brian Salomon8a375832018-03-14 10:21:40 -04003698 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003699 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003700 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003701 return {};
3702 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003703 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003704 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003705 if (sFormatIdx < 0) {
3706 return {};
3707 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003708 GrGLuint colorID = 0;
3709 GrGLuint stencilID = 0;
3710 auto deleteIDs = [&] {
3711 if (colorID) {
3712 if (useTexture) {
3713 GL_CALL(DeleteTextures(1, &colorID));
3714 } else {
3715 GL_CALL(DeleteRenderbuffers(1, &colorID));
3716 }
Brian Salomonf865b052018-03-09 09:01:53 -05003717 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003718 if (stencilID) {
3719 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003720 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003721 };
3722
3723 if (useTexture) {
3724 GL_CALL(GenTextures(1, &colorID));
3725 } else {
3726 GL_CALL(GenRenderbuffers(1, &colorID));
3727 }
3728 GL_CALL(GenRenderbuffers(1, &stencilID));
3729 if (!stencilID || !colorID) {
3730 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003731 return {};
3732 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003733
Brian Salomonf865b052018-03-09 09:01:53 -05003734 GrGLFramebufferInfo info;
3735 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003736 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003737 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3738 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003739 deleteIDs();
3740 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003741 }
3742
3743 this->invalidateBoundRenderTarget();
3744
Adrienne Walker4ee88512018-05-17 11:37:14 -07003745 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003746 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003747 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon0f396992020-06-19 19:51:21 -04003748 colorID = this->createTexture({w, h}, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
3749 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003750 if (!colorID) {
3751 deleteIDs();
3752 return {};
3753 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003754 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3755 colorID, 0));
3756 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003757 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003758 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003759 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003760 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3761 GR_GL_RENDERBUFFER, colorID));
3762 }
3763 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003764 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003765 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003766 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003767 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003768 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3769 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003770 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003771 }
3772
Brian Salomon93348dd2018-08-29 12:56:23 -04003773 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3774 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3775 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3776 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003777 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003778 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003779
Adrienne Walker4ee88512018-05-17 11:37:14 -07003780 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003781 GrGLenum status;
3782 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3783 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003784 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003785 return {};
3786 }
3787 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003788
Greg Daniel108bb232018-07-03 16:18:29 -04003789 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003790 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003791 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003792}
3793
3794void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003795 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003796 GrGLFramebufferInfo info;
3797 if (backendRT.getGLFramebufferInfo(&info)) {
3798 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003799 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003800 }
3801 }
joshualitt8fd844f2015-12-02 13:36:47 -08003802}
3803
Greg Daniel26b50a42018-03-08 09:49:58 -05003804void GrGLGpu::testingOnly_flushGpuAndSync() {
3805 GL_CALL(Finish());
3806}
Brian Salomonf865b052018-03-09 09:01:53 -05003807#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003808
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003809///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003810
cdaltone2e71c22016-04-07 18:13:29 -07003811GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003812 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003813 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003814 GrGLAttribArrayState* attribState;
3815
cdaltone2e71c22016-04-07 18:13:29 -07003816 if (gpu->glCaps().isCoreProfile()) {
3817 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003818 GrGLuint arrayID;
3819 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3820 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003821 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003822 }
cdaltone2e71c22016-04-07 18:13:29 -07003823 if (ibuf) {
3824 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003825 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003826 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003827 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003828 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003829 if (ibuf) {
3830 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003831 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003832 } else {
3833 this->setVertexArrayID(gpu, 0);
3834 }
3835 int attrCount = gpu->glCaps().maxVertexAttributes();
3836 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3837 fDefaultVertexArrayAttribState.resize(attrCount);
3838 }
3839 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003840 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003841 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003842}
bsalomone179a912016-01-20 06:18:10 -08003843
Greg Danielfe159622020-04-10 17:43:51 +00003844void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3845 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003846 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003847}
3848
Greg Daniel78be37f2020-04-14 16:40:35 -04003849void GrGLGpu::flush(FlushType flushType) {
3850 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3851 GL_CALL(Flush());
3852 fNeedsGLFlush = false;
3853 }
3854}
3855
Greg Danielfe159622020-04-10 17:43:51 +00003856bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3857 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003858 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003859 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003860 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003861 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003862 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003863 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003864 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003865 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003866}
3867
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003868void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3869 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3870 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3871 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003872}
3873
Greg Daniel6be35232017-03-01 17:01:09 -05003874GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003875 if (!this->caps()->fenceSyncSupport()) {
3876 return 0;
3877 }
Greg Daniel6be35232017-03-01 17:01:09 -05003878 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003879 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3880 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3881 GrGLuint fence = 0;
3882 GL_CALL(GenFences(1, &fence));
3883 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3884 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3885 } else {
3886 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3887 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003888 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003889 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003890 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003891}
3892
Brian Salomonb0d8b762019-05-06 16:58:22 -04003893bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003894 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3895 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3896 if (!timeout) {
3897 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003898 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003899 }
3900 GrGLboolean result;
3901 GL_CALL_RET(result, TestFence(nvFence));
3902 return result == GR_GL_TRUE;
3903 }
3904 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3905 // If this really becomes necessary we could poll TestFence().
3906 // FinishFence always flushes so no need to check flush param.
3907 GL_CALL(FinishFence(nvFence));
3908 return true;
3909 } else {
3910 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3911 GrGLenum result;
3912 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3913 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3914 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003915}
3916
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003917bool GrGLGpu::waitFence(GrFence fence) {
3918 if (!this->caps()->fenceSyncSupport()) {
3919 return true;
3920 }
3921 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003922}
3923
3924void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003925 if (this->caps()->fenceSyncSupport()) {
3926 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3927 }
Greg Daniel6be35232017-03-01 17:01:09 -05003928}
3929
Greg Daniel301015c2019-11-18 14:06:46 -05003930std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003931 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003932 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003933}
3934
Greg Daniel301015c2019-11-18 14:06:46 -05003935std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3936 const GrBackendSemaphore& semaphore,
3937 GrResourceProvider::SemaphoreWrapType wrapType,
3938 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003939 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003940 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3941}
3942
Greg Daniel301015c2019-11-18 14:06:46 -05003943void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3944 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003945
Greg Daniel48661b82018-01-22 16:11:35 -05003946 GrGLsync sync;
3947 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3948 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003949 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003950}
3951
Greg Daniel301015c2019-11-18 14:06:46 -05003952void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
3953 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003954
3955 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3956}
3957
Brian Salomonb0d8b762019-05-06 16:58:22 -04003958void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003959 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003960}
3961
Greg Daniel6be35232017-03-01 17:01:09 -05003962void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003963 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3964 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
3965 GL_CALL(DeleteFences(1, &nvFence));
3966 } else {
3967 GL_CALL(DeleteSync(sync));
3968 }
jvanverth84741b32016-09-30 08:39:02 -07003969}
Brian Osman13dddce2017-05-09 13:19:50 -04003970
Greg Daniel301015c2019-11-18 14:06:46 -05003971std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04003972 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05003973 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05003974 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003975 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05003976 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
Greg Daniel78be37f2020-04-14 16:40:35 -04003977 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04003978
3979 return semaphore;
3980}
Robert Phillips646e4292017-06-13 12:44:56 -04003981
3982int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04003983 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04003984 case kTexture2DSampler_GrSLType:
3985 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04003986 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003987 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04003988 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003989 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04003990 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04003991 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04003992 }
3993}
Brian Osman71a18892017-08-10 10:23:25 -04003994
Kevin Lubickf4def342018-10-04 12:52:50 -04003995#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003996#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04003997void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
3998 // We are called by the base class, which has already called beginObject(). We choose to nest
3999 // all of our caps information in a named sub-object.
4000 writer->beginObject("GL GPU");
4001
4002 const GrGLubyte* str;
4003 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4004 writer->appendString("GL_VERSION", (const char*)(str));
4005 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4006 writer->appendString("GL_RENDERER", (const char*)(str));
4007 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4008 writer->appendString("GL_VENDOR", (const char*)(str));
4009 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4010 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4011
4012 writer->appendName("extensions");
4013 glInterface()->fExtensions.dumpJSON(writer);
4014
4015 writer->endObject();
4016}
Kevin Lubickf4def342018-10-04 12:52:50 -04004017#endif