blob: d9f6a961a0c6126ba0cf1e2b92d0e4929d7d52e3 [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"
18#include "src/core/SkConvertPixels.h"
19#include "src/core/SkMakeUnique.h"
20#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};
88GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
89GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
91GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
92GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
93GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
94GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
95GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
96GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
97GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
98GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
99GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
100GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
101GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
102GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
103GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
104GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
105GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700106GR_STATIC_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];
bsalomonf7cc8772015-05-11 11:21:14 -0700161 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000162
bsalomon@google.com47059542012-06-06 20:51:20 +0000163 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
164 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
165 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
166 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
167 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
168 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
169 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
170 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
171 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
172 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
173 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
174 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
175 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
176 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000177
bsalomon@google.com47059542012-06-06 20:51:20 +0000178 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
179 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
180 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
181 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000182
183 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700184 GR_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
287 void bindSampler(int unitIdx, const GrSamplerState& state) {
288 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:
336 static int StateToIndex(const GrSamplerState& state) {
337 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;
cdaltone2e71c22016-04-07 18:13:29 -0700396 if (GrGLCaps::kChromium_TransferBufferType == 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 Salomonae64c192019-02-05 09:41:37 -0500408 GR_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
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400555 if (this->caps()->wireframeMode()) {
556 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
557 } else {
558 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
559 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500560#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000561 // Since ES doesn't support glPointSize at all we always use the VS to
562 // set the point size
563 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
564
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000565 }
joshualitt58162332014-08-01 06:44:53 -0700566
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400567 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500568 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700569 // The arm extension requires specifically enabling MSAA fetching per sample.
570 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500571 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700572 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000573 fHWWriteToColor = kUnknown_TriState;
574 // we only ever use lines in hairline mode
575 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700576 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500577
578 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000579 }
edisonn@google.comba669992013-06-28 16:03:21 +0000580
egdanielb414f252014-07-29 13:15:47 -0700581 if (resetBits & kMSAAEnable_GrGLBackendState) {
582 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700583
Chris Dalton6ce447a2019-06-23 18:07:38 -0600584 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800585 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
586 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700587 GL_CALL(CoverageModulation(GR_GL_RGBA));
588 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000590
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000591 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400592 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000593
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500595 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500596 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 }
Brian Salomondc829942018-10-23 16:07:24 -0400598 if (fSamplerObjectCache) {
599 fSamplerObjectCache->invalidateBindings();
600 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000601 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000602
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000603 if (resetBits & kBlend_GrGLBackendState) {
604 fHWBlendState.invalidate();
605 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000606
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 if (resetBits & kView_GrGLBackendState) {
608 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700609 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 fHWViewport.invalidate();
611 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000612
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000613 if (resetBits & kStencil_GrGLBackendState) {
614 fHWStencilSettings.invalidate();
615 fHWStencilTestEnabled = kUnknown_TriState;
616 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000617
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000618 // Vertex
619 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700620 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500621 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
622 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500626 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700627 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000628 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000629
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000630 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700631 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700632 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000633 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000634 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000635
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000636 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000637 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400638 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000639 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
640 }
Brian Salomon1047a492019-07-02 12:25:21 -0400641 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000642 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
643 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000644 if (this->glCaps().packFlipYSupport()) {
645 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
646 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000647 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000648
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 if (resetBits & kProgram_GrGLBackendState) {
650 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400651 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000652 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400653 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000654}
655
Brian Salomonea4ad302019-08-07 13:04:55 -0400656static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
657 const GrGLCaps& caps, GrGLTexture::Desc* desc,
658 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400659 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400660 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400661 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800662 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000663
Brian Salomonea4ad302019-08-07 13:04:55 -0400664 desc->fSize = {backendTex.width(), backendTex.height()};
665 desc->fTarget = info.fTarget;
666 desc->fID = info.fID;
667 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800668
Brian Salomonea4ad302019-08-07 13:04:55 -0400669 if (desc->fFormat == GrGLFormat::kUnknown) {
670 return false;
671 }
672 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400673 if (!caps.shaderCaps()->externalTextureSupport()) {
674 return false;
675 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400676 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
677 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400678 return false;
679 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400680 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400681 return false;
682 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400683 if (backendTex.isProtected()) {
684 // Not supported in GL backend at this time.
685 return false;
686 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400687
688 desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType);
689 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
690
Brian Salomond17f6582017-07-19 18:28:58 -0400691 return true;
692}
693
694sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400695 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400696 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400697 GrGLTexture::Desc desc;
698 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400699 return nullptr;
700 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400701
Brian Salomond17f6582017-07-19 18:28:58 -0400702 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400703 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400704 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400705 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400706 }
bsalomone5286e02016-01-14 09:24:09 -0800707
Greg Daniel177e6952017-10-12 12:27:11 -0400708 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
709 : GrMipMapsStatus::kNotAllocated;
710
Brian Salomonea4ad302019-08-07 13:04:55 -0400711 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400712 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400713 // We don't know what parameters are already set on wrapped textures.
714 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400715 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400716}
717
718sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400719 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400720 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500721 GrWrapOwnership ownership,
722 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400723 const GrGLCaps& caps = this->glCaps();
724
Brian Salomonea4ad302019-08-07 13:04:55 -0400725 GrGLTexture::Desc desc;
726 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800727 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800728 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400729 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
730 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800731
Brian Salomond17f6582017-07-19 18:28:58 -0400732 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400733 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800734 return nullptr;
735 }
bsalomon10528f12015-10-14 12:54:52 -0700736
Brian Osman766fcbb2017-03-13 09:33:09 -0400737 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400738 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400739 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400740 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800741 }
bsalomonb15b4c12014-10-29 12:41:57 -0700742
Robert Phillips0902c982019-07-16 07:47:56 -0400743
Greg Daniel6fa62e22019-08-07 15:52:37 -0400744 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
745 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000746
Brian Salomonea4ad302019-08-07 13:04:55 -0400747 GrGLRenderTarget::IDs rtIDs;
748 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400749 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000750 }
Greg Daniel177e6952017-10-12 12:27:11 -0400751
752 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
753 : GrMipMapsStatus::kNotAllocated;
754
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500755 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400756 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400757 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400758 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400759 // We don't know what parameters are already set on wrapped textures.
760 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400761 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000762}
763
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400764sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
765 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400766 GrGLFramebufferInfo info;
767 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000768 return nullptr;
769 }
770
Brian Salomone8a766b2019-07-19 14:24:36 -0400771 if (backendRT.isProtected()) {
772 // Not supported in GL at this time.
773 return nullptr;
774 }
775
Brian Salomond4764a12019-08-08 12:08:24 -0400776 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400777 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
778 return nullptr;
779 }
780
Brian Salomonea4ad302019-08-07 13:04:55 -0400781 GrGLRenderTarget::IDs rtIDs;
782 rtIDs.fRTFBOID = info.fFBOID;
783 rtIDs.fMSColorRenderbufferID = 0;
784 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
785 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000786
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400787 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
788 grColorType);
Brian Salomond4764a12019-08-08 12:08:24 -0400789 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400790
Brian Salomonea4ad302019-08-07 13:04:55 -0400791 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400792 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700793
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400795 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000796}
797
Greg Daniel7ef28f32017-04-20 16:41:55 +0000798sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400799 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400800 GrColorType colorType) {
801 GrGLTexture::Desc desc;
802 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
803 // with a texture rectangle,we assume the necessary support exists.
804 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800805 return nullptr;
806 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400807
808 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
809 return nullptr;
810 }
811
812 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400813 GrGLRenderTarget::IDs rtIDs;
814 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800815 return nullptr;
816 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400817 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount,
818 rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800819}
820
Brian Salomonc320b152018-02-20 14:05:36 -0500821static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700822 if (!glTex) {
823 return false;
824 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000825
bsalomone5286e02016-01-14 09:24:09 -0800826 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
827 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800828 return false;
829 }
830
jvanverth17aa0472016-01-05 10:41:27 -0800831 return true;
832}
833
Brian Salomona9b04b92018-06-01 15:04:28 -0400834bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400835 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400836 const GrMipLevel texels[], int mipLevelCount,
837 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500838 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800839
Brian Salomonc320b152018-02-20 14:05:36 -0500840 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800841 return false;
842 }
843
Brian Salomond978b902019-02-07 15:09:18 -0500844 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000845
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400846 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400847 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400848 glTex->target(), left, top, width, height, srcColorType, texels,
849 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000850}
851
Brian Salomone05ba5a2019-04-08 11:59:07 -0400852bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400853 GrColorType textureColorType, GrColorType bufferColorType,
854 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400855 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400856
Jim Van Verth1676cb92019-01-15 13:24:45 -0500857 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400858 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500859
Brian Salomonc320b152018-02-20 14:05:36 -0500860 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400861 return false;
862 }
863
Hal Canaryc36f7e72018-06-18 15:50:09 -0400864 static_assert(sizeof(int) == sizeof(int32_t), "");
865 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400866 return false;
867 }
868
Brian Salomond978b902019-02-07 15:09:18 -0500869 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400870
871 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500872 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400873 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500874 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400875
Greg Daniel660cc992017-06-26 14:55:05 -0400876 SkDEBUGCODE(
877 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
878 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
879 SkASSERT(bounds.contains(subRect));
880 )
881
Brian Salomonb28cb682019-07-26 12:48:47 -0400882 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400883 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400884 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700885 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400886 return false;
887 }
888
889 bool restoreGLRowLength = false;
890 if (trimRowBytes != rowBytes) {
891 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400892 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400893 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
894 restoreGLRowLength = true;
895 }
896
Greg Danielba88ab62019-07-26 09:14:01 -0400897 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400898 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400899 GrGLenum externalFormat = 0;
900 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400901 this->glCaps().getTexSubImageExternalFormatAndType(
902 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400903 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400904 return false;
905 }
906
Brian Salomon8cbf6622019-07-30 11:03:14 -0400907 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400908 GL_CALL(TexSubImage2D(glTex->target(),
909 0,
910 left, top,
911 width,
912 height,
913 externalFormat, externalType,
914 pixels));
915
916 if (restoreGLRowLength) {
917 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
918 }
919
920 return true;
921}
922
Brian Salomon26de56e2019-04-10 12:14:26 -0400923bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400924 GrColorType surfaceColorType, GrColorType dstColorType,
925 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400926 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
927 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400928 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400929 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
930 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400931}
932
Brian Osman9b560242017-09-05 15:34:52 -0400933void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500934 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
935 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
936 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
937 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400938 }
Brian Osman9b560242017-09-05 15:34:52 -0400939}
940
Brian Salomond2a8ae22019-09-10 16:03:59 -0400941bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
942 int texHeight, GrGLenum target, int left, int top, int width,
943 int height, GrColorType srcColorType, const GrMipLevel texels[],
944 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500945 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400946 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947
Greg Daniel7bfc9132019-08-14 14:23:53 -0400948 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400949 SkDEBUGCODE(
950 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
951 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
952 SkASSERT(bounds.contains(subRect));
953 )
Robert Phillips590533f2017-07-11 14:22:35 -0400954 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400955 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800956
Brian Osman9b560242017-09-05 15:34:52 -0400957 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400958
cblume55f2d2d2016-02-26 13:20:48 -0800959 const GrGLInterface* interface = this->glInterface();
960 const GrGLCaps& caps = this->glCaps();
961
Brian Salomonf77c1462019-08-01 15:19:29 -0400962 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800963
964 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000965 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000966 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000967
bsalomon5b30c6f2015-12-17 14:17:34 -0800968 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800969 GrGLenum externalFormat;
970 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400971 this->glCaps().getTexSubImageExternalFormatAndType(
972 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400973 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800974 return false;
975 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400976
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000977 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800978 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000979 * because our srcData has extra bytes past each row. If so, we need
980 * to trim those off here, since GL ES may not let us specify
981 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000982 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000983 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -0800984
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400985 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600986 *mipMapsStatus = (mipLevelCount > 1) ?
987 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -0800988 }
bsalomone699d0c2016-03-09 06:25:15 -0800989
Brian Salomond2a8ae22019-09-10 16:03:59 -0400990 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -0800991
Brian Salomond2a8ae22019-09-10 16:03:59 -0400992 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
993 if (!texels[currentMipLevel].fPixels) {
994 if (mipMapsStatus) {
995 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400996 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400997 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +0000998 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400999 int twoToTheMipLevel = 1 << currentMipLevel;
1000 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1001 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1002 const size_t trimRowBytes = currentWidth * bpp;
1003 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1004
1005 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1006 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1007 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1008 restoreGLRowLength = true;
1009 }
1010
1011 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1012 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001013 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001014 if (restoreGLRowLength) {
1015 SkASSERT(caps.writePixelsRowBytesSupport());
1016 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1017 }
1018 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001019}
1020
Greg Daniel7bfc9132019-08-14 14:23:53 -04001021bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
1022 SkImage::CompressionType compressionType,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001023 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001024 GrGLenum target,
1025 const void* data) {
1026 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001027 const GrGLCaps& caps = this->glCaps();
1028
1029 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001030 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001031 if (!internalFormat) {
1032 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001033 }
1034
Greg Daniel7bfc9132019-08-14 14:23:53 -04001035 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001036
1037 static constexpr int kMipLevelCount = 1;
1038
1039 // Make sure that the width and height that we pass to OpenGL
1040 // is a multiple of the block size.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001041 size_t dataSize =
1042 GrCompressedDataSize(compressionType, dimensions.width(), dimensions.height());
Brian Salomonbb8dde82019-06-27 10:52:13 -04001043
1044 if (useTexStorage) {
1045 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001046 GL_ALLOC_CALL(this->glInterface(),
1047 TexStorage2D(target, kMipLevelCount, internalFormat, dimensions.width(),
1048 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001049 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1050 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001051 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001052 }
1053 GL_CALL(CompressedTexSubImage2D(target,
1054 0, // level
1055 0, // left
1056 0, // top
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001057 dimensions.width(),
1058 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001059 internalFormat,
1060 SkToInt(dataSize),
1061 data));
1062 } else {
1063 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1064 0, // level
1065 internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001066 dimensions.width(),
1067 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001068 0, // border
1069 SkToInt(dataSize),
1070 data));
1071
1072 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1073 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001074 return false;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001075 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001076 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001077 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001078}
1079
bsalomon424cc262015-05-22 10:37:30 -07001080static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001081 int sampleCount,
1082 GrGLenum format,
1083 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001084 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001085 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001086 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001087 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001088 GL_ALLOC_CALL(ctx.interface(),
1089 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1090 sampleCount,
1091 format,
1092 width, height));
1093 break;
1094 case GrGLCaps::kES_Apple_MSFBOType:
1095 GL_ALLOC_CALL(ctx.interface(),
1096 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1097 sampleCount,
1098 format,
1099 width, height));
1100 break;
1101 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1102 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1103 GL_ALLOC_CALL(ctx.interface(),
1104 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1105 sampleCount,
1106 format,
1107 width, height));
1108 break;
1109 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001110 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001111 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001112 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001113 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001114}
1115
Brian Salomonea4ad302019-08-07 13:04:55 -04001116bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001117 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001118 GrGLRenderTarget::IDs* rtIDs) {
1119 rtIDs->fMSColorRenderbufferID = 0;
1120 rtIDs->fRTFBOID = 0;
1121 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1122 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001123
bsalomona11e5fc2015-12-18 07:59:41 -08001124 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001125
Brian Salomonea4ad302019-08-07 13:04:55 -04001126 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001127 goto FAILED;
1128 }
1129
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001130 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001131 goto FAILED;
1132 }
1133
Brian Salomonea4ad302019-08-07 13:04:55 -04001134 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1135 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001136 goto FAILED;
1137 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001138
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001139 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1140 // the texture bound to the other. The exception is the IMG multisample extension. With this
1141 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1142 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001143 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001144 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1145 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1146 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 goto FAILED;
1148 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001149 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001151 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001152 }
1153
egdanield803f272015-03-18 13:01:52 -07001154 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001155 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001156 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001157 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001158 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001159 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001160 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 goto FAILED;
1162 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001163 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001164 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001165 GR_GL_COLOR_ATTACHMENT0,
1166 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001169 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001170
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001171 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001172 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1173 GR_GL_COLOR_ATTACHMENT0,
1174 desc.fTarget,
1175 desc.fID,
1176 0,
1177 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001178 } else {
egdanield803f272015-03-18 13:01:52 -07001179 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001180 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001181 desc.fTarget,
1182 desc.fID,
1183 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001184 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185
1186 return true;
1187
1188FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001189 if (rtIDs->fMSColorRenderbufferID) {
1190 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001191 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1193 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001195 if (rtIDs->fTexFBOID) {
1196 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 }
1198 return false;
1199}
1200
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001201// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001202static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001203// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001204 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001205}
1206
Brian Salomone2826ab2019-06-04 15:58:31 -04001207static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001209 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1210 // drivers have a bug where an FBO won't be complete if it includes a
1211 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001212 GrGLTextureParameters::SamplerOverriddenState state;
1213 state.fMinFilter = GR_GL_NEAREST;
1214 state.fMagFilter = GR_GL_NEAREST;
1215 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1216 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1218 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1219 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1220 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001221 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001222}
1223
Robert Phillips67d52cf2017-06-05 13:38:13 -04001224sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001225 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001226 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001227 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001228 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001229 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001230 int mipLevelCount,
1231 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001232 // We don't support protected textures in GL.
1233 if (isProtected == GrProtected::kYes) {
1234 return nullptr;
1235 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001236 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001237
Brian Salomond2a8ae22019-09-10 16:03:59 -04001238 SkASSERT(mipLevelCount > 0);
1239 GrMipMapsStatus mipMapsStatus =
1240 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001241 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001242 GrGLTexture::Desc texDesc;
1243 texDesc.fSize = {desc.fWidth, desc.fHeight};
1244 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001245 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001246 texDesc.fConfig = desc.fConfig;
1247 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001248 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1249 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001250
Brian Salomond2a8ae22019-09-10 16:03:59 -04001251 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1252 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001253
1254 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001255 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001256 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001257
Robert Phillips67d52cf2017-06-05 13:38:13 -04001258 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001259 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001260 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001261 GL_CALL(BindTexture(texDesc.fTarget, 0));
1262 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001263
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1265 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 return return_null_texture();
1267 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001268 tex = sk_make_sp<GrGLTextureRenderTarget>(
1269 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001270 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001272 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001274 // The non-sampler params are still at their default values.
1275 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1276 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001277 if (levelClearMask) {
1278 GrGLenum externalFormat, externalType;
1279 size_t bpp;
1280 this->glCaps().getTexSubImageZeroFormatTypeAndBpp(texDesc.fFormat, &externalFormat,
1281 &externalType, &bpp);
1282 if (this->glCaps().clearTextureSupport()) {
1283 for (int i = 0; i < mipLevelCount; ++i) {
1284 if (levelClearMask & (1U << i)) {
1285 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1286 nullptr));
1287 }
1288 }
1289 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1290 !this->glCaps().performColorClearsAsDraws()) {
1291 this->disableScissor();
1292 this->disableWindowRectangles();
1293 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001294 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001295 for (int i = 0; i < mipLevelCount; ++i) {
1296 if (levelClearMask & (1U << i)) {
1297 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1298 kDst_TempFBOTarget);
1299 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1300 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1301 }
1302 }
Brian Salomonc2249852019-09-16 11:08:50 -04001303 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001304 } else {
1305 std::unique_ptr<char[]> zeros;
1306 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1307 for (int i = 0; i < mipLevelCount; ++i) {
1308 if (levelClearMask & (1U << i)) {
1309 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1310 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1311 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1312 // smaller levels that need clearing.
1313 if (!zeros) {
1314 size_t size = levelWidth * levelHeight * bpp;
1315 zeros.reset(new char[size]());
1316 }
1317 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1318 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1319 externalFormat, externalType, zeros.get()));
1320 }
Brian Salomona3e29962019-07-16 11:52:08 -04001321 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001322 }
1323 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001324 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001325}
1326
Brian Salomonbb8dde82019-06-27 10:52:13 -04001327sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001328 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -04001329 SkImage::CompressionType compression,
1330 SkBudgeted budgeted, const void* data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001331 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001332 GrGLTexture::Desc desc;
1333 desc.fSize = {width, height};
1334 desc.fTarget = GR_GL_TEXTURE_2D;
1335 desc.fConfig = GrCompressionTypePixelConfig(compression);
1336 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001337 desc.fFormat = format.asGLFormat();
1338 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat, compression, &initialState,
1339 data);
Brian Salomonea4ad302019-08-07 13:04:55 -04001340 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001341 return nullptr;
1342 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001343 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001344 // The non-sampler params are still at their default values.
1345 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1346 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001347 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001348}
1349
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001350namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001351
egdaniel8dc7c3a2015-04-16 11:22:42 -07001352const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001353
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001354void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001355 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001356
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001358 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001359 (kUnknownBitCount == format->fTotalBits));
1360 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001361 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001362 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1363 (GrGLint*)&format->fStencilBits);
1364 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001365 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1367 (GrGLint*)&format->fTotalBits);
1368 format->fTotalBits += format->fStencilBits;
1369 } else {
1370 format->fTotalBits = format->fStencilBits;
1371 }
1372 }
1373}
1374}
1375
Brian Salomon5043f1f2019-07-11 21:27:54 -04001376int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001377 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001378 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1379
1380 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001381 // Default to unsupported, set this if we find a stencil format that works.
1382 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001383
Brian Salomond2a8ae22019-09-10 16:03:59 -04001384 GrGLuint colorID =
1385 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1386 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001387 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001388 }
egdanielff1d5472015-09-10 08:37:20 -07001389 // unbind the texture from the texture unit before binding it to the frame buffer
1390 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1391
1392 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001393 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001394 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001395 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001396 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001397 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1398 GR_GL_COLOR_ATTACHMENT0,
1399 GR_GL_TEXTURE_2D,
1400 colorID,
1401 0));
bsalomon30447372015-12-21 09:03:05 -08001402 GrGLuint sbRBID = 0;
1403 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001404
1405 // look over formats till I find a compatible one
1406 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001407 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001408 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001409 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1410 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1411 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1412 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1413 sFmt.fInternalFormat,
1414 kSize, kSize));
1415 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001416 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001417 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001418 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001419 if (sFmt.fPacked) {
1420 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1421 GR_GL_DEPTH_ATTACHMENT,
1422 GR_GL_RENDERBUFFER, sbRBID));
1423 } else {
1424 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1425 GR_GL_DEPTH_ATTACHMENT,
1426 GR_GL_RENDERBUFFER, 0));
1427 }
1428 GrGLenum status;
1429 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1430 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1431 firstWorkingStencilFormatIndex = i;
1432 break;
1433 }
egdanielff1d5472015-09-10 08:37:20 -07001434 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1435 GR_GL_STENCIL_ATTACHMENT,
1436 GR_GL_RENDERBUFFER, 0));
1437 if (sFmt.fPacked) {
1438 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1439 GR_GL_DEPTH_ATTACHMENT,
1440 GR_GL_RENDERBUFFER, 0));
1441 }
egdanielff1d5472015-09-10 08:37:20 -07001442 }
1443 }
bsalomon30447372015-12-21 09:03:05 -08001444 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001445 }
1446 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001447 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1448 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001449 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001450 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001451 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001452}
1453
Brian Salomonea4ad302019-08-07 13:04:55 -04001454GrGLuint GrGLGpu::createCompressedTexture2D(
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001455 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001456 GrGLFormat format,
Brian Salomonea4ad302019-08-07 13:04:55 -04001457 SkImage::CompressionType compression,
1458 GrGLTextureParameters::SamplerOverriddenState* initialState,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001459 const void* data) {
1460 if (format == GrGLFormat::kUnknown) {
1461 return 0;
1462 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001463 GrGLuint id = 0;
1464 GL_CALL(GenTextures(1, &id));
1465 if (!id) {
1466 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001467 }
1468
Brian Salomonea4ad302019-08-07 13:04:55 -04001469 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001470
Brian Salomonea4ad302019-08-07 13:04:55 -04001471 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1472
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001473 if (!this->uploadCompressedTexData(format, compression, dimensions, GR_GL_TEXTURE_2D, data)) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001474 GL_CALL(DeleteTextures(1, &id));
1475 return 0;
1476 }
1477 return id;
1478}
1479
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001480GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001481 GrGLFormat format,
1482 GrRenderable renderable,
1483 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001484 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001485 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001486 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001487
Brian Salomonea4ad302019-08-07 13:04:55 -04001488 GrGLuint id = 0;
1489 GL_CALL(GenTextures(1, &id));
1490
1491 if (!id) {
1492 return 0;
1493 }
1494
1495 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001496
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001497 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001498 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001499 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001500 }
1501
Brian Salomond2a8ae22019-09-10 16:03:59 -04001502 if (initialState) {
1503 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1504 } else {
1505 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001506 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001507
1508 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1509
1510 bool success = false;
1511 if (internalFormat) {
1512 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1513 if (this->glCaps().formatSupportsTexStorage(format)) {
1514 GL_ALLOC_CALL(this->glInterface(),
1515 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001516 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001517 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1518 } else {
1519 GrGLenum externalFormat, externalType;
1520 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1521 GrGLenum error = GR_GL_NO_ERROR;
1522 if (externalFormat && externalType) {
1523 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1524 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001525 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1526 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001527 GL_ALLOC_CALL(
1528 this->glInterface(),
1529 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1530 currentHeight, 0, externalFormat, externalType, nullptr));
1531 error = CHECK_ALLOC_ERROR(this->glInterface());
1532 }
1533 success = (GR_GL_NO_ERROR == error);
1534 }
1535 }
1536 }
1537 if (success) {
1538 return id;
1539 }
1540 GL_CALL(DeleteTextures(1, &id));
1541 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001542}
1543
Chris Daltoneffee202019-07-01 22:28:03 -06001544GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1545 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001546 SkASSERT(width >= rt->width());
1547 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001548
egdaniel8dc7c3a2015-04-16 11:22:42 -07001549 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001550
Brian Salomond4764a12019-08-08 12:08:24 -04001551 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001552 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001553 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001554 }
egdanielff1d5472015-09-10 08:37:20 -07001555
1556 if (!sbDesc.fRenderbufferID) {
1557 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1558 }
1559 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001560 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001561 }
1562 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1563 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1564 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1565 // we do this "if" so that we don't call the multisample
1566 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001567 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001568 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001569 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001570 sFmt.fInternalFormat,
1571 width, height));
1572 } else {
1573 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1574 sFmt.fInternalFormat,
1575 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001576 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001577 }
1578 fStats.incStencilAttachmentCreates();
1579 // After sized formats we attempt an unsized format and take
1580 // whatever sizes GL gives us. In that case we query for the size.
1581 GrGLStencilAttachment::Format format = sFmt;
1582 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001583 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1584 sbDesc,
1585 width,
1586 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001587 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001588 format);
1589 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001590}
1591
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001592////////////////////////////////////////////////////////////////////////////////
1593
Brian Salomondbf70722019-02-07 11:31:24 -05001594sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1595 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001596 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001597}
1598
Greg Danielacd66b42019-05-22 16:29:12 -04001599void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001600 GrSurfaceOrigin rtOrigin) {
1601 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001602 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001603 // if the scissor fully contains the viewport then we fall through and
1604 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001605 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001606 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001607 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001608 fHWScissorSettings.fRect = scissor;
1609 }
1610 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1611 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1612 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001613 }
1614 return;
1615 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001617
1618 // See fall through note above
1619 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001620}
1621
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001622void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001623 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001624#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001625 typedef GrWindowRectsState::Mode Mode;
1626 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1627 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001628
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001629 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001630 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001631 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001632 }
1633
csmartdalton7535f412016-08-23 06:51:00 -07001634 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1635 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001636 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1637 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001638
Chris Daltond6cda8d2019-09-05 02:30:04 -06001639 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001640 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001641 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001642 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001643 }
1644
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001645 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001646 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001647
Greg Danielacd66b42019-05-22 16:29:12 -04001648 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001649#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001650}
1651
1652void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001653#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001654 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001655 return;
1656 }
1657 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001658 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001659#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001660}
1661
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001662bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
Robert Phillips901aff02019-10-08 12:32:56 -04001663 const GrProgramInfo& programInfo,
Robert Phillips4face832019-10-10 11:58:11 -04001664 GrPrimitiveType primitiveType) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001665
Robert Phillips901aff02019-10-08 12:32:56 -04001666 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo,
Robert Phillips4face832019-10-10 11:58:11 -04001667 primitiveType));
Greg Daniel9a51a862018-11-30 10:18:14 -05001668 if (!program) {
1669 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1670 return false;
1671 }
brianosman33f6b3f2016-06-02 05:49:21 -07001672
Brian Salomon802cb312018-06-08 18:05:20 -04001673 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001674
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001675 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001676 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1677 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001678
Robert Phillips901aff02019-10-08 12:32:56 -04001679 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001680
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001681 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001682 GrStencilSettings stencil;
1683 if (programInfo.pipeline().isStencilEnabled()) {
1684 // TODO: attach stencil and create settings during render target flush.
1685 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1686 stencil.reset(*programInfo.pipeline().getUserStencil(),
1687 programInfo.pipeline().hasStencilClip(),
1688 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001689 }
Robert Phillips901aff02019-10-08 12:32:56 -04001690 this->flushStencil(stencil, programInfo.origin());
1691 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001692 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001693 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1694 : kBogusScissor);
1695 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001696 } else {
1697 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001698 }
Robert Phillips901aff02019-10-08 12:32:56 -04001699 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1700 glRT, programInfo.origin());
1701 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001702
1703 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001704 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001705 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001706
1707 return true;
1708}
1709
Brian Salomon802cb312018-06-08 18:05:20 -04001710void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1711 if (!program) {
1712 fHWProgram.reset();
1713 fHWProgramID = 0;
1714 return;
1715 }
1716 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1717 if (program == fHWProgram) {
1718 return;
1719 }
1720 auto id = program->programID();
1721 SkASSERT(id);
1722 GL_CALL(UseProgram(id));
1723 fHWProgram = std::move(program);
1724 fHWProgramID = id;
1725}
1726
1727void GrGLGpu::flushProgram(GrGLuint id) {
1728 SkASSERT(id);
1729 if (fHWProgramID == id) {
1730 SkASSERT(!fHWProgram);
1731 return;
1732 }
1733 fHWProgram.reset();
1734 GL_CALL(UseProgram(id));
1735 fHWProgramID = id;
1736}
1737
1738void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001739 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001740 int baseVertex,
1741 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001742 int baseInstance,
1743 GrPrimitiveRestart enablePrimitiveRestart) {
1744 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001745
cdaltone2e71c22016-04-07 18:13:29 -07001746 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001747 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001748 SkASSERT(indexBuffer->isCpuBuffer() ||
1749 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001750 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001751 } else {
1752 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001753 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001754
Brian Salomon92be2f72018-06-19 14:33:47 -04001755 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1756 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001757
Brian Salomon802cb312018-06-08 18:05:20 -04001758 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001759 SkASSERT(vertexBuffer);
1760 SkASSERT(vertexBuffer->isCpuBuffer() ||
1761 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1762 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001763 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1764 const auto& attrib = fHWProgram->vertexAttribute(i);
1765 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001766 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1767 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001768 }
Chris Dalton1d616352017-05-31 12:51:23 -06001769 }
Brian Salomon802cb312018-06-08 18:05:20 -04001770 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001771 SkASSERT(instanceBuffer);
1772 SkASSERT(instanceBuffer->isCpuBuffer() ||
1773 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1774 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001775 int attribIdx = fHWProgram->numVertexAttributes();
1776 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1777 const auto& attrib = fHWProgram->instanceAttribute(i);
1778 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001779 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1780 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1781 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001782 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001783 }
1784}
1785
Brian Salomonae64c192019-02-05 09:41:37 -05001786GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001787 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001788
cdaltone2e71c22016-04-07 18:13:29 -07001789 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001790 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001791 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001792 }
cdaltone2e71c22016-04-07 18:13:29 -07001793
Brian Salomonae64c192019-02-05 09:41:37 -05001794 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001795 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001796 if (!bufferState->fBufferZeroKnownBound) {
1797 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1798 bufferState->fBufferZeroKnownBound = true;
1799 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001800 }
Brian Salomondbf70722019-02-07 11:31:24 -05001801 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1802 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001803 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1804 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1805 bufferState->fBufferZeroKnownBound = false;
1806 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001807 }
1808
Brian Salomonae64c192019-02-05 09:41:37 -05001809 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001810}
Brian Salomond818ebf2018-07-02 14:08:49 +00001811void GrGLGpu::disableScissor() {
1812 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1813 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1814 fHWScissorSettings.fEnabled = kNo_TriState;
1815 return;
1816 }
1817}
1818
Brian Osman9a9baae2018-11-05 15:06:26 -05001819void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001820 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001821 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001822 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001823 SkASSERT(!this->caps()->performColorClearsAsDraws());
1824 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001825
Brian Salomon43f8bf02017-10-18 08:33:29 -04001826 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001827
Brian Salomon43f8bf02017-10-18 08:33:29 -04001828 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1829
Brian Salomond818ebf2018-07-02 14:08:49 +00001830 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001831 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001832 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001833 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001834 }
Greg Danielacd66b42019-05-22 16:29:12 -04001835 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001836 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001837 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001838 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001839 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001840}
1841
Robert Phillips95214472017-08-08 18:00:03 -04001842void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001843 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1844
Robert Phillips95214472017-08-08 18:00:03 -04001845 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001846 return;
1847 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001848
Chris Dalton674f77a2019-09-30 20:49:39 -06001849 // This should only be called internally when we know we have a stencil buffer.
1850 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001851
bsalomonb0bd4f62014-09-03 07:19:50 -07001852 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001853 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001854
Brian Salomond818ebf2018-07-02 14:08:49 +00001855 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001856 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001857
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001858 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001859 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001860 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001861 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001862}
1863
Chris Daltonb50cc812019-10-14 16:29:21 -06001864static bool use_tiled_rendering(const GrGLCaps& glCaps,
1865 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1866 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1867 // Otherwise it's faster to just not use it.
1868 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1869 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1870}
1871
1872void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001873 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1874 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1875 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1876
1877 this->handleDirtyContext();
1878
1879 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1880 this->flushRenderTarget(glRT);
1881 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1882
Chris Daltonb50cc812019-10-14 16:29:21 -06001883 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1884 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1885 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1886 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1887 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1888 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1889 nativeBounds.fHeight, preserveMask));
1890 }
1891
Chris Dalton674f77a2019-09-30 20:49:39 -06001892 GrGLbitfield clearMask = 0;
1893 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1894 SkASSERT(!this->caps()->performColorClearsAsDraws());
1895 this->flushClearColor(colorLoadStore.fClearColor);
1896 this->flushColorWrite(true);
1897 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001898 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001899 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1900 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1901 GL_CALL(StencilMask(0xffffffff));
1902 GL_CALL(ClearStencil(0));
1903 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1904 }
1905 if (clearMask) {
1906 this->disableScissor();
1907 this->disableWindowRectangles();
1908 GL_CALL(Clear(clearMask));
1909 }
1910}
1911
1912void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1913 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1914 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1915 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1916
1917 this->handleDirtyContext();
1918
1919 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1920 // The framebuffer binding changed in the middle of a command buffer. We should have already
1921 // printed a warning during onFBOChanged.
1922 return;
1923 }
1924
1925 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1926 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1927
1928 SkSTArray<2, GrGLenum> discardAttachments;
1929 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1930 discardAttachments.push_back(
1931 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1932 }
1933 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1934 discardAttachments.push_back(
1935 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1936 }
1937
1938 if (!discardAttachments.empty()) {
1939 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1940 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1941 discardAttachments.begin()));
1942 } else {
1943 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1944 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1945 discardAttachments.begin()));
1946 }
1947 }
1948 }
1949
Chris Daltonb50cc812019-10-14 16:29:21 -06001950 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1951 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1952 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1953 // Handled by use_tiled_rendering().
1954 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1955 GL_CALL(EndTiling(preserveMask));
1956 }
1957
Chris Dalton674f77a2019-09-30 20:49:39 -06001958 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001959}
1960
csmartdalton29df7602016-08-31 11:55:52 -07001961void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1962 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001963 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07001964 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001965 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07001966 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001967
egdaniel8dc7c3a2015-04-16 11:22:42 -07001968 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001969 // this should only be called internally when we know we have a
1970 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001971 SkASSERT(sb);
1972 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001973#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001974 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001975 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001976#else
1977 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001978 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04001979 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001980 // changing the clip between stencil passes may or may not
1981 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001982 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001983#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001984 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07001985 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001986 value = (1 << (stencilBitCount - 1));
1987 } else {
1988 value = 0;
1989 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001990 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001991 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001992
Greg Danielacd66b42019-05-22 16:29:12 -04001993 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001994 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001995
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001996 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001997 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001998 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001999 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002000}
2001
Brian Salomone05ba5a2019-04-08 11:59:07 -04002002bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002003 GrColorType surfaceColorType, GrColorType dstColorType,
2004 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002005 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002006
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002007 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002008 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002009 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002010 return false;
2011 }
Greg Danielba88ab62019-07-26 09:14:01 -04002012 GrGLenum externalFormat = 0;
2013 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002014 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2015 surfaceColorType,
2016 dstColorType,
2017 &externalFormat,
2018 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002019 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002020 return false;
2021 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002022
Brian Salomon71d9d842016-11-03 13:42:00 -04002023 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002024 if (renderTarget->numSamples() <= 1 ||
2025 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2026 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2027 this->flushRenderTargetNoColorWrites(renderTarget);
2028 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2029 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2030 return false;
2031 } else {
2032 SkASSERT(renderTarget->requiresManualMSAAResolve());
2033 // we don't track the state of the READ FBO ID.
2034 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002035 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002036 } else {
2037 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002038 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002039 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002040 }
2041
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002042 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002043 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002044
Brian Salomona6948702018-06-01 15:33:20 -04002045 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002046 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002047 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002048 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002049 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002050 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002051
Brian Osman1348ed02018-09-12 09:08:39 -04002052 bool reattachStencil = false;
2053 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2054 renderTarget &&
2055 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002056 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002057 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2058 reattachStencil = true;
2059 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2060 GR_GL_RENDERBUFFER, 0));
2061 }
2062
Chris Dalton76500e52019-09-05 02:13:05 -06002063 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002064 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002065
2066 if (reattachStencil) {
2067 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2068 renderTarget->renderTargetPriv().getStencilAttachment());
2069 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2070 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2071 }
2072
Brian Salomon26de56e2019-04-10 12:14:26 -04002073 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002074 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002075 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2076 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002077
Brian Salomone05ba5a2019-04-08 11:59:07 -04002078 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002079 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002080 }
2081 return true;
2082}
2083
2084bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002085 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2086 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002087 SkASSERT(surface);
2088
Brian Salomonb28cb682019-07-26 12:48:47 -04002089 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002090
Brian Salomon26de56e2019-04-10 12:14:26 -04002091 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2092 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002093
Brian Salomon1047a492019-07-02 12:25:21 -04002094 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002095 rowPixelWidth = width;
2096 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002097 SkASSERT(!(rowBytes % bytesPerPixel));
2098 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002099 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002100 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2101 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002102}
2103
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002104GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002105 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002106 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002107 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
2108 const SkTArray<GrTextureProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002109 if (!fCachedOpsRenderPass) {
2110 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002111 }
2112
Chris Daltonb50cc812019-10-14 16:29:21 -06002113 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002114 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002115}
2116
Brian Salomon1fabd512018-02-09 09:54:25 -05002117void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002118 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002119 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002120 this->didWriteToSurface(target, origin, &bounds);
2121}
bsalomon6ba6fa12015-03-04 11:57:37 -08002122
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002123void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2124 this->flushRenderTargetNoColorWrites(target);
2125 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002126}
2127
Brian Osman9aa30c62018-07-02 15:21:46 -04002128void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002129 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002130 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002131 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002132 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002133#ifdef SK_DEBUG
2134 // don't do this check in Chromium -- this is causing
2135 // lots of repeated command buffer flushes when the compositor is
2136 // rendering with Ganesh, which is really slow; even too slow for
2137 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002138 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002139 GrGLenum status;
2140 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2141 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2142 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2143 }
bsalomon160f24c2015-03-17 15:55:42 -07002144 }
egdanield803f272015-03-18 13:01:52 -07002145#endif
2146 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002147 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002148 }
2149
brianosman35b784d2016-05-05 11:52:53 -07002150 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002151 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002152 }
bsalomon083617b2016-02-12 12:10:14 -08002153}
bsalomona9909122016-01-23 10:41:40 -08002154
brianosman33f6b3f2016-06-02 05:49:21 -07002155void GrGLGpu::flushFramebufferSRGB(bool enable) {
2156 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2157 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2158 fHWSRGBFramebuffer = kYes_TriState;
2159 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2160 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2161 fHWSRGBFramebuffer = kNo_TriState;
2162 }
2163}
2164
Greg Danielacd66b42019-05-22 16:29:12 -04002165void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002166 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002167 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002168 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002169 fHWViewport = viewport;
2170 }
2171}
2172
bsalomon@google.comd302f142011-03-03 13:54:13 +00002173#define SWAP_PER_DRAW 0
2174
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002175#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002176 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002177 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002178 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002179 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002180 void SwapBuf() {
2181 DWORD procID = GetCurrentProcessId();
2182 HWND hwnd = GetTopWindow(GetDesktopWindow());
2183 while(hwnd) {
2184 DWORD wndProcID = 0;
2185 GetWindowThreadProcessId(hwnd, &wndProcID);
2186 if(wndProcID == procID) {
2187 SwapBuffers(GetDC(hwnd));
2188 }
2189 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2190 }
2191 }
2192 #endif
2193#endif
2194
Robert Phillips901aff02019-10-08 12:32:56 -04002195void GrGLGpu::draw(GrRenderTarget* renderTarget,
2196 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002197 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002198 int meshCount) {
2199 this->handleDirtyContext();
2200
Robert Phillips2579de42019-10-09 09:51:59 -04002201 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002202
Robert Phillips4face832019-10-10 11:58:11 -04002203 GrPrimitiveType primitiveType = meshes[0].primitiveType();
2204
2205#ifdef SK_DEBUG
2206 // kPoints should never be intermingled in with the other primitive types
2207 for (int i = 1; i < meshCount; ++i) {
2208 if (primitiveType == GrPrimitiveType::kPoints) {
2209 SkASSERT(meshes[i].primitiveType() == GrPrimitiveType::kPoints);
2210 } else {
2211 SkASSERT(meshes[i].primitiveType() != GrPrimitiveType::kPoints);
bsalomon2eda5b32016-09-21 10:53:24 -07002212 }
2213 }
Robert Phillips4face832019-10-10 11:58:11 -04002214#endif
2215
2216 // Passing 'primitiveType' here is a bit misleading. In GL's case it works out, since
2217 // GL only cares if it is kPoints or not.
2218 if (!this->flushGLState(renderTarget, programInfo, primitiveType)) {
bsalomond95263c2014-12-16 13:05:12 -08002219 return;
2220 }
ethannicholas22793252016-01-30 09:59:10 -08002221
Robert Phillips901aff02019-10-08 12:32:56 -04002222 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2223 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2224
Brian Salomonf7232642018-09-19 08:58:08 -04002225 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002226 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2227 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002228 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002229 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002230
Robert Phillips901aff02019-10-08 12:32:56 -04002231 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002232 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002233 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2234 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002235 }
Robert Phillips901aff02019-10-08 12:32:56 -04002236 if (hasDynamicPrimProcTextures) {
2237 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2238 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2239 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002240 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002241 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002242 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002243 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2244 GL_CALL(Enable(GR_GL_CULL_FACE));
2245 GL_CALL(Disable(GR_GL_CULL_FACE));
2246 }
Brian Salomonf7232642018-09-19 08:58:08 -04002247 meshes[m].sendToGpu(this);
2248 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002249 }
ethannicholas22793252016-01-30 09:59:10 -08002250
bsalomon@google.comd302f142011-03-03 13:54:13 +00002251#if SWAP_PER_DRAW
2252 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002253 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002254 aglSwapBuffers(aglGetCurrentContext());
2255 int set_a_break_pt_here = 9;
2256 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002257 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002258 SwapBuf();
2259 int set_a_break_pt_here = 9;
2260 SwapBuf();
2261 #endif
2262#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002263}
2264
Chris Dalton3809bab2017-06-13 10:55:06 -06002265static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2266 switch (primitiveType) {
2267 case GrPrimitiveType::kTriangles:
2268 return GR_GL_TRIANGLES;
2269 case GrPrimitiveType::kTriangleStrip:
2270 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002271 case GrPrimitiveType::kPoints:
2272 return GR_GL_POINTS;
2273 case GrPrimitiveType::kLines:
2274 return GR_GL_LINES;
2275 case GrPrimitiveType::kLineStrip:
2276 return GR_GL_LINE_STRIP;
Robert Phillips571177f2019-10-04 14:41:49 -04002277 case GrPrimitiveType::kPath:
2278 SK_ABORT("non-mesh-based GrPrimitiveType");
2279 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002280 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002281 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002282}
2283
Brian Salomon802cb312018-06-08 18:05:20 -04002284void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2285 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002286 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002287 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002288 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002289 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2290 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002291 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002292 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2293 }
2294 fStats.incNumDraws();
2295}
2296
Brian Salomondbf70722019-02-07 11:31:24 -05002297static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2298 size_t baseOffset = baseIndex * sizeof(uint16_t);
2299 if (indexBuffer->isCpuBuffer()) {
2300 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2301 } else {
2302 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2303 }
2304}
2305
Brian Salomon802cb312018-06-08 18:05:20 -04002306void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002307 int indexCount, int baseIndex, uint16_t minIndexValue,
2308 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002309 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002310 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002311 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002312
Brian Salomon802cb312018-06-08 18:05:20 -04002313 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002314
2315 if (this->glCaps().drawRangeElementsSupport()) {
2316 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002317 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002318 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002319 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002320 }
2321 fStats.incNumDraws();
2322}
2323
Brian Salomon802cb312018-06-08 18:05:20 -04002324void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002325 int vertexCount, int baseVertex,
2326 const GrBuffer* instanceBuffer, int instanceCount,
2327 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002328 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002329 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002330 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002331 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2332 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002333 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2334 SkTMin(instanceCount - i, maxInstances)));
2335 fStats.incNumDraws();
2336 }
Chris Dalton1d616352017-05-31 12:51:23 -06002337}
2338
Brian Salomon802cb312018-06-08 18:05:20 -04002339void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002340 const GrBuffer* indexBuffer, int indexCount,
2341 int baseIndex, const GrBuffer* vertexBuffer,
2342 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002343 int instanceCount, int baseInstance,
2344 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002345 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002346 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002347 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2348 for (int i = 0; i < instanceCount; i += maxInstances) {
2349 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2350 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002351 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002352 SkTMin(instanceCount - i, maxInstances)));
2353 fStats.incNumDraws();
2354 }
Chris Dalton1d616352017-05-31 12:51:23 -06002355}
2356
Chris Dalton16a33c62019-09-24 22:19:17 -06002357void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2358 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002359 // Some extensions automatically resolves the texture when it is read.
2360 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2361
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002362 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002363 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2364 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2365 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2366 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002367
Chris Daltonc139d082019-09-26 14:04:24 -06002368 // make sure we go through flushRenderTarget() since we've modified
2369 // the bound DRAW FBO ID.
2370 fHWBoundRenderTargetUniqueID.makeInvalid();
2371 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2372 // Apple's extension uses the scissor as the blit bounds.
2373 GrScissorState scissorState;
2374 scissorState.set(resolveRect);
2375 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2376 this->disableWindowRectangles();
2377 GL_CALL(ResolveMultisampleFramebuffer());
2378 } else {
2379 int l, b, r, t;
2380 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2381 this->glCaps().blitFramebufferSupportFlags()) {
2382 l = 0;
2383 b = 0;
2384 r = target->width();
2385 t = target->height();
2386 } else {
2387 auto rect = GrNativeRect::MakeRelativeTo(
2388 resolveOrigin, rt->height(), resolveRect);
2389 l = rect.fX;
2390 b = rect.fY;
2391 r = rect.fX + rect.fWidth;
2392 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002393 }
Chris Daltonc139d082019-09-26 14:04:24 -06002394
2395 // BlitFrameBuffer respects the scissor, so disable it.
2396 this->disableScissor();
2397 this->disableWindowRectangles();
2398 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 +00002399 }
2400}
2401
bsalomon@google.com411dad02012-06-05 20:24:20 +00002402namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002403
bsalomon@google.com411dad02012-06-05 20:24:20 +00002404
2405GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002406 static const GrGLenum gTable[kGrStencilOpCount] = {
2407 GR_GL_KEEP, // kKeep
2408 GR_GL_ZERO, // kZero
2409 GR_GL_REPLACE, // kReplace
2410 GR_GL_INVERT, // kInvert
2411 GR_GL_INCR_WRAP, // kIncWrap
2412 GR_GL_DECR_WRAP, // kDecWrap
2413 GR_GL_INCR, // kIncClamp
2414 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002415 };
cdalton93a379b2016-05-11 13:58:08 -07002416 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2417 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2418 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2419 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2420 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2421 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2422 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2423 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2424 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2425 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002426}
2427
2428void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002429 const GrStencilSettings::Face& face,
2430 GrGLenum glFace) {
2431 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2432 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2433 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002434
cdalton93a379b2016-05-11 13:58:08 -07002435 GrGLint ref = face.fRef;
2436 GrGLint mask = face.fTestMask;
2437 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002438
2439 if (GR_GL_FRONT_AND_BACK == glFace) {
2440 // we call the combined func just in case separate stencil is not
2441 // supported.
2442 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2443 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002444 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002445 } else {
2446 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2447 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002448 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002449 }
2450}
2451}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002452
Chris Dalton71713f62019-04-18 12:41:03 -06002453void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002454 if (stencilSettings.isDisabled()) {
2455 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002456 } else if (fHWStencilSettings != stencilSettings ||
2457 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002458 if (kYes_TriState != fHWStencilTestEnabled) {
2459 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002460
csmartdaltonc7d85332016-10-26 10:13:46 -07002461 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002462 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002463 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002464 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2465 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002466 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002467 set_gl_stencil(
2468 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002469 }
joshualitta58fe352014-10-27 08:39:00 -07002470 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002471 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002472 }
2473}
2474
csmartdaltonc7d85332016-10-26 10:13:46 -07002475void GrGLGpu::disableStencil() {
2476 if (kNo_TriState != fHWStencilTestEnabled) {
2477 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002478
csmartdaltonc7d85332016-10-26 10:13:46 -07002479 fHWStencilTestEnabled = kNo_TriState;
2480 fHWStencilSettings.invalidate();
2481 }
2482}
2483
Chris Dalton4c56b032019-03-04 12:28:42 -07002484void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002485 // rt is only optional if useHWAA is false.
2486 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002487#ifdef SK_DEBUG
2488 if (useHWAA && rt->numSamples() <= 1) {
2489 SkASSERT(this->caps()->mixedSamplesSupport());
2490 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2491 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2492 }
2493#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002494
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002495 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002496 if (useHWAA) {
2497 if (kYes_TriState != fMSAAEnabled) {
2498 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2499 fMSAAEnabled = kYes_TriState;
2500 }
2501 } else {
2502 if (kNo_TriState != fMSAAEnabled) {
2503 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2504 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002505 }
2506 }
2507 }
2508}
2509
Chris Daltonbbb3f642019-07-24 12:25:08 -04002510void GrGLGpu::flushBlendAndColorWrite(
2511 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2512 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2513 // We need to work around a driver bug by using a blend state that preserves the dst color,
2514 // rather than disabling color writes.
2515 GrXferProcessor::BlendInfo preserveDstBlend;
2516 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2517 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2518 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2519 return;
2520 }
bsalomonf7cc8772015-05-11 11:21:14 -07002521
cdalton8917d622015-05-06 13:40:21 -07002522 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002523 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2524 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002525
2526 // Any optimization to disable blending should have already been applied and
2527 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002528 bool blendOff =
2529 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2530 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2531 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002532
egdanielb414f252014-07-29 13:15:47 -07002533 if (blendOff) {
2534 if (kNo_TriState != fHWBlendState.fEnabled) {
2535 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002536
2537 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2538 // https://code.google.com/p/skia/issues/detail?id=3943
2539 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2540 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2541 SkASSERT(this->caps()->advancedBlendEquationSupport());
2542 // Set to any basic blending equation.
2543 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2544 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2545 fHWBlendState.fEquation = blend_equation;
2546 }
2547
egdanielb414f252014-07-29 13:15:47 -07002548 fHWBlendState.fEnabled = kNo_TriState;
2549 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002550 } else {
2551 if (kYes_TriState != fHWBlendState.fEnabled) {
2552 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002553
Chris Daltonbbb3f642019-07-24 12:25:08 -04002554 fHWBlendState.fEnabled = kYes_TriState;
2555 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002556
Chris Daltonbbb3f642019-07-24 12:25:08 -04002557 if (fHWBlendState.fEquation != equation) {
2558 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2559 fHWBlendState.fEquation = equation;
2560 }
cdalton8917d622015-05-06 13:40:21 -07002561
Chris Daltonbbb3f642019-07-24 12:25:08 -04002562 if (GrBlendEquationIsAdvanced(equation)) {
2563 SkASSERT(this->caps()->advancedBlendEquationSupport());
2564 // Advanced equations have no other blend state.
2565 return;
2566 }
cdalton8917d622015-05-06 13:40:21 -07002567
Chris Daltonbbb3f642019-07-24 12:25:08 -04002568 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2569 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2570 gXfermodeCoeff2Blend[dstCoeff]));
2571 fHWBlendState.fSrcCoeff = srcCoeff;
2572 fHWBlendState.fDstCoeff = dstCoeff;
2573 }
cdalton8917d622015-05-06 13:40:21 -07002574
Chris Daltonbbb3f642019-07-24 12:25:08 -04002575 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2576 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2577 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2578 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2579 fHWBlendState.fConstColor = blendConst;
2580 fHWBlendState.fConstColorValid = true;
2581 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002582 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002583 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002584
2585 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002586}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002587
Brian Salomondc829942018-10-23 16:07:24 -04002588static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002589 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002590 switch (swizzle[i]) {
2591 case 'r': glValues[i] = GR_GL_RED; break;
2592 case 'g': glValues[i] = GR_GL_GREEN; break;
2593 case 'b': glValues[i] = GR_GL_BLUE; break;
2594 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002595 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002596 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002597 default: SK_ABORT("Unsupported component");
2598 }
Brian Salomon327955b2018-10-22 15:39:22 +00002599 }
2600}
2601
Greg Daniel2c3398d2019-06-19 11:58:01 -04002602void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2603 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002604 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002605
reed856e9d92015-09-30 12:21:45 -07002606#ifdef SK_DEBUG
2607 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002608 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002609 const int w = texture->width();
2610 const int h = texture->height();
2611 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2612 }
2613 }
2614#endif
2615
Robert Phillips294870f2016-11-11 12:38:40 -05002616 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002617 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002618 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002619 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002620 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002621 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002622 }
2623
Brian Salomondc829942018-10-23 16:07:24 -04002624 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002625 if (!this->caps()->mipMapSupport() ||
2626 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002627 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002628 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002629 }
bsalomonefd7d452014-10-23 14:17:46 -07002630
brianosman33f6b3f2016-06-02 05:49:21 -07002631#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002632 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002633 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002634 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002635 }
2636#endif
2637
Brian Salomone2826ab2019-06-04 15:58:31 -04002638 auto timestamp = texture->parameters()->resetTimestamp();
2639 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002640
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2642 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002643 if (fSamplerObjectCache) {
2644 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2645 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002646 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2647 texture->parameters()->samplerOverriddenState();
2648 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002649
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2651 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002652
Brian Salomone2826ab2019-06-04 15:58:31 -04002653 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2654 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002655
2656 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002657 newSamplerState.fMinLOD = -1000.f;
2658 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002659
Brian Salomone2826ab2019-06-04 15:58:31 -04002660 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002661 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002662 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002663 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002664 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002665 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002666 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002667 }
Mike Klein1bccae52018-10-19 11:38:44 +00002668 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002669 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002670 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002671 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002672 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002673 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002674 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002675 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002676 }
2677 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002678 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002679 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002680 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002681 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002682 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002683 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002684 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002685 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002686 if (this->glCaps().clampToBorderSupport()) {
2687 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002688 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002689 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002690 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2691 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002692 }
2693 }
Brian Salomondc829942018-10-23 16:07:24 -04002694 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002695 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2696 newNonsamplerState.fBaseMipMapLevel = 0;
2697 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002698
Brian Salomone2826ab2019-06-04 15:58:31 -04002699 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2700 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002701 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002702 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2703 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002704 GrGLenum glValues[4];
2705 get_gl_swizzle_values(swizzle, glValues);
2706 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002707 if (GR_IS_GR_GL(this->glStandard())) {
2708 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
2709 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2710 reinterpret_cast<const GrGLint*>(glValues)));
2711 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002712 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2713 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2714 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2715 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2716 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002717 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002718 }
2719 }
Brian Salomondc829942018-10-23 16:07:24 -04002720 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002721 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2722 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2723 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002724 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002725 this->setTextureUnit(unitIdx);
2726 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002727 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002728 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002729 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002730 this->setTextureUnit(unitIdx);
2731 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002732 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002733 }
Brian Salomon327955b2018-10-22 15:39:22 +00002734 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002735 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2736 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002737}
2738
Brian Salomon1f05d452019-02-08 12:33:08 -05002739void GrGLGpu::onResetTextureBindings() {
2740 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2741 GR_GL_TEXTURE_EXTERNAL};
2742 for (int i = 0; i < this->numTextureUnits(); ++i) {
2743 this->setTextureUnit(i);
2744 for (auto target : kTargets) {
2745 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2746 GL_CALL(BindTexture(target, 0));
2747 }
2748 }
2749 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2750 }
2751}
2752
egdaniel080e6732014-12-22 07:35:52 -08002753void GrGLGpu::flushColorWrite(bool writeColor) {
2754 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002755 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002756 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2757 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002758 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002759 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002760 } else {
2761 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002762 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002763 fHWWriteToColor = kYes_TriState;
2764 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002765 }
bsalomon3e791242014-12-17 13:43:13 -08002766}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002767
Chris Dalton674f77a2019-09-30 20:49:39 -06002768void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2769 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2770 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2771 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2772 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2773 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2774 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2775 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002776 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2777 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2778 GL_CALL(ClearColor(r, g, b, a));
2779 fHWClearColor[0] = r;
2780 fHWClearColor[1] = g;
2781 fHWClearColor[2] = b;
2782 fHWClearColor[3] = a;
2783 }
2784}
2785
bsalomon861e1032014-12-16 07:33:49 -08002786void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002787 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002788 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002789 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002790 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002791 }
2792}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002793
Brian Salomond978b902019-02-07 15:09:18 -05002794void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002795 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002796 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002797 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2798 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2799 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002800 }
Brian Salomond978b902019-02-07 15:09:18 -05002801 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2802 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002803 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002804 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002805}
2806
Brian Salomone5e7eb12016-10-14 16:18:33 -04002807// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002808static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2809 const GrSurface* src,
2810 const SkIRect& srcRect,
2811 const SkIPoint& dstPoint,
2812 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002813 int dstSampleCnt = 0;
2814 int srcSampleCnt = 0;
2815 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002816 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002817 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002818 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002819 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002820 }
2821 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2822 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2823
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002824 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2825 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2826
Brian Salomone5e7eb12016-10-14 16:18:33 -04002827 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002828 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002829
Greg Daniel46cfbc62019-06-07 11:43:30 -04002830 GrTextureType dstTexType;
2831 GrTextureType* dstTexTypePtr = nullptr;
2832 GrTextureType srcTexType;
2833 GrTextureType* srcTexTypePtr = nullptr;
2834 if (dstTex) {
2835 dstTexType = dstTex->texturePriv().textureType();
2836 dstTexTypePtr = &dstTexType;
2837 }
2838 if (srcTex) {
2839 srcTexType = srcTex->texturePriv().textureType();
2840 srcTexTypePtr = &srcTexType;
2841 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002842
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002843 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2844 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002845 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002846}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002847
Brian Osmana9c8a052018-01-19 10:31:56 -05002848static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2849 // A RT has a separate MSAA renderbuffer if:
2850 // 1) It's multisampled
2851 // 2) We're using an extension with separate MSAA renderbuffers
2852 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002853 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002854}
2855
Greg Daniel46cfbc62019-06-07 11:43:30 -04002856static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002857 const GrGLCaps& caps) {
2858
bsalomon@google.comeb851172013-04-15 13:51:00 +00002859 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002860 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002861 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002862 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002863
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002864 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2865 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2866
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002867 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2868 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2869
Greg Daniel46cfbc62019-06-07 11:43:30 -04002870 GrTextureType dstTexType;
2871 GrTextureType* dstTexTypePtr = nullptr;
2872 GrTextureType srcTexType;
2873 GrTextureType* srcTexTypePtr = nullptr;
2874 if (dstTex) {
2875 dstTexType = dstTex->texturePriv().textureType();
2876 dstTexTypePtr = &dstTexType;
2877 }
2878 if (srcTex) {
2879 srcTexType = srcTex->texturePriv().textureType();
2880 srcTexTypePtr = &srcTexType;
2881 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002882
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002883 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2884 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002885}
2886
Greg Danielacd66b42019-05-22 16:29:12 -04002887// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002888void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002889 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002890 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002891 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002892 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002893 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2894 GrGLuint texID = texture->textureID();
2895 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002896 GrGLuint* tempFBOID;
2897 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002898
egdanield803f272015-03-18 13:01:52 -07002899 if (0 == *tempFBOID) {
2900 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002901 }
2902
Adrienne Walker4ee88512018-05-17 11:37:14 -07002903 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002904 GR_GL_CALL(
2905 this->glInterface(),
2906 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2907 if (mipLevel == 0) {
2908 texture->baseLevelWasBoundToFBO();
2909 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002910 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002911 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002912 }
egdaniel0f5f9672015-02-03 11:10:51 -08002913}
2914
Brian Salomond2a8ae22019-09-10 16:03:59 -04002915void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002916 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002917 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002918 SkASSERT(surface->asTexture());
2919 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2920 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2921 GR_GL_COLOR_ATTACHMENT0,
2922 textureTarget,
2923 0,
2924 0));
2925 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002926}
2927
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002928void GrGLGpu::onFBOChanged() {
2929 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2930 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2931 GL_CALL(Flush());
2932 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002933#ifdef SK_DEBUG
2934 if (fIsExecutingCommandBuffer_DebugOnly) {
2935 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2936 "This will severely hurt performance.\n");
2937 }
2938#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002939}
2940
Adrienne Walker4ee88512018-05-17 11:37:14 -07002941void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2942 fStats.incRenderTargetBinds();
2943 GL_CALL(BindFramebuffer(target, fboid));
2944 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2945 fBoundDrawFramebuffer = fboid;
2946 }
2947
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002948 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2949 // The driver forgets the correct scissor when modifying the FBO binding.
2950 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002951 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002952 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002953 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002954 }
2955
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002956 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002957}
2958
Adrienne Walker4ee88512018-05-17 11:37:14 -07002959void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
2960 if (fboid == fBoundDrawFramebuffer &&
2961 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2962 // This workaround only applies to deleting currently bound framebuffers
2963 // on Adreno 420. Because this is a somewhat rare case, instead of
2964 // tracking all the attachments of every framebuffer instead just always
2965 // unbind all attachments.
2966 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2967 GR_GL_RENDERBUFFER, 0));
2968 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2969 GR_GL_RENDERBUFFER, 0));
2970 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2971 GR_GL_RENDERBUFFER, 0));
2972 }
2973
2974 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002975
2976 // Deleting the currently bound framebuffer rebinds to 0.
2977 if (fboid == fBoundDrawFramebuffer) {
2978 this->onFBOChanged();
2979 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002980}
2981
Greg Daniel46cfbc62019-06-07 11:43:30 -04002982bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002983 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002984 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2985 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2986 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002987 auto dstFormat = dst->backendFormat().asGLFormat();
2988 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002989 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002990 return true;
2991 }
2992 }
cblume61214052016-01-26 09:10:48 -08002993
Greg Daniel46cfbc62019-06-07 11:43:30 -04002994 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2995 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002996 return true;
2997 }
2998
Greg Daniel46cfbc62019-06-07 11:43:30 -04002999 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3000 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003001 }
3002
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003003 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003004 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003005 return true;
3006 }
bsalomon083617b2016-02-12 12:10:14 -08003007 }
3008
bsalomon6df86402015-06-01 10:41:49 -07003009 return false;
3010}
3011
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003012bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003013 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003014
3015 int progIdx = TextureToCopyProgramIdx(srcTex);
3016 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3017 GrSLType samplerType =
3018 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3019
3020 if (!fCopyProgramArrayBuffer) {
3021 static const GrGLfloat vdata[] = {
3022 0, 0,
3023 0, 1,
3024 1, 0,
3025 1, 1
3026 };
3027 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3028 kStatic_GrAccessPattern, vdata);
3029 }
3030 if (!fCopyProgramArrayBuffer) {
3031 return false;
3032 }
3033
3034 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3035 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3036 if (!fCopyPrograms[progIdx].fProgram) {
3037 return false;
3038 }
3039
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003040 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3041 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3042 GrShaderVar::kUniform_TypeModifier);
3043 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3044 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3045 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3046 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3047
Greg Daniel9e3169b2019-06-06 14:38:17 -04003048 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003049 if (shaderCaps->noperspectiveInterpolationSupport()) {
3050 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3051 vshaderTxt.appendf("#extension %s : require\n", extension);
3052 }
3053 vTexCoord.addModifier("noperspective");
3054 }
3055
3056 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3057 vshaderTxt.append(";");
3058 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3059 vshaderTxt.append(";");
3060 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3061 vshaderTxt.append(";");
3062 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3063 vshaderTxt.append(";");
3064
3065 vshaderTxt.append(
3066 "// Copy Program VS\n"
3067 "void main() {"
3068 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3069 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3070 " sk_Position.zw = half2(0, 1);"
3071 "}"
3072 );
3073
Greg Daniel9e3169b2019-06-06 14:38:17 -04003074 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003075 if (shaderCaps->noperspectiveInterpolationSupport()) {
3076 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3077 fshaderTxt.appendf("#extension %s : require\n", extension);
3078 }
3079 }
3080 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3081 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3082 fshaderTxt.append(";");
3083 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3084 fshaderTxt.append(";");
3085 fshaderTxt.appendf(
3086 "// Copy Program FS\n"
3087 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003088 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003089 "}"
3090 );
3091
3092 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3093 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3094 SkSL::Program::Settings settings;
3095 settings.fCaps = shaderCaps;
3096 SkSL::String glsl;
3097 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3098 sksl, settings, &glsl, errorHandler);
3099 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3100 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3101 SkASSERT(program->fInputs.isEmpty());
3102
3103 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3104 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3105 errorHandler);
3106 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3107 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3108 errorHandler);
3109 SkASSERT(program->fInputs.isEmpty());
3110
3111 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3112
3113 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3114 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3115 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3116 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3117 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3118 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3119
3120 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3121
3122 GL_CALL(DeleteShader(vshader));
3123 GL_CALL(DeleteShader(fshader));
3124
3125 return true;
3126}
3127
brianosman33f6b3f2016-06-02 05:49:21 -07003128bool GrGLGpu::createMipmapProgram(int progIdx) {
3129 const bool oddWidth = SkToBool(progIdx & 0x2);
3130 const bool oddHeight = SkToBool(progIdx & 0x1);
3131 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3132
Brian Salomon1edc5b92016-11-29 13:43:46 -05003133 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003134
3135 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3136 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3137 if (!fMipmapPrograms[progIdx].fProgram) {
3138 return false;
3139 }
3140
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003141 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3142 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003143 GrShaderVar::kUniform_TypeModifier);
3144 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3145 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003146 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003147 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003148 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3149 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3150 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3151 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003152 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003153 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003154
Ethan Nicholasfc994162019-06-06 10:04:27 -04003155 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003156 if (shaderCaps->noperspectiveInterpolationSupport()) {
3157 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003158 vshaderTxt.appendf("#extension %s : require\n", extension);
3159 }
3160 vTexCoords[0].addModifier("noperspective");
3161 vTexCoords[1].addModifier("noperspective");
3162 vTexCoords[2].addModifier("noperspective");
3163 vTexCoords[3].addModifier("noperspective");
3164 }
3165
Brian Salomon1edc5b92016-11-29 13:43:46 -05003166 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003167 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003168 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003169 vshaderTxt.append(";");
3170 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003171 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003172 vshaderTxt.append(";");
3173 }
3174
3175 vshaderTxt.append(
3176 "// Mipmap Program VS\n"
3177 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003178 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3179 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003180 );
3181
3182 // Insert texture coordinate computation:
3183 if (oddWidth && oddHeight) {
3184 vshaderTxt.append(
3185 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003186 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3187 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003188 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3189 );
3190 } else if (oddWidth) {
3191 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003192 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3193 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003194 );
3195 } else if (oddHeight) {
3196 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003197 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3198 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003199 );
3200 } else {
3201 vshaderTxt.append(
3202 " v_texCoord0 = a_vertex.xy;"
3203 );
3204 }
3205
3206 vshaderTxt.append("}");
3207
Ethan Nicholasfc994162019-06-06 10:04:27 -04003208 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003209 if (shaderCaps->noperspectiveInterpolationSupport()) {
3210 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003211 fshaderTxt.appendf("#extension %s : require\n", extension);
3212 }
3213 }
brianosman33f6b3f2016-06-02 05:49:21 -07003214 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003215 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003216 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003217 fshaderTxt.append(";");
3218 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003219 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003220 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003221 fshaderTxt.append(
3222 "// Mipmap Program FS\n"
3223 "void main() {"
3224 );
3225
3226 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003227 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003228 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3229 " sample(u_texture, v_texCoord1) + "
3230 " sample(u_texture, v_texCoord2) + "
3231 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003232 );
3233 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003234 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003235 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3236 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003237 );
3238 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003239 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003240 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003241 );
3242 }
3243
3244 fshaderTxt.append("}");
3245
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003246 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003247 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003248 SkSL::Program::Settings settings;
3249 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003250 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003251 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003252 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003253 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003254 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003255 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003256
Brian Osman6c431d52019-04-15 16:31:54 -04003257 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003258 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3259 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003260 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003261 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003262 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003263 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003264
3265 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3266
3267 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3268 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3269 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3270 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3271
3272 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3273
3274 GL_CALL(DeleteShader(vshader));
3275 GL_CALL(DeleteShader(fshader));
3276
3277 return true;
3278}
3279
Greg Daniel46cfbc62019-06-07 11:43:30 -04003280bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003281 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003282 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3283 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3284 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3285 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003286 return false;
3287 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003288 int progIdx = TextureToCopyProgramIdx(srcTex);
3289 if (!dstRT) {
3290 SkASSERT(dstTex);
3291 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3292 return false;
3293 }
3294 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003295 if (!fCopyPrograms[progIdx].fProgram) {
3296 if (!this->createCopyProgram(srcTex)) {
3297 SkDebugf("Failed to create copy program.\n");
3298 return false;
3299 }
3300 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 int w = srcRect.width();
3302 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003303 // We don't swizzle at all in our copies.
3304 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003305 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003306 this->flushViewport(dst->width(), dst->height());
3307 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003308 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003309 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003310 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003311 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3312 attribs->enableVertexArrays(this, 1);
3313 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3314 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003315 // dst rect edges in NDC (-1 to 1)
3316 int dw = dst->width();
3317 int dh = dst->height();
3318 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3319 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3320 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3321 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003322 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3323 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3324 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3325 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3326 int sw = src->width();
3327 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003328 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3329 // src rect edges in normalized texture space (0 to 1)
3330 sx0 /= sw;
3331 sx1 /= sw;
3332 sy0 /= sh;
3333 sy1 /= sh;
3334 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003335 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3336 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3337 sx1 - sx0, sy1 - sy0, sx0, sy0));
3338 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003339 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003340 this->flushHWAAState(nullptr, false);
3341 this->disableScissor();
3342 this->disableWindowRectangles();
3343 this->disableStencil();
3344 if (this->glCaps().srgbWriteControl()) {
3345 this->flushFramebufferSRGB(true);
3346 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003347 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003348 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003349 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3350 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003351 return true;
3352}
3353
Greg Daniel46cfbc62019-06-07 11:43:30 -04003354void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003355 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003356 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003357 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003358 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003359 SkASSERT(dstTex);
3360 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003361 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003362
Brian Salomond978b902019-02-07 15:09:18 -05003363 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003364 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003365 dstPoint.fX, dstPoint.fY,
3366 srcRect.fLeft, srcRect.fTop,
3367 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003368 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003369 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3370 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003371 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3372 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003373}
3374
Greg Daniel46cfbc62019-06-07 11:43:30 -04003375bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003376 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003377 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003378 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3379 srcRect.width(), srcRect.height());
3380 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003381 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003382 return false;
mtklein404b3b22015-05-18 09:29:10 -07003383 }
bsalomon5df6fee2015-05-18 06:26:15 -07003384 }
bsalomon6df86402015-06-01 10:41:49 -07003385
Brian Salomond2a8ae22019-09-10 16:03:59 -04003386 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3387 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003388 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003389 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003390
3391 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003392 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003393 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003394
Greg Daniel46cfbc62019-06-07 11:43:30 -04003395 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3396 srcRect.fTop,
3397 srcRect.fRight,
3398 srcRect.fBottom,
3399 dstRect.fLeft,
3400 dstRect.fTop,
3401 dstRect.fRight,
3402 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003403 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003404 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3405 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003406
3407 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3408 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003409 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003410}
3411
Brian Salomon930f9392018-06-20 16:25:26 -04003412bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3413 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003414 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003415 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003416 return false;
3417 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003418 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003419 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3420 // Uses draw calls to do a series of downsample operations to successive mips.
3421
3422 // The manual approach requires the ability to limit which level we're sampling and that the
3423 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003424 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003425 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003426 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003427 GL_CALL(GenerateMipmap(glTex->target()));
3428 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003429 }
3430
brianosman33f6b3f2016-06-02 05:49:21 -07003431 int width = texture->width();
3432 int height = texture->height();
3433 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003434 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003435
3436 // Create (if necessary), then bind temporary FBO:
3437 if (0 == fTempDstFBOID) {
3438 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3439 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003440 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003441 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003442
3443 // Bind the texture, to get things configured for filtering.
3444 // We'll be changing our base level further below:
3445 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003446 // The mipmap program does not do any swizzling.
3447 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003448
3449 // Vertex data:
3450 if (!fMipmapProgramArrayBuffer) {
3451 static const GrGLfloat vdata[] = {
3452 0, 0,
3453 0, 1,
3454 1, 0,
3455 1, 1
3456 };
Brian Salomonae64c192019-02-05 09:41:37 -05003457 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003458 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003459 }
3460 if (!fMipmapProgramArrayBuffer) {
3461 return false;
3462 }
3463
3464 fHWVertexArrayState.setVertexArrayID(this, 0);
3465
3466 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003467 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003468 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003469 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003470
3471 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003472 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003473 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003474 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003475 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003476 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003477
3478 // Do all the blits:
3479 width = texture->width();
3480 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003481
brianosman33f6b3f2016-06-02 05:49:21 -07003482 for (GrGLint level = 1; level < levelCount; ++level) {
3483 // Get and bind the program for this particular downsample (filter shape can vary):
3484 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3485 if (!fMipmapPrograms[progIdx].fProgram) {
3486 if (!this->createMipmapProgram(progIdx)) {
3487 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003488 // Invalidate all params to cover base level change in a previous iteration.
3489 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003490 return false;
3491 }
3492 }
Brian Salomon802cb312018-06-08 18:05:20 -04003493 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003494
3495 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3496 const float invWidth = 1.0f / width;
3497 const float invHeight = 1.0f / height;
3498 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3499 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3500 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3501
3502 // Only sample from previous mip
3503 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3504
Brian Salomon930f9392018-06-20 16:25:26 -04003505 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3506 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003507
3508 width = SkTMax(1, width / 2);
3509 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003510 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003511
3512 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3513 }
3514
3515 // Unbind:
3516 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3517 GR_GL_TEXTURE_2D, 0, 0));
3518
Brian Salomon930f9392018-06-20 16:25:26 -04003519 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003520 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3521 // We drew the 2nd to last level into the last level.
3522 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3523 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003524
brianosman33f6b3f2016-06-02 05:49:21 -07003525 return true;
3526}
3527
Chris Daltond7291ba2019-03-07 14:17:03 -07003528void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003529 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3530 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003531
3532 int effectiveSampleCnt;
3533 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003534 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003535
3536 sampleLocations->reset(effectiveSampleCnt);
3537 for (int i = 0; i < effectiveSampleCnt; ++i) {
3538 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3539 }
3540}
3541
cdalton231c5fd2015-05-13 12:35:36 -07003542void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003543 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003544 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003545 case kTexture_GrXferBarrierType: {
3546 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003547 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003548 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3549 // The render target uses separate storage so no need for glTextureBarrier.
3550 // FIXME: The render target will resolve automatically when its texture is bound,
3551 // but we could resolve only the bounds that will be read if we do it here instead.
3552 return;
3553 }
cdalton9954bc32015-04-29 14:17:00 -07003554 SkASSERT(this->caps()->textureBarrierSupport());
3555 GL_CALL(TextureBarrier());
3556 return;
cdalton231c5fd2015-05-13 12:35:36 -07003557 }
cdalton8917d622015-05-06 13:40:21 -07003558 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003559 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003560 this->caps()->blendEquationSupport());
3561 GL_CALL(BlendBarrier());
3562 return;
bsalomoncb02b382015-08-12 11:14:50 -07003563 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003564 }
3565}
3566
Brian Salomon5043f1f2019-07-11 21:27:54 -04003567static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003568 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003569 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3570 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3571 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3572 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3573 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3574 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3575 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3576 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3577 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003578 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003579 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003580 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3581 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3582 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3583 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003584
Brian Salomon5043f1f2019-07-11 21:27:54 -04003585 // Configs with multiple equivalent formats.
3586
Robert Phillipsebab03f2019-07-22 08:48:18 -04003587 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3588 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3589
Brian Salomon5043f1f2019-07-11 21:27:54 -04003590 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3591 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3592
3593 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3594 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003595 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003596 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003597}
3598
Robert Phillips57ef6802019-09-23 10:12:47 -04003599GrBackendTexture GrGLGpu::onCreateBackendTexture(int w, int h,
3600 const GrBackendFormat& format,
3601 GrMipMapped mipMapped,
3602 GrRenderable renderable,
3603 const SkPixmap srcData[], int numMipLevels,
3604 const SkColor4f* color,
3605 GrProtected isProtected) {
Brian Salomon8a375832018-03-14 10:21:40 -04003606 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003607
Robert Phillips57ef6802019-09-23 10:12:47 -04003608 SkDEBUGCODE(const GrCaps* caps = this->caps();)
3609
3610 // GrGpu::createBackendTexture should've ensured these conditions
3611 SkASSERT(w >= 1 && w <= caps->maxTextureSize() && h >= 1 && h <= caps->maxTextureSize());
3612 SkASSERT(GrGpu::MipMapsAreCorrect(w, h, mipMapped, srcData, numMipLevels));
3613 SkASSERT(mipMapped == GrMipMapped::kNo || caps->mipMapSupport());
3614
Brian Salomond4764a12019-08-08 12:08:24 -04003615 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003616 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003617 return GrBackendTexture(); // invalid
3618 }
3619
Robert Phillipsd34691b2019-09-24 13:38:43 -04003620 // Compressed formats go through onCreateCompressedBackendTexture
3621 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3622
Brian Salomon5043f1f2019-07-11 21:27:54 -04003623 GrPixelConfig config = gl_format_to_pixel_config(glFormat);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003624
Brian Salomon5043f1f2019-07-11 21:27:54 -04003625 if (config == kUnknown_GrPixelConfig) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003626 return GrBackendTexture(); // invalid
3627 }
3628
Greg Daniel15500642019-06-27 03:05:55 +00003629 GrGLTextureInfo info;
3630 GrGLTextureParameters::SamplerOverriddenState initialState;
3631
Brian Salomond2a8ae22019-09-10 16:03:59 -04003632 SkTDArray<GrMipLevel> texels;
Brian Salomonbb8dde82019-06-27 10:52:13 -04003633 SkAutoMalloc pixelStorage;
Robert Phillipsd34691b2019-09-24 13:38:43 -04003634
3635 int mipLevelCount = 1;
Brian Salomon4ab4e642019-10-24 08:59:29 -04003636 GrColorType textureAndDataColorType = GrColorType::kUnknown;
Robert Phillipsd34691b2019-09-24 13:38:43 -04003637 if (srcData) {
3638 mipLevelCount = numMipLevels;
Brian Salomon4ab4e642019-10-24 08:59:29 -04003639 textureAndDataColorType = SkColorTypeToGrColorType(srcData[0].colorType());
Robert Phillipsd34691b2019-09-24 13:38:43 -04003640 texels.append(mipLevelCount);
3641 for (int i = 0; i < mipLevelCount; ++i) {
3642 texels[i] = { srcData[i].addr(), srcData[i].rowBytes() };
3643 }
3644 } else if (color) {
Brian Salomon4ab4e642019-10-24 08:59:29 -04003645 // We don't currently communicate the intended color type in this case so reverse engineer
3646 // it from the config. Note this is lossy.
3647 textureAndDataColorType = GrPixelConfigToColorType(config);
3648
Robert Phillipsd34691b2019-09-24 13:38:43 -04003649 if (GrMipMapped::kYes == mipMapped) {
3650 mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
Robert Phillips57ef6802019-09-23 10:12:47 -04003651 }
3652
Robert Phillipsd34691b2019-09-24 13:38:43 -04003653 texels.append(mipLevelCount);
3654 SkTArray<size_t> individualMipOffsets(mipLevelCount);
3655
Greg Daniel7fd7a8a2019-10-10 16:10:31 -04003656 size_t bytesPerPixel = this->glCaps().bytesPerPixel(glFormat);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003657
3658 size_t totalSize = GrComputeTightCombinedBufferSize(
3659 bytesPerPixel, w, h, &individualMipOffsets, mipLevelCount);
3660
3661 char* tmpPixels = (char*)pixelStorage.reset(totalSize);
3662
Brian Salomon4ab4e642019-10-24 08:59:29 -04003663 GrFillInData(textureAndDataColorType, w, h, individualMipOffsets, tmpPixels, *color);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003664 for (int i = 0; i < mipLevelCount; ++i) {
3665 size_t offset = individualMipOffsets[i];
3666
3667 int twoToTheMipLevel = 1 << i;
3668 int currentWidth = SkTMax(1, w / twoToTheMipLevel);
3669
3670 texels[i] = {&(tmpPixels[offset]), currentWidth * bytesPerPixel};
Brian Salomonbb8dde82019-06-27 10:52:13 -04003671 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003672 }
Robert Phillips7f367982019-09-26 14:01:36 -04003673
Robert Phillipsd34691b2019-09-24 13:38:43 -04003674 GrSurfaceDesc desc;
3675 desc.fWidth = w;
3676 desc.fHeight = h;
3677 desc.fConfig = config;
Robert Phillips57ef6802019-09-23 10:12:47 -04003678
Robert Phillipsd34691b2019-09-24 13:38:43 -04003679 info.fTarget = GR_GL_TEXTURE_2D;
3680 info.fFormat = GrGLFormatToEnum(glFormat);
3681 info.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, glFormat, renderable,
3682 &initialState, SkTMax(1, texels.count()));
3683 if (!info.fID) {
3684 return GrBackendTexture(); // invalid
3685 }
Brian Salomon4ab4e642019-10-24 08:59:29 -04003686 if (!texels.empty()) {
3687 // TODO: move the texturability check up to GrGpu::createBackendTexture and just assert here
3688 if (!this->caps()->isFormatTexturableAndUploadable(textureAndDataColorType, format)) {
3689 return GrBackendTexture(); // invalid
3690 }
3691 if (!this->uploadTexData(glFormat, textureAndDataColorType, desc.fWidth, desc.fHeight,
3692 GR_GL_TEXTURE_2D, 0, 0, desc.fWidth, desc.fHeight,
3693 textureAndDataColorType, texels.begin(), texels.count())) {
3694 GL_CALL(DeleteTextures(1, &info.fID));
3695 return GrBackendTexture();
3696 }
Robert Phillipse3bd6732019-05-29 14:20:35 -04003697 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003698
Robert Phillipse8fabb22018-02-04 14:33:21 -05003699 // unbind the texture from the texture unit to avoid asserts
3700 GL_CALL(BindTexture(info.fTarget, 0));
3701
Brian Salomone2826ab2019-06-04 15:58:31 -04003702 auto parameters = sk_make_sp<GrGLTextureParameters>();
3703 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3704 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003705
3706 return GrBackendTexture(w, h, mipMapped, info, std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003707}
3708
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003709void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003710 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3711
3712 GrGLTextureInfo info;
3713 if (tex.getGLTextureInfo(&info)) {
3714 GL_CALL(DeleteTextures(1, &info.fID));
3715 }
3716}
3717
3718#if GR_TEST_UTILS
3719
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003720bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003721 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003722
Greg Daniel52e16d92018-04-10 09:34:07 -04003723 GrGLTextureInfo info;
3724 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003725 return false;
3726 }
3727
3728 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003729 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003730
3731 return (GR_GL_TRUE == result);
3732}
3733
Brian Salomonf865b052018-03-09 09:01:53 -05003734GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003735 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003736 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3737 return GrBackendRenderTarget(); // invalid
3738 }
Brian Salomon8a375832018-03-14 10:21:40 -04003739 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003740 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003741 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003742 return {};
3743 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003744 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003745 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003746 if (sFormatIdx < 0) {
3747 return {};
3748 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003749 GrGLuint colorID = 0;
3750 GrGLuint stencilID = 0;
3751 auto deleteIDs = [&] {
3752 if (colorID) {
3753 if (useTexture) {
3754 GL_CALL(DeleteTextures(1, &colorID));
3755 } else {
3756 GL_CALL(DeleteRenderbuffers(1, &colorID));
3757 }
Brian Salomonf865b052018-03-09 09:01:53 -05003758 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003759 if (stencilID) {
3760 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003761 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003762 };
3763
3764 if (useTexture) {
3765 GL_CALL(GenTextures(1, &colorID));
3766 } else {
3767 GL_CALL(GenRenderbuffers(1, &colorID));
3768 }
3769 GL_CALL(GenRenderbuffers(1, &stencilID));
3770 if (!stencilID || !colorID) {
3771 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003772 return {};
3773 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003774
Brian Salomonf865b052018-03-09 09:01:53 -05003775 GrGLFramebufferInfo info;
3776 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003777 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003778 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3779 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003780 deleteIDs();
3781 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003782 }
3783
3784 this->invalidateBoundRenderTarget();
3785
Adrienne Walker4ee88512018-05-17 11:37:14 -07003786 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003787 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003788 GrGLTextureParameters::SamplerOverriddenState initialState;
3789 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3790 if (!colorID) {
3791 deleteIDs();
3792 return {};
3793 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003794 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3795 colorID, 0));
3796 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003797 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003798 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3799 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003800 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003801 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3802 GR_GL_RENDERBUFFER, colorID));
3803 }
3804 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003805 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3806 GL_ALLOC_CALL(this->glInterface(),
3807 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3808 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003809 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003810 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3811 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003812 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003813 }
3814
Brian Salomon93348dd2018-08-29 12:56:23 -04003815 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3816 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3817 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3818 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003819 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003820 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003821
Adrienne Walker4ee88512018-05-17 11:37:14 -07003822 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003823 GrGLenum status;
3824 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3825 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003826 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003827 return {};
3828 }
3829 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003830
Greg Daniel108bb232018-07-03 16:18:29 -04003831 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003832 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003833 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003834}
3835
3836void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003837 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003838 GrGLFramebufferInfo info;
3839 if (backendRT.getGLFramebufferInfo(&info)) {
3840 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003841 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003842 }
3843 }
joshualitt8fd844f2015-12-02 13:36:47 -08003844}
3845
Greg Daniel26b50a42018-03-08 09:49:58 -05003846void GrGLGpu::testingOnly_flushGpuAndSync() {
3847 GL_CALL(Finish());
3848}
Brian Salomonf865b052018-03-09 09:01:53 -05003849#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003850
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003851///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003852
cdaltone2e71c22016-04-07 18:13:29 -07003853GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003854 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003855 GrGLAttribArrayState* attribState;
3856
cdaltone2e71c22016-04-07 18:13:29 -07003857 if (gpu->glCaps().isCoreProfile()) {
3858 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003859 GrGLuint arrayID;
3860 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3861 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003862 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003863 }
cdaltone2e71c22016-04-07 18:13:29 -07003864 if (ibuf) {
3865 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003866 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003867 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003868 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003869 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003870 if (ibuf) {
3871 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003872 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003873 } else {
3874 this->setVertexArrayID(gpu, 0);
3875 }
3876 int attrCount = gpu->glCaps().maxVertexAttributes();
3877 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3878 fDefaultVertexArrayAttribState.resize(attrCount);
3879 }
3880 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003881 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003882 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003883}
bsalomone179a912016-01-20 06:18:10 -08003884
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04003885void GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003886 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003887 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003888 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003889 // We call finish if the client told us to sync or if we have a finished proc but don't support
3890 // GLsync objects.
3891 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3892 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3893 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003894 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003895 // After a finish everything previously sent to GL is done.
3896 for (const auto& cb : fFinishCallbacks) {
3897 cb.fCallback(cb.fContext);
3898 this->deleteSync(cb.fSync);
3899 }
3900 fFinishCallbacks.clear();
3901 if (info.fFinishedProc) {
3902 info.fFinishedProc(info.fFinishedContext);
3903 }
3904 } else {
3905 if (info.fFinishedProc) {
3906 FinishCallback callback;
3907 callback.fCallback = info.fFinishedProc;
3908 callback.fContext = info.fFinishedContext;
3909 callback.fSync = (GrGLsync)this->insertFence();
3910 fFinishCallbacks.push_back(callback);
3911 GL_CALL(Flush());
3912 } else if (insertedSemaphore) {
3913 // Must call flush after semaphores in case they are waited on another GL context.
3914 GL_CALL(Flush());
3915 }
3916 // See if any previously inserted finish procs are good to go.
3917 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003918 }
Greg Daniel51316782017-08-02 15:10:09 +00003919}
3920
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003921void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3922 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3923 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3924 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003925}
3926
Greg Daniel6be35232017-03-01 17:01:09 -05003927GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003928 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003929 GrGLsync sync;
3930 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3931 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
3932 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003933}
3934
Brian Salomonb0d8b762019-05-06 16:58:22 -04003935bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
3936 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07003937 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04003938 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3939 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3940}
3941
3942bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3943 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003944}
3945
3946void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003947 this->deleteSync((GrGLsync)fence);
3948}
3949
Greg Daniela5cb7812017-06-16 09:45:32 -04003950sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003951 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003952 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003953}
3954
Greg Daniel48661b82018-01-22 16:11:35 -05003955sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
3956 GrResourceProvider::SemaphoreWrapType wrapType,
3957 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003958 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003959 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3960}
3961
Greg Daniel858e12c2018-12-06 11:11:37 -05003962void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05003963 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
3964
Greg Daniel48661b82018-01-22 16:11:35 -05003965 GrGLsync sync;
3966 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3967 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05003968}
3969
Greg Daniel48661b82018-01-22 16:11:35 -05003970void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05003971 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
3972
3973 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3974}
3975
Brian Salomonb0d8b762019-05-06 16:58:22 -04003976void GrGLGpu::checkFinishProcs() {
3977 // Bail after the first unfinished sync since we expect they signal in the order inserted.
3978 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
3979 /* timeout = */ 0, /* flush = */ false)) {
3980 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
3981 this->deleteSync(fFinishCallbacks.front().fSync);
3982 fFinishCallbacks.pop_front();
3983 }
3984}
3985
Greg Daniel6be35232017-03-01 17:01:09 -05003986void GrGLGpu::deleteSync(GrGLsync sync) const {
3987 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07003988}
Brian Osman13dddce2017-05-09 13:19:50 -04003989
Robert Phillips65a88fa2017-08-08 08:36:22 -04003990void GrGLGpu::insertEventMarker(const char* msg) {
3991 GL_CALL(InsertEventMarker(strlen(msg), msg));
3992}
3993
Brian Osman13dddce2017-05-09 13:19:50 -04003994sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
3995 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04003996 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05003997 this->insertSemaphore(semaphore);
3998 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
3999 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004000
4001 return semaphore;
4002}
Robert Phillips646e4292017-06-13 12:44:56 -04004003
4004int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004005 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004006 case kTexture2DSampler_GrSLType:
4007 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004008 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004009 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004010 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004011 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004012 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004013 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004014 }
4015}
Brian Osman71a18892017-08-10 10:23:25 -04004016
Kevin Lubickf4def342018-10-04 12:52:50 -04004017#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004018#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004019void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4020 // We are called by the base class, which has already called beginObject(). We choose to nest
4021 // all of our caps information in a named sub-object.
4022 writer->beginObject("GL GPU");
4023
4024 const GrGLubyte* str;
4025 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4026 writer->appendString("GL_VERSION", (const char*)(str));
4027 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4028 writer->appendString("GL_RENDERER", (const char*)(str));
4029 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4030 writer->appendString("GL_VENDOR", (const char*)(str));
4031 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4032 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4033
4034 writer->appendName("extensions");
4035 glInterface()->fExtensions.dumpJSON(writer);
4036
4037 writer->endObject();
4038}
Kevin Lubickf4def342018-10-04 12:52:50 -04004039#endif