blob: 382773383fa7166a361002edf55acd4d4563d8cb [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050018#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040022#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040024#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrFixedClip.h"
26#include "src/gpu/GrGpuResourcePriv.h"
27#include "src/gpu/GrMesh.h"
28#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040029#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrRenderTargetPriv.h"
31#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/gl/GrGLBuffer.h"
35#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040036#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/gl/GrGLSemaphore.h"
38#include "src/gpu/gl/GrGLStencilAttachment.h"
39#include "src/gpu/gl/GrGLTextureRenderTarget.h"
40#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
41#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <cmath>
44
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000046#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000047
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000048#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
49 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
50 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
51 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000053 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
54 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
55 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
56#endif
57
Jim Van Verth32ac83e2016-11-28 15:23:57 -050058//#define USE_NSIGHT
59
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000060///////////////////////////////////////////////////////////////////////////////
61
cdalton8917d622015-05-06 13:40:21 -070062static const GrGLenum gXfermodeEquation2Blend[] = {
63 // Basic OpenGL blend equations.
64 GR_GL_FUNC_ADD,
65 GR_GL_FUNC_SUBTRACT,
66 GR_GL_FUNC_REVERSE_SUBTRACT,
67
68 // GL_KHR_blend_equation_advanced.
69 GR_GL_SCREEN,
70 GR_GL_OVERLAY,
71 GR_GL_DARKEN,
72 GR_GL_LIGHTEN,
73 GR_GL_COLORDODGE,
74 GR_GL_COLORBURN,
75 GR_GL_HARDLIGHT,
76 GR_GL_SOFTLIGHT,
77 GR_GL_DIFFERENCE,
78 GR_GL_EXCLUSION,
79 GR_GL_MULTIPLY,
80 GR_GL_HSL_HUE,
81 GR_GL_HSL_SATURATION,
82 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050083 GR_GL_HSL_LUMINOSITY,
84
85 // Illegal... needs to map to something.
86 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070087};
Brian Salomon4dea72a2019-12-18 10:43:10 -050088static_assert(0 == kAdd_GrBlendEquation);
89static_assert(1 == kSubtract_GrBlendEquation);
90static_assert(2 == kReverseSubtract_GrBlendEquation);
91static_assert(3 == kScreen_GrBlendEquation);
92static_assert(4 == kOverlay_GrBlendEquation);
93static_assert(5 == kDarken_GrBlendEquation);
94static_assert(6 == kLighten_GrBlendEquation);
95static_assert(7 == kColorDodge_GrBlendEquation);
96static_assert(8 == kColorBurn_GrBlendEquation);
97static_assert(9 == kHardLight_GrBlendEquation);
98static_assert(10 == kSoftLight_GrBlendEquation);
99static_assert(11 == kDifference_GrBlendEquation);
100static_assert(12 == kExclusion_GrBlendEquation);
101static_assert(13 == kMultiply_GrBlendEquation);
102static_assert(14 == kHSLHue_GrBlendEquation);
103static_assert(15 == kHSLSaturation_GrBlendEquation);
104static_assert(16 == kHSLColor_GrBlendEquation);
105static_assert(17 == kHSLLuminosity_GrBlendEquation);
106static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700107
twiz@google.com0f31ca72011-03-18 17:38:11 +0000108static const GrGLenum gXfermodeCoeff2Blend[] = {
109 GR_GL_ZERO,
110 GR_GL_ONE,
111 GR_GL_SRC_COLOR,
112 GR_GL_ONE_MINUS_SRC_COLOR,
113 GR_GL_DST_COLOR,
114 GR_GL_ONE_MINUS_DST_COLOR,
115 GR_GL_SRC_ALPHA,
116 GR_GL_ONE_MINUS_SRC_ALPHA,
117 GR_GL_DST_ALPHA,
118 GR_GL_ONE_MINUS_DST_ALPHA,
119 GR_GL_CONSTANT_COLOR,
120 GR_GL_ONE_MINUS_CONSTANT_COLOR,
121 GR_GL_CONSTANT_ALPHA,
122 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000123
124 // extended blend coeffs
125 GR_GL_SRC1_COLOR,
126 GR_GL_ONE_MINUS_SRC1_COLOR,
127 GR_GL_SRC1_ALPHA,
128 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500129
130 // Illegal... needs to map to something.
131 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
bsalomon861e1032014-12-16 07:33:49 -0800134bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000135 static const bool gCoeffReferencesBlendConst[] = {
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 false,
146 true,
147 true,
148 true,
149 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150
151 // extended blend coeffs
152 false,
153 false,
154 false,
155 false,
Mike Klein36743362018-11-06 08:23:30 -0500156
157 // Illegal.
158 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000159 };
160 return gCoeffReferencesBlendConst[coeff];
Brian Salomon4dea72a2019-12-18 10:43:10 -0500161 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000162
Brian Salomon4dea72a2019-12-18 10:43:10 -0500163 static_assert(0 == kZero_GrBlendCoeff);
164 static_assert(1 == kOne_GrBlendCoeff);
165 static_assert(2 == kSC_GrBlendCoeff);
166 static_assert(3 == kISC_GrBlendCoeff);
167 static_assert(4 == kDC_GrBlendCoeff);
168 static_assert(5 == kIDC_GrBlendCoeff);
169 static_assert(6 == kSA_GrBlendCoeff);
170 static_assert(7 == kISA_GrBlendCoeff);
171 static_assert(8 == kDA_GrBlendCoeff);
172 static_assert(9 == kIDA_GrBlendCoeff);
173 static_assert(10 == kConstC_GrBlendCoeff);
174 static_assert(11 == kIConstC_GrBlendCoeff);
175 static_assert(12 == kConstA_GrBlendCoeff);
176 static_assert(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
Brian Salomon4dea72a2019-12-18 10:43:10 -0500178 static_assert(14 == kS2C_GrBlendCoeff);
179 static_assert(15 == kIS2C_GrBlendCoeff);
180 static_assert(16 == kS2A_GrBlendCoeff);
181 static_assert(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000182
183 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
Brian Salomon4dea72a2019-12-18 10:43:10 -0500184 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000185}
186
Brian Salomond978b902019-02-07 15:09:18 -0500187//////////////////////////////////////////////////////////////////////////////
188
189static int gl_target_to_binding_index(GrGLenum target) {
190 switch (target) {
191 case GR_GL_TEXTURE_2D:
192 return 0;
193 case GR_GL_TEXTURE_RECTANGLE:
194 return 1;
195 case GR_GL_TEXTURE_EXTERNAL:
196 return 2;
197 }
198 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500199}
200
201GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500202 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
203}
204
205bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
206 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500207}
208
209void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500210 int targetIndex = gl_target_to_binding_index(target);
211 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
212 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500213}
214
Brian Salomon1f05d452019-02-08 12:33:08 -0500215void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
216 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500217}
218
Brian Salomon1f05d452019-02-08 12:33:08 -0500219void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
220 for (auto& targetBinding : fTargetBindings) {
221 targetBinding.fBoundResourceID.makeInvalid();
222 if (markUnmodified) {
223 targetBinding.fHasBeenModified = false;
224 }
Brian Salomond978b902019-02-07 15:09:18 -0500225 }
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
Brian Salomondc829942018-10-23 16:07:24 -0400230static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
231 switch (filter) {
232 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
233 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
234 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
235 }
236 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400237}
238
239static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
240 switch (filter) {
241 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
242 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
243 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
244 }
245 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400246}
247
Michael Ludwigf23a1522018-12-10 11:36:13 -0500248static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
249 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400250 switch (wrapMode) {
251 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
252 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
253 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500254 case GrSamplerState::WrapMode::kClampToBorder:
255 // May not be supported but should have been caught earlier
256 SkASSERT(caps.clampToBorderSupport());
257 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500258 }
Brian Salomondc829942018-10-23 16:07:24 -0400259 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400260}
261
262///////////////////////////////////////////////////////////////////////////////
263
264class GrGLGpu::SamplerObjectCache {
265public:
266 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
267 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
268 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
269 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
270 std::fill_n(fSamplers, kNumSamplers, 0);
271 }
272
273 ~SamplerObjectCache() {
274 if (!fNumTextureUnits) {
275 // We've already been abandoned.
276 return;
277 }
Chris Dalton703fe682019-05-15 12:58:12 -0600278 for (GrGLuint sampler : fSamplers) {
279 // The spec states that "zero" values should be silently ignored, however they still
280 // trigger GL errors on some NVIDIA platforms.
281 if (sampler) {
282 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
283 }
284 }
Brian Salomondc829942018-10-23 16:07:24 -0400285 }
286
Brian Salomonccb61422020-01-09 10:46:36 -0500287 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400288 int index = StateToIndex(state);
289 if (!fSamplers[index]) {
290 GrGLuint s;
291 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
292 if (!s) {
293 return;
294 }
295 fSamplers[index] = s;
296 auto minFilter = filter_to_gl_min_filter(state.filter());
297 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500298 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
299 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400300 GR_GL_CALL(fGpu->glInterface(),
301 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
302 GR_GL_CALL(fGpu->glInterface(),
303 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
304 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
305 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
306 }
307 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
308 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
309 fHWBoundSamplers[unitIdx] = fSamplers[index];
310 }
311 }
312
313 void invalidateBindings() {
314 // When we have sampler support we always use samplers. So setting these to zero will cause
315 // a rebind on next usage.
316 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
317 }
318
319 void abandon() {
320 fHWBoundSamplers.reset();
321 fNumTextureUnits = 0;
322 }
323
324 void release() {
325 if (!fNumTextureUnits) {
326 // We've already been abandoned.
327 return;
328 }
329 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
330 std::fill_n(fSamplers, kNumSamplers, 0);
331 // Deleting a bound sampler implicitly binds sampler 0.
332 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
333 }
334
335private:
Brian Salomonccb61422020-01-09 10:46:36 -0500336 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400337 int filter = static_cast<int>(state.filter());
338 SkASSERT(filter >= 0 && filter < 3);
339 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500340 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400341 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500342 SkASSERT(wrapY >= 0 && wrapY < 4);
343 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400344 SkASSERT(idx < kNumSamplers);
345 return idx;
346 }
347
348 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500349 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400350 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
351 GrGLuint fSamplers[kNumSamplers];
352 int fNumTextureUnits;
353};
354
reed@google.comac10a2d2010-12-22 21:39:39 +0000355///////////////////////////////////////////////////////////////////////////////
356
Brian Salomon384fab42017-12-07 12:33:05 -0500357sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
358 GrContext* context) {
359 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500360 interface = GrGLMakeNativeInterface();
361 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
362 // to GrGLMakeNativeInterface.
363 if (!interface) {
364 interface = sk_ref_sp(GrGLCreateNativeInterface());
365 }
Brian Salomon384fab42017-12-07 12:33:05 -0500366 if (!interface) {
367 return nullptr;
368 }
bsalomon424cc262015-05-22 10:37:30 -0700369 }
Jim Van Verth76334772017-05-05 16:46:05 -0400370#ifdef USE_NSIGHT
371 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
372#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500373 auto glContext = GrGLContext::Make(std::move(interface), options);
374 if (!glContext) {
375 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700376 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500377 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700378}
379
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500380GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
381 : GrGpu(context)
382 , fGLContext(std::move(ctx))
383 , fProgramCache(new ProgramCache(this))
384 , fHWProgramID(0)
385 , fTempSrcFBOID(0)
386 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500387 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500388 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400389 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500390 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000391
Brian Salomond978b902019-02-07 15:09:18 -0500392 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000393
Brian Salomonae64c192019-02-05 09:41:37 -0500394 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
395 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500396 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500397 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
398 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
399 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
400 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700401 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500402 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
403 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700404 }
Brian Salomonae64c192019-02-05 09:41:37 -0500405 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400406 fHWBufferState[i].invalidate();
407 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500408 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700409
410 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
411 fPathRendering.reset(new GrGLPathRendering(this));
412 }
413
Brian Salomondc829942018-10-23 16:07:24 -0400414 if (this->glCaps().samplerObjectSupport()) {
415 fSamplerObjectCache.reset(new SamplerObjectCache(this));
416 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000417}
418
bsalomon861e1032014-12-16 07:33:49 -0800419GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700420 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
421 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800422 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700423 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700424 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800425
Brian Salomon802cb312018-06-08 18:05:20 -0400426 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400427 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000428 // detach the current program so there is no confusion on OpenGL's part
429 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000430 GL_CALL(UseProgram(0));
431 }
432
Brian Salomon43f8bf02017-10-18 08:33:29 -0400433 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700434 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800435 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700437 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800438 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400439 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700440 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800441 }
egdaniel0f5f9672015-02-03 11:10:51 -0800442
bsalomon7ea33f52015-11-22 14:51:00 -0800443 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
444 if (0 != fCopyPrograms[i].fProgram) {
445 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
446 }
bsalomon6df86402015-06-01 10:41:49 -0700447 }
bsalomon6dea83f2015-12-03 12:58:06 -0800448
brianosman33f6b3f2016-06-02 05:49:21 -0700449 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
450 if (0 != fMipmapPrograms[i].fProgram) {
451 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
452 }
453 }
454
Brian Salomondc829942018-10-23 16:07:24 -0400455 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700456}
457
bsalomon6e2aad42016-04-01 11:54:31 -0700458void GrGLGpu::disconnect(DisconnectType type) {
459 INHERITED::disconnect(type);
460 if (DisconnectType::kCleanup == type) {
461 if (fHWProgramID) {
462 GL_CALL(UseProgram(0));
463 }
464 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700465 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700466 }
467 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700468 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700469 }
470 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700471 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700472 }
bsalomon6e2aad42016-04-01 11:54:31 -0700473 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
474 if (fCopyPrograms[i].fProgram) {
475 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
476 }
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
479 if (fMipmapPrograms[i].fProgram) {
480 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
481 }
482 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400483
Brian Salomondc829942018-10-23 16:07:24 -0400484 if (fSamplerObjectCache) {
485 fSamplerObjectCache->release();
486 }
bsalomon6e2aad42016-04-01 11:54:31 -0700487 } else {
488 if (fProgramCache) {
489 fProgramCache->abandon();
490 }
Brian Salomondc829942018-10-23 16:07:24 -0400491 if (fSamplerObjectCache) {
492 fSamplerObjectCache->abandon();
493 }
bsalomon6e2aad42016-04-01 11:54:31 -0700494 }
495
Brian Salomon802cb312018-06-08 18:05:20 -0400496 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500497 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700498
bsalomonc8dc1f72014-08-21 13:02:13 -0700499 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700500 fTempSrcFBOID = 0;
501 fTempDstFBOID = 0;
502 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700503 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800504 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
505 fCopyPrograms[i].fProgram = 0;
506 }
brianosman33f6b3f2016-06-02 05:49:21 -0700507 fMipmapProgramArrayBuffer.reset();
508 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
509 fMipmapPrograms[i].fProgram = 0;
510 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400511
jvanverthe9c0fc62015-04-29 11:18:05 -0700512 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700513 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700514 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000515}
516
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000517///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000518
bsalomon861e1032014-12-16 07:33:49 -0800519void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000520 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400521 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000522 GL_CALL(Disable(GR_GL_DEPTH_TEST));
523 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000524
Brian Salomonf0861672017-05-08 11:10:10 -0400525 // We don't use face culling.
526 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600527 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
528 // just set this to the default for self-consistency.
529 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400530
Brian Salomonae64c192019-02-05 09:41:37 -0500531 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
532 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700533
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400534 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500535#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 // Desktop-only state that we never change
537 if (!this->glCaps().isCoreProfile()) {
538 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
539 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
540 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
541 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
542 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
543 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
544 }
545 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
546 // core profile. This seems like a bug since the core spec removes any mention of
547 // GL_ARB_imaging.
548 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
549 GL_CALL(Disable(GR_GL_COLOR_TABLE));
550 }
551 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400552
Chris Dalton1215cda2019-12-17 21:44:04 -0700553 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500554#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 // Since ES doesn't support glPointSize at all we always use the VS to
556 // set the point size
557 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
558
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000559 }
joshualitt58162332014-08-01 06:44:53 -0700560
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400561 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500562 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700563 // The arm extension requires specifically enabling MSAA fetching per sample.
564 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500565 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700566 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000567 fHWWriteToColor = kUnknown_TriState;
568 // we only ever use lines in hairline mode
569 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700570 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500571
572 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000573 }
edisonn@google.comba669992013-06-28 16:03:21 +0000574
egdanielb414f252014-07-29 13:15:47 -0700575 if (resetBits & kMSAAEnable_GrGLBackendState) {
576 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700577
Chris Dalton6ce447a2019-06-23 18:07:38 -0600578 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800579 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
580 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700581 GL_CALL(CoverageModulation(GR_GL_RGBA));
582 }
Chris Daltonce425af2019-12-16 10:39:03 -0700583
584 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000585 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000587 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400588 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000589
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000590 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500591 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500592 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000593 }
Brian Salomondc829942018-10-23 16:07:24 -0400594 if (fSamplerObjectCache) {
595 fSamplerObjectCache->invalidateBindings();
596 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000597 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000598
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 if (resetBits & kBlend_GrGLBackendState) {
600 fHWBlendState.invalidate();
601 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000602
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000603 if (resetBits & kView_GrGLBackendState) {
604 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700605 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 fHWViewport.invalidate();
607 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000608
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 if (resetBits & kStencil_GrGLBackendState) {
610 fHWStencilSettings.invalidate();
611 fHWStencilTestEnabled = kUnknown_TriState;
612 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000613
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000614 // Vertex
615 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700616 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500617 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
618 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700619 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400635 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
Brian Salomon1047a492019-07-02 12:25:21 -0400638 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400650 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000651}
652
Brian Salomonea4ad302019-08-07 13:04:55 -0400653static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
654 const GrGLCaps& caps, GrGLTexture::Desc* desc,
655 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400656 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400657 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800659 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000660
Brian Salomonea4ad302019-08-07 13:04:55 -0400661 desc->fSize = {backendTex.width(), backendTex.height()};
662 desc->fTarget = info.fTarget;
663 desc->fID = info.fID;
664 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800665
Brian Salomonea4ad302019-08-07 13:04:55 -0400666 if (desc->fFormat == GrGLFormat::kUnknown) {
667 return false;
668 }
669 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400670 if (!caps.shaderCaps()->externalTextureSupport()) {
671 return false;
672 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400673 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
674 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400675 return false;
676 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400677 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400678 return false;
679 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400680 if (backendTex.isProtected()) {
681 // Not supported in GL backend at this time.
682 return false;
683 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400684
Brian Salomond17f6582017-07-19 18:28:58 -0400685 return true;
686}
687
688sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400689 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400690 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400691 GrGLTexture::Desc desc;
692 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400693 return nullptr;
694 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400695
Brian Salomond17f6582017-07-19 18:28:58 -0400696 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400697 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400698 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400699 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400700 }
bsalomone5286e02016-01-14 09:24:09 -0800701
Greg Daniel177e6952017-10-12 12:27:11 -0400702 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
703 : GrMipMapsStatus::kNotAllocated;
704
Brian Salomonea4ad302019-08-07 13:04:55 -0400705 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400706 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400707 // We don't know what parameters are already set on wrapped textures.
708 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600709 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400710}
711
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500712static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
713 const GrGLCaps& caps, GrGLTexture::Desc* desc,
714 bool skipRectTexSupportCheck = false) {
715 GrGLTextureInfo info;
716 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
717 return false;
718 }
719
720 desc->fSize = {backendTex.width(), backendTex.height()};
721 desc->fTarget = info.fTarget;
722 desc->fID = info.fID;
723 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
724
725 if (desc->fFormat == GrGLFormat::kUnknown) {
726 return false;
727 }
728
729 if (GR_GL_TEXTURE_2D != desc->fTarget) {
730 return false;
731 }
732 if (backendTex.isProtected()) {
733 // Not supported in GL backend at this time.
734 return false;
735 }
736
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500737 return true;
738}
739
Robert Phillipsb915c942019-12-17 14:44:37 -0500740sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
741 GrWrapOwnership ownership,
742 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500743 GrGLTexture::Desc desc;
744 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
745 return nullptr;
746 }
747
748 if (kBorrow_GrWrapOwnership == ownership) {
749 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
750 } else {
751 desc.fOwnership = GrBackendObjectOwnership::kOwned;
752 }
753
754 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
755 : GrMipMapsStatus::kNotAllocated;
756
757 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
758 backendTex.getGLTextureParams(), cacheable,
759 kRead_GrIOType);
760 // We don't know what parameters are already set on wrapped textures.
761 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600762 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500763}
764
Brian Salomond17f6582017-07-19 18:28:58 -0400765sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400766 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400767 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500768 GrWrapOwnership ownership,
769 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400770 const GrGLCaps& caps = this->glCaps();
771
Brian Salomonea4ad302019-08-07 13:04:55 -0400772 GrGLTexture::Desc desc;
773 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800774 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800775 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400776 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
777 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800778
Brian Salomond17f6582017-07-19 18:28:58 -0400779 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400780 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800781 return nullptr;
782 }
bsalomon10528f12015-10-14 12:54:52 -0700783
Brian Osman766fcbb2017-03-13 09:33:09 -0400784 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400785 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400786 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400787 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800788 }
bsalomonb15b4c12014-10-29 12:41:57 -0700789
Robert Phillips0902c982019-07-16 07:47:56 -0400790
Greg Daniel6fa62e22019-08-07 15:52:37 -0400791 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
792 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000793
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 GrGLRenderTarget::IDs rtIDs;
795 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400796 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000797 }
Greg Daniel177e6952017-10-12 12:27:11 -0400798
799 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
800 : GrMipMapsStatus::kNotAllocated;
801
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500802 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400803 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400804 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400805 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400806 // We don't know what parameters are already set on wrapped textures.
807 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600808 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000809}
810
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400811sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
812 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400813 GrGLFramebufferInfo info;
814 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000815 return nullptr;
816 }
817
Brian Salomone8a766b2019-07-19 14:24:36 -0400818 if (backendRT.isProtected()) {
819 // Not supported in GL at this time.
820 return nullptr;
821 }
822
Brian Salomond4764a12019-08-08 12:08:24 -0400823 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400824 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
825 return nullptr;
826 }
827
Brian Salomonea4ad302019-08-07 13:04:55 -0400828 GrGLRenderTarget::IDs rtIDs;
829 rtIDs.fRTFBOID = info.fFBOID;
830 rtIDs.fMSColorRenderbufferID = 0;
831 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
832 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000833
Greg Daniel6fa62e22019-08-07 15:52:37 -0400834 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700835
Brian Salomona56a7462020-02-07 14:17:25 -0500836 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400837 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000838}
839
Greg Daniel7ef28f32017-04-20 16:41:55 +0000840sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400841 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400842 GrColorType colorType) {
843 GrGLTexture::Desc desc;
844 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
845 // with a texture rectangle,we assume the necessary support exists.
846 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800847 return nullptr;
848 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400849
850 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
851 return nullptr;
852 }
853
854 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400855 GrGLRenderTarget::IDs rtIDs;
856 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800857 return nullptr;
858 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500859 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800860}
861
Brian Salomonc320b152018-02-20 14:05:36 -0500862static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700863 if (!glTex) {
864 return false;
865 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000866
bsalomone5286e02016-01-14 09:24:09 -0800867 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
868 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800869 return false;
870 }
871
jvanverth17aa0472016-01-05 10:41:27 -0800872 return true;
873}
874
Brian Salomona9b04b92018-06-01 15:04:28 -0400875bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400876 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400877 const GrMipLevel texels[], int mipLevelCount,
878 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500879 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800880
Brian Salomonc320b152018-02-20 14:05:36 -0500881 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800882 return false;
883 }
884
Brian Salomond978b902019-02-07 15:09:18 -0500885 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000886
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400887 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400888 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400889 glTex->target(), left, top, width, height, srcColorType, texels,
890 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000891}
892
Brian Salomone05ba5a2019-04-08 11:59:07 -0400893bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400894 GrColorType textureColorType, GrColorType bufferColorType,
895 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897
Jim Van Verth1676cb92019-01-15 13:24:45 -0500898 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400899 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500900
Brian Salomonc320b152018-02-20 14:05:36 -0500901 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400902 return false;
903 }
904
Hal Canaryc36f7e72018-06-18 15:50:09 -0400905 static_assert(sizeof(int) == sizeof(int32_t), "");
906 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 return false;
908 }
909
Brian Salomond978b902019-02-07 15:09:18 -0500910 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911
912 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500913 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500915 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400916
Greg Daniel660cc992017-06-26 14:55:05 -0400917 SkDEBUGCODE(
918 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
919 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
920 SkASSERT(bounds.contains(subRect));
921 )
922
Brian Salomonb28cb682019-07-26 12:48:47 -0400923 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400925 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700926 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400927 return false;
928 }
929
930 bool restoreGLRowLength = false;
931 if (trimRowBytes != rowBytes) {
932 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400933 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400934 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
935 restoreGLRowLength = true;
936 }
937
Greg Danielba88ab62019-07-26 09:14:01 -0400938 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400939 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400940 GrGLenum externalFormat = 0;
941 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400942 this->glCaps().getTexSubImageExternalFormatAndType(
943 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400944 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400945 return false;
946 }
947
Brian Salomon8cbf6622019-07-30 11:03:14 -0400948 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400949 GL_CALL(TexSubImage2D(glTex->target(),
950 0,
951 left, top,
952 width,
953 height,
954 externalFormat, externalType,
955 pixels));
956
957 if (restoreGLRowLength) {
958 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
959 }
960
961 return true;
962}
963
Brian Salomon26de56e2019-04-10 12:14:26 -0400964bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400965 GrColorType surfaceColorType, GrColorType dstColorType,
966 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400967 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
968 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400969 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400970 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
971 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400972}
973
Brian Osman9b560242017-09-05 15:34:52 -0400974void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500975 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
976 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
977 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
978 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400979 }
Brian Osman9b560242017-09-05 15:34:52 -0400980}
981
Brian Salomond2a8ae22019-09-10 16:03:59 -0400982bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
983 int texHeight, GrGLenum target, int left, int top, int width,
984 int height, GrColorType srcColorType, const GrMipLevel texels[],
985 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500986 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400987 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500988
Greg Daniel7bfc9132019-08-14 14:23:53 -0400989 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400990 SkDEBUGCODE(
991 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
992 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
993 SkASSERT(bounds.contains(subRect));
994 )
Robert Phillips590533f2017-07-11 14:22:35 -0400995 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400996 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800997
Brian Osman9b560242017-09-05 15:34:52 -0400998 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400999
cblume55f2d2d2016-02-26 13:20:48 -08001000 const GrGLInterface* interface = this->glInterface();
1001 const GrGLCaps& caps = this->glCaps();
1002
Brian Salomonf77c1462019-08-01 15:19:29 -04001003 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001004
1005 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001006 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001007 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001008
bsalomon5b30c6f2015-12-17 14:17:34 -08001009 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001010 GrGLenum externalFormat;
1011 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001012 this->glCaps().getTexSubImageExternalFormatAndType(
1013 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001014 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001015 return false;
1016 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001017
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001019 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001020 * because our srcData has extra bytes past each row. If so, we need
1021 * to trim those off here, since GL ES may not let us specify
1022 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001023 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001024 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001025
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001026 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001027 *mipMapsStatus = (mipLevelCount > 1) ?
1028 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001029 }
bsalomone699d0c2016-03-09 06:25:15 -08001030
Brian Salomond2a8ae22019-09-10 16:03:59 -04001031 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001032
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1034 if (!texels[currentMipLevel].fPixels) {
1035 if (mipMapsStatus) {
1036 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001037 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001038 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001039 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001040 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001041 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1042 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 const size_t trimRowBytes = currentWidth * bpp;
1044 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1045
1046 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1047 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1048 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1049 restoreGLRowLength = true;
1050 }
1051
1052 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1053 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001055 if (restoreGLRowLength) {
1056 SkASSERT(caps.writePixelsRowBytesSupport());
1057 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1058 }
1059 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001060}
1061
Greg Daniel7bfc9132019-08-14 14:23:53 -04001062bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001063 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001064 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001066 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001067 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001068 const GrGLCaps& caps = this->glCaps();
1069
1070 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001071 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001073 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001074 }
1075
Robert Phillips9f744f72019-12-19 19:14:33 -05001076 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1077 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1078
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001081 int numMipLevels = 1;
1082 if (mipMapped == GrMipMapped::kYes) {
1083 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1084 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001085
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001086 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001088
1089 if (useTexStorage) {
1090 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001091 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001092 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001093 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001094 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1095 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001096 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001097 }
Robert Phillips42716d42019-12-16 12:19:54 -05001098
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 size_t offset = 0;
1100 for (int level = 0; level < numMipLevels; ++level) {
1101
Robert Phillips99dead92020-01-27 16:11:57 -05001102 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1103 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001104
1105 GL_CALL(CompressedTexSubImage2D(target,
1106 level,
1107 0, // left
1108 0, // top
1109 dimensions.width(),
1110 dimensions.height(),
1111 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001112 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001113 &((char*)data)[offset]));
1114
1115 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1116 if (error != GR_GL_NO_ERROR) {
1117 return false;
1118 }
1119
Robert Phillips9f744f72019-12-19 19:14:33 -05001120 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001121 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001122 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001123 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001124 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001125
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001126 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001127 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1128 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129
1130 const char* rawLevelData = &((char*)data)[offset];
1131 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1132 level,
1133 internalFormat,
1134 dimensions.width(),
1135 dimensions.height(),
1136 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001137 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001138 rawLevelData));
1139
1140 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1141 if (error != GR_GL_NO_ERROR) {
1142 return false;
1143 }
1144
Robert Phillips9f744f72019-12-19 19:14:33 -05001145 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001146 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001147 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001148 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001149 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001150}
1151
bsalomon424cc262015-05-22 10:37:30 -07001152static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001153 int sampleCount,
1154 GrGLenum format,
1155 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001156 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001157 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001158 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001159 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001160 GL_ALLOC_CALL(ctx.interface(),
1161 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1162 sampleCount,
1163 format,
1164 width, height));
1165 break;
1166 case GrGLCaps::kES_Apple_MSFBOType:
1167 GL_ALLOC_CALL(ctx.interface(),
1168 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1169 sampleCount,
1170 format,
1171 width, height));
1172 break;
1173 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1174 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1175 GL_ALLOC_CALL(ctx.interface(),
1176 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1177 sampleCount,
1178 format,
1179 width, height));
1180 break;
1181 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001182 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001183 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001184 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001185 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186}
1187
Brian Salomonea4ad302019-08-07 13:04:55 -04001188bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001189 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 GrGLRenderTarget::IDs* rtIDs) {
1191 rtIDs->fMSColorRenderbufferID = 0;
1192 rtIDs->fRTFBOID = 0;
1193 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1194 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195
bsalomona11e5fc2015-12-18 07:59:41 -08001196 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001197
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001199 goto FAILED;
1200 }
1201
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001202 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001203 goto FAILED;
1204 }
1205
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1207 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 goto FAILED;
1209 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001210
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001211 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1212 // the texture bound to the other. The exception is the IMG multisample extension. With this
1213 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1214 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001215 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001216 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1217 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1218 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219 goto FAILED;
1220 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001221 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
1225
egdanield803f272015-03-18 13:01:52 -07001226 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001227 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001229 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001231 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001232 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 goto FAILED;
1234 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001236 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001237 GR_GL_COLOR_ATTACHMENT0,
1238 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001239 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001243 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1245 GR_GL_COLOR_ATTACHMENT0,
1246 desc.fTarget,
1247 desc.fID,
1248 0,
1249 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001250 } else {
egdanield803f272015-03-18 13:01:52 -07001251 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001252 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 desc.fTarget,
1254 desc.fID,
1255 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001256 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001257
1258 return true;
1259
1260FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001261 if (rtIDs->fMSColorRenderbufferID) {
1262 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001263 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1265 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001267 if (rtIDs->fTexFBOID) {
1268 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 }
1270 return false;
1271}
1272
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001273// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001274static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001275// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001276 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001277}
1278
Brian Salomone2826ab2019-06-04 15:58:31 -04001279static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001280 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001281 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1282 // drivers have a bug where an FBO won't be complete if it includes a
1283 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001284 GrGLTextureParameters::SamplerOverriddenState state;
1285 state.fMinFilter = GR_GL_NEAREST;
1286 state.fMagFilter = GR_GL_NEAREST;
1287 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1288 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001289 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1290 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001293 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001294}
1295
Brian Salomona56a7462020-02-07 14:17:25 -05001296sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001297 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001298 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001299 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001300 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001301 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001302 int mipLevelCount,
1303 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001304 // We don't support protected textures in GL.
1305 if (isProtected == GrProtected::kYes) {
1306 return nullptr;
1307 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001308 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001309
Brian Salomond2a8ae22019-09-10 16:03:59 -04001310 SkASSERT(mipLevelCount > 0);
1311 GrMipMapsStatus mipMapsStatus =
1312 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001313 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001314 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001315 texDesc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001317 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001318 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001319 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1320 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001321
Brian Salomona56a7462020-02-07 14:17:25 -05001322 texDesc.fID = this->createTexture2D(dimensions, texDesc.fFormat, renderable, &initialState,
1323 mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001324
1325 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001326 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001327 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001328
Robert Phillips67d52cf2017-06-05 13:38:13 -04001329 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001330 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001331 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001332 GL_CALL(BindTexture(texDesc.fTarget, 0));
1333 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001334
Brian Salomonea4ad302019-08-07 13:04:55 -04001335 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1336 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 return return_null_texture();
1338 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001339 tex = sk_make_sp<GrGLTextureRenderTarget>(
1340 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001341 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001342 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001343 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001344 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001345 // The non-sampler params are still at their default values.
1346 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1347 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001348 if (levelClearMask) {
1349 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001350 GrColorType colorType;
1351 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1352 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001353 if (this->glCaps().clearTextureSupport()) {
1354 for (int i = 0; i < mipLevelCount; ++i) {
1355 if (levelClearMask & (1U << i)) {
1356 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1357 nullptr));
1358 }
1359 }
1360 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1361 !this->glCaps().performColorClearsAsDraws()) {
1362 this->disableScissor();
1363 this->disableWindowRectangles();
1364 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001365 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001366 for (int i = 0; i < mipLevelCount; ++i) {
1367 if (levelClearMask & (1U << i)) {
1368 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1369 kDst_TempFBOTarget);
1370 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1371 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1372 }
1373 }
Brian Salomonc2249852019-09-16 11:08:50 -04001374 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001375 } else {
1376 std::unique_ptr<char[]> zeros;
1377 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1378 for (int i = 0; i < mipLevelCount; ++i) {
1379 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001380 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1381 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001382 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1383 // smaller levels that need clearing.
1384 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001385 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001386 size_t size = levelWidth * levelHeight * bpp;
1387 zeros.reset(new char[size]());
1388 }
1389 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1390 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1391 externalFormat, externalType, zeros.get()));
1392 }
Brian Salomona3e29962019-07-16 11:52:08 -04001393 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001394 }
1395 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001396 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001397}
1398
Robert Phillips9f744f72019-12-19 19:14:33 -05001399sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001400 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001401 SkBudgeted budgeted,
1402 GrMipMapped mipMapped,
1403 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001404 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001405 // We don't support protected textures in GL.
1406 if (isProtected == GrProtected::kYes) {
1407 return nullptr;
1408 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001409 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001410 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001411 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001412 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001413 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001414 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001415 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001416 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001417 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001418 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001419 return nullptr;
1420 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001421
Robert Phillipsee946932019-12-18 11:16:17 -05001422 // Unbind this texture from the scratch texture unit.
1423 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1424
Robert Phillipse4720c62020-01-14 14:33:24 -05001425 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1426 ? GrMipMapsStatus::kValid
1427 : GrMipMapsStatus::kNotAllocated;
1428
1429 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001430 // The non-sampler params are still at their default values.
1431 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1432 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001433 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001434}
1435
Robert Phillipsb915c942019-12-17 14:44:37 -05001436GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1437 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001438 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001439 GrProtected isProtected,
1440 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001441 // We don't support protected textures in GL.
1442 if (isProtected == GrProtected::kYes) {
1443 return {};
1444 }
1445
1446 this->handleDirtyContext();
1447
1448 GrGLFormat glFormat = format.asGLFormat();
1449 if (glFormat == GrGLFormat::kUnknown) {
1450 return {};
1451 }
1452
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001453 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001454 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001455 SkAutoMalloc am;
1456
1457 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1458 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1459 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001460 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001461 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001462 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1463 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001464
Robert Phillips99dead92020-01-27 16:11:57 -05001465 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1466 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001467
1468 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001469
1470 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1471
1472 rawData = (const char*) am.get();
1473 }
1474
1475 GrGLTextureInfo info;
1476 GrGLTextureParameters::SamplerOverriddenState initialState;
1477
1478 info.fTarget = GR_GL_TEXTURE_2D;
1479 info.fFormat = GrGLFormatToEnum(glFormat);
1480 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001481 mipMapped, &initialState,
1482 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001483 if (!info.fID) {
1484 return {};
1485 }
1486
1487 // Unbind this texture from the scratch texture unit.
1488 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1489
1490 auto parameters = sk_make_sp<GrGLTextureParameters>();
1491 // The non-sampler params are still at their default values.
1492 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1493 fResetTimestampForTextureParameters);
1494
1495 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1496 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001497}
1498
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001499namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001500
egdaniel8dc7c3a2015-04-16 11:22:42 -07001501const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001502
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001503void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001504 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001505
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001506 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001507 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001508 (kUnknownBitCount == format->fTotalBits));
1509 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001510 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1512 (GrGLint*)&format->fStencilBits);
1513 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001514 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001515 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1516 (GrGLint*)&format->fTotalBits);
1517 format->fTotalBits += format->fStencilBits;
1518 } else {
1519 format->fTotalBits = format->fStencilBits;
1520 }
1521 }
1522}
1523}
1524
Brian Salomon5043f1f2019-07-11 21:27:54 -04001525int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001526 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001527 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1528
1529 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001530 // Default to unsupported, set this if we find a stencil format that works.
1531 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001532
Brian Salomond2a8ae22019-09-10 16:03:59 -04001533 GrGLuint colorID =
1534 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1535 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001536 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001537 }
egdanielff1d5472015-09-10 08:37:20 -07001538 // unbind the texture from the texture unit before binding it to the frame buffer
1539 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1540
1541 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001542 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001543 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001544 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001545 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001546 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1547 GR_GL_COLOR_ATTACHMENT0,
1548 GR_GL_TEXTURE_2D,
1549 colorID,
1550 0));
bsalomon30447372015-12-21 09:03:05 -08001551 GrGLuint sbRBID = 0;
1552 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001553
1554 // look over formats till I find a compatible one
1555 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001556 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001557 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001558 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1559 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1560 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1561 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1562 sFmt.fInternalFormat,
1563 kSize, kSize));
1564 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001565 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001566 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001567 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001568 if (sFmt.fPacked) {
1569 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1570 GR_GL_DEPTH_ATTACHMENT,
1571 GR_GL_RENDERBUFFER, sbRBID));
1572 } else {
1573 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1574 GR_GL_DEPTH_ATTACHMENT,
1575 GR_GL_RENDERBUFFER, 0));
1576 }
1577 GrGLenum status;
1578 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1579 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1580 firstWorkingStencilFormatIndex = i;
1581 break;
1582 }
egdanielff1d5472015-09-10 08:37:20 -07001583 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1584 GR_GL_STENCIL_ATTACHMENT,
1585 GR_GL_RENDERBUFFER, 0));
1586 if (sFmt.fPacked) {
1587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1588 GR_GL_DEPTH_ATTACHMENT,
1589 GR_GL_RENDERBUFFER, 0));
1590 }
egdanielff1d5472015-09-10 08:37:20 -07001591 }
1592 }
bsalomon30447372015-12-21 09:03:05 -08001593 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001596 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1597 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001598 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001599 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001600 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001601}
1602
Brian Salomonea4ad302019-08-07 13:04:55 -04001603GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001604 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001605 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001606 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001607 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001608 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001609 if (format == GrGLFormat::kUnknown) {
1610 return 0;
1611 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001612 GrGLuint id = 0;
1613 GL_CALL(GenTextures(1, &id));
1614 if (!id) {
1615 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001616 }
1617
Brian Salomonea4ad302019-08-07 13:04:55 -04001618 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001619
Brian Salomonea4ad302019-08-07 13:04:55 -04001620 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1621
Robert Phillips42716d42019-12-16 12:19:54 -05001622 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001623 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1624 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001625 GL_CALL(DeleteTextures(1, &id));
1626 return 0;
1627 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001628 }
Robert Phillips42716d42019-12-16 12:19:54 -05001629
Brian Salomonea4ad302019-08-07 13:04:55 -04001630 return id;
1631}
1632
Robert Phillips2716daf2020-01-23 12:53:42 -05001633GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001634 GrGLFormat format,
1635 GrRenderable renderable,
1636 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001637 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001638 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001639 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001640
Brian Salomonea4ad302019-08-07 13:04:55 -04001641 GrGLuint id = 0;
1642 GL_CALL(GenTextures(1, &id));
1643
1644 if (!id) {
1645 return 0;
1646 }
1647
1648 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001649
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001650 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001651 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001652 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001653 }
1654
Brian Salomond2a8ae22019-09-10 16:03:59 -04001655 if (initialState) {
1656 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1657 } else {
1658 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001659 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001660
1661 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1662
1663 bool success = false;
1664 if (internalFormat) {
1665 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1666 if (this->glCaps().formatSupportsTexStorage(format)) {
1667 GL_ALLOC_CALL(this->glInterface(),
Brian Osman788b9162020-02-07 10:36:46 -05001668 TexStorage2D(GR_GL_TEXTURE_2D, std::max(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001669 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001670 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1671 } else {
1672 GrGLenum externalFormat, externalType;
1673 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1674 GrGLenum error = GR_GL_NO_ERROR;
1675 if (externalFormat && externalType) {
1676 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1677 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001678 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1679 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001680 GL_ALLOC_CALL(
1681 this->glInterface(),
1682 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1683 currentHeight, 0, externalFormat, externalType, nullptr));
1684 error = CHECK_ALLOC_ERROR(this->glInterface());
1685 }
1686 success = (GR_GL_NO_ERROR == error);
1687 }
1688 }
1689 }
1690 if (success) {
1691 return id;
1692 }
1693 GL_CALL(DeleteTextures(1, &id));
1694 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001695}
1696
Chris Daltoneffee202019-07-01 22:28:03 -06001697GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1698 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001699 SkASSERT(width >= rt->width());
1700 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001701
egdaniel8dc7c3a2015-04-16 11:22:42 -07001702 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001703
Brian Salomond4764a12019-08-08 12:08:24 -04001704 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001705 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001706 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001707 }
egdanielff1d5472015-09-10 08:37:20 -07001708
1709 if (!sbDesc.fRenderbufferID) {
1710 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1711 }
1712 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001713 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001714 }
1715 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1716 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1717 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1718 // we do this "if" so that we don't call the multisample
1719 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001720 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001721 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001722 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001723 sFmt.fInternalFormat,
1724 width, height));
1725 } else {
1726 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1727 sFmt.fInternalFormat,
1728 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001729 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001730 }
1731 fStats.incStencilAttachmentCreates();
1732 // After sized formats we attempt an unsized format and take
1733 // whatever sizes GL gives us. In that case we query for the size.
1734 GrGLStencilAttachment::Format format = sFmt;
1735 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001736 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1737 sbDesc,
1738 width,
1739 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001740 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001741 format);
1742 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001743}
1744
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001745////////////////////////////////////////////////////////////////////////////////
1746
Brian Salomondbf70722019-02-07 11:31:24 -05001747sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1748 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001749 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001750}
1751
Greg Danielacd66b42019-05-22 16:29:12 -04001752void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001753 GrSurfaceOrigin rtOrigin) {
1754 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001755 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Chris Daltondc2b15e2020-02-19 11:45:21 -07001756 if (fHWScissorSettings.fRect != scissor) {
1757 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
1758 fHWScissorSettings.fRect = scissor;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001759 }
Chris Daltondc2b15e2020-02-19 11:45:21 -07001760 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1761 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1762 fHWScissorSettings.fEnabled = kYes_TriState;
1763 }
1764 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001766
1767 // See fall through note above
1768 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001769}
1770
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001771void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001772 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001773#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001774 typedef GrWindowRectsState::Mode Mode;
1775 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1776 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001777
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001778 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001779 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001780 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001781 }
1782
csmartdalton7535f412016-08-23 06:51:00 -07001783 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1784 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001785 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001786 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001787
Chris Daltond6cda8d2019-09-05 02:30:04 -06001788 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001789 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001790 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001791 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001792 }
1793
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001794 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001795 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001796
Greg Danielacd66b42019-05-22 16:29:12 -04001797 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001798#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001799}
1800
1801void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001803 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001804 return;
1805 }
1806 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001807 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001808#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001809}
1810
Robert Phillipsfcaae482019-11-07 10:17:03 -05001811bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001812 this->handleDirtyContext();
1813
1814 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1815 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1816 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001817
Stephen Whiteb1857852020-02-07 15:33:23 +00001818 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001819 if (!program) {
1820 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1821 return false;
1822 }
brianosman33f6b3f2016-06-02 05:49:21 -07001823
Brian Salomon802cb312018-06-08 18:05:20 -04001824 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001825
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001826 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001827 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1828 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001829
Robert Phillips901aff02019-10-08 12:32:56 -04001830 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001831
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001832 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001833 GrStencilSettings stencil;
1834 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001835 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1836 stencil.reset(*programInfo.pipeline().getUserStencil(),
1837 programInfo.pipeline().hasStencilClip(),
1838 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001839 }
Robert Phillips901aff02019-10-08 12:32:56 -04001840 this->flushStencil(stencil, programInfo.origin());
1841 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001842 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001843 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1844 : kBogusScissor);
1845 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001846 } else {
1847 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001848 }
Robert Phillips901aff02019-10-08 12:32:56 -04001849 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1850 glRT, programInfo.origin());
1851 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001852 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001853 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001854
1855 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001856 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001857 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001858
1859 return true;
1860}
1861
Brian Salomon802cb312018-06-08 18:05:20 -04001862void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1863 if (!program) {
1864 fHWProgram.reset();
1865 fHWProgramID = 0;
1866 return;
1867 }
1868 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1869 if (program == fHWProgram) {
1870 return;
1871 }
1872 auto id = program->programID();
1873 SkASSERT(id);
1874 GL_CALL(UseProgram(id));
1875 fHWProgram = std::move(program);
1876 fHWProgramID = id;
1877}
1878
1879void GrGLGpu::flushProgram(GrGLuint id) {
1880 SkASSERT(id);
1881 if (fHWProgramID == id) {
1882 SkASSERT(!fHWProgram);
1883 return;
1884 }
1885 fHWProgram.reset();
1886 GL_CALL(UseProgram(id));
1887 fHWProgramID = id;
1888}
1889
1890void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001891 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001892 int baseVertex,
1893 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001894 int baseInstance,
1895 GrPrimitiveRestart enablePrimitiveRestart) {
1896 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001897
cdaltone2e71c22016-04-07 18:13:29 -07001898 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001899 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001900 SkASSERT(indexBuffer->isCpuBuffer() ||
1901 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001902 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001903 } else {
1904 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001905 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001906
Brian Salomon92be2f72018-06-19 14:33:47 -04001907 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1908 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001909
Brian Salomon802cb312018-06-08 18:05:20 -04001910 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001911 SkASSERT(vertexBuffer);
1912 SkASSERT(vertexBuffer->isCpuBuffer() ||
1913 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1914 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001915 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1916 const auto& attrib = fHWProgram->vertexAttribute(i);
1917 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001918 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1919 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001920 }
Chris Dalton1d616352017-05-31 12:51:23 -06001921 }
Brian Salomon802cb312018-06-08 18:05:20 -04001922 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001923 SkASSERT(instanceBuffer);
1924 SkASSERT(instanceBuffer->isCpuBuffer() ||
1925 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1926 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001927 int attribIdx = fHWProgram->numVertexAttributes();
1928 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1929 const auto& attrib = fHWProgram->instanceAttribute(i);
1930 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001931 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1932 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1933 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001934 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001935 }
1936}
1937
Brian Salomonae64c192019-02-05 09:41:37 -05001938GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001939 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001940
cdaltone2e71c22016-04-07 18:13:29 -07001941 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001942 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001943 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001944 }
cdaltone2e71c22016-04-07 18:13:29 -07001945
Brian Salomonae64c192019-02-05 09:41:37 -05001946 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001947 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001948 if (!bufferState->fBufferZeroKnownBound) {
1949 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1950 bufferState->fBufferZeroKnownBound = true;
1951 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001952 }
Brian Salomondbf70722019-02-07 11:31:24 -05001953 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1954 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001955 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1956 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1957 bufferState->fBufferZeroKnownBound = false;
1958 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001959 }
1960
Brian Salomonae64c192019-02-05 09:41:37 -05001961 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001962}
Brian Salomond818ebf2018-07-02 14:08:49 +00001963void GrGLGpu::disableScissor() {
1964 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1965 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1966 fHWScissorSettings.fEnabled = kNo_TriState;
1967 return;
1968 }
1969}
1970
Brian Osman9a9baae2018-11-05 15:06:26 -05001971void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001972 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001973 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001974 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001975 SkASSERT(!this->caps()->performColorClearsAsDraws());
1976 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001977
Brian Salomon43f8bf02017-10-18 08:33:29 -04001978 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001979
Brian Salomon43f8bf02017-10-18 08:33:29 -04001980 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1981
Brian Salomond818ebf2018-07-02 14:08:49 +00001982 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001983 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001984 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001985 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001986 }
Greg Danielacd66b42019-05-22 16:29:12 -04001987 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001988 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001989 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001990 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001991 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001992}
1993
Robert Phillips95214472017-08-08 18:00:03 -04001994void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001995 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1996
Robert Phillips95214472017-08-08 18:00:03 -04001997 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001998 return;
1999 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002000
Chris Dalton674f77a2019-09-30 20:49:39 -06002001 // This should only be called internally when we know we have a stencil buffer.
2002 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002003
bsalomonb0bd4f62014-09-03 07:19:50 -07002004 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002005 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002006
Brian Salomond818ebf2018-07-02 14:08:49 +00002007 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002008 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002009
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002011 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002013 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002014}
2015
Chris Daltonb50cc812019-10-14 16:29:21 -06002016static bool use_tiled_rendering(const GrGLCaps& glCaps,
2017 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2018 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2019 // Otherwise it's faster to just not use it.
2020 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2021 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2022}
2023
2024void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002025 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2026 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2027 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2028
2029 this->handleDirtyContext();
2030
2031 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2032 this->flushRenderTarget(glRT);
2033 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2034
Chris Daltonb50cc812019-10-14 16:29:21 -06002035 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2036 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2037 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2038 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2039 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2040 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2041 nativeBounds.fHeight, preserveMask));
2042 }
2043
Chris Dalton674f77a2019-09-30 20:49:39 -06002044 GrGLbitfield clearMask = 0;
2045 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2046 SkASSERT(!this->caps()->performColorClearsAsDraws());
2047 this->flushClearColor(colorLoadStore.fClearColor);
2048 this->flushColorWrite(true);
2049 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002050 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002051 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2052 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2053 GL_CALL(StencilMask(0xffffffff));
2054 GL_CALL(ClearStencil(0));
2055 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2056 }
2057 if (clearMask) {
2058 this->disableScissor();
2059 this->disableWindowRectangles();
2060 GL_CALL(Clear(clearMask));
2061 }
2062}
2063
2064void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2065 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2066 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2067 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2068
2069 this->handleDirtyContext();
2070
2071 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2072 // The framebuffer binding changed in the middle of a command buffer. We should have already
2073 // printed a warning during onFBOChanged.
2074 return;
2075 }
2076
2077 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2078 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2079
2080 SkSTArray<2, GrGLenum> discardAttachments;
2081 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2082 discardAttachments.push_back(
2083 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2084 }
2085 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2086 discardAttachments.push_back(
2087 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2088 }
2089
2090 if (!discardAttachments.empty()) {
2091 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2092 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2093 discardAttachments.begin()));
2094 } else {
2095 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2096 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2097 discardAttachments.begin()));
2098 }
2099 }
2100 }
2101
Chris Daltonb50cc812019-10-14 16:29:21 -06002102 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2103 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2104 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2105 // Handled by use_tiled_rendering().
2106 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2107 GL_CALL(EndTiling(preserveMask));
2108 }
2109
Chris Dalton674f77a2019-09-30 20:49:39 -06002110 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002111}
2112
csmartdalton29df7602016-08-31 11:55:52 -07002113void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2114 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002115 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002116 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002117 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002118 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002119
egdaniel8dc7c3a2015-04-16 11:22:42 -07002120 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002121 if (!sb) {
2122 // We should only get here if we marked a proxy as requiring a SB. However,
2123 // the SB creation could later fail. Likely clipping is going to go awry now.
2124 return;
2125 }
2126
bsalomon6bc1b5f2015-02-23 09:06:38 -08002127 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002128#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002129 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002130 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002131#else
2132 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002133 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002134 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002135 // changing the clip between stencil passes may or may not
2136 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002137 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002138#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002139 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002140 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002141 value = (1 << (stencilBitCount - 1));
2142 } else {
2143 value = 0;
2144 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002145 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002146 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002147
Greg Danielacd66b42019-05-22 16:29:12 -04002148 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002149 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002150
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002151 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002152 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002154 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002155}
2156
Brian Salomone05ba5a2019-04-08 11:59:07 -04002157bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002158 GrColorType surfaceColorType, GrColorType dstColorType,
2159 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002160 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002161
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002162 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002163 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002164 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002165 return false;
2166 }
Greg Danielba88ab62019-07-26 09:14:01 -04002167 GrGLenum externalFormat = 0;
2168 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002169 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2170 surfaceColorType,
2171 dstColorType,
2172 &externalFormat,
2173 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002174 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002175 return false;
2176 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002177
Brian Salomon71d9d842016-11-03 13:42:00 -04002178 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002179 if (renderTarget->numSamples() <= 1 ||
2180 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2181 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2182 this->flushRenderTargetNoColorWrites(renderTarget);
2183 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2184 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2185 return false;
2186 } else {
2187 SkASSERT(renderTarget->requiresManualMSAAResolve());
2188 // we don't track the state of the READ FBO ID.
2189 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002190 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002191 } else {
2192 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002193 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002194 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 }
2196
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002197 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002198 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002199
Brian Salomona6948702018-06-01 15:33:20 -04002200 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002201 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002202 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002203 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002204 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002205 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002206
Brian Osman1348ed02018-09-12 09:08:39 -04002207 bool reattachStencil = false;
2208 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2209 renderTarget &&
2210 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002211 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002212 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2213 reattachStencil = true;
2214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2215 GR_GL_RENDERBUFFER, 0));
2216 }
2217
Chris Dalton76500e52019-09-05 02:13:05 -06002218 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002219 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002220
2221 if (reattachStencil) {
2222 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2223 renderTarget->renderTargetPriv().getStencilAttachment());
2224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2225 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2226 }
2227
Brian Salomon26de56e2019-04-10 12:14:26 -04002228 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002229 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002230 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2231 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002232
Brian Salomone05ba5a2019-04-08 11:59:07 -04002233 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002234 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002235 }
2236 return true;
2237}
2238
2239bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002240 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2241 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002242 SkASSERT(surface);
2243
Brian Salomonb28cb682019-07-26 12:48:47 -04002244 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002245
Brian Salomon26de56e2019-04-10 12:14:26 -04002246 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2247 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002248
Brian Salomon1047a492019-07-02 12:25:21 -04002249 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002250 rowPixelWidth = width;
2251 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002252 SkASSERT(!(rowBytes % bytesPerPixel));
2253 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002254 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002255 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2256 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002257}
2258
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002259GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002260 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002261 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002262 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002263 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002264 if (!fCachedOpsRenderPass) {
2265 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002266 }
2267
Chris Daltonb50cc812019-10-14 16:29:21 -06002268 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002269 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002270}
2271
Brian Salomon1fabd512018-02-09 09:54:25 -05002272void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002273 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002274 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002275 this->didWriteToSurface(target, origin, &bounds);
2276}
bsalomon6ba6fa12015-03-04 11:57:37 -08002277
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002278void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2279 this->flushRenderTargetNoColorWrites(target);
2280 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002281}
2282
Brian Osman9aa30c62018-07-02 15:21:46 -04002283void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002284 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002285 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002286 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002287 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002288#ifdef SK_DEBUG
2289 // don't do this check in Chromium -- this is causing
2290 // lots of repeated command buffer flushes when the compositor is
2291 // rendering with Ganesh, which is really slow; even too slow for
2292 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002293 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002294 GrGLenum status;
2295 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2296 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2297 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2298 }
bsalomon160f24c2015-03-17 15:55:42 -07002299 }
egdanield803f272015-03-18 13:01:52 -07002300#endif
2301 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002302 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002303 }
2304
brianosman35b784d2016-05-05 11:52:53 -07002305 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002306 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002307 }
Brian Salomon9b553272020-01-24 14:38:37 -05002308
2309 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2310 GrGLint format;
2311 GrGLint type;
2312 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2313 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2314 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2315 }
bsalomon083617b2016-02-12 12:10:14 -08002316}
bsalomona9909122016-01-23 10:41:40 -08002317
brianosman33f6b3f2016-06-02 05:49:21 -07002318void GrGLGpu::flushFramebufferSRGB(bool enable) {
2319 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2320 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2321 fHWSRGBFramebuffer = kYes_TriState;
2322 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2323 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2324 fHWSRGBFramebuffer = kNo_TriState;
2325 }
2326}
2327
Greg Danielacd66b42019-05-22 16:29:12 -04002328void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002329 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002330 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002331 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002332 fHWViewport = viewport;
2333 }
2334}
2335
bsalomon@google.comd302f142011-03-03 13:54:13 +00002336#define SWAP_PER_DRAW 0
2337
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002338#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002339 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002340 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002341 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002342 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002343 void SwapBuf() {
2344 DWORD procID = GetCurrentProcessId();
2345 HWND hwnd = GetTopWindow(GetDesktopWindow());
2346 while(hwnd) {
2347 DWORD wndProcID = 0;
2348 GetWindowThreadProcessId(hwnd, &wndProcID);
2349 if(wndProcID == procID) {
2350 SwapBuffers(GetDC(hwnd));
2351 }
2352 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2353 }
2354 }
2355 #endif
2356#endif
2357
Chris Dalton4386ad12020-02-19 16:42:06 -07002358void GrGLGpu::drawMeshes(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo,
2359 const GrMesh meshes[], int meshCount) {
Robert Phillips2579de42019-10-09 09:51:59 -04002360 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002361
Robert Phillips901aff02019-10-08 12:32:56 -04002362 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2363 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2364
Brian Salomonf7232642018-09-19 08:58:08 -04002365 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002366 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2367 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002368 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002370
Robert Phillips901aff02019-10-08 12:32:56 -04002371 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002372 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002373 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2374 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002375 }
Robert Phillips901aff02019-10-08 12:32:56 -04002376 if (hasDynamicPrimProcTextures) {
2377 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2378 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2379 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002380 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002381 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Chris Dalton79f336d2020-02-11 14:42:56 -07002382 GrIsPrimTypeLines(programInfo.primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002383 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2384 GL_CALL(Enable(GR_GL_CULL_FACE));
2385 GL_CALL(Disable(GR_GL_CULL_FACE));
2386 }
Chris Dalton79f336d2020-02-11 14:42:56 -07002387 meshes[m].sendToGpu(programInfo.primitiveType(), this);
2388 fLastPrimitiveType = programInfo.primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002389 }
ethannicholas22793252016-01-30 09:59:10 -08002390
bsalomon@google.comd302f142011-03-03 13:54:13 +00002391#if SWAP_PER_DRAW
2392 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002393 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002394 aglSwapBuffers(aglGetCurrentContext());
2395 int set_a_break_pt_here = 9;
2396 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002397 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002398 SwapBuf();
2399 int set_a_break_pt_here = 9;
2400 SwapBuf();
2401 #endif
2402#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002403}
2404
Chris Dalton3809bab2017-06-13 10:55:06 -06002405static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2406 switch (primitiveType) {
2407 case GrPrimitiveType::kTriangles:
2408 return GR_GL_TRIANGLES;
2409 case GrPrimitiveType::kTriangleStrip:
2410 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002411 case GrPrimitiveType::kPoints:
2412 return GR_GL_POINTS;
2413 case GrPrimitiveType::kLines:
2414 return GR_GL_LINES;
2415 case GrPrimitiveType::kLineStrip:
2416 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002417 case GrPrimitiveType::kPatches:
2418 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002419 case GrPrimitiveType::kPath:
2420 SK_ABORT("non-mesh-based GrPrimitiveType");
2421 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002422 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002423 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002424}
2425
Chris Dalton79f336d2020-02-11 14:42:56 -07002426void GrGLGpu::sendArrayMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh, int vertexCount,
2427 int baseVertex) {
2428 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002429 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002430 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2431 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002432 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2433 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002434 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002435 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2436 }
2437 fStats.incNumDraws();
2438}
2439
Brian Salomondbf70722019-02-07 11:31:24 -05002440static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2441 size_t baseOffset = baseIndex * sizeof(uint16_t);
2442 if (indexBuffer->isCpuBuffer()) {
2443 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2444 } else {
2445 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2446 }
2447}
2448
Chris Dalton79f336d2020-02-11 14:42:56 -07002449void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2450 int indexCount, int baseIndex, uint16_t minIndexValue,
2451 uint16_t maxIndexValue, int baseVertex) {
2452 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002453 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2454
2455 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2456 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002457
2458 if (this->glCaps().drawRangeElementsSupport()) {
2459 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002460 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002461 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002462 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002463 }
2464 fStats.incNumDraws();
2465}
2466
Chris Dalton79f336d2020-02-11 14:42:56 -07002467void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2468 int vertexCount, int baseVertex, int instanceCount,
2469 int baseInstance) {
2470 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002471 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002472 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002473 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2474 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002475 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
Brian Osman788b9162020-02-07 10:36:46 -05002476 std::min(instanceCount - i, maxInstances)));
Chris Daltoncc604e52017-10-06 16:27:32 -06002477 fStats.incNumDraws();
2478 }
Chris Dalton1d616352017-05-31 12:51:23 -06002479}
2480
Chris Dalton79f336d2020-02-11 14:42:56 -07002481void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2482 int indexCount, int baseIndex, int baseVertex,
2483 int instanceCount, int baseInstance) {
2484 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002485 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002486 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2487 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002488 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2489 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002490 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Brian Osman788b9162020-02-07 10:36:46 -05002491 std::min(instanceCount - i, maxInstances)));
Chris Dalton1b4ad762018-10-04 11:58:09 -06002492 fStats.incNumDraws();
2493 }
Chris Dalton1d616352017-05-31 12:51:23 -06002494}
2495
Chris Dalton16a33c62019-09-24 22:19:17 -06002496void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002497 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002498 // Some extensions automatically resolves the texture when it is read.
2499 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2500
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002501 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002502 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2503 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2504 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2505 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002506
Chris Daltonc139d082019-09-26 14:04:24 -06002507 // make sure we go through flushRenderTarget() since we've modified
2508 // the bound DRAW FBO ID.
2509 fHWBoundRenderTargetUniqueID.makeInvalid();
2510 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2511 // Apple's extension uses the scissor as the blit bounds.
2512 GrScissorState scissorState;
2513 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002514 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2515 // happens inside flushScissor since resolveRect is already in native device coordinates.
2516 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002517 this->disableWindowRectangles();
2518 GL_CALL(ResolveMultisampleFramebuffer());
2519 } else {
2520 int l, b, r, t;
2521 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2522 this->glCaps().blitFramebufferSupportFlags()) {
2523 l = 0;
2524 b = 0;
2525 r = target->width();
2526 t = target->height();
2527 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002528 l = resolveRect.x();
2529 b = resolveRect.y();
2530 r = resolveRect.x() + resolveRect.width();
2531 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002532 }
Chris Daltonc139d082019-09-26 14:04:24 -06002533
2534 // BlitFrameBuffer respects the scissor, so disable it.
2535 this->disableScissor();
2536 this->disableWindowRectangles();
2537 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 +00002538 }
2539}
2540
bsalomon@google.com411dad02012-06-05 20:24:20 +00002541namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002542
bsalomon@google.com411dad02012-06-05 20:24:20 +00002543
2544GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002545 static const GrGLenum gTable[kGrStencilOpCount] = {
2546 GR_GL_KEEP, // kKeep
2547 GR_GL_ZERO, // kZero
2548 GR_GL_REPLACE, // kReplace
2549 GR_GL_INVERT, // kInvert
2550 GR_GL_INCR_WRAP, // kIncWrap
2551 GR_GL_DECR_WRAP, // kDecWrap
2552 GR_GL_INCR, // kIncClamp
2553 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002554 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002555 static_assert(0 == (int)GrStencilOp::kKeep);
2556 static_assert(1 == (int)GrStencilOp::kZero);
2557 static_assert(2 == (int)GrStencilOp::kReplace);
2558 static_assert(3 == (int)GrStencilOp::kInvert);
2559 static_assert(4 == (int)GrStencilOp::kIncWrap);
2560 static_assert(5 == (int)GrStencilOp::kDecWrap);
2561 static_assert(6 == (int)GrStencilOp::kIncClamp);
2562 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002563 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2564 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002565}
2566
2567void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002568 const GrStencilSettings::Face& face,
2569 GrGLenum glFace) {
2570 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2571 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2572 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002573
cdalton93a379b2016-05-11 13:58:08 -07002574 GrGLint ref = face.fRef;
2575 GrGLint mask = face.fTestMask;
2576 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002577
2578 if (GR_GL_FRONT_AND_BACK == glFace) {
2579 // we call the combined func just in case separate stencil is not
2580 // supported.
2581 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2582 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002583 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002584 } else {
2585 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2586 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002587 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002588 }
2589}
2590}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002591
Chris Dalton71713f62019-04-18 12:41:03 -06002592void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002593 if (stencilSettings.isDisabled()) {
2594 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002595 } else if (fHWStencilSettings != stencilSettings ||
2596 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002597 if (kYes_TriState != fHWStencilTestEnabled) {
2598 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002599
csmartdaltonc7d85332016-10-26 10:13:46 -07002600 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002601 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002602 if (!stencilSettings.isTwoSided()) {
2603 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2604 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002605 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002606 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2607 GR_GL_FRONT);
2608 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2609 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002610 }
joshualitta58fe352014-10-27 08:39:00 -07002611 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002612 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002613 }
2614}
2615
csmartdaltonc7d85332016-10-26 10:13:46 -07002616void GrGLGpu::disableStencil() {
2617 if (kNo_TriState != fHWStencilTestEnabled) {
2618 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002619
csmartdaltonc7d85332016-10-26 10:13:46 -07002620 fHWStencilTestEnabled = kNo_TriState;
2621 fHWStencilSettings.invalidate();
2622 }
2623}
2624
Chris Dalton4c56b032019-03-04 12:28:42 -07002625void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002626 // rt is only optional if useHWAA is false.
2627 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002628#ifdef SK_DEBUG
2629 if (useHWAA && rt->numSamples() <= 1) {
2630 SkASSERT(this->caps()->mixedSamplesSupport());
2631 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2632 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2633 }
2634#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002635
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002636 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002637 if (useHWAA) {
2638 if (kYes_TriState != fMSAAEnabled) {
2639 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2640 fMSAAEnabled = kYes_TriState;
2641 }
2642 } else {
2643 if (kNo_TriState != fMSAAEnabled) {
2644 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2645 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002646 }
2647 }
2648 }
2649}
2650
Chris Daltonce425af2019-12-16 10:39:03 -07002651void GrGLGpu::flushConservativeRasterState(bool enabled) {
2652 if (this->caps()->conservativeRasterSupport()) {
2653 if (enabled) {
2654 if (kYes_TriState != fHWConservativeRasterEnabled) {
2655 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2656 fHWConservativeRasterEnabled = kYes_TriState;
2657 }
2658 } else {
2659 if (kNo_TriState != fHWConservativeRasterEnabled) {
2660 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2661 fHWConservativeRasterEnabled = kNo_TriState;
2662 }
2663 }
2664 }
2665}
2666
Chris Dalton1215cda2019-12-17 21:44:04 -07002667void GrGLGpu::flushWireframeState(bool enabled) {
2668 if (this->caps()->wireframeSupport()) {
2669 if (this->caps()->wireframeMode() || enabled) {
2670 if (kYes_TriState != fHWWireframeEnabled) {
2671 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2672 fHWWireframeEnabled = kYes_TriState;
2673 }
2674 } else {
2675 if (kNo_TriState != fHWWireframeEnabled) {
2676 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2677 fHWWireframeEnabled = kNo_TriState;
2678 }
2679 }
2680 }
2681}
2682
Chris Daltonbbb3f642019-07-24 12:25:08 -04002683void GrGLGpu::flushBlendAndColorWrite(
2684 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2685 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2686 // We need to work around a driver bug by using a blend state that preserves the dst color,
2687 // rather than disabling color writes.
2688 GrXferProcessor::BlendInfo preserveDstBlend;
2689 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2690 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2691 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2692 return;
2693 }
bsalomonf7cc8772015-05-11 11:21:14 -07002694
cdalton8917d622015-05-06 13:40:21 -07002695 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002696 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2697 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002698
2699 // Any optimization to disable blending should have already been applied and
2700 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002701 bool blendOff =
2702 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2703 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2704 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002705
egdanielb414f252014-07-29 13:15:47 -07002706 if (blendOff) {
2707 if (kNo_TriState != fHWBlendState.fEnabled) {
2708 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002709
2710 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2711 // https://code.google.com/p/skia/issues/detail?id=3943
2712 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2713 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2714 SkASSERT(this->caps()->advancedBlendEquationSupport());
2715 // Set to any basic blending equation.
2716 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2717 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2718 fHWBlendState.fEquation = blend_equation;
2719 }
2720
egdanielb414f252014-07-29 13:15:47 -07002721 fHWBlendState.fEnabled = kNo_TriState;
2722 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002723 } else {
2724 if (kYes_TriState != fHWBlendState.fEnabled) {
2725 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002726
Chris Daltonbbb3f642019-07-24 12:25:08 -04002727 fHWBlendState.fEnabled = kYes_TriState;
2728 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002729
Chris Daltonbbb3f642019-07-24 12:25:08 -04002730 if (fHWBlendState.fEquation != equation) {
2731 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2732 fHWBlendState.fEquation = equation;
2733 }
cdalton8917d622015-05-06 13:40:21 -07002734
Chris Daltonbbb3f642019-07-24 12:25:08 -04002735 if (GrBlendEquationIsAdvanced(equation)) {
2736 SkASSERT(this->caps()->advancedBlendEquationSupport());
2737 // Advanced equations have no other blend state.
2738 return;
2739 }
cdalton8917d622015-05-06 13:40:21 -07002740
Chris Daltonbbb3f642019-07-24 12:25:08 -04002741 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2742 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2743 gXfermodeCoeff2Blend[dstCoeff]));
2744 fHWBlendState.fSrcCoeff = srcCoeff;
2745 fHWBlendState.fDstCoeff = dstCoeff;
2746 }
cdalton8917d622015-05-06 13:40:21 -07002747
Chris Daltonbbb3f642019-07-24 12:25:08 -04002748 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2749 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2750 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2751 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2752 fHWBlendState.fConstColor = blendConst;
2753 fHWBlendState.fConstColorValid = true;
2754 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002755 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002756 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002757
2758 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002759}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002760
Brian Salomondc829942018-10-23 16:07:24 -04002761static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002762 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002763 switch (swizzle[i]) {
2764 case 'r': glValues[i] = GR_GL_RED; break;
2765 case 'g': glValues[i] = GR_GL_GREEN; break;
2766 case 'b': glValues[i] = GR_GL_BLUE; break;
2767 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002768 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002769 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002770 default: SK_ABORT("Unsupported component");
2771 }
Brian Salomon327955b2018-10-22 15:39:22 +00002772 }
2773}
2774
Greg Daniel2c3398d2019-06-19 11:58:01 -04002775void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2776 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002777 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002778
reed856e9d92015-09-30 12:21:45 -07002779#ifdef SK_DEBUG
2780 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002781 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002782 const int w = texture->width();
2783 const int h = texture->height();
2784 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2785 }
2786 }
2787#endif
2788
Robert Phillips294870f2016-11-11 12:38:40 -05002789 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002790 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002791 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002792 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002793 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002794 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002795 }
2796
Brian Salomondc829942018-10-23 16:07:24 -04002797 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002798 if (!this->caps()->mipMapSupport() ||
2799 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002800 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002801 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002802 }
bsalomonefd7d452014-10-23 14:17:46 -07002803
brianosman33f6b3f2016-06-02 05:49:21 -07002804#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002805 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002806 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002807 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002808 }
2809#endif
2810
Brian Salomone2826ab2019-06-04 15:58:31 -04002811 auto timestamp = texture->parameters()->resetTimestamp();
2812 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002813
Brian Salomone2826ab2019-06-04 15:58:31 -04002814 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2815 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002816 if (fSamplerObjectCache) {
2817 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2818 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002819 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2820 texture->parameters()->samplerOverriddenState();
2821 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002822
Brian Salomone2826ab2019-06-04 15:58:31 -04002823 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2824 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002825
Brian Salomone2826ab2019-06-04 15:58:31 -04002826 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2827 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002828
2829 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 newSamplerState.fMinLOD = -1000.f;
2831 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002832
Brian Salomone2826ab2019-06-04 15:58:31 -04002833 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002834 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002835 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002836 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002837 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002838 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002839 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002840 }
Mike Klein1bccae52018-10-19 11:38:44 +00002841 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002842 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002843 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002844 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002845 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002847 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002849 }
2850 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002851 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002852 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002853 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002854 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002856 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002858 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002859 if (this->glCaps().clampToBorderSupport()) {
2860 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002861 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002862 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002863 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2864 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002865 }
2866 }
Brian Salomondc829942018-10-23 16:07:24 -04002867 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002868 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2869 newNonsamplerState.fBaseMipMapLevel = 0;
2870 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002871
Brian Salomone2826ab2019-06-04 15:58:31 -04002872 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2873 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002874 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002875 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2876 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002877 GrGLenum glValues[4];
2878 get_gl_swizzle_values(swizzle, glValues);
2879 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002880 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002881 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002882 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2883 reinterpret_cast<const GrGLint*>(glValues)));
2884 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002885 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2886 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2887 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2888 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2889 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002890 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002891 }
2892 }
Brian Salomondc829942018-10-23 16:07:24 -04002893 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002894 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2895 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2896 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002897 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002898 this->setTextureUnit(unitIdx);
2899 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002900 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002901 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002902 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002903 this->setTextureUnit(unitIdx);
2904 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002905 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002906 }
Brian Salomon327955b2018-10-22 15:39:22 +00002907 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002908 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2909 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002910}
2911
Brian Salomon1f05d452019-02-08 12:33:08 -05002912void GrGLGpu::onResetTextureBindings() {
2913 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2914 GR_GL_TEXTURE_EXTERNAL};
2915 for (int i = 0; i < this->numTextureUnits(); ++i) {
2916 this->setTextureUnit(i);
2917 for (auto target : kTargets) {
2918 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2919 GL_CALL(BindTexture(target, 0));
2920 }
2921 }
2922 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2923 }
2924}
2925
Chris Dalton5a2f9622019-12-27 14:56:38 -07002926void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2927 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2928 if (fHWPatchVertexCount != count) {
2929 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2930 fHWPatchVertexCount = count;
2931 }
2932}
2933
egdaniel080e6732014-12-22 07:35:52 -08002934void GrGLGpu::flushColorWrite(bool writeColor) {
2935 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002936 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002937 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2938 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002939 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002940 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002941 } else {
2942 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002943 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002944 fHWWriteToColor = kYes_TriState;
2945 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002946 }
bsalomon3e791242014-12-17 13:43:13 -08002947}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002948
Chris Dalton674f77a2019-09-30 20:49:39 -06002949void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2950 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2951 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2952 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2953 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2954 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2955 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2956 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002957 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2958 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2959 GL_CALL(ClearColor(r, g, b, a));
2960 fHWClearColor[0] = r;
2961 fHWClearColor[1] = g;
2962 fHWClearColor[2] = b;
2963 fHWClearColor[3] = a;
2964 }
2965}
2966
bsalomon861e1032014-12-16 07:33:49 -08002967void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002968 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002969 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002970 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002971 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002972 }
2973}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002974
Brian Salomond978b902019-02-07 15:09:18 -05002975void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002976 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002977 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002978 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2979 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2980 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002981 }
Brian Salomond978b902019-02-07 15:09:18 -05002982 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2983 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002984 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002985 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002986}
2987
Brian Salomone5e7eb12016-10-14 16:18:33 -04002988// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002989static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2990 const GrSurface* src,
2991 const SkIRect& srcRect,
2992 const SkIPoint& dstPoint,
2993 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002994 int dstSampleCnt = 0;
2995 int srcSampleCnt = 0;
2996 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002997 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002998 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002999 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003000 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003001 }
3002 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3003 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3004
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003005 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3006 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3007
Brian Salomone5e7eb12016-10-14 16:18:33 -04003008 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003009 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003010
Greg Daniel46cfbc62019-06-07 11:43:30 -04003011 GrTextureType dstTexType;
3012 GrTextureType* dstTexTypePtr = nullptr;
3013 GrTextureType srcTexType;
3014 GrTextureType* srcTexTypePtr = nullptr;
3015 if (dstTex) {
3016 dstTexType = dstTex->texturePriv().textureType();
3017 dstTexTypePtr = &dstTexType;
3018 }
3019 if (srcTex) {
3020 srcTexType = srcTex->texturePriv().textureType();
3021 srcTexTypePtr = &srcTexType;
3022 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003023
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003024 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3025 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003026 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003027}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003028
Brian Osmana9c8a052018-01-19 10:31:56 -05003029static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3030 // A RT has a separate MSAA renderbuffer if:
3031 // 1) It's multisampled
3032 // 2) We're using an extension with separate MSAA renderbuffers
3033 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003034 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003035}
3036
Greg Daniel46cfbc62019-06-07 11:43:30 -04003037static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003038 const GrGLCaps& caps) {
3039
bsalomon@google.comeb851172013-04-15 13:51:00 +00003040 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003041 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003042 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003043 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003044
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003045 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3046 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3047
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003048 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3049 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3050
Greg Daniel46cfbc62019-06-07 11:43:30 -04003051 GrTextureType dstTexType;
3052 GrTextureType* dstTexTypePtr = nullptr;
3053 GrTextureType srcTexType;
3054 GrTextureType* srcTexTypePtr = nullptr;
3055 if (dstTex) {
3056 dstTexType = dstTex->texturePriv().textureType();
3057 dstTexTypePtr = &dstTexType;
3058 }
3059 if (srcTex) {
3060 srcTexType = srcTex->texturePriv().textureType();
3061 srcTexTypePtr = &srcTexType;
3062 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003063
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003064 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3065 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003066}
3067
Greg Danielacd66b42019-05-22 16:29:12 -04003068// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003069void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003070 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003071 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003072 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003073 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003074 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3075 GrGLuint texID = texture->textureID();
3076 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003077 GrGLuint* tempFBOID;
3078 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003079
egdanield803f272015-03-18 13:01:52 -07003080 if (0 == *tempFBOID) {
3081 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003082 }
3083
Adrienne Walker4ee88512018-05-17 11:37:14 -07003084 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003085 GR_GL_CALL(
3086 this->glInterface(),
3087 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3088 if (mipLevel == 0) {
3089 texture->baseLevelWasBoundToFBO();
3090 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003091 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003092 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003093 }
egdaniel0f5f9672015-02-03 11:10:51 -08003094}
3095
Brian Salomond2a8ae22019-09-10 16:03:59 -04003096void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003097 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003098 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003099 SkASSERT(surface->asTexture());
3100 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3101 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3102 GR_GL_COLOR_ATTACHMENT0,
3103 textureTarget,
3104 0,
3105 0));
3106 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003107}
3108
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003109void GrGLGpu::onFBOChanged() {
3110 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3111 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3112 GL_CALL(Flush());
3113 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003114#ifdef SK_DEBUG
3115 if (fIsExecutingCommandBuffer_DebugOnly) {
3116 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3117 "This will severely hurt performance.\n");
3118 }
3119#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003120}
3121
Adrienne Walker4ee88512018-05-17 11:37:14 -07003122void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3123 fStats.incRenderTargetBinds();
3124 GL_CALL(BindFramebuffer(target, fboid));
3125 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3126 fBoundDrawFramebuffer = fboid;
3127 }
3128
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003129 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3130 // The driver forgets the correct scissor when modifying the FBO binding.
3131 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003132 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003133 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003134 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003135 }
3136
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003137 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003138}
3139
Adrienne Walker4ee88512018-05-17 11:37:14 -07003140void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3141 if (fboid == fBoundDrawFramebuffer &&
3142 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3143 // This workaround only applies to deleting currently bound framebuffers
3144 // on Adreno 420. Because this is a somewhat rare case, instead of
3145 // tracking all the attachments of every framebuffer instead just always
3146 // unbind all attachments.
3147 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3148 GR_GL_RENDERBUFFER, 0));
3149 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3150 GR_GL_RENDERBUFFER, 0));
3151 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3152 GR_GL_RENDERBUFFER, 0));
3153 }
3154
3155 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003156
3157 // Deleting the currently bound framebuffer rebinds to 0.
3158 if (fboid == fBoundDrawFramebuffer) {
3159 this->onFBOChanged();
3160 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003161}
3162
Greg Daniel46cfbc62019-06-07 11:43:30 -04003163bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003164 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003165 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3166 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3167 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003168 auto dstFormat = dst->backendFormat().asGLFormat();
3169 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003170 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003171 return true;
3172 }
3173 }
cblume61214052016-01-26 09:10:48 -08003174
Greg Daniel46cfbc62019-06-07 11:43:30 -04003175 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3176 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003177 return true;
3178 }
3179
Greg Daniel46cfbc62019-06-07 11:43:30 -04003180 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3181 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003182 }
3183
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003184 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003185 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003186 return true;
3187 }
bsalomon083617b2016-02-12 12:10:14 -08003188 }
3189
bsalomon6df86402015-06-01 10:41:49 -07003190 return false;
3191}
3192
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003193bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003194 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003195
3196 int progIdx = TextureToCopyProgramIdx(srcTex);
3197 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3198 GrSLType samplerType =
3199 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3200
3201 if (!fCopyProgramArrayBuffer) {
3202 static const GrGLfloat vdata[] = {
3203 0, 0,
3204 0, 1,
3205 1, 0,
3206 1, 1
3207 };
3208 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3209 kStatic_GrAccessPattern, vdata);
3210 }
3211 if (!fCopyProgramArrayBuffer) {
3212 return false;
3213 }
3214
3215 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3216 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3217 if (!fCopyPrograms[progIdx].fProgram) {
3218 return false;
3219 }
3220
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003221 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3222 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3223 GrShaderVar::kUniform_TypeModifier);
3224 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3225 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3226 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3227 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3228
Greg Daniel9e3169b2019-06-06 14:38:17 -04003229 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003230 if (shaderCaps->noperspectiveInterpolationSupport()) {
3231 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3232 vshaderTxt.appendf("#extension %s : require\n", extension);
3233 }
3234 vTexCoord.addModifier("noperspective");
3235 }
3236
3237 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3238 vshaderTxt.append(";");
3239 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3240 vshaderTxt.append(";");
3241 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3242 vshaderTxt.append(";");
3243 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3244 vshaderTxt.append(";");
3245
3246 vshaderTxt.append(
3247 "// Copy Program VS\n"
3248 "void main() {"
3249 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3250 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3251 " sk_Position.zw = half2(0, 1);"
3252 "}"
3253 );
3254
Greg Daniel9e3169b2019-06-06 14:38:17 -04003255 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003256 if (shaderCaps->noperspectiveInterpolationSupport()) {
3257 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3258 fshaderTxt.appendf("#extension %s : require\n", extension);
3259 }
3260 }
3261 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3262 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3263 fshaderTxt.append(";");
3264 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3265 fshaderTxt.append(";");
3266 fshaderTxt.appendf(
3267 "// Copy Program FS\n"
3268 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003269 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003270 "}"
3271 );
3272
3273 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3274 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3275 SkSL::Program::Settings settings;
3276 settings.fCaps = shaderCaps;
3277 SkSL::String glsl;
3278 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3279 sksl, settings, &glsl, errorHandler);
3280 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3281 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3282 SkASSERT(program->fInputs.isEmpty());
3283
3284 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3285 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3286 errorHandler);
3287 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3288 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3289 errorHandler);
3290 SkASSERT(program->fInputs.isEmpty());
3291
3292 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3293
3294 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3295 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3296 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3297 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3298 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3299 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3300
3301 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3302
3303 GL_CALL(DeleteShader(vshader));
3304 GL_CALL(DeleteShader(fshader));
3305
3306 return true;
3307}
3308
brianosman33f6b3f2016-06-02 05:49:21 -07003309bool GrGLGpu::createMipmapProgram(int progIdx) {
3310 const bool oddWidth = SkToBool(progIdx & 0x2);
3311 const bool oddHeight = SkToBool(progIdx & 0x1);
3312 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3313
Brian Salomon1edc5b92016-11-29 13:43:46 -05003314 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003315
3316 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3317 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3318 if (!fMipmapPrograms[progIdx].fProgram) {
3319 return false;
3320 }
3321
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003322 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3323 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003324 GrShaderVar::kUniform_TypeModifier);
3325 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3326 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003327 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003328 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003329 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3330 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3331 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3332 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003333 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003334 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003335
Ethan Nicholasfc994162019-06-06 10:04:27 -04003336 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003337 if (shaderCaps->noperspectiveInterpolationSupport()) {
3338 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003339 vshaderTxt.appendf("#extension %s : require\n", extension);
3340 }
3341 vTexCoords[0].addModifier("noperspective");
3342 vTexCoords[1].addModifier("noperspective");
3343 vTexCoords[2].addModifier("noperspective");
3344 vTexCoords[3].addModifier("noperspective");
3345 }
3346
Brian Salomon1edc5b92016-11-29 13:43:46 -05003347 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003348 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003349 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003350 vshaderTxt.append(";");
3351 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003352 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003353 vshaderTxt.append(";");
3354 }
3355
3356 vshaderTxt.append(
3357 "// Mipmap Program VS\n"
3358 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003359 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3360 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003361 );
3362
3363 // Insert texture coordinate computation:
3364 if (oddWidth && oddHeight) {
3365 vshaderTxt.append(
3366 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003367 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3368 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003369 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3370 );
3371 } else if (oddWidth) {
3372 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003373 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3374 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003375 );
3376 } else if (oddHeight) {
3377 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003378 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3379 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003380 );
3381 } else {
3382 vshaderTxt.append(
3383 " v_texCoord0 = a_vertex.xy;"
3384 );
3385 }
3386
3387 vshaderTxt.append("}");
3388
Ethan Nicholasfc994162019-06-06 10:04:27 -04003389 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003390 if (shaderCaps->noperspectiveInterpolationSupport()) {
3391 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003392 fshaderTxt.appendf("#extension %s : require\n", extension);
3393 }
3394 }
brianosman33f6b3f2016-06-02 05:49:21 -07003395 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003396 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003397 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003398 fshaderTxt.append(";");
3399 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003400 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003401 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003402 fshaderTxt.append(
3403 "// Mipmap Program FS\n"
3404 "void main() {"
3405 );
3406
3407 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003408 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003409 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3410 " sample(u_texture, v_texCoord1) + "
3411 " sample(u_texture, v_texCoord2) + "
3412 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003413 );
3414 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003415 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003416 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3417 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003418 );
3419 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003420 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003421 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003422 );
3423 }
3424
3425 fshaderTxt.append("}");
3426
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003427 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003428 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003429 SkSL::Program::Settings settings;
3430 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003431 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003432 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003433 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003434 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003435 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003436 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003437
Brian Osman6c431d52019-04-15 16:31:54 -04003438 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003439 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3440 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003441 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003442 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003443 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003444 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003445
3446 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3447
3448 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3449 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3450 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3451 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3452
3453 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3454
3455 GL_CALL(DeleteShader(vshader));
3456 GL_CALL(DeleteShader(fshader));
3457
3458 return true;
3459}
3460
Greg Daniel46cfbc62019-06-07 11:43:30 -04003461bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003462 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003463 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3464 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3465 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3466 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003467 return false;
3468 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003469 int progIdx = TextureToCopyProgramIdx(srcTex);
3470 if (!dstRT) {
3471 SkASSERT(dstTex);
3472 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3473 return false;
3474 }
3475 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003476 if (!fCopyPrograms[progIdx].fProgram) {
3477 if (!this->createCopyProgram(srcTex)) {
3478 SkDebugf("Failed to create copy program.\n");
3479 return false;
3480 }
3481 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003482 int w = srcRect.width();
3483 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003484 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003485 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003486 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003487 this->flushViewport(dst->width(), dst->height());
3488 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003489 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003490 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003491 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003492 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3493 attribs->enableVertexArrays(this, 1);
3494 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3495 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003496 // dst rect edges in NDC (-1 to 1)
3497 int dw = dst->width();
3498 int dh = dst->height();
3499 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3500 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3501 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3502 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003503 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3504 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3505 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3506 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3507 int sw = src->width();
3508 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003509 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3510 // src rect edges in normalized texture space (0 to 1)
3511 sx0 /= sw;
3512 sx1 /= sw;
3513 sy0 /= sh;
3514 sy1 /= sh;
3515 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003516 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3517 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3518 sx1 - sx0, sy1 - sy0, sx0, sy0));
3519 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003520 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003521 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003522 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003523 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003524 this->disableScissor();
3525 this->disableWindowRectangles();
3526 this->disableStencil();
3527 if (this->glCaps().srgbWriteControl()) {
3528 this->flushFramebufferSRGB(true);
3529 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003530 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003531 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003532 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3533 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003534 return true;
3535}
3536
Greg Daniel46cfbc62019-06-07 11:43:30 -04003537void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003538 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003539 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003540 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003541 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003542 SkASSERT(dstTex);
3543 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003544 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003545
Brian Salomond978b902019-02-07 15:09:18 -05003546 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003547 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003548 dstPoint.fX, dstPoint.fY,
3549 srcRect.fLeft, srcRect.fTop,
3550 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003551 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003552 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3553 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003554 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3555 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003556}
3557
Greg Daniel46cfbc62019-06-07 11:43:30 -04003558bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003559 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003560 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003561 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3562 srcRect.width(), srcRect.height());
3563 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003564 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003565 return false;
mtklein404b3b22015-05-18 09:29:10 -07003566 }
bsalomon5df6fee2015-05-18 06:26:15 -07003567 }
bsalomon6df86402015-06-01 10:41:49 -07003568
Brian Salomond2a8ae22019-09-10 16:03:59 -04003569 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3570 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003571 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003572 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003573
3574 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003575 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003576 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003577
Greg Daniel46cfbc62019-06-07 11:43:30 -04003578 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3579 srcRect.fTop,
3580 srcRect.fRight,
3581 srcRect.fBottom,
3582 dstRect.fLeft,
3583 dstRect.fTop,
3584 dstRect.fRight,
3585 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003586 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003587 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3588 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003589
3590 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3591 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003592 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003593}
3594
Brian Salomon930f9392018-06-20 16:25:26 -04003595bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3596 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003597 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003598 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003599 return false;
3600 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003601 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003602 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3603 // Uses draw calls to do a series of downsample operations to successive mips.
3604
3605 // The manual approach requires the ability to limit which level we're sampling and that the
3606 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003607 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003608 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003609 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003610 GL_CALL(GenerateMipmap(glTex->target()));
3611 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003612 }
3613
brianosman33f6b3f2016-06-02 05:49:21 -07003614 int width = texture->width();
3615 int height = texture->height();
3616 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003617 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003618
3619 // Create (if necessary), then bind temporary FBO:
3620 if (0 == fTempDstFBOID) {
3621 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3622 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003623 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003624 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003625
3626 // Bind the texture, to get things configured for filtering.
3627 // We'll be changing our base level further below:
3628 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003629 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003630 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003631
3632 // Vertex data:
3633 if (!fMipmapProgramArrayBuffer) {
3634 static const GrGLfloat vdata[] = {
3635 0, 0,
3636 0, 1,
3637 1, 0,
3638 1, 1
3639 };
Brian Salomonae64c192019-02-05 09:41:37 -05003640 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003641 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003642 }
3643 if (!fMipmapProgramArrayBuffer) {
3644 return false;
3645 }
3646
3647 fHWVertexArrayState.setVertexArrayID(this, 0);
3648
3649 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003650 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003651 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003652 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003653
3654 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003655 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003656 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003657 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003658 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003659 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003660
3661 // Do all the blits:
3662 width = texture->width();
3663 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003664
brianosman33f6b3f2016-06-02 05:49:21 -07003665 for (GrGLint level = 1; level < levelCount; ++level) {
3666 // Get and bind the program for this particular downsample (filter shape can vary):
3667 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3668 if (!fMipmapPrograms[progIdx].fProgram) {
3669 if (!this->createMipmapProgram(progIdx)) {
3670 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003671 // Invalidate all params to cover base level change in a previous iteration.
3672 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003673 return false;
3674 }
3675 }
Brian Salomon802cb312018-06-08 18:05:20 -04003676 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003677
3678 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3679 const float invWidth = 1.0f / width;
3680 const float invHeight = 1.0f / height;
3681 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3682 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3683 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3684
3685 // Only sample from previous mip
3686 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3687
Brian Salomon930f9392018-06-20 16:25:26 -04003688 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3689 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003690
Brian Osman788b9162020-02-07 10:36:46 -05003691 width = std::max(1, width / 2);
3692 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003693 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003694
3695 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3696 }
3697
3698 // Unbind:
3699 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3700 GR_GL_TEXTURE_2D, 0, 0));
3701
Brian Salomon930f9392018-06-20 16:25:26 -04003702 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003703 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3704 // We drew the 2nd to last level into the last level.
3705 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3706 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003707
brianosman33f6b3f2016-06-02 05:49:21 -07003708 return true;
3709}
3710
Chris Daltond7291ba2019-03-07 14:17:03 -07003711void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003712 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3713 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003714
3715 int effectiveSampleCnt;
3716 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003717 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003718
3719 sampleLocations->reset(effectiveSampleCnt);
3720 for (int i = 0; i < effectiveSampleCnt; ++i) {
3721 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3722 }
3723}
3724
cdalton231c5fd2015-05-13 12:35:36 -07003725void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003726 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003727 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003728 case kTexture_GrXferBarrierType: {
3729 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003730 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003731 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3732 // The render target uses separate storage so no need for glTextureBarrier.
3733 // FIXME: The render target will resolve automatically when its texture is bound,
3734 // but we could resolve only the bounds that will be read if we do it here instead.
3735 return;
3736 }
cdalton9954bc32015-04-29 14:17:00 -07003737 SkASSERT(this->caps()->textureBarrierSupport());
3738 GL_CALL(TextureBarrier());
3739 return;
cdalton231c5fd2015-05-13 12:35:36 -07003740 }
cdalton8917d622015-05-06 13:40:21 -07003741 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003742 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003743 this->caps()->blendEquationSupport());
3744 GL_CALL(BlendBarrier());
3745 return;
bsalomoncb02b382015-08-12 11:14:50 -07003746 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003747 }
3748}
3749
Chris Daltone1196c52019-12-28 14:31:09 -07003750void GrGLGpu::insertManualFramebufferBarrier() {
3751 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3752 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3753}
3754
Brian Salomon85c3d682019-11-04 15:04:54 -05003755GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003756 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003757 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003758 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003759 GrProtected isProtected,
3760 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003761 // We don't support protected textures in GL.
3762 if (isProtected == GrProtected::kYes) {
3763 return {};
3764 }
3765
Brian Salomon8a375832018-03-14 10:21:40 -04003766 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003767
Brian Salomond4764a12019-08-08 12:08:24 -04003768 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003769 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003770 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003771 }
3772
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003773 int numMipLevels = 1;
3774 if (mipMapped == GrMipMapped::kYes) {
3775 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3776 }
3777
Robert Phillipsd34691b2019-09-24 13:38:43 -04003778 // Compressed formats go through onCreateCompressedBackendTexture
3779 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3780
Greg Daniel15500642019-06-27 03:05:55 +00003781 GrGLTextureInfo info;
3782 GrGLTextureParameters::SamplerOverriddenState initialState;
3783
Greg Danield51fa2f2020-01-22 16:53:38 -05003784 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003785 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003786 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003787
Robert Phillipsd34691b2019-09-24 13:38:43 -04003788 info.fTarget = GR_GL_TEXTURE_2D;
3789 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003790 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003791 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003792 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003793 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003794
Robert Phillips42716d42019-12-16 12:19:54 -05003795 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003796 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3797 SkTDArray<GrMipLevel> texels;
3798 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003799 texels.append(numMipLevels);
3800 for (int i = 0; i < numMipLevels; ++i) {
3801 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3802 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003803 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3804 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3805 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003806 GL_CALL(DeleteTextures(1, &info.fID));
3807 return {};
3808 }
3809 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3810 // TODO: Unify this with the clear texture code in onCreateTexture().
3811 GrColorType colorType;
3812 GrGLenum externalFormat, externalType;
3813 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3814 &externalType, &colorType);
3815 if (colorType == GrColorType::kUnknown) {
3816 GL_CALL(DeleteTextures(1, &info.fID));
3817 return {};
3818 }
3819
3820 // Make one tight image at the base size and reuse it for smaller levels.
3821 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3822 auto rb = ii.minRowBytes();
3823 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3824 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3825 GL_CALL(DeleteTextures(1, &info.fID));
3826 return {};
3827 }
3828
3829 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3830 SkISize levelDimensions = dimensions;
3831 for (int i = 0; i < numMipLevels; ++i) {
3832 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3833 levelDimensions.height(), externalFormat, externalType,
3834 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003835 levelDimensions = {std::max(1, levelDimensions.width() /2),
3836 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003837 }
3838 }
3839 // Unbind this texture from the scratch texture unit.
3840 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003841
Brian Salomone2826ab2019-06-04 15:58:31 -04003842 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003843 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003844 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3845 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003846
Brian Salomon85c3d682019-11-04 15:04:54 -05003847 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3848 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003849}
3850
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003851void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003852 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3853
3854 GrGLTextureInfo info;
3855 if (tex.getGLTextureInfo(&info)) {
3856 GL_CALL(DeleteTextures(1, &info.fID));
3857 }
3858}
3859
3860#if GR_TEST_UTILS
3861
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003862bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003863 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003864
Greg Daniel52e16d92018-04-10 09:34:07 -04003865 GrGLTextureInfo info;
3866 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003867 return false;
3868 }
3869
3870 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003871 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003872
3873 return (GR_GL_TRUE == result);
3874}
3875
Brian Salomonf865b052018-03-09 09:01:53 -05003876GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003877 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003878 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3879 return GrBackendRenderTarget(); // invalid
3880 }
Brian Salomon8a375832018-03-14 10:21:40 -04003881 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003882 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003883 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003884 return {};
3885 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003886 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003887 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003888 if (sFormatIdx < 0) {
3889 return {};
3890 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003891 GrGLuint colorID = 0;
3892 GrGLuint stencilID = 0;
3893 auto deleteIDs = [&] {
3894 if (colorID) {
3895 if (useTexture) {
3896 GL_CALL(DeleteTextures(1, &colorID));
3897 } else {
3898 GL_CALL(DeleteRenderbuffers(1, &colorID));
3899 }
Brian Salomonf865b052018-03-09 09:01:53 -05003900 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003901 if (stencilID) {
3902 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003903 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003904 };
3905
3906 if (useTexture) {
3907 GL_CALL(GenTextures(1, &colorID));
3908 } else {
3909 GL_CALL(GenRenderbuffers(1, &colorID));
3910 }
3911 GL_CALL(GenRenderbuffers(1, &stencilID));
3912 if (!stencilID || !colorID) {
3913 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003914 return {};
3915 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003916
Brian Salomonf865b052018-03-09 09:01:53 -05003917 GrGLFramebufferInfo info;
3918 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003919 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003920 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3921 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003922 deleteIDs();
3923 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003924 }
3925
3926 this->invalidateBoundRenderTarget();
3927
Adrienne Walker4ee88512018-05-17 11:37:14 -07003928 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003929 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003930 GrGLTextureParameters::SamplerOverriddenState initialState;
3931 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3932 if (!colorID) {
3933 deleteIDs();
3934 return {};
3935 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003936 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3937 colorID, 0));
3938 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003939 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003940 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3941 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003942 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003943 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3944 GR_GL_RENDERBUFFER, colorID));
3945 }
3946 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003947 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3948 GL_ALLOC_CALL(this->glInterface(),
3949 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3950 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003951 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003952 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3953 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003954 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003955 }
3956
Brian Salomon93348dd2018-08-29 12:56:23 -04003957 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3958 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3959 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3960 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003961 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003962 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003963
Adrienne Walker4ee88512018-05-17 11:37:14 -07003964 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003965 GrGLenum status;
3966 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3967 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003968 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003969 return {};
3970 }
3971 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003972
Greg Daniel108bb232018-07-03 16:18:29 -04003973 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003974 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003975 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003976}
3977
3978void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003979 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003980 GrGLFramebufferInfo info;
3981 if (backendRT.getGLFramebufferInfo(&info)) {
3982 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003983 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003984 }
3985 }
joshualitt8fd844f2015-12-02 13:36:47 -08003986}
3987
Greg Daniel26b50a42018-03-08 09:49:58 -05003988void GrGLGpu::testingOnly_flushGpuAndSync() {
3989 GL_CALL(Finish());
3990}
Brian Salomonf865b052018-03-09 09:01:53 -05003991#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003992
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003993///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003994
cdaltone2e71c22016-04-07 18:13:29 -07003995GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003996 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003997 GrGLAttribArrayState* attribState;
3998
cdaltone2e71c22016-04-07 18:13:29 -07003999 if (gpu->glCaps().isCoreProfile()) {
4000 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004001 GrGLuint arrayID;
4002 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4003 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004004 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004005 }
cdaltone2e71c22016-04-07 18:13:29 -07004006 if (ibuf) {
4007 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004008 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004009 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004010 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004011 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004012 if (ibuf) {
4013 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004014 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004015 } else {
4016 this->setVertexArrayID(gpu, 0);
4017 }
4018 int attrCount = gpu->glCaps().maxVertexAttributes();
4019 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4020 fDefaultVertexArrayAttribState.resize(attrCount);
4021 }
4022 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004023 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004024 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004025}
bsalomone179a912016-01-20 06:18:10 -08004026
Greg Daniel30a35e82019-11-19 14:12:25 -05004027bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004028 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004029 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004030 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004031 // We call finish if the client told us to sync or if we have a finished proc but don't support
4032 // GLsync objects.
4033 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4034 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4035 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004036 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004037 // After a finish everything previously sent to GL is done.
4038 for (const auto& cb : fFinishCallbacks) {
4039 cb.fCallback(cb.fContext);
4040 this->deleteSync(cb.fSync);
4041 }
4042 fFinishCallbacks.clear();
4043 if (info.fFinishedProc) {
4044 info.fFinishedProc(info.fFinishedContext);
4045 }
4046 } else {
4047 if (info.fFinishedProc) {
4048 FinishCallback callback;
4049 callback.fCallback = info.fFinishedProc;
4050 callback.fContext = info.fFinishedContext;
4051 callback.fSync = (GrGLsync)this->insertFence();
4052 fFinishCallbacks.push_back(callback);
4053 GL_CALL(Flush());
4054 } else if (insertedSemaphore) {
4055 // Must call flush after semaphores in case they are waited on another GL context.
4056 GL_CALL(Flush());
4057 }
4058 // See if any previously inserted finish procs are good to go.
4059 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004060 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004061 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004062}
4063
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004064void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4065 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4066 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4067 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004068}
4069
Greg Daniel6be35232017-03-01 17:01:09 -05004070GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004071 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004072 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004073 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4074 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4075 GrGLuint fence = 0;
4076 GL_CALL(GenFences(1, &fence));
4077 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4078 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4079 } else {
4080 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4081 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004082 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004083 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004084}
4085
Brian Salomonb0d8b762019-05-06 16:58:22 -04004086bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004087 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4088 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4089 if (!timeout) {
4090 if (flush) {
4091 GL_CALL(Flush);
4092 }
4093 GrGLboolean result;
4094 GL_CALL_RET(result, TestFence(nvFence));
4095 return result == GR_GL_TRUE;
4096 }
4097 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4098 // If this really becomes necessary we could poll TestFence().
4099 // FinishFence always flushes so no need to check flush param.
4100 GL_CALL(FinishFence(nvFence));
4101 return true;
4102 } else {
4103 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4104 GrGLenum result;
4105 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4106 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4107 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004108}
4109
4110bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4111 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004112}
4113
4114void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004115 this->deleteSync((GrGLsync)fence);
4116}
4117
Greg Daniel301015c2019-11-18 14:06:46 -05004118std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004119 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004120 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004121}
4122
Greg Daniel301015c2019-11-18 14:06:46 -05004123std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4124 const GrBackendSemaphore& semaphore,
4125 GrResourceProvider::SemaphoreWrapType wrapType,
4126 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004127 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004128 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4129}
4130
Greg Daniel301015c2019-11-18 14:06:46 -05004131void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4132 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004133
Greg Daniel48661b82018-01-22 16:11:35 -05004134 GrGLsync sync;
4135 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4136 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004137}
4138
Greg Daniel301015c2019-11-18 14:06:46 -05004139void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4140 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004141
4142 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4143}
4144
Brian Salomonb0d8b762019-05-06 16:58:22 -04004145void GrGLGpu::checkFinishProcs() {
4146 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4147 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4148 /* timeout = */ 0, /* flush = */ false)) {
4149 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4150 this->deleteSync(fFinishCallbacks.front().fSync);
4151 fFinishCallbacks.pop_front();
4152 }
4153}
4154
Greg Daniel6be35232017-03-01 17:01:09 -05004155void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004156 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4157 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4158 GL_CALL(DeleteFences(1, &nvFence));
4159 } else {
4160 GL_CALL(DeleteSync(sync));
4161 }
jvanverth84741b32016-09-30 08:39:02 -07004162}
Brian Osman13dddce2017-05-09 13:19:50 -04004163
Greg Daniel301015c2019-11-18 14:06:46 -05004164std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004165 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004166 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004167 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004168 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004169 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4170 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004171
4172 return semaphore;
4173}
Robert Phillips646e4292017-06-13 12:44:56 -04004174
4175int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004176 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004177 case kTexture2DSampler_GrSLType:
4178 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004179 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004180 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004181 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004182 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004183 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004184 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004185 }
4186}
Brian Osman71a18892017-08-10 10:23:25 -04004187
Kevin Lubickf4def342018-10-04 12:52:50 -04004188#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004189#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004190void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4191 // We are called by the base class, which has already called beginObject(). We choose to nest
4192 // all of our caps information in a named sub-object.
4193 writer->beginObject("GL GPU");
4194
4195 const GrGLubyte* str;
4196 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4197 writer->appendString("GL_VERSION", (const char*)(str));
4198 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4199 writer->appendString("GL_RENDERER", (const char*)(str));
4200 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4201 writer->appendString("GL_VENDOR", (const char*)(str));
4202 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4203 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4204
4205 writer->appendName("extensions");
4206 glInterface()->fExtensions.dumpJSON(writer);
4207
4208 writer->endObject();
4209}
Kevin Lubickf4def342018-10-04 12:52:50 -04004210#endif