blob: bfaab17d8c43df8922709395d65fc3e9f293285e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040022#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040024#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrFixedClip.h"
26#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040028#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
30#include "src/gpu/GrShaderCaps.h"
31#include "src/gpu/GrSurfaceProxyPriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/gl/GrGLBuffer.h"
34#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
Brian Salomond8575452020-02-25 12:13:29 -050047#define GL_ALLOC_CALL(call) \
48 [&] { \
49 if (this->glCaps().skipErrorChecks()) { \
50 GR_GL_CALL(this->glInterface(), call); \
51 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
52 } else { \
53 GrGLClearErr(this->glInterface()); \
54 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
55 return GR_GL_GET_ERROR(this->glInterface()); \
56 } \
57 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000058
Jim Van Verth32ac83e2016-11-28 15:23:57 -050059//#define USE_NSIGHT
60
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000061///////////////////////////////////////////////////////////////////////////////
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050084 GR_GL_HSL_LUMINOSITY,
85
86 // Illegal... needs to map to something.
87 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070088};
Brian Salomon4dea72a2019-12-18 10:43:10 -050089static_assert(0 == kAdd_GrBlendEquation);
90static_assert(1 == kSubtract_GrBlendEquation);
91static_assert(2 == kReverseSubtract_GrBlendEquation);
92static_assert(3 == kScreen_GrBlendEquation);
93static_assert(4 == kOverlay_GrBlendEquation);
94static_assert(5 == kDarken_GrBlendEquation);
95static_assert(6 == kLighten_GrBlendEquation);
96static_assert(7 == kColorDodge_GrBlendEquation);
97static_assert(8 == kColorBurn_GrBlendEquation);
98static_assert(9 == kHardLight_GrBlendEquation);
99static_assert(10 == kSoftLight_GrBlendEquation);
100static_assert(11 == kDifference_GrBlendEquation);
101static_assert(12 == kExclusion_GrBlendEquation);
102static_assert(13 == kMultiply_GrBlendEquation);
103static_assert(14 == kHSLHue_GrBlendEquation);
104static_assert(15 == kHSLSaturation_GrBlendEquation);
105static_assert(16 == kHSLColor_GrBlendEquation);
106static_assert(17 == kHSLLuminosity_GrBlendEquation);
107static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
Brian Salomond978b902019-02-07 15:09:18 -0500133//////////////////////////////////////////////////////////////////////////////
134
135static int gl_target_to_binding_index(GrGLenum target) {
136 switch (target) {
137 case GR_GL_TEXTURE_2D:
138 return 0;
139 case GR_GL_TEXTURE_RECTANGLE:
140 return 1;
141 case GR_GL_TEXTURE_EXTERNAL:
142 return 2;
143 }
144 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500145}
146
147GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500148 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
149}
150
151bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
152 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500153}
154
155void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500156 int targetIndex = gl_target_to_binding_index(target);
157 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
158 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500159}
160
Brian Salomon1f05d452019-02-08 12:33:08 -0500161void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
162 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500163}
164
Brian Salomon1f05d452019-02-08 12:33:08 -0500165void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
166 for (auto& targetBinding : fTargetBindings) {
167 targetBinding.fBoundResourceID.makeInvalid();
168 if (markUnmodified) {
169 targetBinding.fHasBeenModified = false;
170 }
Brian Salomond978b902019-02-07 15:09:18 -0500171 }
172}
173
174//////////////////////////////////////////////////////////////////////////////
175
Brian Salomondc829942018-10-23 16:07:24 -0400176static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
177 switch (filter) {
178 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
179 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
180 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
181 }
182 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400183}
184
185static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
186 switch (filter) {
187 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
188 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
189 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
190 }
191 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400192}
193
Michael Ludwigf23a1522018-12-10 11:36:13 -0500194static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
195 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400196 switch (wrapMode) {
197 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
198 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
199 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500200 case GrSamplerState::WrapMode::kClampToBorder:
201 // May not be supported but should have been caught earlier
202 SkASSERT(caps.clampToBorderSupport());
203 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500204 }
Brian Salomondc829942018-10-23 16:07:24 -0400205 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400206}
207
208///////////////////////////////////////////////////////////////////////////////
209
210class GrGLGpu::SamplerObjectCache {
211public:
212 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
213 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
214 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
215 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
216 std::fill_n(fSamplers, kNumSamplers, 0);
217 }
218
219 ~SamplerObjectCache() {
220 if (!fNumTextureUnits) {
221 // We've already been abandoned.
222 return;
223 }
Chris Dalton703fe682019-05-15 12:58:12 -0600224 for (GrGLuint sampler : fSamplers) {
225 // The spec states that "zero" values should be silently ignored, however they still
226 // trigger GL errors on some NVIDIA platforms.
227 if (sampler) {
228 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
229 }
230 }
Brian Salomondc829942018-10-23 16:07:24 -0400231 }
232
Brian Salomonccb61422020-01-09 10:46:36 -0500233 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400234 int index = StateToIndex(state);
235 if (!fSamplers[index]) {
236 GrGLuint s;
237 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
238 if (!s) {
239 return;
240 }
241 fSamplers[index] = s;
242 auto minFilter = filter_to_gl_min_filter(state.filter());
243 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500244 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
245 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400246 GR_GL_CALL(fGpu->glInterface(),
247 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
248 GR_GL_CALL(fGpu->glInterface(),
249 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
250 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
251 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
252 }
253 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
254 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
255 fHWBoundSamplers[unitIdx] = fSamplers[index];
256 }
257 }
258
259 void invalidateBindings() {
260 // When we have sampler support we always use samplers. So setting these to zero will cause
261 // a rebind on next usage.
262 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
263 }
264
265 void abandon() {
266 fHWBoundSamplers.reset();
267 fNumTextureUnits = 0;
268 }
269
270 void release() {
271 if (!fNumTextureUnits) {
272 // We've already been abandoned.
273 return;
274 }
275 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
276 std::fill_n(fSamplers, kNumSamplers, 0);
277 // Deleting a bound sampler implicitly binds sampler 0.
278 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
279 }
280
281private:
Brian Salomonccb61422020-01-09 10:46:36 -0500282 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400283 int filter = static_cast<int>(state.filter());
284 SkASSERT(filter >= 0 && filter < 3);
285 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500286 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400287 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500288 SkASSERT(wrapY >= 0 && wrapY < 4);
289 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400290 SkASSERT(idx < kNumSamplers);
291 return idx;
292 }
293
294 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500295 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400296 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
297 GrGLuint fSamplers[kNumSamplers];
298 int fNumTextureUnits;
299};
300
reed@google.comac10a2d2010-12-22 21:39:39 +0000301///////////////////////////////////////////////////////////////////////////////
302
Brian Salomon384fab42017-12-07 12:33:05 -0500303sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
304 GrContext* context) {
305 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500306 interface = GrGLMakeNativeInterface();
307 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
308 // to GrGLMakeNativeInterface.
309 if (!interface) {
310 interface = sk_ref_sp(GrGLCreateNativeInterface());
311 }
Brian Salomon384fab42017-12-07 12:33:05 -0500312 if (!interface) {
313 return nullptr;
314 }
bsalomon424cc262015-05-22 10:37:30 -0700315 }
Jim Van Verth76334772017-05-05 16:46:05 -0400316#ifdef USE_NSIGHT
317 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
318#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500319 auto glContext = GrGLContext::Make(std::move(interface), options);
320 if (!glContext) {
321 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700322 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500323 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700324}
325
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500326GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
327 : GrGpu(context)
328 , fGLContext(std::move(ctx))
329 , fProgramCache(new ProgramCache(this))
330 , fHWProgramID(0)
331 , fTempSrcFBOID(0)
332 , fTempDstFBOID(0)
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()));
Robert Phillips71f06f62020-05-15 16:37:21 +0000840 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
841 glTex->target(), left, top, width, height, srcColorType, texels,
842 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000843}
844
Brian Salomone05ba5a2019-04-08 11:59:07 -0400845bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400846 GrColorType textureColorType, GrColorType bufferColorType,
847 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400848 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400849
Jim Van Verth1676cb92019-01-15 13:24:45 -0500850 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400851 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500852
Brian Salomonc320b152018-02-20 14:05:36 -0500853 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854 return false;
855 }
856
Hal Canaryc36f7e72018-06-18 15:50:09 -0400857 static_assert(sizeof(int) == sizeof(int32_t), "");
858 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859 return false;
860 }
861
Brian Salomond978b902019-02-07 15:09:18 -0500862 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400863
864 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500865 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400866 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500867 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400868
Greg Daniel660cc992017-06-26 14:55:05 -0400869 SkDEBUGCODE(
870 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
871 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
872 SkASSERT(bounds.contains(subRect));
873 )
874
Brian Salomonb28cb682019-07-26 12:48:47 -0400875 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400876 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400877 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700878 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400879 return false;
880 }
881
882 bool restoreGLRowLength = false;
883 if (trimRowBytes != rowBytes) {
884 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400885 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400886 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
887 restoreGLRowLength = true;
888 }
889
Greg Danielba88ab62019-07-26 09:14:01 -0400890 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400891 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400892 GrGLenum externalFormat = 0;
893 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400894 this->glCaps().getTexSubImageExternalFormatAndType(
895 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400896 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897 return false;
898 }
899
Brian Salomon8cbf6622019-07-30 11:03:14 -0400900 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 GL_CALL(TexSubImage2D(glTex->target(),
902 0,
903 left, top,
904 width,
905 height,
906 externalFormat, externalType,
907 pixels));
908
909 if (restoreGLRowLength) {
910 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
911 }
912
913 return true;
914}
915
Brian Salomon26de56e2019-04-10 12:14:26 -0400916bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400917 GrColorType surfaceColorType, GrColorType dstColorType,
918 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400919 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
920 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400921 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400922 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
923 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400924}
925
Brian Osman9b560242017-09-05 15:34:52 -0400926void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500927 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
928 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
929 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
930 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400931 }
Brian Osman9b560242017-09-05 15:34:52 -0400932}
933
Robert Phillips71f06f62020-05-15 16:37:21 +0000934bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
935 int texHeight, GrGLenum target, int left, int top, int width,
936 int height, GrColorType srcColorType, const GrMipLevel texels[],
937 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500938 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400939 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500940
Greg Daniel7bfc9132019-08-14 14:23:53 -0400941 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Robert Phillips71f06f62020-05-15 16:37:21 +0000942 SkDEBUGCODE(
943 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
944 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
945 SkASSERT(bounds.contains(subRect));
946 )
947 SkASSERT(1 == mipLevelCount ||
948 (0 == left && 0 == top && width == texWidth && height == texHeight));
949
950 this->unbindCpuToGpuXferBuffer();
951
952 const GrGLInterface* interface = this->glInterface();
953 const GrGLCaps& caps = this->glCaps();
cblume55f2d2d2016-02-26 13:20:48 -0800954
Brian Salomonf77c1462019-08-01 15:19:29 -0400955 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800956
Robert Phillips71f06f62020-05-15 16:37:21 +0000957 if (width == 0 || height == 0) {
958 return false;
959 }
960
bsalomon5b30c6f2015-12-17 14:17:34 -0800961 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800962 GrGLenum externalFormat;
963 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400964 this->glCaps().getTexSubImageExternalFormatAndType(
965 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400966 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800967 return false;
968 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400969
Robert Phillips71f06f62020-05-15 16:37:21 +0000970 /*
971 * Check whether to allocate a temporary buffer for flipping y or
972 * because our srcData has extra bytes past each row. If so, we need
973 * to trim those off here, since GL ES may not let us specify
974 * GL_UNPACK_ROW_LENGTH.
975 */
Brian Salomon5c66aa72020-05-13 10:15:55 -0400976 bool restoreGLRowLength = false;
977
Robert Phillips71f06f62020-05-15 16:37:21 +0000978 if (mipMapsStatus) {
979 *mipMapsStatus = (mipLevelCount > 1) ?
980 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
981 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400982
Robert Phillips71f06f62020-05-15 16:37:21 +0000983 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
984
985 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
986 if (!texels[currentMipLevel].fPixels) {
987 if (mipMapsStatus) {
988 *mipMapsStatus = GrMipMapsStatus::kDirty;
989 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400990 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +0000991 }
Robert Phillips71f06f62020-05-15 16:37:21 +0000992 int twoToTheMipLevel = 1 << currentMipLevel;
993 const int currentWidth = std::max(1, width / twoToTheMipLevel);
994 const int currentHeight = std::max(1, height / twoToTheMipLevel);
995 const size_t trimRowBytes = currentWidth * bpp;
996 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400997
998 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
999 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Robert Phillips71f06f62020-05-15 16:37:21 +00001000 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001001 restoreGLRowLength = true;
1002 }
1003
Robert Phillips71f06f62020-05-15 16:37:21 +00001004 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1005 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001006 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001007 if (restoreGLRowLength) {
1008 SkASSERT(caps.writePixelsRowBytesSupport());
1009 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1010 }
Robert Phillips71f06f62020-05-15 16:37:21 +00001011 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001012}
1013
Greg Daniel7bfc9132019-08-14 14:23:53 -04001014bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001015 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001016 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001017 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001018 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001019 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001020 const GrGLCaps& caps = this->glCaps();
1021
1022 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001023 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001024 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001025 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001026 }
1027
Robert Phillips9f744f72019-12-19 19:14:33 -05001028 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1029 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1030
Greg Daniel7bfc9132019-08-14 14:23:53 -04001031 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001032
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001033 int numMipLevels = 1;
1034 if (mipMapped == GrMipMapped::kYes) {
1035 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1036 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001037
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001038 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001039 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001040
1041 if (useTexStorage) {
1042 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001043 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1044 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001045 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001046 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001047 }
Robert Phillips42716d42019-12-16 12:19:54 -05001048
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001049 size_t offset = 0;
1050 for (int level = 0; level < numMipLevels; ++level) {
1051
Robert Phillips99dead92020-01-27 16:11:57 -05001052 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1053 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001054
Brian Salomond8575452020-02-25 12:13:29 -05001055 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1056 level,
1057 0, // left
1058 0, // top
1059 dimensions.width(),
1060 dimensions.height(),
1061 internalFormat,
1062 SkToInt(levelDataSize),
1063 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001064
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001065 if (error != GR_GL_NO_ERROR) {
1066 return false;
1067 }
1068
Robert Phillips9f744f72019-12-19 19:14:33 -05001069 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001070 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001071 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001072 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001073 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001074
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001075 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001076 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1077 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001078
1079 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001080 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1081 level,
1082 internalFormat,
1083 dimensions.width(),
1084 dimensions.height(),
1085 0, // border
1086 SkToInt(levelDataSize),
1087 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001088
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001089 if (error != GR_GL_NO_ERROR) {
1090 return false;
1091 }
1092
Robert Phillips9f744f72019-12-19 19:14:33 -05001093 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001094 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001095 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001096 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001097 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001098}
1099
Brian Salomond8575452020-02-25 12:13:29 -05001100bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001101 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001102 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001103 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001104 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001105 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001106 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1107 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001108 break;
1109 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001110 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1111 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001112 break;
1113 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1114 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001115 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1116 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001117 break;
1118 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001119 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001120 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001121 }
Brian Salomond8575452020-02-25 12:13:29 -05001122 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001123}
1124
Brian Salomonea4ad302019-08-07 13:04:55 -04001125bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001126 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001127 GrGLRenderTarget::IDs* rtIDs) {
1128 rtIDs->fMSColorRenderbufferID = 0;
1129 rtIDs->fRTFBOID = 0;
1130 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1131 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001132
bsalomona11e5fc2015-12-18 07:59:41 -08001133 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001134
Brian Salomonea4ad302019-08-07 13:04:55 -04001135 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001136 goto FAILED;
1137 }
1138
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001139 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001140 goto FAILED;
1141 }
1142
Brian Salomonea4ad302019-08-07 13:04:55 -04001143 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1144 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 goto FAILED;
1146 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001147
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001148 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1149 // the texture bound to the other. The exception is the IMG multisample extension. With this
1150 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1151 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001152 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001153 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1154 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1155 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001156 goto FAILED;
1157 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001158 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001160 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 }
1162
egdanield803f272015-03-18 13:01:52 -07001163 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001164 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001165 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001166 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001168 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1169 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 goto FAILED;
1171 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001172 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001173 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001174 GR_GL_COLOR_ATTACHMENT0,
1175 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001176 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001178 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001180 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001181 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1182 GR_GL_COLOR_ATTACHMENT0,
1183 desc.fTarget,
1184 desc.fID,
1185 0,
1186 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001187 } else {
egdanield803f272015-03-18 13:01:52 -07001188 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001189 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 desc.fTarget,
1191 desc.fID,
1192 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001193 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194
1195 return true;
1196
1197FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 if (rtIDs->fMSColorRenderbufferID) {
1199 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001201 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1202 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001204 if (rtIDs->fTexFBOID) {
1205 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 }
1207 return false;
1208}
1209
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001210// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001211static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001212// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001213 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001214}
1215
Brian Salomone2826ab2019-06-04 15:58:31 -04001216static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001218 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1219 // drivers have a bug where an FBO won't be complete if it includes a
1220 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001221 GrGLTextureParameters::SamplerOverriddenState state;
1222 state.fMinFilter = GR_GL_NEAREST;
1223 state.fMagFilter = GR_GL_NEAREST;
1224 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1225 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001226 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1227 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1228 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1229 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001230 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001231}
1232
Brian Salomona56a7462020-02-07 14:17:25 -05001233sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001234 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001235 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001236 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001237 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001238 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001239 int mipLevelCount,
1240 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001241 // We don't support protected textures in GL.
1242 if (isProtected == GrProtected::kYes) {
1243 return nullptr;
1244 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001245 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001246
Brian Salomond2a8ae22019-09-10 16:03:59 -04001247 SkASSERT(mipLevelCount > 0);
1248 GrMipMapsStatus mipMapsStatus =
1249 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001250 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001251 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001252 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001254 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001255 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001256 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1257 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001258
Brian Salomona56a7462020-02-07 14:17:25 -05001259 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1260 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001261
1262 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001263 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001264 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001265
Robert Phillips67d52cf2017-06-05 13:38:13 -04001266 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001267 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001268 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001269 GL_CALL(BindTexture(texDesc.fTarget, 0));
1270 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001271
Brian Salomonea4ad302019-08-07 13:04:55 -04001272 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1273 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 return return_null_texture();
1275 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001276 tex = sk_make_sp<GrGLTextureRenderTarget>(
1277 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001278 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001280 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001282 // The non-sampler params are still at their default values.
1283 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1284 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001285 if (levelClearMask) {
1286 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001287 GrColorType colorType;
1288 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1289 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001290 if (this->glCaps().clearTextureSupport()) {
1291 for (int i = 0; i < mipLevelCount; ++i) {
1292 if (levelClearMask & (1U << i)) {
1293 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1294 nullptr));
1295 }
1296 }
1297 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1298 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001299 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001300 this->disableWindowRectangles();
1301 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001302 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001303 for (int i = 0; i < mipLevelCount; ++i) {
1304 if (levelClearMask & (1U << i)) {
1305 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1306 kDst_TempFBOTarget);
1307 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1308 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1309 }
1310 }
Brian Salomonc2249852019-09-16 11:08:50 -04001311 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001312 } else {
Robert Phillips71f06f62020-05-15 16:37:21 +00001313 std::unique_ptr<char[]> zeros;
1314 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1315 for (int i = 0; i < mipLevelCount; ++i) {
1316 if (levelClearMask & (1U << i)) {
1317 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1318 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
1319 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1320 // smaller levels that need clearing.
1321 if (!zeros) {
1322 size_t bpp = GrColorTypeBytesPerPixel(colorType);
1323 size_t size = levelWidth * levelHeight * bpp;
1324 zeros.reset(new char[size]());
1325 }
1326 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1327 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1328 externalFormat, externalType, zeros.get()));
1329 }
1330 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001331 }
1332 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001333 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001334}
1335
Robert Phillips9f744f72019-12-19 19:14:33 -05001336sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001337 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001338 SkBudgeted budgeted,
1339 GrMipMapped mipMapped,
1340 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001341 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001342 // We don't support protected textures in GL.
1343 if (isProtected == GrProtected::kYes) {
1344 return nullptr;
1345 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001346 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001347 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001348 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001349 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001350 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001351 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001352 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001353 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001354 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001355 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001356 return nullptr;
1357 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001358
Robert Phillipsee946932019-12-18 11:16:17 -05001359 // Unbind this texture from the scratch texture unit.
1360 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1361
Robert Phillipse4720c62020-01-14 14:33:24 -05001362 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1363 ? GrMipMapsStatus::kValid
1364 : GrMipMapsStatus::kNotAllocated;
1365
1366 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001367 // The non-sampler params are still at their default values.
1368 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1369 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001370 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001371}
1372
Greg Danielc1ad77c2020-05-06 11:40:03 -04001373GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
1374 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
1375 GrProtected isProtected, sk_sp<GrRefCntedCallback> finishedCallback,
1376 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001377 // We don't support protected textures in GL.
1378 if (isProtected == GrProtected::kYes) {
1379 return {};
1380 }
1381
1382 this->handleDirtyContext();
1383
1384 GrGLFormat glFormat = format.asGLFormat();
1385 if (glFormat == GrGLFormat::kUnknown) {
1386 return {};
1387 }
1388
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001389 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001390 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001391 SkAutoMalloc am;
1392
1393 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1394 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1395 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001396 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001397 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001398 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1399 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001400
Robert Phillips99dead92020-01-27 16:11:57 -05001401 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1402 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001403
1404 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001405
1406 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1407
1408 rawData = (const char*) am.get();
1409 }
1410
1411 GrGLTextureInfo info;
1412 GrGLTextureParameters::SamplerOverriddenState initialState;
1413
1414 info.fTarget = GR_GL_TEXTURE_2D;
1415 info.fFormat = GrGLFormatToEnum(glFormat);
1416 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001417 mipMapped, &initialState,
1418 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001419 if (!info.fID) {
1420 return {};
1421 }
1422
1423 // Unbind this texture from the scratch texture unit.
1424 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1425
1426 auto parameters = sk_make_sp<GrGLTextureParameters>();
1427 // The non-sampler params are still at their default values.
1428 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1429 fResetTimestampForTextureParameters);
1430
1431 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1432 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001433}
1434
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001435namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001436
egdaniel8dc7c3a2015-04-16 11:22:42 -07001437const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001438
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001439void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001440 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001441
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001442 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001443 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444 (kUnknownBitCount == format->fTotalBits));
1445 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001446 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001447 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1448 (GrGLint*)&format->fStencilBits);
1449 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001450 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001451 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1452 (GrGLint*)&format->fTotalBits);
1453 format->fTotalBits += format->fStencilBits;
1454 } else {
1455 format->fTotalBits = format->fStencilBits;
1456 }
1457 }
1458}
1459}
1460
Brian Salomon5043f1f2019-07-11 21:27:54 -04001461int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001462 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001463 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1464
1465 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001466 // Default to unsupported, set this if we find a stencil format that works.
1467 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001468
Brian Salomond2a8ae22019-09-10 16:03:59 -04001469 GrGLuint colorID =
1470 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1471 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001472 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001473 }
egdanielff1d5472015-09-10 08:37:20 -07001474 // unbind the texture from the texture unit before binding it to the frame buffer
1475 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1476
1477 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001478 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001479 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001480 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001481 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001482 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1483 GR_GL_COLOR_ATTACHMENT0,
1484 GR_GL_TEXTURE_2D,
1485 colorID,
1486 0));
bsalomon30447372015-12-21 09:03:05 -08001487 GrGLuint sbRBID = 0;
1488 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001489
1490 // look over formats till I find a compatible one
1491 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001492 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001493 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001494 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1495 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001496 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1497 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1498 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001499 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001500 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001501 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001502 if (sFmt.fPacked) {
1503 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1504 GR_GL_DEPTH_ATTACHMENT,
1505 GR_GL_RENDERBUFFER, sbRBID));
1506 } else {
1507 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1508 GR_GL_DEPTH_ATTACHMENT,
1509 GR_GL_RENDERBUFFER, 0));
1510 }
1511 GrGLenum status;
1512 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1513 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1514 firstWorkingStencilFormatIndex = i;
1515 break;
1516 }
egdanielff1d5472015-09-10 08:37:20 -07001517 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1518 GR_GL_STENCIL_ATTACHMENT,
1519 GR_GL_RENDERBUFFER, 0));
1520 if (sFmt.fPacked) {
1521 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1522 GR_GL_DEPTH_ATTACHMENT,
1523 GR_GL_RENDERBUFFER, 0));
1524 }
egdanielff1d5472015-09-10 08:37:20 -07001525 }
1526 }
bsalomon30447372015-12-21 09:03:05 -08001527 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001528 }
1529 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001530 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1531 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001532 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001533 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001534 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001535}
1536
Brian Salomonea4ad302019-08-07 13:04:55 -04001537GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001538 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001539 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001540 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001541 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001542 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001543 if (format == GrGLFormat::kUnknown) {
1544 return 0;
1545 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001546 GrGLuint id = 0;
1547 GL_CALL(GenTextures(1, &id));
1548 if (!id) {
1549 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001550 }
1551
Brian Salomonea4ad302019-08-07 13:04:55 -04001552 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001553
Brian Salomonea4ad302019-08-07 13:04:55 -04001554 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1555
Robert Phillips42716d42019-12-16 12:19:54 -05001556 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001557 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1558 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001559 GL_CALL(DeleteTextures(1, &id));
1560 return 0;
1561 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001562 }
Robert Phillips42716d42019-12-16 12:19:54 -05001563
Brian Salomonea4ad302019-08-07 13:04:55 -04001564 return id;
1565}
1566
Robert Phillips2716daf2020-01-23 12:53:42 -05001567GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001568 GrGLFormat format,
1569 GrRenderable renderable,
1570 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001571 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001572 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001573 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001574
Brian Salomonea4ad302019-08-07 13:04:55 -04001575 GrGLuint id = 0;
1576 GL_CALL(GenTextures(1, &id));
1577
1578 if (!id) {
1579 return 0;
1580 }
1581
1582 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001583
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001584 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001585 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001586 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001587 }
1588
Brian Salomond2a8ae22019-09-10 16:03:59 -04001589 if (initialState) {
1590 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1591 } else {
1592 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001593 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001594
1595 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1596
1597 bool success = false;
1598 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001599 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001600 auto levelCount = std::max(mipLevelCount, 1);
1601 GrGLenum error =
1602 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1603 dimensions.width(), dimensions.height()));
1604 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001605 } else {
1606 GrGLenum externalFormat, externalType;
1607 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1608 GrGLenum error = GR_GL_NO_ERROR;
1609 if (externalFormat && externalType) {
1610 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1611 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001612 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1613 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001614 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1615 currentWidth, currentHeight, 0, externalFormat,
1616 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001617 }
Brian Salomond8575452020-02-25 12:13:29 -05001618 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001619 }
1620 }
1621 }
1622 if (success) {
1623 return id;
1624 }
1625 GL_CALL(DeleteTextures(1, &id));
1626 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001627}
1628
Chris Daltoneffee202019-07-01 22:28:03 -06001629GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1630 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001631 SkASSERT(width >= rt->width());
1632 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001633
egdaniel8dc7c3a2015-04-16 11:22:42 -07001634 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001635
Brian Salomond4764a12019-08-08 12:08:24 -04001636 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001637 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001638 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001639 }
egdanielff1d5472015-09-10 08:37:20 -07001640
1641 if (!sbDesc.fRenderbufferID) {
1642 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1643 }
1644 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001645 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001646 }
1647 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1648 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001649 // we do this "if" so that we don't call the multisample
1650 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001651 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001652 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1653 width, height)) {
1654 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1655 return nullptr;
1656 }
egdanielff1d5472015-09-10 08:37:20 -07001657 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001658 GrGLenum error = GL_ALLOC_CALL(
1659 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1660 if (error != GR_GL_NO_ERROR) {
1661 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1662 return nullptr;
1663 }
egdanielff1d5472015-09-10 08:37:20 -07001664 }
1665 fStats.incStencilAttachmentCreates();
1666 // After sized formats we attempt an unsized format and take
1667 // whatever sizes GL gives us. In that case we query for the size.
1668 GrGLStencilAttachment::Format format = sFmt;
1669 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001670 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1671 sbDesc,
1672 width,
1673 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001674 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001675 format);
1676 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001677}
1678
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001679////////////////////////////////////////////////////////////////////////////////
1680
Brian Salomondbf70722019-02-07 11:31:24 -05001681sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1682 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001683 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001684}
1685
Chris Dalton2e7ed262020-02-21 15:17:59 -07001686void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1687 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001688 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1689 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1690 fHWScissorSettings.fEnabled = kYes_TriState;
1691 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001692 } else {
1693 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1694 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1695 fHWScissorSettings.fEnabled = kNo_TriState;
1696 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001698}
Brian Salomond818ebf2018-07-02 14:08:49 +00001699
Chris Dalton2e7ed262020-02-21 15:17:59 -07001700void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1701 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001702 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001703 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1704 if (fHWScissorSettings.fRect != nativeScissor) {
1705 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1706 nativeScissor.fHeight));
1707 fHWScissorSettings.fRect = nativeScissor;
1708 }
joshualitt77b13072014-10-27 14:51:01 -07001709}
1710
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001711void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001712 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001713#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001714 typedef GrWindowRectsState::Mode Mode;
1715 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1716 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001717
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001718 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001719 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001720 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001721 }
1722
csmartdalton7535f412016-08-23 06:51:00 -07001723 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1724 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001725 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001726 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001727
Chris Daltond6cda8d2019-09-05 02:30:04 -06001728 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001729 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001730 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001731 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001732 }
1733
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001734 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001735 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001736
Greg Danielacd66b42019-05-22 16:29:12 -04001737 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001738#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001739}
1740
1741void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001742#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001743 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001744 return;
1745 }
1746 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001747 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001748#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001749}
1750
Chris Daltond42d2002020-02-28 12:05:04 -07001751bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1752 const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001753 this->handleDirtyContext();
1754
Chris Daltond42d2002020-02-28 12:05:04 -07001755 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001756 if (!program) {
1757 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1758 return false;
1759 }
1760
1761 this->flushProgram(std::move(program));
1762
Chris Daltond42d2002020-02-28 12:05:04 -07001763 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1764 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1765 }
1766
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001767 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001768 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001769 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001770
Chris Dalton20e7a532020-02-28 15:37:53 +00001771 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001772
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001773 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001774 GrStencilSettings stencil;
1775 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001776 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1777 stencil.reset(*programInfo.pipeline().getUserStencil(),
1778 programInfo.pipeline().hasStencilClip(),
1779 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001780 }
Robert Phillips901aff02019-10-08 12:32:56 -04001781 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001782 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001783 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1784 glRT, programInfo.origin());
1785 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001786 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001787 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001788
1789 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001790 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001791 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001792
Chris Dalton20e7a532020-02-28 15:37:53 +00001793 return true;
1794}
1795
1796void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1797 if (!program) {
1798 fHWProgram.reset();
1799 fHWProgramID = 0;
1800 return;
1801 }
1802 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1803 if (program == fHWProgram) {
1804 return;
1805 }
1806 auto id = program->programID();
1807 SkASSERT(id);
1808 GL_CALL(UseProgram(id));
1809 fHWProgram = std::move(program);
1810 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001811}
1812
1813void GrGLGpu::flushProgram(GrGLuint id) {
1814 SkASSERT(id);
1815 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001816 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001817 return;
1818 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001819 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001820 GL_CALL(UseProgram(id));
1821 fHWProgramID = id;
1822}
1823
Brian Salomonae64c192019-02-05 09:41:37 -05001824GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001825 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001826
cdaltone2e71c22016-04-07 18:13:29 -07001827 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001828 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001829 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001830 }
cdaltone2e71c22016-04-07 18:13:29 -07001831
Brian Salomonae64c192019-02-05 09:41:37 -05001832 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001833 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001834 if (!bufferState->fBufferZeroKnownBound) {
1835 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1836 bufferState->fBufferZeroKnownBound = true;
1837 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001838 }
Brian Salomondbf70722019-02-07 11:31:24 -05001839 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1840 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001841 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1842 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1843 bufferState->fBufferZeroKnownBound = false;
1844 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001845 }
1846
Brian Salomonae64c192019-02-05 09:41:37 -05001847 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001848}
Brian Salomond818ebf2018-07-02 14:08:49 +00001849
Brian Osman9a9baae2018-11-05 15:06:26 -05001850void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001851 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001852 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001853 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001854 SkASSERT(!this->caps()->performColorClearsAsDraws());
1855 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001856
Brian Salomon43f8bf02017-10-18 08:33:29 -04001857 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001858
Brian Salomon43f8bf02017-10-18 08:33:29 -04001859 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1860
Brian Salomond818ebf2018-07-02 14:08:49 +00001861 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001862 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001863 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001864 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001865 }
Greg Danielacd66b42019-05-22 16:29:12 -04001866 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001867 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001868 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001869 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001870 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001871}
1872
Robert Phillips95214472017-08-08 18:00:03 -04001873void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001874 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1875
Robert Phillips95214472017-08-08 18:00:03 -04001876 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001877 return;
1878 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001879
Chris Dalton674f77a2019-09-30 20:49:39 -06001880 // This should only be called internally when we know we have a stencil buffer.
1881 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001882
bsalomonb0bd4f62014-09-03 07:19:50 -07001883 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001884 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001885
Chris Dalton2e7ed262020-02-21 15:17:59 -07001886 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001887 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001888
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001889 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001890 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001891 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001892 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001893}
1894
Chris Daltonb50cc812019-10-14 16:29:21 -06001895static bool use_tiled_rendering(const GrGLCaps& glCaps,
1896 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1897 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1898 // Otherwise it's faster to just not use it.
1899 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1900 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1901}
1902
1903void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001904 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1905 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1906 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1907
1908 this->handleDirtyContext();
1909
1910 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1911 this->flushRenderTarget(glRT);
1912 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1913
Chris Daltonb50cc812019-10-14 16:29:21 -06001914 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1915 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1916 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1917 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1918 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1919 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1920 nativeBounds.fHeight, preserveMask));
1921 }
1922
Chris Dalton674f77a2019-09-30 20:49:39 -06001923 GrGLbitfield clearMask = 0;
1924 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1925 SkASSERT(!this->caps()->performColorClearsAsDraws());
1926 this->flushClearColor(colorLoadStore.fClearColor);
1927 this->flushColorWrite(true);
1928 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001929 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001930 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1931 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1932 GL_CALL(StencilMask(0xffffffff));
1933 GL_CALL(ClearStencil(0));
1934 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1935 }
1936 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001937 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001938 this->disableWindowRectangles();
1939 GL_CALL(Clear(clearMask));
1940 }
1941}
1942
1943void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1944 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1945 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1946 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1947
1948 this->handleDirtyContext();
1949
1950 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1951 // The framebuffer binding changed in the middle of a command buffer. We should have already
1952 // printed a warning during onFBOChanged.
1953 return;
1954 }
1955
1956 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1957 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1958
1959 SkSTArray<2, GrGLenum> discardAttachments;
1960 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1961 discardAttachments.push_back(
1962 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1963 }
1964 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1965 discardAttachments.push_back(
1966 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1967 }
1968
1969 if (!discardAttachments.empty()) {
1970 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1971 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1972 discardAttachments.begin()));
1973 } else {
1974 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1975 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1976 discardAttachments.begin()));
1977 }
1978 }
1979 }
1980
Chris Daltonb50cc812019-10-14 16:29:21 -06001981 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1982 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1983 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1984 // Handled by use_tiled_rendering().
1985 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1986 GL_CALL(EndTiling(preserveMask));
1987 }
1988
Chris Dalton674f77a2019-09-30 20:49:39 -06001989 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001990}
1991
csmartdalton29df7602016-08-31 11:55:52 -07001992void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1993 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001994 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07001995 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001996 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07001997 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001998
egdaniel8dc7c3a2015-04-16 11:22:42 -07001999 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002000 if (!sb) {
2001 // We should only get here if we marked a proxy as requiring a SB. However,
2002 // the SB creation could later fail. Likely clipping is going to go awry now.
2003 return;
2004 }
2005
bsalomon6bc1b5f2015-02-23 09:06:38 -08002006 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002007#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002008 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002009 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002010#else
2011 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002012 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002013 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002014 // changing the clip between stencil passes may or may not
2015 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002016 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002017#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002018 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002019 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002020 value = (1 << (stencilBitCount - 1));
2021 } else {
2022 value = 0;
2023 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002024 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002025 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002026
Greg Danielacd66b42019-05-22 16:29:12 -04002027 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002028 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002029
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002030 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002031 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002032 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002033 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002034}
2035
Brian Salomone05ba5a2019-04-08 11:59:07 -04002036bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002037 GrColorType surfaceColorType, GrColorType dstColorType,
2038 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002039 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002040
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002041 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002042 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002043 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002044 return false;
2045 }
Greg Danielba88ab62019-07-26 09:14:01 -04002046 GrGLenum externalFormat = 0;
2047 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002048 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2049 surfaceColorType,
2050 dstColorType,
2051 &externalFormat,
2052 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002053 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002054 return false;
2055 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002056
Brian Salomon71d9d842016-11-03 13:42:00 -04002057 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002058 if (renderTarget->numSamples() <= 1 ||
2059 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2060 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2061 this->flushRenderTargetNoColorWrites(renderTarget);
2062 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2063 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2064 return false;
2065 } else {
2066 SkASSERT(renderTarget->requiresManualMSAAResolve());
2067 // we don't track the state of the READ FBO ID.
2068 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002069 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002070 } else {
2071 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002072 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002073 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002074 }
2075
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002076 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002077 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002078
Brian Salomona6948702018-06-01 15:33:20 -04002079 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002080 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002081 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002082 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002083 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002084 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002085
Brian Osman1348ed02018-09-12 09:08:39 -04002086 bool reattachStencil = false;
2087 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2088 renderTarget &&
2089 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002090 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002091 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2092 reattachStencil = true;
2093 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2094 GR_GL_RENDERBUFFER, 0));
2095 }
2096
Chris Dalton76500e52019-09-05 02:13:05 -06002097 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002098 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002099
2100 if (reattachStencil) {
2101 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2102 renderTarget->renderTargetPriv().getStencilAttachment());
2103 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2104 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2105 }
2106
Brian Salomon26de56e2019-04-10 12:14:26 -04002107 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002108 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002109 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2110 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002111
Brian Salomone05ba5a2019-04-08 11:59:07 -04002112 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002113 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002114 }
2115 return true;
2116}
2117
2118bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002119 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2120 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002121 SkASSERT(surface);
2122
Brian Salomonb28cb682019-07-26 12:48:47 -04002123 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002124
Brian Salomon26de56e2019-04-10 12:14:26 -04002125 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2126 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002127
Brian Salomon1047a492019-07-02 12:25:21 -04002128 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002129 rowPixelWidth = width;
2130 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002131 SkASSERT(!(rowBytes % bytesPerPixel));
2132 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002133 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002134 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2135 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002136}
2137
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002138GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002139 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002140 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002141 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002142 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002143 if (!fCachedOpsRenderPass) {
2144 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002145 }
2146
Chris Daltonb50cc812019-10-14 16:29:21 -06002147 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002148 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002149}
2150
Brian Salomon1fabd512018-02-09 09:54:25 -05002151void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002152 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002153 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002154 this->didWriteToSurface(target, origin, &bounds);
2155}
bsalomon6ba6fa12015-03-04 11:57:37 -08002156
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002157void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2158 this->flushRenderTargetNoColorWrites(target);
2159 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002160}
2161
Brian Osman9aa30c62018-07-02 15:21:46 -04002162void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002163 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002164 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002165 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002166 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002167#ifdef SK_DEBUG
2168 // don't do this check in Chromium -- this is causing
2169 // lots of repeated command buffer flushes when the compositor is
2170 // rendering with Ganesh, which is really slow; even too slow for
2171 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002172 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002173 GrGLenum status;
2174 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2175 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2176 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2177 }
bsalomon160f24c2015-03-17 15:55:42 -07002178 }
egdanield803f272015-03-18 13:01:52 -07002179#endif
2180 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002181 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002182 }
2183
brianosman35b784d2016-05-05 11:52:53 -07002184 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002185 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002186 }
Brian Salomon9b553272020-01-24 14:38:37 -05002187
2188 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2189 GrGLint format;
2190 GrGLint type;
2191 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2192 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2193 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2194 }
bsalomon083617b2016-02-12 12:10:14 -08002195}
bsalomona9909122016-01-23 10:41:40 -08002196
brianosman33f6b3f2016-06-02 05:49:21 -07002197void GrGLGpu::flushFramebufferSRGB(bool enable) {
2198 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2199 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2200 fHWSRGBFramebuffer = kYes_TriState;
2201 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2202 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2203 fHWSRGBFramebuffer = kNo_TriState;
2204 }
2205}
2206
Greg Danielacd66b42019-05-22 16:29:12 -04002207void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002208 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002209 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002210 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002211 fHWViewport = viewport;
2212 }
2213}
2214
Chris Dalton33db9fc2020-02-25 18:52:12 -07002215GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002216 fStats.incNumDraws();
2217
Chris Dalton2e7ed262020-02-21 15:17:59 -07002218 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2219 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2220 GL_CALL(Enable(GR_GL_CULL_FACE));
2221 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002222 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002223 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002224
Chris Dalton3809bab2017-06-13 10:55:06 -06002225 switch (primitiveType) {
2226 case GrPrimitiveType::kTriangles:
2227 return GR_GL_TRIANGLES;
2228 case GrPrimitiveType::kTriangleStrip:
2229 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002230 case GrPrimitiveType::kPoints:
2231 return GR_GL_POINTS;
2232 case GrPrimitiveType::kLines:
2233 return GR_GL_LINES;
2234 case GrPrimitiveType::kLineStrip:
2235 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002236 case GrPrimitiveType::kPatches:
2237 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002238 case GrPrimitiveType::kPath:
2239 SK_ABORT("non-mesh-based GrPrimitiveType");
2240 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002241 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002242 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002243}
2244
Chris Dalton16a33c62019-09-24 22:19:17 -06002245void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002246 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002247 // Some extensions automatically resolves the texture when it is read.
2248 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2249
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002250 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002251 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2252 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2253 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2254 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002255
Chris Daltonc139d082019-09-26 14:04:24 -06002256 // make sure we go through flushRenderTarget() since we've modified
2257 // the bound DRAW FBO ID.
2258 fHWBoundRenderTargetUniqueID.makeInvalid();
2259 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2260 // Apple's extension uses the scissor as the blit bounds.
2261 GrScissorState scissorState;
2262 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002263 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2264 // happens inside flushScissor since resolveRect is already in native device coordinates.
2265 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002266 this->disableWindowRectangles();
2267 GL_CALL(ResolveMultisampleFramebuffer());
2268 } else {
2269 int l, b, r, t;
2270 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2271 this->glCaps().blitFramebufferSupportFlags()) {
2272 l = 0;
2273 b = 0;
2274 r = target->width();
2275 t = target->height();
2276 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002277 l = resolveRect.x();
2278 b = resolveRect.y();
2279 r = resolveRect.x() + resolveRect.width();
2280 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002281 }
Chris Daltonc139d082019-09-26 14:04:24 -06002282
2283 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002284 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002285 this->disableWindowRectangles();
2286 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 +00002287 }
2288}
2289
bsalomon@google.com411dad02012-06-05 20:24:20 +00002290namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002291
bsalomon@google.com411dad02012-06-05 20:24:20 +00002292
2293GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002294 static const GrGLenum gTable[kGrStencilOpCount] = {
2295 GR_GL_KEEP, // kKeep
2296 GR_GL_ZERO, // kZero
2297 GR_GL_REPLACE, // kReplace
2298 GR_GL_INVERT, // kInvert
2299 GR_GL_INCR_WRAP, // kIncWrap
2300 GR_GL_DECR_WRAP, // kDecWrap
2301 GR_GL_INCR, // kIncClamp
2302 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002303 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002304 static_assert(0 == (int)GrStencilOp::kKeep);
2305 static_assert(1 == (int)GrStencilOp::kZero);
2306 static_assert(2 == (int)GrStencilOp::kReplace);
2307 static_assert(3 == (int)GrStencilOp::kInvert);
2308 static_assert(4 == (int)GrStencilOp::kIncWrap);
2309 static_assert(5 == (int)GrStencilOp::kDecWrap);
2310 static_assert(6 == (int)GrStencilOp::kIncClamp);
2311 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002312 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2313 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002314}
2315
2316void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002317 const GrStencilSettings::Face& face,
2318 GrGLenum glFace) {
2319 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2320 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2321 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002322
cdalton93a379b2016-05-11 13:58:08 -07002323 GrGLint ref = face.fRef;
2324 GrGLint mask = face.fTestMask;
2325 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002326
2327 if (GR_GL_FRONT_AND_BACK == glFace) {
2328 // we call the combined func just in case separate stencil is not
2329 // supported.
2330 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2331 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002332 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002333 } else {
2334 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2335 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002336 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002337 }
2338}
2339}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002340
Chris Dalton71713f62019-04-18 12:41:03 -06002341void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002342 if (stencilSettings.isDisabled()) {
2343 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002344 } else if (fHWStencilSettings != stencilSettings ||
2345 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002346 if (kYes_TriState != fHWStencilTestEnabled) {
2347 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002348
csmartdaltonc7d85332016-10-26 10:13:46 -07002349 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002350 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002351 if (!stencilSettings.isTwoSided()) {
2352 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2353 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002354 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002355 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2356 GR_GL_FRONT);
2357 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2358 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002359 }
joshualitta58fe352014-10-27 08:39:00 -07002360 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002361 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002362 }
2363}
2364
csmartdaltonc7d85332016-10-26 10:13:46 -07002365void GrGLGpu::disableStencil() {
2366 if (kNo_TriState != fHWStencilTestEnabled) {
2367 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002368
csmartdaltonc7d85332016-10-26 10:13:46 -07002369 fHWStencilTestEnabled = kNo_TriState;
2370 fHWStencilSettings.invalidate();
2371 }
2372}
2373
Chris Dalton4c56b032019-03-04 12:28:42 -07002374void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002375 // rt is only optional if useHWAA is false.
2376 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002377#ifdef SK_DEBUG
2378 if (useHWAA && rt->numSamples() <= 1) {
2379 SkASSERT(this->caps()->mixedSamplesSupport());
2380 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2381 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2382 }
2383#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002384
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002385 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002386 if (useHWAA) {
2387 if (kYes_TriState != fMSAAEnabled) {
2388 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2389 fMSAAEnabled = kYes_TriState;
2390 }
2391 } else {
2392 if (kNo_TriState != fMSAAEnabled) {
2393 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2394 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002395 }
2396 }
2397 }
2398}
2399
Chris Daltonce425af2019-12-16 10:39:03 -07002400void GrGLGpu::flushConservativeRasterState(bool enabled) {
2401 if (this->caps()->conservativeRasterSupport()) {
2402 if (enabled) {
2403 if (kYes_TriState != fHWConservativeRasterEnabled) {
2404 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2405 fHWConservativeRasterEnabled = kYes_TriState;
2406 }
2407 } else {
2408 if (kNo_TriState != fHWConservativeRasterEnabled) {
2409 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2410 fHWConservativeRasterEnabled = kNo_TriState;
2411 }
2412 }
2413 }
2414}
2415
Chris Dalton1215cda2019-12-17 21:44:04 -07002416void GrGLGpu::flushWireframeState(bool enabled) {
2417 if (this->caps()->wireframeSupport()) {
2418 if (this->caps()->wireframeMode() || enabled) {
2419 if (kYes_TriState != fHWWireframeEnabled) {
2420 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2421 fHWWireframeEnabled = kYes_TriState;
2422 }
2423 } else {
2424 if (kNo_TriState != fHWWireframeEnabled) {
2425 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2426 fHWWireframeEnabled = kNo_TriState;
2427 }
2428 }
2429 }
2430}
2431
Chris Daltonbbb3f642019-07-24 12:25:08 -04002432void GrGLGpu::flushBlendAndColorWrite(
2433 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2434 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2435 // We need to work around a driver bug by using a blend state that preserves the dst color,
2436 // rather than disabling color writes.
2437 GrXferProcessor::BlendInfo preserveDstBlend;
2438 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2439 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2440 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2441 return;
2442 }
bsalomonf7cc8772015-05-11 11:21:14 -07002443
cdalton8917d622015-05-06 13:40:21 -07002444 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002445 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2446 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002447
2448 // Any optimization to disable blending should have already been applied and
2449 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002450 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2451 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002452
egdanielb414f252014-07-29 13:15:47 -07002453 if (blendOff) {
2454 if (kNo_TriState != fHWBlendState.fEnabled) {
2455 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002456
2457 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2458 // https://code.google.com/p/skia/issues/detail?id=3943
2459 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2460 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2461 SkASSERT(this->caps()->advancedBlendEquationSupport());
2462 // Set to any basic blending equation.
2463 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2464 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2465 fHWBlendState.fEquation = blend_equation;
2466 }
2467
egdanielb414f252014-07-29 13:15:47 -07002468 fHWBlendState.fEnabled = kNo_TriState;
2469 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002470 } else {
2471 if (kYes_TriState != fHWBlendState.fEnabled) {
2472 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002473
Chris Daltonbbb3f642019-07-24 12:25:08 -04002474 fHWBlendState.fEnabled = kYes_TriState;
2475 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002476
Chris Daltonbbb3f642019-07-24 12:25:08 -04002477 if (fHWBlendState.fEquation != equation) {
2478 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2479 fHWBlendState.fEquation = equation;
2480 }
cdalton8917d622015-05-06 13:40:21 -07002481
Chris Daltonbbb3f642019-07-24 12:25:08 -04002482 if (GrBlendEquationIsAdvanced(equation)) {
2483 SkASSERT(this->caps()->advancedBlendEquationSupport());
2484 // Advanced equations have no other blend state.
2485 return;
2486 }
cdalton8917d622015-05-06 13:40:21 -07002487
Chris Daltonbbb3f642019-07-24 12:25:08 -04002488 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2489 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2490 gXfermodeCoeff2Blend[dstCoeff]));
2491 fHWBlendState.fSrcCoeff = srcCoeff;
2492 fHWBlendState.fDstCoeff = dstCoeff;
2493 }
cdalton8917d622015-05-06 13:40:21 -07002494
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002495 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002496 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2497 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2498 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2499 fHWBlendState.fConstColor = blendConst;
2500 fHWBlendState.fConstColorValid = true;
2501 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002502 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002503 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002504
2505 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002506}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002507
Brian Salomondc829942018-10-23 16:07:24 -04002508static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002509 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002510 switch (swizzle[i]) {
2511 case 'r': glValues[i] = GR_GL_RED; break;
2512 case 'g': glValues[i] = GR_GL_GREEN; break;
2513 case 'b': glValues[i] = GR_GL_BLUE; break;
2514 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002515 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002516 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002517 default: SK_ABORT("Unsupported component");
2518 }
Brian Salomon327955b2018-10-22 15:39:22 +00002519 }
2520}
2521
Greg Daniel2c3398d2019-06-19 11:58:01 -04002522void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2523 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002524 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002525
reed856e9d92015-09-30 12:21:45 -07002526#ifdef SK_DEBUG
2527 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002528 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002529 const int w = texture->width();
2530 const int h = texture->height();
2531 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2532 }
2533 }
2534#endif
2535
Robert Phillips294870f2016-11-11 12:38:40 -05002536 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002537 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002538 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002539 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002540 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002541 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002542 }
2543
Brian Salomondc829942018-10-23 16:07:24 -04002544 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002545 if (!this->caps()->mipMapSupport() ||
2546 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002547 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002548 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002549 }
bsalomonefd7d452014-10-23 14:17:46 -07002550
brianosman33f6b3f2016-06-02 05:49:21 -07002551#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002552 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002553 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002554 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002555 }
2556#endif
2557
Brian Salomone2826ab2019-06-04 15:58:31 -04002558 auto timestamp = texture->parameters()->resetTimestamp();
2559 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002560
Brian Salomone2826ab2019-06-04 15:58:31 -04002561 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2562 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002563 if (fSamplerObjectCache) {
2564 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Brian Salomon1908bb82020-05-13 12:22:54 -04002565 if (this->glCaps().mustSetTexParameterMinFilterToEnableMipMapping()) {
2566 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
2567 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2568 texture->parameters()->samplerOverriddenState();
2569 if (setAll || oldSamplerState.fMinFilter != GR_GL_LINEAR_MIPMAP_LINEAR) {
2570 this->setTextureUnit(unitIdx);
2571 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER,
2572 GR_GL_LINEAR_MIPMAP_LINEAR));
2573 newSamplerState = oldSamplerState;
2574 newSamplerState.fMinFilter = GR_GL_LINEAR_MIPMAP_LINEAR;
2575 samplerStateToRecord = &newSamplerState;
2576 }
2577 }
2578 }
Brian Salomondc829942018-10-23 16:07:24 -04002579 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002580 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2581 texture->parameters()->samplerOverriddenState();
2582 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002583
Brian Salomone2826ab2019-06-04 15:58:31 -04002584 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2585 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002586
Brian Salomone2826ab2019-06-04 15:58:31 -04002587 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2588 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002589
2590 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002591 newSamplerState.fMinLOD = -1000.f;
2592 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002593
Brian Salomone2826ab2019-06-04 15:58:31 -04002594 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002595 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002596 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002597 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002598 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002599 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002600 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002601 }
Mike Klein1bccae52018-10-19 11:38:44 +00002602 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002603 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002604 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002605 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002606 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002607 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002608 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002609 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002610 }
2611 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002612 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002613 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002614 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002615 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002616 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002617 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002618 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002619 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002620 if (this->glCaps().clampToBorderSupport()) {
2621 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002622 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002623 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002624 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2625 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002626 }
2627 }
Brian Salomondc829942018-10-23 16:07:24 -04002628 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002629 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2630 newNonsamplerState.fBaseMipMapLevel = 0;
2631 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002632
Brian Salomone2826ab2019-06-04 15:58:31 -04002633 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2634 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002635 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002636 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2637 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002638 GrGLenum glValues[4];
2639 get_gl_swizzle_values(swizzle, glValues);
2640 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002641 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002642 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002643 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2644 reinterpret_cast<const GrGLint*>(glValues)));
2645 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002646 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2647 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2648 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2649 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2650 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002651 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002652 }
2653 }
Brian Salomondc829942018-10-23 16:07:24 -04002654 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002655 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2656 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2657 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002658 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002659 this->setTextureUnit(unitIdx);
2660 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002662 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002663 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002664 this->setTextureUnit(unitIdx);
2665 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002666 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002667 }
Brian Salomon327955b2018-10-22 15:39:22 +00002668 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002669 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2670 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002671}
2672
Brian Salomon1f05d452019-02-08 12:33:08 -05002673void GrGLGpu::onResetTextureBindings() {
2674 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2675 GR_GL_TEXTURE_EXTERNAL};
2676 for (int i = 0; i < this->numTextureUnits(); ++i) {
2677 this->setTextureUnit(i);
2678 for (auto target : kTargets) {
2679 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2680 GL_CALL(BindTexture(target, 0));
2681 }
2682 }
2683 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2684 }
2685}
2686
Chris Dalton5a2f9622019-12-27 14:56:38 -07002687void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2688 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2689 if (fHWPatchVertexCount != count) {
2690 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2691 fHWPatchVertexCount = count;
2692 }
2693}
2694
egdaniel080e6732014-12-22 07:35:52 -08002695void GrGLGpu::flushColorWrite(bool writeColor) {
2696 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002697 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002698 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2699 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002700 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002701 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002702 } else {
2703 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002704 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002705 fHWWriteToColor = kYes_TriState;
2706 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002707 }
bsalomon3e791242014-12-17 13:43:13 -08002708}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002709
Chris Dalton674f77a2019-09-30 20:49:39 -06002710void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2711 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2712 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2713 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2714 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2715 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2716 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2717 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002718 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2719 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2720 GL_CALL(ClearColor(r, g, b, a));
2721 fHWClearColor[0] = r;
2722 fHWClearColor[1] = g;
2723 fHWClearColor[2] = b;
2724 fHWClearColor[3] = a;
2725 }
2726}
2727
bsalomon861e1032014-12-16 07:33:49 -08002728void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002729 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002730 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002731 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002732 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002733 }
2734}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002735
Brian Salomond978b902019-02-07 15:09:18 -05002736void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002737 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002738 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002739 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2740 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2741 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002742 }
Brian Salomond978b902019-02-07 15:09:18 -05002743 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2744 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002745 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002746 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002747}
2748
Brian Salomone5e7eb12016-10-14 16:18:33 -04002749// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002750static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2751 const GrSurface* src,
2752 const SkIRect& srcRect,
2753 const SkIPoint& dstPoint,
2754 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002755 int dstSampleCnt = 0;
2756 int srcSampleCnt = 0;
2757 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002758 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002759 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002760 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002761 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002762 }
2763 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2764 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2765
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002766 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2767 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2768
Brian Salomone5e7eb12016-10-14 16:18:33 -04002769 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002770 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002771
Greg Daniel46cfbc62019-06-07 11:43:30 -04002772 GrTextureType dstTexType;
2773 GrTextureType* dstTexTypePtr = nullptr;
2774 GrTextureType srcTexType;
2775 GrTextureType* srcTexTypePtr = nullptr;
2776 if (dstTex) {
2777 dstTexType = dstTex->texturePriv().textureType();
2778 dstTexTypePtr = &dstTexType;
2779 }
2780 if (srcTex) {
2781 srcTexType = srcTex->texturePriv().textureType();
2782 srcTexTypePtr = &srcTexType;
2783 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002784
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002785 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2786 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002787 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002788}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002789
Brian Osmana9c8a052018-01-19 10:31:56 -05002790static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2791 // A RT has a separate MSAA renderbuffer if:
2792 // 1) It's multisampled
2793 // 2) We're using an extension with separate MSAA renderbuffers
2794 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002795 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002796}
2797
Greg Daniel46cfbc62019-06-07 11:43:30 -04002798static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002799 const GrGLCaps& caps) {
2800
bsalomon@google.comeb851172013-04-15 13:51:00 +00002801 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002802 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002803 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002804 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002805
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002806 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2807 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2808
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002809 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2810 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2811
Greg Daniel46cfbc62019-06-07 11:43:30 -04002812 GrTextureType dstTexType;
2813 GrTextureType* dstTexTypePtr = nullptr;
2814 GrTextureType srcTexType;
2815 GrTextureType* srcTexTypePtr = nullptr;
2816 if (dstTex) {
2817 dstTexType = dstTex->texturePriv().textureType();
2818 dstTexTypePtr = &dstTexType;
2819 }
2820 if (srcTex) {
2821 srcTexType = srcTex->texturePriv().textureType();
2822 srcTexTypePtr = &srcTexType;
2823 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002824
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002825 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2826 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002827}
2828
Greg Danielacd66b42019-05-22 16:29:12 -04002829// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002830void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002831 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002832 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002833 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002834 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002835 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2836 GrGLuint texID = texture->textureID();
2837 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002838 GrGLuint* tempFBOID;
2839 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002840
egdanield803f272015-03-18 13:01:52 -07002841 if (0 == *tempFBOID) {
2842 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002843 }
2844
Adrienne Walker4ee88512018-05-17 11:37:14 -07002845 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002846 GR_GL_CALL(
2847 this->glInterface(),
2848 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2849 if (mipLevel == 0) {
2850 texture->baseLevelWasBoundToFBO();
2851 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002852 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002853 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002854 }
egdaniel0f5f9672015-02-03 11:10:51 -08002855}
2856
Brian Salomond2a8ae22019-09-10 16:03:59 -04002857void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002858 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002859 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002860 SkASSERT(surface->asTexture());
2861 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2862 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2863 GR_GL_COLOR_ATTACHMENT0,
2864 textureTarget,
2865 0,
2866 0));
2867 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002868}
2869
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002870void GrGLGpu::onFBOChanged() {
2871 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2872 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002873 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002874 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002875#ifdef SK_DEBUG
2876 if (fIsExecutingCommandBuffer_DebugOnly) {
2877 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2878 "This will severely hurt performance.\n");
2879 }
2880#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002881}
2882
Adrienne Walker4ee88512018-05-17 11:37:14 -07002883void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2884 fStats.incRenderTargetBinds();
2885 GL_CALL(BindFramebuffer(target, fboid));
2886 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2887 fBoundDrawFramebuffer = fboid;
2888 }
2889
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002890 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2891 // The driver forgets the correct scissor when modifying the FBO binding.
2892 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002893 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002894 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002895 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002896 }
2897
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002898 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002899}
2900
Adrienne Walker4ee88512018-05-17 11:37:14 -07002901void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002902 // We're relying on the GL state shadowing being correct in the workaround code below so we
2903 // need to handle a dirty context.
2904 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002905 if (fboid == fBoundDrawFramebuffer &&
2906 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2907 // This workaround only applies to deleting currently bound framebuffers
2908 // on Adreno 420. Because this is a somewhat rare case, instead of
2909 // tracking all the attachments of every framebuffer instead just always
2910 // unbind all attachments.
2911 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2912 GR_GL_RENDERBUFFER, 0));
2913 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2914 GR_GL_RENDERBUFFER, 0));
2915 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2916 GR_GL_RENDERBUFFER, 0));
2917 }
2918
2919 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002920
2921 // Deleting the currently bound framebuffer rebinds to 0.
2922 if (fboid == fBoundDrawFramebuffer) {
2923 this->onFBOChanged();
2924 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002925}
2926
Greg Daniel46cfbc62019-06-07 11:43:30 -04002927bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002928 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002929 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2930 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2931 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002932 auto dstFormat = dst->backendFormat().asGLFormat();
2933 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002934 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002935 return true;
2936 }
2937 }
cblume61214052016-01-26 09:10:48 -08002938
Greg Daniel46cfbc62019-06-07 11:43:30 -04002939 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2940 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002941 return true;
2942 }
2943
Greg Daniel46cfbc62019-06-07 11:43:30 -04002944 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2945 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002946 }
2947
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002948 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002949 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002950 return true;
2951 }
bsalomon083617b2016-02-12 12:10:14 -08002952 }
2953
bsalomon6df86402015-06-01 10:41:49 -07002954 return false;
2955}
2956
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002957bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002958 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002959
2960 int progIdx = TextureToCopyProgramIdx(srcTex);
2961 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2962 GrSLType samplerType =
2963 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2964
2965 if (!fCopyProgramArrayBuffer) {
2966 static const GrGLfloat vdata[] = {
2967 0, 0,
2968 0, 1,
2969 1, 0,
2970 1, 1
2971 };
2972 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2973 kStatic_GrAccessPattern, vdata);
2974 }
2975 if (!fCopyProgramArrayBuffer) {
2976 return false;
2977 }
2978
2979 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2980 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
2981 if (!fCopyPrograms[progIdx].fProgram) {
2982 return false;
2983 }
2984
Ben Wagnerdabd98c2020-03-25 15:17:18 -04002985 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002986 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04002987 GrShaderVar::TypeModifier::Uniform);
2988 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
2989 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
2990 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
2991 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002992
Greg Daniel9e3169b2019-06-06 14:38:17 -04002993 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002994 if (shaderCaps->noperspectiveInterpolationSupport()) {
2995 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
2996 vshaderTxt.appendf("#extension %s : require\n", extension);
2997 }
2998 vTexCoord.addModifier("noperspective");
2999 }
3000
3001 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3002 vshaderTxt.append(";");
3003 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3004 vshaderTxt.append(";");
3005 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3006 vshaderTxt.append(";");
3007 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3008 vshaderTxt.append(";");
3009
3010 vshaderTxt.append(
3011 "// Copy Program VS\n"
3012 "void main() {"
3013 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3014 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3015 " sk_Position.zw = half2(0, 1);"
3016 "}"
3017 );
3018
Greg Daniel9e3169b2019-06-06 14:38:17 -04003019 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003020 if (shaderCaps->noperspectiveInterpolationSupport()) {
3021 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3022 fshaderTxt.appendf("#extension %s : require\n", extension);
3023 }
3024 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003025 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003026 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3027 fshaderTxt.append(";");
3028 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3029 fshaderTxt.append(";");
3030 fshaderTxt.appendf(
3031 "// Copy Program FS\n"
3032 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003033 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003034 "}"
3035 );
3036
3037 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3038 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3039 SkSL::Program::Settings settings;
3040 settings.fCaps = shaderCaps;
3041 SkSL::String glsl;
3042 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3043 sksl, settings, &glsl, errorHandler);
3044 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3045 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3046 SkASSERT(program->fInputs.isEmpty());
3047
3048 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3049 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3050 errorHandler);
3051 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3052 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3053 errorHandler);
3054 SkASSERT(program->fInputs.isEmpty());
3055
3056 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3057
3058 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3059 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3060 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3061 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3062 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3063 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3064
3065 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3066
3067 GL_CALL(DeleteShader(vshader));
3068 GL_CALL(DeleteShader(fshader));
3069
3070 return true;
3071}
3072
brianosman33f6b3f2016-06-02 05:49:21 -07003073bool GrGLGpu::createMipmapProgram(int progIdx) {
3074 const bool oddWidth = SkToBool(progIdx & 0x2);
3075 const bool oddHeight = SkToBool(progIdx & 0x1);
3076 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3077
Brian Salomon1edc5b92016-11-29 13:43:46 -05003078 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003079
3080 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3081 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3082 if (!fMipmapPrograms[progIdx].fProgram) {
3083 return false;
3084 }
3085
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003086 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003087 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003088 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003089 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003090 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003091 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003092 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003093 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3094 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3095 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3096 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003097 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003098 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003099
Ethan Nicholasfc994162019-06-06 10:04:27 -04003100 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003101 if (shaderCaps->noperspectiveInterpolationSupport()) {
3102 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003103 vshaderTxt.appendf("#extension %s : require\n", extension);
3104 }
3105 vTexCoords[0].addModifier("noperspective");
3106 vTexCoords[1].addModifier("noperspective");
3107 vTexCoords[2].addModifier("noperspective");
3108 vTexCoords[3].addModifier("noperspective");
3109 }
3110
Brian Salomon1edc5b92016-11-29 13:43:46 -05003111 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003112 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003113 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003114 vshaderTxt.append(";");
3115 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003116 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003117 vshaderTxt.append(";");
3118 }
3119
3120 vshaderTxt.append(
3121 "// Mipmap Program VS\n"
3122 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003123 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3124 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003125 );
3126
3127 // Insert texture coordinate computation:
3128 if (oddWidth && oddHeight) {
3129 vshaderTxt.append(
3130 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003131 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3132 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003133 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3134 );
3135 } else if (oddWidth) {
3136 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003137 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3138 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003139 );
3140 } else if (oddHeight) {
3141 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003142 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3143 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003144 );
3145 } else {
3146 vshaderTxt.append(
3147 " v_texCoord0 = a_vertex.xy;"
3148 );
3149 }
3150
3151 vshaderTxt.append("}");
3152
Ethan Nicholasfc994162019-06-06 10:04:27 -04003153 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003154 if (shaderCaps->noperspectiveInterpolationSupport()) {
3155 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003156 fshaderTxt.appendf("#extension %s : require\n", extension);
3157 }
3158 }
brianosman33f6b3f2016-06-02 05:49:21 -07003159 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003160 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003161 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003162 fshaderTxt.append(";");
3163 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003164 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003165 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003166 fshaderTxt.append(
3167 "// Mipmap Program FS\n"
3168 "void main() {"
3169 );
3170
3171 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003172 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003173 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3174 " sample(u_texture, v_texCoord1) + "
3175 " sample(u_texture, v_texCoord2) + "
3176 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003177 );
3178 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003179 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003180 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3181 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003182 );
3183 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003184 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003185 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003186 );
3187 }
3188
3189 fshaderTxt.append("}");
3190
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003191 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003192 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003193 SkSL::Program::Settings settings;
3194 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003195 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003196 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003197 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003198 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003199 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003200 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003201
Brian Osman6c431d52019-04-15 16:31:54 -04003202 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003203 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3204 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003205 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003206 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003207 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003208 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003209
3210 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3211
3212 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3213 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3214 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3215 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3216
3217 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3218
3219 GL_CALL(DeleteShader(vshader));
3220 GL_CALL(DeleteShader(fshader));
3221
3222 return true;
3223}
3224
Greg Daniel46cfbc62019-06-07 11:43:30 -04003225bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003226 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003227 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3228 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3229 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3230 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003231 return false;
3232 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003233 int progIdx = TextureToCopyProgramIdx(srcTex);
3234 if (!dstRT) {
3235 SkASSERT(dstTex);
3236 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3237 return false;
3238 }
3239 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003240 if (!fCopyPrograms[progIdx].fProgram) {
3241 if (!this->createCopyProgram(srcTex)) {
3242 SkDebugf("Failed to create copy program.\n");
3243 return false;
3244 }
3245 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003246 int w = srcRect.width();
3247 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003248 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003249 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003250 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003251 this->flushViewport(dst->width(), dst->height());
3252 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003253 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003254 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003255 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003256 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3257 attribs->enableVertexArrays(this, 1);
3258 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3259 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003260 // dst rect edges in NDC (-1 to 1)
3261 int dw = dst->width();
3262 int dh = dst->height();
3263 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3264 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3265 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3266 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3268 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3269 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3270 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3271 int sw = src->width();
3272 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003273 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3274 // src rect edges in normalized texture space (0 to 1)
3275 sx0 /= sw;
3276 sx1 /= sw;
3277 sy0 /= sh;
3278 sy1 /= sh;
3279 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003280 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3281 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3282 sx1 - sx0, sy1 - sy0, sx0, sy0));
3283 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003284 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003285 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003286 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003287 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003288 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003289 this->disableWindowRectangles();
3290 this->disableStencil();
3291 if (this->glCaps().srgbWriteControl()) {
3292 this->flushFramebufferSRGB(true);
3293 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003295 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003296 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3297 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003298 return true;
3299}
3300
Greg Daniel46cfbc62019-06-07 11:43:30 -04003301void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003302 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003303 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003304 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003305 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003306 SkASSERT(dstTex);
3307 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003308 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003309
Brian Salomond978b902019-02-07 15:09:18 -05003310 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003311 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003312 dstPoint.fX, dstPoint.fY,
3313 srcRect.fLeft, srcRect.fTop,
3314 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003315 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003316 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3317 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003318 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3319 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003320}
3321
Greg Daniel46cfbc62019-06-07 11:43:30 -04003322bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003323 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003324 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003325 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3326 srcRect.width(), srcRect.height());
3327 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003328 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003329 return false;
mtklein404b3b22015-05-18 09:29:10 -07003330 }
bsalomon5df6fee2015-05-18 06:26:15 -07003331 }
bsalomon6df86402015-06-01 10:41:49 -07003332
Brian Salomond2a8ae22019-09-10 16:03:59 -04003333 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3334 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003335 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003336 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003337
3338 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003339 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003340 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003341
Greg Daniel46cfbc62019-06-07 11:43:30 -04003342 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3343 srcRect.fTop,
3344 srcRect.fRight,
3345 srcRect.fBottom,
3346 dstRect.fLeft,
3347 dstRect.fTop,
3348 dstRect.fRight,
3349 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003350 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003351 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3352 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003353
3354 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3355 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003356 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003357}
3358
Brian Salomon930f9392018-06-20 16:25:26 -04003359bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3360 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003361 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003362 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003363 return false;
3364 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003365 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003366 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3367 // Uses draw calls to do a series of downsample operations to successive mips.
3368
3369 // The manual approach requires the ability to limit which level we're sampling and that the
3370 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003371 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003372 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003373 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003374 GL_CALL(GenerateMipmap(glTex->target()));
3375 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003376 }
3377
brianosman33f6b3f2016-06-02 05:49:21 -07003378 int width = texture->width();
3379 int height = texture->height();
3380 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003381 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003382
3383 // Create (if necessary), then bind temporary FBO:
3384 if (0 == fTempDstFBOID) {
3385 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3386 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003387 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003388 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003389
3390 // Bind the texture, to get things configured for filtering.
3391 // We'll be changing our base level further below:
3392 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003393 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003394 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003395
3396 // Vertex data:
3397 if (!fMipmapProgramArrayBuffer) {
3398 static const GrGLfloat vdata[] = {
3399 0, 0,
3400 0, 1,
3401 1, 0,
3402 1, 1
3403 };
Brian Salomonae64c192019-02-05 09:41:37 -05003404 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003405 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003406 }
3407 if (!fMipmapProgramArrayBuffer) {
3408 return false;
3409 }
3410
3411 fHWVertexArrayState.setVertexArrayID(this, 0);
3412
3413 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003414 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003415 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003416 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003417
3418 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003419 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003420 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003421 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003422 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003423 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003424
3425 // Do all the blits:
3426 width = texture->width();
3427 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003428
brianosman33f6b3f2016-06-02 05:49:21 -07003429 for (GrGLint level = 1; level < levelCount; ++level) {
3430 // Get and bind the program for this particular downsample (filter shape can vary):
3431 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3432 if (!fMipmapPrograms[progIdx].fProgram) {
3433 if (!this->createMipmapProgram(progIdx)) {
3434 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003435 // Invalidate all params to cover base level change in a previous iteration.
3436 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003437 return false;
3438 }
3439 }
Brian Salomon802cb312018-06-08 18:05:20 -04003440 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003441
3442 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3443 const float invWidth = 1.0f / width;
3444 const float invHeight = 1.0f / height;
3445 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3446 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3447 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3448
3449 // Only sample from previous mip
3450 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3451
Brian Salomon930f9392018-06-20 16:25:26 -04003452 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3453 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003454
Brian Osman788b9162020-02-07 10:36:46 -05003455 width = std::max(1, width / 2);
3456 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003457 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003458
3459 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3460 }
3461
3462 // Unbind:
3463 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3464 GR_GL_TEXTURE_2D, 0, 0));
3465
Brian Salomon930f9392018-06-20 16:25:26 -04003466 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003467 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3468 // We drew the 2nd to last level into the last level.
3469 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3470 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003471
brianosman33f6b3f2016-06-02 05:49:21 -07003472 return true;
3473}
3474
Chris Daltond7291ba2019-03-07 14:17:03 -07003475void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003476 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3477 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003478
3479 int effectiveSampleCnt;
3480 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003481 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003482
3483 sampleLocations->reset(effectiveSampleCnt);
3484 for (int i = 0; i < effectiveSampleCnt; ++i) {
3485 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3486 }
3487}
3488
cdalton231c5fd2015-05-13 12:35:36 -07003489void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003490 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003491 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003492 case kTexture_GrXferBarrierType: {
3493 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003494 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003495 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3496 // The render target uses separate storage so no need for glTextureBarrier.
3497 // FIXME: The render target will resolve automatically when its texture is bound,
3498 // but we could resolve only the bounds that will be read if we do it here instead.
3499 return;
3500 }
cdalton9954bc32015-04-29 14:17:00 -07003501 SkASSERT(this->caps()->textureBarrierSupport());
3502 GL_CALL(TextureBarrier());
3503 return;
cdalton231c5fd2015-05-13 12:35:36 -07003504 }
cdalton8917d622015-05-06 13:40:21 -07003505 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003506 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003507 this->caps()->blendEquationSupport());
3508 GL_CALL(BlendBarrier());
3509 return;
bsalomoncb02b382015-08-12 11:14:50 -07003510 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003511 }
3512}
3513
Chris Daltone1196c52019-12-28 14:31:09 -07003514void GrGLGpu::insertManualFramebufferBarrier() {
3515 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3516 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3517}
3518
Brian Salomon85c3d682019-11-04 15:04:54 -05003519GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003520 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003521 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003522 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003523 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003524 // We don't support protected textures in GL.
3525 if (isProtected == GrProtected::kYes) {
3526 return {};
3527 }
3528
Brian Salomon8a375832018-03-14 10:21:40 -04003529 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003530
Brian Salomond4764a12019-08-08 12:08:24 -04003531 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003532 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003533 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003534 }
3535
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003536 int numMipLevels = 1;
3537 if (mipMapped == GrMipMapped::kYes) {
3538 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3539 }
3540
Robert Phillipsd34691b2019-09-24 13:38:43 -04003541 // Compressed formats go through onCreateCompressedBackendTexture
3542 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3543
Greg Daniel15500642019-06-27 03:05:55 +00003544 GrGLTextureInfo info;
3545 GrGLTextureParameters::SamplerOverriddenState initialState;
3546
Greg Danield51fa2f2020-01-22 16:53:38 -05003547 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003548 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003549 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003550
Robert Phillipsd34691b2019-09-24 13:38:43 -04003551 info.fTarget = GR_GL_TEXTURE_2D;
3552 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003553 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003554 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003555 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003556 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003557
Brian Salomon85c3d682019-11-04 15:04:54 -05003558 // Unbind this texture from the scratch texture unit.
3559 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003560
Brian Salomone2826ab2019-06-04 15:58:31 -04003561 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003562 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003563 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3564 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003565
Brian Salomon85c3d682019-11-04 15:04:54 -05003566 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3567 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003568}
3569
Greg Daniel16032b32020-05-06 15:31:10 -04003570bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3571 sk_sp<GrRefCntedCallback> finishedCallback,
3572 const BackendTextureData* data) {
3573 GrGLTextureInfo info;
3574 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3575
3576 int numMipLevels = 1;
3577 if (backendTexture.hasMipMaps()) {
3578 numMipLevels =
3579 SkMipMap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
3580 }
3581
3582 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3583
Robert Phillips71f06f62020-05-15 16:37:21 +00003584 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003585
Greg Danielb2365d82020-05-13 15:32:04 -04003586 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevesl-1
3587 // so that the uploads go to the right levels.
3588 if (numMipLevels) {
3589 auto params = backendTexture.getGLTextureParams();
3590 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3591 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Robert Phillips71f06f62020-05-15 16:37:21 +00003592 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003593 nonsamplerState.fBaseMipMapLevel = 0;
3594 }
3595 if (params->nonsamplerState().fMaxMipMapLevel != (numMipLevels - 1)) {
Robert Phillips71f06f62020-05-15 16:37:21 +00003596 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003597 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3598 }
3599 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3600 }
3601
Greg Daniel16032b32020-05-06 15:31:10 -04003602 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
3603 if (data->type() == BackendTextureData::Type::kPixmaps) {
3604 SkTDArray<GrMipLevel> texels;
3605 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3606 texels.append(numMipLevels);
3607 for (int i = 0; i < numMipLevels; ++i) {
3608 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3609 }
Robert Phillips71f06f62020-05-15 16:37:21 +00003610 if (!this->uploadTexData(glFormat, colorType, backendTexture.width(),
3611 backendTexture.height(), GR_GL_TEXTURE_2D, 0, 0,
3612 backendTexture.width(), backendTexture.height(),
3613 colorType, texels.begin(), texels.count())) {
3614 return false;
3615 }
3616 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3617 // TODO: Unify this with the clear texture code in onCreateTexture().
3618 GrColorType colorType;
3619 GrGLenum externalFormat, externalType;
3620 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3621 &externalType, &colorType);
3622 if (colorType == GrColorType::kUnknown) {
3623 return false;
3624 }
3625
3626 // Make one tight image at the base size and reuse it for smaller levels.
3627 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, backendTexture.dimensions());
3628 auto rb = ii.minRowBytes();
3629 std::unique_ptr<char[]> pixelStorage(new char[rb * backendTexture.height()]);
3630 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3631 return false;
3632 }
3633
3634 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3635 SkISize levelDimensions = backendTexture.dimensions();
3636 for (int i = 0; i < numMipLevels; ++i) {
3637 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3638 levelDimensions.height(), externalFormat, externalType,
3639 pixelStorage.get()));
3640 levelDimensions = {std::max(1, levelDimensions.width() / 2),
3641 std::max(1, levelDimensions.height() / 2)};
3642 }
Greg Daniel16032b32020-05-06 15:31:10 -04003643 }
3644
3645 // Unbind this texture from the scratch texture unit.
Robert Phillips71f06f62020-05-15 16:37:21 +00003646 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
3647 return true;
Greg Daniel16032b32020-05-06 15:31:10 -04003648}
3649
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003650void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003651 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3652
3653 GrGLTextureInfo info;
3654 if (tex.getGLTextureInfo(&info)) {
3655 GL_CALL(DeleteTextures(1, &info.fID));
3656 }
3657}
3658
Robert Phillips3bce1f72020-05-12 12:41:58 -04003659bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3660 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3661
3662 Stats::ProgramCacheResult stat;
3663
3664 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3665 if (!tmp) {
3666 return false;
3667 }
3668
3669 return stat != Stats::ProgramCacheResult::kHit;
3670}
3671
Robert Phillipsf0ced622019-05-16 09:06:25 -04003672#if GR_TEST_UTILS
3673
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003674bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003675 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003676
Greg Daniel52e16d92018-04-10 09:34:07 -04003677 GrGLTextureInfo info;
3678 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003679 return false;
3680 }
3681
3682 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003683 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003684
3685 return (GR_GL_TRUE == result);
3686}
3687
Brian Salomonf865b052018-03-09 09:01:53 -05003688GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003689 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003690 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3691 return GrBackendRenderTarget(); // invalid
3692 }
Brian Salomon8a375832018-03-14 10:21:40 -04003693 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003694 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003695 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003696 return {};
3697 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003698 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003699 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003700 if (sFormatIdx < 0) {
3701 return {};
3702 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003703 GrGLuint colorID = 0;
3704 GrGLuint stencilID = 0;
3705 auto deleteIDs = [&] {
3706 if (colorID) {
3707 if (useTexture) {
3708 GL_CALL(DeleteTextures(1, &colorID));
3709 } else {
3710 GL_CALL(DeleteRenderbuffers(1, &colorID));
3711 }
Brian Salomonf865b052018-03-09 09:01:53 -05003712 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003713 if (stencilID) {
3714 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003715 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003716 };
3717
3718 if (useTexture) {
3719 GL_CALL(GenTextures(1, &colorID));
3720 } else {
3721 GL_CALL(GenRenderbuffers(1, &colorID));
3722 }
3723 GL_CALL(GenRenderbuffers(1, &stencilID));
3724 if (!stencilID || !colorID) {
3725 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003726 return {};
3727 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003728
Brian Salomonf865b052018-03-09 09:01:53 -05003729 GrGLFramebufferInfo info;
3730 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003731 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003732 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3733 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003734 deleteIDs();
3735 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003736 }
3737
3738 this->invalidateBoundRenderTarget();
3739
Adrienne Walker4ee88512018-05-17 11:37:14 -07003740 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003741 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003742 GrGLTextureParameters::SamplerOverriddenState initialState;
3743 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3744 if (!colorID) {
3745 deleteIDs();
3746 return {};
3747 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003748 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3749 colorID, 0));
3750 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003751 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003752 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003753 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003754 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3755 GR_GL_RENDERBUFFER, colorID));
3756 }
3757 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003758 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003759 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003760 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003761 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003762 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3763 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003764 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003765 }
3766
Brian Salomon93348dd2018-08-29 12:56:23 -04003767 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3768 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3769 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3770 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003771 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003772 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003773
Adrienne Walker4ee88512018-05-17 11:37:14 -07003774 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003775 GrGLenum status;
3776 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3777 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003778 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003779 return {};
3780 }
3781 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003782
Greg Daniel108bb232018-07-03 16:18:29 -04003783 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003784 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003785 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003786}
3787
3788void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003789 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003790 GrGLFramebufferInfo info;
3791 if (backendRT.getGLFramebufferInfo(&info)) {
3792 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003793 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003794 }
3795 }
joshualitt8fd844f2015-12-02 13:36:47 -08003796}
3797
Greg Daniel26b50a42018-03-08 09:49:58 -05003798void GrGLGpu::testingOnly_flushGpuAndSync() {
3799 GL_CALL(Finish());
3800}
Brian Salomonf865b052018-03-09 09:01:53 -05003801#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003802
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003803///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003804
cdaltone2e71c22016-04-07 18:13:29 -07003805GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003806 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003807 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003808 GrGLAttribArrayState* attribState;
3809
cdaltone2e71c22016-04-07 18:13:29 -07003810 if (gpu->glCaps().isCoreProfile()) {
3811 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003812 GrGLuint arrayID;
3813 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3814 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003815 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003816 }
cdaltone2e71c22016-04-07 18:13:29 -07003817 if (ibuf) {
3818 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003819 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003820 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003821 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003822 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003823 if (ibuf) {
3824 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003825 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003826 } else {
3827 this->setVertexArrayID(gpu, 0);
3828 }
3829 int attrCount = gpu->glCaps().maxVertexAttributes();
3830 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3831 fDefaultVertexArrayAttribState.resize(attrCount);
3832 }
3833 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003834 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003835 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003836}
bsalomone179a912016-01-20 06:18:10 -08003837
Greg Danielfe159622020-04-10 17:43:51 +00003838void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3839 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003840 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003841}
3842
Greg Daniel78be37f2020-04-14 16:40:35 -04003843void GrGLGpu::flush(FlushType flushType) {
3844 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3845 GL_CALL(Flush());
3846 fNeedsGLFlush = false;
3847 }
3848}
3849
Greg Danielfe159622020-04-10 17:43:51 +00003850bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3851 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003852 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003853 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003854 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003855 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003856 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003857 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003858 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003859 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003860}
3861
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003862void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3863 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3864 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3865 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003866}
3867
Greg Daniel6be35232017-03-01 17:01:09 -05003868GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003869 if (!this->caps()->fenceSyncSupport()) {
3870 return 0;
3871 }
Greg Daniel6be35232017-03-01 17:01:09 -05003872 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003873 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3874 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3875 GrGLuint fence = 0;
3876 GL_CALL(GenFences(1, &fence));
3877 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3878 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3879 } else {
3880 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3881 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003882 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003883 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003884 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003885}
3886
Brian Salomonb0d8b762019-05-06 16:58:22 -04003887bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003888 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3889 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3890 if (!timeout) {
3891 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003892 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003893 }
3894 GrGLboolean result;
3895 GL_CALL_RET(result, TestFence(nvFence));
3896 return result == GR_GL_TRUE;
3897 }
3898 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3899 // If this really becomes necessary we could poll TestFence().
3900 // FinishFence always flushes so no need to check flush param.
3901 GL_CALL(FinishFence(nvFence));
3902 return true;
3903 } else {
3904 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3905 GrGLenum result;
3906 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3907 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3908 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003909}
3910
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003911bool GrGLGpu::waitFence(GrFence fence) {
3912 if (!this->caps()->fenceSyncSupport()) {
3913 return true;
3914 }
3915 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003916}
3917
3918void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003919 if (this->caps()->fenceSyncSupport()) {
3920 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3921 }
Greg Daniel6be35232017-03-01 17:01:09 -05003922}
3923
Greg Daniel301015c2019-11-18 14:06:46 -05003924std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003925 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003926 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003927}
3928
Greg Daniel301015c2019-11-18 14:06:46 -05003929std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3930 const GrBackendSemaphore& semaphore,
3931 GrResourceProvider::SemaphoreWrapType wrapType,
3932 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003933 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003934 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3935}
3936
Greg Daniel301015c2019-11-18 14:06:46 -05003937void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3938 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003939
Greg Daniel48661b82018-01-22 16:11:35 -05003940 GrGLsync sync;
3941 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3942 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003943 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003944}
3945
Greg Daniel301015c2019-11-18 14:06:46 -05003946void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
3947 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003948
3949 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3950}
3951
Brian Salomonb0d8b762019-05-06 16:58:22 -04003952void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003953 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003954}
3955
Greg Daniel6be35232017-03-01 17:01:09 -05003956void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003957 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3958 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
3959 GL_CALL(DeleteFences(1, &nvFence));
3960 } else {
3961 GL_CALL(DeleteSync(sync));
3962 }
jvanverth84741b32016-09-30 08:39:02 -07003963}
Brian Osman13dddce2017-05-09 13:19:50 -04003964
Greg Daniel301015c2019-11-18 14:06:46 -05003965std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04003966 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05003967 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05003968 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003969 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05003970 // 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 -04003971 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04003972
3973 return semaphore;
3974}
Robert Phillips646e4292017-06-13 12:44:56 -04003975
3976int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04003977 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04003978 case kTexture2DSampler_GrSLType:
3979 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04003980 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003981 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04003982 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003983 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04003984 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04003985 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04003986 }
3987}
Brian Osman71a18892017-08-10 10:23:25 -04003988
Kevin Lubickf4def342018-10-04 12:52:50 -04003989#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003990#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04003991void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
3992 // We are called by the base class, which has already called beginObject(). We choose to nest
3993 // all of our caps information in a named sub-object.
3994 writer->beginObject("GL GPU");
3995
3996 const GrGLubyte* str;
3997 GL_CALL_RET(str, GetString(GR_GL_VERSION));
3998 writer->appendString("GL_VERSION", (const char*)(str));
3999 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4000 writer->appendString("GL_RENDERER", (const char*)(str));
4001 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4002 writer->appendString("GL_VENDOR", (const char*)(str));
4003 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4004 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4005
4006 writer->appendName("extensions");
4007 glInterface()->fExtensions.dumpJSON(writer);
4008
4009 writer->endObject();
4010}
Kevin Lubickf4def342018-10-04 12:52:50 -04004011#endif