blob: 3e3648f4bf24edbaed0756307ddd1cc5f55bafee [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040022#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040024#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrFixedClip.h"
26#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040028#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
30#include "src/gpu/GrShaderCaps.h"
31#include "src/gpu/GrSurfaceProxyPriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/gl/GrGLBuffer.h"
34#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
Brian Salomond8575452020-02-25 12:13:29 -050047#define GL_ALLOC_CALL(call) \
48 [&] { \
49 if (this->glCaps().skipErrorChecks()) { \
50 GR_GL_CALL(this->glInterface(), call); \
51 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
52 } else { \
53 GrGLClearErr(this->glInterface()); \
54 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
55 return GR_GL_GET_ERROR(this->glInterface()); \
56 } \
57 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000058
Jim Van Verth32ac83e2016-11-28 15:23:57 -050059//#define USE_NSIGHT
60
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000061///////////////////////////////////////////////////////////////////////////////
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050084 GR_GL_HSL_LUMINOSITY,
85
86 // Illegal... needs to map to something.
87 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070088};
Brian Salomon4dea72a2019-12-18 10:43:10 -050089static_assert(0 == kAdd_GrBlendEquation);
90static_assert(1 == kSubtract_GrBlendEquation);
91static_assert(2 == kReverseSubtract_GrBlendEquation);
92static_assert(3 == kScreen_GrBlendEquation);
93static_assert(4 == kOverlay_GrBlendEquation);
94static_assert(5 == kDarken_GrBlendEquation);
95static_assert(6 == kLighten_GrBlendEquation);
96static_assert(7 == kColorDodge_GrBlendEquation);
97static_assert(8 == kColorBurn_GrBlendEquation);
98static_assert(9 == kHardLight_GrBlendEquation);
99static_assert(10 == kSoftLight_GrBlendEquation);
100static_assert(11 == kDifference_GrBlendEquation);
101static_assert(12 == kExclusion_GrBlendEquation);
102static_assert(13 == kMultiply_GrBlendEquation);
103static_assert(14 == kHSLHue_GrBlendEquation);
104static_assert(15 == kHSLSaturation_GrBlendEquation);
105static_assert(16 == kHSLColor_GrBlendEquation);
106static_assert(17 == kHSLLuminosity_GrBlendEquation);
107static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
Brian Salomond978b902019-02-07 15:09:18 -0500133//////////////////////////////////////////////////////////////////////////////
134
135static int gl_target_to_binding_index(GrGLenum target) {
136 switch (target) {
137 case GR_GL_TEXTURE_2D:
138 return 0;
139 case GR_GL_TEXTURE_RECTANGLE:
140 return 1;
141 case GR_GL_TEXTURE_EXTERNAL:
142 return 2;
143 }
144 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500145}
146
147GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500148 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
149}
150
151bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
152 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500153}
154
155void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500156 int targetIndex = gl_target_to_binding_index(target);
157 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
158 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500159}
160
Brian Salomon1f05d452019-02-08 12:33:08 -0500161void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
162 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500163}
164
Brian Salomon1f05d452019-02-08 12:33:08 -0500165void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
166 for (auto& targetBinding : fTargetBindings) {
167 targetBinding.fBoundResourceID.makeInvalid();
168 if (markUnmodified) {
169 targetBinding.fHasBeenModified = false;
170 }
Brian Salomond978b902019-02-07 15:09:18 -0500171 }
172}
173
174//////////////////////////////////////////////////////////////////////////////
175
Brian Salomondc829942018-10-23 16:07:24 -0400176static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
177 switch (filter) {
178 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
179 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
180 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
181 }
182 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400183}
184
185static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
186 switch (filter) {
187 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
188 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
189 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
190 }
191 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400192}
193
Michael Ludwigf23a1522018-12-10 11:36:13 -0500194static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
195 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400196 switch (wrapMode) {
197 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
198 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
199 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500200 case GrSamplerState::WrapMode::kClampToBorder:
201 // May not be supported but should have been caught earlier
202 SkASSERT(caps.clampToBorderSupport());
203 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500204 }
Brian Salomondc829942018-10-23 16:07:24 -0400205 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400206}
207
208///////////////////////////////////////////////////////////////////////////////
209
210class GrGLGpu::SamplerObjectCache {
211public:
212 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
213 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
214 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
215 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
216 std::fill_n(fSamplers, kNumSamplers, 0);
217 }
218
219 ~SamplerObjectCache() {
220 if (!fNumTextureUnits) {
221 // We've already been abandoned.
222 return;
223 }
Chris Dalton703fe682019-05-15 12:58:12 -0600224 for (GrGLuint sampler : fSamplers) {
225 // The spec states that "zero" values should be silently ignored, however they still
226 // trigger GL errors on some NVIDIA platforms.
227 if (sampler) {
228 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
229 }
230 }
Brian Salomondc829942018-10-23 16:07:24 -0400231 }
232
Brian Salomonccb61422020-01-09 10:46:36 -0500233 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400234 int index = StateToIndex(state);
235 if (!fSamplers[index]) {
236 GrGLuint s;
237 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
238 if (!s) {
239 return;
240 }
241 fSamplers[index] = s;
242 auto minFilter = filter_to_gl_min_filter(state.filter());
243 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500244 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
245 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400246 GR_GL_CALL(fGpu->glInterface(),
247 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
248 GR_GL_CALL(fGpu->glInterface(),
249 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
250 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
251 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
252 }
253 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
254 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
255 fHWBoundSamplers[unitIdx] = fSamplers[index];
256 }
257 }
258
259 void invalidateBindings() {
260 // When we have sampler support we always use samplers. So setting these to zero will cause
261 // a rebind on next usage.
262 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
263 }
264
265 void abandon() {
266 fHWBoundSamplers.reset();
267 fNumTextureUnits = 0;
268 }
269
270 void release() {
271 if (!fNumTextureUnits) {
272 // We've already been abandoned.
273 return;
274 }
275 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
276 std::fill_n(fSamplers, kNumSamplers, 0);
277 // Deleting a bound sampler implicitly binds sampler 0.
278 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
279 }
280
281private:
Brian Salomonccb61422020-01-09 10:46:36 -0500282 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400283 int filter = static_cast<int>(state.filter());
284 SkASSERT(filter >= 0 && filter < 3);
285 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500286 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400287 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500288 SkASSERT(wrapY >= 0 && wrapY < 4);
289 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400290 SkASSERT(idx < kNumSamplers);
291 return idx;
292 }
293
294 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500295 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400296 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
297 GrGLuint fSamplers[kNumSamplers];
298 int fNumTextureUnits;
299};
300
reed@google.comac10a2d2010-12-22 21:39:39 +0000301///////////////////////////////////////////////////////////////////////////////
302
Brian Salomon384fab42017-12-07 12:33:05 -0500303sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
304 GrContext* context) {
305 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500306 interface = GrGLMakeNativeInterface();
307 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
308 // to GrGLMakeNativeInterface.
309 if (!interface) {
310 interface = sk_ref_sp(GrGLCreateNativeInterface());
311 }
Brian Salomon384fab42017-12-07 12:33:05 -0500312 if (!interface) {
313 return nullptr;
314 }
bsalomon424cc262015-05-22 10:37:30 -0700315 }
Jim Van Verth76334772017-05-05 16:46:05 -0400316#ifdef USE_NSIGHT
317 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
318#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500319 auto glContext = GrGLContext::Make(std::move(interface), options);
320 if (!glContext) {
321 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700322 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500323 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700324}
325
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500326GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
327 : GrGpu(context)
328 , fGLContext(std::move(ctx))
329 , fProgramCache(new ProgramCache(this))
330 , fHWProgramID(0)
331 , fTempSrcFBOID(0)
332 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500333 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500334 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400335 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500336 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000337
Brian Salomond978b902019-02-07 15:09:18 -0500338 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000339
Brian Salomonae64c192019-02-05 09:41:37 -0500340 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
341 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500342 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500343 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
344 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
345 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
346 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700347 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500348 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
349 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700350 }
Brian Salomonae64c192019-02-05 09:41:37 -0500351 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400352 fHWBufferState[i].invalidate();
353 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500354 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700355
356 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
357 fPathRendering.reset(new GrGLPathRendering(this));
358 }
359
Brian Salomondc829942018-10-23 16:07:24 -0400360 if (this->glCaps().samplerObjectSupport()) {
361 fSamplerObjectCache.reset(new SamplerObjectCache(this));
362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363}
364
bsalomon861e1032014-12-16 07:33:49 -0800365GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700366 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
367 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800368 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700369 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700370 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800371
Chris Dalton20e7a532020-02-28 15:37:53 +0000372 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400373 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000374 // detach the current program so there is no confusion on OpenGL's part
375 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000376 GL_CALL(UseProgram(0));
377 }
378
Brian Salomon43f8bf02017-10-18 08:33:29 -0400379 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700380 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800381 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400382 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700383 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800384 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400385 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700386 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800387 }
egdaniel0f5f9672015-02-03 11:10:51 -0800388
bsalomon7ea33f52015-11-22 14:51:00 -0800389 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
390 if (0 != fCopyPrograms[i].fProgram) {
391 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
392 }
bsalomon6df86402015-06-01 10:41:49 -0700393 }
bsalomon6dea83f2015-12-03 12:58:06 -0800394
brianosman33f6b3f2016-06-02 05:49:21 -0700395 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
396 if (0 != fMipmapPrograms[i].fProgram) {
397 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
398 }
399 }
400
Brian Salomondc829942018-10-23 16:07:24 -0400401 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500402
403 while (!fFinishCallbacks.empty()) {
404 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
405 this->deleteSync(fFinishCallbacks.front().fSync);
406 fFinishCallbacks.pop_front();
407 }
bsalomonc8dc1f72014-08-21 13:02:13 -0700408}
409
bsalomon6e2aad42016-04-01 11:54:31 -0700410void GrGLGpu::disconnect(DisconnectType type) {
411 INHERITED::disconnect(type);
412 if (DisconnectType::kCleanup == type) {
413 if (fHWProgramID) {
414 GL_CALL(UseProgram(0));
415 }
416 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700417 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700418 }
419 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700420 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700421 }
422 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700423 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700424 }
bsalomon6e2aad42016-04-01 11:54:31 -0700425 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
426 if (fCopyPrograms[i].fProgram) {
427 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
428 }
429 }
brianosman33f6b3f2016-06-02 05:49:21 -0700430 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
431 if (fMipmapPrograms[i].fProgram) {
432 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
433 }
434 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400435
Brian Salomondc829942018-10-23 16:07:24 -0400436 if (fSamplerObjectCache) {
437 fSamplerObjectCache->release();
438 }
bsalomon6e2aad42016-04-01 11:54:31 -0700439 } else {
440 if (fProgramCache) {
441 fProgramCache->abandon();
442 }
Brian Salomondc829942018-10-23 16:07:24 -0400443 if (fSamplerObjectCache) {
444 fSamplerObjectCache->abandon();
445 }
bsalomon6e2aad42016-04-01 11:54:31 -0700446 }
447
Chris Dalton20e7a532020-02-28 15:37:53 +0000448 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500449 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700450
bsalomonc8dc1f72014-08-21 13:02:13 -0700451 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700452 fTempSrcFBOID = 0;
453 fTempDstFBOID = 0;
454 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700455 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800456 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
457 fCopyPrograms[i].fProgram = 0;
458 }
brianosman33f6b3f2016-06-02 05:49:21 -0700459 fMipmapProgramArrayBuffer.reset();
460 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
461 fMipmapPrograms[i].fProgram = 0;
462 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400463
jvanverthe9c0fc62015-04-29 11:18:05 -0700464 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700465 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700466 }
Greg Daniel822f7b22020-02-21 14:41:36 -0500467
468 while (!fFinishCallbacks.empty()) {
469 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
470 if (DisconnectType::kCleanup == type) {
471 this->deleteSync(fFinishCallbacks.front().fSync);
472 }
473 fFinishCallbacks.pop_front();
474 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000475}
476
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000477///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000478
bsalomon861e1032014-12-16 07:33:49 -0800479void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000480 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400481 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000482 GL_CALL(Disable(GR_GL_DEPTH_TEST));
483 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000484
Brian Salomonf0861672017-05-08 11:10:10 -0400485 // We don't use face culling.
486 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600487 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
488 // just set this to the default for self-consistency.
489 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400490
Brian Salomonae64c192019-02-05 09:41:37 -0500491 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
492 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700493
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400494 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500495#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000496 // Desktop-only state that we never change
497 if (!this->glCaps().isCoreProfile()) {
498 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
499 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
500 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
501 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
502 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
503 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
504 }
505 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
506 // core profile. This seems like a bug since the core spec removes any mention of
507 // GL_ARB_imaging.
508 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
509 GL_CALL(Disable(GR_GL_COLOR_TABLE));
510 }
511 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400512
Chris Dalton1215cda2019-12-17 21:44:04 -0700513 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500514#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000515 // Since ES doesn't support glPointSize at all we always use the VS to
516 // set the point size
517 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
518
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000519 }
joshualitt58162332014-08-01 06:44:53 -0700520
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400521 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500522 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700523 // The arm extension requires specifically enabling MSAA fetching per sample.
524 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500525 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700526 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000527 fHWWriteToColor = kUnknown_TriState;
528 // we only ever use lines in hairline mode
529 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700530 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500531
532 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000533 }
edisonn@google.comba669992013-06-28 16:03:21 +0000534
egdanielb414f252014-07-29 13:15:47 -0700535 if (resetBits & kMSAAEnable_GrGLBackendState) {
536 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700537
Chris Dalton6ce447a2019-06-23 18:07:38 -0600538 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800539 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
540 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700541 GL_CALL(CoverageModulation(GR_GL_RGBA));
542 }
Chris Daltonce425af2019-12-16 10:39:03 -0700543
544 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000545 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000546
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000547 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400548 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000549
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000550 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500551 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500552 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000553 }
Brian Salomondc829942018-10-23 16:07:24 -0400554 if (fSamplerObjectCache) {
555 fSamplerObjectCache->invalidateBindings();
556 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000557 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000558
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000559 if (resetBits & kBlend_GrGLBackendState) {
560 fHWBlendState.invalidate();
561 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000562
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000563 if (resetBits & kView_GrGLBackendState) {
564 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700565 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000566 fHWViewport.invalidate();
567 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000568
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 if (resetBits & kStencil_GrGLBackendState) {
570 fHWStencilSettings.invalidate();
571 fHWStencilTestEnabled = kUnknown_TriState;
572 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000573
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000574 // Vertex
575 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700576 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500577 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
578 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700579 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000580 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000581
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500583 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700584 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500585 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000587
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000588 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700589 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700590 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000592 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000593
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000594 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400596 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
598 }
Brian Salomon1047a492019-07-02 12:25:21 -0400599 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
601 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 if (this->glCaps().packFlipYSupport()) {
603 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
604 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000605 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000606
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 if (resetBits & kProgram_GrGLBackendState) {
608 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000609 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400611 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000612}
613
Brian Salomonea4ad302019-08-07 13:04:55 -0400614static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
615 const GrGLCaps& caps, GrGLTexture::Desc* desc,
616 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400617 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400618 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400619 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800620 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000621
Brian Salomonea4ad302019-08-07 13:04:55 -0400622 desc->fSize = {backendTex.width(), backendTex.height()};
623 desc->fTarget = info.fTarget;
624 desc->fID = info.fID;
625 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800626
Brian Salomonea4ad302019-08-07 13:04:55 -0400627 if (desc->fFormat == GrGLFormat::kUnknown) {
628 return false;
629 }
630 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400631 if (!caps.shaderCaps()->externalTextureSupport()) {
632 return false;
633 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400634 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
635 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400636 return false;
637 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400638 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400639 return false;
640 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400641 if (backendTex.isProtected()) {
642 // Not supported in GL backend at this time.
643 return false;
644 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400645
Brian Salomond17f6582017-07-19 18:28:58 -0400646 return true;
647}
648
649sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400650 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400651 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400652 GrGLTexture::Desc desc;
653 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400654 return nullptr;
655 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400656
Brian Salomond17f6582017-07-19 18:28:58 -0400657 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400658 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400659 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400660 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400661 }
bsalomone5286e02016-01-14 09:24:09 -0800662
Greg Daniel177e6952017-10-12 12:27:11 -0400663 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
664 : GrMipMapsStatus::kNotAllocated;
665
Brian Salomonea4ad302019-08-07 13:04:55 -0400666 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400667 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400668 // We don't know what parameters are already set on wrapped textures.
669 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600670 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400671}
672
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500673static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
674 const GrGLCaps& caps, GrGLTexture::Desc* desc,
675 bool skipRectTexSupportCheck = false) {
676 GrGLTextureInfo info;
677 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
678 return false;
679 }
680
681 desc->fSize = {backendTex.width(), backendTex.height()};
682 desc->fTarget = info.fTarget;
683 desc->fID = info.fID;
684 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
685
686 if (desc->fFormat == GrGLFormat::kUnknown) {
687 return false;
688 }
689
690 if (GR_GL_TEXTURE_2D != desc->fTarget) {
691 return false;
692 }
693 if (backendTex.isProtected()) {
694 // Not supported in GL backend at this time.
695 return false;
696 }
697
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500698 return true;
699}
700
Robert Phillipsb915c942019-12-17 14:44:37 -0500701sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
702 GrWrapOwnership ownership,
703 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500704 GrGLTexture::Desc desc;
705 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
706 return nullptr;
707 }
708
709 if (kBorrow_GrWrapOwnership == ownership) {
710 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
711 } else {
712 desc.fOwnership = GrBackendObjectOwnership::kOwned;
713 }
714
715 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
716 : GrMipMapsStatus::kNotAllocated;
717
718 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
719 backendTex.getGLTextureParams(), cacheable,
720 kRead_GrIOType);
721 // We don't know what parameters are already set on wrapped textures.
722 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600723 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500724}
725
Brian Salomond17f6582017-07-19 18:28:58 -0400726sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400727 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400728 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500729 GrWrapOwnership ownership,
730 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400731 const GrGLCaps& caps = this->glCaps();
732
Brian Salomonea4ad302019-08-07 13:04:55 -0400733 GrGLTexture::Desc desc;
734 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800735 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800736 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400737 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
738 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800739
Brian Salomond17f6582017-07-19 18:28:58 -0400740 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400741 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800742 return nullptr;
743 }
bsalomon10528f12015-10-14 12:54:52 -0700744
Brian Osman766fcbb2017-03-13 09:33:09 -0400745 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400746 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400747 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400748 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800749 }
bsalomonb15b4c12014-10-29 12:41:57 -0700750
Robert Phillips0902c982019-07-16 07:47:56 -0400751
Greg Daniel6fa62e22019-08-07 15:52:37 -0400752 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
753 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000754
Brian Salomonea4ad302019-08-07 13:04:55 -0400755 GrGLRenderTarget::IDs rtIDs;
756 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400757 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000758 }
Greg Daniel177e6952017-10-12 12:27:11 -0400759
760 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
761 : GrMipMapsStatus::kNotAllocated;
762
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500763 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400764 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400765 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400766 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400767 // We don't know what parameters are already set on wrapped textures.
768 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600769 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000770}
771
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400772sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
773 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400774 GrGLFramebufferInfo info;
775 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000776 return nullptr;
777 }
778
Brian Salomone8a766b2019-07-19 14:24:36 -0400779 if (backendRT.isProtected()) {
780 // Not supported in GL at this time.
781 return nullptr;
782 }
783
Brian Salomond4764a12019-08-08 12:08:24 -0400784 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400785 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
786 return nullptr;
787 }
788
Brian Salomonea4ad302019-08-07 13:04:55 -0400789 GrGLRenderTarget::IDs rtIDs;
790 rtIDs.fRTFBOID = info.fFBOID;
791 rtIDs.fMSColorRenderbufferID = 0;
792 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
793 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000794
Greg Daniel6fa62e22019-08-07 15:52:37 -0400795 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700796
Brian Salomona56a7462020-02-07 14:17:25 -0500797 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400798 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000799}
800
Greg Daniel7ef28f32017-04-20 16:41:55 +0000801sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400802 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400803 GrColorType colorType) {
804 GrGLTexture::Desc desc;
805 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
806 // with a texture rectangle,we assume the necessary support exists.
807 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800808 return nullptr;
809 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400810
811 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
812 return nullptr;
813 }
814
815 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400816 GrGLRenderTarget::IDs rtIDs;
817 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800818 return nullptr;
819 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500820 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800821}
822
Brian Salomonc320b152018-02-20 14:05:36 -0500823static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700824 if (!glTex) {
825 return false;
826 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000827
bsalomone5286e02016-01-14 09:24:09 -0800828 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
829 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800830 return false;
831 }
832
jvanverth17aa0472016-01-05 10:41:27 -0800833 return true;
834}
835
Brian Salomona9b04b92018-06-01 15:04:28 -0400836bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400837 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400838 const GrMipLevel texels[], int mipLevelCount,
839 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500840 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800841
Brian Salomonc320b152018-02-20 14:05:36 -0500842 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800843 return false;
844 }
845
Brian Salomond978b902019-02-07 15:09:18 -0500846 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000847
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400848 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400849 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400850 glTex->target(), left, top, width, height, srcColorType, texels,
851 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000852}
853
Brian Salomone05ba5a2019-04-08 11:59:07 -0400854bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400855 GrColorType textureColorType, GrColorType bufferColorType,
856 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400857 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858
Jim Van Verth1676cb92019-01-15 13:24:45 -0500859 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400860 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500861
Brian Salomonc320b152018-02-20 14:05:36 -0500862 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400863 return false;
864 }
865
Hal Canaryc36f7e72018-06-18 15:50:09 -0400866 static_assert(sizeof(int) == sizeof(int32_t), "");
867 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400868 return false;
869 }
870
Brian Salomond978b902019-02-07 15:09:18 -0500871 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400872
873 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500874 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400875 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500876 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400877
Greg Daniel660cc992017-06-26 14:55:05 -0400878 SkDEBUGCODE(
879 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
880 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
881 SkASSERT(bounds.contains(subRect));
882 )
883
Brian Salomonb28cb682019-07-26 12:48:47 -0400884 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400885 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400886 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700887 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400888 return false;
889 }
890
891 bool restoreGLRowLength = false;
892 if (trimRowBytes != rowBytes) {
893 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400894 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400895 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
896 restoreGLRowLength = true;
897 }
898
Greg Danielba88ab62019-07-26 09:14:01 -0400899 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400900 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400901 GrGLenum externalFormat = 0;
902 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400903 this->glCaps().getTexSubImageExternalFormatAndType(
904 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400905 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400906 return false;
907 }
908
Brian Salomon8cbf6622019-07-30 11:03:14 -0400909 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910 GL_CALL(TexSubImage2D(glTex->target(),
911 0,
912 left, top,
913 width,
914 height,
915 externalFormat, externalType,
916 pixels));
917
918 if (restoreGLRowLength) {
919 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
920 }
921
922 return true;
923}
924
Brian Salomon26de56e2019-04-10 12:14:26 -0400925bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400926 GrColorType surfaceColorType, GrColorType dstColorType,
927 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400928 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
929 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400930 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400931 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
932 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400933}
934
Brian Osman9b560242017-09-05 15:34:52 -0400935void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500936 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
937 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
938 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
939 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400940 }
Brian Osman9b560242017-09-05 15:34:52 -0400941}
942
Brian Salomond2a8ae22019-09-10 16:03:59 -0400943bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
944 int texHeight, GrGLenum target, int left, int top, int width,
945 int height, GrColorType srcColorType, const GrMipLevel texels[],
946 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400948 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500949
Greg Daniel7bfc9132019-08-14 14:23:53 -0400950 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400951 SkDEBUGCODE(
952 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
953 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
954 SkASSERT(bounds.contains(subRect));
955 )
Robert Phillips590533f2017-07-11 14:22:35 -0400956 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400957 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800958
Brian Osman9b560242017-09-05 15:34:52 -0400959 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400960
cblume55f2d2d2016-02-26 13:20:48 -0800961 const GrGLInterface* interface = this->glInterface();
962 const GrGLCaps& caps = this->glCaps();
963
Brian Salomonf77c1462019-08-01 15:19:29 -0400964 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800965
966 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000967 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000968 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000969
bsalomon5b30c6f2015-12-17 14:17:34 -0800970 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800971 GrGLenum externalFormat;
972 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400973 this->glCaps().getTexSubImageExternalFormatAndType(
974 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400975 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800976 return false;
977 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400978
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000979 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800980 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000981 * because our srcData has extra bytes past each row. If so, we need
982 * to trim those off here, since GL ES may not let us specify
983 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000984 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000985 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -0800986
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400987 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600988 *mipMapsStatus = (mipLevelCount > 1) ?
989 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -0800990 }
bsalomone699d0c2016-03-09 06:25:15 -0800991
Brian Salomond2a8ae22019-09-10 16:03:59 -0400992 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -0800993
Brian Salomond2a8ae22019-09-10 16:03:59 -0400994 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
995 if (!texels[currentMipLevel].fPixels) {
996 if (mipMapsStatus) {
997 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400998 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400999 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001000 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001001 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001002 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1003 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001004 const size_t trimRowBytes = currentWidth * bpp;
1005 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1006
1007 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1008 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1009 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1010 restoreGLRowLength = true;
1011 }
1012
1013 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1014 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001015 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001016 if (restoreGLRowLength) {
1017 SkASSERT(caps.writePixelsRowBytesSupport());
1018 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1019 }
1020 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021}
1022
Greg Daniel7bfc9132019-08-14 14:23:53 -04001023bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001024 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001025 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001026 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001027 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001028 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001029 const GrGLCaps& caps = this->glCaps();
1030
1031 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001032 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001033 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001034 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001035 }
1036
Robert Phillips9f744f72019-12-19 19:14:33 -05001037 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1038 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1039
Greg Daniel7bfc9132019-08-14 14:23:53 -04001040 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001041
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001042 int numMipLevels = 1;
1043 if (mipMapped == GrMipMapped::kYes) {
1044 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1045 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001046
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001047 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001048 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001049
1050 if (useTexStorage) {
1051 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001052 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1053 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001054 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001055 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001056 }
Robert Phillips42716d42019-12-16 12:19:54 -05001057
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001058 size_t offset = 0;
1059 for (int level = 0; level < numMipLevels; ++level) {
1060
Robert Phillips99dead92020-01-27 16:11:57 -05001061 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1062 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001063
Brian Salomond8575452020-02-25 12:13:29 -05001064 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1065 level,
1066 0, // left
1067 0, // top
1068 dimensions.width(),
1069 dimensions.height(),
1070 internalFormat,
1071 SkToInt(levelDataSize),
1072 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001073
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001074 if (error != GR_GL_NO_ERROR) {
1075 return false;
1076 }
1077
Robert Phillips9f744f72019-12-19 19:14:33 -05001078 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001079 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001080 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001081 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001082 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001083
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001084 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001085 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1086 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001087
1088 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001089 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1090 level,
1091 internalFormat,
1092 dimensions.width(),
1093 dimensions.height(),
1094 0, // border
1095 SkToInt(levelDataSize),
1096 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001097
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001098 if (error != GR_GL_NO_ERROR) {
1099 return false;
1100 }
1101
Robert Phillips9f744f72019-12-19 19:14:33 -05001102 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001103 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001104 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001105 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001106 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001107}
1108
Brian Salomond8575452020-02-25 12:13:29 -05001109bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001110 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001111 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001112 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001113 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001114 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001115 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1116 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001117 break;
1118 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001119 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1120 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001121 break;
1122 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1123 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001124 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1125 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001126 break;
1127 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001128 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001129 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001130 }
Brian Salomond8575452020-02-25 12:13:29 -05001131 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001132}
1133
Brian Salomonea4ad302019-08-07 13:04:55 -04001134bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001135 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001136 GrGLRenderTarget::IDs* rtIDs) {
1137 rtIDs->fMSColorRenderbufferID = 0;
1138 rtIDs->fRTFBOID = 0;
1139 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1140 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001141
bsalomona11e5fc2015-12-18 07:59:41 -08001142 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001143
Brian Salomonea4ad302019-08-07 13:04:55 -04001144 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001145 goto FAILED;
1146 }
1147
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001148 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001149 goto FAILED;
1150 }
1151
Brian Salomonea4ad302019-08-07 13:04:55 -04001152 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1153 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 goto FAILED;
1155 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001156
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001157 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1158 // the texture bound to the other. The exception is the IMG multisample extension. With this
1159 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1160 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001161 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001162 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1163 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1164 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165 goto FAILED;
1166 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001169 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170 }
1171
egdanield803f272015-03-18 13:01:52 -07001172 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001173 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001174 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001175 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001176 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001177 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1178 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 goto FAILED;
1180 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001181 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001182 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001183 GR_GL_COLOR_ATTACHMENT0,
1184 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001185 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001187 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001189 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1191 GR_GL_COLOR_ATTACHMENT0,
1192 desc.fTarget,
1193 desc.fID,
1194 0,
1195 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001196 } else {
egdanield803f272015-03-18 13:01:52 -07001197 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001198 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 desc.fTarget,
1200 desc.fID,
1201 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001202 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203
1204 return true;
1205
1206FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 if (rtIDs->fMSColorRenderbufferID) {
1208 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001209 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001210 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1211 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001213 if (rtIDs->fTexFBOID) {
1214 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001215 }
1216 return false;
1217}
1218
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001219// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001220static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001221// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001222 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001223}
1224
Brian Salomone2826ab2019-06-04 15:58:31 -04001225static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001226 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001227 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1228 // drivers have a bug where an FBO won't be complete if it includes a
1229 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001230 GrGLTextureParameters::SamplerOverriddenState state;
1231 state.fMinFilter = GR_GL_NEAREST;
1232 state.fMagFilter = GR_GL_NEAREST;
1233 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1234 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1236 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1237 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1238 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001239 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001240}
1241
Brian Salomona56a7462020-02-07 14:17:25 -05001242sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001243 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001244 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001245 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001246 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001247 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001248 int mipLevelCount,
1249 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001250 // We don't support protected textures in GL.
1251 if (isProtected == GrProtected::kYes) {
1252 return nullptr;
1253 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001254 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001255
Brian Salomond2a8ae22019-09-10 16:03:59 -04001256 SkASSERT(mipLevelCount > 0);
1257 GrMipMapsStatus mipMapsStatus =
1258 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001259 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001260 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001261 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001262 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001263 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001265 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1266 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001267
Brian Salomona56a7462020-02-07 14:17:25 -05001268 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1269 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001270
1271 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001272 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001273 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001274
Robert Phillips67d52cf2017-06-05 13:38:13 -04001275 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001276 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001277 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001278 GL_CALL(BindTexture(texDesc.fTarget, 0));
1279 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001280
Brian Salomonea4ad302019-08-07 13:04:55 -04001281 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1282 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 return return_null_texture();
1284 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001285 tex = sk_make_sp<GrGLTextureRenderTarget>(
1286 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001287 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001288 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001289 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001291 // The non-sampler params are still at their default values.
1292 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1293 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001294 if (levelClearMask) {
1295 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001296 GrColorType colorType;
1297 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1298 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001299 if (this->glCaps().clearTextureSupport()) {
1300 for (int i = 0; i < mipLevelCount; ++i) {
1301 if (levelClearMask & (1U << i)) {
1302 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1303 nullptr));
1304 }
1305 }
1306 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1307 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001308 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001309 this->disableWindowRectangles();
1310 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001311 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001312 for (int i = 0; i < mipLevelCount; ++i) {
1313 if (levelClearMask & (1U << i)) {
1314 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1315 kDst_TempFBOTarget);
1316 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1317 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1318 }
1319 }
Brian Salomonc2249852019-09-16 11:08:50 -04001320 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001321 } else {
1322 std::unique_ptr<char[]> zeros;
1323 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1324 for (int i = 0; i < mipLevelCount; ++i) {
1325 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001326 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1327 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001328 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1329 // smaller levels that need clearing.
1330 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001331 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001332 size_t size = levelWidth * levelHeight * bpp;
1333 zeros.reset(new char[size]());
1334 }
1335 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1336 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1337 externalFormat, externalType, zeros.get()));
1338 }
Brian Salomona3e29962019-07-16 11:52:08 -04001339 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001340 }
1341 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001342 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001343}
1344
Robert Phillips9f744f72019-12-19 19:14:33 -05001345sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001346 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001347 SkBudgeted budgeted,
1348 GrMipMapped mipMapped,
1349 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001350 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001351 // We don't support protected textures in GL.
1352 if (isProtected == GrProtected::kYes) {
1353 return nullptr;
1354 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001355 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001356 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001357 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001358 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001359 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001360 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001361 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001362 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001363 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001364 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001365 return nullptr;
1366 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001367
Robert Phillipsee946932019-12-18 11:16:17 -05001368 // Unbind this texture from the scratch texture unit.
1369 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1370
Robert Phillipse4720c62020-01-14 14:33:24 -05001371 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1372 ? GrMipMapsStatus::kValid
1373 : GrMipMapsStatus::kNotAllocated;
1374
1375 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001376 // The non-sampler params are still at their default values.
1377 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1378 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001379 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001380}
1381
Robert Phillipsb915c942019-12-17 14:44:37 -05001382GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1383 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001384 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001385 GrProtected isProtected,
1386 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001387 // We don't support protected textures in GL.
1388 if (isProtected == GrProtected::kYes) {
1389 return {};
1390 }
1391
1392 this->handleDirtyContext();
1393
1394 GrGLFormat glFormat = format.asGLFormat();
1395 if (glFormat == GrGLFormat::kUnknown) {
1396 return {};
1397 }
1398
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001399 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001400 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001401 SkAutoMalloc am;
1402
1403 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1404 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1405 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001406 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001407 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001408 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1409 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001410
Robert Phillips99dead92020-01-27 16:11:57 -05001411 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1412 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001413
1414 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001415
1416 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1417
1418 rawData = (const char*) am.get();
1419 }
1420
1421 GrGLTextureInfo info;
1422 GrGLTextureParameters::SamplerOverriddenState initialState;
1423
1424 info.fTarget = GR_GL_TEXTURE_2D;
1425 info.fFormat = GrGLFormatToEnum(glFormat);
1426 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001427 mipMapped, &initialState,
1428 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001429 if (!info.fID) {
1430 return {};
1431 }
1432
1433 // Unbind this texture from the scratch texture unit.
1434 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1435
1436 auto parameters = sk_make_sp<GrGLTextureParameters>();
1437 // The non-sampler params are still at their default values.
1438 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1439 fResetTimestampForTextureParameters);
1440
1441 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1442 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001443}
1444
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001445namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001446
egdaniel8dc7c3a2015-04-16 11:22:42 -07001447const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001448
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001449void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001450 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001451
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001452 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001453 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001454 (kUnknownBitCount == format->fTotalBits));
1455 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001456 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001457 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1458 (GrGLint*)&format->fStencilBits);
1459 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001460 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001461 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1462 (GrGLint*)&format->fTotalBits);
1463 format->fTotalBits += format->fStencilBits;
1464 } else {
1465 format->fTotalBits = format->fStencilBits;
1466 }
1467 }
1468}
1469}
1470
Brian Salomon5043f1f2019-07-11 21:27:54 -04001471int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001472 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001473 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1474
1475 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001476 // Default to unsupported, set this if we find a stencil format that works.
1477 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001478
Brian Salomond2a8ae22019-09-10 16:03:59 -04001479 GrGLuint colorID =
1480 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1481 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001482 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001483 }
egdanielff1d5472015-09-10 08:37:20 -07001484 // unbind the texture from the texture unit before binding it to the frame buffer
1485 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1486
1487 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001488 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001489 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001490 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001491 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001492 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1493 GR_GL_COLOR_ATTACHMENT0,
1494 GR_GL_TEXTURE_2D,
1495 colorID,
1496 0));
bsalomon30447372015-12-21 09:03:05 -08001497 GrGLuint sbRBID = 0;
1498 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001499
1500 // look over formats till I find a compatible one
1501 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001502 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001503 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001504 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1505 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001506 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1507 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1508 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001509 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001510 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001511 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001512 if (sFmt.fPacked) {
1513 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1514 GR_GL_DEPTH_ATTACHMENT,
1515 GR_GL_RENDERBUFFER, sbRBID));
1516 } else {
1517 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1518 GR_GL_DEPTH_ATTACHMENT,
1519 GR_GL_RENDERBUFFER, 0));
1520 }
1521 GrGLenum status;
1522 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1523 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1524 firstWorkingStencilFormatIndex = i;
1525 break;
1526 }
egdanielff1d5472015-09-10 08:37:20 -07001527 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1528 GR_GL_STENCIL_ATTACHMENT,
1529 GR_GL_RENDERBUFFER, 0));
1530 if (sFmt.fPacked) {
1531 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1532 GR_GL_DEPTH_ATTACHMENT,
1533 GR_GL_RENDERBUFFER, 0));
1534 }
egdanielff1d5472015-09-10 08:37:20 -07001535 }
1536 }
bsalomon30447372015-12-21 09:03:05 -08001537 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001538 }
1539 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001540 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1541 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001542 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001543 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001544 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001545}
1546
Brian Salomonea4ad302019-08-07 13:04:55 -04001547GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001548 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001549 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001550 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001551 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001552 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001553 if (format == GrGLFormat::kUnknown) {
1554 return 0;
1555 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001556 GrGLuint id = 0;
1557 GL_CALL(GenTextures(1, &id));
1558 if (!id) {
1559 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001560 }
1561
Brian Salomonea4ad302019-08-07 13:04:55 -04001562 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001563
Brian Salomonea4ad302019-08-07 13:04:55 -04001564 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1565
Robert Phillips42716d42019-12-16 12:19:54 -05001566 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001567 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1568 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001569 GL_CALL(DeleteTextures(1, &id));
1570 return 0;
1571 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001572 }
Robert Phillips42716d42019-12-16 12:19:54 -05001573
Brian Salomonea4ad302019-08-07 13:04:55 -04001574 return id;
1575}
1576
Robert Phillips2716daf2020-01-23 12:53:42 -05001577GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001578 GrGLFormat format,
1579 GrRenderable renderable,
1580 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001581 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001582 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001583 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001584
Brian Salomonea4ad302019-08-07 13:04:55 -04001585 GrGLuint id = 0;
1586 GL_CALL(GenTextures(1, &id));
1587
1588 if (!id) {
1589 return 0;
1590 }
1591
1592 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001593
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001594 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001595 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001596 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001597 }
1598
Brian Salomond2a8ae22019-09-10 16:03:59 -04001599 if (initialState) {
1600 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1601 } else {
1602 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001603 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001604
1605 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1606
1607 bool success = false;
1608 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001609 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001610 auto levelCount = std::max(mipLevelCount, 1);
1611 GrGLenum error =
1612 GL_ALLOC_CALL(TexStorage2D(GR_GL_TEXTURE_2D, levelCount, internalFormat,
1613 dimensions.width(), dimensions.height()));
1614 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001615 } else {
1616 GrGLenum externalFormat, externalType;
1617 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1618 GrGLenum error = GR_GL_NO_ERROR;
1619 if (externalFormat && externalType) {
1620 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1621 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001622 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1623 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond8575452020-02-25 12:13:29 -05001624 error = GL_ALLOC_CALL(TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat,
1625 currentWidth, currentHeight, 0, externalFormat,
1626 externalType, nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001627 }
Brian Salomond8575452020-02-25 12:13:29 -05001628 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001629 }
1630 }
1631 }
1632 if (success) {
1633 return id;
1634 }
1635 GL_CALL(DeleteTextures(1, &id));
1636 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001637}
1638
Chris Daltoneffee202019-07-01 22:28:03 -06001639GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1640 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001641 SkASSERT(width >= rt->width());
1642 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001643
egdaniel8dc7c3a2015-04-16 11:22:42 -07001644 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001645
Brian Salomond4764a12019-08-08 12:08:24 -04001646 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001647 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001648 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001649 }
egdanielff1d5472015-09-10 08:37:20 -07001650
1651 if (!sbDesc.fRenderbufferID) {
1652 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1653 }
1654 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001655 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001656 }
1657 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1658 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001659 // we do this "if" so that we don't call the multisample
1660 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001661 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001662 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1663 width, height)) {
1664 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1665 return nullptr;
1666 }
egdanielff1d5472015-09-10 08:37:20 -07001667 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001668 GrGLenum error = GL_ALLOC_CALL(
1669 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1670 if (error != GR_GL_NO_ERROR) {
1671 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1672 return nullptr;
1673 }
egdanielff1d5472015-09-10 08:37:20 -07001674 }
1675 fStats.incStencilAttachmentCreates();
1676 // After sized formats we attempt an unsized format and take
1677 // whatever sizes GL gives us. In that case we query for the size.
1678 GrGLStencilAttachment::Format format = sFmt;
1679 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001680 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1681 sbDesc,
1682 width,
1683 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001684 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001685 format);
1686 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001687}
1688
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001689////////////////////////////////////////////////////////////////////////////////
1690
Brian Salomondbf70722019-02-07 11:31:24 -05001691sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1692 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001693 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001694}
1695
Chris Dalton2e7ed262020-02-21 15:17:59 -07001696void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1697 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001698 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1699 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1700 fHWScissorSettings.fEnabled = kYes_TriState;
1701 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001702 } else {
1703 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1704 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1705 fHWScissorSettings.fEnabled = kNo_TriState;
1706 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001707 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001708}
Brian Salomond818ebf2018-07-02 14:08:49 +00001709
Chris Dalton2e7ed262020-02-21 15:17:59 -07001710void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1711 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001712 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001713 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1714 if (fHWScissorSettings.fRect != nativeScissor) {
1715 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1716 nativeScissor.fHeight));
1717 fHWScissorSettings.fRect = nativeScissor;
1718 }
joshualitt77b13072014-10-27 14:51:01 -07001719}
1720
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001721void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001722 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001723#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001724 typedef GrWindowRectsState::Mode Mode;
1725 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1726 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001727
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001728 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001729 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001730 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001731 }
1732
csmartdalton7535f412016-08-23 06:51:00 -07001733 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1734 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001735 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001736 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001737
Chris Daltond6cda8d2019-09-05 02:30:04 -06001738 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001739 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001740 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001741 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001742 }
1743
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001744 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001745 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001746
Greg Danielacd66b42019-05-22 16:29:12 -04001747 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001748#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001749}
1750
1751void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001752#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001753 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001754 return;
1755 }
1756 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001757 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001758#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001759}
1760
Chris Daltond42d2002020-02-28 12:05:04 -07001761bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1762 const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001763 this->handleDirtyContext();
1764
Chris Daltond42d2002020-02-28 12:05:04 -07001765 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001766 if (!program) {
1767 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1768 return false;
1769 }
1770
1771 this->flushProgram(std::move(program));
1772
Chris Daltond42d2002020-02-28 12:05:04 -07001773 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1774 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1775 }
1776
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001777 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001778 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1779 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001780
Chris Dalton20e7a532020-02-28 15:37:53 +00001781 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001782
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001783 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001784 GrStencilSettings stencil;
1785 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001786 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1787 stencil.reset(*programInfo.pipeline().getUserStencil(),
1788 programInfo.pipeline().hasStencilClip(),
1789 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001790 }
Robert Phillips901aff02019-10-08 12:32:56 -04001791 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001792 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001793 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1794 glRT, programInfo.origin());
1795 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001796 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001797 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001798
1799 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001800 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001801 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001802
Chris Dalton20e7a532020-02-28 15:37:53 +00001803 return true;
1804}
1805
1806void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1807 if (!program) {
1808 fHWProgram.reset();
1809 fHWProgramID = 0;
1810 return;
1811 }
1812 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1813 if (program == fHWProgram) {
1814 return;
1815 }
1816 auto id = program->programID();
1817 SkASSERT(id);
1818 GL_CALL(UseProgram(id));
1819 fHWProgram = std::move(program);
1820 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001821}
1822
1823void GrGLGpu::flushProgram(GrGLuint id) {
1824 SkASSERT(id);
1825 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001826 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001827 return;
1828 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001829 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001830 GL_CALL(UseProgram(id));
1831 fHWProgramID = id;
1832}
1833
Brian Salomonae64c192019-02-05 09:41:37 -05001834GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001835 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001836
cdaltone2e71c22016-04-07 18:13:29 -07001837 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001838 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001839 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001840 }
cdaltone2e71c22016-04-07 18:13:29 -07001841
Brian Salomonae64c192019-02-05 09:41:37 -05001842 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001843 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001844 if (!bufferState->fBufferZeroKnownBound) {
1845 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1846 bufferState->fBufferZeroKnownBound = true;
1847 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001848 }
Brian Salomondbf70722019-02-07 11:31:24 -05001849 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1850 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001851 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1852 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1853 bufferState->fBufferZeroKnownBound = false;
1854 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001855 }
1856
Brian Salomonae64c192019-02-05 09:41:37 -05001857 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001858}
Brian Salomond818ebf2018-07-02 14:08:49 +00001859
Brian Osman9a9baae2018-11-05 15:06:26 -05001860void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001861 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001862 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001863 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001864 SkASSERT(!this->caps()->performColorClearsAsDraws());
1865 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001866
Brian Salomon43f8bf02017-10-18 08:33:29 -04001867 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001868
Brian Salomon43f8bf02017-10-18 08:33:29 -04001869 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1870
Brian Salomond818ebf2018-07-02 14:08:49 +00001871 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001872 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001873 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001874 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001875 }
Greg Danielacd66b42019-05-22 16:29:12 -04001876 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001877 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001878 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001879 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001880 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001881}
1882
Robert Phillips95214472017-08-08 18:00:03 -04001883void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001884 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1885
Robert Phillips95214472017-08-08 18:00:03 -04001886 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001887 return;
1888 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001889
Chris Dalton674f77a2019-09-30 20:49:39 -06001890 // This should only be called internally when we know we have a stencil buffer.
1891 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001892
bsalomonb0bd4f62014-09-03 07:19:50 -07001893 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001894 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001895
Chris Dalton2e7ed262020-02-21 15:17:59 -07001896 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07001897 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001898
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001899 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001900 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001901 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001902 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001903}
1904
Chris Daltonb50cc812019-10-14 16:29:21 -06001905static bool use_tiled_rendering(const GrGLCaps& glCaps,
1906 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1907 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1908 // Otherwise it's faster to just not use it.
1909 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1910 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1911}
1912
1913void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001914 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1915 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1916 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1917
1918 this->handleDirtyContext();
1919
1920 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1921 this->flushRenderTarget(glRT);
1922 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1923
Chris Daltonb50cc812019-10-14 16:29:21 -06001924 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1925 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1926 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1927 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1928 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1929 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1930 nativeBounds.fHeight, preserveMask));
1931 }
1932
Chris Dalton674f77a2019-09-30 20:49:39 -06001933 GrGLbitfield clearMask = 0;
1934 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1935 SkASSERT(!this->caps()->performColorClearsAsDraws());
1936 this->flushClearColor(colorLoadStore.fClearColor);
1937 this->flushColorWrite(true);
1938 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001939 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001940 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1941 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1942 GL_CALL(StencilMask(0xffffffff));
1943 GL_CALL(ClearStencil(0));
1944 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1945 }
1946 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001947 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001948 this->disableWindowRectangles();
1949 GL_CALL(Clear(clearMask));
1950 }
1951}
1952
1953void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1954 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1955 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1956 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1957
1958 this->handleDirtyContext();
1959
1960 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1961 // The framebuffer binding changed in the middle of a command buffer. We should have already
1962 // printed a warning during onFBOChanged.
1963 return;
1964 }
1965
1966 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1967 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1968
1969 SkSTArray<2, GrGLenum> discardAttachments;
1970 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1971 discardAttachments.push_back(
1972 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1973 }
1974 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1975 discardAttachments.push_back(
1976 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1977 }
1978
1979 if (!discardAttachments.empty()) {
1980 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1981 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1982 discardAttachments.begin()));
1983 } else {
1984 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1985 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1986 discardAttachments.begin()));
1987 }
1988 }
1989 }
1990
Chris Daltonb50cc812019-10-14 16:29:21 -06001991 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1992 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1993 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1994 // Handled by use_tiled_rendering().
1995 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1996 GL_CALL(EndTiling(preserveMask));
1997 }
1998
Chris Dalton674f77a2019-09-30 20:49:39 -06001999 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002000}
2001
csmartdalton29df7602016-08-31 11:55:52 -07002002void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2003 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002004 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002005 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002006 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002007 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002008
egdaniel8dc7c3a2015-04-16 11:22:42 -07002009 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002010 if (!sb) {
2011 // We should only get here if we marked a proxy as requiring a SB. However,
2012 // the SB creation could later fail. Likely clipping is going to go awry now.
2013 return;
2014 }
2015
bsalomon6bc1b5f2015-02-23 09:06:38 -08002016 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002017#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002018 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002019 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002020#else
2021 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002022 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002023 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002024 // changing the clip between stencil passes may or may not
2025 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002026 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002027#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002028 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002029 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002030 value = (1 << (stencilBitCount - 1));
2031 } else {
2032 value = 0;
2033 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002034 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002035 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002036
Greg Danielacd66b42019-05-22 16:29:12 -04002037 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002038 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002039
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002040 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002041 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002042 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002043 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002044}
2045
Brian Salomone05ba5a2019-04-08 11:59:07 -04002046bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002047 GrColorType surfaceColorType, GrColorType dstColorType,
2048 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002049 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002050
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002051 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002052 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002053 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002054 return false;
2055 }
Greg Danielba88ab62019-07-26 09:14:01 -04002056 GrGLenum externalFormat = 0;
2057 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002058 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2059 surfaceColorType,
2060 dstColorType,
2061 &externalFormat,
2062 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002063 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002064 return false;
2065 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002066
Brian Salomon71d9d842016-11-03 13:42:00 -04002067 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002068 if (renderTarget->numSamples() <= 1 ||
2069 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2070 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2071 this->flushRenderTargetNoColorWrites(renderTarget);
2072 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2073 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2074 return false;
2075 } else {
2076 SkASSERT(renderTarget->requiresManualMSAAResolve());
2077 // we don't track the state of the READ FBO ID.
2078 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002079 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002080 } else {
2081 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002082 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002083 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002084 }
2085
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002086 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002087 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002088
Brian Salomona6948702018-06-01 15:33:20 -04002089 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002090 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002091 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002092 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002093 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002094 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002095
Brian Osman1348ed02018-09-12 09:08:39 -04002096 bool reattachStencil = false;
2097 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2098 renderTarget &&
2099 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002100 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002101 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2102 reattachStencil = true;
2103 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2104 GR_GL_RENDERBUFFER, 0));
2105 }
2106
Chris Dalton76500e52019-09-05 02:13:05 -06002107 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002108 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002109
2110 if (reattachStencil) {
2111 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2112 renderTarget->renderTargetPriv().getStencilAttachment());
2113 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2114 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2115 }
2116
Brian Salomon26de56e2019-04-10 12:14:26 -04002117 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002118 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002119 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2120 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002121
Brian Salomone05ba5a2019-04-08 11:59:07 -04002122 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002123 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002124 }
2125 return true;
2126}
2127
2128bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002129 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2130 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002131 SkASSERT(surface);
2132
Brian Salomonb28cb682019-07-26 12:48:47 -04002133 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002134
Brian Salomon26de56e2019-04-10 12:14:26 -04002135 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2136 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002137
Brian Salomon1047a492019-07-02 12:25:21 -04002138 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002139 rowPixelWidth = width;
2140 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002141 SkASSERT(!(rowBytes % bytesPerPixel));
2142 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002143 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002144 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2145 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002146}
2147
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002148GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002149 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002150 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002151 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002152 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002153 if (!fCachedOpsRenderPass) {
2154 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002155 }
2156
Chris Daltonb50cc812019-10-14 16:29:21 -06002157 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002158 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002159}
2160
Brian Salomon1fabd512018-02-09 09:54:25 -05002161void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002162 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002163 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002164 this->didWriteToSurface(target, origin, &bounds);
2165}
bsalomon6ba6fa12015-03-04 11:57:37 -08002166
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002167void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2168 this->flushRenderTargetNoColorWrites(target);
2169 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002170}
2171
Brian Osman9aa30c62018-07-02 15:21:46 -04002172void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002173 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002174 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002175 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002176 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002177#ifdef SK_DEBUG
2178 // don't do this check in Chromium -- this is causing
2179 // lots of repeated command buffer flushes when the compositor is
2180 // rendering with Ganesh, which is really slow; even too slow for
2181 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002182 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002183 GrGLenum status;
2184 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2185 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2186 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2187 }
bsalomon160f24c2015-03-17 15:55:42 -07002188 }
egdanield803f272015-03-18 13:01:52 -07002189#endif
2190 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002191 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002192 }
2193
brianosman35b784d2016-05-05 11:52:53 -07002194 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002195 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002196 }
Brian Salomon9b553272020-01-24 14:38:37 -05002197
2198 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2199 GrGLint format;
2200 GrGLint type;
2201 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2202 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2203 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2204 }
bsalomon083617b2016-02-12 12:10:14 -08002205}
bsalomona9909122016-01-23 10:41:40 -08002206
brianosman33f6b3f2016-06-02 05:49:21 -07002207void GrGLGpu::flushFramebufferSRGB(bool enable) {
2208 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2209 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2210 fHWSRGBFramebuffer = kYes_TriState;
2211 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2212 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2213 fHWSRGBFramebuffer = kNo_TriState;
2214 }
2215}
2216
Greg Danielacd66b42019-05-22 16:29:12 -04002217void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002218 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002219 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002220 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002221 fHWViewport = viewport;
2222 }
2223}
2224
Chris Dalton33db9fc2020-02-25 18:52:12 -07002225GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002226 fStats.incNumDraws();
2227
Chris Dalton2e7ed262020-02-21 15:17:59 -07002228 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2229 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2230 GL_CALL(Enable(GR_GL_CULL_FACE));
2231 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002232 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002233 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002234
Chris Dalton3809bab2017-06-13 10:55:06 -06002235 switch (primitiveType) {
2236 case GrPrimitiveType::kTriangles:
2237 return GR_GL_TRIANGLES;
2238 case GrPrimitiveType::kTriangleStrip:
2239 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002240 case GrPrimitiveType::kPoints:
2241 return GR_GL_POINTS;
2242 case GrPrimitiveType::kLines:
2243 return GR_GL_LINES;
2244 case GrPrimitiveType::kLineStrip:
2245 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002246 case GrPrimitiveType::kPatches:
2247 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002248 case GrPrimitiveType::kPath:
2249 SK_ABORT("non-mesh-based GrPrimitiveType");
2250 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002251 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002252 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002253}
2254
Chris Daltond42d2002020-02-28 12:05:04 -07002255void GrGLGpu::drawArrays(GrPrimitiveType primitiveType, GrGLint baseVertex, GrGLsizei vertexCount) {
2256 SkASSERT(!this->glCaps().drawArraysBaseVertexIsBroken() || 0 == baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -07002257 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltond42d2002020-02-28 12:05:04 -07002258 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
Chris Dalton114a3c02017-05-26 15:17:19 -06002259}
2260
Chris Daltond42d2002020-02-28 12:05:04 -07002261void GrGLGpu::drawElements(GrPrimitiveType primitiveType, GrGLsizei indexCount, GrGLenum indexType,
2262 const void* indices) {
2263 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2264 GL_CALL(DrawElements(glPrimType, indexCount, indexType, indices));
Brian Salomondbf70722019-02-07 11:31:24 -05002265}
2266
Chris Daltond42d2002020-02-28 12:05:04 -07002267void GrGLGpu::drawRangeElements(GrPrimitiveType primitiveType, GrGLuint minIndexValue,
2268 GrGLuint maxIndexValue, GrGLsizei indexCount, GrGLenum indexType,
2269 const void* indices) {
2270 SkASSERT(this->glCaps().drawRangeElementsSupport());
Chris Dalton33db9fc2020-02-25 18:52:12 -07002271 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltond42d2002020-02-28 12:05:04 -07002272 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, indexType,
2273 indices));
Chris Dalton114a3c02017-05-26 15:17:19 -06002274}
2275
Chris Daltond42d2002020-02-28 12:05:04 -07002276void GrGLGpu::drawArraysInstanced(GrPrimitiveType primitiveType, GrGLint baseVertex,
2277 GrGLsizei vertexCount, GrGLsizei instanceCount) {
2278 SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
Chris Dalton33db9fc2020-02-25 18:52:12 -07002279 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltond42d2002020-02-28 12:05:04 -07002280 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
Chris Dalton1d616352017-05-31 12:51:23 -06002281}
2282
Chris Daltond42d2002020-02-28 12:05:04 -07002283void GrGLGpu::drawElementsInstanced(GrPrimitiveType primitiveType, GrGLsizei indexCount,
2284 GrGLenum indexType, const void* indices,
2285 GrGLsizei instanceCount) {
2286 SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
Chris Dalton33db9fc2020-02-25 18:52:12 -07002287 GrGLenum glPrimType = this->prepareToDraw(primitiveType);
Chris Daltond42d2002020-02-28 12:05:04 -07002288 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, indexType, indices, instanceCount));
Chris Dalton1d616352017-05-31 12:51:23 -06002289}
2290
Chris Dalton16a33c62019-09-24 22:19:17 -06002291void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002292 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002293 // Some extensions automatically resolves the texture when it is read.
2294 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2295
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002296 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002297 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2298 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2299 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2300 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002301
Chris Daltonc139d082019-09-26 14:04:24 -06002302 // make sure we go through flushRenderTarget() since we've modified
2303 // the bound DRAW FBO ID.
2304 fHWBoundRenderTargetUniqueID.makeInvalid();
2305 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2306 // Apple's extension uses the scissor as the blit bounds.
2307 GrScissorState scissorState;
2308 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002309 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2310 // happens inside flushScissor since resolveRect is already in native device coordinates.
2311 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002312 this->disableWindowRectangles();
2313 GL_CALL(ResolveMultisampleFramebuffer());
2314 } else {
2315 int l, b, r, t;
2316 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2317 this->glCaps().blitFramebufferSupportFlags()) {
2318 l = 0;
2319 b = 0;
2320 r = target->width();
2321 t = target->height();
2322 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002323 l = resolveRect.x();
2324 b = resolveRect.y();
2325 r = resolveRect.x() + resolveRect.width();
2326 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002327 }
Chris Daltonc139d082019-09-26 14:04:24 -06002328
2329 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002330 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002331 this->disableWindowRectangles();
2332 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 +00002333 }
2334}
2335
bsalomon@google.com411dad02012-06-05 20:24:20 +00002336namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002337
bsalomon@google.com411dad02012-06-05 20:24:20 +00002338
2339GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002340 static const GrGLenum gTable[kGrStencilOpCount] = {
2341 GR_GL_KEEP, // kKeep
2342 GR_GL_ZERO, // kZero
2343 GR_GL_REPLACE, // kReplace
2344 GR_GL_INVERT, // kInvert
2345 GR_GL_INCR_WRAP, // kIncWrap
2346 GR_GL_DECR_WRAP, // kDecWrap
2347 GR_GL_INCR, // kIncClamp
2348 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002349 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002350 static_assert(0 == (int)GrStencilOp::kKeep);
2351 static_assert(1 == (int)GrStencilOp::kZero);
2352 static_assert(2 == (int)GrStencilOp::kReplace);
2353 static_assert(3 == (int)GrStencilOp::kInvert);
2354 static_assert(4 == (int)GrStencilOp::kIncWrap);
2355 static_assert(5 == (int)GrStencilOp::kDecWrap);
2356 static_assert(6 == (int)GrStencilOp::kIncClamp);
2357 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002358 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2359 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002360}
2361
2362void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002363 const GrStencilSettings::Face& face,
2364 GrGLenum glFace) {
2365 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2366 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2367 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002368
cdalton93a379b2016-05-11 13:58:08 -07002369 GrGLint ref = face.fRef;
2370 GrGLint mask = face.fTestMask;
2371 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002372
2373 if (GR_GL_FRONT_AND_BACK == glFace) {
2374 // we call the combined func just in case separate stencil is not
2375 // supported.
2376 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2377 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002378 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002379 } else {
2380 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2381 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002382 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002383 }
2384}
2385}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002386
Chris Dalton71713f62019-04-18 12:41:03 -06002387void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002388 if (stencilSettings.isDisabled()) {
2389 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002390 } else if (fHWStencilSettings != stencilSettings ||
2391 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002392 if (kYes_TriState != fHWStencilTestEnabled) {
2393 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002394
csmartdaltonc7d85332016-10-26 10:13:46 -07002395 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002396 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002397 if (!stencilSettings.isTwoSided()) {
2398 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2399 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002400 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002401 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2402 GR_GL_FRONT);
2403 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2404 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002405 }
joshualitta58fe352014-10-27 08:39:00 -07002406 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002407 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 }
2409}
2410
csmartdaltonc7d85332016-10-26 10:13:46 -07002411void GrGLGpu::disableStencil() {
2412 if (kNo_TriState != fHWStencilTestEnabled) {
2413 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002414
csmartdaltonc7d85332016-10-26 10:13:46 -07002415 fHWStencilTestEnabled = kNo_TriState;
2416 fHWStencilSettings.invalidate();
2417 }
2418}
2419
Chris Dalton4c56b032019-03-04 12:28:42 -07002420void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002421 // rt is only optional if useHWAA is false.
2422 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002423#ifdef SK_DEBUG
2424 if (useHWAA && rt->numSamples() <= 1) {
2425 SkASSERT(this->caps()->mixedSamplesSupport());
2426 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2427 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2428 }
2429#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002430
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002431 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002432 if (useHWAA) {
2433 if (kYes_TriState != fMSAAEnabled) {
2434 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2435 fMSAAEnabled = kYes_TriState;
2436 }
2437 } else {
2438 if (kNo_TriState != fMSAAEnabled) {
2439 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2440 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002441 }
2442 }
2443 }
2444}
2445
Chris Daltonce425af2019-12-16 10:39:03 -07002446void GrGLGpu::flushConservativeRasterState(bool enabled) {
2447 if (this->caps()->conservativeRasterSupport()) {
2448 if (enabled) {
2449 if (kYes_TriState != fHWConservativeRasterEnabled) {
2450 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2451 fHWConservativeRasterEnabled = kYes_TriState;
2452 }
2453 } else {
2454 if (kNo_TriState != fHWConservativeRasterEnabled) {
2455 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2456 fHWConservativeRasterEnabled = kNo_TriState;
2457 }
2458 }
2459 }
2460}
2461
Chris Dalton1215cda2019-12-17 21:44:04 -07002462void GrGLGpu::flushWireframeState(bool enabled) {
2463 if (this->caps()->wireframeSupport()) {
2464 if (this->caps()->wireframeMode() || enabled) {
2465 if (kYes_TriState != fHWWireframeEnabled) {
2466 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2467 fHWWireframeEnabled = kYes_TriState;
2468 }
2469 } else {
2470 if (kNo_TriState != fHWWireframeEnabled) {
2471 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2472 fHWWireframeEnabled = kNo_TriState;
2473 }
2474 }
2475 }
2476}
2477
Chris Daltonbbb3f642019-07-24 12:25:08 -04002478void GrGLGpu::flushBlendAndColorWrite(
2479 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2480 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2481 // We need to work around a driver bug by using a blend state that preserves the dst color,
2482 // rather than disabling color writes.
2483 GrXferProcessor::BlendInfo preserveDstBlend;
2484 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2485 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2486 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2487 return;
2488 }
bsalomonf7cc8772015-05-11 11:21:14 -07002489
cdalton8917d622015-05-06 13:40:21 -07002490 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002491 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2492 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002493
2494 // Any optimization to disable blending should have already been applied and
2495 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002496 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2497 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002498
egdanielb414f252014-07-29 13:15:47 -07002499 if (blendOff) {
2500 if (kNo_TriState != fHWBlendState.fEnabled) {
2501 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002502
2503 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2504 // https://code.google.com/p/skia/issues/detail?id=3943
2505 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2506 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2507 SkASSERT(this->caps()->advancedBlendEquationSupport());
2508 // Set to any basic blending equation.
2509 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2510 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2511 fHWBlendState.fEquation = blend_equation;
2512 }
2513
egdanielb414f252014-07-29 13:15:47 -07002514 fHWBlendState.fEnabled = kNo_TriState;
2515 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002516 } else {
2517 if (kYes_TriState != fHWBlendState.fEnabled) {
2518 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002519
Chris Daltonbbb3f642019-07-24 12:25:08 -04002520 fHWBlendState.fEnabled = kYes_TriState;
2521 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002522
Chris Daltonbbb3f642019-07-24 12:25:08 -04002523 if (fHWBlendState.fEquation != equation) {
2524 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2525 fHWBlendState.fEquation = equation;
2526 }
cdalton8917d622015-05-06 13:40:21 -07002527
Chris Daltonbbb3f642019-07-24 12:25:08 -04002528 if (GrBlendEquationIsAdvanced(equation)) {
2529 SkASSERT(this->caps()->advancedBlendEquationSupport());
2530 // Advanced equations have no other blend state.
2531 return;
2532 }
cdalton8917d622015-05-06 13:40:21 -07002533
Chris Daltonbbb3f642019-07-24 12:25:08 -04002534 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2535 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2536 gXfermodeCoeff2Blend[dstCoeff]));
2537 fHWBlendState.fSrcCoeff = srcCoeff;
2538 fHWBlendState.fDstCoeff = dstCoeff;
2539 }
cdalton8917d622015-05-06 13:40:21 -07002540
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002541 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002542 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2543 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2544 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2545 fHWBlendState.fConstColor = blendConst;
2546 fHWBlendState.fConstColorValid = true;
2547 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002548 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002549 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002550
2551 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002552}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002553
Brian Salomondc829942018-10-23 16:07:24 -04002554static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002555 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002556 switch (swizzle[i]) {
2557 case 'r': glValues[i] = GR_GL_RED; break;
2558 case 'g': glValues[i] = GR_GL_GREEN; break;
2559 case 'b': glValues[i] = GR_GL_BLUE; break;
2560 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002561 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002562 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002563 default: SK_ABORT("Unsupported component");
2564 }
Brian Salomon327955b2018-10-22 15:39:22 +00002565 }
2566}
2567
Greg Daniel2c3398d2019-06-19 11:58:01 -04002568void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2569 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002570 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002571
reed856e9d92015-09-30 12:21:45 -07002572#ifdef SK_DEBUG
2573 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002574 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002575 const int w = texture->width();
2576 const int h = texture->height();
2577 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2578 }
2579 }
2580#endif
2581
Robert Phillips294870f2016-11-11 12:38:40 -05002582 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002583 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002584 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002585 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002586 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002587 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002588 }
2589
Brian Salomondc829942018-10-23 16:07:24 -04002590 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002591 if (!this->caps()->mipMapSupport() ||
2592 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002593 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002594 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002595 }
bsalomonefd7d452014-10-23 14:17:46 -07002596
brianosman33f6b3f2016-06-02 05:49:21 -07002597#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002598 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002599 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002600 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002601 }
2602#endif
2603
Brian Salomone2826ab2019-06-04 15:58:31 -04002604 auto timestamp = texture->parameters()->resetTimestamp();
2605 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002606
Brian Salomone2826ab2019-06-04 15:58:31 -04002607 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2608 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002609 if (fSamplerObjectCache) {
2610 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2611 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002612 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2613 texture->parameters()->samplerOverriddenState();
2614 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002615
Brian Salomone2826ab2019-06-04 15:58:31 -04002616 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2617 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002618
Brian Salomone2826ab2019-06-04 15:58:31 -04002619 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2620 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002621
2622 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002623 newSamplerState.fMinLOD = -1000.f;
2624 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002625
Brian Salomone2826ab2019-06-04 15:58:31 -04002626 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002627 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002629 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002631 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002632 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002633 }
Mike Klein1bccae52018-10-19 11:38:44 +00002634 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002635 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002636 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002637 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002638 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002639 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002640 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002642 }
2643 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002644 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002645 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002646 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002647 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002649 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002651 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002652 if (this->glCaps().clampToBorderSupport()) {
2653 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002654 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002655 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002656 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2657 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002658 }
2659 }
Brian Salomondc829942018-10-23 16:07:24 -04002660 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2662 newNonsamplerState.fBaseMipMapLevel = 0;
2663 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002664
Brian Salomone2826ab2019-06-04 15:58:31 -04002665 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2666 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002667 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002668 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2669 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002670 GrGLenum glValues[4];
2671 get_gl_swizzle_values(swizzle, glValues);
2672 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002673 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002674 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002675 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2676 reinterpret_cast<const GrGLint*>(glValues)));
2677 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002678 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2679 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2680 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2681 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2682 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002683 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002684 }
2685 }
Brian Salomondc829942018-10-23 16:07:24 -04002686 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002687 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2688 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2689 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002690 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002691 this->setTextureUnit(unitIdx);
2692 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002693 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002694 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002695 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002696 this->setTextureUnit(unitIdx);
2697 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002698 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002699 }
Brian Salomon327955b2018-10-22 15:39:22 +00002700 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002701 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2702 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002703}
2704
Brian Salomon1f05d452019-02-08 12:33:08 -05002705void GrGLGpu::onResetTextureBindings() {
2706 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2707 GR_GL_TEXTURE_EXTERNAL};
2708 for (int i = 0; i < this->numTextureUnits(); ++i) {
2709 this->setTextureUnit(i);
2710 for (auto target : kTargets) {
2711 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2712 GL_CALL(BindTexture(target, 0));
2713 }
2714 }
2715 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2716 }
2717}
2718
Chris Dalton5a2f9622019-12-27 14:56:38 -07002719void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2720 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2721 if (fHWPatchVertexCount != count) {
2722 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2723 fHWPatchVertexCount = count;
2724 }
2725}
2726
egdaniel080e6732014-12-22 07:35:52 -08002727void GrGLGpu::flushColorWrite(bool writeColor) {
2728 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002729 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002730 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2731 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002732 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002733 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002734 } else {
2735 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002736 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002737 fHWWriteToColor = kYes_TriState;
2738 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002739 }
bsalomon3e791242014-12-17 13:43:13 -08002740}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002741
Chris Dalton674f77a2019-09-30 20:49:39 -06002742void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2743 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2744 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2745 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2746 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2747 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2748 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2749 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002750 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2751 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2752 GL_CALL(ClearColor(r, g, b, a));
2753 fHWClearColor[0] = r;
2754 fHWClearColor[1] = g;
2755 fHWClearColor[2] = b;
2756 fHWClearColor[3] = a;
2757 }
2758}
2759
bsalomon861e1032014-12-16 07:33:49 -08002760void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002761 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002762 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002763 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002764 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002765 }
2766}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002767
Brian Salomond978b902019-02-07 15:09:18 -05002768void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002769 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002770 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002771 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2772 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2773 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002774 }
Brian Salomond978b902019-02-07 15:09:18 -05002775 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2776 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002777 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002778 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002779}
2780
Brian Salomone5e7eb12016-10-14 16:18:33 -04002781// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002782static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2783 const GrSurface* src,
2784 const SkIRect& srcRect,
2785 const SkIPoint& dstPoint,
2786 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002787 int dstSampleCnt = 0;
2788 int srcSampleCnt = 0;
2789 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002790 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002791 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002792 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002793 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002794 }
2795 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2796 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2797
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002798 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2799 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2800
Brian Salomone5e7eb12016-10-14 16:18:33 -04002801 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002802 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002803
Greg Daniel46cfbc62019-06-07 11:43:30 -04002804 GrTextureType dstTexType;
2805 GrTextureType* dstTexTypePtr = nullptr;
2806 GrTextureType srcTexType;
2807 GrTextureType* srcTexTypePtr = nullptr;
2808 if (dstTex) {
2809 dstTexType = dstTex->texturePriv().textureType();
2810 dstTexTypePtr = &dstTexType;
2811 }
2812 if (srcTex) {
2813 srcTexType = srcTex->texturePriv().textureType();
2814 srcTexTypePtr = &srcTexType;
2815 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002816
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002817 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2818 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002819 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002820}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002821
Brian Osmana9c8a052018-01-19 10:31:56 -05002822static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2823 // A RT has a separate MSAA renderbuffer if:
2824 // 1) It's multisampled
2825 // 2) We're using an extension with separate MSAA renderbuffers
2826 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002827 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002828}
2829
Greg Daniel46cfbc62019-06-07 11:43:30 -04002830static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002831 const GrGLCaps& caps) {
2832
bsalomon@google.comeb851172013-04-15 13:51:00 +00002833 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002834 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002835 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002836 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002837
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002838 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2839 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2840
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002841 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2842 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2843
Greg Daniel46cfbc62019-06-07 11:43:30 -04002844 GrTextureType dstTexType;
2845 GrTextureType* dstTexTypePtr = nullptr;
2846 GrTextureType srcTexType;
2847 GrTextureType* srcTexTypePtr = nullptr;
2848 if (dstTex) {
2849 dstTexType = dstTex->texturePriv().textureType();
2850 dstTexTypePtr = &dstTexType;
2851 }
2852 if (srcTex) {
2853 srcTexType = srcTex->texturePriv().textureType();
2854 srcTexTypePtr = &srcTexType;
2855 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002856
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002857 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2858 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002859}
2860
Greg Danielacd66b42019-05-22 16:29:12 -04002861// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002862void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002863 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002864 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002865 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002866 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002867 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2868 GrGLuint texID = texture->textureID();
2869 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002870 GrGLuint* tempFBOID;
2871 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002872
egdanield803f272015-03-18 13:01:52 -07002873 if (0 == *tempFBOID) {
2874 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002875 }
2876
Adrienne Walker4ee88512018-05-17 11:37:14 -07002877 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002878 GR_GL_CALL(
2879 this->glInterface(),
2880 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2881 if (mipLevel == 0) {
2882 texture->baseLevelWasBoundToFBO();
2883 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002884 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002885 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002886 }
egdaniel0f5f9672015-02-03 11:10:51 -08002887}
2888
Brian Salomond2a8ae22019-09-10 16:03:59 -04002889void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002890 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002891 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002892 SkASSERT(surface->asTexture());
2893 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2894 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2895 GR_GL_COLOR_ATTACHMENT0,
2896 textureTarget,
2897 0,
2898 0));
2899 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002900}
2901
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002902void GrGLGpu::onFBOChanged() {
2903 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2904 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2905 GL_CALL(Flush());
2906 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002907#ifdef SK_DEBUG
2908 if (fIsExecutingCommandBuffer_DebugOnly) {
2909 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2910 "This will severely hurt performance.\n");
2911 }
2912#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002913}
2914
Adrienne Walker4ee88512018-05-17 11:37:14 -07002915void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2916 fStats.incRenderTargetBinds();
2917 GL_CALL(BindFramebuffer(target, fboid));
2918 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2919 fBoundDrawFramebuffer = fboid;
2920 }
2921
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002922 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2923 // The driver forgets the correct scissor when modifying the FBO binding.
2924 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002925 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002926 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002927 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002928 }
2929
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002930 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002931}
2932
Adrienne Walker4ee88512018-05-17 11:37:14 -07002933void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002934 // We're relying on the GL state shadowing being correct in the workaround code below so we
2935 // need to handle a dirty context.
2936 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002937 if (fboid == fBoundDrawFramebuffer &&
2938 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2939 // This workaround only applies to deleting currently bound framebuffers
2940 // on Adreno 420. Because this is a somewhat rare case, instead of
2941 // tracking all the attachments of every framebuffer instead just always
2942 // unbind all attachments.
2943 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2944 GR_GL_RENDERBUFFER, 0));
2945 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2946 GR_GL_RENDERBUFFER, 0));
2947 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2948 GR_GL_RENDERBUFFER, 0));
2949 }
2950
2951 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002952
2953 // Deleting the currently bound framebuffer rebinds to 0.
2954 if (fboid == fBoundDrawFramebuffer) {
2955 this->onFBOChanged();
2956 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002957}
2958
Greg Daniel46cfbc62019-06-07 11:43:30 -04002959bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002960 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002961 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2962 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2963 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002964 auto dstFormat = dst->backendFormat().asGLFormat();
2965 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002966 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002967 return true;
2968 }
2969 }
cblume61214052016-01-26 09:10:48 -08002970
Greg Daniel46cfbc62019-06-07 11:43:30 -04002971 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2972 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002973 return true;
2974 }
2975
Greg Daniel46cfbc62019-06-07 11:43:30 -04002976 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2977 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002978 }
2979
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002980 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002981 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002982 return true;
2983 }
bsalomon083617b2016-02-12 12:10:14 -08002984 }
2985
bsalomon6df86402015-06-01 10:41:49 -07002986 return false;
2987}
2988
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002989bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002990 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991
2992 int progIdx = TextureToCopyProgramIdx(srcTex);
2993 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2994 GrSLType samplerType =
2995 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2996
2997 if (!fCopyProgramArrayBuffer) {
2998 static const GrGLfloat vdata[] = {
2999 0, 0,
3000 0, 1,
3001 1, 0,
3002 1, 1
3003 };
3004 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3005 kStatic_GrAccessPattern, vdata);
3006 }
3007 if (!fCopyProgramArrayBuffer) {
3008 return false;
3009 }
3010
3011 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3012 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3013 if (!fCopyPrograms[progIdx].fProgram) {
3014 return false;
3015 }
3016
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003017 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003018 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003019 GrShaderVar::TypeModifier::Uniform);
3020 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3021 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3022 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3023 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003024
Greg Daniel9e3169b2019-06-06 14:38:17 -04003025 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003026 if (shaderCaps->noperspectiveInterpolationSupport()) {
3027 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3028 vshaderTxt.appendf("#extension %s : require\n", extension);
3029 }
3030 vTexCoord.addModifier("noperspective");
3031 }
3032
3033 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3034 vshaderTxt.append(";");
3035 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3036 vshaderTxt.append(";");
3037 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3038 vshaderTxt.append(";");
3039 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3040 vshaderTxt.append(";");
3041
3042 vshaderTxt.append(
3043 "// Copy Program VS\n"
3044 "void main() {"
3045 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3046 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3047 " sk_Position.zw = half2(0, 1);"
3048 "}"
3049 );
3050
Greg Daniel9e3169b2019-06-06 14:38:17 -04003051 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003052 if (shaderCaps->noperspectiveInterpolationSupport()) {
3053 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3054 fshaderTxt.appendf("#extension %s : require\n", extension);
3055 }
3056 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003057 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003058 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3059 fshaderTxt.append(";");
3060 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3061 fshaderTxt.append(";");
3062 fshaderTxt.appendf(
3063 "// Copy Program FS\n"
3064 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003065 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003066 "}"
3067 );
3068
3069 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3070 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3071 SkSL::Program::Settings settings;
3072 settings.fCaps = shaderCaps;
3073 SkSL::String glsl;
3074 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3075 sksl, settings, &glsl, errorHandler);
3076 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3077 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3078 SkASSERT(program->fInputs.isEmpty());
3079
3080 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3081 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3082 errorHandler);
3083 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3084 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3085 errorHandler);
3086 SkASSERT(program->fInputs.isEmpty());
3087
3088 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3089
3090 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3091 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3092 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3093 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3094 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3095 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3096
3097 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3098
3099 GL_CALL(DeleteShader(vshader));
3100 GL_CALL(DeleteShader(fshader));
3101
3102 return true;
3103}
3104
brianosman33f6b3f2016-06-02 05:49:21 -07003105bool GrGLGpu::createMipmapProgram(int progIdx) {
3106 const bool oddWidth = SkToBool(progIdx & 0x2);
3107 const bool oddHeight = SkToBool(progIdx & 0x1);
3108 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3109
Brian Salomon1edc5b92016-11-29 13:43:46 -05003110 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003111
3112 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3113 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3114 if (!fMipmapPrograms[progIdx].fProgram) {
3115 return false;
3116 }
3117
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003118 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003119 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003120 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003121 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003122 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003123 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003124 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003125 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3126 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3127 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3128 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003129 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003130 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003131
Ethan Nicholasfc994162019-06-06 10:04:27 -04003132 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003133 if (shaderCaps->noperspectiveInterpolationSupport()) {
3134 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003135 vshaderTxt.appendf("#extension %s : require\n", extension);
3136 }
3137 vTexCoords[0].addModifier("noperspective");
3138 vTexCoords[1].addModifier("noperspective");
3139 vTexCoords[2].addModifier("noperspective");
3140 vTexCoords[3].addModifier("noperspective");
3141 }
3142
Brian Salomon1edc5b92016-11-29 13:43:46 -05003143 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003144 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003145 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003146 vshaderTxt.append(";");
3147 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003148 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003149 vshaderTxt.append(";");
3150 }
3151
3152 vshaderTxt.append(
3153 "// Mipmap Program VS\n"
3154 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003155 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3156 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003157 );
3158
3159 // Insert texture coordinate computation:
3160 if (oddWidth && oddHeight) {
3161 vshaderTxt.append(
3162 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003163 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3164 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003165 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3166 );
3167 } else if (oddWidth) {
3168 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003169 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3170 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003171 );
3172 } else if (oddHeight) {
3173 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003174 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3175 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003176 );
3177 } else {
3178 vshaderTxt.append(
3179 " v_texCoord0 = a_vertex.xy;"
3180 );
3181 }
3182
3183 vshaderTxt.append("}");
3184
Ethan Nicholasfc994162019-06-06 10:04:27 -04003185 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003186 if (shaderCaps->noperspectiveInterpolationSupport()) {
3187 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003188 fshaderTxt.appendf("#extension %s : require\n", extension);
3189 }
3190 }
brianosman33f6b3f2016-06-02 05:49:21 -07003191 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003192 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003193 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003194 fshaderTxt.append(";");
3195 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003196 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003197 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003198 fshaderTxt.append(
3199 "// Mipmap Program FS\n"
3200 "void main() {"
3201 );
3202
3203 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003204 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003205 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3206 " sample(u_texture, v_texCoord1) + "
3207 " sample(u_texture, v_texCoord2) + "
3208 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003209 );
3210 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003211 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003212 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3213 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003214 );
3215 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003216 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003217 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003218 );
3219 }
3220
3221 fshaderTxt.append("}");
3222
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003223 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003224 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003225 SkSL::Program::Settings settings;
3226 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003227 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003228 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003229 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003230 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003231 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003232 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003233
Brian Osman6c431d52019-04-15 16:31:54 -04003234 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003235 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3236 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003237 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003238 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003239 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003240 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003241
3242 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3243
3244 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3245 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3246 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3247 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3248
3249 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3250
3251 GL_CALL(DeleteShader(vshader));
3252 GL_CALL(DeleteShader(fshader));
3253
3254 return true;
3255}
3256
Greg Daniel46cfbc62019-06-07 11:43:30 -04003257bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003258 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003259 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3260 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3261 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3262 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003263 return false;
3264 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003265 int progIdx = TextureToCopyProgramIdx(srcTex);
3266 if (!dstRT) {
3267 SkASSERT(dstTex);
3268 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3269 return false;
3270 }
3271 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003272 if (!fCopyPrograms[progIdx].fProgram) {
3273 if (!this->createCopyProgram(srcTex)) {
3274 SkDebugf("Failed to create copy program.\n");
3275 return false;
3276 }
3277 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003278 int w = srcRect.width();
3279 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003280 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003281 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003282 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003283 this->flushViewport(dst->width(), dst->height());
3284 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003285 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003286 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003287 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003288 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3289 attribs->enableVertexArrays(this, 1);
3290 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3291 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003292 // dst rect edges in NDC (-1 to 1)
3293 int dw = dst->width();
3294 int dh = dst->height();
3295 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3296 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3297 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3298 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003299 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3300 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3301 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3302 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3303 int sw = src->width();
3304 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003305 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3306 // src rect edges in normalized texture space (0 to 1)
3307 sx0 /= sw;
3308 sx1 /= sw;
3309 sy0 /= sh;
3310 sy1 /= sh;
3311 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003312 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3313 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3314 sx1 - sx0, sy1 - sy0, sx0, sy0));
3315 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003316 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003317 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003318 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003319 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003320 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003321 this->disableWindowRectangles();
3322 this->disableStencil();
3323 if (this->glCaps().srgbWriteControl()) {
3324 this->flushFramebufferSRGB(true);
3325 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003326 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003327 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003328 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3329 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003330 return true;
3331}
3332
Greg Daniel46cfbc62019-06-07 11:43:30 -04003333void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003334 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003335 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003336 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003337 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003338 SkASSERT(dstTex);
3339 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003340 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003341
Brian Salomond978b902019-02-07 15:09:18 -05003342 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003343 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003344 dstPoint.fX, dstPoint.fY,
3345 srcRect.fLeft, srcRect.fTop,
3346 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003347 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003348 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3349 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003350 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3351 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003352}
3353
Greg Daniel46cfbc62019-06-07 11:43:30 -04003354bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003355 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003356 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003357 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3358 srcRect.width(), srcRect.height());
3359 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003360 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003361 return false;
mtklein404b3b22015-05-18 09:29:10 -07003362 }
bsalomon5df6fee2015-05-18 06:26:15 -07003363 }
bsalomon6df86402015-06-01 10:41:49 -07003364
Brian Salomond2a8ae22019-09-10 16:03:59 -04003365 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3366 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003367 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003368 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003369
3370 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003371 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003372 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003373
Greg Daniel46cfbc62019-06-07 11:43:30 -04003374 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3375 srcRect.fTop,
3376 srcRect.fRight,
3377 srcRect.fBottom,
3378 dstRect.fLeft,
3379 dstRect.fTop,
3380 dstRect.fRight,
3381 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003382 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003383 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3384 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003385
3386 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3387 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003388 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003389}
3390
Brian Salomon930f9392018-06-20 16:25:26 -04003391bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3392 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003393 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003394 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003395 return false;
3396 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003397 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003398 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3399 // Uses draw calls to do a series of downsample operations to successive mips.
3400
3401 // The manual approach requires the ability to limit which level we're sampling and that the
3402 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003403 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003404 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003405 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003406 GL_CALL(GenerateMipmap(glTex->target()));
3407 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003408 }
3409
brianosman33f6b3f2016-06-02 05:49:21 -07003410 int width = texture->width();
3411 int height = texture->height();
3412 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003413 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003414
3415 // Create (if necessary), then bind temporary FBO:
3416 if (0 == fTempDstFBOID) {
3417 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3418 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003419 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003420 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003421
3422 // Bind the texture, to get things configured for filtering.
3423 // We'll be changing our base level further below:
3424 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003425 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003426 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003427
3428 // Vertex data:
3429 if (!fMipmapProgramArrayBuffer) {
3430 static const GrGLfloat vdata[] = {
3431 0, 0,
3432 0, 1,
3433 1, 0,
3434 1, 1
3435 };
Brian Salomonae64c192019-02-05 09:41:37 -05003436 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003437 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003438 }
3439 if (!fMipmapProgramArrayBuffer) {
3440 return false;
3441 }
3442
3443 fHWVertexArrayState.setVertexArrayID(this, 0);
3444
3445 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003446 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003447 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003448 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003449
3450 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003451 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003452 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003453 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003454 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003455 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003456
3457 // Do all the blits:
3458 width = texture->width();
3459 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003460
brianosman33f6b3f2016-06-02 05:49:21 -07003461 for (GrGLint level = 1; level < levelCount; ++level) {
3462 // Get and bind the program for this particular downsample (filter shape can vary):
3463 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3464 if (!fMipmapPrograms[progIdx].fProgram) {
3465 if (!this->createMipmapProgram(progIdx)) {
3466 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003467 // Invalidate all params to cover base level change in a previous iteration.
3468 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003469 return false;
3470 }
3471 }
Brian Salomon802cb312018-06-08 18:05:20 -04003472 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003473
3474 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3475 const float invWidth = 1.0f / width;
3476 const float invHeight = 1.0f / height;
3477 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3478 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3479 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3480
3481 // Only sample from previous mip
3482 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3483
Brian Salomon930f9392018-06-20 16:25:26 -04003484 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3485 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003486
Brian Osman788b9162020-02-07 10:36:46 -05003487 width = std::max(1, width / 2);
3488 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003489 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003490
3491 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3492 }
3493
3494 // Unbind:
3495 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3496 GR_GL_TEXTURE_2D, 0, 0));
3497
Brian Salomon930f9392018-06-20 16:25:26 -04003498 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003499 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3500 // We drew the 2nd to last level into the last level.
3501 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3502 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003503
brianosman33f6b3f2016-06-02 05:49:21 -07003504 return true;
3505}
3506
Chris Daltond7291ba2019-03-07 14:17:03 -07003507void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003508 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3509 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003510
3511 int effectiveSampleCnt;
3512 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003513 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003514
3515 sampleLocations->reset(effectiveSampleCnt);
3516 for (int i = 0; i < effectiveSampleCnt; ++i) {
3517 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3518 }
3519}
3520
cdalton231c5fd2015-05-13 12:35:36 -07003521void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003522 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003523 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003524 case kTexture_GrXferBarrierType: {
3525 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003526 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003527 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3528 // The render target uses separate storage so no need for glTextureBarrier.
3529 // FIXME: The render target will resolve automatically when its texture is bound,
3530 // but we could resolve only the bounds that will be read if we do it here instead.
3531 return;
3532 }
cdalton9954bc32015-04-29 14:17:00 -07003533 SkASSERT(this->caps()->textureBarrierSupport());
3534 GL_CALL(TextureBarrier());
3535 return;
cdalton231c5fd2015-05-13 12:35:36 -07003536 }
cdalton8917d622015-05-06 13:40:21 -07003537 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003538 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003539 this->caps()->blendEquationSupport());
3540 GL_CALL(BlendBarrier());
3541 return;
bsalomoncb02b382015-08-12 11:14:50 -07003542 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003543 }
3544}
3545
Chris Daltone1196c52019-12-28 14:31:09 -07003546void GrGLGpu::insertManualFramebufferBarrier() {
3547 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3548 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3549}
3550
Brian Salomon85c3d682019-11-04 15:04:54 -05003551GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003552 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003553 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003554 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003555 GrProtected isProtected,
3556 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003557 // We don't support protected textures in GL.
3558 if (isProtected == GrProtected::kYes) {
3559 return {};
3560 }
3561
Brian Salomon8a375832018-03-14 10:21:40 -04003562 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003563
Brian Salomond4764a12019-08-08 12:08:24 -04003564 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003565 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003566 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003567 }
3568
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003569 int numMipLevels = 1;
3570 if (mipMapped == GrMipMapped::kYes) {
3571 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3572 }
3573
Robert Phillipsd34691b2019-09-24 13:38:43 -04003574 // Compressed formats go through onCreateCompressedBackendTexture
3575 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3576
Greg Daniel15500642019-06-27 03:05:55 +00003577 GrGLTextureInfo info;
3578 GrGLTextureParameters::SamplerOverriddenState initialState;
3579
Greg Danield51fa2f2020-01-22 16:53:38 -05003580 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003581 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003582 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003583
Robert Phillipsd34691b2019-09-24 13:38:43 -04003584 info.fTarget = GR_GL_TEXTURE_2D;
3585 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003586 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003587 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003588 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003589 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003590
Robert Phillips42716d42019-12-16 12:19:54 -05003591 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003592 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3593 SkTDArray<GrMipLevel> texels;
3594 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003595 texels.append(numMipLevels);
3596 for (int i = 0; i < numMipLevels; ++i) {
3597 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3598 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003599 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3600 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3601 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003602 GL_CALL(DeleteTextures(1, &info.fID));
3603 return {};
3604 }
3605 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3606 // TODO: Unify this with the clear texture code in onCreateTexture().
3607 GrColorType colorType;
3608 GrGLenum externalFormat, externalType;
3609 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3610 &externalType, &colorType);
3611 if (colorType == GrColorType::kUnknown) {
3612 GL_CALL(DeleteTextures(1, &info.fID));
3613 return {};
3614 }
3615
3616 // Make one tight image at the base size and reuse it for smaller levels.
3617 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3618 auto rb = ii.minRowBytes();
3619 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3620 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3621 GL_CALL(DeleteTextures(1, &info.fID));
3622 return {};
3623 }
3624
3625 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3626 SkISize levelDimensions = dimensions;
3627 for (int i = 0; i < numMipLevels; ++i) {
3628 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3629 levelDimensions.height(), externalFormat, externalType,
3630 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003631 levelDimensions = {std::max(1, levelDimensions.width() /2),
3632 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003633 }
3634 }
3635 // Unbind this texture from the scratch texture unit.
3636 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003637
Brian Salomone2826ab2019-06-04 15:58:31 -04003638 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003639 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003640 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3641 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003642
Brian Salomon85c3d682019-11-04 15:04:54 -05003643 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3644 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003645}
3646
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003647void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003648 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3649
3650 GrGLTextureInfo info;
3651 if (tex.getGLTextureInfo(&info)) {
3652 GL_CALL(DeleteTextures(1, &info.fID));
3653 }
3654}
3655
3656#if GR_TEST_UTILS
3657
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003658bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003659 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003660
Greg Daniel52e16d92018-04-10 09:34:07 -04003661 GrGLTextureInfo info;
3662 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003663 return false;
3664 }
3665
3666 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003667 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003668
3669 return (GR_GL_TRUE == result);
3670}
3671
Brian Salomonf865b052018-03-09 09:01:53 -05003672GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003673 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003674 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3675 return GrBackendRenderTarget(); // invalid
3676 }
Brian Salomon8a375832018-03-14 10:21:40 -04003677 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003678 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003679 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003680 return {};
3681 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003682 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003683 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003684 if (sFormatIdx < 0) {
3685 return {};
3686 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003687 GrGLuint colorID = 0;
3688 GrGLuint stencilID = 0;
3689 auto deleteIDs = [&] {
3690 if (colorID) {
3691 if (useTexture) {
3692 GL_CALL(DeleteTextures(1, &colorID));
3693 } else {
3694 GL_CALL(DeleteRenderbuffers(1, &colorID));
3695 }
Brian Salomonf865b052018-03-09 09:01:53 -05003696 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003697 if (stencilID) {
3698 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003699 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003700 };
3701
3702 if (useTexture) {
3703 GL_CALL(GenTextures(1, &colorID));
3704 } else {
3705 GL_CALL(GenRenderbuffers(1, &colorID));
3706 }
3707 GL_CALL(GenRenderbuffers(1, &stencilID));
3708 if (!stencilID || !colorID) {
3709 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003710 return {};
3711 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003712
Brian Salomonf865b052018-03-09 09:01:53 -05003713 GrGLFramebufferInfo info;
3714 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003715 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003716 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3717 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003718 deleteIDs();
3719 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003720 }
3721
3722 this->invalidateBoundRenderTarget();
3723
Adrienne Walker4ee88512018-05-17 11:37:14 -07003724 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003725 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003726 GrGLTextureParameters::SamplerOverriddenState initialState;
3727 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3728 if (!colorID) {
3729 deleteIDs();
3730 return {};
3731 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003732 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3733 colorID, 0));
3734 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003735 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003736 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003737 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003738 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3739 GR_GL_RENDERBUFFER, colorID));
3740 }
3741 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003742 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003743 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003744 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003745 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003746 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3747 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003748 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003749 }
3750
Brian Salomon93348dd2018-08-29 12:56:23 -04003751 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3752 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3753 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3754 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003755 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003756 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003757
Adrienne Walker4ee88512018-05-17 11:37:14 -07003758 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003759 GrGLenum status;
3760 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3761 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003762 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003763 return {};
3764 }
3765 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003766
Greg Daniel108bb232018-07-03 16:18:29 -04003767 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003768 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003769 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003770}
3771
3772void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003773 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003774 GrGLFramebufferInfo info;
3775 if (backendRT.getGLFramebufferInfo(&info)) {
3776 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003777 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003778 }
3779 }
joshualitt8fd844f2015-12-02 13:36:47 -08003780}
3781
Greg Daniel26b50a42018-03-08 09:49:58 -05003782void GrGLGpu::testingOnly_flushGpuAndSync() {
3783 GL_CALL(Finish());
3784}
Brian Salomonf865b052018-03-09 09:01:53 -05003785#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003786
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003787///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003788
cdaltone2e71c22016-04-07 18:13:29 -07003789GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003790 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003791 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003792 GrGLAttribArrayState* attribState;
3793
cdaltone2e71c22016-04-07 18:13:29 -07003794 if (gpu->glCaps().isCoreProfile()) {
3795 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003796 GrGLuint arrayID;
3797 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3798 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003799 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003800 }
cdaltone2e71c22016-04-07 18:13:29 -07003801 if (ibuf) {
3802 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003803 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003804 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003805 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003806 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003807 if (ibuf) {
3808 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003809 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003810 } else {
3811 this->setVertexArrayID(gpu, 0);
3812 }
3813 int attrCount = gpu->glCaps().maxVertexAttributes();
3814 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3815 fDefaultVertexArrayAttribState.resize(attrCount);
3816 }
3817 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003818 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003819 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003820}
bsalomone179a912016-01-20 06:18:10 -08003821
Greg Daniel30a35e82019-11-19 14:12:25 -05003822bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003823 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003824 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003825 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003826 // We call finish if the client told us to sync or if we have a finished proc but don't support
3827 // GLsync objects.
3828 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3829 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3830 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003831 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003832 // After a finish everything previously sent to GL is done.
3833 for (const auto& cb : fFinishCallbacks) {
3834 cb.fCallback(cb.fContext);
3835 this->deleteSync(cb.fSync);
3836 }
3837 fFinishCallbacks.clear();
3838 if (info.fFinishedProc) {
3839 info.fFinishedProc(info.fFinishedContext);
3840 }
3841 } else {
3842 if (info.fFinishedProc) {
3843 FinishCallback callback;
3844 callback.fCallback = info.fFinishedProc;
3845 callback.fContext = info.fFinishedContext;
3846 callback.fSync = (GrGLsync)this->insertFence();
3847 fFinishCallbacks.push_back(callback);
3848 GL_CALL(Flush());
3849 } else if (insertedSemaphore) {
3850 // Must call flush after semaphores in case they are waited on another GL context.
3851 GL_CALL(Flush());
3852 }
3853 // See if any previously inserted finish procs are good to go.
3854 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003855 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003856 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003857}
3858
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003859void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3860 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3861 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3862 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003863}
3864
Greg Daniel6be35232017-03-01 17:01:09 -05003865GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003866 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003867 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003868 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3869 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3870 GrGLuint fence = 0;
3871 GL_CALL(GenFences(1, &fence));
3872 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3873 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3874 } else {
3875 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3876 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05003877 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003878 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003879}
3880
Brian Salomonb0d8b762019-05-06 16:58:22 -04003881bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003882 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3883 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3884 if (!timeout) {
3885 if (flush) {
3886 GL_CALL(Flush);
3887 }
3888 GrGLboolean result;
3889 GL_CALL_RET(result, TestFence(nvFence));
3890 return result == GR_GL_TRUE;
3891 }
3892 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3893 // If this really becomes necessary we could poll TestFence().
3894 // FinishFence always flushes so no need to check flush param.
3895 GL_CALL(FinishFence(nvFence));
3896 return true;
3897 } else {
3898 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3899 GrGLenum result;
3900 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3901 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3902 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003903}
3904
3905bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3906 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003907}
3908
3909void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003910 this->deleteSync((GrGLsync)fence);
3911}
3912
Greg Daniel301015c2019-11-18 14:06:46 -05003913std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003914 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003915 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003916}
3917
Greg Daniel301015c2019-11-18 14:06:46 -05003918std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3919 const GrBackendSemaphore& semaphore,
3920 GrResourceProvider::SemaphoreWrapType wrapType,
3921 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003922 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003923 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3924}
3925
Greg Daniel301015c2019-11-18 14:06:46 -05003926void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3927 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003928
Greg Daniel48661b82018-01-22 16:11:35 -05003929 GrGLsync sync;
3930 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3931 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05003932}
3933
Greg Daniel301015c2019-11-18 14:06:46 -05003934void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
3935 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003936
3937 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3938}
3939
Brian Salomonb0d8b762019-05-06 16:58:22 -04003940void GrGLGpu::checkFinishProcs() {
3941 // Bail after the first unfinished sync since we expect they signal in the order inserted.
3942 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
3943 /* timeout = */ 0, /* flush = */ false)) {
3944 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
3945 this->deleteSync(fFinishCallbacks.front().fSync);
3946 fFinishCallbacks.pop_front();
3947 }
3948}
3949
Greg Daniel6be35232017-03-01 17:01:09 -05003950void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003951 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3952 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
3953 GL_CALL(DeleteFences(1, &nvFence));
3954 } else {
3955 GL_CALL(DeleteSync(sync));
3956 }
jvanverth84741b32016-09-30 08:39:02 -07003957}
Brian Osman13dddce2017-05-09 13:19:50 -04003958
Greg Daniel301015c2019-11-18 14:06:46 -05003959std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04003960 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05003961 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05003962 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003963 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05003964 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
3965 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04003966
3967 return semaphore;
3968}
Robert Phillips646e4292017-06-13 12:44:56 -04003969
3970int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04003971 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04003972 case kTexture2DSampler_GrSLType:
3973 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04003974 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003975 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04003976 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003977 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04003978 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04003979 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04003980 }
3981}
Brian Osman71a18892017-08-10 10:23:25 -04003982
Kevin Lubickf4def342018-10-04 12:52:50 -04003983#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003984#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04003985void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
3986 // We are called by the base class, which has already called beginObject(). We choose to nest
3987 // all of our caps information in a named sub-object.
3988 writer->beginObject("GL GPU");
3989
3990 const GrGLubyte* str;
3991 GL_CALL_RET(str, GetString(GR_GL_VERSION));
3992 writer->appendString("GL_VERSION", (const char*)(str));
3993 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
3994 writer->appendString("GL_RENDERER", (const char*)(str));
3995 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
3996 writer->appendString("GL_VENDOR", (const char*)(str));
3997 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
3998 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
3999
4000 writer->appendName("extensions");
4001 glInterface()->fExtensions.dumpJSON(writer);
4002
4003 writer->endObject();
4004}
Kevin Lubickf4def342018-10-04 12:52:50 -04004005#endif