blob: 300ff8a2c9cb9fc2117b65eb10f6b51715eb790e [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkMipMap.h"
20#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040021#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040023#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrFixedClip.h"
25#include "src/gpu/GrGpuResourcePriv.h"
26#include "src/gpu/GrMesh.h"
27#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040028#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
30#include "src/gpu/GrShaderCaps.h"
31#include "src/gpu/GrSurfaceProxyPriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/gl/GrGLBuffer.h"
34#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000047#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
48 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000052 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
53 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
54 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
55#endif
56
Jim Van Verth32ac83e2016-11-28 15:23:57 -050057//#define USE_NSIGHT
58
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000059///////////////////////////////////////////////////////////////////////////////
60
cdalton8917d622015-05-06 13:40:21 -070061static const GrGLenum gXfermodeEquation2Blend[] = {
62 // Basic OpenGL blend equations.
63 GR_GL_FUNC_ADD,
64 GR_GL_FUNC_SUBTRACT,
65 GR_GL_FUNC_REVERSE_SUBTRACT,
66
67 // GL_KHR_blend_equation_advanced.
68 GR_GL_SCREEN,
69 GR_GL_OVERLAY,
70 GR_GL_DARKEN,
71 GR_GL_LIGHTEN,
72 GR_GL_COLORDODGE,
73 GR_GL_COLORBURN,
74 GR_GL_HARDLIGHT,
75 GR_GL_SOFTLIGHT,
76 GR_GL_DIFFERENCE,
77 GR_GL_EXCLUSION,
78 GR_GL_MULTIPLY,
79 GR_GL_HSL_HUE,
80 GR_GL_HSL_SATURATION,
81 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050082 GR_GL_HSL_LUMINOSITY,
83
84 // Illegal... needs to map to something.
85 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070086};
Brian Salomon4dea72a2019-12-18 10:43:10 -050087static_assert(0 == kAdd_GrBlendEquation);
88static_assert(1 == kSubtract_GrBlendEquation);
89static_assert(2 == kReverseSubtract_GrBlendEquation);
90static_assert(3 == kScreen_GrBlendEquation);
91static_assert(4 == kOverlay_GrBlendEquation);
92static_assert(5 == kDarken_GrBlendEquation);
93static_assert(6 == kLighten_GrBlendEquation);
94static_assert(7 == kColorDodge_GrBlendEquation);
95static_assert(8 == kColorBurn_GrBlendEquation);
96static_assert(9 == kHardLight_GrBlendEquation);
97static_assert(10 == kSoftLight_GrBlendEquation);
98static_assert(11 == kDifference_GrBlendEquation);
99static_assert(12 == kExclusion_GrBlendEquation);
100static_assert(13 == kMultiply_GrBlendEquation);
101static_assert(14 == kHSLHue_GrBlendEquation);
102static_assert(15 == kHSLSaturation_GrBlendEquation);
103static_assert(16 == kHSLColor_GrBlendEquation);
104static_assert(17 == kHSLLuminosity_GrBlendEquation);
105static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
bsalomon861e1032014-12-16 07:33:49 -0800133bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000134 static const bool gCoeffReferencesBlendConst[] = {
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 true,
146 true,
147 true,
148 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
150 // extended blend coeffs
151 false,
152 false,
153 false,
154 false,
Mike Klein36743362018-11-06 08:23:30 -0500155
156 // Illegal.
157 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000158 };
159 return gCoeffReferencesBlendConst[coeff];
Brian Salomon4dea72a2019-12-18 10:43:10 -0500160 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
Brian Salomon4dea72a2019-12-18 10:43:10 -0500162 static_assert(0 == kZero_GrBlendCoeff);
163 static_assert(1 == kOne_GrBlendCoeff);
164 static_assert(2 == kSC_GrBlendCoeff);
165 static_assert(3 == kISC_GrBlendCoeff);
166 static_assert(4 == kDC_GrBlendCoeff);
167 static_assert(5 == kIDC_GrBlendCoeff);
168 static_assert(6 == kSA_GrBlendCoeff);
169 static_assert(7 == kISA_GrBlendCoeff);
170 static_assert(8 == kDA_GrBlendCoeff);
171 static_assert(9 == kIDA_GrBlendCoeff);
172 static_assert(10 == kConstC_GrBlendCoeff);
173 static_assert(11 == kIConstC_GrBlendCoeff);
174 static_assert(12 == kConstA_GrBlendCoeff);
175 static_assert(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176
Brian Salomon4dea72a2019-12-18 10:43:10 -0500177 static_assert(14 == kS2C_GrBlendCoeff);
178 static_assert(15 == kIS2C_GrBlendCoeff);
179 static_assert(16 == kS2A_GrBlendCoeff);
180 static_assert(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000181
182 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
Brian Salomon4dea72a2019-12-18 10:43:10 -0500183 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000184}
185
Brian Salomond978b902019-02-07 15:09:18 -0500186//////////////////////////////////////////////////////////////////////////////
187
188static int gl_target_to_binding_index(GrGLenum target) {
189 switch (target) {
190 case GR_GL_TEXTURE_2D:
191 return 0;
192 case GR_GL_TEXTURE_RECTANGLE:
193 return 1;
194 case GR_GL_TEXTURE_EXTERNAL:
195 return 2;
196 }
197 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500198}
199
200GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500201 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
202}
203
204bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
205 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500206}
207
208void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500209 int targetIndex = gl_target_to_binding_index(target);
210 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
211 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500212}
213
Brian Salomon1f05d452019-02-08 12:33:08 -0500214void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
215 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500216}
217
Brian Salomon1f05d452019-02-08 12:33:08 -0500218void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
219 for (auto& targetBinding : fTargetBindings) {
220 targetBinding.fBoundResourceID.makeInvalid();
221 if (markUnmodified) {
222 targetBinding.fHasBeenModified = false;
223 }
Brian Salomond978b902019-02-07 15:09:18 -0500224 }
225}
226
227//////////////////////////////////////////////////////////////////////////////
228
Brian Salomondc829942018-10-23 16:07:24 -0400229static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
230 switch (filter) {
231 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
232 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
233 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
234 }
235 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400236}
237
238static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
239 switch (filter) {
240 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
241 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
242 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
243 }
244 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400245}
246
Michael Ludwigf23a1522018-12-10 11:36:13 -0500247static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
248 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400249 switch (wrapMode) {
250 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
251 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
252 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500253 case GrSamplerState::WrapMode::kClampToBorder:
254 // May not be supported but should have been caught earlier
255 SkASSERT(caps.clampToBorderSupport());
256 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500257 }
Brian Salomondc829942018-10-23 16:07:24 -0400258 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400259}
260
261///////////////////////////////////////////////////////////////////////////////
262
263class GrGLGpu::SamplerObjectCache {
264public:
265 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
266 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
267 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
268 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
269 std::fill_n(fSamplers, kNumSamplers, 0);
270 }
271
272 ~SamplerObjectCache() {
273 if (!fNumTextureUnits) {
274 // We've already been abandoned.
275 return;
276 }
Chris Dalton703fe682019-05-15 12:58:12 -0600277 for (GrGLuint sampler : fSamplers) {
278 // The spec states that "zero" values should be silently ignored, however they still
279 // trigger GL errors on some NVIDIA platforms.
280 if (sampler) {
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
282 }
283 }
Brian Salomondc829942018-10-23 16:07:24 -0400284 }
285
286 void bindSampler(int unitIdx, const GrSamplerState& state) {
287 int index = StateToIndex(state);
288 if (!fSamplers[index]) {
289 GrGLuint s;
290 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
291 if (!s) {
292 return;
293 }
294 fSamplers[index] = s;
295 auto minFilter = filter_to_gl_min_filter(state.filter());
296 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500297 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
298 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400299 GR_GL_CALL(fGpu->glInterface(),
300 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
301 GR_GL_CALL(fGpu->glInterface(),
302 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
303 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
304 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
305 }
306 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
307 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
308 fHWBoundSamplers[unitIdx] = fSamplers[index];
309 }
310 }
311
312 void invalidateBindings() {
313 // When we have sampler support we always use samplers. So setting these to zero will cause
314 // a rebind on next usage.
315 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
316 }
317
318 void abandon() {
319 fHWBoundSamplers.reset();
320 fNumTextureUnits = 0;
321 }
322
323 void release() {
324 if (!fNumTextureUnits) {
325 // We've already been abandoned.
326 return;
327 }
328 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
329 std::fill_n(fSamplers, kNumSamplers, 0);
330 // Deleting a bound sampler implicitly binds sampler 0.
331 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
332 }
333
334private:
335 static int StateToIndex(const GrSamplerState& state) {
336 int filter = static_cast<int>(state.filter());
337 SkASSERT(filter >= 0 && filter < 3);
338 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500339 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400340 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500341 SkASSERT(wrapY >= 0 && wrapY < 4);
342 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400343 SkASSERT(idx < kNumSamplers);
344 return idx;
345 }
346
347 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500348 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400349 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
350 GrGLuint fSamplers[kNumSamplers];
351 int fNumTextureUnits;
352};
353
reed@google.comac10a2d2010-12-22 21:39:39 +0000354///////////////////////////////////////////////////////////////////////////////
355
Brian Salomon384fab42017-12-07 12:33:05 -0500356sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
357 GrContext* context) {
358 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500359 interface = GrGLMakeNativeInterface();
360 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
361 // to GrGLMakeNativeInterface.
362 if (!interface) {
363 interface = sk_ref_sp(GrGLCreateNativeInterface());
364 }
Brian Salomon384fab42017-12-07 12:33:05 -0500365 if (!interface) {
366 return nullptr;
367 }
bsalomon424cc262015-05-22 10:37:30 -0700368 }
Jim Van Verth76334772017-05-05 16:46:05 -0400369#ifdef USE_NSIGHT
370 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
371#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500372 auto glContext = GrGLContext::Make(std::move(interface), options);
373 if (!glContext) {
374 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700375 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500376 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700377}
378
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500379GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
380 : GrGpu(context)
381 , fGLContext(std::move(ctx))
382 , fProgramCache(new ProgramCache(this))
383 , fHWProgramID(0)
384 , fTempSrcFBOID(0)
385 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500386 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500387 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400388 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500389 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000390
Brian Salomond978b902019-02-07 15:09:18 -0500391 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000392
Brian Salomonae64c192019-02-05 09:41:37 -0500393 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
394 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700395 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500396 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
397 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
398 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
399 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700400 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500401 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
402 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700403 }
Brian Salomonae64c192019-02-05 09:41:37 -0500404 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400405 fHWBufferState[i].invalidate();
406 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500407 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700408
409 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
410 fPathRendering.reset(new GrGLPathRendering(this));
411 }
412
Brian Salomondc829942018-10-23 16:07:24 -0400413 if (this->glCaps().samplerObjectSupport()) {
414 fSamplerObjectCache.reset(new SamplerObjectCache(this));
415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416}
417
bsalomon861e1032014-12-16 07:33:49 -0800418GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700419 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
420 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800421 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700422 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700423 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800424
Brian Salomon802cb312018-06-08 18:05:20 -0400425 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400426 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000427 // detach the current program so there is no confusion on OpenGL's part
428 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000429 GL_CALL(UseProgram(0));
430 }
431
Brian Salomon43f8bf02017-10-18 08:33:29 -0400432 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700433 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800434 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400435 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700436 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800437 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400438 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700439 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800440 }
egdaniel0f5f9672015-02-03 11:10:51 -0800441
bsalomon7ea33f52015-11-22 14:51:00 -0800442 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
443 if (0 != fCopyPrograms[i].fProgram) {
444 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
445 }
bsalomon6df86402015-06-01 10:41:49 -0700446 }
bsalomon6dea83f2015-12-03 12:58:06 -0800447
brianosman33f6b3f2016-06-02 05:49:21 -0700448 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
449 if (0 != fMipmapPrograms[i].fProgram) {
450 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
451 }
452 }
453
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000454 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400455 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700456}
457
bsalomon6e2aad42016-04-01 11:54:31 -0700458void GrGLGpu::disconnect(DisconnectType type) {
459 INHERITED::disconnect(type);
460 if (DisconnectType::kCleanup == type) {
461 if (fHWProgramID) {
462 GL_CALL(UseProgram(0));
463 }
464 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700465 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700466 }
467 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700468 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700469 }
470 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700471 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700472 }
bsalomon6e2aad42016-04-01 11:54:31 -0700473 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
474 if (fCopyPrograms[i].fProgram) {
475 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
476 }
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
479 if (fMipmapPrograms[i].fProgram) {
480 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
481 }
482 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400483
Brian Salomondc829942018-10-23 16:07:24 -0400484 if (fSamplerObjectCache) {
485 fSamplerObjectCache->release();
486 }
bsalomon6e2aad42016-04-01 11:54:31 -0700487 } else {
488 if (fProgramCache) {
489 fProgramCache->abandon();
490 }
Brian Salomondc829942018-10-23 16:07:24 -0400491 if (fSamplerObjectCache) {
492 fSamplerObjectCache->abandon();
493 }
bsalomon6e2aad42016-04-01 11:54:31 -0700494 }
495
Brian Salomon802cb312018-06-08 18:05:20 -0400496 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700497 delete fProgramCache;
498 fProgramCache = nullptr;
499
bsalomonc8dc1f72014-08-21 13:02:13 -0700500 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700501 fTempSrcFBOID = 0;
502 fTempDstFBOID = 0;
503 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700504 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800505 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
506 fCopyPrograms[i].fProgram = 0;
507 }
brianosman33f6b3f2016-06-02 05:49:21 -0700508 fMipmapProgramArrayBuffer.reset();
509 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
510 fMipmapPrograms[i].fProgram = 0;
511 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400512
jvanverthe9c0fc62015-04-29 11:18:05 -0700513 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700514 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700515 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000516}
517
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000518///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000519
bsalomon861e1032014-12-16 07:33:49 -0800520void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400522 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 GL_CALL(Disable(GR_GL_DEPTH_TEST));
524 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000525
Brian Salomonf0861672017-05-08 11:10:10 -0400526 // We don't use face culling.
527 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600528 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
529 // just set this to the default for self-consistency.
530 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400531
Brian Salomonae64c192019-02-05 09:41:37 -0500532 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
533 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700534
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400535 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500536#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000537 // Desktop-only state that we never change
538 if (!this->glCaps().isCoreProfile()) {
539 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
540 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
541 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
542 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
543 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
544 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
545 }
546 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
547 // core profile. This seems like a bug since the core spec removes any mention of
548 // GL_ARB_imaging.
549 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
550 GL_CALL(Disable(GR_GL_COLOR_TABLE));
551 }
552 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400553
Chris Dalton1215cda2019-12-17 21:44:04 -0700554 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500555#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000556 // Since ES doesn't support glPointSize at all we always use the VS to
557 // set the point size
558 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
559
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000560 }
joshualitt58162332014-08-01 06:44:53 -0700561
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400562 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500563 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700564 // The arm extension requires specifically enabling MSAA fetching per sample.
565 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500566 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700567 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000568 fHWWriteToColor = kUnknown_TriState;
569 // we only ever use lines in hairline mode
570 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700571 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500572
573 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000574 }
edisonn@google.comba669992013-06-28 16:03:21 +0000575
egdanielb414f252014-07-29 13:15:47 -0700576 if (resetBits & kMSAAEnable_GrGLBackendState) {
577 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700578
Chris Dalton6ce447a2019-06-23 18:07:38 -0600579 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800580 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
581 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700582 GL_CALL(CoverageModulation(GR_GL_RGBA));
583 }
Chris Daltonce425af2019-12-16 10:39:03 -0700584
585 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000588 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400589 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500592 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500593 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 }
Brian Salomondc829942018-10-23 16:07:24 -0400595 if (fSamplerObjectCache) {
596 fSamplerObjectCache->invalidateBindings();
597 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000598 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000599
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 if (resetBits & kBlend_GrGLBackendState) {
601 fHWBlendState.invalidate();
602 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kView_GrGLBackendState) {
605 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700606 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 fHWViewport.invalidate();
608 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kStencil_GrGLBackendState) {
611 fHWStencilSettings.invalidate();
612 fHWStencilTestEnabled = kUnknown_TriState;
613 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000614
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 // Vertex
616 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700617 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500618 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
619 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400635 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
Brian Salomon1047a492019-07-02 12:25:21 -0400638 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400650 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000651}
652
Brian Salomonea4ad302019-08-07 13:04:55 -0400653static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
654 const GrGLCaps& caps, GrGLTexture::Desc* desc,
655 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400656 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400657 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800659 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000660
Brian Salomonea4ad302019-08-07 13:04:55 -0400661 desc->fSize = {backendTex.width(), backendTex.height()};
662 desc->fTarget = info.fTarget;
663 desc->fID = info.fID;
664 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800665
Brian Salomonea4ad302019-08-07 13:04:55 -0400666 if (desc->fFormat == GrGLFormat::kUnknown) {
667 return false;
668 }
669 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400670 if (!caps.shaderCaps()->externalTextureSupport()) {
671 return false;
672 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400673 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
674 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400675 return false;
676 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400677 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400678 return false;
679 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400680 if (backendTex.isProtected()) {
681 // Not supported in GL backend at this time.
682 return false;
683 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400684
685 desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType);
686 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
687
Brian Salomond17f6582017-07-19 18:28:58 -0400688 return true;
689}
690
691sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400692 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400693 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400694 GrGLTexture::Desc desc;
695 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400696 return nullptr;
697 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400698
Brian Salomond17f6582017-07-19 18:28:58 -0400699 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400700 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400701 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400702 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400703 }
bsalomone5286e02016-01-14 09:24:09 -0800704
Greg Daniel177e6952017-10-12 12:27:11 -0400705 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
706 : GrMipMapsStatus::kNotAllocated;
707
Brian Salomonea4ad302019-08-07 13:04:55 -0400708 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400709 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400710 // We don't know what parameters are already set on wrapped textures.
711 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400712 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400713}
714
Robert Phillipsb915c942019-12-17 14:44:37 -0500715sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
716 GrWrapOwnership ownership,
717 GrWrapCacheable cacheable) {
718 return nullptr;
719}
720
Brian Salomond17f6582017-07-19 18:28:58 -0400721sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400722 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400723 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500724 GrWrapOwnership ownership,
725 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400726 const GrGLCaps& caps = this->glCaps();
727
Brian Salomonea4ad302019-08-07 13:04:55 -0400728 GrGLTexture::Desc desc;
729 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800730 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800731 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400732 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
733 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800734
Brian Salomond17f6582017-07-19 18:28:58 -0400735 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400736 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800737 return nullptr;
738 }
bsalomon10528f12015-10-14 12:54:52 -0700739
Brian Osman766fcbb2017-03-13 09:33:09 -0400740 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400741 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400742 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400743 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800744 }
bsalomonb15b4c12014-10-29 12:41:57 -0700745
Robert Phillips0902c982019-07-16 07:47:56 -0400746
Greg Daniel6fa62e22019-08-07 15:52:37 -0400747 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
748 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000749
Brian Salomonea4ad302019-08-07 13:04:55 -0400750 GrGLRenderTarget::IDs rtIDs;
751 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400752 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000753 }
Greg Daniel177e6952017-10-12 12:27:11 -0400754
755 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
756 : GrMipMapsStatus::kNotAllocated;
757
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500758 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400759 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400760 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400761 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400762 // We don't know what parameters are already set on wrapped textures.
763 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400764 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000765}
766
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400767sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
768 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400769 GrGLFramebufferInfo info;
770 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000771 return nullptr;
772 }
773
Brian Salomone8a766b2019-07-19 14:24:36 -0400774 if (backendRT.isProtected()) {
775 // Not supported in GL at this time.
776 return nullptr;
777 }
778
Brian Salomond4764a12019-08-08 12:08:24 -0400779 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400780 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
781 return nullptr;
782 }
783
Brian Salomonea4ad302019-08-07 13:04:55 -0400784 GrGLRenderTarget::IDs rtIDs;
785 rtIDs.fRTFBOID = info.fFBOID;
786 rtIDs.fMSColorRenderbufferID = 0;
787 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
788 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000789
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400790 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
791 grColorType);
Brian Salomond4764a12019-08-08 12:08:24 -0400792 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400793
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400795 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700796
Brian Salomonea4ad302019-08-07 13:04:55 -0400797 return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400798 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000799}
800
Greg Daniel7ef28f32017-04-20 16:41:55 +0000801sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400802 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400803 GrColorType colorType) {
804 GrGLTexture::Desc desc;
805 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
806 // with a texture rectangle,we assume the necessary support exists.
807 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800808 return nullptr;
809 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400810
811 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
812 return nullptr;
813 }
814
815 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400816 GrGLRenderTarget::IDs rtIDs;
817 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800818 return nullptr;
819 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400820 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount,
821 rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800822}
823
Brian Salomonc320b152018-02-20 14:05:36 -0500824static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700825 if (!glTex) {
826 return false;
827 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000828
bsalomone5286e02016-01-14 09:24:09 -0800829 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
830 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800831 return false;
832 }
833
jvanverth17aa0472016-01-05 10:41:27 -0800834 return true;
835}
836
Brian Salomona9b04b92018-06-01 15:04:28 -0400837bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400838 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400839 const GrMipLevel texels[], int mipLevelCount,
840 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500841 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800842
Brian Salomonc320b152018-02-20 14:05:36 -0500843 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800844 return false;
845 }
846
Brian Salomond978b902019-02-07 15:09:18 -0500847 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000848
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400849 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400850 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400851 glTex->target(), left, top, width, height, srcColorType, texels,
852 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000853}
854
Brian Salomone05ba5a2019-04-08 11:59:07 -0400855bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400856 GrColorType textureColorType, GrColorType bufferColorType,
857 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859
Jim Van Verth1676cb92019-01-15 13:24:45 -0500860 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400861 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500862
Brian Salomonc320b152018-02-20 14:05:36 -0500863 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400864 return false;
865 }
866
Hal Canaryc36f7e72018-06-18 15:50:09 -0400867 static_assert(sizeof(int) == sizeof(int32_t), "");
868 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400869 return false;
870 }
871
Brian Salomond978b902019-02-07 15:09:18 -0500872 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400873
874 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500875 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400876 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500877 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400878
Greg Daniel660cc992017-06-26 14:55:05 -0400879 SkDEBUGCODE(
880 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
881 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
882 SkASSERT(bounds.contains(subRect));
883 )
884
Brian Salomonb28cb682019-07-26 12:48:47 -0400885 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400886 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400887 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700888 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400889 return false;
890 }
891
892 bool restoreGLRowLength = false;
893 if (trimRowBytes != rowBytes) {
894 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400895 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
897 restoreGLRowLength = true;
898 }
899
Greg Danielba88ab62019-07-26 09:14:01 -0400900 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400902 GrGLenum externalFormat = 0;
903 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400904 this->glCaps().getTexSubImageExternalFormatAndType(
905 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400906 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 return false;
908 }
909
Brian Salomon8cbf6622019-07-30 11:03:14 -0400910 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911 GL_CALL(TexSubImage2D(glTex->target(),
912 0,
913 left, top,
914 width,
915 height,
916 externalFormat, externalType,
917 pixels));
918
919 if (restoreGLRowLength) {
920 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
921 }
922
923 return true;
924}
925
Brian Salomon26de56e2019-04-10 12:14:26 -0400926bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400927 GrColorType surfaceColorType, GrColorType dstColorType,
928 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400929 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
930 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400931 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400932 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
933 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400934}
935
Brian Osman9b560242017-09-05 15:34:52 -0400936void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500937 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
938 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
939 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
940 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400941 }
Brian Osman9b560242017-09-05 15:34:52 -0400942}
943
Brian Salomond2a8ae22019-09-10 16:03:59 -0400944bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
945 int texHeight, GrGLenum target, int left, int top, int width,
946 int height, GrColorType srcColorType, const GrMipLevel texels[],
947 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500948 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400949 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500950
Greg Daniel7bfc9132019-08-14 14:23:53 -0400951 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400952 SkDEBUGCODE(
953 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
954 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
955 SkASSERT(bounds.contains(subRect));
956 )
Robert Phillips590533f2017-07-11 14:22:35 -0400957 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400958 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800959
Brian Osman9b560242017-09-05 15:34:52 -0400960 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400961
cblume55f2d2d2016-02-26 13:20:48 -0800962 const GrGLInterface* interface = this->glInterface();
963 const GrGLCaps& caps = this->glCaps();
964
Brian Salomonf77c1462019-08-01 15:19:29 -0400965 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800966
967 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000968 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000969 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000970
bsalomon5b30c6f2015-12-17 14:17:34 -0800971 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800972 GrGLenum externalFormat;
973 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400974 this->glCaps().getTexSubImageExternalFormatAndType(
975 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400976 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800977 return false;
978 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400979
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000980 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800981 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000982 * because our srcData has extra bytes past each row. If so, we need
983 * to trim those off here, since GL ES may not let us specify
984 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000985 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000986 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -0800987
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400988 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600989 *mipMapsStatus = (mipLevelCount > 1) ?
990 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -0800991 }
bsalomone699d0c2016-03-09 06:25:15 -0800992
Brian Salomond2a8ae22019-09-10 16:03:59 -0400993 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -0800994
Brian Salomond2a8ae22019-09-10 16:03:59 -0400995 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
996 if (!texels[currentMipLevel].fPixels) {
997 if (mipMapsStatus) {
998 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400999 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001000 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001001 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001002 int twoToTheMipLevel = 1 << currentMipLevel;
1003 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1004 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1005 const size_t trimRowBytes = currentWidth * bpp;
1006 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1007
1008 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1009 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1010 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1011 restoreGLRowLength = true;
1012 }
1013
1014 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1015 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001016 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001017 if (restoreGLRowLength) {
1018 SkASSERT(caps.writePixelsRowBytesSupport());
1019 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1020 }
1021 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001022}
1023
Greg Daniel7bfc9132019-08-14 14:23:53 -04001024bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
1025 SkImage::CompressionType compressionType,
Robert Phillipsb915c942019-12-17 14:44:37 -05001026 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001027 GrGLenum target,
1028 const void* data) {
1029 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001030 const GrGLCaps& caps = this->glCaps();
1031
1032 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001034 if (!internalFormat) {
1035 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001036 }
1037
Greg Daniel7bfc9132019-08-14 14:23:53 -04001038 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001039
1040 static constexpr int kMipLevelCount = 1;
1041
1042 // Make sure that the width and height that we pass to OpenGL
1043 // is a multiple of the block size.
Robert Phillips48257d72019-12-16 14:20:53 -05001044 size_t dataSize = GrCompressedDataSize(compressionType, dimensions, GrMipMapped::kNo);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001045
1046 if (useTexStorage) {
1047 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001048 GL_ALLOC_CALL(this->glInterface(),
1049 TexStorage2D(target, kMipLevelCount, internalFormat, dimensions.width(),
1050 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001051 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1052 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001053 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001054 }
1055 GL_CALL(CompressedTexSubImage2D(target,
1056 0, // level
1057 0, // left
1058 0, // top
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001059 dimensions.width(),
1060 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001061 internalFormat,
1062 SkToInt(dataSize),
1063 data));
Robert Phillips42716d42019-12-16 12:19:54 -05001064
1065 error = CHECK_ALLOC_ERROR(this->glInterface());
1066 if (error != GR_GL_NO_ERROR) {
1067 return false;
1068 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001069 } else {
1070 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1071 0, // level
1072 internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001073 dimensions.width(),
1074 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001075 0, // border
1076 SkToInt(dataSize),
1077 data));
1078
1079 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1080 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001081 return false;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001082 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001083 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001084 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001085}
1086
bsalomon424cc262015-05-22 10:37:30 -07001087static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001088 int sampleCount,
1089 GrGLenum format,
1090 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001091 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001092 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001093 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001094 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001095 GL_ALLOC_CALL(ctx.interface(),
1096 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1097 sampleCount,
1098 format,
1099 width, height));
1100 break;
1101 case GrGLCaps::kES_Apple_MSFBOType:
1102 GL_ALLOC_CALL(ctx.interface(),
1103 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1104 sampleCount,
1105 format,
1106 width, height));
1107 break;
1108 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1109 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1110 GL_ALLOC_CALL(ctx.interface(),
1111 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1112 sampleCount,
1113 format,
1114 width, height));
1115 break;
1116 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001117 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001118 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001119 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001120 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001121}
1122
Brian Salomonea4ad302019-08-07 13:04:55 -04001123bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001124 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001125 GrGLRenderTarget::IDs* rtIDs) {
1126 rtIDs->fMSColorRenderbufferID = 0;
1127 rtIDs->fRTFBOID = 0;
1128 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1129 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001130
bsalomona11e5fc2015-12-18 07:59:41 -08001131 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001132
Brian Salomonea4ad302019-08-07 13:04:55 -04001133 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001134 goto FAILED;
1135 }
1136
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001137 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001138 goto FAILED;
1139 }
1140
Brian Salomonea4ad302019-08-07 13:04:55 -04001141 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1142 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001143 goto FAILED;
1144 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001145
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001146 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1147 // the texture bound to the other. The exception is the IMG multisample extension. With this
1148 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1149 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001150 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001151 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1152 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1153 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001154 goto FAILED;
1155 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001156 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001157 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001158 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 }
1160
egdanield803f272015-03-18 13:01:52 -07001161 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001162 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001163 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001164 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001165 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001166 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168 goto FAILED;
1169 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001170 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001171 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001172 GR_GL_COLOR_ATTACHMENT0,
1173 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001174 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001175 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001176 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001177
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001178 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001179 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1180 GR_GL_COLOR_ATTACHMENT0,
1181 desc.fTarget,
1182 desc.fID,
1183 0,
1184 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001185 } else {
egdanield803f272015-03-18 13:01:52 -07001186 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001187 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001188 desc.fTarget,
1189 desc.fID,
1190 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001191 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192
1193 return true;
1194
1195FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001196 if (rtIDs->fMSColorRenderbufferID) {
1197 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001198 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1200 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001202 if (rtIDs->fTexFBOID) {
1203 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 }
1205 return false;
1206}
1207
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001208// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001209static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001210// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001211 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001212}
1213
Brian Salomone2826ab2019-06-04 15:58:31 -04001214static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001215 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001216 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1217 // drivers have a bug where an FBO won't be complete if it includes a
1218 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001219 GrGLTextureParameters::SamplerOverriddenState state;
1220 state.fMinFilter = GR_GL_NEAREST;
1221 state.fMagFilter = GR_GL_NEAREST;
1222 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1223 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001224 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1225 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1226 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1227 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001228 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001229}
1230
Robert Phillips67d52cf2017-06-05 13:38:13 -04001231sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001232 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001233 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001234 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001235 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001236 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001237 int mipLevelCount,
1238 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001239 // We don't support protected textures in GL.
1240 if (isProtected == GrProtected::kYes) {
1241 return nullptr;
1242 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001243 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001244
Brian Salomond2a8ae22019-09-10 16:03:59 -04001245 SkASSERT(mipLevelCount > 0);
1246 GrMipMapsStatus mipMapsStatus =
1247 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001248 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001249 GrGLTexture::Desc texDesc;
1250 texDesc.fSize = {desc.fWidth, desc.fHeight};
1251 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001252 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 texDesc.fConfig = desc.fConfig;
1254 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001255 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1256 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001257
Brian Salomond2a8ae22019-09-10 16:03:59 -04001258 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1259 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001260
1261 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001262 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001263 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001264
Robert Phillips67d52cf2017-06-05 13:38:13 -04001265 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001266 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001267 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001268 GL_CALL(BindTexture(texDesc.fTarget, 0));
1269 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001270
Brian Salomonea4ad302019-08-07 13:04:55 -04001271 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1272 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 return return_null_texture();
1274 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001275 tex = sk_make_sp<GrGLTextureRenderTarget>(
1276 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001277 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001279 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001281 // The non-sampler params are still at their default values.
1282 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1283 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001284 if (levelClearMask) {
1285 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001286 GrColorType colorType;
1287 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1288 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001289 if (this->glCaps().clearTextureSupport()) {
1290 for (int i = 0; i < mipLevelCount; ++i) {
1291 if (levelClearMask & (1U << i)) {
1292 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1293 nullptr));
1294 }
1295 }
1296 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1297 !this->glCaps().performColorClearsAsDraws()) {
1298 this->disableScissor();
1299 this->disableWindowRectangles();
1300 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001301 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001302 for (int i = 0; i < mipLevelCount; ++i) {
1303 if (levelClearMask & (1U << i)) {
1304 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1305 kDst_TempFBOTarget);
1306 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1307 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1308 }
1309 }
Brian Salomonc2249852019-09-16 11:08:50 -04001310 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001311 } else {
1312 std::unique_ptr<char[]> zeros;
1313 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1314 for (int i = 0; i < mipLevelCount; ++i) {
1315 if (levelClearMask & (1U << i)) {
1316 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1317 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1318 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1319 // smaller levels that need clearing.
1320 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001321 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001322 size_t size = levelWidth * levelHeight * bpp;
1323 zeros.reset(new char[size]());
1324 }
1325 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1326 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1327 externalFormat, externalType, zeros.get()));
1328 }
Brian Salomona3e29962019-07-16 11:52:08 -04001329 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001330 }
1331 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001332 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333}
1334
Brian Salomonbb8dde82019-06-27 10:52:13 -04001335sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001336 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -04001337 SkImage::CompressionType compression,
1338 SkBudgeted budgeted, const void* data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001339 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001340 GrGLTexture::Desc desc;
1341 desc.fSize = {width, height};
1342 desc.fTarget = GR_GL_TEXTURE_2D;
Robert Phillipsa27d6252019-12-10 14:48:36 -05001343 desc.fConfig = GrCompressionTypeToPixelConfig(compression);
Brian Salomonea4ad302019-08-07 13:04:55 -04001344 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001345 desc.fFormat = format.asGLFormat();
1346 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat, compression, &initialState,
1347 data);
Brian Salomonea4ad302019-08-07 13:04:55 -04001348 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001349 return nullptr;
1350 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001351
Robert Phillipsee946932019-12-18 11:16:17 -05001352 // Unbind this texture from the scratch texture unit.
1353 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1354
Brian Salomonea4ad302019-08-07 13:04:55 -04001355 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001356 // The non-sampler params are still at their default values.
1357 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1358 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001359 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001360}
1361
Robert Phillipsb915c942019-12-17 14:44:37 -05001362GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1363 const GrBackendFormat& format,
1364 const BackendTextureData* data,
1365 GrMipMapped mipMapped,
1366 GrProtected isProtected) {
1367 return {};
1368}
1369
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001370namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001371
egdaniel8dc7c3a2015-04-16 11:22:42 -07001372const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001373
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001374void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001375 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001376
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001377 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001378 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001379 (kUnknownBitCount == format->fTotalBits));
1380 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001381 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001382 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1383 (GrGLint*)&format->fStencilBits);
1384 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001385 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001386 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1387 (GrGLint*)&format->fTotalBits);
1388 format->fTotalBits += format->fStencilBits;
1389 } else {
1390 format->fTotalBits = format->fStencilBits;
1391 }
1392 }
1393}
1394}
1395
Brian Salomon5043f1f2019-07-11 21:27:54 -04001396int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001397 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001398 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1399
1400 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001401 // Default to unsupported, set this if we find a stencil format that works.
1402 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001403
Brian Salomond2a8ae22019-09-10 16:03:59 -04001404 GrGLuint colorID =
1405 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1406 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001407 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001408 }
egdanielff1d5472015-09-10 08:37:20 -07001409 // unbind the texture from the texture unit before binding it to the frame buffer
1410 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1411
1412 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001413 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001414 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001415 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001416 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001417 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1418 GR_GL_COLOR_ATTACHMENT0,
1419 GR_GL_TEXTURE_2D,
1420 colorID,
1421 0));
bsalomon30447372015-12-21 09:03:05 -08001422 GrGLuint sbRBID = 0;
1423 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001424
1425 // look over formats till I find a compatible one
1426 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001427 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001428 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001429 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1430 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1431 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1432 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1433 sFmt.fInternalFormat,
1434 kSize, kSize));
1435 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001436 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001437 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001438 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001439 if (sFmt.fPacked) {
1440 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1441 GR_GL_DEPTH_ATTACHMENT,
1442 GR_GL_RENDERBUFFER, sbRBID));
1443 } else {
1444 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1445 GR_GL_DEPTH_ATTACHMENT,
1446 GR_GL_RENDERBUFFER, 0));
1447 }
1448 GrGLenum status;
1449 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1450 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1451 firstWorkingStencilFormatIndex = i;
1452 break;
1453 }
egdanielff1d5472015-09-10 08:37:20 -07001454 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1455 GR_GL_STENCIL_ATTACHMENT,
1456 GR_GL_RENDERBUFFER, 0));
1457 if (sFmt.fPacked) {
1458 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1459 GR_GL_DEPTH_ATTACHMENT,
1460 GR_GL_RENDERBUFFER, 0));
1461 }
egdanielff1d5472015-09-10 08:37:20 -07001462 }
1463 }
bsalomon30447372015-12-21 09:03:05 -08001464 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001465 }
1466 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001467 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1468 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001469 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001470 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001471 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001472}
1473
Brian Salomonea4ad302019-08-07 13:04:55 -04001474GrGLuint GrGLGpu::createCompressedTexture2D(
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001475 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001476 GrGLFormat format,
Brian Salomonea4ad302019-08-07 13:04:55 -04001477 SkImage::CompressionType compression,
1478 GrGLTextureParameters::SamplerOverriddenState* initialState,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001479 const void* data) {
1480 if (format == GrGLFormat::kUnknown) {
1481 return 0;
1482 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001483 GrGLuint id = 0;
1484 GL_CALL(GenTextures(1, &id));
1485 if (!id) {
1486 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001487 }
1488
Brian Salomonea4ad302019-08-07 13:04:55 -04001489 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001490
Brian Salomonea4ad302019-08-07 13:04:55 -04001491 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1492
Robert Phillips42716d42019-12-16 12:19:54 -05001493 if (data) {
1494 if (!this->uploadCompressedTexData(format, compression, dimensions,
1495 GR_GL_TEXTURE_2D, data)) {
1496 GL_CALL(DeleteTextures(1, &id));
1497 return 0;
1498 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001499 }
Robert Phillips42716d42019-12-16 12:19:54 -05001500
Brian Salomonea4ad302019-08-07 13:04:55 -04001501 return id;
1502}
1503
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001504GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001505 GrGLFormat format,
1506 GrRenderable renderable,
1507 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001508 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001509 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001510 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001511
Brian Salomonea4ad302019-08-07 13:04:55 -04001512 GrGLuint id = 0;
1513 GL_CALL(GenTextures(1, &id));
1514
1515 if (!id) {
1516 return 0;
1517 }
1518
1519 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001520
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001521 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001522 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001523 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001524 }
1525
Brian Salomond2a8ae22019-09-10 16:03:59 -04001526 if (initialState) {
1527 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1528 } else {
1529 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001530 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001531
1532 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1533
1534 bool success = false;
1535 if (internalFormat) {
1536 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1537 if (this->glCaps().formatSupportsTexStorage(format)) {
1538 GL_ALLOC_CALL(this->glInterface(),
1539 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001540 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001541 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1542 } else {
1543 GrGLenum externalFormat, externalType;
1544 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1545 GrGLenum error = GR_GL_NO_ERROR;
1546 if (externalFormat && externalType) {
1547 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1548 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001549 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1550 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001551 GL_ALLOC_CALL(
1552 this->glInterface(),
1553 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1554 currentHeight, 0, externalFormat, externalType, nullptr));
1555 error = CHECK_ALLOC_ERROR(this->glInterface());
1556 }
1557 success = (GR_GL_NO_ERROR == error);
1558 }
1559 }
1560 }
1561 if (success) {
1562 return id;
1563 }
1564 GL_CALL(DeleteTextures(1, &id));
1565 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001566}
1567
Chris Daltoneffee202019-07-01 22:28:03 -06001568GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1569 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001570 SkASSERT(width >= rt->width());
1571 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001572
egdaniel8dc7c3a2015-04-16 11:22:42 -07001573 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001574
Brian Salomond4764a12019-08-08 12:08:24 -04001575 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001576 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001577 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001578 }
egdanielff1d5472015-09-10 08:37:20 -07001579
1580 if (!sbDesc.fRenderbufferID) {
1581 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1582 }
1583 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001584 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001585 }
1586 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1587 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1588 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1589 // we do this "if" so that we don't call the multisample
1590 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001591 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001592 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001593 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001594 sFmt.fInternalFormat,
1595 width, height));
1596 } else {
1597 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1598 sFmt.fInternalFormat,
1599 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001600 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001601 }
1602 fStats.incStencilAttachmentCreates();
1603 // After sized formats we attempt an unsized format and take
1604 // whatever sizes GL gives us. In that case we query for the size.
1605 GrGLStencilAttachment::Format format = sFmt;
1606 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001607 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1608 sbDesc,
1609 width,
1610 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001611 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001612 format);
1613 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001614}
1615
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001616////////////////////////////////////////////////////////////////////////////////
1617
Brian Salomondbf70722019-02-07 11:31:24 -05001618sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1619 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001620 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001621}
1622
Greg Danielacd66b42019-05-22 16:29:12 -04001623void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001624 GrSurfaceOrigin rtOrigin) {
1625 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001626 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001627 // if the scissor fully contains the viewport then we fall through and
1628 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001629 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001630 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001631 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001632 fHWScissorSettings.fRect = scissor;
1633 }
1634 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1635 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1636 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001637 }
1638 return;
1639 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001641
1642 // See fall through note above
1643 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001644}
1645
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001646void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001647 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001648#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001649 typedef GrWindowRectsState::Mode Mode;
1650 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1651 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001652
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001653 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001654 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001655 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001656 }
1657
csmartdalton7535f412016-08-23 06:51:00 -07001658 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1659 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001660 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1661 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001662
Chris Daltond6cda8d2019-09-05 02:30:04 -06001663 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001664 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001665 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001666 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001667 }
1668
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001669 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001670 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001671
Greg Danielacd66b42019-05-22 16:29:12 -04001672 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001673#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001674}
1675
1676void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001677#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001678 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001679 return;
1680 }
1681 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001682 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001683#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001684}
1685
Robert Phillipsfcaae482019-11-07 10:17:03 -05001686bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001687
Robert Phillipsfcaae482019-11-07 10:17:03 -05001688 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001689 if (!program) {
1690 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1691 return false;
1692 }
brianosman33f6b3f2016-06-02 05:49:21 -07001693
Brian Salomon802cb312018-06-08 18:05:20 -04001694 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001695
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001696 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001697 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1698 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001699
Robert Phillips901aff02019-10-08 12:32:56 -04001700 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001701
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001702 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001703 GrStencilSettings stencil;
1704 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001705 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1706 stencil.reset(*programInfo.pipeline().getUserStencil(),
1707 programInfo.pipeline().hasStencilClip(),
1708 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001709 }
Robert Phillips901aff02019-10-08 12:32:56 -04001710 this->flushStencil(stencil, programInfo.origin());
1711 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001712 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001713 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1714 : kBogusScissor);
1715 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001716 } else {
1717 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001718 }
Robert Phillips901aff02019-10-08 12:32:56 -04001719 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1720 glRT, programInfo.origin());
1721 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001722 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001723 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001724
1725 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001726 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001727 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001728
1729 return true;
1730}
1731
Brian Salomon802cb312018-06-08 18:05:20 -04001732void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1733 if (!program) {
1734 fHWProgram.reset();
1735 fHWProgramID = 0;
1736 return;
1737 }
1738 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1739 if (program == fHWProgram) {
1740 return;
1741 }
1742 auto id = program->programID();
1743 SkASSERT(id);
1744 GL_CALL(UseProgram(id));
1745 fHWProgram = std::move(program);
1746 fHWProgramID = id;
1747}
1748
1749void GrGLGpu::flushProgram(GrGLuint id) {
1750 SkASSERT(id);
1751 if (fHWProgramID == id) {
1752 SkASSERT(!fHWProgram);
1753 return;
1754 }
1755 fHWProgram.reset();
1756 GL_CALL(UseProgram(id));
1757 fHWProgramID = id;
1758}
1759
1760void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001761 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001762 int baseVertex,
1763 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001764 int baseInstance,
1765 GrPrimitiveRestart enablePrimitiveRestart) {
1766 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001767
cdaltone2e71c22016-04-07 18:13:29 -07001768 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001769 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001770 SkASSERT(indexBuffer->isCpuBuffer() ||
1771 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001772 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001773 } else {
1774 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001775 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001776
Brian Salomon92be2f72018-06-19 14:33:47 -04001777 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1778 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001779
Brian Salomon802cb312018-06-08 18:05:20 -04001780 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001781 SkASSERT(vertexBuffer);
1782 SkASSERT(vertexBuffer->isCpuBuffer() ||
1783 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1784 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001785 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1786 const auto& attrib = fHWProgram->vertexAttribute(i);
1787 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001788 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1789 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001790 }
Chris Dalton1d616352017-05-31 12:51:23 -06001791 }
Brian Salomon802cb312018-06-08 18:05:20 -04001792 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001793 SkASSERT(instanceBuffer);
1794 SkASSERT(instanceBuffer->isCpuBuffer() ||
1795 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1796 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001797 int attribIdx = fHWProgram->numVertexAttributes();
1798 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1799 const auto& attrib = fHWProgram->instanceAttribute(i);
1800 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001801 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1802 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1803 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001804 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001805 }
1806}
1807
Brian Salomonae64c192019-02-05 09:41:37 -05001808GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001809 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001810
cdaltone2e71c22016-04-07 18:13:29 -07001811 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001812 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001813 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001814 }
cdaltone2e71c22016-04-07 18:13:29 -07001815
Brian Salomonae64c192019-02-05 09:41:37 -05001816 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001817 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001818 if (!bufferState->fBufferZeroKnownBound) {
1819 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1820 bufferState->fBufferZeroKnownBound = true;
1821 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001822 }
Brian Salomondbf70722019-02-07 11:31:24 -05001823 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1824 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001825 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1826 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1827 bufferState->fBufferZeroKnownBound = false;
1828 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001829 }
1830
Brian Salomonae64c192019-02-05 09:41:37 -05001831 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001832}
Brian Salomond818ebf2018-07-02 14:08:49 +00001833void GrGLGpu::disableScissor() {
1834 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1835 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1836 fHWScissorSettings.fEnabled = kNo_TriState;
1837 return;
1838 }
1839}
1840
Brian Osman9a9baae2018-11-05 15:06:26 -05001841void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001842 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001843 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001844 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001845 SkASSERT(!this->caps()->performColorClearsAsDraws());
1846 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001847
Brian Salomon43f8bf02017-10-18 08:33:29 -04001848 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001849
Brian Salomon43f8bf02017-10-18 08:33:29 -04001850 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1851
Brian Salomond818ebf2018-07-02 14:08:49 +00001852 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001853 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001854 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001855 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001856 }
Greg Danielacd66b42019-05-22 16:29:12 -04001857 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001858 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001859 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001860 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001861 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001862}
1863
Robert Phillips95214472017-08-08 18:00:03 -04001864void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001865 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1866
Robert Phillips95214472017-08-08 18:00:03 -04001867 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001868 return;
1869 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001870
Chris Dalton674f77a2019-09-30 20:49:39 -06001871 // This should only be called internally when we know we have a stencil buffer.
1872 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001873
bsalomonb0bd4f62014-09-03 07:19:50 -07001874 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001875 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001876
Brian Salomond818ebf2018-07-02 14:08:49 +00001877 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001878 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001879
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001880 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001881 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001882 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001883 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001884}
1885
Chris Daltonb50cc812019-10-14 16:29:21 -06001886static bool use_tiled_rendering(const GrGLCaps& glCaps,
1887 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1888 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1889 // Otherwise it's faster to just not use it.
1890 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1891 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1892}
1893
1894void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001895 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1896 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1897 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1898
1899 this->handleDirtyContext();
1900
1901 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1902 this->flushRenderTarget(glRT);
1903 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1904
Chris Daltonb50cc812019-10-14 16:29:21 -06001905 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1906 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1907 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1908 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1909 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1910 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1911 nativeBounds.fHeight, preserveMask));
1912 }
1913
Chris Dalton674f77a2019-09-30 20:49:39 -06001914 GrGLbitfield clearMask = 0;
1915 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1916 SkASSERT(!this->caps()->performColorClearsAsDraws());
1917 this->flushClearColor(colorLoadStore.fClearColor);
1918 this->flushColorWrite(true);
1919 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001920 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001921 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1922 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1923 GL_CALL(StencilMask(0xffffffff));
1924 GL_CALL(ClearStencil(0));
1925 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1926 }
1927 if (clearMask) {
1928 this->disableScissor();
1929 this->disableWindowRectangles();
1930 GL_CALL(Clear(clearMask));
1931 }
1932}
1933
1934void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1935 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1936 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1937 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1938
1939 this->handleDirtyContext();
1940
1941 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1942 // The framebuffer binding changed in the middle of a command buffer. We should have already
1943 // printed a warning during onFBOChanged.
1944 return;
1945 }
1946
1947 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1948 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1949
1950 SkSTArray<2, GrGLenum> discardAttachments;
1951 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1952 discardAttachments.push_back(
1953 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1954 }
1955 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1956 discardAttachments.push_back(
1957 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1958 }
1959
1960 if (!discardAttachments.empty()) {
1961 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1962 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1963 discardAttachments.begin()));
1964 } else {
1965 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1966 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1967 discardAttachments.begin()));
1968 }
1969 }
1970 }
1971
Chris Daltonb50cc812019-10-14 16:29:21 -06001972 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1973 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1974 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1975 // Handled by use_tiled_rendering().
1976 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1977 GL_CALL(EndTiling(preserveMask));
1978 }
1979
Chris Dalton674f77a2019-09-30 20:49:39 -06001980 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001981}
1982
csmartdalton29df7602016-08-31 11:55:52 -07001983void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1984 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001985 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07001986 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001987 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07001988 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001989
egdaniel8dc7c3a2015-04-16 11:22:42 -07001990 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001991 // this should only be called internally when we know we have a
1992 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001993 SkASSERT(sb);
1994 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001995#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001996 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001997 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001998#else
1999 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002000 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002001 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002002 // changing the clip between stencil passes may or may not
2003 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002004 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002005#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002006 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002007 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002008 value = (1 << (stencilBitCount - 1));
2009 } else {
2010 value = 0;
2011 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002012 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002013 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002014
Greg Danielacd66b42019-05-22 16:29:12 -04002015 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002016 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002017
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002018 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002019 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002020 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002021 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002022}
2023
Brian Salomone05ba5a2019-04-08 11:59:07 -04002024bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002025 GrColorType surfaceColorType, GrColorType dstColorType,
2026 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002027 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002028
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002029 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002030 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002031 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002032 return false;
2033 }
Greg Danielba88ab62019-07-26 09:14:01 -04002034 GrGLenum externalFormat = 0;
2035 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002036 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2037 surfaceColorType,
2038 dstColorType,
2039 &externalFormat,
2040 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002041 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002042 return false;
2043 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002044
Brian Salomon71d9d842016-11-03 13:42:00 -04002045 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002046 if (renderTarget->numSamples() <= 1 ||
2047 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2048 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2049 this->flushRenderTargetNoColorWrites(renderTarget);
2050 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2051 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2052 return false;
2053 } else {
2054 SkASSERT(renderTarget->requiresManualMSAAResolve());
2055 // we don't track the state of the READ FBO ID.
2056 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002057 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002058 } else {
2059 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002060 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002061 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002062 }
2063
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002064 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002065 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002066
Brian Salomona6948702018-06-01 15:33:20 -04002067 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002068 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002069 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002070 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002071 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002072 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002073
Brian Osman1348ed02018-09-12 09:08:39 -04002074 bool reattachStencil = false;
2075 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2076 renderTarget &&
2077 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002078 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002079 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2080 reattachStencil = true;
2081 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2082 GR_GL_RENDERBUFFER, 0));
2083 }
2084
Chris Dalton76500e52019-09-05 02:13:05 -06002085 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002086 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002087
2088 if (reattachStencil) {
2089 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2090 renderTarget->renderTargetPriv().getStencilAttachment());
2091 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2092 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2093 }
2094
Brian Salomon26de56e2019-04-10 12:14:26 -04002095 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002096 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002097 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2098 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002099
Brian Salomone05ba5a2019-04-08 11:59:07 -04002100 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002101 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002102 }
2103 return true;
2104}
2105
2106bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002107 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2108 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002109 SkASSERT(surface);
2110
Brian Salomonb28cb682019-07-26 12:48:47 -04002111 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002112
Brian Salomon26de56e2019-04-10 12:14:26 -04002113 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2114 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002115
Brian Salomon1047a492019-07-02 12:25:21 -04002116 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002117 rowPixelWidth = width;
2118 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002119 SkASSERT(!(rowBytes % bytesPerPixel));
2120 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002121 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002122 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2123 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002124}
2125
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002126GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002127 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002128 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002129 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002130 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002131 if (!fCachedOpsRenderPass) {
2132 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002133 }
2134
Chris Daltonb50cc812019-10-14 16:29:21 -06002135 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002136 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002137}
2138
Brian Salomon1fabd512018-02-09 09:54:25 -05002139void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002140 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002141 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002142 this->didWriteToSurface(target, origin, &bounds);
2143}
bsalomon6ba6fa12015-03-04 11:57:37 -08002144
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002145void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2146 this->flushRenderTargetNoColorWrites(target);
2147 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002148}
2149
Brian Osman9aa30c62018-07-02 15:21:46 -04002150void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002151 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002152 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002153 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002154 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002155#ifdef SK_DEBUG
2156 // don't do this check in Chromium -- this is causing
2157 // lots of repeated command buffer flushes when the compositor is
2158 // rendering with Ganesh, which is really slow; even too slow for
2159 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002160 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002161 GrGLenum status;
2162 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2163 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2164 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2165 }
bsalomon160f24c2015-03-17 15:55:42 -07002166 }
egdanield803f272015-03-18 13:01:52 -07002167#endif
2168 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002169 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002170 }
2171
brianosman35b784d2016-05-05 11:52:53 -07002172 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002173 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002174 }
bsalomon083617b2016-02-12 12:10:14 -08002175}
bsalomona9909122016-01-23 10:41:40 -08002176
brianosman33f6b3f2016-06-02 05:49:21 -07002177void GrGLGpu::flushFramebufferSRGB(bool enable) {
2178 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2179 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2180 fHWSRGBFramebuffer = kYes_TriState;
2181 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2182 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2183 fHWSRGBFramebuffer = kNo_TriState;
2184 }
2185}
2186
Greg Danielacd66b42019-05-22 16:29:12 -04002187void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002188 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002189 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002190 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002191 fHWViewport = viewport;
2192 }
2193}
2194
bsalomon@google.comd302f142011-03-03 13:54:13 +00002195#define SWAP_PER_DRAW 0
2196
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002197#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002198 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002199 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002200 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002201 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002202 void SwapBuf() {
2203 DWORD procID = GetCurrentProcessId();
2204 HWND hwnd = GetTopWindow(GetDesktopWindow());
2205 while(hwnd) {
2206 DWORD wndProcID = 0;
2207 GetWindowThreadProcessId(hwnd, &wndProcID);
2208 if(wndProcID == procID) {
2209 SwapBuffers(GetDC(hwnd));
2210 }
2211 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2212 }
2213 }
2214 #endif
2215#endif
2216
Robert Phillips901aff02019-10-08 12:32:56 -04002217void GrGLGpu::draw(GrRenderTarget* renderTarget,
2218 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002219 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002220 int meshCount) {
2221 this->handleDirtyContext();
2222
Robert Phillips2579de42019-10-09 09:51:59 -04002223 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002224
Robert Phillipsfcaae482019-11-07 10:17:03 -05002225 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002226 return;
2227 }
ethannicholas22793252016-01-30 09:59:10 -08002228
Robert Phillips901aff02019-10-08 12:32:56 -04002229 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2230 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2231
Brian Salomonf7232642018-09-19 08:58:08 -04002232 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsfcaae482019-11-07 10:17:03 -05002233 SkASSERT(meshes[m].primitiveType() == programInfo.primitiveType());
Robert Phillips6e54a292019-11-05 15:42:16 -05002234
Robert Phillips901aff02019-10-08 12:32:56 -04002235 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2236 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002237 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002238 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002239
Robert Phillips901aff02019-10-08 12:32:56 -04002240 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002241 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002242 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2243 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002244 }
Robert Phillips901aff02019-10-08 12:32:56 -04002245 if (hasDynamicPrimProcTextures) {
2246 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2247 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2248 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002249 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002250 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002251 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002252 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2253 GL_CALL(Enable(GR_GL_CULL_FACE));
2254 GL_CALL(Disable(GR_GL_CULL_FACE));
2255 }
Brian Salomonf7232642018-09-19 08:58:08 -04002256 meshes[m].sendToGpu(this);
2257 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002258 }
ethannicholas22793252016-01-30 09:59:10 -08002259
bsalomon@google.comd302f142011-03-03 13:54:13 +00002260#if SWAP_PER_DRAW
2261 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002262 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002263 aglSwapBuffers(aglGetCurrentContext());
2264 int set_a_break_pt_here = 9;
2265 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002266 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002267 SwapBuf();
2268 int set_a_break_pt_here = 9;
2269 SwapBuf();
2270 #endif
2271#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002272}
2273
Chris Dalton3809bab2017-06-13 10:55:06 -06002274static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2275 switch (primitiveType) {
2276 case GrPrimitiveType::kTriangles:
2277 return GR_GL_TRIANGLES;
2278 case GrPrimitiveType::kTriangleStrip:
2279 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002280 case GrPrimitiveType::kPoints:
2281 return GR_GL_POINTS;
2282 case GrPrimitiveType::kLines:
2283 return GR_GL_LINES;
2284 case GrPrimitiveType::kLineStrip:
2285 return GR_GL_LINE_STRIP;
Robert Phillips571177f2019-10-04 14:41:49 -04002286 case GrPrimitiveType::kPath:
2287 SK_ABORT("non-mesh-based GrPrimitiveType");
2288 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002289 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002290 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002291}
2292
Brian Salomon802cb312018-06-08 18:05:20 -04002293void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2294 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002295 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002296 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002297 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002298 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2299 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002300 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002301 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2302 }
2303 fStats.incNumDraws();
2304}
2305
Brian Salomondbf70722019-02-07 11:31:24 -05002306static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2307 size_t baseOffset = baseIndex * sizeof(uint16_t);
2308 if (indexBuffer->isCpuBuffer()) {
2309 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2310 } else {
2311 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2312 }
2313}
2314
Brian Salomon802cb312018-06-08 18:05:20 -04002315void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002316 int indexCount, int baseIndex, uint16_t minIndexValue,
2317 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002318 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002319 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002320 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002321
Brian Salomon802cb312018-06-08 18:05:20 -04002322 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002323
2324 if (this->glCaps().drawRangeElementsSupport()) {
2325 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002326 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002327 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002328 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002329 }
2330 fStats.incNumDraws();
2331}
2332
Brian Salomon802cb312018-06-08 18:05:20 -04002333void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002334 int vertexCount, int baseVertex,
2335 const GrBuffer* instanceBuffer, int instanceCount,
2336 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002337 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002338 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002339 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002340 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2341 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002342 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2343 SkTMin(instanceCount - i, maxInstances)));
2344 fStats.incNumDraws();
2345 }
Chris Dalton1d616352017-05-31 12:51:23 -06002346}
2347
Brian Salomon802cb312018-06-08 18:05:20 -04002348void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002349 const GrBuffer* indexBuffer, int indexCount,
2350 int baseIndex, const GrBuffer* vertexBuffer,
2351 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002352 int instanceCount, int baseInstance,
2353 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002354 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002355 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002356 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2357 for (int i = 0; i < instanceCount; i += maxInstances) {
2358 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2359 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002360 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002361 SkTMin(instanceCount - i, maxInstances)));
2362 fStats.incNumDraws();
2363 }
Chris Dalton1d616352017-05-31 12:51:23 -06002364}
2365
Chris Dalton16a33c62019-09-24 22:19:17 -06002366void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2367 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002368 // Some extensions automatically resolves the texture when it is read.
2369 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2370
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002371 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002372 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2373 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2374 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2375 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002376
Chris Daltonc139d082019-09-26 14:04:24 -06002377 // make sure we go through flushRenderTarget() since we've modified
2378 // the bound DRAW FBO ID.
2379 fHWBoundRenderTargetUniqueID.makeInvalid();
2380 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2381 // Apple's extension uses the scissor as the blit bounds.
2382 GrScissorState scissorState;
2383 scissorState.set(resolveRect);
2384 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2385 this->disableWindowRectangles();
2386 GL_CALL(ResolveMultisampleFramebuffer());
2387 } else {
2388 int l, b, r, t;
2389 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2390 this->glCaps().blitFramebufferSupportFlags()) {
2391 l = 0;
2392 b = 0;
2393 r = target->width();
2394 t = target->height();
2395 } else {
2396 auto rect = GrNativeRect::MakeRelativeTo(
2397 resolveOrigin, rt->height(), resolveRect);
2398 l = rect.fX;
2399 b = rect.fY;
2400 r = rect.fX + rect.fWidth;
2401 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002402 }
Chris Daltonc139d082019-09-26 14:04:24 -06002403
2404 // BlitFrameBuffer respects the scissor, so disable it.
2405 this->disableScissor();
2406 this->disableWindowRectangles();
2407 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 +00002408 }
2409}
2410
bsalomon@google.com411dad02012-06-05 20:24:20 +00002411namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002412
bsalomon@google.com411dad02012-06-05 20:24:20 +00002413
2414GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002415 static const GrGLenum gTable[kGrStencilOpCount] = {
2416 GR_GL_KEEP, // kKeep
2417 GR_GL_ZERO, // kZero
2418 GR_GL_REPLACE, // kReplace
2419 GR_GL_INVERT, // kInvert
2420 GR_GL_INCR_WRAP, // kIncWrap
2421 GR_GL_DECR_WRAP, // kDecWrap
2422 GR_GL_INCR, // kIncClamp
2423 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002424 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002425 static_assert(0 == (int)GrStencilOp::kKeep);
2426 static_assert(1 == (int)GrStencilOp::kZero);
2427 static_assert(2 == (int)GrStencilOp::kReplace);
2428 static_assert(3 == (int)GrStencilOp::kInvert);
2429 static_assert(4 == (int)GrStencilOp::kIncWrap);
2430 static_assert(5 == (int)GrStencilOp::kDecWrap);
2431 static_assert(6 == (int)GrStencilOp::kIncClamp);
2432 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002433 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2434 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002435}
2436
2437void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002438 const GrStencilSettings::Face& face,
2439 GrGLenum glFace) {
2440 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2441 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2442 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002443
cdalton93a379b2016-05-11 13:58:08 -07002444 GrGLint ref = face.fRef;
2445 GrGLint mask = face.fTestMask;
2446 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002447
2448 if (GR_GL_FRONT_AND_BACK == glFace) {
2449 // we call the combined func just in case separate stencil is not
2450 // supported.
2451 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2452 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002453 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002454 } else {
2455 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2456 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002457 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002458 }
2459}
2460}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002461
Chris Dalton71713f62019-04-18 12:41:03 -06002462void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002463 if (stencilSettings.isDisabled()) {
2464 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002465 } else if (fHWStencilSettings != stencilSettings ||
2466 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002467 if (kYes_TriState != fHWStencilTestEnabled) {
2468 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002469
csmartdaltonc7d85332016-10-26 10:13:46 -07002470 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002471 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002472 if (!stencilSettings.isTwoSided()) {
2473 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2474 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002475 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002476 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2477 GR_GL_FRONT);
2478 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2479 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002480 }
joshualitta58fe352014-10-27 08:39:00 -07002481 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002482 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002483 }
2484}
2485
csmartdaltonc7d85332016-10-26 10:13:46 -07002486void GrGLGpu::disableStencil() {
2487 if (kNo_TriState != fHWStencilTestEnabled) {
2488 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002489
csmartdaltonc7d85332016-10-26 10:13:46 -07002490 fHWStencilTestEnabled = kNo_TriState;
2491 fHWStencilSettings.invalidate();
2492 }
2493}
2494
Chris Dalton4c56b032019-03-04 12:28:42 -07002495void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002496 // rt is only optional if useHWAA is false.
2497 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002498#ifdef SK_DEBUG
2499 if (useHWAA && rt->numSamples() <= 1) {
2500 SkASSERT(this->caps()->mixedSamplesSupport());
2501 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2502 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2503 }
2504#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002505
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002506 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002507 if (useHWAA) {
2508 if (kYes_TriState != fMSAAEnabled) {
2509 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2510 fMSAAEnabled = kYes_TriState;
2511 }
2512 } else {
2513 if (kNo_TriState != fMSAAEnabled) {
2514 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2515 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002516 }
2517 }
2518 }
2519}
2520
Chris Daltonce425af2019-12-16 10:39:03 -07002521void GrGLGpu::flushConservativeRasterState(bool enabled) {
2522 if (this->caps()->conservativeRasterSupport()) {
2523 if (enabled) {
2524 if (kYes_TriState != fHWConservativeRasterEnabled) {
2525 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2526 fHWConservativeRasterEnabled = kYes_TriState;
2527 }
2528 } else {
2529 if (kNo_TriState != fHWConservativeRasterEnabled) {
2530 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2531 fHWConservativeRasterEnabled = kNo_TriState;
2532 }
2533 }
2534 }
2535}
2536
Chris Dalton1215cda2019-12-17 21:44:04 -07002537void GrGLGpu::flushWireframeState(bool enabled) {
2538 if (this->caps()->wireframeSupport()) {
2539 if (this->caps()->wireframeMode() || enabled) {
2540 if (kYes_TriState != fHWWireframeEnabled) {
2541 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2542 fHWWireframeEnabled = kYes_TriState;
2543 }
2544 } else {
2545 if (kNo_TriState != fHWWireframeEnabled) {
2546 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2547 fHWWireframeEnabled = kNo_TriState;
2548 }
2549 }
2550 }
2551}
2552
Chris Daltonbbb3f642019-07-24 12:25:08 -04002553void GrGLGpu::flushBlendAndColorWrite(
2554 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2555 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2556 // We need to work around a driver bug by using a blend state that preserves the dst color,
2557 // rather than disabling color writes.
2558 GrXferProcessor::BlendInfo preserveDstBlend;
2559 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2560 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2561 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2562 return;
2563 }
bsalomonf7cc8772015-05-11 11:21:14 -07002564
cdalton8917d622015-05-06 13:40:21 -07002565 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002566 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2567 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002568
2569 // Any optimization to disable blending should have already been applied and
2570 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002571 bool blendOff =
2572 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2573 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2574 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002575
egdanielb414f252014-07-29 13:15:47 -07002576 if (blendOff) {
2577 if (kNo_TriState != fHWBlendState.fEnabled) {
2578 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002579
2580 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2581 // https://code.google.com/p/skia/issues/detail?id=3943
2582 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2583 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2584 SkASSERT(this->caps()->advancedBlendEquationSupport());
2585 // Set to any basic blending equation.
2586 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2587 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2588 fHWBlendState.fEquation = blend_equation;
2589 }
2590
egdanielb414f252014-07-29 13:15:47 -07002591 fHWBlendState.fEnabled = kNo_TriState;
2592 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002593 } else {
2594 if (kYes_TriState != fHWBlendState.fEnabled) {
2595 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002596
Chris Daltonbbb3f642019-07-24 12:25:08 -04002597 fHWBlendState.fEnabled = kYes_TriState;
2598 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002599
Chris Daltonbbb3f642019-07-24 12:25:08 -04002600 if (fHWBlendState.fEquation != equation) {
2601 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2602 fHWBlendState.fEquation = equation;
2603 }
cdalton8917d622015-05-06 13:40:21 -07002604
Chris Daltonbbb3f642019-07-24 12:25:08 -04002605 if (GrBlendEquationIsAdvanced(equation)) {
2606 SkASSERT(this->caps()->advancedBlendEquationSupport());
2607 // Advanced equations have no other blend state.
2608 return;
2609 }
cdalton8917d622015-05-06 13:40:21 -07002610
Chris Daltonbbb3f642019-07-24 12:25:08 -04002611 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2612 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2613 gXfermodeCoeff2Blend[dstCoeff]));
2614 fHWBlendState.fSrcCoeff = srcCoeff;
2615 fHWBlendState.fDstCoeff = dstCoeff;
2616 }
cdalton8917d622015-05-06 13:40:21 -07002617
Chris Daltonbbb3f642019-07-24 12:25:08 -04002618 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2619 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2620 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2621 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2622 fHWBlendState.fConstColor = blendConst;
2623 fHWBlendState.fConstColorValid = true;
2624 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002625 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002626 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002627
2628 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002629}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002630
Brian Salomondc829942018-10-23 16:07:24 -04002631static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002632 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002633 switch (swizzle[i]) {
2634 case 'r': glValues[i] = GR_GL_RED; break;
2635 case 'g': glValues[i] = GR_GL_GREEN; break;
2636 case 'b': glValues[i] = GR_GL_BLUE; break;
2637 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002638 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002639 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002640 default: SK_ABORT("Unsupported component");
2641 }
Brian Salomon327955b2018-10-22 15:39:22 +00002642 }
2643}
2644
Greg Daniel2c3398d2019-06-19 11:58:01 -04002645void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2646 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002647 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002648
reed856e9d92015-09-30 12:21:45 -07002649#ifdef SK_DEBUG
2650 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002651 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002652 const int w = texture->width();
2653 const int h = texture->height();
2654 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2655 }
2656 }
2657#endif
2658
Robert Phillips294870f2016-11-11 12:38:40 -05002659 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002660 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002661 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002662 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002663 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002664 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002665 }
2666
Brian Salomondc829942018-10-23 16:07:24 -04002667 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002668 if (!this->caps()->mipMapSupport() ||
2669 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002670 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002671 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002672 }
bsalomonefd7d452014-10-23 14:17:46 -07002673
brianosman33f6b3f2016-06-02 05:49:21 -07002674#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002675 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002676 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002677 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002678 }
2679#endif
2680
Brian Salomone2826ab2019-06-04 15:58:31 -04002681 auto timestamp = texture->parameters()->resetTimestamp();
2682 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002683
Brian Salomone2826ab2019-06-04 15:58:31 -04002684 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2685 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002686 if (fSamplerObjectCache) {
2687 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2688 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002689 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2690 texture->parameters()->samplerOverriddenState();
2691 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002692
Brian Salomone2826ab2019-06-04 15:58:31 -04002693 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2694 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002695
Brian Salomone2826ab2019-06-04 15:58:31 -04002696 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2697 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002698
2699 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002700 newSamplerState.fMinLOD = -1000.f;
2701 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002702
Brian Salomone2826ab2019-06-04 15:58:31 -04002703 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002704 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002705 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002706 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002707 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002708 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002709 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002710 }
Mike Klein1bccae52018-10-19 11:38:44 +00002711 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002712 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002713 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002714 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002715 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002716 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002717 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002718 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002719 }
2720 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002721 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002722 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002723 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002724 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002725 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002726 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002727 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002728 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002729 if (this->glCaps().clampToBorderSupport()) {
2730 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002731 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002732 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002733 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2734 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002735 }
2736 }
Brian Salomondc829942018-10-23 16:07:24 -04002737 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002738 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2739 newNonsamplerState.fBaseMipMapLevel = 0;
2740 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002741
Brian Salomone2826ab2019-06-04 15:58:31 -04002742 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2743 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002744 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002745 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2746 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002747 GrGLenum glValues[4];
2748 get_gl_swizzle_values(swizzle, glValues);
2749 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002750 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002751 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002752 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2753 reinterpret_cast<const GrGLint*>(glValues)));
2754 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002755 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2756 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2757 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2758 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2759 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002760 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002761 }
2762 }
Brian Salomondc829942018-10-23 16:07:24 -04002763 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002764 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2765 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2766 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002767 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002768 this->setTextureUnit(unitIdx);
2769 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002770 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002771 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002772 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002773 this->setTextureUnit(unitIdx);
2774 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002775 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002776 }
Brian Salomon327955b2018-10-22 15:39:22 +00002777 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002778 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2779 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002780}
2781
Brian Salomon1f05d452019-02-08 12:33:08 -05002782void GrGLGpu::onResetTextureBindings() {
2783 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2784 GR_GL_TEXTURE_EXTERNAL};
2785 for (int i = 0; i < this->numTextureUnits(); ++i) {
2786 this->setTextureUnit(i);
2787 for (auto target : kTargets) {
2788 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2789 GL_CALL(BindTexture(target, 0));
2790 }
2791 }
2792 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2793 }
2794}
2795
egdaniel080e6732014-12-22 07:35:52 -08002796void GrGLGpu::flushColorWrite(bool writeColor) {
2797 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002798 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002799 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2800 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002801 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002802 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002803 } else {
2804 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002805 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002806 fHWWriteToColor = kYes_TriState;
2807 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002808 }
bsalomon3e791242014-12-17 13:43:13 -08002809}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002810
Chris Dalton674f77a2019-09-30 20:49:39 -06002811void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2812 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2813 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2814 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2815 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2816 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2817 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2818 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002819 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2820 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2821 GL_CALL(ClearColor(r, g, b, a));
2822 fHWClearColor[0] = r;
2823 fHWClearColor[1] = g;
2824 fHWClearColor[2] = b;
2825 fHWClearColor[3] = a;
2826 }
2827}
2828
bsalomon861e1032014-12-16 07:33:49 -08002829void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002830 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002831 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002832 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002833 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002834 }
2835}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002836
Brian Salomond978b902019-02-07 15:09:18 -05002837void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002838 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002839 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002840 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2841 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2842 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002843 }
Brian Salomond978b902019-02-07 15:09:18 -05002844 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2845 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002846 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002847 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002848}
2849
Brian Salomone5e7eb12016-10-14 16:18:33 -04002850// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002851static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2852 const GrSurface* src,
2853 const SkIRect& srcRect,
2854 const SkIPoint& dstPoint,
2855 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002856 int dstSampleCnt = 0;
2857 int srcSampleCnt = 0;
2858 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002859 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002860 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002861 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002862 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002863 }
2864 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2865 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2866
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002867 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2868 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2869
Brian Salomone5e7eb12016-10-14 16:18:33 -04002870 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002871 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002872
Greg Daniel46cfbc62019-06-07 11:43:30 -04002873 GrTextureType dstTexType;
2874 GrTextureType* dstTexTypePtr = nullptr;
2875 GrTextureType srcTexType;
2876 GrTextureType* srcTexTypePtr = nullptr;
2877 if (dstTex) {
2878 dstTexType = dstTex->texturePriv().textureType();
2879 dstTexTypePtr = &dstTexType;
2880 }
2881 if (srcTex) {
2882 srcTexType = srcTex->texturePriv().textureType();
2883 srcTexTypePtr = &srcTexType;
2884 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002885
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002886 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2887 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002888 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002889}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002890
Brian Osmana9c8a052018-01-19 10:31:56 -05002891static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2892 // A RT has a separate MSAA renderbuffer if:
2893 // 1) It's multisampled
2894 // 2) We're using an extension with separate MSAA renderbuffers
2895 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002896 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002897}
2898
Greg Daniel46cfbc62019-06-07 11:43:30 -04002899static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002900 const GrGLCaps& caps) {
2901
bsalomon@google.comeb851172013-04-15 13:51:00 +00002902 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002903 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002904 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002905 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002906
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002907 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2908 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2909
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002910 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2911 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2912
Greg Daniel46cfbc62019-06-07 11:43:30 -04002913 GrTextureType dstTexType;
2914 GrTextureType* dstTexTypePtr = nullptr;
2915 GrTextureType srcTexType;
2916 GrTextureType* srcTexTypePtr = nullptr;
2917 if (dstTex) {
2918 dstTexType = dstTex->texturePriv().textureType();
2919 dstTexTypePtr = &dstTexType;
2920 }
2921 if (srcTex) {
2922 srcTexType = srcTex->texturePriv().textureType();
2923 srcTexTypePtr = &srcTexType;
2924 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002925
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002926 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2927 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002928}
2929
Greg Danielacd66b42019-05-22 16:29:12 -04002930// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002931void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002932 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002933 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002934 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002935 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002936 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2937 GrGLuint texID = texture->textureID();
2938 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002939 GrGLuint* tempFBOID;
2940 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002941
egdanield803f272015-03-18 13:01:52 -07002942 if (0 == *tempFBOID) {
2943 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002944 }
2945
Adrienne Walker4ee88512018-05-17 11:37:14 -07002946 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002947 GR_GL_CALL(
2948 this->glInterface(),
2949 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2950 if (mipLevel == 0) {
2951 texture->baseLevelWasBoundToFBO();
2952 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002953 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002954 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002955 }
egdaniel0f5f9672015-02-03 11:10:51 -08002956}
2957
Brian Salomond2a8ae22019-09-10 16:03:59 -04002958void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002959 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002960 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002961 SkASSERT(surface->asTexture());
2962 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2963 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2964 GR_GL_COLOR_ATTACHMENT0,
2965 textureTarget,
2966 0,
2967 0));
2968 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002969}
2970
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002971void GrGLGpu::onFBOChanged() {
2972 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2973 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2974 GL_CALL(Flush());
2975 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002976#ifdef SK_DEBUG
2977 if (fIsExecutingCommandBuffer_DebugOnly) {
2978 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2979 "This will severely hurt performance.\n");
2980 }
2981#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002982}
2983
Adrienne Walker4ee88512018-05-17 11:37:14 -07002984void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2985 fStats.incRenderTargetBinds();
2986 GL_CALL(BindFramebuffer(target, fboid));
2987 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2988 fBoundDrawFramebuffer = fboid;
2989 }
2990
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002991 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2992 // The driver forgets the correct scissor when modifying the FBO binding.
2993 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002994 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002995 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002996 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002997 }
2998
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002999 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003000}
3001
Adrienne Walker4ee88512018-05-17 11:37:14 -07003002void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3003 if (fboid == fBoundDrawFramebuffer &&
3004 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3005 // This workaround only applies to deleting currently bound framebuffers
3006 // on Adreno 420. Because this is a somewhat rare case, instead of
3007 // tracking all the attachments of every framebuffer instead just always
3008 // unbind all attachments.
3009 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3010 GR_GL_RENDERBUFFER, 0));
3011 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3012 GR_GL_RENDERBUFFER, 0));
3013 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3014 GR_GL_RENDERBUFFER, 0));
3015 }
3016
3017 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003018
3019 // Deleting the currently bound framebuffer rebinds to 0.
3020 if (fboid == fBoundDrawFramebuffer) {
3021 this->onFBOChanged();
3022 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003023}
3024
Greg Daniel46cfbc62019-06-07 11:43:30 -04003025bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003026 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003027 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3028 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3029 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003030 auto dstFormat = dst->backendFormat().asGLFormat();
3031 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003032 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003033 return true;
3034 }
3035 }
cblume61214052016-01-26 09:10:48 -08003036
Greg Daniel46cfbc62019-06-07 11:43:30 -04003037 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3038 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003039 return true;
3040 }
3041
Greg Daniel46cfbc62019-06-07 11:43:30 -04003042 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3043 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003044 }
3045
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003046 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003047 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003048 return true;
3049 }
bsalomon083617b2016-02-12 12:10:14 -08003050 }
3051
bsalomon6df86402015-06-01 10:41:49 -07003052 return false;
3053}
3054
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003055bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003056 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003057
3058 int progIdx = TextureToCopyProgramIdx(srcTex);
3059 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3060 GrSLType samplerType =
3061 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3062
3063 if (!fCopyProgramArrayBuffer) {
3064 static const GrGLfloat vdata[] = {
3065 0, 0,
3066 0, 1,
3067 1, 0,
3068 1, 1
3069 };
3070 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3071 kStatic_GrAccessPattern, vdata);
3072 }
3073 if (!fCopyProgramArrayBuffer) {
3074 return false;
3075 }
3076
3077 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3078 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3079 if (!fCopyPrograms[progIdx].fProgram) {
3080 return false;
3081 }
3082
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003083 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3084 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3085 GrShaderVar::kUniform_TypeModifier);
3086 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3087 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3088 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3089 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3090
Greg Daniel9e3169b2019-06-06 14:38:17 -04003091 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003092 if (shaderCaps->noperspectiveInterpolationSupport()) {
3093 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3094 vshaderTxt.appendf("#extension %s : require\n", extension);
3095 }
3096 vTexCoord.addModifier("noperspective");
3097 }
3098
3099 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3100 vshaderTxt.append(";");
3101 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3102 vshaderTxt.append(";");
3103 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3104 vshaderTxt.append(";");
3105 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3106 vshaderTxt.append(";");
3107
3108 vshaderTxt.append(
3109 "// Copy Program VS\n"
3110 "void main() {"
3111 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3112 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3113 " sk_Position.zw = half2(0, 1);"
3114 "}"
3115 );
3116
Greg Daniel9e3169b2019-06-06 14:38:17 -04003117 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003118 if (shaderCaps->noperspectiveInterpolationSupport()) {
3119 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3120 fshaderTxt.appendf("#extension %s : require\n", extension);
3121 }
3122 }
3123 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3124 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3125 fshaderTxt.append(";");
3126 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3127 fshaderTxt.append(";");
3128 fshaderTxt.appendf(
3129 "// Copy Program FS\n"
3130 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003131 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003132 "}"
3133 );
3134
3135 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3136 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3137 SkSL::Program::Settings settings;
3138 settings.fCaps = shaderCaps;
3139 SkSL::String glsl;
3140 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3141 sksl, settings, &glsl, errorHandler);
3142 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3143 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3144 SkASSERT(program->fInputs.isEmpty());
3145
3146 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3147 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3148 errorHandler);
3149 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3150 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3151 errorHandler);
3152 SkASSERT(program->fInputs.isEmpty());
3153
3154 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3155
3156 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3157 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3158 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3159 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3160 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3161 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3162
3163 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3164
3165 GL_CALL(DeleteShader(vshader));
3166 GL_CALL(DeleteShader(fshader));
3167
3168 return true;
3169}
3170
brianosman33f6b3f2016-06-02 05:49:21 -07003171bool GrGLGpu::createMipmapProgram(int progIdx) {
3172 const bool oddWidth = SkToBool(progIdx & 0x2);
3173 const bool oddHeight = SkToBool(progIdx & 0x1);
3174 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3175
Brian Salomon1edc5b92016-11-29 13:43:46 -05003176 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003177
3178 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3179 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3180 if (!fMipmapPrograms[progIdx].fProgram) {
3181 return false;
3182 }
3183
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003184 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3185 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003186 GrShaderVar::kUniform_TypeModifier);
3187 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3188 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003189 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003190 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003191 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3192 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3193 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3194 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003195 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003196 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003197
Ethan Nicholasfc994162019-06-06 10:04:27 -04003198 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003199 if (shaderCaps->noperspectiveInterpolationSupport()) {
3200 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003201 vshaderTxt.appendf("#extension %s : require\n", extension);
3202 }
3203 vTexCoords[0].addModifier("noperspective");
3204 vTexCoords[1].addModifier("noperspective");
3205 vTexCoords[2].addModifier("noperspective");
3206 vTexCoords[3].addModifier("noperspective");
3207 }
3208
Brian Salomon1edc5b92016-11-29 13:43:46 -05003209 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003210 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003211 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003212 vshaderTxt.append(";");
3213 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003214 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003215 vshaderTxt.append(";");
3216 }
3217
3218 vshaderTxt.append(
3219 "// Mipmap Program VS\n"
3220 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003221 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3222 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003223 );
3224
3225 // Insert texture coordinate computation:
3226 if (oddWidth && oddHeight) {
3227 vshaderTxt.append(
3228 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003229 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3230 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003231 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3232 );
3233 } else if (oddWidth) {
3234 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003235 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3236 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003237 );
3238 } else if (oddHeight) {
3239 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003240 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3241 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003242 );
3243 } else {
3244 vshaderTxt.append(
3245 " v_texCoord0 = a_vertex.xy;"
3246 );
3247 }
3248
3249 vshaderTxt.append("}");
3250
Ethan Nicholasfc994162019-06-06 10:04:27 -04003251 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003252 if (shaderCaps->noperspectiveInterpolationSupport()) {
3253 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003254 fshaderTxt.appendf("#extension %s : require\n", extension);
3255 }
3256 }
brianosman33f6b3f2016-06-02 05:49:21 -07003257 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003258 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003259 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003260 fshaderTxt.append(";");
3261 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003262 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003263 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003264 fshaderTxt.append(
3265 "// Mipmap Program FS\n"
3266 "void main() {"
3267 );
3268
3269 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003270 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003271 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3272 " sample(u_texture, v_texCoord1) + "
3273 " sample(u_texture, v_texCoord2) + "
3274 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003275 );
3276 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003277 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003278 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3279 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003280 );
3281 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003282 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003283 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003284 );
3285 }
3286
3287 fshaderTxt.append("}");
3288
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003289 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003290 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003291 SkSL::Program::Settings settings;
3292 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003293 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003294 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003295 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003296 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003297 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003298 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003299
Brian Osman6c431d52019-04-15 16:31:54 -04003300 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003301 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3302 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003303 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003304 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003305 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003306 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003307
3308 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3309
3310 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3311 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3312 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3313 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3314
3315 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3316
3317 GL_CALL(DeleteShader(vshader));
3318 GL_CALL(DeleteShader(fshader));
3319
3320 return true;
3321}
3322
Greg Daniel46cfbc62019-06-07 11:43:30 -04003323bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003324 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003325 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3326 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3327 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3328 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003329 return false;
3330 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003331 int progIdx = TextureToCopyProgramIdx(srcTex);
3332 if (!dstRT) {
3333 SkASSERT(dstTex);
3334 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3335 return false;
3336 }
3337 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003338 if (!fCopyPrograms[progIdx].fProgram) {
3339 if (!this->createCopyProgram(srcTex)) {
3340 SkDebugf("Failed to create copy program.\n");
3341 return false;
3342 }
3343 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003344 int w = srcRect.width();
3345 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003346 // We don't swizzle at all in our copies.
3347 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003348 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003349 this->flushViewport(dst->width(), dst->height());
3350 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003351 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003352 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003353 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003354 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3355 attribs->enableVertexArrays(this, 1);
3356 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3357 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003358 // dst rect edges in NDC (-1 to 1)
3359 int dw = dst->width();
3360 int dh = dst->height();
3361 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3362 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3363 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3364 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003365 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3366 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3367 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3368 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3369 int sw = src->width();
3370 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003371 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3372 // src rect edges in normalized texture space (0 to 1)
3373 sx0 /= sw;
3374 sx1 /= sw;
3375 sy0 /= sh;
3376 sy1 /= sh;
3377 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003378 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3379 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3380 sx1 - sx0, sy1 - sy0, sx0, sy0));
3381 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003382 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003383 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003384 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003385 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003386 this->disableScissor();
3387 this->disableWindowRectangles();
3388 this->disableStencil();
3389 if (this->glCaps().srgbWriteControl()) {
3390 this->flushFramebufferSRGB(true);
3391 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003392 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003393 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003394 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3395 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003396 return true;
3397}
3398
Greg Daniel46cfbc62019-06-07 11:43:30 -04003399void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003400 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003401 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003402 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003403 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003404 SkASSERT(dstTex);
3405 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003406 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003407
Brian Salomond978b902019-02-07 15:09:18 -05003408 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003409 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003410 dstPoint.fX, dstPoint.fY,
3411 srcRect.fLeft, srcRect.fTop,
3412 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003413 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003414 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3415 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003416 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3417 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003418}
3419
Greg Daniel46cfbc62019-06-07 11:43:30 -04003420bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003421 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003422 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003423 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3424 srcRect.width(), srcRect.height());
3425 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003426 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003427 return false;
mtklein404b3b22015-05-18 09:29:10 -07003428 }
bsalomon5df6fee2015-05-18 06:26:15 -07003429 }
bsalomon6df86402015-06-01 10:41:49 -07003430
Brian Salomond2a8ae22019-09-10 16:03:59 -04003431 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3432 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003433 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003434 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003435
3436 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003437 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003438 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003439
Greg Daniel46cfbc62019-06-07 11:43:30 -04003440 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3441 srcRect.fTop,
3442 srcRect.fRight,
3443 srcRect.fBottom,
3444 dstRect.fLeft,
3445 dstRect.fTop,
3446 dstRect.fRight,
3447 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003448 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003449 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3450 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003451
3452 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3453 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003454 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003455}
3456
Brian Salomon930f9392018-06-20 16:25:26 -04003457bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3458 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003459 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003460 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003461 return false;
3462 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003463 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003464 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3465 // Uses draw calls to do a series of downsample operations to successive mips.
3466
3467 // The manual approach requires the ability to limit which level we're sampling and that the
3468 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003469 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003470 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003471 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003472 GL_CALL(GenerateMipmap(glTex->target()));
3473 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003474 }
3475
brianosman33f6b3f2016-06-02 05:49:21 -07003476 int width = texture->width();
3477 int height = texture->height();
3478 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003479 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003480
3481 // Create (if necessary), then bind temporary FBO:
3482 if (0 == fTempDstFBOID) {
3483 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3484 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003485 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003486 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003487
3488 // Bind the texture, to get things configured for filtering.
3489 // We'll be changing our base level further below:
3490 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003491 // The mipmap program does not do any swizzling.
3492 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003493
3494 // Vertex data:
3495 if (!fMipmapProgramArrayBuffer) {
3496 static const GrGLfloat vdata[] = {
3497 0, 0,
3498 0, 1,
3499 1, 0,
3500 1, 1
3501 };
Brian Salomonae64c192019-02-05 09:41:37 -05003502 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003503 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003504 }
3505 if (!fMipmapProgramArrayBuffer) {
3506 return false;
3507 }
3508
3509 fHWVertexArrayState.setVertexArrayID(this, 0);
3510
3511 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003512 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003513 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003514 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003515
3516 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003517 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003518 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003519 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003520 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003521 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003522
3523 // Do all the blits:
3524 width = texture->width();
3525 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003526
brianosman33f6b3f2016-06-02 05:49:21 -07003527 for (GrGLint level = 1; level < levelCount; ++level) {
3528 // Get and bind the program for this particular downsample (filter shape can vary):
3529 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3530 if (!fMipmapPrograms[progIdx].fProgram) {
3531 if (!this->createMipmapProgram(progIdx)) {
3532 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003533 // Invalidate all params to cover base level change in a previous iteration.
3534 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003535 return false;
3536 }
3537 }
Brian Salomon802cb312018-06-08 18:05:20 -04003538 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003539
3540 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3541 const float invWidth = 1.0f / width;
3542 const float invHeight = 1.0f / height;
3543 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3544 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3545 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3546
3547 // Only sample from previous mip
3548 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3549
Brian Salomon930f9392018-06-20 16:25:26 -04003550 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3551 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003552
3553 width = SkTMax(1, width / 2);
3554 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003555 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003556
3557 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3558 }
3559
3560 // Unbind:
3561 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3562 GR_GL_TEXTURE_2D, 0, 0));
3563
Brian Salomon930f9392018-06-20 16:25:26 -04003564 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003565 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3566 // We drew the 2nd to last level into the last level.
3567 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3568 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003569
brianosman33f6b3f2016-06-02 05:49:21 -07003570 return true;
3571}
3572
Chris Daltond7291ba2019-03-07 14:17:03 -07003573void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003574 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3575 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003576
3577 int effectiveSampleCnt;
3578 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003579 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003580
3581 sampleLocations->reset(effectiveSampleCnt);
3582 for (int i = 0; i < effectiveSampleCnt; ++i) {
3583 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3584 }
3585}
3586
cdalton231c5fd2015-05-13 12:35:36 -07003587void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003588 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003589 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003590 case kTexture_GrXferBarrierType: {
3591 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003592 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003593 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3594 // The render target uses separate storage so no need for glTextureBarrier.
3595 // FIXME: The render target will resolve automatically when its texture is bound,
3596 // but we could resolve only the bounds that will be read if we do it here instead.
3597 return;
3598 }
cdalton9954bc32015-04-29 14:17:00 -07003599 SkASSERT(this->caps()->textureBarrierSupport());
3600 GL_CALL(TextureBarrier());
3601 return;
cdalton231c5fd2015-05-13 12:35:36 -07003602 }
cdalton8917d622015-05-06 13:40:21 -07003603 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003604 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003605 this->caps()->blendEquationSupport());
3606 GL_CALL(BlendBarrier());
3607 return;
bsalomoncb02b382015-08-12 11:14:50 -07003608 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003609 }
3610}
3611
Brian Salomon5043f1f2019-07-11 21:27:54 -04003612static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003613 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003614 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3615 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3616 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3617 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3618 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3619 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3620 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3621 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3622 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003623 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003624 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003625 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3626 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3627 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3628 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003629
Brian Salomon5043f1f2019-07-11 21:27:54 -04003630 // Configs with multiple equivalent formats.
3631
Robert Phillipsebab03f2019-07-22 08:48:18 -04003632 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3633 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3634
Brian Salomon5043f1f2019-07-11 21:27:54 -04003635 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3636 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3637
3638 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3639 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003640 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003641 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003642}
3643
Brian Salomon85c3d682019-11-04 15:04:54 -05003644GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003645 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003646 GrRenderable renderable,
Brian Salomon85c3d682019-11-04 15:04:54 -05003647 const BackendTextureData* data,
3648 int numMipLevels,
Robert Phillips57ef6802019-09-23 10:12:47 -04003649 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003650 // We don't support protected textures in GL.
3651 if (isProtected == GrProtected::kYes) {
3652 return {};
3653 }
3654
Brian Salomon8a375832018-03-14 10:21:40 -04003655 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003656
Brian Salomond4764a12019-08-08 12:08:24 -04003657 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003658 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003659 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003660 }
3661
Robert Phillipsd34691b2019-09-24 13:38:43 -04003662 // Compressed formats go through onCreateCompressedBackendTexture
3663 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3664
Greg Daniel15500642019-06-27 03:05:55 +00003665 GrGLTextureInfo info;
3666 GrGLTextureParameters::SamplerOverriddenState initialState;
3667
Robert Phillipsd34691b2019-09-24 13:38:43 -04003668 GrSurfaceDesc desc;
Brian Salomon85c3d682019-11-04 15:04:54 -05003669 desc.fWidth = dimensions.width();
3670 desc.fHeight = dimensions.height();
3671 desc.fConfig = gl_format_to_pixel_config(glFormat);
3672 if (desc.fConfig == kUnknown_GrPixelConfig) {
Robert Phillips42716d42019-12-16 12:19:54 -05003673 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003674 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003675
Robert Phillipsd34691b2019-09-24 13:38:43 -04003676 info.fTarget = GR_GL_TEXTURE_2D;
3677 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003678 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003679 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003680 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003681 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003682
Robert Phillips42716d42019-12-16 12:19:54 -05003683 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003684 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3685 SkTDArray<GrMipLevel> texels;
3686 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3687 // Incorporate the color type into the config to make it "specific" if applicable.
3688 desc.fConfig = this->caps()->getConfigFromBackendFormat(format, colorType);
3689 SkASSERT(desc.fConfig != kUnknown_GrPixelConfig);
3690 texels.append(numMipLevels);
3691 for (int i = 0; i < numMipLevels; ++i) {
3692 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3693 }
3694 if (!this->uploadTexData(glFormat, colorType, desc.fWidth, desc.fHeight, GR_GL_TEXTURE_2D,
3695 0, 0, desc.fWidth, desc.fHeight, colorType, texels.begin(),
3696 texels.count())) {
3697 GL_CALL(DeleteTextures(1, &info.fID));
3698 return {};
3699 }
3700 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3701 // TODO: Unify this with the clear texture code in onCreateTexture().
3702 GrColorType colorType;
3703 GrGLenum externalFormat, externalType;
3704 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3705 &externalType, &colorType);
3706 if (colorType == GrColorType::kUnknown) {
3707 GL_CALL(DeleteTextures(1, &info.fID));
3708 return {};
3709 }
3710
3711 // Make one tight image at the base size and reuse it for smaller levels.
3712 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3713 auto rb = ii.minRowBytes();
3714 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3715 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3716 GL_CALL(DeleteTextures(1, &info.fID));
3717 return {};
3718 }
3719
3720 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3721 SkISize levelDimensions = dimensions;
3722 for (int i = 0; i < numMipLevels; ++i) {
3723 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3724 levelDimensions.height(), externalFormat, externalType,
3725 pixelStorage.get()));
3726 levelDimensions = {SkTMax(1, levelDimensions.width() /2),
3727 SkTMax(1, levelDimensions.height()/2)};
3728 }
3729 }
3730 // Unbind this texture from the scratch texture unit.
3731 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003732
Brian Salomone2826ab2019-06-04 15:58:31 -04003733 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003734 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003735 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3736 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003737
Brian Salomon85c3d682019-11-04 15:04:54 -05003738 auto mipMapped = numMipLevels > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
3739 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3740 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003741}
3742
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003743void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003744 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3745
3746 GrGLTextureInfo info;
3747 if (tex.getGLTextureInfo(&info)) {
3748 GL_CALL(DeleteTextures(1, &info.fID));
3749 }
3750}
3751
3752#if GR_TEST_UTILS
3753
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003754bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003755 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003756
Greg Daniel52e16d92018-04-10 09:34:07 -04003757 GrGLTextureInfo info;
3758 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003759 return false;
3760 }
3761
3762 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003763 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003764
3765 return (GR_GL_TRUE == result);
3766}
3767
Brian Salomonf865b052018-03-09 09:01:53 -05003768GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003769 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003770 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3771 return GrBackendRenderTarget(); // invalid
3772 }
Brian Salomon8a375832018-03-14 10:21:40 -04003773 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003774 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003775 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003776 return {};
3777 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003778 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003779 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003780 if (sFormatIdx < 0) {
3781 return {};
3782 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003783 GrGLuint colorID = 0;
3784 GrGLuint stencilID = 0;
3785 auto deleteIDs = [&] {
3786 if (colorID) {
3787 if (useTexture) {
3788 GL_CALL(DeleteTextures(1, &colorID));
3789 } else {
3790 GL_CALL(DeleteRenderbuffers(1, &colorID));
3791 }
Brian Salomonf865b052018-03-09 09:01:53 -05003792 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003793 if (stencilID) {
3794 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003795 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003796 };
3797
3798 if (useTexture) {
3799 GL_CALL(GenTextures(1, &colorID));
3800 } else {
3801 GL_CALL(GenRenderbuffers(1, &colorID));
3802 }
3803 GL_CALL(GenRenderbuffers(1, &stencilID));
3804 if (!stencilID || !colorID) {
3805 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003806 return {};
3807 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003808
Brian Salomonf865b052018-03-09 09:01:53 -05003809 GrGLFramebufferInfo info;
3810 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003811 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003812 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3813 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003814 deleteIDs();
3815 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003816 }
3817
3818 this->invalidateBoundRenderTarget();
3819
Adrienne Walker4ee88512018-05-17 11:37:14 -07003820 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003821 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003822 GrGLTextureParameters::SamplerOverriddenState initialState;
3823 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3824 if (!colorID) {
3825 deleteIDs();
3826 return {};
3827 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003828 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3829 colorID, 0));
3830 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003831 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003832 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3833 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003834 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003835 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3836 GR_GL_RENDERBUFFER, colorID));
3837 }
3838 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003839 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3840 GL_ALLOC_CALL(this->glInterface(),
3841 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3842 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003843 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003844 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3845 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003846 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003847 }
3848
Brian Salomon93348dd2018-08-29 12:56:23 -04003849 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3850 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3851 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3852 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003853 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003854 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003855
Adrienne Walker4ee88512018-05-17 11:37:14 -07003856 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003857 GrGLenum status;
3858 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3859 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003860 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003861 return {};
3862 }
3863 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003864
Greg Daniel108bb232018-07-03 16:18:29 -04003865 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003866 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003867 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003868}
3869
3870void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003871 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003872 GrGLFramebufferInfo info;
3873 if (backendRT.getGLFramebufferInfo(&info)) {
3874 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003875 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003876 }
3877 }
joshualitt8fd844f2015-12-02 13:36:47 -08003878}
3879
Greg Daniel26b50a42018-03-08 09:49:58 -05003880void GrGLGpu::testingOnly_flushGpuAndSync() {
3881 GL_CALL(Finish());
3882}
Brian Salomonf865b052018-03-09 09:01:53 -05003883#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003884
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003885///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003886
cdaltone2e71c22016-04-07 18:13:29 -07003887GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003888 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003889 GrGLAttribArrayState* attribState;
3890
cdaltone2e71c22016-04-07 18:13:29 -07003891 if (gpu->glCaps().isCoreProfile()) {
3892 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003893 GrGLuint arrayID;
3894 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3895 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003896 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003897 }
cdaltone2e71c22016-04-07 18:13:29 -07003898 if (ibuf) {
3899 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003900 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003901 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003902 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003903 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003904 if (ibuf) {
3905 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003906 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003907 } else {
3908 this->setVertexArrayID(gpu, 0);
3909 }
3910 int attrCount = gpu->glCaps().maxVertexAttributes();
3911 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3912 fDefaultVertexArrayAttribState.resize(attrCount);
3913 }
3914 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003915 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003916 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003917}
bsalomone179a912016-01-20 06:18:10 -08003918
Greg Daniel30a35e82019-11-19 14:12:25 -05003919bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003920 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003921 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003922 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003923 // We call finish if the client told us to sync or if we have a finished proc but don't support
3924 // GLsync objects.
3925 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3926 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3927 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003928 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003929 // After a finish everything previously sent to GL is done.
3930 for (const auto& cb : fFinishCallbacks) {
3931 cb.fCallback(cb.fContext);
3932 this->deleteSync(cb.fSync);
3933 }
3934 fFinishCallbacks.clear();
3935 if (info.fFinishedProc) {
3936 info.fFinishedProc(info.fFinishedContext);
3937 }
3938 } else {
3939 if (info.fFinishedProc) {
3940 FinishCallback callback;
3941 callback.fCallback = info.fFinishedProc;
3942 callback.fContext = info.fFinishedContext;
3943 callback.fSync = (GrGLsync)this->insertFence();
3944 fFinishCallbacks.push_back(callback);
3945 GL_CALL(Flush());
3946 } else if (insertedSemaphore) {
3947 // Must call flush after semaphores in case they are waited on another GL context.
3948 GL_CALL(Flush());
3949 }
3950 // See if any previously inserted finish procs are good to go.
3951 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003952 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003953 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003954}
3955
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003956void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3957 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3958 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3959 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003960}
3961
Greg Daniel6be35232017-03-01 17:01:09 -05003962GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003963 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003964 GrGLsync sync;
3965 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Brian Salomon4dea72a2019-12-18 10:43:10 -05003966 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003967 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003968}
3969
Brian Salomonb0d8b762019-05-06 16:58:22 -04003970bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
3971 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07003972 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04003973 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3974 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3975}
3976
3977bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3978 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003979}
3980
3981void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003982 this->deleteSync((GrGLsync)fence);
3983}
3984
Greg Daniel301015c2019-11-18 14:06:46 -05003985std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003986 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003987 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003988}
3989
Greg Daniel301015c2019-11-18 14:06:46 -05003990std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3991 const GrBackendSemaphore& semaphore,
3992 GrResourceProvider::SemaphoreWrapType wrapType,
3993 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003994 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003995 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3996}
3997
Greg Daniel301015c2019-11-18 14:06:46 -05003998void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
3999 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004000
Greg Daniel48661b82018-01-22 16:11:35 -05004001 GrGLsync sync;
4002 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4003 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004004}
4005
Greg Daniel301015c2019-11-18 14:06:46 -05004006void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4007 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004008
4009 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4010}
4011
Brian Salomonb0d8b762019-05-06 16:58:22 -04004012void GrGLGpu::checkFinishProcs() {
4013 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4014 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4015 /* timeout = */ 0, /* flush = */ false)) {
4016 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4017 this->deleteSync(fFinishCallbacks.front().fSync);
4018 fFinishCallbacks.pop_front();
4019 }
4020}
4021
Greg Daniel6be35232017-03-01 17:01:09 -05004022void GrGLGpu::deleteSync(GrGLsync sync) const {
4023 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004024}
Brian Osman13dddce2017-05-09 13:19:50 -04004025
Greg Daniel301015c2019-11-18 14:06:46 -05004026std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004027 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004028 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004029 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004030 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004031 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4032 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004033
4034 return semaphore;
4035}
Robert Phillips646e4292017-06-13 12:44:56 -04004036
4037int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004038 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004039 case kTexture2DSampler_GrSLType:
4040 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004041 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004042 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004043 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004044 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004045 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004046 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004047 }
4048}
Brian Osman71a18892017-08-10 10:23:25 -04004049
Kevin Lubickf4def342018-10-04 12:52:50 -04004050#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004051#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004052void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4053 // We are called by the base class, which has already called beginObject(). We choose to nest
4054 // all of our caps information in a named sub-object.
4055 writer->beginObject("GL GPU");
4056
4057 const GrGLubyte* str;
4058 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4059 writer->appendString("GL_VERSION", (const char*)(str));
4060 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4061 writer->appendString("GL_RENDERER", (const char*)(str));
4062 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4063 writer->appendString("GL_VENDOR", (const char*)(str));
4064 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4065 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4066
4067 writer->appendName("extensions");
4068 glInterface()->fExtensions.dumpJSON(writer);
4069
4070 writer->endObject();
4071}
Kevin Lubickf4def342018-10-04 12:52:50 -04004072#endif