blob: 7697ebb325fc7bf73e64ae176a88ec0855cbf62e [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
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000455 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400456 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700457}
458
bsalomon6e2aad42016-04-01 11:54:31 -0700459void GrGLGpu::disconnect(DisconnectType type) {
460 INHERITED::disconnect(type);
461 if (DisconnectType::kCleanup == type) {
462 if (fHWProgramID) {
463 GL_CALL(UseProgram(0));
464 }
465 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700466 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700467 }
468 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700469 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700470 }
471 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700472 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700473 }
bsalomon6e2aad42016-04-01 11:54:31 -0700474 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
475 if (fCopyPrograms[i].fProgram) {
476 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
477 }
478 }
brianosman33f6b3f2016-06-02 05:49:21 -0700479 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
480 if (fMipmapPrograms[i].fProgram) {
481 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
482 }
483 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400484
Brian Salomondc829942018-10-23 16:07:24 -0400485 if (fSamplerObjectCache) {
486 fSamplerObjectCache->release();
487 }
bsalomon6e2aad42016-04-01 11:54:31 -0700488 } else {
489 if (fProgramCache) {
490 fProgramCache->abandon();
491 }
Brian Salomondc829942018-10-23 16:07:24 -0400492 if (fSamplerObjectCache) {
493 fSamplerObjectCache->abandon();
494 }
bsalomon6e2aad42016-04-01 11:54:31 -0700495 }
496
Brian Salomon802cb312018-06-08 18:05:20 -0400497 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700498 delete fProgramCache;
499 fProgramCache = nullptr;
500
bsalomonc8dc1f72014-08-21 13:02:13 -0700501 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700502 fTempSrcFBOID = 0;
503 fTempDstFBOID = 0;
504 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700505 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800506 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
507 fCopyPrograms[i].fProgram = 0;
508 }
brianosman33f6b3f2016-06-02 05:49:21 -0700509 fMipmapProgramArrayBuffer.reset();
510 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
511 fMipmapPrograms[i].fProgram = 0;
512 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400513
jvanverthe9c0fc62015-04-29 11:18:05 -0700514 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700515 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700516 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000517}
518
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000519///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000520
bsalomon861e1032014-12-16 07:33:49 -0800521void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000522 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400523 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000524 GL_CALL(Disable(GR_GL_DEPTH_TEST));
525 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000526
Brian Salomonf0861672017-05-08 11:10:10 -0400527 // We don't use face culling.
528 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600529 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
530 // just set this to the default for self-consistency.
531 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400532
Brian Salomonae64c192019-02-05 09:41:37 -0500533 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
534 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700535
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400536 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500537#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000538 // Desktop-only state that we never change
539 if (!this->glCaps().isCoreProfile()) {
540 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
541 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
542 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
543 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
544 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
545 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
546 }
547 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
548 // core profile. This seems like a bug since the core spec removes any mention of
549 // GL_ARB_imaging.
550 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
551 GL_CALL(Disable(GR_GL_COLOR_TABLE));
552 }
553 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400554
Chris Dalton1215cda2019-12-17 21:44:04 -0700555 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500556#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000557 // Since ES doesn't support glPointSize at all we always use the VS to
558 // set the point size
559 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
560
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000561 }
joshualitt58162332014-08-01 06:44:53 -0700562
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400563 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500564 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700565 // The arm extension requires specifically enabling MSAA fetching per sample.
566 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500567 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700568 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 fHWWriteToColor = kUnknown_TriState;
570 // we only ever use lines in hairline mode
571 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700572 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500573
574 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000575 }
edisonn@google.comba669992013-06-28 16:03:21 +0000576
egdanielb414f252014-07-29 13:15:47 -0700577 if (resetBits & kMSAAEnable_GrGLBackendState) {
578 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700579
Chris Dalton6ce447a2019-06-23 18:07:38 -0600580 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800581 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
582 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700583 GL_CALL(CoverageModulation(GR_GL_RGBA));
584 }
Chris Daltonce425af2019-12-16 10:39:03 -0700585
586 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000589 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400590 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000591
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500593 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500594 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 }
Brian Salomondc829942018-10-23 16:07:24 -0400596 if (fSamplerObjectCache) {
597 fSamplerObjectCache->invalidateBindings();
598 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000599 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000600
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000601 if (resetBits & kBlend_GrGLBackendState) {
602 fHWBlendState.invalidate();
603 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000604
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000605 if (resetBits & kView_GrGLBackendState) {
606 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700607 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000608 fHWViewport.invalidate();
609 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000610
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000611 if (resetBits & kStencil_GrGLBackendState) {
612 fHWStencilSettings.invalidate();
613 fHWStencilTestEnabled = kUnknown_TriState;
614 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000615
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000616 // Vertex
617 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700618 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500619 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
620 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700621 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000624 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500625 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700626 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000627 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000628
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000629 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700630 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700631 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000632 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000633 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000634
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000635 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400637 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000638 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
639 }
Brian Salomon1047a492019-07-02 12:25:21 -0400640 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
642 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000643 if (this->glCaps().packFlipYSupport()) {
644 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
645 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000646 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000647
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000648 if (resetBits & kProgram_GrGLBackendState) {
649 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400650 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000651 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400652 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000653}
654
Brian Salomonea4ad302019-08-07 13:04:55 -0400655static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
656 const GrGLCaps& caps, GrGLTexture::Desc* desc,
657 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400658 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400659 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400660 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800661 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000662
Brian Salomonea4ad302019-08-07 13:04:55 -0400663 desc->fSize = {backendTex.width(), backendTex.height()};
664 desc->fTarget = info.fTarget;
665 desc->fID = info.fID;
666 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800667
Brian Salomonea4ad302019-08-07 13:04:55 -0400668 if (desc->fFormat == GrGLFormat::kUnknown) {
669 return false;
670 }
671 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400672 if (!caps.shaderCaps()->externalTextureSupport()) {
673 return false;
674 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400675 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
676 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400677 return false;
678 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400679 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400680 return false;
681 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400682 if (backendTex.isProtected()) {
683 // Not supported in GL backend at this time.
684 return false;
685 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400686
Brian Salomond17f6582017-07-19 18:28:58 -0400687 return true;
688}
689
690sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400691 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400692 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400693 GrGLTexture::Desc desc;
694 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400695 return nullptr;
696 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400697
Brian Salomond17f6582017-07-19 18:28:58 -0400698 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400699 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400700 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400701 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400702 }
bsalomone5286e02016-01-14 09:24:09 -0800703
Greg Daniel177e6952017-10-12 12:27:11 -0400704 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
705 : GrMipMapsStatus::kNotAllocated;
706
Brian Salomonea4ad302019-08-07 13:04:55 -0400707 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400708 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400709 // We don't know what parameters are already set on wrapped textures.
710 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400711 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400712}
713
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500714static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
715 const GrGLCaps& caps, GrGLTexture::Desc* desc,
716 bool skipRectTexSupportCheck = false) {
717 GrGLTextureInfo info;
718 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
719 return false;
720 }
721
722 desc->fSize = {backendTex.width(), backendTex.height()};
723 desc->fTarget = info.fTarget;
724 desc->fID = info.fID;
725 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
726
727 if (desc->fFormat == GrGLFormat::kUnknown) {
728 return false;
729 }
730
731 if (GR_GL_TEXTURE_2D != desc->fTarget) {
732 return false;
733 }
734 if (backendTex.isProtected()) {
735 // Not supported in GL backend at this time.
736 return false;
737 }
738
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500739 return true;
740}
741
Robert Phillipsb915c942019-12-17 14:44:37 -0500742sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
743 GrWrapOwnership ownership,
744 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500745 GrGLTexture::Desc desc;
746 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
747 return nullptr;
748 }
749
750 if (kBorrow_GrWrapOwnership == ownership) {
751 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
752 } else {
753 desc.fOwnership = GrBackendObjectOwnership::kOwned;
754 }
755
756 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
757 : GrMipMapsStatus::kNotAllocated;
758
759 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
760 backendTex.getGLTextureParams(), cacheable,
761 kRead_GrIOType);
762 // We don't know what parameters are already set on wrapped textures.
763 texture->textureParamsModified();
764 return texture;
Robert Phillipsb915c942019-12-17 14:44:37 -0500765}
766
Brian Salomond17f6582017-07-19 18:28:58 -0400767sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400768 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400769 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500770 GrWrapOwnership ownership,
771 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400772 const GrGLCaps& caps = this->glCaps();
773
Brian Salomonea4ad302019-08-07 13:04:55 -0400774 GrGLTexture::Desc desc;
775 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800776 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800777 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400778 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
779 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800780
Brian Salomond17f6582017-07-19 18:28:58 -0400781 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400782 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800783 return nullptr;
784 }
bsalomon10528f12015-10-14 12:54:52 -0700785
Brian Osman766fcbb2017-03-13 09:33:09 -0400786 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400787 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400788 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400789 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800790 }
bsalomonb15b4c12014-10-29 12:41:57 -0700791
Robert Phillips0902c982019-07-16 07:47:56 -0400792
Greg Daniel6fa62e22019-08-07 15:52:37 -0400793 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
794 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000795
Brian Salomonea4ad302019-08-07 13:04:55 -0400796 GrGLRenderTarget::IDs rtIDs;
797 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400798 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000799 }
Greg Daniel177e6952017-10-12 12:27:11 -0400800
801 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
802 : GrMipMapsStatus::kNotAllocated;
803
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500804 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400805 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400806 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400807 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400808 // We don't know what parameters are already set on wrapped textures.
809 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400810 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000811}
812
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400813sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
814 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400815 GrGLFramebufferInfo info;
816 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000817 return nullptr;
818 }
819
Brian Salomone8a766b2019-07-19 14:24:36 -0400820 if (backendRT.isProtected()) {
821 // Not supported in GL at this time.
822 return nullptr;
823 }
824
Brian Salomond4764a12019-08-08 12:08:24 -0400825 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400826 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
827 return nullptr;
828 }
829
Brian Salomonea4ad302019-08-07 13:04:55 -0400830 GrGLRenderTarget::IDs rtIDs;
831 rtIDs.fRTFBOID = info.fFBOID;
832 rtIDs.fMSColorRenderbufferID = 0;
833 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
834 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000835
Brian Salomonea4ad302019-08-07 13:04:55 -0400836 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400837 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700838
Greg Danield51fa2f2020-01-22 16:53:38 -0500839 return GrGLRenderTarget::MakeWrapped(this, size, format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400840 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000841}
842
Greg Daniel7ef28f32017-04-20 16:41:55 +0000843sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400844 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400845 GrColorType colorType) {
846 GrGLTexture::Desc desc;
847 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
848 // with a texture rectangle,we assume the necessary support exists.
849 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800850 return nullptr;
851 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400852
853 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
854 return nullptr;
855 }
856
857 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400858 GrGLRenderTarget::IDs rtIDs;
859 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800860 return nullptr;
861 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500862 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800863}
864
Brian Salomonc320b152018-02-20 14:05:36 -0500865static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700866 if (!glTex) {
867 return false;
868 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000869
bsalomone5286e02016-01-14 09:24:09 -0800870 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
871 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800872 return false;
873 }
874
jvanverth17aa0472016-01-05 10:41:27 -0800875 return true;
876}
877
Brian Salomona9b04b92018-06-01 15:04:28 -0400878bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400879 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400880 const GrMipLevel texels[], int mipLevelCount,
881 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500882 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800883
Brian Salomonc320b152018-02-20 14:05:36 -0500884 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800885 return false;
886 }
887
Brian Salomond978b902019-02-07 15:09:18 -0500888 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000889
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400890 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400891 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400892 glTex->target(), left, top, width, height, srcColorType, texels,
893 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000894}
895
Brian Salomone05ba5a2019-04-08 11:59:07 -0400896bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400897 GrColorType textureColorType, GrColorType bufferColorType,
898 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400899 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400900
Jim Van Verth1676cb92019-01-15 13:24:45 -0500901 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400902 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500903
Brian Salomonc320b152018-02-20 14:05:36 -0500904 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400905 return false;
906 }
907
Hal Canaryc36f7e72018-06-18 15:50:09 -0400908 static_assert(sizeof(int) == sizeof(int32_t), "");
909 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910 return false;
911 }
912
Brian Salomond978b902019-02-07 15:09:18 -0500913 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914
915 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500916 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400917 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500918 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400919
Greg Daniel660cc992017-06-26 14:55:05 -0400920 SkDEBUGCODE(
921 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
922 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
923 SkASSERT(bounds.contains(subRect));
924 )
925
Brian Salomonb28cb682019-07-26 12:48:47 -0400926 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400927 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400928 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700929 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400930 return false;
931 }
932
933 bool restoreGLRowLength = false;
934 if (trimRowBytes != rowBytes) {
935 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400936 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400937 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
938 restoreGLRowLength = true;
939 }
940
Greg Danielba88ab62019-07-26 09:14:01 -0400941 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400942 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400943 GrGLenum externalFormat = 0;
944 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400945 this->glCaps().getTexSubImageExternalFormatAndType(
946 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400947 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400948 return false;
949 }
950
Brian Salomon8cbf6622019-07-30 11:03:14 -0400951 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400952 GL_CALL(TexSubImage2D(glTex->target(),
953 0,
954 left, top,
955 width,
956 height,
957 externalFormat, externalType,
958 pixels));
959
960 if (restoreGLRowLength) {
961 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
962 }
963
964 return true;
965}
966
Brian Salomon26de56e2019-04-10 12:14:26 -0400967bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400968 GrColorType surfaceColorType, GrColorType dstColorType,
969 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400970 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
971 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400972 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400973 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
974 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400975}
976
Brian Osman9b560242017-09-05 15:34:52 -0400977void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500978 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
979 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
980 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
981 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400982 }
Brian Osman9b560242017-09-05 15:34:52 -0400983}
984
Brian Salomond2a8ae22019-09-10 16:03:59 -0400985bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
986 int texHeight, GrGLenum target, int left, int top, int width,
987 int height, GrColorType srcColorType, const GrMipLevel texels[],
988 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500989 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400990 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500991
Greg Daniel7bfc9132019-08-14 14:23:53 -0400992 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400993 SkDEBUGCODE(
994 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
995 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
996 SkASSERT(bounds.contains(subRect));
997 )
Robert Phillips590533f2017-07-11 14:22:35 -0400998 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400999 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001000
Brian Osman9b560242017-09-05 15:34:52 -04001001 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001002
cblume55f2d2d2016-02-26 13:20:48 -08001003 const GrGLInterface* interface = this->glInterface();
1004 const GrGLCaps& caps = this->glCaps();
1005
Brian Salomonf77c1462019-08-01 15:19:29 -04001006 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001007
1008 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001009 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001010 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001011
bsalomon5b30c6f2015-12-17 14:17:34 -08001012 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001013 GrGLenum externalFormat;
1014 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001015 this->glCaps().getTexSubImageExternalFormatAndType(
1016 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001017 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001018 return false;
1019 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001020
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001021 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001022 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001023 * because our srcData has extra bytes past each row. If so, we need
1024 * to trim those off here, since GL ES may not let us specify
1025 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001026 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001027 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001028
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001029 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001030 *mipMapsStatus = (mipLevelCount > 1) ?
1031 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001032 }
bsalomone699d0c2016-03-09 06:25:15 -08001033
Brian Salomond2a8ae22019-09-10 16:03:59 -04001034 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001035
Brian Salomond2a8ae22019-09-10 16:03:59 -04001036 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1037 if (!texels[currentMipLevel].fPixels) {
1038 if (mipMapsStatus) {
1039 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001040 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001041 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001042 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Osman788b9162020-02-07 10:36:46 -05001044 const int currentWidth = std::max(1, width / twoToTheMipLevel);
1045 const int currentHeight = std::max(1, height / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001046 const size_t trimRowBytes = currentWidth * bpp;
1047 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1048
1049 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1050 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1051 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1052 restoreGLRowLength = true;
1053 }
1054
1055 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1056 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001057 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001058 if (restoreGLRowLength) {
1059 SkASSERT(caps.writePixelsRowBytesSupport());
1060 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1061 }
1062 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001063}
1064
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001066 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001067 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001068 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001069 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001070 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001071 const GrGLCaps& caps = this->glCaps();
1072
1073 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001074 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001075 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001076 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001077 }
1078
Robert Phillips9f744f72019-12-19 19:14:33 -05001079 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1080 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1081
Greg Daniel7bfc9132019-08-14 14:23:53 -04001082 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001083
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001084 int numMipLevels = 1;
1085 if (mipMapped == GrMipMapped::kYes) {
1086 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1087 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001088
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001089 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001090 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001091
1092 if (useTexStorage) {
1093 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001094 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001095 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001096 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001097 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1098 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001099 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001100 }
Robert Phillips42716d42019-12-16 12:19:54 -05001101
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001102 size_t offset = 0;
1103 for (int level = 0; level < numMipLevels; ++level) {
1104
Robert Phillips99dead92020-01-27 16:11:57 -05001105 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1106 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001107
1108 GL_CALL(CompressedTexSubImage2D(target,
1109 level,
1110 0, // left
1111 0, // top
1112 dimensions.width(),
1113 dimensions.height(),
1114 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001115 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001116 &((char*)data)[offset]));
1117
1118 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1119 if (error != GR_GL_NO_ERROR) {
1120 return false;
1121 }
1122
Robert Phillips9f744f72019-12-19 19:14:33 -05001123 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001124 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001125 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001126 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001127 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001128
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001130 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1131 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001132
1133 const char* rawLevelData = &((char*)data)[offset];
1134 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1135 level,
1136 internalFormat,
1137 dimensions.width(),
1138 dimensions.height(),
1139 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001140 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001141 rawLevelData));
1142
1143 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1144 if (error != GR_GL_NO_ERROR) {
1145 return false;
1146 }
1147
Robert Phillips9f744f72019-12-19 19:14:33 -05001148 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001149 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001150 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001151 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001152 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001153}
1154
bsalomon424cc262015-05-22 10:37:30 -07001155static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001156 int sampleCount,
1157 GrGLenum format,
1158 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001159 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001160 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001161 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001162 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001163 GL_ALLOC_CALL(ctx.interface(),
1164 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1165 sampleCount,
1166 format,
1167 width, height));
1168 break;
1169 case GrGLCaps::kES_Apple_MSFBOType:
1170 GL_ALLOC_CALL(ctx.interface(),
1171 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1172 sampleCount,
1173 format,
1174 width, height));
1175 break;
1176 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1177 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1178 GL_ALLOC_CALL(ctx.interface(),
1179 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1180 sampleCount,
1181 format,
1182 width, height));
1183 break;
1184 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001185 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001186 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001187 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001188 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001189}
1190
Brian Salomonea4ad302019-08-07 13:04:55 -04001191bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001192 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 GrGLRenderTarget::IDs* rtIDs) {
1194 rtIDs->fMSColorRenderbufferID = 0;
1195 rtIDs->fRTFBOID = 0;
1196 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1197 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198
bsalomona11e5fc2015-12-18 07:59:41 -08001199 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001200
Brian Salomonea4ad302019-08-07 13:04:55 -04001201 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001202 goto FAILED;
1203 }
1204
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001205 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001206 goto FAILED;
1207 }
1208
Brian Salomonea4ad302019-08-07 13:04:55 -04001209 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1210 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 goto FAILED;
1212 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001213
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001214 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1215 // the texture bound to the other. The exception is the IMG multisample extension. With this
1216 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1217 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001218 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001219 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1220 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1221 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001222 goto FAILED;
1223 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001224 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001225 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001226 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 }
1228
egdanield803f272015-03-18 13:01:52 -07001229 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001230 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001231 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001232 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001233 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001234 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 goto FAILED;
1237 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001238 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001239 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001240 GR_GL_COLOR_ATTACHMENT0,
1241 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001242 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001246 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001247 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1248 GR_GL_COLOR_ATTACHMENT0,
1249 desc.fTarget,
1250 desc.fID,
1251 0,
1252 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001253 } else {
egdanield803f272015-03-18 13:01:52 -07001254 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001255 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001256 desc.fTarget,
1257 desc.fID,
1258 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001259 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001260
1261 return true;
1262
1263FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 if (rtIDs->fMSColorRenderbufferID) {
1265 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001266 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001267 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1268 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001270 if (rtIDs->fTexFBOID) {
1271 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001272 }
1273 return false;
1274}
1275
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001276// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001277static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001278// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001279 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001280}
1281
Brian Salomone2826ab2019-06-04 15:58:31 -04001282static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001283 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001284 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1285 // drivers have a bug where an FBO won't be complete if it includes a
1286 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001287 GrGLTextureParameters::SamplerOverriddenState state;
1288 state.fMinFilter = GR_GL_NEAREST;
1289 state.fMagFilter = GR_GL_NEAREST;
1290 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1291 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1293 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1294 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1295 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001296 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001297}
1298
Robert Phillips67d52cf2017-06-05 13:38:13 -04001299sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001300 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001301 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001302 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001303 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001304 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001305 int mipLevelCount,
1306 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001307 // We don't support protected textures in GL.
1308 if (isProtected == GrProtected::kYes) {
1309 return nullptr;
1310 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001311 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001312
Brian Salomond2a8ae22019-09-10 16:03:59 -04001313 SkASSERT(mipLevelCount > 0);
1314 GrMipMapsStatus mipMapsStatus =
1315 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001316 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001317 GrGLTexture::Desc texDesc;
1318 texDesc.fSize = {desc.fWidth, desc.fHeight};
1319 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001320 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001321 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001322 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1323 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001324
Brian Salomond2a8ae22019-09-10 16:03:59 -04001325 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1326 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001327
1328 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001329 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001330 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001331
Robert Phillips67d52cf2017-06-05 13:38:13 -04001332 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001333 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001334 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001335 GL_CALL(BindTexture(texDesc.fTarget, 0));
1336 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001337
Brian Salomonea4ad302019-08-07 13:04:55 -04001338 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1339 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 return return_null_texture();
1341 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001342 tex = sk_make_sp<GrGLTextureRenderTarget>(
1343 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001344 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001345 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001346 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001348 // The non-sampler params are still at their default values.
1349 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1350 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001351 if (levelClearMask) {
1352 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001353 GrColorType colorType;
1354 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1355 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001356 if (this->glCaps().clearTextureSupport()) {
1357 for (int i = 0; i < mipLevelCount; ++i) {
1358 if (levelClearMask & (1U << i)) {
1359 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1360 nullptr));
1361 }
1362 }
1363 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1364 !this->glCaps().performColorClearsAsDraws()) {
1365 this->disableScissor();
1366 this->disableWindowRectangles();
1367 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001368 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001369 for (int i = 0; i < mipLevelCount; ++i) {
1370 if (levelClearMask & (1U << i)) {
1371 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1372 kDst_TempFBOTarget);
1373 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1374 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1375 }
1376 }
Brian Salomonc2249852019-09-16 11:08:50 -04001377 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001378 } else {
1379 std::unique_ptr<char[]> zeros;
1380 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1381 for (int i = 0; i < mipLevelCount; ++i) {
1382 if (levelClearMask & (1U << i)) {
Brian Osman788b9162020-02-07 10:36:46 -05001383 int levelWidth = std::max(1, texDesc.fSize.width() >> i);
1384 int levelHeight = std::max(1, texDesc.fSize.height() >> i);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001385 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1386 // smaller levels that need clearing.
1387 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001388 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001389 size_t size = levelWidth * levelHeight * bpp;
1390 zeros.reset(new char[size]());
1391 }
1392 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1393 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1394 externalFormat, externalType, zeros.get()));
1395 }
Brian Salomona3e29962019-07-16 11:52:08 -04001396 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001397 }
1398 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001399 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001400}
1401
Robert Phillips9f744f72019-12-19 19:14:33 -05001402sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001403 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001404 SkBudgeted budgeted,
1405 GrMipMapped mipMapped,
1406 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001407 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001408 // We don't support protected textures in GL.
1409 if (isProtected == GrProtected::kYes) {
1410 return nullptr;
1411 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001412 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001413 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001414 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001415 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001416 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001417 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001418 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001419 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001420 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001421 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001422 return nullptr;
1423 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001424
Robert Phillipsee946932019-12-18 11:16:17 -05001425 // Unbind this texture from the scratch texture unit.
1426 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1427
Robert Phillipse4720c62020-01-14 14:33:24 -05001428 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1429 ? GrMipMapsStatus::kValid
1430 : GrMipMapsStatus::kNotAllocated;
1431
1432 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001433 // The non-sampler params are still at their default values.
1434 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1435 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001436 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001437}
1438
Robert Phillipsb915c942019-12-17 14:44:37 -05001439GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1440 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001441 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001442 GrProtected isProtected,
1443 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001444 // We don't support protected textures in GL.
1445 if (isProtected == GrProtected::kYes) {
1446 return {};
1447 }
1448
1449 this->handleDirtyContext();
1450
1451 GrGLFormat glFormat = format.asGLFormat();
1452 if (glFormat == GrGLFormat::kUnknown) {
1453 return {};
1454 }
1455
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001456 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001457 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001458 SkAutoMalloc am;
1459
1460 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1461 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1462 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001463 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001464 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001465 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1466 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001467
Robert Phillips99dead92020-01-27 16:11:57 -05001468 rawDataSize = SkCompressedDataSize(compression, dimensions, nullptr,
1469 mipMapped == GrMipMapped::kYes);
Robert Phillips9f744f72019-12-19 19:14:33 -05001470
1471 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001472
1473 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1474
1475 rawData = (const char*) am.get();
1476 }
1477
1478 GrGLTextureInfo info;
1479 GrGLTextureParameters::SamplerOverriddenState initialState;
1480
1481 info.fTarget = GR_GL_TEXTURE_2D;
1482 info.fFormat = GrGLFormatToEnum(glFormat);
1483 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001484 mipMapped, &initialState,
1485 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001486 if (!info.fID) {
1487 return {};
1488 }
1489
1490 // Unbind this texture from the scratch texture unit.
1491 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1492
1493 auto parameters = sk_make_sp<GrGLTextureParameters>();
1494 // The non-sampler params are still at their default values.
1495 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1496 fResetTimestampForTextureParameters);
1497
1498 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1499 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001500}
1501
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001503
egdaniel8dc7c3a2015-04-16 11:22:42 -07001504const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001506void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001507 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001508
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001510 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 (kUnknownBitCount == format->fTotalBits));
1512 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001513 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1515 (GrGLint*)&format->fStencilBits);
1516 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001517 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1519 (GrGLint*)&format->fTotalBits);
1520 format->fTotalBits += format->fStencilBits;
1521 } else {
1522 format->fTotalBits = format->fStencilBits;
1523 }
1524 }
1525}
1526}
1527
Brian Salomon5043f1f2019-07-11 21:27:54 -04001528int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001529 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001530 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1531
1532 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001533 // Default to unsupported, set this if we find a stencil format that works.
1534 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001535
Brian Salomond2a8ae22019-09-10 16:03:59 -04001536 GrGLuint colorID =
1537 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1538 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001539 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001540 }
egdanielff1d5472015-09-10 08:37:20 -07001541 // unbind the texture from the texture unit before binding it to the frame buffer
1542 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1543
1544 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001545 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001546 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001547 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001548 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001549 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1550 GR_GL_COLOR_ATTACHMENT0,
1551 GR_GL_TEXTURE_2D,
1552 colorID,
1553 0));
bsalomon30447372015-12-21 09:03:05 -08001554 GrGLuint sbRBID = 0;
1555 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001556
1557 // look over formats till I find a compatible one
1558 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001559 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001560 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001561 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1562 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1563 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1564 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1565 sFmt.fInternalFormat,
1566 kSize, kSize));
1567 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001568 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001569 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001570 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001571 if (sFmt.fPacked) {
1572 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1573 GR_GL_DEPTH_ATTACHMENT,
1574 GR_GL_RENDERBUFFER, sbRBID));
1575 } else {
1576 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1577 GR_GL_DEPTH_ATTACHMENT,
1578 GR_GL_RENDERBUFFER, 0));
1579 }
1580 GrGLenum status;
1581 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1582 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1583 firstWorkingStencilFormatIndex = i;
1584 break;
1585 }
egdanielff1d5472015-09-10 08:37:20 -07001586 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1587 GR_GL_STENCIL_ATTACHMENT,
1588 GR_GL_RENDERBUFFER, 0));
1589 if (sFmt.fPacked) {
1590 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1591 GR_GL_DEPTH_ATTACHMENT,
1592 GR_GL_RENDERBUFFER, 0));
1593 }
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 }
bsalomon30447372015-12-21 09:03:05 -08001596 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001597 }
1598 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001599 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1600 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001601 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001602 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001603 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001604}
1605
Brian Salomonea4ad302019-08-07 13:04:55 -04001606GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001607 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001608 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001609 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001610 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001611 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001612 if (format == GrGLFormat::kUnknown) {
1613 return 0;
1614 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001615 GrGLuint id = 0;
1616 GL_CALL(GenTextures(1, &id));
1617 if (!id) {
1618 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001619 }
1620
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001622
Brian Salomonea4ad302019-08-07 13:04:55 -04001623 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1624
Robert Phillips42716d42019-12-16 12:19:54 -05001625 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001626 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1627 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001628 GL_CALL(DeleteTextures(1, &id));
1629 return 0;
1630 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001631 }
Robert Phillips42716d42019-12-16 12:19:54 -05001632
Brian Salomonea4ad302019-08-07 13:04:55 -04001633 return id;
1634}
1635
Robert Phillips2716daf2020-01-23 12:53:42 -05001636GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001637 GrGLFormat format,
1638 GrRenderable renderable,
1639 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001640 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001641 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001642 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001643
Brian Salomonea4ad302019-08-07 13:04:55 -04001644 GrGLuint id = 0;
1645 GL_CALL(GenTextures(1, &id));
1646
1647 if (!id) {
1648 return 0;
1649 }
1650
1651 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001652
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001653 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001654 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001655 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001656 }
1657
Brian Salomond2a8ae22019-09-10 16:03:59 -04001658 if (initialState) {
1659 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1660 } else {
1661 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001662 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001663
1664 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1665
1666 bool success = false;
1667 if (internalFormat) {
1668 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1669 if (this->glCaps().formatSupportsTexStorage(format)) {
1670 GL_ALLOC_CALL(this->glInterface(),
Brian Osman788b9162020-02-07 10:36:46 -05001671 TexStorage2D(GR_GL_TEXTURE_2D, std::max(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001672 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001673 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1674 } else {
1675 GrGLenum externalFormat, externalType;
1676 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1677 GrGLenum error = GR_GL_NO_ERROR;
1678 if (externalFormat && externalType) {
1679 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1680 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001681 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1682 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001683 GL_ALLOC_CALL(
1684 this->glInterface(),
1685 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1686 currentHeight, 0, externalFormat, externalType, nullptr));
1687 error = CHECK_ALLOC_ERROR(this->glInterface());
1688 }
1689 success = (GR_GL_NO_ERROR == error);
1690 }
1691 }
1692 }
1693 if (success) {
1694 return id;
1695 }
1696 GL_CALL(DeleteTextures(1, &id));
1697 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001698}
1699
Chris Daltoneffee202019-07-01 22:28:03 -06001700GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1701 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001702 SkASSERT(width >= rt->width());
1703 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001704
egdaniel8dc7c3a2015-04-16 11:22:42 -07001705 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001706
Brian Salomond4764a12019-08-08 12:08:24 -04001707 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001708 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001709 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001710 }
egdanielff1d5472015-09-10 08:37:20 -07001711
1712 if (!sbDesc.fRenderbufferID) {
1713 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1714 }
1715 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001716 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001717 }
1718 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1719 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1720 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1721 // we do this "if" so that we don't call the multisample
1722 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001723 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001724 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001725 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001726 sFmt.fInternalFormat,
1727 width, height));
1728 } else {
1729 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1730 sFmt.fInternalFormat,
1731 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001732 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001733 }
1734 fStats.incStencilAttachmentCreates();
1735 // After sized formats we attempt an unsized format and take
1736 // whatever sizes GL gives us. In that case we query for the size.
1737 GrGLStencilAttachment::Format format = sFmt;
1738 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001739 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1740 sbDesc,
1741 width,
1742 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001743 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001744 format);
1745 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001746}
1747
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001748////////////////////////////////////////////////////////////////////////////////
1749
Brian Salomondbf70722019-02-07 11:31:24 -05001750sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1751 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001752 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001753}
1754
Greg Danielacd66b42019-05-22 16:29:12 -04001755void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001756 GrSurfaceOrigin rtOrigin) {
1757 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001758 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001759 // if the scissor fully contains the viewport then we fall through and
1760 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001761 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001762 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001763 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001764 fHWScissorSettings.fRect = scissor;
1765 }
1766 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1767 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1768 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001769 }
1770 return;
1771 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001773
1774 // See fall through note above
1775 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001776}
1777
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001778void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001779 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001780#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001781 typedef GrWindowRectsState::Mode Mode;
1782 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1783 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001784
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001785 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001786 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001787 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001788 }
1789
csmartdalton7535f412016-08-23 06:51:00 -07001790 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1791 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001792 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001794
Chris Daltond6cda8d2019-09-05 02:30:04 -06001795 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001796 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001797 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001798 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001799 }
1800
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001801 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001802 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001803
Greg Danielacd66b42019-05-22 16:29:12 -04001804 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001805#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001806}
1807
1808void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001809#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001810 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001811 return;
1812 }
1813 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001814 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001815#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001816}
1817
Robert Phillipsfcaae482019-11-07 10:17:03 -05001818bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001819
Stephen Whiteb1857852020-02-07 15:33:23 +00001820 sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001821 if (!program) {
1822 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1823 return false;
1824 }
brianosman33f6b3f2016-06-02 05:49:21 -07001825
Brian Salomon802cb312018-06-08 18:05:20 -04001826 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001827
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001828 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001829 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1830 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001831
Robert Phillips901aff02019-10-08 12:32:56 -04001832 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001833
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001834 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001835 GrStencilSettings stencil;
1836 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001837 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1838 stencil.reset(*programInfo.pipeline().getUserStencil(),
1839 programInfo.pipeline().hasStencilClip(),
1840 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001841 }
Robert Phillips901aff02019-10-08 12:32:56 -04001842 this->flushStencil(stencil, programInfo.origin());
1843 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001844 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001845 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1846 : kBogusScissor);
1847 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001848 } else {
1849 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001850 }
Robert Phillips901aff02019-10-08 12:32:56 -04001851 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1852 glRT, programInfo.origin());
1853 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001854 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001855 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001856
1857 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001858 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001859 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001860
1861 return true;
1862}
1863
Brian Salomon802cb312018-06-08 18:05:20 -04001864void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1865 if (!program) {
1866 fHWProgram.reset();
1867 fHWProgramID = 0;
1868 return;
1869 }
1870 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1871 if (program == fHWProgram) {
1872 return;
1873 }
1874 auto id = program->programID();
1875 SkASSERT(id);
1876 GL_CALL(UseProgram(id));
1877 fHWProgram = std::move(program);
1878 fHWProgramID = id;
1879}
1880
1881void GrGLGpu::flushProgram(GrGLuint id) {
1882 SkASSERT(id);
1883 if (fHWProgramID == id) {
1884 SkASSERT(!fHWProgram);
1885 return;
1886 }
1887 fHWProgram.reset();
1888 GL_CALL(UseProgram(id));
1889 fHWProgramID = id;
1890}
1891
1892void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001893 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001894 int baseVertex,
1895 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001896 int baseInstance,
1897 GrPrimitiveRestart enablePrimitiveRestart) {
1898 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001899
cdaltone2e71c22016-04-07 18:13:29 -07001900 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001901 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001902 SkASSERT(indexBuffer->isCpuBuffer() ||
1903 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001904 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001905 } else {
1906 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001907 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001908
Brian Salomon92be2f72018-06-19 14:33:47 -04001909 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1910 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001911
Brian Salomon802cb312018-06-08 18:05:20 -04001912 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001913 SkASSERT(vertexBuffer);
1914 SkASSERT(vertexBuffer->isCpuBuffer() ||
1915 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1916 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001917 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1918 const auto& attrib = fHWProgram->vertexAttribute(i);
1919 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001920 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1921 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001922 }
Chris Dalton1d616352017-05-31 12:51:23 -06001923 }
Brian Salomon802cb312018-06-08 18:05:20 -04001924 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001925 SkASSERT(instanceBuffer);
1926 SkASSERT(instanceBuffer->isCpuBuffer() ||
1927 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1928 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001929 int attribIdx = fHWProgram->numVertexAttributes();
1930 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1931 const auto& attrib = fHWProgram->instanceAttribute(i);
1932 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001933 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1934 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1935 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001936 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001937 }
1938}
1939
Brian Salomonae64c192019-02-05 09:41:37 -05001940GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001941 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001942
cdaltone2e71c22016-04-07 18:13:29 -07001943 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001944 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001945 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001946 }
cdaltone2e71c22016-04-07 18:13:29 -07001947
Brian Salomonae64c192019-02-05 09:41:37 -05001948 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001949 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001950 if (!bufferState->fBufferZeroKnownBound) {
1951 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1952 bufferState->fBufferZeroKnownBound = true;
1953 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001954 }
Brian Salomondbf70722019-02-07 11:31:24 -05001955 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1956 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001957 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1958 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1959 bufferState->fBufferZeroKnownBound = false;
1960 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001961 }
1962
Brian Salomonae64c192019-02-05 09:41:37 -05001963 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001964}
Brian Salomond818ebf2018-07-02 14:08:49 +00001965void GrGLGpu::disableScissor() {
1966 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1967 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1968 fHWScissorSettings.fEnabled = kNo_TriState;
1969 return;
1970 }
1971}
1972
Brian Osman9a9baae2018-11-05 15:06:26 -05001973void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001974 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001975 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001976 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001977 SkASSERT(!this->caps()->performColorClearsAsDraws());
1978 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001979
Brian Salomon43f8bf02017-10-18 08:33:29 -04001980 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001981
Brian Salomon43f8bf02017-10-18 08:33:29 -04001982 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1983
Brian Salomond818ebf2018-07-02 14:08:49 +00001984 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001985 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001986 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001987 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001988 }
Greg Danielacd66b42019-05-22 16:29:12 -04001989 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001990 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001991 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001992 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001994}
1995
Robert Phillips95214472017-08-08 18:00:03 -04001996void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001997 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1998
Robert Phillips95214472017-08-08 18:00:03 -04001999 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002000 return;
2001 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002002
Chris Dalton674f77a2019-09-30 20:49:39 -06002003 // This should only be called internally when we know we have a stencil buffer.
2004 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002005
bsalomonb0bd4f62014-09-03 07:19:50 -07002006 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002007 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002008
Brian Salomond818ebf2018-07-02 14:08:49 +00002009 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002010 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002013 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002015 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002016}
2017
Chris Daltonb50cc812019-10-14 16:29:21 -06002018static bool use_tiled_rendering(const GrGLCaps& glCaps,
2019 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2020 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2021 // Otherwise it's faster to just not use it.
2022 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2023 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2024}
2025
2026void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002027 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2028 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2029 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2030
2031 this->handleDirtyContext();
2032
2033 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2034 this->flushRenderTarget(glRT);
2035 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2036
Chris Daltonb50cc812019-10-14 16:29:21 -06002037 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2038 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2039 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2040 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2041 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2042 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2043 nativeBounds.fHeight, preserveMask));
2044 }
2045
Chris Dalton674f77a2019-09-30 20:49:39 -06002046 GrGLbitfield clearMask = 0;
2047 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2048 SkASSERT(!this->caps()->performColorClearsAsDraws());
2049 this->flushClearColor(colorLoadStore.fClearColor);
2050 this->flushColorWrite(true);
2051 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002052 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002053 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2054 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2055 GL_CALL(StencilMask(0xffffffff));
2056 GL_CALL(ClearStencil(0));
2057 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2058 }
2059 if (clearMask) {
2060 this->disableScissor();
2061 this->disableWindowRectangles();
2062 GL_CALL(Clear(clearMask));
2063 }
2064}
2065
2066void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2067 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2068 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2069 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2070
2071 this->handleDirtyContext();
2072
2073 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2074 // The framebuffer binding changed in the middle of a command buffer. We should have already
2075 // printed a warning during onFBOChanged.
2076 return;
2077 }
2078
2079 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2080 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2081
2082 SkSTArray<2, GrGLenum> discardAttachments;
2083 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2084 discardAttachments.push_back(
2085 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2086 }
2087 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2088 discardAttachments.push_back(
2089 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2090 }
2091
2092 if (!discardAttachments.empty()) {
2093 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2094 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2095 discardAttachments.begin()));
2096 } else {
2097 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2098 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2099 discardAttachments.begin()));
2100 }
2101 }
2102 }
2103
Chris Daltonb50cc812019-10-14 16:29:21 -06002104 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2105 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2106 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2107 // Handled by use_tiled_rendering().
2108 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2109 GL_CALL(EndTiling(preserveMask));
2110 }
2111
Chris Dalton674f77a2019-09-30 20:49:39 -06002112 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002113}
2114
csmartdalton29df7602016-08-31 11:55:52 -07002115void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2116 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002117 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002118 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002119 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002120 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002121
egdaniel8dc7c3a2015-04-16 11:22:42 -07002122 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002123 if (!sb) {
2124 // We should only get here if we marked a proxy as requiring a SB. However,
2125 // the SB creation could later fail. Likely clipping is going to go awry now.
2126 return;
2127 }
2128
bsalomon6bc1b5f2015-02-23 09:06:38 -08002129 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002130#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002131 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002132 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002133#else
2134 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002135 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002136 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002137 // changing the clip between stencil passes may or may not
2138 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002139 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002140#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002141 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002142 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002143 value = (1 << (stencilBitCount - 1));
2144 } else {
2145 value = 0;
2146 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002147 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002148 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002149
Greg Danielacd66b42019-05-22 16:29:12 -04002150 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002151 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002152
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002153 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002154 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002155 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002156 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002157}
2158
Brian Salomone05ba5a2019-04-08 11:59:07 -04002159bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002160 GrColorType surfaceColorType, GrColorType dstColorType,
2161 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002162 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002163
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002164 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002165 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002166 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002167 return false;
2168 }
Greg Danielba88ab62019-07-26 09:14:01 -04002169 GrGLenum externalFormat = 0;
2170 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002171 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2172 surfaceColorType,
2173 dstColorType,
2174 &externalFormat,
2175 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002176 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002177 return false;
2178 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002179
Brian Salomon71d9d842016-11-03 13:42:00 -04002180 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002181 if (renderTarget->numSamples() <= 1 ||
2182 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2183 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2184 this->flushRenderTargetNoColorWrites(renderTarget);
2185 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2186 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2187 return false;
2188 } else {
2189 SkASSERT(renderTarget->requiresManualMSAAResolve());
2190 // we don't track the state of the READ FBO ID.
2191 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002192 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002193 } else {
2194 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002195 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002196 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002197 }
2198
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002199 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002200 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002201
Brian Salomona6948702018-06-01 15:33:20 -04002202 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002203 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002204 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002205 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002206 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002207 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002208
Brian Osman1348ed02018-09-12 09:08:39 -04002209 bool reattachStencil = false;
2210 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2211 renderTarget &&
2212 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002213 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002214 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2215 reattachStencil = true;
2216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2217 GR_GL_RENDERBUFFER, 0));
2218 }
2219
Chris Dalton76500e52019-09-05 02:13:05 -06002220 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002221 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002222
2223 if (reattachStencil) {
2224 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2225 renderTarget->renderTargetPriv().getStencilAttachment());
2226 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2227 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2228 }
2229
Brian Salomon26de56e2019-04-10 12:14:26 -04002230 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002231 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002232 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2233 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002234
Brian Salomone05ba5a2019-04-08 11:59:07 -04002235 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002236 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002237 }
2238 return true;
2239}
2240
2241bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002242 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2243 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002244 SkASSERT(surface);
2245
Brian Salomonb28cb682019-07-26 12:48:47 -04002246 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002247
Brian Salomon26de56e2019-04-10 12:14:26 -04002248 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2249 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002250
Brian Salomon1047a492019-07-02 12:25:21 -04002251 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002252 rowPixelWidth = width;
2253 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002254 SkASSERT(!(rowBytes % bytesPerPixel));
2255 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002256 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002257 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2258 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002259}
2260
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002261GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002262 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002263 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002264 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002265 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002266 if (!fCachedOpsRenderPass) {
2267 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002268 }
2269
Chris Daltonb50cc812019-10-14 16:29:21 -06002270 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002271 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002272}
2273
Brian Salomon1fabd512018-02-09 09:54:25 -05002274void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002275 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002276 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002277 this->didWriteToSurface(target, origin, &bounds);
2278}
bsalomon6ba6fa12015-03-04 11:57:37 -08002279
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002280void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2281 this->flushRenderTargetNoColorWrites(target);
2282 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002283}
2284
Brian Osman9aa30c62018-07-02 15:21:46 -04002285void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002286 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002287 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002288 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002289 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002290#ifdef SK_DEBUG
2291 // don't do this check in Chromium -- this is causing
2292 // lots of repeated command buffer flushes when the compositor is
2293 // rendering with Ganesh, which is really slow; even too slow for
2294 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002295 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002296 GrGLenum status;
2297 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2298 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2299 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2300 }
bsalomon160f24c2015-03-17 15:55:42 -07002301 }
egdanield803f272015-03-18 13:01:52 -07002302#endif
2303 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002304 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002305 }
2306
brianosman35b784d2016-05-05 11:52:53 -07002307 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002308 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002309 }
Brian Salomon9b553272020-01-24 14:38:37 -05002310
2311 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2312 GrGLint format;
2313 GrGLint type;
2314 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2315 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2316 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2317 }
bsalomon083617b2016-02-12 12:10:14 -08002318}
bsalomona9909122016-01-23 10:41:40 -08002319
brianosman33f6b3f2016-06-02 05:49:21 -07002320void GrGLGpu::flushFramebufferSRGB(bool enable) {
2321 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2322 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2323 fHWSRGBFramebuffer = kYes_TriState;
2324 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2325 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2326 fHWSRGBFramebuffer = kNo_TriState;
2327 }
2328}
2329
Greg Danielacd66b42019-05-22 16:29:12 -04002330void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002331 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002332 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002333 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002334 fHWViewport = viewport;
2335 }
2336}
2337
bsalomon@google.comd302f142011-03-03 13:54:13 +00002338#define SWAP_PER_DRAW 0
2339
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002340#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002341 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002342 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002343 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002344 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002345 void SwapBuf() {
2346 DWORD procID = GetCurrentProcessId();
2347 HWND hwnd = GetTopWindow(GetDesktopWindow());
2348 while(hwnd) {
2349 DWORD wndProcID = 0;
2350 GetWindowThreadProcessId(hwnd, &wndProcID);
2351 if(wndProcID == procID) {
2352 SwapBuffers(GetDC(hwnd));
2353 }
2354 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2355 }
2356 }
2357 #endif
2358#endif
2359
Robert Phillips901aff02019-10-08 12:32:56 -04002360void GrGLGpu::draw(GrRenderTarget* renderTarget,
2361 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002362 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002363 int meshCount) {
2364 this->handleDirtyContext();
2365
Robert Phillips2579de42019-10-09 09:51:59 -04002366 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002367
Robert Phillipsfcaae482019-11-07 10:17:03 -05002368 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002369 return;
2370 }
ethannicholas22793252016-01-30 09:59:10 -08002371
Robert Phillips901aff02019-10-08 12:32:56 -04002372 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2373 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2374
Brian Salomonf7232642018-09-19 08:58:08 -04002375 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002376 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2377 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002378 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002379 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002380
Robert Phillips901aff02019-10-08 12:32:56 -04002381 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002382 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002383 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2384 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002385 }
Robert Phillips901aff02019-10-08 12:32:56 -04002386 if (hasDynamicPrimProcTextures) {
2387 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2388 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2389 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002390 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002391 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002392 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002393 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2394 GL_CALL(Enable(GR_GL_CULL_FACE));
2395 GL_CALL(Disable(GR_GL_CULL_FACE));
2396 }
Brian Salomonf7232642018-09-19 08:58:08 -04002397 meshes[m].sendToGpu(this);
2398 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002399 }
ethannicholas22793252016-01-30 09:59:10 -08002400
bsalomon@google.comd302f142011-03-03 13:54:13 +00002401#if SWAP_PER_DRAW
2402 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002403 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002404 aglSwapBuffers(aglGetCurrentContext());
2405 int set_a_break_pt_here = 9;
2406 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002407 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002408 SwapBuf();
2409 int set_a_break_pt_here = 9;
2410 SwapBuf();
2411 #endif
2412#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002413}
2414
Chris Dalton3809bab2017-06-13 10:55:06 -06002415static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2416 switch (primitiveType) {
2417 case GrPrimitiveType::kTriangles:
2418 return GR_GL_TRIANGLES;
2419 case GrPrimitiveType::kTriangleStrip:
2420 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002421 case GrPrimitiveType::kPoints:
2422 return GR_GL_POINTS;
2423 case GrPrimitiveType::kLines:
2424 return GR_GL_LINES;
2425 case GrPrimitiveType::kLineStrip:
2426 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002427 case GrPrimitiveType::kPatches:
2428 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002429 case GrPrimitiveType::kPath:
2430 SK_ABORT("non-mesh-based GrPrimitiveType");
2431 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002432 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002433 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002434}
2435
Chris Dalton34280e22019-12-20 11:08:25 -07002436void GrGLGpu::sendArrayMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex) {
2437 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002438 if (GR_GL_PATCHES == glPrimType) {
2439 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2440 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002441 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002442 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2443 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002444 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2445 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002446 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002447 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2448 }
2449 fStats.incNumDraws();
2450}
2451
Brian Salomondbf70722019-02-07 11:31:24 -05002452static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2453 size_t baseOffset = baseIndex * sizeof(uint16_t);
2454 if (indexBuffer->isCpuBuffer()) {
2455 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2456 } else {
2457 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2458 }
2459}
2460
Chris Dalton34280e22019-12-20 11:08:25 -07002461void GrGLGpu::sendIndexedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2462 uint16_t minIndexValue, uint16_t maxIndexValue, int baseVertex) {
2463 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002464 if (GR_GL_PATCHES == glPrimType) {
2465 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2466 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002467
Chris Dalton34280e22019-12-20 11:08:25 -07002468 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2469
2470 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2471 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002472
2473 if (this->glCaps().drawRangeElementsSupport()) {
2474 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002475 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002476 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002477 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002478 }
2479 fStats.incNumDraws();
2480}
2481
Chris Dalton34280e22019-12-20 11:08:25 -07002482void GrGLGpu::sendInstancedMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex,
2483 int instanceCount, int baseInstance) {
2484 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002485 if (GR_GL_PATCHES == glPrimType) {
2486 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2487 }
Chris Dalton1b4ad762018-10-04 11:58:09 -06002488 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002489 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002490 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2491 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002492 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
Brian Osman788b9162020-02-07 10:36:46 -05002493 std::min(instanceCount - i, maxInstances)));
Chris Daltoncc604e52017-10-06 16:27:32 -06002494 fStats.incNumDraws();
2495 }
Chris Dalton1d616352017-05-31 12:51:23 -06002496}
2497
Chris Dalton34280e22019-12-20 11:08:25 -07002498void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2499 int baseVertex, int instanceCount, int baseInstance) {
2500 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002501 if (GR_GL_PATCHES == glPrimType) {
2502 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2503 }
Chris Dalton34280e22019-12-20 11:08:25 -07002504 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002505 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2506 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002507 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2508 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002509 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Brian Osman788b9162020-02-07 10:36:46 -05002510 std::min(instanceCount - i, maxInstances)));
Chris Dalton1b4ad762018-10-04 11:58:09 -06002511 fStats.incNumDraws();
2512 }
Chris Dalton1d616352017-05-31 12:51:23 -06002513}
2514
Chris Dalton16a33c62019-09-24 22:19:17 -06002515void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2516 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002517 // Some extensions automatically resolves the texture when it is read.
2518 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2519
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002520 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002521 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2522 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2523 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2524 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002525
Chris Daltonc139d082019-09-26 14:04:24 -06002526 // make sure we go through flushRenderTarget() since we've modified
2527 // the bound DRAW FBO ID.
2528 fHWBoundRenderTargetUniqueID.makeInvalid();
2529 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2530 // Apple's extension uses the scissor as the blit bounds.
2531 GrScissorState scissorState;
2532 scissorState.set(resolveRect);
2533 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2534 this->disableWindowRectangles();
2535 GL_CALL(ResolveMultisampleFramebuffer());
2536 } else {
2537 int l, b, r, t;
2538 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2539 this->glCaps().blitFramebufferSupportFlags()) {
2540 l = 0;
2541 b = 0;
2542 r = target->width();
2543 t = target->height();
2544 } else {
2545 auto rect = GrNativeRect::MakeRelativeTo(
2546 resolveOrigin, rt->height(), resolveRect);
2547 l = rect.fX;
2548 b = rect.fY;
2549 r = rect.fX + rect.fWidth;
2550 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002551 }
Chris Daltonc139d082019-09-26 14:04:24 -06002552
2553 // BlitFrameBuffer respects the scissor, so disable it.
2554 this->disableScissor();
2555 this->disableWindowRectangles();
2556 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 +00002557 }
2558}
2559
bsalomon@google.com411dad02012-06-05 20:24:20 +00002560namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002561
bsalomon@google.com411dad02012-06-05 20:24:20 +00002562
2563GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002564 static const GrGLenum gTable[kGrStencilOpCount] = {
2565 GR_GL_KEEP, // kKeep
2566 GR_GL_ZERO, // kZero
2567 GR_GL_REPLACE, // kReplace
2568 GR_GL_INVERT, // kInvert
2569 GR_GL_INCR_WRAP, // kIncWrap
2570 GR_GL_DECR_WRAP, // kDecWrap
2571 GR_GL_INCR, // kIncClamp
2572 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002573 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002574 static_assert(0 == (int)GrStencilOp::kKeep);
2575 static_assert(1 == (int)GrStencilOp::kZero);
2576 static_assert(2 == (int)GrStencilOp::kReplace);
2577 static_assert(3 == (int)GrStencilOp::kInvert);
2578 static_assert(4 == (int)GrStencilOp::kIncWrap);
2579 static_assert(5 == (int)GrStencilOp::kDecWrap);
2580 static_assert(6 == (int)GrStencilOp::kIncClamp);
2581 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002582 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2583 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002584}
2585
2586void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002587 const GrStencilSettings::Face& face,
2588 GrGLenum glFace) {
2589 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2590 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2591 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002592
cdalton93a379b2016-05-11 13:58:08 -07002593 GrGLint ref = face.fRef;
2594 GrGLint mask = face.fTestMask;
2595 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002596
2597 if (GR_GL_FRONT_AND_BACK == glFace) {
2598 // we call the combined func just in case separate stencil is not
2599 // supported.
2600 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2601 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002602 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002603 } else {
2604 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2605 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002606 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002607 }
2608}
2609}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002610
Chris Dalton71713f62019-04-18 12:41:03 -06002611void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002612 if (stencilSettings.isDisabled()) {
2613 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002614 } else if (fHWStencilSettings != stencilSettings ||
2615 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002616 if (kYes_TriState != fHWStencilTestEnabled) {
2617 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002618
csmartdaltonc7d85332016-10-26 10:13:46 -07002619 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002620 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002621 if (!stencilSettings.isTwoSided()) {
2622 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2623 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002624 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002625 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2626 GR_GL_FRONT);
2627 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2628 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002629 }
joshualitta58fe352014-10-27 08:39:00 -07002630 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002631 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002632 }
2633}
2634
csmartdaltonc7d85332016-10-26 10:13:46 -07002635void GrGLGpu::disableStencil() {
2636 if (kNo_TriState != fHWStencilTestEnabled) {
2637 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002638
csmartdaltonc7d85332016-10-26 10:13:46 -07002639 fHWStencilTestEnabled = kNo_TriState;
2640 fHWStencilSettings.invalidate();
2641 }
2642}
2643
Chris Dalton4c56b032019-03-04 12:28:42 -07002644void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002645 // rt is only optional if useHWAA is false.
2646 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002647#ifdef SK_DEBUG
2648 if (useHWAA && rt->numSamples() <= 1) {
2649 SkASSERT(this->caps()->mixedSamplesSupport());
2650 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2651 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2652 }
2653#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002654
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002655 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002656 if (useHWAA) {
2657 if (kYes_TriState != fMSAAEnabled) {
2658 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2659 fMSAAEnabled = kYes_TriState;
2660 }
2661 } else {
2662 if (kNo_TriState != fMSAAEnabled) {
2663 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2664 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002665 }
2666 }
2667 }
2668}
2669
Chris Daltonce425af2019-12-16 10:39:03 -07002670void GrGLGpu::flushConservativeRasterState(bool enabled) {
2671 if (this->caps()->conservativeRasterSupport()) {
2672 if (enabled) {
2673 if (kYes_TriState != fHWConservativeRasterEnabled) {
2674 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2675 fHWConservativeRasterEnabled = kYes_TriState;
2676 }
2677 } else {
2678 if (kNo_TriState != fHWConservativeRasterEnabled) {
2679 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2680 fHWConservativeRasterEnabled = kNo_TriState;
2681 }
2682 }
2683 }
2684}
2685
Chris Dalton1215cda2019-12-17 21:44:04 -07002686void GrGLGpu::flushWireframeState(bool enabled) {
2687 if (this->caps()->wireframeSupport()) {
2688 if (this->caps()->wireframeMode() || enabled) {
2689 if (kYes_TriState != fHWWireframeEnabled) {
2690 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2691 fHWWireframeEnabled = kYes_TriState;
2692 }
2693 } else {
2694 if (kNo_TriState != fHWWireframeEnabled) {
2695 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2696 fHWWireframeEnabled = kNo_TriState;
2697 }
2698 }
2699 }
2700}
2701
Chris Daltonbbb3f642019-07-24 12:25:08 -04002702void GrGLGpu::flushBlendAndColorWrite(
2703 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2704 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2705 // We need to work around a driver bug by using a blend state that preserves the dst color,
2706 // rather than disabling color writes.
2707 GrXferProcessor::BlendInfo preserveDstBlend;
2708 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2709 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2710 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2711 return;
2712 }
bsalomonf7cc8772015-05-11 11:21:14 -07002713
cdalton8917d622015-05-06 13:40:21 -07002714 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002715 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2716 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002717
2718 // Any optimization to disable blending should have already been applied and
2719 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002720 bool blendOff =
2721 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2722 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2723 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002724
egdanielb414f252014-07-29 13:15:47 -07002725 if (blendOff) {
2726 if (kNo_TriState != fHWBlendState.fEnabled) {
2727 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002728
2729 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2730 // https://code.google.com/p/skia/issues/detail?id=3943
2731 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2732 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2733 SkASSERT(this->caps()->advancedBlendEquationSupport());
2734 // Set to any basic blending equation.
2735 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2736 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2737 fHWBlendState.fEquation = blend_equation;
2738 }
2739
egdanielb414f252014-07-29 13:15:47 -07002740 fHWBlendState.fEnabled = kNo_TriState;
2741 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002742 } else {
2743 if (kYes_TriState != fHWBlendState.fEnabled) {
2744 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002745
Chris Daltonbbb3f642019-07-24 12:25:08 -04002746 fHWBlendState.fEnabled = kYes_TriState;
2747 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002748
Chris Daltonbbb3f642019-07-24 12:25:08 -04002749 if (fHWBlendState.fEquation != equation) {
2750 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2751 fHWBlendState.fEquation = equation;
2752 }
cdalton8917d622015-05-06 13:40:21 -07002753
Chris Daltonbbb3f642019-07-24 12:25:08 -04002754 if (GrBlendEquationIsAdvanced(equation)) {
2755 SkASSERT(this->caps()->advancedBlendEquationSupport());
2756 // Advanced equations have no other blend state.
2757 return;
2758 }
cdalton8917d622015-05-06 13:40:21 -07002759
Chris Daltonbbb3f642019-07-24 12:25:08 -04002760 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2761 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2762 gXfermodeCoeff2Blend[dstCoeff]));
2763 fHWBlendState.fSrcCoeff = srcCoeff;
2764 fHWBlendState.fDstCoeff = dstCoeff;
2765 }
cdalton8917d622015-05-06 13:40:21 -07002766
Chris Daltonbbb3f642019-07-24 12:25:08 -04002767 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2768 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2769 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2770 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2771 fHWBlendState.fConstColor = blendConst;
2772 fHWBlendState.fConstColorValid = true;
2773 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002774 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002775 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002776
2777 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002778}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002779
Brian Salomondc829942018-10-23 16:07:24 -04002780static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002781 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002782 switch (swizzle[i]) {
2783 case 'r': glValues[i] = GR_GL_RED; break;
2784 case 'g': glValues[i] = GR_GL_GREEN; break;
2785 case 'b': glValues[i] = GR_GL_BLUE; break;
2786 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002787 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002788 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002789 default: SK_ABORT("Unsupported component");
2790 }
Brian Salomon327955b2018-10-22 15:39:22 +00002791 }
2792}
2793
Greg Daniel2c3398d2019-06-19 11:58:01 -04002794void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2795 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002796 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002797
reed856e9d92015-09-30 12:21:45 -07002798#ifdef SK_DEBUG
2799 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002800 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002801 const int w = texture->width();
2802 const int h = texture->height();
2803 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2804 }
2805 }
2806#endif
2807
Robert Phillips294870f2016-11-11 12:38:40 -05002808 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002809 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002810 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002811 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002812 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002813 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002814 }
2815
Brian Salomondc829942018-10-23 16:07:24 -04002816 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002817 if (!this->caps()->mipMapSupport() ||
2818 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002819 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002820 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002821 }
bsalomonefd7d452014-10-23 14:17:46 -07002822
brianosman33f6b3f2016-06-02 05:49:21 -07002823#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002824 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002825 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002826 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002827 }
2828#endif
2829
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 auto timestamp = texture->parameters()->resetTimestamp();
2831 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002832
Brian Salomone2826ab2019-06-04 15:58:31 -04002833 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2834 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002835 if (fSamplerObjectCache) {
2836 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2837 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002838 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2839 texture->parameters()->samplerOverriddenState();
2840 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002841
Brian Salomone2826ab2019-06-04 15:58:31 -04002842 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2843 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002844
Brian Salomone2826ab2019-06-04 15:58:31 -04002845 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2846 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002847
2848 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002849 newSamplerState.fMinLOD = -1000.f;
2850 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002851
Brian Salomone2826ab2019-06-04 15:58:31 -04002852 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002853 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002854 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002855 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002856 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002857 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002858 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002859 }
Mike Klein1bccae52018-10-19 11:38:44 +00002860 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002861 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002862 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002863 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002864 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002865 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002866 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002867 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002868 }
2869 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002870 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002871 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002872 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002873 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002874 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002875 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002876 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002877 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002878 if (this->glCaps().clampToBorderSupport()) {
2879 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002880 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002881 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002882 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2883 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002884 }
2885 }
Brian Salomondc829942018-10-23 16:07:24 -04002886 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002887 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2888 newNonsamplerState.fBaseMipMapLevel = 0;
2889 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002890
Brian Salomone2826ab2019-06-04 15:58:31 -04002891 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2892 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002893 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002894 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2895 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002896 GrGLenum glValues[4];
2897 get_gl_swizzle_values(swizzle, glValues);
2898 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002899 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002900 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002901 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2902 reinterpret_cast<const GrGLint*>(glValues)));
2903 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002904 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2905 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2906 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2907 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2908 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002909 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002910 }
2911 }
Brian Salomondc829942018-10-23 16:07:24 -04002912 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002913 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2914 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2915 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002916 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002917 this->setTextureUnit(unitIdx);
2918 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002919 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002920 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002921 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002922 this->setTextureUnit(unitIdx);
2923 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002924 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002925 }
Brian Salomon327955b2018-10-22 15:39:22 +00002926 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002927 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2928 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002929}
2930
Brian Salomon1f05d452019-02-08 12:33:08 -05002931void GrGLGpu::onResetTextureBindings() {
2932 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2933 GR_GL_TEXTURE_EXTERNAL};
2934 for (int i = 0; i < this->numTextureUnits(); ++i) {
2935 this->setTextureUnit(i);
2936 for (auto target : kTargets) {
2937 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2938 GL_CALL(BindTexture(target, 0));
2939 }
2940 }
2941 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2942 }
2943}
2944
Chris Dalton5a2f9622019-12-27 14:56:38 -07002945void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2946 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2947 if (fHWPatchVertexCount != count) {
2948 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2949 fHWPatchVertexCount = count;
2950 }
2951}
2952
egdaniel080e6732014-12-22 07:35:52 -08002953void GrGLGpu::flushColorWrite(bool writeColor) {
2954 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002955 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002956 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2957 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002958 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002959 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002960 } else {
2961 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002962 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002963 fHWWriteToColor = kYes_TriState;
2964 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002965 }
bsalomon3e791242014-12-17 13:43:13 -08002966}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002967
Chris Dalton674f77a2019-09-30 20:49:39 -06002968void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2969 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2970 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2971 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2972 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2973 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2974 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2975 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002976 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2977 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2978 GL_CALL(ClearColor(r, g, b, a));
2979 fHWClearColor[0] = r;
2980 fHWClearColor[1] = g;
2981 fHWClearColor[2] = b;
2982 fHWClearColor[3] = a;
2983 }
2984}
2985
bsalomon861e1032014-12-16 07:33:49 -08002986void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002987 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002988 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002989 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002990 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002991 }
2992}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002993
Brian Salomond978b902019-02-07 15:09:18 -05002994void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002995 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002996 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002997 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2998 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2999 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003000 }
Brian Salomond978b902019-02-07 15:09:18 -05003001 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
3002 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05003003 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05003004 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003005}
3006
Brian Salomone5e7eb12016-10-14 16:18:33 -04003007// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04003008static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3009 const GrSurface* src,
3010 const SkIRect& srcRect,
3011 const SkIPoint& dstPoint,
3012 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003013 int dstSampleCnt = 0;
3014 int srcSampleCnt = 0;
3015 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003016 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003017 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003018 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003019 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003020 }
3021 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3022 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3023
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003024 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3025 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3026
Brian Salomone5e7eb12016-10-14 16:18:33 -04003027 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003028 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003029
Greg Daniel46cfbc62019-06-07 11:43:30 -04003030 GrTextureType dstTexType;
3031 GrTextureType* dstTexTypePtr = nullptr;
3032 GrTextureType srcTexType;
3033 GrTextureType* srcTexTypePtr = nullptr;
3034 if (dstTex) {
3035 dstTexType = dstTex->texturePriv().textureType();
3036 dstTexTypePtr = &dstTexType;
3037 }
3038 if (srcTex) {
3039 srcTexType = srcTex->texturePriv().textureType();
3040 srcTexTypePtr = &srcTexType;
3041 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003042
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003043 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3044 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003045 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003046}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003047
Brian Osmana9c8a052018-01-19 10:31:56 -05003048static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3049 // A RT has a separate MSAA renderbuffer if:
3050 // 1) It's multisampled
3051 // 2) We're using an extension with separate MSAA renderbuffers
3052 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003053 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003054}
3055
Greg Daniel46cfbc62019-06-07 11:43:30 -04003056static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003057 const GrGLCaps& caps) {
3058
bsalomon@google.comeb851172013-04-15 13:51:00 +00003059 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003060 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003061 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003062 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003063
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003064 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3065 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3066
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003067 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3068 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3069
Greg Daniel46cfbc62019-06-07 11:43:30 -04003070 GrTextureType dstTexType;
3071 GrTextureType* dstTexTypePtr = nullptr;
3072 GrTextureType srcTexType;
3073 GrTextureType* srcTexTypePtr = nullptr;
3074 if (dstTex) {
3075 dstTexType = dstTex->texturePriv().textureType();
3076 dstTexTypePtr = &dstTexType;
3077 }
3078 if (srcTex) {
3079 srcTexType = srcTex->texturePriv().textureType();
3080 srcTexTypePtr = &srcTexType;
3081 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003082
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003083 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3084 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003085}
3086
Greg Danielacd66b42019-05-22 16:29:12 -04003087// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003088void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003089 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003090 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003091 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003092 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003093 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3094 GrGLuint texID = texture->textureID();
3095 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003096 GrGLuint* tempFBOID;
3097 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003098
egdanield803f272015-03-18 13:01:52 -07003099 if (0 == *tempFBOID) {
3100 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003101 }
3102
Adrienne Walker4ee88512018-05-17 11:37:14 -07003103 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003104 GR_GL_CALL(
3105 this->glInterface(),
3106 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3107 if (mipLevel == 0) {
3108 texture->baseLevelWasBoundToFBO();
3109 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003110 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003111 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003112 }
egdaniel0f5f9672015-02-03 11:10:51 -08003113}
3114
Brian Salomond2a8ae22019-09-10 16:03:59 -04003115void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003116 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003117 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003118 SkASSERT(surface->asTexture());
3119 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3120 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3121 GR_GL_COLOR_ATTACHMENT0,
3122 textureTarget,
3123 0,
3124 0));
3125 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003126}
3127
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003128void GrGLGpu::onFBOChanged() {
3129 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3130 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3131 GL_CALL(Flush());
3132 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003133#ifdef SK_DEBUG
3134 if (fIsExecutingCommandBuffer_DebugOnly) {
3135 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3136 "This will severely hurt performance.\n");
3137 }
3138#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003139}
3140
Adrienne Walker4ee88512018-05-17 11:37:14 -07003141void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3142 fStats.incRenderTargetBinds();
3143 GL_CALL(BindFramebuffer(target, fboid));
3144 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3145 fBoundDrawFramebuffer = fboid;
3146 }
3147
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003148 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3149 // The driver forgets the correct scissor when modifying the FBO binding.
3150 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003151 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003152 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003153 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003154 }
3155
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003156 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003157}
3158
Adrienne Walker4ee88512018-05-17 11:37:14 -07003159void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3160 if (fboid == fBoundDrawFramebuffer &&
3161 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3162 // This workaround only applies to deleting currently bound framebuffers
3163 // on Adreno 420. Because this is a somewhat rare case, instead of
3164 // tracking all the attachments of every framebuffer instead just always
3165 // unbind all attachments.
3166 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3167 GR_GL_RENDERBUFFER, 0));
3168 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3169 GR_GL_RENDERBUFFER, 0));
3170 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3171 GR_GL_RENDERBUFFER, 0));
3172 }
3173
3174 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003175
3176 // Deleting the currently bound framebuffer rebinds to 0.
3177 if (fboid == fBoundDrawFramebuffer) {
3178 this->onFBOChanged();
3179 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003180}
3181
Greg Daniel46cfbc62019-06-07 11:43:30 -04003182bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003183 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003184 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3185 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3186 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003187 auto dstFormat = dst->backendFormat().asGLFormat();
3188 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003189 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003190 return true;
3191 }
3192 }
cblume61214052016-01-26 09:10:48 -08003193
Greg Daniel46cfbc62019-06-07 11:43:30 -04003194 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3195 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003196 return true;
3197 }
3198
Greg Daniel46cfbc62019-06-07 11:43:30 -04003199 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3200 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003201 }
3202
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003203 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003204 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003205 return true;
3206 }
bsalomon083617b2016-02-12 12:10:14 -08003207 }
3208
bsalomon6df86402015-06-01 10:41:49 -07003209 return false;
3210}
3211
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003212bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003213 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003214
3215 int progIdx = TextureToCopyProgramIdx(srcTex);
3216 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3217 GrSLType samplerType =
3218 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3219
3220 if (!fCopyProgramArrayBuffer) {
3221 static const GrGLfloat vdata[] = {
3222 0, 0,
3223 0, 1,
3224 1, 0,
3225 1, 1
3226 };
3227 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3228 kStatic_GrAccessPattern, vdata);
3229 }
3230 if (!fCopyProgramArrayBuffer) {
3231 return false;
3232 }
3233
3234 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3235 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3236 if (!fCopyPrograms[progIdx].fProgram) {
3237 return false;
3238 }
3239
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003240 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3241 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3242 GrShaderVar::kUniform_TypeModifier);
3243 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3244 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3245 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3246 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3247
Greg Daniel9e3169b2019-06-06 14:38:17 -04003248 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003249 if (shaderCaps->noperspectiveInterpolationSupport()) {
3250 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3251 vshaderTxt.appendf("#extension %s : require\n", extension);
3252 }
3253 vTexCoord.addModifier("noperspective");
3254 }
3255
3256 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3257 vshaderTxt.append(";");
3258 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3259 vshaderTxt.append(";");
3260 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3261 vshaderTxt.append(";");
3262 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3263 vshaderTxt.append(";");
3264
3265 vshaderTxt.append(
3266 "// Copy Program VS\n"
3267 "void main() {"
3268 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3269 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3270 " sk_Position.zw = half2(0, 1);"
3271 "}"
3272 );
3273
Greg Daniel9e3169b2019-06-06 14:38:17 -04003274 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003275 if (shaderCaps->noperspectiveInterpolationSupport()) {
3276 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3277 fshaderTxt.appendf("#extension %s : require\n", extension);
3278 }
3279 }
3280 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3281 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3282 fshaderTxt.append(";");
3283 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3284 fshaderTxt.append(";");
3285 fshaderTxt.appendf(
3286 "// Copy Program FS\n"
3287 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003288 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003289 "}"
3290 );
3291
3292 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3293 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3294 SkSL::Program::Settings settings;
3295 settings.fCaps = shaderCaps;
3296 SkSL::String glsl;
3297 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3298 sksl, settings, &glsl, errorHandler);
3299 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3300 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3301 SkASSERT(program->fInputs.isEmpty());
3302
3303 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3304 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3305 errorHandler);
3306 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3307 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3308 errorHandler);
3309 SkASSERT(program->fInputs.isEmpty());
3310
3311 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3312
3313 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3314 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3315 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3316 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3317 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3318 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3319
3320 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3321
3322 GL_CALL(DeleteShader(vshader));
3323 GL_CALL(DeleteShader(fshader));
3324
3325 return true;
3326}
3327
brianosman33f6b3f2016-06-02 05:49:21 -07003328bool GrGLGpu::createMipmapProgram(int progIdx) {
3329 const bool oddWidth = SkToBool(progIdx & 0x2);
3330 const bool oddHeight = SkToBool(progIdx & 0x1);
3331 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3332
Brian Salomon1edc5b92016-11-29 13:43:46 -05003333 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003334
3335 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3336 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3337 if (!fMipmapPrograms[progIdx].fProgram) {
3338 return false;
3339 }
3340
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003341 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3342 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003343 GrShaderVar::kUniform_TypeModifier);
3344 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3345 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003346 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003347 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003348 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3349 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3350 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3351 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003352 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003353 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003354
Ethan Nicholasfc994162019-06-06 10:04:27 -04003355 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003356 if (shaderCaps->noperspectiveInterpolationSupport()) {
3357 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003358 vshaderTxt.appendf("#extension %s : require\n", extension);
3359 }
3360 vTexCoords[0].addModifier("noperspective");
3361 vTexCoords[1].addModifier("noperspective");
3362 vTexCoords[2].addModifier("noperspective");
3363 vTexCoords[3].addModifier("noperspective");
3364 }
3365
Brian Salomon1edc5b92016-11-29 13:43:46 -05003366 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003367 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003368 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003369 vshaderTxt.append(";");
3370 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003371 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003372 vshaderTxt.append(";");
3373 }
3374
3375 vshaderTxt.append(
3376 "// Mipmap Program VS\n"
3377 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003378 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3379 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003380 );
3381
3382 // Insert texture coordinate computation:
3383 if (oddWidth && oddHeight) {
3384 vshaderTxt.append(
3385 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003386 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3387 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003388 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3389 );
3390 } else if (oddWidth) {
3391 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003392 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3393 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003394 );
3395 } else if (oddHeight) {
3396 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003397 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3398 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003399 );
3400 } else {
3401 vshaderTxt.append(
3402 " v_texCoord0 = a_vertex.xy;"
3403 );
3404 }
3405
3406 vshaderTxt.append("}");
3407
Ethan Nicholasfc994162019-06-06 10:04:27 -04003408 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003409 if (shaderCaps->noperspectiveInterpolationSupport()) {
3410 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003411 fshaderTxt.appendf("#extension %s : require\n", extension);
3412 }
3413 }
brianosman33f6b3f2016-06-02 05:49:21 -07003414 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003415 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003416 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003417 fshaderTxt.append(";");
3418 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003419 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003420 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003421 fshaderTxt.append(
3422 "// Mipmap Program FS\n"
3423 "void main() {"
3424 );
3425
3426 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003427 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003428 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3429 " sample(u_texture, v_texCoord1) + "
3430 " sample(u_texture, v_texCoord2) + "
3431 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003432 );
3433 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003434 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003435 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3436 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003437 );
3438 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003439 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003440 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003441 );
3442 }
3443
3444 fshaderTxt.append("}");
3445
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003446 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003447 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003448 SkSL::Program::Settings settings;
3449 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003450 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003451 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003452 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003453 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003454 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003455 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003456
Brian Osman6c431d52019-04-15 16:31:54 -04003457 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003458 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3459 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003460 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003461 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003462 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003463 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003464
3465 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3466
3467 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3468 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3469 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3470 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3471
3472 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3473
3474 GL_CALL(DeleteShader(vshader));
3475 GL_CALL(DeleteShader(fshader));
3476
3477 return true;
3478}
3479
Greg Daniel46cfbc62019-06-07 11:43:30 -04003480bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003481 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003482 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3483 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3484 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3485 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003486 return false;
3487 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003488 int progIdx = TextureToCopyProgramIdx(srcTex);
3489 if (!dstRT) {
3490 SkASSERT(dstTex);
3491 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3492 return false;
3493 }
3494 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003495 if (!fCopyPrograms[progIdx].fProgram) {
3496 if (!this->createCopyProgram(srcTex)) {
3497 SkDebugf("Failed to create copy program.\n");
3498 return false;
3499 }
3500 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003501 int w = srcRect.width();
3502 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003503 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003504 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003505 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003506 this->flushViewport(dst->width(), dst->height());
3507 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003508 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003509 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003510 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003511 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3512 attribs->enableVertexArrays(this, 1);
3513 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3514 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003515 // dst rect edges in NDC (-1 to 1)
3516 int dw = dst->width();
3517 int dh = dst->height();
3518 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3519 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3520 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3521 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003522 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3523 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3524 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3525 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3526 int sw = src->width();
3527 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003528 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3529 // src rect edges in normalized texture space (0 to 1)
3530 sx0 /= sw;
3531 sx1 /= sw;
3532 sy0 /= sh;
3533 sy1 /= sh;
3534 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003535 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3536 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3537 sx1 - sx0, sy1 - sy0, sx0, sy0));
3538 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003539 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003540 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003541 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003542 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003543 this->disableScissor();
3544 this->disableWindowRectangles();
3545 this->disableStencil();
3546 if (this->glCaps().srgbWriteControl()) {
3547 this->flushFramebufferSRGB(true);
3548 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003549 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003550 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003551 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3552 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003553 return true;
3554}
3555
Greg Daniel46cfbc62019-06-07 11:43:30 -04003556void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003557 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003558 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003559 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003560 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003561 SkASSERT(dstTex);
3562 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003563 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003564
Brian Salomond978b902019-02-07 15:09:18 -05003565 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003566 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003567 dstPoint.fX, dstPoint.fY,
3568 srcRect.fLeft, srcRect.fTop,
3569 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003570 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003571 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3572 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003573 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3574 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003575}
3576
Greg Daniel46cfbc62019-06-07 11:43:30 -04003577bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003578 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003579 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003580 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3581 srcRect.width(), srcRect.height());
3582 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003583 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003584 return false;
mtklein404b3b22015-05-18 09:29:10 -07003585 }
bsalomon5df6fee2015-05-18 06:26:15 -07003586 }
bsalomon6df86402015-06-01 10:41:49 -07003587
Brian Salomond2a8ae22019-09-10 16:03:59 -04003588 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3589 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003590 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003591 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003592
3593 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003594 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003595 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003596
Greg Daniel46cfbc62019-06-07 11:43:30 -04003597 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3598 srcRect.fTop,
3599 srcRect.fRight,
3600 srcRect.fBottom,
3601 dstRect.fLeft,
3602 dstRect.fTop,
3603 dstRect.fRight,
3604 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003605 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003606 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3607 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003608
3609 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3610 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003611 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003612}
3613
Brian Salomon930f9392018-06-20 16:25:26 -04003614bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3615 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003616 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003617 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003618 return false;
3619 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003620 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003621 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3622 // Uses draw calls to do a series of downsample operations to successive mips.
3623
3624 // The manual approach requires the ability to limit which level we're sampling and that the
3625 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003626 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003627 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003628 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003629 GL_CALL(GenerateMipmap(glTex->target()));
3630 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003631 }
3632
brianosman33f6b3f2016-06-02 05:49:21 -07003633 int width = texture->width();
3634 int height = texture->height();
3635 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003636 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003637
3638 // Create (if necessary), then bind temporary FBO:
3639 if (0 == fTempDstFBOID) {
3640 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3641 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003642 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003643 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003644
3645 // Bind the texture, to get things configured for filtering.
3646 // We'll be changing our base level further below:
3647 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003648 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003649 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003650
3651 // Vertex data:
3652 if (!fMipmapProgramArrayBuffer) {
3653 static const GrGLfloat vdata[] = {
3654 0, 0,
3655 0, 1,
3656 1, 0,
3657 1, 1
3658 };
Brian Salomonae64c192019-02-05 09:41:37 -05003659 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003660 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003661 }
3662 if (!fMipmapProgramArrayBuffer) {
3663 return false;
3664 }
3665
3666 fHWVertexArrayState.setVertexArrayID(this, 0);
3667
3668 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003669 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003670 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003671 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003672
3673 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003674 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003675 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003676 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003677 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003678 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003679
3680 // Do all the blits:
3681 width = texture->width();
3682 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003683
brianosman33f6b3f2016-06-02 05:49:21 -07003684 for (GrGLint level = 1; level < levelCount; ++level) {
3685 // Get and bind the program for this particular downsample (filter shape can vary):
3686 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3687 if (!fMipmapPrograms[progIdx].fProgram) {
3688 if (!this->createMipmapProgram(progIdx)) {
3689 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003690 // Invalidate all params to cover base level change in a previous iteration.
3691 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003692 return false;
3693 }
3694 }
Brian Salomon802cb312018-06-08 18:05:20 -04003695 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003696
3697 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3698 const float invWidth = 1.0f / width;
3699 const float invHeight = 1.0f / height;
3700 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3701 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3702 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3703
3704 // Only sample from previous mip
3705 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3706
Brian Salomon930f9392018-06-20 16:25:26 -04003707 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3708 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003709
Brian Osman788b9162020-02-07 10:36:46 -05003710 width = std::max(1, width / 2);
3711 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003712 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003713
3714 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3715 }
3716
3717 // Unbind:
3718 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3719 GR_GL_TEXTURE_2D, 0, 0));
3720
Brian Salomon930f9392018-06-20 16:25:26 -04003721 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003722 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3723 // We drew the 2nd to last level into the last level.
3724 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3725 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003726
brianosman33f6b3f2016-06-02 05:49:21 -07003727 return true;
3728}
3729
Chris Daltond7291ba2019-03-07 14:17:03 -07003730void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003731 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3732 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003733
3734 int effectiveSampleCnt;
3735 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003736 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003737
3738 sampleLocations->reset(effectiveSampleCnt);
3739 for (int i = 0; i < effectiveSampleCnt; ++i) {
3740 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3741 }
3742}
3743
cdalton231c5fd2015-05-13 12:35:36 -07003744void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003745 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003746 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003747 case kTexture_GrXferBarrierType: {
3748 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003749 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003750 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3751 // The render target uses separate storage so no need for glTextureBarrier.
3752 // FIXME: The render target will resolve automatically when its texture is bound,
3753 // but we could resolve only the bounds that will be read if we do it here instead.
3754 return;
3755 }
cdalton9954bc32015-04-29 14:17:00 -07003756 SkASSERT(this->caps()->textureBarrierSupport());
3757 GL_CALL(TextureBarrier());
3758 return;
cdalton231c5fd2015-05-13 12:35:36 -07003759 }
cdalton8917d622015-05-06 13:40:21 -07003760 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003761 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003762 this->caps()->blendEquationSupport());
3763 GL_CALL(BlendBarrier());
3764 return;
bsalomoncb02b382015-08-12 11:14:50 -07003765 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003766 }
3767}
3768
Chris Daltone1196c52019-12-28 14:31:09 -07003769void GrGLGpu::insertManualFramebufferBarrier() {
3770 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3771 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3772}
3773
Brian Salomon85c3d682019-11-04 15:04:54 -05003774GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003775 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003776 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003777 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003778 GrProtected isProtected,
3779 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003780 // We don't support protected textures in GL.
3781 if (isProtected == GrProtected::kYes) {
3782 return {};
3783 }
3784
Brian Salomon8a375832018-03-14 10:21:40 -04003785 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003786
Brian Salomond4764a12019-08-08 12:08:24 -04003787 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003788 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003789 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003790 }
3791
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003792 int numMipLevels = 1;
3793 if (mipMapped == GrMipMapped::kYes) {
3794 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3795 }
3796
Robert Phillipsd34691b2019-09-24 13:38:43 -04003797 // Compressed formats go through onCreateCompressedBackendTexture
3798 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3799
Greg Daniel15500642019-06-27 03:05:55 +00003800 GrGLTextureInfo info;
3801 GrGLTextureParameters::SamplerOverriddenState initialState;
3802
Greg Danield51fa2f2020-01-22 16:53:38 -05003803 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003804 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003805 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003806
Robert Phillipsd34691b2019-09-24 13:38:43 -04003807 info.fTarget = GR_GL_TEXTURE_2D;
3808 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003809 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003810 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003811 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003812 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003813
Robert Phillips42716d42019-12-16 12:19:54 -05003814 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003815 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3816 SkTDArray<GrMipLevel> texels;
3817 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003818 texels.append(numMipLevels);
3819 for (int i = 0; i < numMipLevels; ++i) {
3820 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3821 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003822 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3823 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3824 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003825 GL_CALL(DeleteTextures(1, &info.fID));
3826 return {};
3827 }
3828 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3829 // TODO: Unify this with the clear texture code in onCreateTexture().
3830 GrColorType colorType;
3831 GrGLenum externalFormat, externalType;
3832 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3833 &externalType, &colorType);
3834 if (colorType == GrColorType::kUnknown) {
3835 GL_CALL(DeleteTextures(1, &info.fID));
3836 return {};
3837 }
3838
3839 // Make one tight image at the base size and reuse it for smaller levels.
3840 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3841 auto rb = ii.minRowBytes();
3842 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3843 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3844 GL_CALL(DeleteTextures(1, &info.fID));
3845 return {};
3846 }
3847
3848 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3849 SkISize levelDimensions = dimensions;
3850 for (int i = 0; i < numMipLevels; ++i) {
3851 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3852 levelDimensions.height(), externalFormat, externalType,
3853 pixelStorage.get()));
Brian Osman788b9162020-02-07 10:36:46 -05003854 levelDimensions = {std::max(1, levelDimensions.width() /2),
3855 std::max(1, levelDimensions.height()/2)};
Brian Salomon85c3d682019-11-04 15:04:54 -05003856 }
3857 }
3858 // Unbind this texture from the scratch texture unit.
3859 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003860
Brian Salomone2826ab2019-06-04 15:58:31 -04003861 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003862 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003863 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3864 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003865
Brian Salomon85c3d682019-11-04 15:04:54 -05003866 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3867 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003868}
3869
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003870void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003871 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3872
3873 GrGLTextureInfo info;
3874 if (tex.getGLTextureInfo(&info)) {
3875 GL_CALL(DeleteTextures(1, &info.fID));
3876 }
3877}
3878
3879#if GR_TEST_UTILS
3880
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003881bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003882 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003883
Greg Daniel52e16d92018-04-10 09:34:07 -04003884 GrGLTextureInfo info;
3885 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003886 return false;
3887 }
3888
3889 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003890 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003891
3892 return (GR_GL_TRUE == result);
3893}
3894
Brian Salomonf865b052018-03-09 09:01:53 -05003895GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003896 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003897 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3898 return GrBackendRenderTarget(); // invalid
3899 }
Brian Salomon8a375832018-03-14 10:21:40 -04003900 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003901 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003902 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003903 return {};
3904 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003905 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003906 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003907 if (sFormatIdx < 0) {
3908 return {};
3909 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003910 GrGLuint colorID = 0;
3911 GrGLuint stencilID = 0;
3912 auto deleteIDs = [&] {
3913 if (colorID) {
3914 if (useTexture) {
3915 GL_CALL(DeleteTextures(1, &colorID));
3916 } else {
3917 GL_CALL(DeleteRenderbuffers(1, &colorID));
3918 }
Brian Salomonf865b052018-03-09 09:01:53 -05003919 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003920 if (stencilID) {
3921 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003922 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003923 };
3924
3925 if (useTexture) {
3926 GL_CALL(GenTextures(1, &colorID));
3927 } else {
3928 GL_CALL(GenRenderbuffers(1, &colorID));
3929 }
3930 GL_CALL(GenRenderbuffers(1, &stencilID));
3931 if (!stencilID || !colorID) {
3932 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003933 return {};
3934 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003935
Brian Salomonf865b052018-03-09 09:01:53 -05003936 GrGLFramebufferInfo info;
3937 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003938 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003939 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3940 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003941 deleteIDs();
3942 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003943 }
3944
3945 this->invalidateBoundRenderTarget();
3946
Adrienne Walker4ee88512018-05-17 11:37:14 -07003947 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003948 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003949 GrGLTextureParameters::SamplerOverriddenState initialState;
3950 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3951 if (!colorID) {
3952 deleteIDs();
3953 return {};
3954 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003955 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3956 colorID, 0));
3957 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003958 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003959 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3960 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003961 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003962 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3963 GR_GL_RENDERBUFFER, colorID));
3964 }
3965 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003966 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3967 GL_ALLOC_CALL(this->glInterface(),
3968 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3969 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003970 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003971 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3972 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003973 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003974 }
3975
Brian Salomon93348dd2018-08-29 12:56:23 -04003976 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3977 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3978 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3979 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003980 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003981 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003982
Adrienne Walker4ee88512018-05-17 11:37:14 -07003983 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003984 GrGLenum status;
3985 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3986 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003987 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003988 return {};
3989 }
3990 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003991
Greg Daniel108bb232018-07-03 16:18:29 -04003992 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003993 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003994 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003995}
3996
3997void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003998 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003999 GrGLFramebufferInfo info;
4000 if (backendRT.getGLFramebufferInfo(&info)) {
4001 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004002 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004003 }
4004 }
joshualitt8fd844f2015-12-02 13:36:47 -08004005}
4006
Greg Daniel26b50a42018-03-08 09:49:58 -05004007void GrGLGpu::testingOnly_flushGpuAndSync() {
4008 GL_CALL(Finish());
4009}
Brian Salomonf865b052018-03-09 09:01:53 -05004010#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004011
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004012///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004013
cdaltone2e71c22016-04-07 18:13:29 -07004014GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004015 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004016 GrGLAttribArrayState* attribState;
4017
cdaltone2e71c22016-04-07 18:13:29 -07004018 if (gpu->glCaps().isCoreProfile()) {
4019 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004020 GrGLuint arrayID;
4021 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4022 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004023 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004024 }
cdaltone2e71c22016-04-07 18:13:29 -07004025 if (ibuf) {
4026 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004027 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004028 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004029 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004030 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004031 if (ibuf) {
4032 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004033 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004034 } else {
4035 this->setVertexArrayID(gpu, 0);
4036 }
4037 int attrCount = gpu->glCaps().maxVertexAttributes();
4038 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4039 fDefaultVertexArrayAttribState.resize(attrCount);
4040 }
4041 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004042 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004043 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004044}
bsalomone179a912016-01-20 06:18:10 -08004045
Greg Daniel30a35e82019-11-19 14:12:25 -05004046bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004047 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004048 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004049 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004050 // We call finish if the client told us to sync or if we have a finished proc but don't support
4051 // GLsync objects.
4052 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4053 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4054 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004055 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004056 // After a finish everything previously sent to GL is done.
4057 for (const auto& cb : fFinishCallbacks) {
4058 cb.fCallback(cb.fContext);
4059 this->deleteSync(cb.fSync);
4060 }
4061 fFinishCallbacks.clear();
4062 if (info.fFinishedProc) {
4063 info.fFinishedProc(info.fFinishedContext);
4064 }
4065 } else {
4066 if (info.fFinishedProc) {
4067 FinishCallback callback;
4068 callback.fCallback = info.fFinishedProc;
4069 callback.fContext = info.fFinishedContext;
4070 callback.fSync = (GrGLsync)this->insertFence();
4071 fFinishCallbacks.push_back(callback);
4072 GL_CALL(Flush());
4073 } else if (insertedSemaphore) {
4074 // Must call flush after semaphores in case they are waited on another GL context.
4075 GL_CALL(Flush());
4076 }
4077 // See if any previously inserted finish procs are good to go.
4078 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004079 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004080 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004081}
4082
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004083void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4084 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4085 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4086 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004087}
4088
Greg Daniel6be35232017-03-01 17:01:09 -05004089GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004090 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004091 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05004092 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4093 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
4094 GrGLuint fence = 0;
4095 GL_CALL(GenFences(1, &fence));
4096 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
4097 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
4098 } else {
4099 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4100 }
Brian Salomon4dea72a2019-12-18 10:43:10 -05004101 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004102 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004103}
4104
Brian Salomonb0d8b762019-05-06 16:58:22 -04004105bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004106 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4107 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
4108 if (!timeout) {
4109 if (flush) {
4110 GL_CALL(Flush);
4111 }
4112 GrGLboolean result;
4113 GL_CALL_RET(result, TestFence(nvFence));
4114 return result == GR_GL_TRUE;
4115 }
4116 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
4117 // If this really becomes necessary we could poll TestFence().
4118 // FinishFence always flushes so no need to check flush param.
4119 GL_CALL(FinishFence(nvFence));
4120 return true;
4121 } else {
4122 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
4123 GrGLenum result;
4124 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4125 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4126 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04004127}
4128
4129bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4130 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004131}
4132
4133void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004134 this->deleteSync((GrGLsync)fence);
4135}
4136
Greg Daniel301015c2019-11-18 14:06:46 -05004137std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004138 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004139 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004140}
4141
Greg Daniel301015c2019-11-18 14:06:46 -05004142std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4143 const GrBackendSemaphore& semaphore,
4144 GrResourceProvider::SemaphoreWrapType wrapType,
4145 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004146 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004147 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4148}
4149
Greg Daniel301015c2019-11-18 14:06:46 -05004150void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4151 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004152
Greg Daniel48661b82018-01-22 16:11:35 -05004153 GrGLsync sync;
4154 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4155 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004156}
4157
Greg Daniel301015c2019-11-18 14:06:46 -05004158void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4159 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004160
4161 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4162}
4163
Brian Salomonb0d8b762019-05-06 16:58:22 -04004164void GrGLGpu::checkFinishProcs() {
4165 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4166 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4167 /* timeout = */ 0, /* flush = */ false)) {
4168 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4169 this->deleteSync(fFinishCallbacks.front().fSync);
4170 fFinishCallbacks.pop_front();
4171 }
4172}
4173
Greg Daniel6be35232017-03-01 17:01:09 -05004174void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004175 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4176 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4177 GL_CALL(DeleteFences(1, &nvFence));
4178 } else {
4179 GL_CALL(DeleteSync(sync));
4180 }
jvanverth84741b32016-09-30 08:39:02 -07004181}
Brian Osman13dddce2017-05-09 13:19:50 -04004182
Greg Daniel301015c2019-11-18 14:06:46 -05004183std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004184 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004185 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004186 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004187 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004188 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4189 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004190
4191 return semaphore;
4192}
Robert Phillips646e4292017-06-13 12:44:56 -04004193
4194int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004195 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004196 case kTexture2DSampler_GrSLType:
4197 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004198 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004199 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004200 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004201 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004202 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004203 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004204 }
4205}
Brian Osman71a18892017-08-10 10:23:25 -04004206
Kevin Lubickf4def342018-10-04 12:52:50 -04004207#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004208#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004209void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4210 // We are called by the base class, which has already called beginObject(). We choose to nest
4211 // all of our caps information in a named sub-object.
4212 writer->beginObject("GL GPU");
4213
4214 const GrGLubyte* str;
4215 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4216 writer->appendString("GL_VERSION", (const char*)(str));
4217 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4218 writer->appendString("GL_RENDERER", (const char*)(str));
4219 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4220 writer->appendString("GL_VENDOR", (const char*)(str));
4221 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4222 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4223
4224 writer->appendName("extensions");
4225 glInterface()->fExtensions.dumpJSON(writer);
4226
4227 writer->endObject();
4228}
Kevin Lubickf4def342018-10-04 12:52:50 -04004229#endif