blob: c7092a2d0b736555524f9e313f451761de05664d [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());
Brian Salomond818ebf2018-07-02 14:08:49 +00001756 // if the scissor fully contains the viewport then we fall through and
1757 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001758 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001759 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001760 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001761 fHWScissorSettings.fRect = scissor;
1762 }
1763 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1764 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1765 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001766 }
1767 return;
1768 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001770
1771 // See fall through note above
1772 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001773}
1774
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001775void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001776 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001777#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001778 typedef GrWindowRectsState::Mode Mode;
1779 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1780 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001781
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001782 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001783 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001784 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001785 }
1786
csmartdalton7535f412016-08-23 06:51:00 -07001787 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1788 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001789 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001791
Chris Daltond6cda8d2019-09-05 02:30:04 -06001792 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001794 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001795 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001796 }
1797
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001798 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001799 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001800
Greg Danielacd66b42019-05-22 16:29:12 -04001801 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001803}
1804
1805void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001806#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001807 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001808 return;
1809 }
1810 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001811 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001812#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001813}
1814
Robert Phillipsfcaae482019-11-07 10:17:03 -05001815bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001816
Stephen Whiteb1857852020-02-07 15:33:23 +00001817 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001818 if (!program) {
1819 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1820 return false;
1821 }
brianosman33f6b3f2016-06-02 05:49:21 -07001822
Brian Salomon802cb312018-06-08 18:05:20 -04001823 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001824
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001825 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001826 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1827 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001828
Robert Phillips901aff02019-10-08 12:32:56 -04001829 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001830
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001831 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001832 GrStencilSettings stencil;
1833 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001834 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1835 stencil.reset(*programInfo.pipeline().getUserStencil(),
1836 programInfo.pipeline().hasStencilClip(),
1837 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001838 }
Robert Phillips901aff02019-10-08 12:32:56 -04001839 this->flushStencil(stencil, programInfo.origin());
1840 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001841 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001842 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1843 : kBogusScissor);
1844 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001845 } else {
1846 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001847 }
Robert Phillips901aff02019-10-08 12:32:56 -04001848 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1849 glRT, programInfo.origin());
1850 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001851 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001852 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001853
1854 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001855 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001856 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001857
1858 return true;
1859}
1860
Brian Salomon802cb312018-06-08 18:05:20 -04001861void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1862 if (!program) {
1863 fHWProgram.reset();
1864 fHWProgramID = 0;
1865 return;
1866 }
1867 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1868 if (program == fHWProgram) {
1869 return;
1870 }
1871 auto id = program->programID();
1872 SkASSERT(id);
1873 GL_CALL(UseProgram(id));
1874 fHWProgram = std::move(program);
1875 fHWProgramID = id;
1876}
1877
1878void GrGLGpu::flushProgram(GrGLuint id) {
1879 SkASSERT(id);
1880 if (fHWProgramID == id) {
1881 SkASSERT(!fHWProgram);
1882 return;
1883 }
1884 fHWProgram.reset();
1885 GL_CALL(UseProgram(id));
1886 fHWProgramID = id;
1887}
1888
1889void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001890 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001891 int baseVertex,
1892 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001893 int baseInstance,
1894 GrPrimitiveRestart enablePrimitiveRestart) {
1895 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001896
cdaltone2e71c22016-04-07 18:13:29 -07001897 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001898 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001899 SkASSERT(indexBuffer->isCpuBuffer() ||
1900 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001901 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001902 } else {
1903 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001904 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001905
Brian Salomon92be2f72018-06-19 14:33:47 -04001906 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1907 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001908
Brian Salomon802cb312018-06-08 18:05:20 -04001909 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001910 SkASSERT(vertexBuffer);
1911 SkASSERT(vertexBuffer->isCpuBuffer() ||
1912 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1913 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001914 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1915 const auto& attrib = fHWProgram->vertexAttribute(i);
1916 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001917 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1918 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001919 }
Chris Dalton1d616352017-05-31 12:51:23 -06001920 }
Brian Salomon802cb312018-06-08 18:05:20 -04001921 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001922 SkASSERT(instanceBuffer);
1923 SkASSERT(instanceBuffer->isCpuBuffer() ||
1924 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1925 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001926 int attribIdx = fHWProgram->numVertexAttributes();
1927 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1928 const auto& attrib = fHWProgram->instanceAttribute(i);
1929 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001930 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1931 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1932 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001933 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001934 }
1935}
1936
Brian Salomonae64c192019-02-05 09:41:37 -05001937GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001938 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001939
cdaltone2e71c22016-04-07 18:13:29 -07001940 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001941 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001942 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001943 }
cdaltone2e71c22016-04-07 18:13:29 -07001944
Brian Salomonae64c192019-02-05 09:41:37 -05001945 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001946 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001947 if (!bufferState->fBufferZeroKnownBound) {
1948 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1949 bufferState->fBufferZeroKnownBound = true;
1950 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001951 }
Brian Salomondbf70722019-02-07 11:31:24 -05001952 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1953 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001954 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1955 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1956 bufferState->fBufferZeroKnownBound = false;
1957 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001958 }
1959
Brian Salomonae64c192019-02-05 09:41:37 -05001960 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001961}
Brian Salomond818ebf2018-07-02 14:08:49 +00001962void GrGLGpu::disableScissor() {
1963 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1964 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1965 fHWScissorSettings.fEnabled = kNo_TriState;
1966 return;
1967 }
1968}
1969
Brian Osman9a9baae2018-11-05 15:06:26 -05001970void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001971 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001972 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001973 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001974 SkASSERT(!this->caps()->performColorClearsAsDraws());
1975 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001976
Brian Salomon43f8bf02017-10-18 08:33:29 -04001977 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001978
Brian Salomon43f8bf02017-10-18 08:33:29 -04001979 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1980
Brian Salomond818ebf2018-07-02 14:08:49 +00001981 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001982 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001983 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001984 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001985 }
Greg Danielacd66b42019-05-22 16:29:12 -04001986 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001987 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001988 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001989 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001990 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001991}
1992
Robert Phillips95214472017-08-08 18:00:03 -04001993void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001994 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1995
Robert Phillips95214472017-08-08 18:00:03 -04001996 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001997 return;
1998 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001999
Chris Dalton674f77a2019-09-30 20:49:39 -06002000 // This should only be called internally when we know we have a stencil buffer.
2001 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002002
bsalomonb0bd4f62014-09-03 07:19:50 -07002003 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002004 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002005
Brian Salomond818ebf2018-07-02 14:08:49 +00002006 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002007 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002008
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002009 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002010 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002011 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002012 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002013}
2014
Chris Daltonb50cc812019-10-14 16:29:21 -06002015static bool use_tiled_rendering(const GrGLCaps& glCaps,
2016 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2017 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2018 // Otherwise it's faster to just not use it.
2019 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2020 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2021}
2022
2023void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002024 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2025 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2026 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2027
2028 this->handleDirtyContext();
2029
2030 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2031 this->flushRenderTarget(glRT);
2032 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2033
Chris Daltonb50cc812019-10-14 16:29:21 -06002034 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2035 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2036 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2037 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2038 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2039 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2040 nativeBounds.fHeight, preserveMask));
2041 }
2042
Chris Dalton674f77a2019-09-30 20:49:39 -06002043 GrGLbitfield clearMask = 0;
2044 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2045 SkASSERT(!this->caps()->performColorClearsAsDraws());
2046 this->flushClearColor(colorLoadStore.fClearColor);
2047 this->flushColorWrite(true);
2048 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002049 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002050 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2051 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2052 GL_CALL(StencilMask(0xffffffff));
2053 GL_CALL(ClearStencil(0));
2054 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2055 }
2056 if (clearMask) {
2057 this->disableScissor();
2058 this->disableWindowRectangles();
2059 GL_CALL(Clear(clearMask));
2060 }
2061}
2062
2063void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2064 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2065 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2066 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2067
2068 this->handleDirtyContext();
2069
2070 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2071 // The framebuffer binding changed in the middle of a command buffer. We should have already
2072 // printed a warning during onFBOChanged.
2073 return;
2074 }
2075
2076 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2077 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2078
2079 SkSTArray<2, GrGLenum> discardAttachments;
2080 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2081 discardAttachments.push_back(
2082 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2083 }
2084 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2085 discardAttachments.push_back(
2086 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2087 }
2088
2089 if (!discardAttachments.empty()) {
2090 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2091 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2092 discardAttachments.begin()));
2093 } else {
2094 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2095 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2096 discardAttachments.begin()));
2097 }
2098 }
2099 }
2100
Chris Daltonb50cc812019-10-14 16:29:21 -06002101 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2102 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2103 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2104 // Handled by use_tiled_rendering().
2105 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2106 GL_CALL(EndTiling(preserveMask));
2107 }
2108
Chris Dalton674f77a2019-09-30 20:49:39 -06002109 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002110}
2111
csmartdalton29df7602016-08-31 11:55:52 -07002112void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2113 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002114 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002115 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002116 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002117 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002118
egdaniel8dc7c3a2015-04-16 11:22:42 -07002119 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002120 if (!sb) {
2121 // We should only get here if we marked a proxy as requiring a SB. However,
2122 // the SB creation could later fail. Likely clipping is going to go awry now.
2123 return;
2124 }
2125
bsalomon6bc1b5f2015-02-23 09:06:38 -08002126 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002127#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002128 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002129 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002130#else
2131 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002132 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002133 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002134 // changing the clip between stencil passes may or may not
2135 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002136 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002137#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002138 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002139 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002140 value = (1 << (stencilBitCount - 1));
2141 } else {
2142 value = 0;
2143 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002144 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002145 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002146
Greg Danielacd66b42019-05-22 16:29:12 -04002147 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002148 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002149
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002150 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002151 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002153 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
Brian Salomone05ba5a2019-04-08 11:59:07 -04002156bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002157 GrColorType surfaceColorType, GrColorType dstColorType,
2158 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002159 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002160
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002161 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002162 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002163 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002164 return false;
2165 }
Greg Danielba88ab62019-07-26 09:14:01 -04002166 GrGLenum externalFormat = 0;
2167 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002168 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2169 surfaceColorType,
2170 dstColorType,
2171 &externalFormat,
2172 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002173 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002174 return false;
2175 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002176
Brian Salomon71d9d842016-11-03 13:42:00 -04002177 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002178 if (renderTarget->numSamples() <= 1 ||
2179 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2180 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2181 this->flushRenderTargetNoColorWrites(renderTarget);
2182 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2183 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2184 return false;
2185 } else {
2186 SkASSERT(renderTarget->requiresManualMSAAResolve());
2187 // we don't track the state of the READ FBO ID.
2188 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002189 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002190 } else {
2191 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002192 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002193 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 }
2195
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002196 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002197 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002198
Brian Salomona6948702018-06-01 15:33:20 -04002199 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002200 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002201 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002202 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002203 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002204 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002205
Brian Osman1348ed02018-09-12 09:08:39 -04002206 bool reattachStencil = false;
2207 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2208 renderTarget &&
2209 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002210 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002211 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2212 reattachStencil = true;
2213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2214 GR_GL_RENDERBUFFER, 0));
2215 }
2216
Chris Dalton76500e52019-09-05 02:13:05 -06002217 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002218 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002219
2220 if (reattachStencil) {
2221 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2222 renderTarget->renderTargetPriv().getStencilAttachment());
2223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2224 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2225 }
2226
Brian Salomon26de56e2019-04-10 12:14:26 -04002227 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002228 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002229 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2230 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002231
Brian Salomone05ba5a2019-04-08 11:59:07 -04002232 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002233 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002234 }
2235 return true;
2236}
2237
2238bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002239 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2240 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002241 SkASSERT(surface);
2242
Brian Salomonb28cb682019-07-26 12:48:47 -04002243 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002244
Brian Salomon26de56e2019-04-10 12:14:26 -04002245 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2246 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002247
Brian Salomon1047a492019-07-02 12:25:21 -04002248 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002249 rowPixelWidth = width;
2250 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002251 SkASSERT(!(rowBytes % bytesPerPixel));
2252 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002253 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002254 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2255 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002258GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002259 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002260 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002261 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002262 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002263 if (!fCachedOpsRenderPass) {
2264 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002265 }
2266
Chris Daltonb50cc812019-10-14 16:29:21 -06002267 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002268 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002269}
2270
Brian Salomon1fabd512018-02-09 09:54:25 -05002271void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002272 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002273 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002274 this->didWriteToSurface(target, origin, &bounds);
2275}
bsalomon6ba6fa12015-03-04 11:57:37 -08002276
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002277void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2278 this->flushRenderTargetNoColorWrites(target);
2279 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002280}
2281
Brian Osman9aa30c62018-07-02 15:21:46 -04002282void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002283 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002284 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002285 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002286 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002287#ifdef SK_DEBUG
2288 // don't do this check in Chromium -- this is causing
2289 // lots of repeated command buffer flushes when the compositor is
2290 // rendering with Ganesh, which is really slow; even too slow for
2291 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002292 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002293 GrGLenum status;
2294 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2295 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2296 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2297 }
bsalomon160f24c2015-03-17 15:55:42 -07002298 }
egdanield803f272015-03-18 13:01:52 -07002299#endif
2300 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002301 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002302 }
2303
brianosman35b784d2016-05-05 11:52:53 -07002304 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002305 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002306 }
Brian Salomon9b553272020-01-24 14:38:37 -05002307
2308 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2309 GrGLint format;
2310 GrGLint type;
2311 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2312 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2313 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2314 }
bsalomon083617b2016-02-12 12:10:14 -08002315}
bsalomona9909122016-01-23 10:41:40 -08002316
brianosman33f6b3f2016-06-02 05:49:21 -07002317void GrGLGpu::flushFramebufferSRGB(bool enable) {
2318 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2319 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2320 fHWSRGBFramebuffer = kYes_TriState;
2321 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2322 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2323 fHWSRGBFramebuffer = kNo_TriState;
2324 }
2325}
2326
Greg Danielacd66b42019-05-22 16:29:12 -04002327void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002328 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002329 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002330 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002331 fHWViewport = viewport;
2332 }
2333}
2334
bsalomon@google.comd302f142011-03-03 13:54:13 +00002335#define SWAP_PER_DRAW 0
2336
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002337#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002338 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002339 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002340 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002341 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002342 void SwapBuf() {
2343 DWORD procID = GetCurrentProcessId();
2344 HWND hwnd = GetTopWindow(GetDesktopWindow());
2345 while(hwnd) {
2346 DWORD wndProcID = 0;
2347 GetWindowThreadProcessId(hwnd, &wndProcID);
2348 if(wndProcID == procID) {
2349 SwapBuffers(GetDC(hwnd));
2350 }
2351 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2352 }
2353 }
2354 #endif
2355#endif
2356
Robert Phillips901aff02019-10-08 12:32:56 -04002357void GrGLGpu::draw(GrRenderTarget* renderTarget,
2358 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002359 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002360 int meshCount) {
2361 this->handleDirtyContext();
2362
Robert Phillips2579de42019-10-09 09:51:59 -04002363 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002364
Robert Phillipsfcaae482019-11-07 10:17:03 -05002365 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002366 return;
2367 }
ethannicholas22793252016-01-30 09:59:10 -08002368
Chris Dalton79f336d2020-02-11 14:42:56 -07002369 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
2370 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
2371 }
2372
Robert Phillips901aff02019-10-08 12:32:56 -04002373 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2374 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2375
Brian Salomonf7232642018-09-19 08:58:08 -04002376 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002377 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2378 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002379 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002380 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002381
Robert Phillips901aff02019-10-08 12:32:56 -04002382 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002383 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002384 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2385 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002386 }
Robert Phillips901aff02019-10-08 12:32:56 -04002387 if (hasDynamicPrimProcTextures) {
2388 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2389 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2390 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002391 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002392 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Chris Dalton79f336d2020-02-11 14:42:56 -07002393 GrIsPrimTypeLines(programInfo.primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002394 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2395 GL_CALL(Enable(GR_GL_CULL_FACE));
2396 GL_CALL(Disable(GR_GL_CULL_FACE));
2397 }
Chris Dalton79f336d2020-02-11 14:42:56 -07002398 meshes[m].sendToGpu(programInfo.primitiveType(), this);
2399 fLastPrimitiveType = programInfo.primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002400 }
ethannicholas22793252016-01-30 09:59:10 -08002401
bsalomon@google.comd302f142011-03-03 13:54:13 +00002402#if SWAP_PER_DRAW
2403 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002404 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002405 aglSwapBuffers(aglGetCurrentContext());
2406 int set_a_break_pt_here = 9;
2407 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002408 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002409 SwapBuf();
2410 int set_a_break_pt_here = 9;
2411 SwapBuf();
2412 #endif
2413#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002414}
2415
Chris Dalton3809bab2017-06-13 10:55:06 -06002416static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2417 switch (primitiveType) {
2418 case GrPrimitiveType::kTriangles:
2419 return GR_GL_TRIANGLES;
2420 case GrPrimitiveType::kTriangleStrip:
2421 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002422 case GrPrimitiveType::kPoints:
2423 return GR_GL_POINTS;
2424 case GrPrimitiveType::kLines:
2425 return GR_GL_LINES;
2426 case GrPrimitiveType::kLineStrip:
2427 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002428 case GrPrimitiveType::kPatches:
2429 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002430 case GrPrimitiveType::kPath:
2431 SK_ABORT("non-mesh-based GrPrimitiveType");
2432 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002433 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002434 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002435}
2436
Chris Dalton79f336d2020-02-11 14:42:56 -07002437void GrGLGpu::sendArrayMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh, int vertexCount,
2438 int baseVertex) {
2439 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002440 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002441 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2442 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002443 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2444 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002445 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002446 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2447 }
2448 fStats.incNumDraws();
2449}
2450
Brian Salomondbf70722019-02-07 11:31:24 -05002451static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2452 size_t baseOffset = baseIndex * sizeof(uint16_t);
2453 if (indexBuffer->isCpuBuffer()) {
2454 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2455 } else {
2456 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2457 }
2458}
2459
Chris Dalton79f336d2020-02-11 14:42:56 -07002460void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2461 int indexCount, int baseIndex, uint16_t minIndexValue,
2462 uint16_t maxIndexValue, int baseVertex) {
2463 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002464 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2465
2466 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2467 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002468
2469 if (this->glCaps().drawRangeElementsSupport()) {
2470 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002471 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002472 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002473 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002474 }
2475 fStats.incNumDraws();
2476}
2477
Chris Dalton79f336d2020-02-11 14:42:56 -07002478void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2479 int vertexCount, int baseVertex, int instanceCount,
2480 int baseInstance) {
2481 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002482 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002483 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002484 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2485 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002486 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
Brian Osman788b9162020-02-07 10:36:46 -05002487 std::min(instanceCount - i, maxInstances)));
Chris Daltoncc604e52017-10-06 16:27:32 -06002488 fStats.incNumDraws();
2489 }
Chris Dalton1d616352017-05-31 12:51:23 -06002490}
2491
Chris Dalton79f336d2020-02-11 14:42:56 -07002492void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrMesh& mesh,
2493 int indexCount, int baseIndex, int baseVertex,
2494 int instanceCount, int baseInstance) {
2495 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton34280e22019-12-20 11:08:25 -07002496 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002497 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2498 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002499 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2500 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002501 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Brian Osman788b9162020-02-07 10:36:46 -05002502 std::min(instanceCount - i, maxInstances)));
Chris Dalton1b4ad762018-10-04 11:58:09 -06002503 fStats.incNumDraws();
2504 }
Chris Dalton1d616352017-05-31 12:51:23 -06002505}
2506
Chris Dalton16a33c62019-09-24 22:19:17 -06002507void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
Greg Daniel242536f2020-02-13 14:12:46 -05002508 ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002509 // Some extensions automatically resolves the texture when it is read.
2510 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2511
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002512 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002513 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2514 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2515 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2516 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002517
Chris Daltonc139d082019-09-26 14:04:24 -06002518 // make sure we go through flushRenderTarget() since we've modified
2519 // the bound DRAW FBO ID.
2520 fHWBoundRenderTargetUniqueID.makeInvalid();
2521 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2522 // Apple's extension uses the scissor as the blit bounds.
2523 GrScissorState scissorState;
2524 scissorState.set(resolveRect);
Greg Daniel242536f2020-02-13 14:12:46 -05002525 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2526 // happens inside flushScissor since resolveRect is already in native device coordinates.
2527 this->flushScissor(scissorState, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002528 this->disableWindowRectangles();
2529 GL_CALL(ResolveMultisampleFramebuffer());
2530 } else {
2531 int l, b, r, t;
2532 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2533 this->glCaps().blitFramebufferSupportFlags()) {
2534 l = 0;
2535 b = 0;
2536 r = target->width();
2537 t = target->height();
2538 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002539 l = resolveRect.x();
2540 b = resolveRect.y();
2541 r = resolveRect.x() + resolveRect.width();
2542 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002543 }
Chris Daltonc139d082019-09-26 14:04:24 -06002544
2545 // BlitFrameBuffer respects the scissor, so disable it.
2546 this->disableScissor();
2547 this->disableWindowRectangles();
2548 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 +00002549 }
2550}
2551
bsalomon@google.com411dad02012-06-05 20:24:20 +00002552namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002553
bsalomon@google.com411dad02012-06-05 20:24:20 +00002554
2555GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002556 static const GrGLenum gTable[kGrStencilOpCount] = {
2557 GR_GL_KEEP, // kKeep
2558 GR_GL_ZERO, // kZero
2559 GR_GL_REPLACE, // kReplace
2560 GR_GL_INVERT, // kInvert
2561 GR_GL_INCR_WRAP, // kIncWrap
2562 GR_GL_DECR_WRAP, // kDecWrap
2563 GR_GL_INCR, // kIncClamp
2564 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002565 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002566 static_assert(0 == (int)GrStencilOp::kKeep);
2567 static_assert(1 == (int)GrStencilOp::kZero);
2568 static_assert(2 == (int)GrStencilOp::kReplace);
2569 static_assert(3 == (int)GrStencilOp::kInvert);
2570 static_assert(4 == (int)GrStencilOp::kIncWrap);
2571 static_assert(5 == (int)GrStencilOp::kDecWrap);
2572 static_assert(6 == (int)GrStencilOp::kIncClamp);
2573 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002574 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2575 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002576}
2577
2578void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002579 const GrStencilSettings::Face& face,
2580 GrGLenum glFace) {
2581 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2582 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2583 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002584
cdalton93a379b2016-05-11 13:58:08 -07002585 GrGLint ref = face.fRef;
2586 GrGLint mask = face.fTestMask;
2587 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002588
2589 if (GR_GL_FRONT_AND_BACK == glFace) {
2590 // we call the combined func just in case separate stencil is not
2591 // supported.
2592 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2593 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002594 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002595 } else {
2596 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2597 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002598 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002599 }
2600}
2601}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002602
Chris Dalton71713f62019-04-18 12:41:03 -06002603void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002604 if (stencilSettings.isDisabled()) {
2605 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002606 } else if (fHWStencilSettings != stencilSettings ||
2607 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002608 if (kYes_TriState != fHWStencilTestEnabled) {
2609 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002610
csmartdaltonc7d85332016-10-26 10:13:46 -07002611 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002612 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002613 if (!stencilSettings.isTwoSided()) {
2614 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2615 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002616 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002617 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2618 GR_GL_FRONT);
2619 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2620 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002621 }
joshualitta58fe352014-10-27 08:39:00 -07002622 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002623 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002624 }
2625}
2626
csmartdaltonc7d85332016-10-26 10:13:46 -07002627void GrGLGpu::disableStencil() {
2628 if (kNo_TriState != fHWStencilTestEnabled) {
2629 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002630
csmartdaltonc7d85332016-10-26 10:13:46 -07002631 fHWStencilTestEnabled = kNo_TriState;
2632 fHWStencilSettings.invalidate();
2633 }
2634}
2635
Chris Dalton4c56b032019-03-04 12:28:42 -07002636void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002637 // rt is only optional if useHWAA is false.
2638 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002639#ifdef SK_DEBUG
2640 if (useHWAA && rt->numSamples() <= 1) {
2641 SkASSERT(this->caps()->mixedSamplesSupport());
2642 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2643 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2644 }
2645#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002646
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002647 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002648 if (useHWAA) {
2649 if (kYes_TriState != fMSAAEnabled) {
2650 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2651 fMSAAEnabled = kYes_TriState;
2652 }
2653 } else {
2654 if (kNo_TriState != fMSAAEnabled) {
2655 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2656 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002657 }
2658 }
2659 }
2660}
2661
Chris Daltonce425af2019-12-16 10:39:03 -07002662void GrGLGpu::flushConservativeRasterState(bool enabled) {
2663 if (this->caps()->conservativeRasterSupport()) {
2664 if (enabled) {
2665 if (kYes_TriState != fHWConservativeRasterEnabled) {
2666 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2667 fHWConservativeRasterEnabled = kYes_TriState;
2668 }
2669 } else {
2670 if (kNo_TriState != fHWConservativeRasterEnabled) {
2671 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2672 fHWConservativeRasterEnabled = kNo_TriState;
2673 }
2674 }
2675 }
2676}
2677
Chris Dalton1215cda2019-12-17 21:44:04 -07002678void GrGLGpu::flushWireframeState(bool enabled) {
2679 if (this->caps()->wireframeSupport()) {
2680 if (this->caps()->wireframeMode() || enabled) {
2681 if (kYes_TriState != fHWWireframeEnabled) {
2682 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2683 fHWWireframeEnabled = kYes_TriState;
2684 }
2685 } else {
2686 if (kNo_TriState != fHWWireframeEnabled) {
2687 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2688 fHWWireframeEnabled = kNo_TriState;
2689 }
2690 }
2691 }
2692}
2693
Chris Daltonbbb3f642019-07-24 12:25:08 -04002694void GrGLGpu::flushBlendAndColorWrite(
2695 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2696 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2697 // We need to work around a driver bug by using a blend state that preserves the dst color,
2698 // rather than disabling color writes.
2699 GrXferProcessor::BlendInfo preserveDstBlend;
2700 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2701 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2702 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2703 return;
2704 }
bsalomonf7cc8772015-05-11 11:21:14 -07002705
cdalton8917d622015-05-06 13:40:21 -07002706 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002707 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2708 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002709
2710 // Any optimization to disable blending should have already been applied and
2711 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002712 bool blendOff =
2713 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2714 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2715 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002716
egdanielb414f252014-07-29 13:15:47 -07002717 if (blendOff) {
2718 if (kNo_TriState != fHWBlendState.fEnabled) {
2719 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002720
2721 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2722 // https://code.google.com/p/skia/issues/detail?id=3943
2723 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2724 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2725 SkASSERT(this->caps()->advancedBlendEquationSupport());
2726 // Set to any basic blending equation.
2727 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2728 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2729 fHWBlendState.fEquation = blend_equation;
2730 }
2731
egdanielb414f252014-07-29 13:15:47 -07002732 fHWBlendState.fEnabled = kNo_TriState;
2733 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002734 } else {
2735 if (kYes_TriState != fHWBlendState.fEnabled) {
2736 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002737
Chris Daltonbbb3f642019-07-24 12:25:08 -04002738 fHWBlendState.fEnabled = kYes_TriState;
2739 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002740
Chris Daltonbbb3f642019-07-24 12:25:08 -04002741 if (fHWBlendState.fEquation != equation) {
2742 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2743 fHWBlendState.fEquation = equation;
2744 }
cdalton8917d622015-05-06 13:40:21 -07002745
Chris Daltonbbb3f642019-07-24 12:25:08 -04002746 if (GrBlendEquationIsAdvanced(equation)) {
2747 SkASSERT(this->caps()->advancedBlendEquationSupport());
2748 // Advanced equations have no other blend state.
2749 return;
2750 }
cdalton8917d622015-05-06 13:40:21 -07002751
Chris Daltonbbb3f642019-07-24 12:25:08 -04002752 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2753 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2754 gXfermodeCoeff2Blend[dstCoeff]));
2755 fHWBlendState.fSrcCoeff = srcCoeff;
2756 fHWBlendState.fDstCoeff = dstCoeff;
2757 }
cdalton8917d622015-05-06 13:40:21 -07002758
Chris Daltonbbb3f642019-07-24 12:25:08 -04002759 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2760 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2761 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2762 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2763 fHWBlendState.fConstColor = blendConst;
2764 fHWBlendState.fConstColorValid = true;
2765 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002766 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002767 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002768
2769 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002770}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002771
Brian Salomondc829942018-10-23 16:07:24 -04002772static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002773 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002774 switch (swizzle[i]) {
2775 case 'r': glValues[i] = GR_GL_RED; break;
2776 case 'g': glValues[i] = GR_GL_GREEN; break;
2777 case 'b': glValues[i] = GR_GL_BLUE; break;
2778 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002779 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002780 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002781 default: SK_ABORT("Unsupported component");
2782 }
Brian Salomon327955b2018-10-22 15:39:22 +00002783 }
2784}
2785
Greg Daniel2c3398d2019-06-19 11:58:01 -04002786void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2787 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002788 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002789
reed856e9d92015-09-30 12:21:45 -07002790#ifdef SK_DEBUG
2791 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002792 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002793 const int w = texture->width();
2794 const int h = texture->height();
2795 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2796 }
2797 }
2798#endif
2799
Robert Phillips294870f2016-11-11 12:38:40 -05002800 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002801 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002802 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002803 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002804 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002805 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002806 }
2807
Brian Salomondc829942018-10-23 16:07:24 -04002808 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002809 if (!this->caps()->mipMapSupport() ||
2810 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002811 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002812 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002813 }
bsalomonefd7d452014-10-23 14:17:46 -07002814
brianosman33f6b3f2016-06-02 05:49:21 -07002815#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002816 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002817 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002818 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002819 }
2820#endif
2821
Brian Salomone2826ab2019-06-04 15:58:31 -04002822 auto timestamp = texture->parameters()->resetTimestamp();
2823 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002824
Brian Salomone2826ab2019-06-04 15:58:31 -04002825 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2826 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002827 if (fSamplerObjectCache) {
2828 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2829 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2831 texture->parameters()->samplerOverriddenState();
2832 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002833
Brian Salomone2826ab2019-06-04 15:58:31 -04002834 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2835 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002836
Brian Salomone2826ab2019-06-04 15:58:31 -04002837 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2838 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002839
2840 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002841 newSamplerState.fMinLOD = -1000.f;
2842 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002843
Brian Salomone2826ab2019-06-04 15:58:31 -04002844 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002845 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002847 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002849 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002850 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002851 }
Mike Klein1bccae52018-10-19 11:38:44 +00002852 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002853 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002854 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002856 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002858 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002859 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002860 }
2861 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002862 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002863 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002864 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002865 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002866 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002867 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002868 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002869 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002870 if (this->glCaps().clampToBorderSupport()) {
2871 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002872 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002873 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002874 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2875 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002876 }
2877 }
Brian Salomondc829942018-10-23 16:07:24 -04002878 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002879 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2880 newNonsamplerState.fBaseMipMapLevel = 0;
2881 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002882
Brian Salomone2826ab2019-06-04 15:58:31 -04002883 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2884 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002885 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002886 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2887 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002888 GrGLenum glValues[4];
2889 get_gl_swizzle_values(swizzle, glValues);
2890 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002891 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002892 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002893 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2894 reinterpret_cast<const GrGLint*>(glValues)));
2895 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002896 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2897 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2898 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2899 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2900 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002901 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002902 }
2903 }
Brian Salomondc829942018-10-23 16:07:24 -04002904 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002905 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2906 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2907 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002908 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002909 this->setTextureUnit(unitIdx);
2910 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002911 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002912 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002913 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002914 this->setTextureUnit(unitIdx);
2915 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002916 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002917 }
Brian Salomon327955b2018-10-22 15:39:22 +00002918 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002919 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2920 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002921}
2922
Brian Salomon1f05d452019-02-08 12:33:08 -05002923void GrGLGpu::onResetTextureBindings() {
2924 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2925 GR_GL_TEXTURE_EXTERNAL};
2926 for (int i = 0; i < this->numTextureUnits(); ++i) {
2927 this->setTextureUnit(i);
2928 for (auto target : kTargets) {
2929 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2930 GL_CALL(BindTexture(target, 0));
2931 }
2932 }
2933 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2934 }
2935}
2936
Chris Dalton5a2f9622019-12-27 14:56:38 -07002937void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2938 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2939 if (fHWPatchVertexCount != count) {
2940 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2941 fHWPatchVertexCount = count;
2942 }
2943}
2944
egdaniel080e6732014-12-22 07:35:52 -08002945void GrGLGpu::flushColorWrite(bool writeColor) {
2946 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002947 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002948 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2949 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002950 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002951 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002952 } else {
2953 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002954 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002955 fHWWriteToColor = kYes_TriState;
2956 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002957 }
bsalomon3e791242014-12-17 13:43:13 -08002958}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002959
Chris Dalton674f77a2019-09-30 20:49:39 -06002960void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2961 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2962 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2963 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2964 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2965 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2966 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2967 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002968 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2969 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2970 GL_CALL(ClearColor(r, g, b, a));
2971 fHWClearColor[0] = r;
2972 fHWClearColor[1] = g;
2973 fHWClearColor[2] = b;
2974 fHWClearColor[3] = a;
2975 }
2976}
2977
bsalomon861e1032014-12-16 07:33:49 -08002978void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002979 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002980 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002981 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002982 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002983 }
2984}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002985
Brian Salomond978b902019-02-07 15:09:18 -05002986void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002987 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002988 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002989 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2990 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2991 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002992 }
Brian Salomond978b902019-02-07 15:09:18 -05002993 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2994 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002995 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002996 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002997}
2998
Brian Salomone5e7eb12016-10-14 16:18:33 -04002999// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04003000static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3001 const GrSurface* src,
3002 const SkIRect& srcRect,
3003 const SkIPoint& dstPoint,
3004 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003005 int dstSampleCnt = 0;
3006 int srcSampleCnt = 0;
3007 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003008 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003009 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003010 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003011 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003012 }
3013 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3014 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3015
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003016 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3017 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3018
Brian Salomone5e7eb12016-10-14 16:18:33 -04003019 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003020 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003021
Greg Daniel46cfbc62019-06-07 11:43:30 -04003022 GrTextureType dstTexType;
3023 GrTextureType* dstTexTypePtr = nullptr;
3024 GrTextureType srcTexType;
3025 GrTextureType* srcTexTypePtr = nullptr;
3026 if (dstTex) {
3027 dstTexType = dstTex->texturePriv().textureType();
3028 dstTexTypePtr = &dstTexType;
3029 }
3030 if (srcTex) {
3031 srcTexType = srcTex->texturePriv().textureType();
3032 srcTexTypePtr = &srcTexType;
3033 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003034
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003035 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3036 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003037 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003038}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003039
Brian Osmana9c8a052018-01-19 10:31:56 -05003040static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3041 // A RT has a separate MSAA renderbuffer if:
3042 // 1) It's multisampled
3043 // 2) We're using an extension with separate MSAA renderbuffers
3044 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003045 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003046}
3047
Greg Daniel46cfbc62019-06-07 11:43:30 -04003048static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003049 const GrGLCaps& caps) {
3050
bsalomon@google.comeb851172013-04-15 13:51:00 +00003051 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003052 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003053 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003054 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003055
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003056 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3057 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3058
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003059 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3060 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3061
Greg Daniel46cfbc62019-06-07 11:43:30 -04003062 GrTextureType dstTexType;
3063 GrTextureType* dstTexTypePtr = nullptr;
3064 GrTextureType srcTexType;
3065 GrTextureType* srcTexTypePtr = nullptr;
3066 if (dstTex) {
3067 dstTexType = dstTex->texturePriv().textureType();
3068 dstTexTypePtr = &dstTexType;
3069 }
3070 if (srcTex) {
3071 srcTexType = srcTex->texturePriv().textureType();
3072 srcTexTypePtr = &srcTexType;
3073 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003074
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003075 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3076 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003077}
3078
Greg Danielacd66b42019-05-22 16:29:12 -04003079// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003080void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003081 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003082 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003083 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003084 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003085 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3086 GrGLuint texID = texture->textureID();
3087 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003088 GrGLuint* tempFBOID;
3089 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003090
egdanield803f272015-03-18 13:01:52 -07003091 if (0 == *tempFBOID) {
3092 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003093 }
3094
Adrienne Walker4ee88512018-05-17 11:37:14 -07003095 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003096 GR_GL_CALL(
3097 this->glInterface(),
3098 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3099 if (mipLevel == 0) {
3100 texture->baseLevelWasBoundToFBO();
3101 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003102 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003103 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003104 }
egdaniel0f5f9672015-02-03 11:10:51 -08003105}
3106
Brian Salomond2a8ae22019-09-10 16:03:59 -04003107void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003108 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003109 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003110 SkASSERT(surface->asTexture());
3111 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3112 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3113 GR_GL_COLOR_ATTACHMENT0,
3114 textureTarget,
3115 0,
3116 0));
3117 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003118}
3119
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003120void GrGLGpu::onFBOChanged() {
3121 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3122 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3123 GL_CALL(Flush());
3124 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003125#ifdef SK_DEBUG
3126 if (fIsExecutingCommandBuffer_DebugOnly) {
3127 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3128 "This will severely hurt performance.\n");
3129 }
3130#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003131}
3132
Adrienne Walker4ee88512018-05-17 11:37:14 -07003133void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3134 fStats.incRenderTargetBinds();
3135 GL_CALL(BindFramebuffer(target, fboid));
3136 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3137 fBoundDrawFramebuffer = fboid;
3138 }
3139
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003140 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3141 // The driver forgets the correct scissor when modifying the FBO binding.
3142 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003143 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003144 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003145 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003146 }
3147
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003148 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003149}
3150
Adrienne Walker4ee88512018-05-17 11:37:14 -07003151void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3152 if (fboid == fBoundDrawFramebuffer &&
3153 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3154 // This workaround only applies to deleting currently bound framebuffers
3155 // on Adreno 420. Because this is a somewhat rare case, instead of
3156 // tracking all the attachments of every framebuffer instead just always
3157 // unbind all attachments.
3158 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3159 GR_GL_RENDERBUFFER, 0));
3160 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3161 GR_GL_RENDERBUFFER, 0));
3162 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3163 GR_GL_RENDERBUFFER, 0));
3164 }
3165
3166 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003167
3168 // Deleting the currently bound framebuffer rebinds to 0.
3169 if (fboid == fBoundDrawFramebuffer) {
3170 this->onFBOChanged();
3171 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003172}
3173
Greg Daniel46cfbc62019-06-07 11:43:30 -04003174bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003175 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003176 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3177 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3178 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003179 auto dstFormat = dst->backendFormat().asGLFormat();
3180 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003181 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003182 return true;
3183 }
3184 }
cblume61214052016-01-26 09:10:48 -08003185
Greg Daniel46cfbc62019-06-07 11:43:30 -04003186 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3187 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003188 return true;
3189 }
3190
Greg Daniel46cfbc62019-06-07 11:43:30 -04003191 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3192 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003193 }
3194
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003195 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003196 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003197 return true;
3198 }
bsalomon083617b2016-02-12 12:10:14 -08003199 }
3200
bsalomon6df86402015-06-01 10:41:49 -07003201 return false;
3202}
3203
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003204bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003205 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003206
3207 int progIdx = TextureToCopyProgramIdx(srcTex);
3208 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3209 GrSLType samplerType =
3210 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3211
3212 if (!fCopyProgramArrayBuffer) {
3213 static const GrGLfloat vdata[] = {
3214 0, 0,
3215 0, 1,
3216 1, 0,
3217 1, 1
3218 };
3219 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3220 kStatic_GrAccessPattern, vdata);
3221 }
3222 if (!fCopyProgramArrayBuffer) {
3223 return false;
3224 }
3225
3226 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3227 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3228 if (!fCopyPrograms[progIdx].fProgram) {
3229 return false;
3230 }
3231
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003232 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3233 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3234 GrShaderVar::kUniform_TypeModifier);
3235 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3236 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3237 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3238 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3239
Greg Daniel9e3169b2019-06-06 14:38:17 -04003240 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003241 if (shaderCaps->noperspectiveInterpolationSupport()) {
3242 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3243 vshaderTxt.appendf("#extension %s : require\n", extension);
3244 }
3245 vTexCoord.addModifier("noperspective");
3246 }
3247
3248 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3249 vshaderTxt.append(";");
3250 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3251 vshaderTxt.append(";");
3252 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3253 vshaderTxt.append(";");
3254 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3255 vshaderTxt.append(";");
3256
3257 vshaderTxt.append(
3258 "// Copy Program VS\n"
3259 "void main() {"
3260 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3261 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3262 " sk_Position.zw = half2(0, 1);"
3263 "}"
3264 );
3265
Greg Daniel9e3169b2019-06-06 14:38:17 -04003266 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 if (shaderCaps->noperspectiveInterpolationSupport()) {
3268 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3269 fshaderTxt.appendf("#extension %s : require\n", extension);
3270 }
3271 }
3272 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3273 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3274 fshaderTxt.append(";");
3275 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3276 fshaderTxt.append(";");
3277 fshaderTxt.appendf(
3278 "// Copy Program FS\n"
3279 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003280 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003281 "}"
3282 );
3283
3284 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3285 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3286 SkSL::Program::Settings settings;
3287 settings.fCaps = shaderCaps;
3288 SkSL::String glsl;
3289 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3290 sksl, settings, &glsl, errorHandler);
3291 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3292 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3293 SkASSERT(program->fInputs.isEmpty());
3294
3295 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3296 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3297 errorHandler);
3298 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3299 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3300 errorHandler);
3301 SkASSERT(program->fInputs.isEmpty());
3302
3303 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3304
3305 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3306 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3307 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3308 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3309 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3310 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3311
3312 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3313
3314 GL_CALL(DeleteShader(vshader));
3315 GL_CALL(DeleteShader(fshader));
3316
3317 return true;
3318}
3319
brianosman33f6b3f2016-06-02 05:49:21 -07003320bool GrGLGpu::createMipmapProgram(int progIdx) {
3321 const bool oddWidth = SkToBool(progIdx & 0x2);
3322 const bool oddHeight = SkToBool(progIdx & 0x1);
3323 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3324
Brian Salomon1edc5b92016-11-29 13:43:46 -05003325 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003326
3327 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3328 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3329 if (!fMipmapPrograms[progIdx].fProgram) {
3330 return false;
3331 }
3332
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003333 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3334 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003335 GrShaderVar::kUniform_TypeModifier);
3336 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3337 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003338 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003339 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003340 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3341 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3342 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3343 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003344 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003345 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003346
Ethan Nicholasfc994162019-06-06 10:04:27 -04003347 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003348 if (shaderCaps->noperspectiveInterpolationSupport()) {
3349 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003350 vshaderTxt.appendf("#extension %s : require\n", extension);
3351 }
3352 vTexCoords[0].addModifier("noperspective");
3353 vTexCoords[1].addModifier("noperspective");
3354 vTexCoords[2].addModifier("noperspective");
3355 vTexCoords[3].addModifier("noperspective");
3356 }
3357
Brian Salomon1edc5b92016-11-29 13:43:46 -05003358 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003359 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003360 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003361 vshaderTxt.append(";");
3362 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003363 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003364 vshaderTxt.append(";");
3365 }
3366
3367 vshaderTxt.append(
3368 "// Mipmap Program VS\n"
3369 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003370 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3371 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003372 );
3373
3374 // Insert texture coordinate computation:
3375 if (oddWidth && oddHeight) {
3376 vshaderTxt.append(
3377 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003378 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3379 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003380 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3381 );
3382 } else if (oddWidth) {
3383 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003384 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3385 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003386 );
3387 } else if (oddHeight) {
3388 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003389 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3390 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003391 );
3392 } else {
3393 vshaderTxt.append(
3394 " v_texCoord0 = a_vertex.xy;"
3395 );
3396 }
3397
3398 vshaderTxt.append("}");
3399
Ethan Nicholasfc994162019-06-06 10:04:27 -04003400 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003401 if (shaderCaps->noperspectiveInterpolationSupport()) {
3402 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003403 fshaderTxt.appendf("#extension %s : require\n", extension);
3404 }
3405 }
brianosman33f6b3f2016-06-02 05:49:21 -07003406 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003407 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003408 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003409 fshaderTxt.append(";");
3410 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003411 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003412 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003413 fshaderTxt.append(
3414 "// Mipmap Program FS\n"
3415 "void main() {"
3416 );
3417
3418 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003419 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003420 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3421 " sample(u_texture, v_texCoord1) + "
3422 " sample(u_texture, v_texCoord2) + "
3423 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003424 );
3425 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003426 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003427 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3428 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003429 );
3430 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003431 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003432 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003433 );
3434 }
3435
3436 fshaderTxt.append("}");
3437
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003438 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003439 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003440 SkSL::Program::Settings settings;
3441 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003442 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003443 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003444 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003445 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003446 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003447 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003448
Brian Osman6c431d52019-04-15 16:31:54 -04003449 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003450 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3451 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003452 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003453 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003454 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003455 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003456
3457 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3458
3459 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3460 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3461 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3462 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3463
3464 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3465
3466 GL_CALL(DeleteShader(vshader));
3467 GL_CALL(DeleteShader(fshader));
3468
3469 return true;
3470}
3471
Greg Daniel46cfbc62019-06-07 11:43:30 -04003472bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003473 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003474 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3475 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3476 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3477 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003478 return false;
3479 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003480 int progIdx = TextureToCopyProgramIdx(srcTex);
3481 if (!dstRT) {
3482 SkASSERT(dstTex);
3483 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3484 return false;
3485 }
3486 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003487 if (!fCopyPrograms[progIdx].fProgram) {
3488 if (!this->createCopyProgram(srcTex)) {
3489 SkDebugf("Failed to create copy program.\n");
3490 return false;
3491 }
3492 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003493 int w = srcRect.width();
3494 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003495 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003496 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003497 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003498 this->flushViewport(dst->width(), dst->height());
3499 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003500 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003501 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003502 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003503 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3504 attribs->enableVertexArrays(this, 1);
3505 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3506 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003507 // dst rect edges in NDC (-1 to 1)
3508 int dw = dst->width();
3509 int dh = dst->height();
3510 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3511 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3512 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3513 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003514 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3515 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3516 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3517 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3518 int sw = src->width();
3519 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003520 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3521 // src rect edges in normalized texture space (0 to 1)
3522 sx0 /= sw;
3523 sx1 /= sw;
3524 sy0 /= sh;
3525 sy1 /= sh;
3526 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003527 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3528 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3529 sx1 - sx0, sy1 - sy0, sx0, sy0));
3530 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003531 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003532 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003533 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003534 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003535 this->disableScissor();
3536 this->disableWindowRectangles();
3537 this->disableStencil();
3538 if (this->glCaps().srgbWriteControl()) {
3539 this->flushFramebufferSRGB(true);
3540 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003541 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003542 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003543 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3544 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003545 return true;
3546}
3547
Greg Daniel46cfbc62019-06-07 11:43:30 -04003548void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003549 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003550 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003551 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003552 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003553 SkASSERT(dstTex);
3554 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003555 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003556
Brian Salomond978b902019-02-07 15:09:18 -05003557 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003558 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003559 dstPoint.fX, dstPoint.fY,
3560 srcRect.fLeft, srcRect.fTop,
3561 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003562 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003563 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3564 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003565 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3566 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003567}
3568
Greg Daniel46cfbc62019-06-07 11:43:30 -04003569bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003570 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003571 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003572 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3573 srcRect.width(), srcRect.height());
3574 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003575 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003576 return false;
mtklein404b3b22015-05-18 09:29:10 -07003577 }
bsalomon5df6fee2015-05-18 06:26:15 -07003578 }
bsalomon6df86402015-06-01 10:41:49 -07003579
Brian Salomond2a8ae22019-09-10 16:03:59 -04003580 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3581 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003582 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003583 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003584
3585 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003586 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003587 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003588
Greg Daniel46cfbc62019-06-07 11:43:30 -04003589 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3590 srcRect.fTop,
3591 srcRect.fRight,
3592 srcRect.fBottom,
3593 dstRect.fLeft,
3594 dstRect.fTop,
3595 dstRect.fRight,
3596 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003597 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003598 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3599 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003600
3601 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3602 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003603 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003604}
3605
Brian Salomon930f9392018-06-20 16:25:26 -04003606bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3607 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003608 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003609 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003610 return false;
3611 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003612 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003613 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3614 // Uses draw calls to do a series of downsample operations to successive mips.
3615
3616 // The manual approach requires the ability to limit which level we're sampling and that the
3617 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003618 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003619 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003620 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003621 GL_CALL(GenerateMipmap(glTex->target()));
3622 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003623 }
3624
brianosman33f6b3f2016-06-02 05:49:21 -07003625 int width = texture->width();
3626 int height = texture->height();
3627 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003628 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003629
3630 // Create (if necessary), then bind temporary FBO:
3631 if (0 == fTempDstFBOID) {
3632 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3633 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003634 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003635 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003636
3637 // Bind the texture, to get things configured for filtering.
3638 // We'll be changing our base level further below:
3639 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003640 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003641 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003642
3643 // Vertex data:
3644 if (!fMipmapProgramArrayBuffer) {
3645 static const GrGLfloat vdata[] = {
3646 0, 0,
3647 0, 1,
3648 1, 0,
3649 1, 1
3650 };
Brian Salomonae64c192019-02-05 09:41:37 -05003651 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003652 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003653 }
3654 if (!fMipmapProgramArrayBuffer) {
3655 return false;
3656 }
3657
3658 fHWVertexArrayState.setVertexArrayID(this, 0);
3659
3660 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003661 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003662 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003663 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003664
3665 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003666 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003667 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003668 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003669 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003670 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003671
3672 // Do all the blits:
3673 width = texture->width();
3674 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003675
brianosman33f6b3f2016-06-02 05:49:21 -07003676 for (GrGLint level = 1; level < levelCount; ++level) {
3677 // Get and bind the program for this particular downsample (filter shape can vary):
3678 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3679 if (!fMipmapPrograms[progIdx].fProgram) {
3680 if (!this->createMipmapProgram(progIdx)) {
3681 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003682 // Invalidate all params to cover base level change in a previous iteration.
3683 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003684 return false;
3685 }
3686 }
Brian Salomon802cb312018-06-08 18:05:20 -04003687 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003688
3689 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3690 const float invWidth = 1.0f / width;
3691 const float invHeight = 1.0f / height;
3692 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3693 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3694 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3695
3696 // Only sample from previous mip
3697 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3698
Brian Salomon930f9392018-06-20 16:25:26 -04003699 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3700 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003701
Brian Osman788b9162020-02-07 10:36:46 -05003702 width = std::max(1, width / 2);
3703 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003704 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003705
3706 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3707 }
3708
3709 // Unbind:
3710 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3711 GR_GL_TEXTURE_2D, 0, 0));
3712
Brian Salomon930f9392018-06-20 16:25:26 -04003713 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003714 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3715 // We drew the 2nd to last level into the last level.
3716 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3717 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003718
brianosman33f6b3f2016-06-02 05:49:21 -07003719 return true;
3720}
3721
Chris Daltond7291ba2019-03-07 14:17:03 -07003722void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003723 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3724 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003725
3726 int effectiveSampleCnt;
3727 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003728 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003729
3730 sampleLocations->reset(effectiveSampleCnt);
3731 for (int i = 0; i < effectiveSampleCnt; ++i) {
3732 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3733 }
3734}
3735
cdalton231c5fd2015-05-13 12:35:36 -07003736void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003737 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003738 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003739 case kTexture_GrXferBarrierType: {
3740 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003741 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003742 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3743 // The render target uses separate storage so no need for glTextureBarrier.
3744 // FIXME: The render target will resolve automatically when its texture is bound,
3745 // but we could resolve only the bounds that will be read if we do it here instead.
3746 return;
3747 }
cdalton9954bc32015-04-29 14:17:00 -07003748 SkASSERT(this->caps()->textureBarrierSupport());
3749 GL_CALL(TextureBarrier());
3750 return;
cdalton231c5fd2015-05-13 12:35:36 -07003751 }
cdalton8917d622015-05-06 13:40:21 -07003752 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003753 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003754 this->caps()->blendEquationSupport());
3755 GL_CALL(BlendBarrier());
3756 return;
bsalomoncb02b382015-08-12 11:14:50 -07003757 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003758 }
3759}
3760
Chris Daltone1196c52019-12-28 14:31:09 -07003761void GrGLGpu::insertManualFramebufferBarrier() {
3762 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3763 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3764}
3765
Brian Salomon85c3d682019-11-04 15:04:54 -05003766GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003767 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003768 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003769 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003770 GrProtected isProtected,
3771 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003772 // We don't support protected textures in GL.
3773 if (isProtected == GrProtected::kYes) {
3774 return {};
3775 }
3776
Brian Salomon8a375832018-03-14 10:21:40 -04003777 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003778
Brian Salomond4764a12019-08-08 12:08:24 -04003779 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003780 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003781 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003782 }
3783
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003784 int numMipLevels = 1;
3785 if (mipMapped == GrMipMapped::kYes) {
3786 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3787 }
3788
Robert Phillipsd34691b2019-09-24 13:38:43 -04003789 // Compressed formats go through onCreateCompressedBackendTexture
3790 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3791
Greg Daniel15500642019-06-27 03:05:55 +00003792 GrGLTextureInfo info;
3793 GrGLTextureParameters::SamplerOverriddenState initialState;
3794
Greg Danield51fa2f2020-01-22 16:53:38 -05003795 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003796 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003797 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003798
Robert Phillipsd34691b2019-09-24 13:38:43 -04003799 info.fTarget = GR_GL_TEXTURE_2D;
3800 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003801 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003802 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003803 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003804 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003805
Robert Phillips42716d42019-12-16 12:19:54 -05003806 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003807 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3808 SkTDArray<GrMipLevel> texels;
3809 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003810 texels.append(numMipLevels);
3811 for (int i = 0; i < numMipLevels; ++i) {
3812 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3813 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003814 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3815 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3816 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003817 GL_CALL(DeleteTextures(1, &info.fID));
3818 return {};
3819 }
3820 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3821 // TODO: Unify this with the clear texture code in onCreateTexture().
3822 GrColorType colorType;
3823 GrGLenum externalFormat, externalType;
3824 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3825 &externalType, &colorType);
3826 if (colorType == GrColorType::kUnknown) {
3827 GL_CALL(DeleteTextures(1, &info.fID));
3828 return {};
3829 }
3830
3831 // Make one tight image at the base size and reuse it for smaller levels.
3832 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3833 auto rb = ii.minRowBytes();
3834 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3835 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3836 GL_CALL(DeleteTextures(1, &info.fID));
3837 return {};
3838 }
3839
3840 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3841 SkISize levelDimensions = dimensions;
3842 for (int i = 0; i < numMipLevels; ++i) {
3843 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3844 levelDimensions.height(), externalFormat, externalType,
3845 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003846 levelDimensions = {std::max(1, levelDimensions.width() /2),
3847 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003848 }
3849 }
3850 // Unbind this texture from the scratch texture unit.
3851 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003852
Brian Salomone2826ab2019-06-04 15:58:31 -04003853 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003854 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003855 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3856 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003857
Brian Salomon85c3d682019-11-04 15:04:54 -05003858 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3859 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003860}
3861
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003862void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003863 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3864
3865 GrGLTextureInfo info;
3866 if (tex.getGLTextureInfo(&info)) {
3867 GL_CALL(DeleteTextures(1, &info.fID));
3868 }
3869}
3870
3871#if GR_TEST_UTILS
3872
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003873bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003874 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003875
Greg Daniel52e16d92018-04-10 09:34:07 -04003876 GrGLTextureInfo info;
3877 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003878 return false;
3879 }
3880
3881 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003882 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003883
3884 return (GR_GL_TRUE == result);
3885}
3886
Brian Salomonf865b052018-03-09 09:01:53 -05003887GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003888 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003889 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3890 return GrBackendRenderTarget(); // invalid
3891 }
Brian Salomon8a375832018-03-14 10:21:40 -04003892 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003893 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003894 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003895 return {};
3896 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003897 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003898 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003899 if (sFormatIdx < 0) {
3900 return {};
3901 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003902 GrGLuint colorID = 0;
3903 GrGLuint stencilID = 0;
3904 auto deleteIDs = [&] {
3905 if (colorID) {
3906 if (useTexture) {
3907 GL_CALL(DeleteTextures(1, &colorID));
3908 } else {
3909 GL_CALL(DeleteRenderbuffers(1, &colorID));
3910 }
Brian Salomonf865b052018-03-09 09:01:53 -05003911 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003912 if (stencilID) {
3913 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003914 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003915 };
3916
3917 if (useTexture) {
3918 GL_CALL(GenTextures(1, &colorID));
3919 } else {
3920 GL_CALL(GenRenderbuffers(1, &colorID));
3921 }
3922 GL_CALL(GenRenderbuffers(1, &stencilID));
3923 if (!stencilID || !colorID) {
3924 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003925 return {};
3926 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003927
Brian Salomonf865b052018-03-09 09:01:53 -05003928 GrGLFramebufferInfo info;
3929 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003930 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003931 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3932 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003933 deleteIDs();
3934 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003935 }
3936
3937 this->invalidateBoundRenderTarget();
3938
Adrienne Walker4ee88512018-05-17 11:37:14 -07003939 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003940 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003941 GrGLTextureParameters::SamplerOverriddenState initialState;
3942 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3943 if (!colorID) {
3944 deleteIDs();
3945 return {};
3946 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003947 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3948 colorID, 0));
3949 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003950 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003951 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3952 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003953 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003954 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3955 GR_GL_RENDERBUFFER, colorID));
3956 }
3957 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003958 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3959 GL_ALLOC_CALL(this->glInterface(),
3960 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3961 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003962 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003963 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3964 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003965 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003966 }
3967
Brian Salomon93348dd2018-08-29 12:56:23 -04003968 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3969 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3970 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3971 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003972 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003973 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003974
Adrienne Walker4ee88512018-05-17 11:37:14 -07003975 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003976 GrGLenum status;
3977 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3978 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003979 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003980 return {};
3981 }
3982 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003983
Greg Daniel108bb232018-07-03 16:18:29 -04003984 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003985 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003986 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003987}
3988
3989void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003990 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003991 GrGLFramebufferInfo info;
3992 if (backendRT.getGLFramebufferInfo(&info)) {
3993 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003994 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003995 }
3996 }
joshualitt8fd844f2015-12-02 13:36:47 -08003997}
3998
Greg Daniel26b50a42018-03-08 09:49:58 -05003999void GrGLGpu::testingOnly_flushGpuAndSync() {
4000 GL_CALL(Finish());
4001}
Brian Salomonf865b052018-03-09 09:01:53 -05004002#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004003
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004004///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004005
cdaltone2e71c22016-04-07 18:13:29 -07004006GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004007 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004008 GrGLAttribArrayState* attribState;
4009
cdaltone2e71c22016-04-07 18:13:29 -07004010 if (gpu->glCaps().isCoreProfile()) {
4011 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004012 GrGLuint arrayID;
4013 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4014 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004015 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004016 }
cdaltone2e71c22016-04-07 18:13:29 -07004017 if (ibuf) {
4018 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004019 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004020 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004021 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004022 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004023 if (ibuf) {
4024 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004025 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004026 } else {
4027 this->setVertexArrayID(gpu, 0);
4028 }
4029 int attrCount = gpu->glCaps().maxVertexAttributes();
4030 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4031 fDefaultVertexArrayAttribState.resize(attrCount);
4032 }
4033 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004034 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004035 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004036}
bsalomone179a912016-01-20 06:18:10 -08004037
Greg Daniel30a35e82019-11-19 14:12:25 -05004038bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004039 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004040 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004041 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004042 // We call finish if the client told us to sync or if we have a finished proc but don't support
4043 // GLsync objects.
4044 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4045 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4046 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004047 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004048 // After a finish everything previously sent to GL is done.
4049 for (const auto& cb : fFinishCallbacks) {
4050 cb.fCallback(cb.fContext);
4051 this->deleteSync(cb.fSync);
4052 }
4053 fFinishCallbacks.clear();
4054 if (info.fFinishedProc) {
4055 info.fFinishedProc(info.fFinishedContext);
4056 }
4057 } else {
4058 if (info.fFinishedProc) {
4059 FinishCallback callback;
4060 callback.fCallback = info.fFinishedProc;
4061 callback.fContext = info.fFinishedContext;
4062 callback.fSync = (GrGLsync)this->insertFence();
4063 fFinishCallbacks.push_back(callback);
4064 GL_CALL(Flush());
4065 } else if (insertedSemaphore) {
4066 // Must call flush after semaphores in case they are waited on another GL context.
4067 GL_CALL(Flush());
4068 }
4069 // See if any previously inserted finish procs are good to go.
4070 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004071 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004072 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004073}
4074
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004075void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4076 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4077 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4078 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004079}
4080
Greg Daniel6be35232017-03-01 17:01:09 -05004081GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004082 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004083 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004084 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4085 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4086 GrGLuint fence = 0;
4087 GL_CALL(GenFences(1, &fence));
4088 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4089 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4090 } else {
4091 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4092 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004093 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004094 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004095}
4096
Brian Salomonb0d8b762019-05-06 16:58:22 -04004097bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004098 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4099 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4100 if (!timeout) {
4101 if (flush) {
4102 GL_CALL(Flush);
4103 }
4104 GrGLboolean result;
4105 GL_CALL_RET(result, TestFence(nvFence));
4106 return result == GR_GL_TRUE;
4107 }
4108 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4109 // If this really becomes necessary we could poll TestFence().
4110 // FinishFence always flushes so no need to check flush param.
4111 GL_CALL(FinishFence(nvFence));
4112 return true;
4113 } else {
4114 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4115 GrGLenum result;
4116 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4117 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4118 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004119}
4120
4121bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4122 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004123}
4124
4125void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004126 this->deleteSync((GrGLsync)fence);
4127}
4128
Greg Daniel301015c2019-11-18 14:06:46 -05004129std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004130 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004131 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004132}
4133
Greg Daniel301015c2019-11-18 14:06:46 -05004134std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4135 const GrBackendSemaphore& semaphore,
4136 GrResourceProvider::SemaphoreWrapType wrapType,
4137 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004138 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004139 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4140}
4141
Greg Daniel301015c2019-11-18 14:06:46 -05004142void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4143 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004144
Greg Daniel48661b82018-01-22 16:11:35 -05004145 GrGLsync sync;
4146 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4147 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004148}
4149
Greg Daniel301015c2019-11-18 14:06:46 -05004150void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4151 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004152
4153 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4154}
4155
Brian Salomonb0d8b762019-05-06 16:58:22 -04004156void GrGLGpu::checkFinishProcs() {
4157 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4158 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4159 /* timeout = */ 0, /* flush = */ false)) {
4160 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4161 this->deleteSync(fFinishCallbacks.front().fSync);
4162 fFinishCallbacks.pop_front();
4163 }
4164}
4165
Greg Daniel6be35232017-03-01 17:01:09 -05004166void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004167 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4168 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4169 GL_CALL(DeleteFences(1, &nvFence));
4170 } else {
4171 GL_CALL(DeleteSync(sync));
4172 }
jvanverth84741b32016-09-30 08:39:02 -07004173}
Brian Osman13dddce2017-05-09 13:19:50 -04004174
Greg Daniel301015c2019-11-18 14:06:46 -05004175std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004176 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004177 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004178 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004179 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004180 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4181 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004182
4183 return semaphore;
4184}
Robert Phillips646e4292017-06-13 12:44:56 -04004185
4186int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004187 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004188 case kTexture2DSampler_GrSLType:
4189 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004190 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004191 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004192 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004193 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004194 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004195 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004196 }
4197}
Brian Osman71a18892017-08-10 10:23:25 -04004198
Kevin Lubickf4def342018-10-04 12:52:50 -04004199#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004200#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004201void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4202 // We are called by the base class, which has already called beginObject(). We choose to nest
4203 // all of our caps information in a named sub-object.
4204 writer->beginObject("GL GPU");
4205
4206 const GrGLubyte* str;
4207 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4208 writer->appendString("GL_VERSION", (const char*)(str));
4209 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4210 writer->appendString("GL_RENDERER", (const char*)(str));
4211 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4212 writer->appendString("GL_VENDOR", (const char*)(str));
4213 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4214 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4215
4216 writer->appendName("extensions");
4217 glInterface()->fExtensions.dumpJSON(writer);
4218
4219 writer->endObject();
4220}
Kevin Lubickf4def342018-10-04 12:52:50 -04004221#endif