blob: ed4c1ce17ca029525ea476e0ae2cf1299f3cf8e7 [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,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001027 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001028 GrGLenum target,
1029 const void* data) {
1030 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001031 const GrGLCaps& caps = this->glCaps();
1032
1033 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001034 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001035 if (!internalFormat) {
1036 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001037 }
1038
Greg Daniel7bfc9132019-08-14 14:23:53 -04001039 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001040
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001041 int numMipLevels = 1;
1042 if (mipMapped == GrMipMapped::kYes) {
1043 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1044 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001045
1046 // Make sure that the width and height that we pass to OpenGL
1047 // is a multiple of the block size.
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001048 size_t dataSize = GrCompressedDataSize(compressionType, dimensions, nullptr, mipMapped);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001049
1050 if (useTexStorage) {
1051 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001052 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001053 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001054 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001055 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1056 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001057 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001058 }
1059 GL_CALL(CompressedTexSubImage2D(target,
1060 0, // level
1061 0, // left
1062 0, // top
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001063 dimensions.width(),
1064 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001065 internalFormat,
1066 SkToInt(dataSize),
1067 data));
Robert Phillips42716d42019-12-16 12:19:54 -05001068
1069 error = CHECK_ALLOC_ERROR(this->glInterface());
1070 if (error != GR_GL_NO_ERROR) {
1071 return false;
1072 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073 } else {
1074 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1075 0, // level
1076 internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001077 dimensions.width(),
1078 dimensions.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001079 0, // border
1080 SkToInt(dataSize),
1081 data));
1082
1083 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1084 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001085 return false;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001086 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001087 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001088 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001089}
1090
bsalomon424cc262015-05-22 10:37:30 -07001091static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001092 int sampleCount,
1093 GrGLenum format,
1094 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001095 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001096 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001097 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001098 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001099 GL_ALLOC_CALL(ctx.interface(),
1100 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1101 sampleCount,
1102 format,
1103 width, height));
1104 break;
1105 case GrGLCaps::kES_Apple_MSFBOType:
1106 GL_ALLOC_CALL(ctx.interface(),
1107 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1108 sampleCount,
1109 format,
1110 width, height));
1111 break;
1112 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1113 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1114 GL_ALLOC_CALL(ctx.interface(),
1115 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1116 sampleCount,
1117 format,
1118 width, height));
1119 break;
1120 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001121 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001122 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001123 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001124 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001125}
1126
Brian Salomonea4ad302019-08-07 13:04:55 -04001127bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001128 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001129 GrGLRenderTarget::IDs* rtIDs) {
1130 rtIDs->fMSColorRenderbufferID = 0;
1131 rtIDs->fRTFBOID = 0;
1132 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1133 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134
bsalomona11e5fc2015-12-18 07:59:41 -08001135 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001136
Brian Salomonea4ad302019-08-07 13:04:55 -04001137 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001138 goto FAILED;
1139 }
1140
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001141 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001142 goto FAILED;
1143 }
1144
Brian Salomonea4ad302019-08-07 13:04:55 -04001145 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1146 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001147 goto FAILED;
1148 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001149
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001150 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1151 // the texture bound to the other. The exception is the IMG multisample extension. With this
1152 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1153 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001154 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001155 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1156 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1157 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158 goto FAILED;
1159 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001160 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001161 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001162 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001163 }
1164
egdanield803f272015-03-18 13:01:52 -07001165 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001166 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001168 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001169 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001170 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001171 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 goto FAILED;
1173 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001174 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001175 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001176 GR_GL_COLOR_ATTACHMENT0,
1177 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001178 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001180 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001181
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001182 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001183 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1184 GR_GL_COLOR_ATTACHMENT0,
1185 desc.fTarget,
1186 desc.fID,
1187 0,
1188 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001189 } else {
egdanield803f272015-03-18 13:01:52 -07001190 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001191 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 desc.fTarget,
1193 desc.fID,
1194 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001195 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196
1197 return true;
1198
1199FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 if (rtIDs->fMSColorRenderbufferID) {
1201 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001203 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1204 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 if (rtIDs->fTexFBOID) {
1207 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 }
1209 return false;
1210}
1211
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001212// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001213static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001214// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001215 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001216}
1217
Brian Salomone2826ab2019-06-04 15:58:31 -04001218static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001219 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001220 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1221 // drivers have a bug where an FBO won't be complete if it includes a
1222 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001223 GrGLTextureParameters::SamplerOverriddenState state;
1224 state.fMinFilter = GR_GL_NEAREST;
1225 state.fMagFilter = GR_GL_NEAREST;
1226 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1227 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1229 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1230 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1231 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001232 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001233}
1234
Robert Phillips67d52cf2017-06-05 13:38:13 -04001235sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001236 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001237 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001238 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001239 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001240 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001241 int mipLevelCount,
1242 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001243 // We don't support protected textures in GL.
1244 if (isProtected == GrProtected::kYes) {
1245 return nullptr;
1246 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001247 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001248
Brian Salomond2a8ae22019-09-10 16:03:59 -04001249 SkASSERT(mipLevelCount > 0);
1250 GrMipMapsStatus mipMapsStatus =
1251 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001252 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 GrGLTexture::Desc texDesc;
1254 texDesc.fSize = {desc.fWidth, desc.fHeight};
1255 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001256 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001257 texDesc.fConfig = desc.fConfig;
1258 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001259 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1260 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001261
Brian Salomond2a8ae22019-09-10 16:03:59 -04001262 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1263 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001264
1265 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001266 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001267 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001268
Robert Phillips67d52cf2017-06-05 13:38:13 -04001269 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001270 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001271 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001272 GL_CALL(BindTexture(texDesc.fTarget, 0));
1273 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001274
Brian Salomonea4ad302019-08-07 13:04:55 -04001275 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1276 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 return return_null_texture();
1278 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001279 tex = sk_make_sp<GrGLTextureRenderTarget>(
1280 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001281 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001282 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001283 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001285 // The non-sampler params are still at their default values.
1286 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1287 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001288 if (levelClearMask) {
1289 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001290 GrColorType colorType;
1291 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1292 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001293 if (this->glCaps().clearTextureSupport()) {
1294 for (int i = 0; i < mipLevelCount; ++i) {
1295 if (levelClearMask & (1U << i)) {
1296 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1297 nullptr));
1298 }
1299 }
1300 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1301 !this->glCaps().performColorClearsAsDraws()) {
1302 this->disableScissor();
1303 this->disableWindowRectangles();
1304 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001305 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001306 for (int i = 0; i < mipLevelCount; ++i) {
1307 if (levelClearMask & (1U << i)) {
1308 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1309 kDst_TempFBOTarget);
1310 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1311 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1312 }
1313 }
Brian Salomonc2249852019-09-16 11:08:50 -04001314 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001315 } else {
1316 std::unique_ptr<char[]> zeros;
1317 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1318 for (int i = 0; i < mipLevelCount; ++i) {
1319 if (levelClearMask & (1U << i)) {
1320 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1321 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1322 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1323 // smaller levels that need clearing.
1324 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001325 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001326 size_t size = levelWidth * levelHeight * bpp;
1327 zeros.reset(new char[size]());
1328 }
1329 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1330 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1331 externalFormat, externalType, zeros.get()));
1332 }
Brian Salomona3e29962019-07-16 11:52:08 -04001333 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001334 }
1335 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001336 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001337}
1338
Brian Salomonbb8dde82019-06-27 10:52:13 -04001339sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001340 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -04001341 SkImage::CompressionType compression,
1342 SkBudgeted budgeted, const void* data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001343 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001344 GrGLTexture::Desc desc;
1345 desc.fSize = {width, height};
1346 desc.fTarget = GR_GL_TEXTURE_2D;
Robert Phillipsa27d6252019-12-10 14:48:36 -05001347 desc.fConfig = GrCompressionTypeToPixelConfig(compression);
Brian Salomonea4ad302019-08-07 13:04:55 -04001348 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001349 desc.fFormat = format.asGLFormat();
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001350 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat, compression,
1351 GrMipMapped::kNo, &initialState,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001352 data);
Brian Salomonea4ad302019-08-07 13:04:55 -04001353 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001354 return nullptr;
1355 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001356
Robert Phillipsee946932019-12-18 11:16:17 -05001357 // Unbind this texture from the scratch texture unit.
1358 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1359
Brian Salomonea4ad302019-08-07 13:04:55 -04001360 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001361 // The non-sampler params are still at their default values.
1362 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1363 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001364 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001365}
1366
Robert Phillipsb915c942019-12-17 14:44:37 -05001367GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1368 const GrBackendFormat& format,
1369 const BackendTextureData* data,
1370 GrMipMapped mipMapped,
1371 GrProtected isProtected) {
1372 return {};
1373}
1374
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001375namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001376
egdaniel8dc7c3a2015-04-16 11:22:42 -07001377const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001378
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001379void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001380 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001381
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001382 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001383 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001384 (kUnknownBitCount == format->fTotalBits));
1385 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001386 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001387 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1388 (GrGLint*)&format->fStencilBits);
1389 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001390 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001391 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1392 (GrGLint*)&format->fTotalBits);
1393 format->fTotalBits += format->fStencilBits;
1394 } else {
1395 format->fTotalBits = format->fStencilBits;
1396 }
1397 }
1398}
1399}
1400
Brian Salomon5043f1f2019-07-11 21:27:54 -04001401int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001402 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001403 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1404
1405 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001406 // Default to unsupported, set this if we find a stencil format that works.
1407 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001408
Brian Salomond2a8ae22019-09-10 16:03:59 -04001409 GrGLuint colorID =
1410 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1411 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001412 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001413 }
egdanielff1d5472015-09-10 08:37:20 -07001414 // unbind the texture from the texture unit before binding it to the frame buffer
1415 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1416
1417 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001418 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001419 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001420 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001421 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001422 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1423 GR_GL_COLOR_ATTACHMENT0,
1424 GR_GL_TEXTURE_2D,
1425 colorID,
1426 0));
bsalomon30447372015-12-21 09:03:05 -08001427 GrGLuint sbRBID = 0;
1428 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001429
1430 // look over formats till I find a compatible one
1431 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001432 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001433 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001434 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1435 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1436 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1437 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1438 sFmt.fInternalFormat,
1439 kSize, kSize));
1440 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001441 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001442 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001443 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001444 if (sFmt.fPacked) {
1445 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1446 GR_GL_DEPTH_ATTACHMENT,
1447 GR_GL_RENDERBUFFER, sbRBID));
1448 } else {
1449 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1450 GR_GL_DEPTH_ATTACHMENT,
1451 GR_GL_RENDERBUFFER, 0));
1452 }
1453 GrGLenum status;
1454 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1455 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1456 firstWorkingStencilFormatIndex = i;
1457 break;
1458 }
egdanielff1d5472015-09-10 08:37:20 -07001459 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1460 GR_GL_STENCIL_ATTACHMENT,
1461 GR_GL_RENDERBUFFER, 0));
1462 if (sFmt.fPacked) {
1463 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1464 GR_GL_DEPTH_ATTACHMENT,
1465 GR_GL_RENDERBUFFER, 0));
1466 }
egdanielff1d5472015-09-10 08:37:20 -07001467 }
1468 }
bsalomon30447372015-12-21 09:03:05 -08001469 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001470 }
1471 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001472 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1473 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001474 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001475 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001476 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001477}
1478
Brian Salomonea4ad302019-08-07 13:04:55 -04001479GrGLuint GrGLGpu::createCompressedTexture2D(
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001480 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001481 GrGLFormat format,
Brian Salomonea4ad302019-08-07 13:04:55 -04001482 SkImage::CompressionType compression,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001483 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001484 GrGLTextureParameters::SamplerOverriddenState* initialState,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001485 const void* data) {
1486 if (format == GrGLFormat::kUnknown) {
1487 return 0;
1488 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001489 GrGLuint id = 0;
1490 GL_CALL(GenTextures(1, &id));
1491 if (!id) {
1492 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001493 }
1494
Brian Salomonea4ad302019-08-07 13:04:55 -04001495 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001496
Brian Salomonea4ad302019-08-07 13:04:55 -04001497 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1498
Robert Phillips42716d42019-12-16 12:19:54 -05001499 if (data) {
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001500 if (!this->uploadCompressedTexData(format, compression, dimensions, mipMapped,
Robert Phillips42716d42019-12-16 12:19:54 -05001501 GR_GL_TEXTURE_2D, data)) {
1502 GL_CALL(DeleteTextures(1, &id));
1503 return 0;
1504 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001505 }
Robert Phillips42716d42019-12-16 12:19:54 -05001506
Brian Salomonea4ad302019-08-07 13:04:55 -04001507 return id;
1508}
1509
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001510GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001511 GrGLFormat format,
1512 GrRenderable renderable,
1513 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001514 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001515 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001516 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001517
Brian Salomonea4ad302019-08-07 13:04:55 -04001518 GrGLuint id = 0;
1519 GL_CALL(GenTextures(1, &id));
1520
1521 if (!id) {
1522 return 0;
1523 }
1524
1525 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001526
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001527 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001528 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001529 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001530 }
1531
Brian Salomond2a8ae22019-09-10 16:03:59 -04001532 if (initialState) {
1533 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1534 } else {
1535 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001536 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001537
1538 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1539
1540 bool success = false;
1541 if (internalFormat) {
1542 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1543 if (this->glCaps().formatSupportsTexStorage(format)) {
1544 GL_ALLOC_CALL(this->glInterface(),
1545 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001546 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001547 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1548 } else {
1549 GrGLenum externalFormat, externalType;
1550 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1551 GrGLenum error = GR_GL_NO_ERROR;
1552 if (externalFormat && externalType) {
1553 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1554 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001555 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1556 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001557 GL_ALLOC_CALL(
1558 this->glInterface(),
1559 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1560 currentHeight, 0, externalFormat, externalType, nullptr));
1561 error = CHECK_ALLOC_ERROR(this->glInterface());
1562 }
1563 success = (GR_GL_NO_ERROR == error);
1564 }
1565 }
1566 }
1567 if (success) {
1568 return id;
1569 }
1570 GL_CALL(DeleteTextures(1, &id));
1571 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001572}
1573
Chris Daltoneffee202019-07-01 22:28:03 -06001574GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1575 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001576 SkASSERT(width >= rt->width());
1577 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001578
egdaniel8dc7c3a2015-04-16 11:22:42 -07001579 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001580
Brian Salomond4764a12019-08-08 12:08:24 -04001581 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001582 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001583 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001584 }
egdanielff1d5472015-09-10 08:37:20 -07001585
1586 if (!sbDesc.fRenderbufferID) {
1587 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1588 }
1589 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001590 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001591 }
1592 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1593 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1594 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1595 // we do this "if" so that we don't call the multisample
1596 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001597 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001598 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001599 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001600 sFmt.fInternalFormat,
1601 width, height));
1602 } else {
1603 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1604 sFmt.fInternalFormat,
1605 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001606 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001607 }
1608 fStats.incStencilAttachmentCreates();
1609 // After sized formats we attempt an unsized format and take
1610 // whatever sizes GL gives us. In that case we query for the size.
1611 GrGLStencilAttachment::Format format = sFmt;
1612 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001613 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1614 sbDesc,
1615 width,
1616 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001617 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001618 format);
1619 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001620}
1621
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001622////////////////////////////////////////////////////////////////////////////////
1623
Brian Salomondbf70722019-02-07 11:31:24 -05001624sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1625 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001626 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001627}
1628
Greg Danielacd66b42019-05-22 16:29:12 -04001629void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001630 GrSurfaceOrigin rtOrigin) {
1631 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001632 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001633 // if the scissor fully contains the viewport then we fall through and
1634 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001635 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001636 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001637 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001638 fHWScissorSettings.fRect = scissor;
1639 }
1640 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1641 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1642 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001643 }
1644 return;
1645 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001646 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001647
1648 // See fall through note above
1649 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001650}
1651
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001652void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001653 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001654#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001655 typedef GrWindowRectsState::Mode Mode;
1656 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1657 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001658
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001659 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001660 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001661 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001662 }
1663
csmartdalton7535f412016-08-23 06:51:00 -07001664 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1665 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001666 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1667 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001668
Chris Daltond6cda8d2019-09-05 02:30:04 -06001669 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001670 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001671 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001672 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001673 }
1674
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001675 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001676 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001677
Greg Danielacd66b42019-05-22 16:29:12 -04001678 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001679#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001680}
1681
1682void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001683#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001684 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001685 return;
1686 }
1687 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001688 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001689#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001690}
1691
Robert Phillipsfcaae482019-11-07 10:17:03 -05001692bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001693
Robert Phillipsfcaae482019-11-07 10:17:03 -05001694 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001695 if (!program) {
1696 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1697 return false;
1698 }
brianosman33f6b3f2016-06-02 05:49:21 -07001699
Brian Salomon802cb312018-06-08 18:05:20 -04001700 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001701
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001702 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001703 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1704 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001705
Robert Phillips901aff02019-10-08 12:32:56 -04001706 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001707
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001708 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001709 GrStencilSettings stencil;
1710 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001711 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1712 stencil.reset(*programInfo.pipeline().getUserStencil(),
1713 programInfo.pipeline().hasStencilClip(),
1714 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001715 }
Robert Phillips901aff02019-10-08 12:32:56 -04001716 this->flushStencil(stencil, programInfo.origin());
1717 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001718 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001719 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1720 : kBogusScissor);
1721 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001722 } else {
1723 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001724 }
Robert Phillips901aff02019-10-08 12:32:56 -04001725 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1726 glRT, programInfo.origin());
1727 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001728 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001729 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001730
1731 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001732 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001733 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001734
1735 return true;
1736}
1737
Brian Salomon802cb312018-06-08 18:05:20 -04001738void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1739 if (!program) {
1740 fHWProgram.reset();
1741 fHWProgramID = 0;
1742 return;
1743 }
1744 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1745 if (program == fHWProgram) {
1746 return;
1747 }
1748 auto id = program->programID();
1749 SkASSERT(id);
1750 GL_CALL(UseProgram(id));
1751 fHWProgram = std::move(program);
1752 fHWProgramID = id;
1753}
1754
1755void GrGLGpu::flushProgram(GrGLuint id) {
1756 SkASSERT(id);
1757 if (fHWProgramID == id) {
1758 SkASSERT(!fHWProgram);
1759 return;
1760 }
1761 fHWProgram.reset();
1762 GL_CALL(UseProgram(id));
1763 fHWProgramID = id;
1764}
1765
1766void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001767 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001768 int baseVertex,
1769 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001770 int baseInstance,
1771 GrPrimitiveRestart enablePrimitiveRestart) {
1772 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001773
cdaltone2e71c22016-04-07 18:13:29 -07001774 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001775 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001776 SkASSERT(indexBuffer->isCpuBuffer() ||
1777 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001778 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001779 } else {
1780 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001781 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001782
Brian Salomon92be2f72018-06-19 14:33:47 -04001783 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1784 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001785
Brian Salomon802cb312018-06-08 18:05:20 -04001786 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001787 SkASSERT(vertexBuffer);
1788 SkASSERT(vertexBuffer->isCpuBuffer() ||
1789 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1790 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001791 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1792 const auto& attrib = fHWProgram->vertexAttribute(i);
1793 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001794 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1795 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001796 }
Chris Dalton1d616352017-05-31 12:51:23 -06001797 }
Brian Salomon802cb312018-06-08 18:05:20 -04001798 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001799 SkASSERT(instanceBuffer);
1800 SkASSERT(instanceBuffer->isCpuBuffer() ||
1801 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1802 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001803 int attribIdx = fHWProgram->numVertexAttributes();
1804 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1805 const auto& attrib = fHWProgram->instanceAttribute(i);
1806 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001807 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1808 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1809 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001810 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001811 }
1812}
1813
Brian Salomonae64c192019-02-05 09:41:37 -05001814GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001815 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001816
cdaltone2e71c22016-04-07 18:13:29 -07001817 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001818 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001819 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001820 }
cdaltone2e71c22016-04-07 18:13:29 -07001821
Brian Salomonae64c192019-02-05 09:41:37 -05001822 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001823 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001824 if (!bufferState->fBufferZeroKnownBound) {
1825 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1826 bufferState->fBufferZeroKnownBound = true;
1827 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001828 }
Brian Salomondbf70722019-02-07 11:31:24 -05001829 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1830 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001831 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1832 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1833 bufferState->fBufferZeroKnownBound = false;
1834 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001835 }
1836
Brian Salomonae64c192019-02-05 09:41:37 -05001837 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001838}
Brian Salomond818ebf2018-07-02 14:08:49 +00001839void GrGLGpu::disableScissor() {
1840 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1841 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1842 fHWScissorSettings.fEnabled = kNo_TriState;
1843 return;
1844 }
1845}
1846
Brian Osman9a9baae2018-11-05 15:06:26 -05001847void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001848 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001849 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001850 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001851 SkASSERT(!this->caps()->performColorClearsAsDraws());
1852 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001853
Brian Salomon43f8bf02017-10-18 08:33:29 -04001854 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001855
Brian Salomon43f8bf02017-10-18 08:33:29 -04001856 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1857
Brian Salomond818ebf2018-07-02 14:08:49 +00001858 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001859 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001860 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001861 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001862 }
Greg Danielacd66b42019-05-22 16:29:12 -04001863 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001864 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001865 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001866 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001867 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001868}
1869
Robert Phillips95214472017-08-08 18:00:03 -04001870void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001871 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1872
Robert Phillips95214472017-08-08 18:00:03 -04001873 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001874 return;
1875 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001876
Chris Dalton674f77a2019-09-30 20:49:39 -06001877 // This should only be called internally when we know we have a stencil buffer.
1878 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04001879
bsalomonb0bd4f62014-09-03 07:19:50 -07001880 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001881 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001882
Brian Salomond818ebf2018-07-02 14:08:49 +00001883 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001884 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001885
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001886 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001887 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001888 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001889 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06001890}
1891
Chris Daltonb50cc812019-10-14 16:29:21 -06001892static bool use_tiled_rendering(const GrGLCaps& glCaps,
1893 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1894 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1895 // Otherwise it's faster to just not use it.
1896 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1897 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1898}
1899
1900void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001901 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1902 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1903 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1904
1905 this->handleDirtyContext();
1906
1907 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1908 this->flushRenderTarget(glRT);
1909 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1910
Chris Daltonb50cc812019-10-14 16:29:21 -06001911 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1912 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1913 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1914 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1915 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1916 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1917 nativeBounds.fHeight, preserveMask));
1918 }
1919
Chris Dalton674f77a2019-09-30 20:49:39 -06001920 GrGLbitfield clearMask = 0;
1921 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1922 SkASSERT(!this->caps()->performColorClearsAsDraws());
1923 this->flushClearColor(colorLoadStore.fClearColor);
1924 this->flushColorWrite(true);
1925 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001926 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001927 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1928 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1929 GL_CALL(StencilMask(0xffffffff));
1930 GL_CALL(ClearStencil(0));
1931 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1932 }
1933 if (clearMask) {
1934 this->disableScissor();
1935 this->disableWindowRectangles();
1936 GL_CALL(Clear(clearMask));
1937 }
1938}
1939
1940void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1941 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1942 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1943 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1944
1945 this->handleDirtyContext();
1946
1947 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1948 // The framebuffer binding changed in the middle of a command buffer. We should have already
1949 // printed a warning during onFBOChanged.
1950 return;
1951 }
1952
1953 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1954 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1955
1956 SkSTArray<2, GrGLenum> discardAttachments;
1957 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1958 discardAttachments.push_back(
1959 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1960 }
1961 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1962 discardAttachments.push_back(
1963 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1964 }
1965
1966 if (!discardAttachments.empty()) {
1967 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1968 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1969 discardAttachments.begin()));
1970 } else {
1971 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1972 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1973 discardAttachments.begin()));
1974 }
1975 }
1976 }
1977
Chris Daltonb50cc812019-10-14 16:29:21 -06001978 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1979 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
1980 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1981 // Handled by use_tiled_rendering().
1982 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
1983 GL_CALL(EndTiling(preserveMask));
1984 }
1985
Chris Dalton674f77a2019-09-30 20:49:39 -06001986 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001987}
1988
csmartdalton29df7602016-08-31 11:55:52 -07001989void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1990 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001991 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07001992 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001993 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07001994 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001995
egdaniel8dc7c3a2015-04-16 11:22:42 -07001996 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001997 // this should only be called internally when we know we have a
1998 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001999 SkASSERT(sb);
2000 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002001#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002002 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002003 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002004#else
2005 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002006 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002007 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002008 // changing the clip between stencil passes may or may not
2009 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002010 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002011#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002012 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002013 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002014 value = (1 << (stencilBitCount - 1));
2015 } else {
2016 value = 0;
2017 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002018 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002019 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002020
Greg Danielacd66b42019-05-22 16:29:12 -04002021 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002022 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002023
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002024 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002025 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002026 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002027 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002028}
2029
Brian Salomone05ba5a2019-04-08 11:59:07 -04002030bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002031 GrColorType surfaceColorType, GrColorType dstColorType,
2032 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002033 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002034
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002035 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002036 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002037 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002038 return false;
2039 }
Greg Danielba88ab62019-07-26 09:14:01 -04002040 GrGLenum externalFormat = 0;
2041 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002042 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2043 surfaceColorType,
2044 dstColorType,
2045 &externalFormat,
2046 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002047 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002048 return false;
2049 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002050
Brian Salomon71d9d842016-11-03 13:42:00 -04002051 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002052 if (renderTarget->numSamples() <= 1 ||
2053 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2054 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2055 this->flushRenderTargetNoColorWrites(renderTarget);
2056 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2057 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2058 return false;
2059 } else {
2060 SkASSERT(renderTarget->requiresManualMSAAResolve());
2061 // we don't track the state of the READ FBO ID.
2062 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002063 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002064 } else {
2065 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002066 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002067 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002068 }
2069
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002070 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002071 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002072
Brian Salomona6948702018-06-01 15:33:20 -04002073 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002074 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002075 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002076 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002077 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002078 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002079
Brian Osman1348ed02018-09-12 09:08:39 -04002080 bool reattachStencil = false;
2081 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2082 renderTarget &&
2083 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002084 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002085 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2086 reattachStencil = true;
2087 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2088 GR_GL_RENDERBUFFER, 0));
2089 }
2090
Chris Dalton76500e52019-09-05 02:13:05 -06002091 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002092 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002093
2094 if (reattachStencil) {
2095 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2096 renderTarget->renderTargetPriv().getStencilAttachment());
2097 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2098 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2099 }
2100
Brian Salomon26de56e2019-04-10 12:14:26 -04002101 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002102 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002103 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2104 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002105
Brian Salomone05ba5a2019-04-08 11:59:07 -04002106 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002107 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002108 }
2109 return true;
2110}
2111
2112bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002113 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2114 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002115 SkASSERT(surface);
2116
Brian Salomonb28cb682019-07-26 12:48:47 -04002117 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002118
Brian Salomon26de56e2019-04-10 12:14:26 -04002119 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2120 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002121
Brian Salomon1047a492019-07-02 12:25:21 -04002122 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002123 rowPixelWidth = width;
2124 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002125 SkASSERT(!(rowBytes % bytesPerPixel));
2126 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002127 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002128 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2129 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002130}
2131
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002132GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002133 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002134 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002135 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002136 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002137 if (!fCachedOpsRenderPass) {
2138 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002139 }
2140
Chris Daltonb50cc812019-10-14 16:29:21 -06002141 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002142 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002143}
2144
Brian Salomon1fabd512018-02-09 09:54:25 -05002145void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002146 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002147 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002148 this->didWriteToSurface(target, origin, &bounds);
2149}
bsalomon6ba6fa12015-03-04 11:57:37 -08002150
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002151void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2152 this->flushRenderTargetNoColorWrites(target);
2153 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002154}
2155
Brian Osman9aa30c62018-07-02 15:21:46 -04002156void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002157 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002158 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002159 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002160 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002161#ifdef SK_DEBUG
2162 // don't do this check in Chromium -- this is causing
2163 // lots of repeated command buffer flushes when the compositor is
2164 // rendering with Ganesh, which is really slow; even too slow for
2165 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002166 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002167 GrGLenum status;
2168 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2169 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2170 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2171 }
bsalomon160f24c2015-03-17 15:55:42 -07002172 }
egdanield803f272015-03-18 13:01:52 -07002173#endif
2174 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002175 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002176 }
2177
brianosman35b784d2016-05-05 11:52:53 -07002178 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002179 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002180 }
bsalomon083617b2016-02-12 12:10:14 -08002181}
bsalomona9909122016-01-23 10:41:40 -08002182
brianosman33f6b3f2016-06-02 05:49:21 -07002183void GrGLGpu::flushFramebufferSRGB(bool enable) {
2184 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2185 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2186 fHWSRGBFramebuffer = kYes_TriState;
2187 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2188 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2189 fHWSRGBFramebuffer = kNo_TriState;
2190 }
2191}
2192
Greg Danielacd66b42019-05-22 16:29:12 -04002193void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002194 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002195 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002196 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002197 fHWViewport = viewport;
2198 }
2199}
2200
bsalomon@google.comd302f142011-03-03 13:54:13 +00002201#define SWAP_PER_DRAW 0
2202
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002203#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002204 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002205 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002206 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002207 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002208 void SwapBuf() {
2209 DWORD procID = GetCurrentProcessId();
2210 HWND hwnd = GetTopWindow(GetDesktopWindow());
2211 while(hwnd) {
2212 DWORD wndProcID = 0;
2213 GetWindowThreadProcessId(hwnd, &wndProcID);
2214 if(wndProcID == procID) {
2215 SwapBuffers(GetDC(hwnd));
2216 }
2217 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2218 }
2219 }
2220 #endif
2221#endif
2222
Robert Phillips901aff02019-10-08 12:32:56 -04002223void GrGLGpu::draw(GrRenderTarget* renderTarget,
2224 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002225 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002226 int meshCount) {
2227 this->handleDirtyContext();
2228
Robert Phillips2579de42019-10-09 09:51:59 -04002229 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002230
Robert Phillipsfcaae482019-11-07 10:17:03 -05002231 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002232 return;
2233 }
ethannicholas22793252016-01-30 09:59:10 -08002234
Robert Phillips901aff02019-10-08 12:32:56 -04002235 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2236 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2237
Brian Salomonf7232642018-09-19 08:58:08 -04002238 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsfcaae482019-11-07 10:17:03 -05002239 SkASSERT(meshes[m].primitiveType() == programInfo.primitiveType());
Robert Phillips6e54a292019-11-05 15:42:16 -05002240
Robert Phillips901aff02019-10-08 12:32:56 -04002241 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2242 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002243 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002244 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002245
Robert Phillips901aff02019-10-08 12:32:56 -04002246 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002247 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002248 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2249 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002250 }
Robert Phillips901aff02019-10-08 12:32:56 -04002251 if (hasDynamicPrimProcTextures) {
2252 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2253 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2254 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002255 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002256 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002257 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002258 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2259 GL_CALL(Enable(GR_GL_CULL_FACE));
2260 GL_CALL(Disable(GR_GL_CULL_FACE));
2261 }
Brian Salomonf7232642018-09-19 08:58:08 -04002262 meshes[m].sendToGpu(this);
2263 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002264 }
ethannicholas22793252016-01-30 09:59:10 -08002265
bsalomon@google.comd302f142011-03-03 13:54:13 +00002266#if SWAP_PER_DRAW
2267 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002268 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002269 aglSwapBuffers(aglGetCurrentContext());
2270 int set_a_break_pt_here = 9;
2271 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002272 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002273 SwapBuf();
2274 int set_a_break_pt_here = 9;
2275 SwapBuf();
2276 #endif
2277#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002278}
2279
Chris Dalton3809bab2017-06-13 10:55:06 -06002280static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2281 switch (primitiveType) {
2282 case GrPrimitiveType::kTriangles:
2283 return GR_GL_TRIANGLES;
2284 case GrPrimitiveType::kTriangleStrip:
2285 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002286 case GrPrimitiveType::kPoints:
2287 return GR_GL_POINTS;
2288 case GrPrimitiveType::kLines:
2289 return GR_GL_LINES;
2290 case GrPrimitiveType::kLineStrip:
2291 return GR_GL_LINE_STRIP;
Robert Phillips571177f2019-10-04 14:41:49 -04002292 case GrPrimitiveType::kPath:
2293 SK_ABORT("non-mesh-based GrPrimitiveType");
2294 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002295 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002296 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002297}
2298
Brian Salomon802cb312018-06-08 18:05:20 -04002299void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2300 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002301 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002302 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002303 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002304 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2305 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002306 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002307 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2308 }
2309 fStats.incNumDraws();
2310}
2311
Brian Salomondbf70722019-02-07 11:31:24 -05002312static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2313 size_t baseOffset = baseIndex * sizeof(uint16_t);
2314 if (indexBuffer->isCpuBuffer()) {
2315 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2316 } else {
2317 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2318 }
2319}
2320
Brian Salomon802cb312018-06-08 18:05:20 -04002321void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002322 int indexCount, int baseIndex, uint16_t minIndexValue,
2323 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002324 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002325 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002326 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002327
Brian Salomon802cb312018-06-08 18:05:20 -04002328 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002329
2330 if (this->glCaps().drawRangeElementsSupport()) {
2331 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002332 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002333 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002334 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002335 }
2336 fStats.incNumDraws();
2337}
2338
Brian Salomon802cb312018-06-08 18:05:20 -04002339void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002340 int vertexCount, int baseVertex,
2341 const GrBuffer* instanceBuffer, int instanceCount,
2342 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002343 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002344 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002345 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002346 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2347 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002348 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2349 SkTMin(instanceCount - i, maxInstances)));
2350 fStats.incNumDraws();
2351 }
Chris Dalton1d616352017-05-31 12:51:23 -06002352}
2353
Brian Salomon802cb312018-06-08 18:05:20 -04002354void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002355 const GrBuffer* indexBuffer, int indexCount,
2356 int baseIndex, const GrBuffer* vertexBuffer,
2357 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002358 int instanceCount, int baseInstance,
2359 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002360 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002361 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002362 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2363 for (int i = 0; i < instanceCount; i += maxInstances) {
2364 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2365 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002366 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002367 SkTMin(instanceCount - i, maxInstances)));
2368 fStats.incNumDraws();
2369 }
Chris Dalton1d616352017-05-31 12:51:23 -06002370}
2371
Chris Dalton16a33c62019-09-24 22:19:17 -06002372void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2373 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002374 // Some extensions automatically resolves the texture when it is read.
2375 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2376
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002377 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002378 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2379 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2380 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2381 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002382
Chris Daltonc139d082019-09-26 14:04:24 -06002383 // make sure we go through flushRenderTarget() since we've modified
2384 // the bound DRAW FBO ID.
2385 fHWBoundRenderTargetUniqueID.makeInvalid();
2386 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2387 // Apple's extension uses the scissor as the blit bounds.
2388 GrScissorState scissorState;
2389 scissorState.set(resolveRect);
2390 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2391 this->disableWindowRectangles();
2392 GL_CALL(ResolveMultisampleFramebuffer());
2393 } else {
2394 int l, b, r, t;
2395 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2396 this->glCaps().blitFramebufferSupportFlags()) {
2397 l = 0;
2398 b = 0;
2399 r = target->width();
2400 t = target->height();
2401 } else {
2402 auto rect = GrNativeRect::MakeRelativeTo(
2403 resolveOrigin, rt->height(), resolveRect);
2404 l = rect.fX;
2405 b = rect.fY;
2406 r = rect.fX + rect.fWidth;
2407 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 }
Chris Daltonc139d082019-09-26 14:04:24 -06002409
2410 // BlitFrameBuffer respects the scissor, so disable it.
2411 this->disableScissor();
2412 this->disableWindowRectangles();
2413 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 +00002414 }
2415}
2416
bsalomon@google.com411dad02012-06-05 20:24:20 +00002417namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002418
bsalomon@google.com411dad02012-06-05 20:24:20 +00002419
2420GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002421 static const GrGLenum gTable[kGrStencilOpCount] = {
2422 GR_GL_KEEP, // kKeep
2423 GR_GL_ZERO, // kZero
2424 GR_GL_REPLACE, // kReplace
2425 GR_GL_INVERT, // kInvert
2426 GR_GL_INCR_WRAP, // kIncWrap
2427 GR_GL_DECR_WRAP, // kDecWrap
2428 GR_GL_INCR, // kIncClamp
2429 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002430 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002431 static_assert(0 == (int)GrStencilOp::kKeep);
2432 static_assert(1 == (int)GrStencilOp::kZero);
2433 static_assert(2 == (int)GrStencilOp::kReplace);
2434 static_assert(3 == (int)GrStencilOp::kInvert);
2435 static_assert(4 == (int)GrStencilOp::kIncWrap);
2436 static_assert(5 == (int)GrStencilOp::kDecWrap);
2437 static_assert(6 == (int)GrStencilOp::kIncClamp);
2438 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002439 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2440 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002441}
2442
2443void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002444 const GrStencilSettings::Face& face,
2445 GrGLenum glFace) {
2446 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2447 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2448 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002449
cdalton93a379b2016-05-11 13:58:08 -07002450 GrGLint ref = face.fRef;
2451 GrGLint mask = face.fTestMask;
2452 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002453
2454 if (GR_GL_FRONT_AND_BACK == glFace) {
2455 // we call the combined func just in case separate stencil is not
2456 // supported.
2457 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2458 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002459 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002460 } else {
2461 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2462 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002463 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002464 }
2465}
2466}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002467
Chris Dalton71713f62019-04-18 12:41:03 -06002468void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002469 if (stencilSettings.isDisabled()) {
2470 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002471 } else if (fHWStencilSettings != stencilSettings ||
2472 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002473 if (kYes_TriState != fHWStencilTestEnabled) {
2474 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002475
csmartdaltonc7d85332016-10-26 10:13:46 -07002476 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002477 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002478 if (!stencilSettings.isTwoSided()) {
2479 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2480 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002481 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002482 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2483 GR_GL_FRONT);
2484 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2485 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002486 }
joshualitta58fe352014-10-27 08:39:00 -07002487 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002488 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002489 }
2490}
2491
csmartdaltonc7d85332016-10-26 10:13:46 -07002492void GrGLGpu::disableStencil() {
2493 if (kNo_TriState != fHWStencilTestEnabled) {
2494 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002495
csmartdaltonc7d85332016-10-26 10:13:46 -07002496 fHWStencilTestEnabled = kNo_TriState;
2497 fHWStencilSettings.invalidate();
2498 }
2499}
2500
Chris Dalton4c56b032019-03-04 12:28:42 -07002501void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002502 // rt is only optional if useHWAA is false.
2503 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002504#ifdef SK_DEBUG
2505 if (useHWAA && rt->numSamples() <= 1) {
2506 SkASSERT(this->caps()->mixedSamplesSupport());
2507 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2508 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2509 }
2510#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002511
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002512 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002513 if (useHWAA) {
2514 if (kYes_TriState != fMSAAEnabled) {
2515 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2516 fMSAAEnabled = kYes_TriState;
2517 }
2518 } else {
2519 if (kNo_TriState != fMSAAEnabled) {
2520 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2521 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002522 }
2523 }
2524 }
2525}
2526
Chris Daltonce425af2019-12-16 10:39:03 -07002527void GrGLGpu::flushConservativeRasterState(bool enabled) {
2528 if (this->caps()->conservativeRasterSupport()) {
2529 if (enabled) {
2530 if (kYes_TriState != fHWConservativeRasterEnabled) {
2531 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2532 fHWConservativeRasterEnabled = kYes_TriState;
2533 }
2534 } else {
2535 if (kNo_TriState != fHWConservativeRasterEnabled) {
2536 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2537 fHWConservativeRasterEnabled = kNo_TriState;
2538 }
2539 }
2540 }
2541}
2542
Chris Dalton1215cda2019-12-17 21:44:04 -07002543void GrGLGpu::flushWireframeState(bool enabled) {
2544 if (this->caps()->wireframeSupport()) {
2545 if (this->caps()->wireframeMode() || enabled) {
2546 if (kYes_TriState != fHWWireframeEnabled) {
2547 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2548 fHWWireframeEnabled = kYes_TriState;
2549 }
2550 } else {
2551 if (kNo_TriState != fHWWireframeEnabled) {
2552 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2553 fHWWireframeEnabled = kNo_TriState;
2554 }
2555 }
2556 }
2557}
2558
Chris Daltonbbb3f642019-07-24 12:25:08 -04002559void GrGLGpu::flushBlendAndColorWrite(
2560 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2561 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2562 // We need to work around a driver bug by using a blend state that preserves the dst color,
2563 // rather than disabling color writes.
2564 GrXferProcessor::BlendInfo preserveDstBlend;
2565 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2566 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2567 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2568 return;
2569 }
bsalomonf7cc8772015-05-11 11:21:14 -07002570
cdalton8917d622015-05-06 13:40:21 -07002571 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002572 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2573 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002574
2575 // Any optimization to disable blending should have already been applied and
2576 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002577 bool blendOff =
2578 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2579 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2580 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002581
egdanielb414f252014-07-29 13:15:47 -07002582 if (blendOff) {
2583 if (kNo_TriState != fHWBlendState.fEnabled) {
2584 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002585
2586 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2587 // https://code.google.com/p/skia/issues/detail?id=3943
2588 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2589 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2590 SkASSERT(this->caps()->advancedBlendEquationSupport());
2591 // Set to any basic blending equation.
2592 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2593 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2594 fHWBlendState.fEquation = blend_equation;
2595 }
2596
egdanielb414f252014-07-29 13:15:47 -07002597 fHWBlendState.fEnabled = kNo_TriState;
2598 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002599 } else {
2600 if (kYes_TriState != fHWBlendState.fEnabled) {
2601 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002602
Chris Daltonbbb3f642019-07-24 12:25:08 -04002603 fHWBlendState.fEnabled = kYes_TriState;
2604 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002605
Chris Daltonbbb3f642019-07-24 12:25:08 -04002606 if (fHWBlendState.fEquation != equation) {
2607 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2608 fHWBlendState.fEquation = equation;
2609 }
cdalton8917d622015-05-06 13:40:21 -07002610
Chris Daltonbbb3f642019-07-24 12:25:08 -04002611 if (GrBlendEquationIsAdvanced(equation)) {
2612 SkASSERT(this->caps()->advancedBlendEquationSupport());
2613 // Advanced equations have no other blend state.
2614 return;
2615 }
cdalton8917d622015-05-06 13:40:21 -07002616
Chris Daltonbbb3f642019-07-24 12:25:08 -04002617 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2618 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2619 gXfermodeCoeff2Blend[dstCoeff]));
2620 fHWBlendState.fSrcCoeff = srcCoeff;
2621 fHWBlendState.fDstCoeff = dstCoeff;
2622 }
cdalton8917d622015-05-06 13:40:21 -07002623
Chris Daltonbbb3f642019-07-24 12:25:08 -04002624 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2625 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2626 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2627 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2628 fHWBlendState.fConstColor = blendConst;
2629 fHWBlendState.fConstColorValid = true;
2630 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002631 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002632 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002633
2634 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002635}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002636
Brian Salomondc829942018-10-23 16:07:24 -04002637static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002638 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002639 switch (swizzle[i]) {
2640 case 'r': glValues[i] = GR_GL_RED; break;
2641 case 'g': glValues[i] = GR_GL_GREEN; break;
2642 case 'b': glValues[i] = GR_GL_BLUE; break;
2643 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002644 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002645 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002646 default: SK_ABORT("Unsupported component");
2647 }
Brian Salomon327955b2018-10-22 15:39:22 +00002648 }
2649}
2650
Greg Daniel2c3398d2019-06-19 11:58:01 -04002651void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2652 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002653 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002654
reed856e9d92015-09-30 12:21:45 -07002655#ifdef SK_DEBUG
2656 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002657 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002658 const int w = texture->width();
2659 const int h = texture->height();
2660 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2661 }
2662 }
2663#endif
2664
Robert Phillips294870f2016-11-11 12:38:40 -05002665 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002666 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002667 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002668 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002669 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002670 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002671 }
2672
Brian Salomondc829942018-10-23 16:07:24 -04002673 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002674 if (!this->caps()->mipMapSupport() ||
2675 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002676 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002677 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002678 }
bsalomonefd7d452014-10-23 14:17:46 -07002679
brianosman33f6b3f2016-06-02 05:49:21 -07002680#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002681 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002682 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002683 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002684 }
2685#endif
2686
Brian Salomone2826ab2019-06-04 15:58:31 -04002687 auto timestamp = texture->parameters()->resetTimestamp();
2688 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002689
Brian Salomone2826ab2019-06-04 15:58:31 -04002690 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2691 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002692 if (fSamplerObjectCache) {
2693 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2694 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002695 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2696 texture->parameters()->samplerOverriddenState();
2697 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002698
Brian Salomone2826ab2019-06-04 15:58:31 -04002699 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2700 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002701
Brian Salomone2826ab2019-06-04 15:58:31 -04002702 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2703 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002704
2705 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002706 newSamplerState.fMinLOD = -1000.f;
2707 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002708
Brian Salomone2826ab2019-06-04 15:58:31 -04002709 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002710 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002711 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002712 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002713 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002714 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002715 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002716 }
Mike Klein1bccae52018-10-19 11:38:44 +00002717 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002718 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002719 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002720 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002721 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002722 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002723 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002724 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002725 }
2726 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002727 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002728 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002729 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002730 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002731 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002732 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002733 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002734 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002735 if (this->glCaps().clampToBorderSupport()) {
2736 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002737 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002738 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002739 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2740 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002741 }
2742 }
Brian Salomondc829942018-10-23 16:07:24 -04002743 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002744 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2745 newNonsamplerState.fBaseMipMapLevel = 0;
2746 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002747
Brian Salomone2826ab2019-06-04 15:58:31 -04002748 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2749 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002750 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002751 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2752 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002753 GrGLenum glValues[4];
2754 get_gl_swizzle_values(swizzle, glValues);
2755 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002756 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002757 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002758 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2759 reinterpret_cast<const GrGLint*>(glValues)));
2760 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002761 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2762 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2763 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2764 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2765 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002766 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002767 }
2768 }
Brian Salomondc829942018-10-23 16:07:24 -04002769 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002770 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2771 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2772 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002773 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002774 this->setTextureUnit(unitIdx);
2775 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002776 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002777 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002778 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002779 this->setTextureUnit(unitIdx);
2780 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002781 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002782 }
Brian Salomon327955b2018-10-22 15:39:22 +00002783 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002784 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2785 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002786}
2787
Brian Salomon1f05d452019-02-08 12:33:08 -05002788void GrGLGpu::onResetTextureBindings() {
2789 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2790 GR_GL_TEXTURE_EXTERNAL};
2791 for (int i = 0; i < this->numTextureUnits(); ++i) {
2792 this->setTextureUnit(i);
2793 for (auto target : kTargets) {
2794 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2795 GL_CALL(BindTexture(target, 0));
2796 }
2797 }
2798 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2799 }
2800}
2801
egdaniel080e6732014-12-22 07:35:52 -08002802void GrGLGpu::flushColorWrite(bool writeColor) {
2803 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002804 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002805 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2806 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002807 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002808 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002809 } else {
2810 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002811 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002812 fHWWriteToColor = kYes_TriState;
2813 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002814 }
bsalomon3e791242014-12-17 13:43:13 -08002815}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002816
Chris Dalton674f77a2019-09-30 20:49:39 -06002817void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2818 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2819 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2820 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2821 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2822 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2823 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2824 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002825 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2826 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2827 GL_CALL(ClearColor(r, g, b, a));
2828 fHWClearColor[0] = r;
2829 fHWClearColor[1] = g;
2830 fHWClearColor[2] = b;
2831 fHWClearColor[3] = a;
2832 }
2833}
2834
bsalomon861e1032014-12-16 07:33:49 -08002835void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002836 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002837 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002838 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002839 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002840 }
2841}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002842
Brian Salomond978b902019-02-07 15:09:18 -05002843void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002844 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002845 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002846 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2847 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2848 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002849 }
Brian Salomond978b902019-02-07 15:09:18 -05002850 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2851 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002852 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002853 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002854}
2855
Brian Salomone5e7eb12016-10-14 16:18:33 -04002856// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002857static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2858 const GrSurface* src,
2859 const SkIRect& srcRect,
2860 const SkIPoint& dstPoint,
2861 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002862 int dstSampleCnt = 0;
2863 int srcSampleCnt = 0;
2864 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002865 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002866 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002867 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002868 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002869 }
2870 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2871 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2872
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002873 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2874 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2875
Brian Salomone5e7eb12016-10-14 16:18:33 -04002876 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002877 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002878
Greg Daniel46cfbc62019-06-07 11:43:30 -04002879 GrTextureType dstTexType;
2880 GrTextureType* dstTexTypePtr = nullptr;
2881 GrTextureType srcTexType;
2882 GrTextureType* srcTexTypePtr = nullptr;
2883 if (dstTex) {
2884 dstTexType = dstTex->texturePriv().textureType();
2885 dstTexTypePtr = &dstTexType;
2886 }
2887 if (srcTex) {
2888 srcTexType = srcTex->texturePriv().textureType();
2889 srcTexTypePtr = &srcTexType;
2890 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002891
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002892 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2893 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002894 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002895}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002896
Brian Osmana9c8a052018-01-19 10:31:56 -05002897static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2898 // A RT has a separate MSAA renderbuffer if:
2899 // 1) It's multisampled
2900 // 2) We're using an extension with separate MSAA renderbuffers
2901 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002902 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002903}
2904
Greg Daniel46cfbc62019-06-07 11:43:30 -04002905static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002906 const GrGLCaps& caps) {
2907
bsalomon@google.comeb851172013-04-15 13:51:00 +00002908 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002909 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002910 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002911 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002912
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002913 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2914 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2915
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002916 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2917 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2918
Greg Daniel46cfbc62019-06-07 11:43:30 -04002919 GrTextureType dstTexType;
2920 GrTextureType* dstTexTypePtr = nullptr;
2921 GrTextureType srcTexType;
2922 GrTextureType* srcTexTypePtr = nullptr;
2923 if (dstTex) {
2924 dstTexType = dstTex->texturePriv().textureType();
2925 dstTexTypePtr = &dstTexType;
2926 }
2927 if (srcTex) {
2928 srcTexType = srcTex->texturePriv().textureType();
2929 srcTexTypePtr = &srcTexType;
2930 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002931
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002932 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2933 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002934}
2935
Greg Danielacd66b42019-05-22 16:29:12 -04002936// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002937void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002938 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002939 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002940 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002941 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002942 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2943 GrGLuint texID = texture->textureID();
2944 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002945 GrGLuint* tempFBOID;
2946 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002947
egdanield803f272015-03-18 13:01:52 -07002948 if (0 == *tempFBOID) {
2949 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002950 }
2951
Adrienne Walker4ee88512018-05-17 11:37:14 -07002952 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002953 GR_GL_CALL(
2954 this->glInterface(),
2955 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2956 if (mipLevel == 0) {
2957 texture->baseLevelWasBoundToFBO();
2958 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002959 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002960 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002961 }
egdaniel0f5f9672015-02-03 11:10:51 -08002962}
2963
Brian Salomond2a8ae22019-09-10 16:03:59 -04002964void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002965 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002966 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002967 SkASSERT(surface->asTexture());
2968 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2969 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2970 GR_GL_COLOR_ATTACHMENT0,
2971 textureTarget,
2972 0,
2973 0));
2974 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002975}
2976
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002977void GrGLGpu::onFBOChanged() {
2978 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2979 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2980 GL_CALL(Flush());
2981 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002982#ifdef SK_DEBUG
2983 if (fIsExecutingCommandBuffer_DebugOnly) {
2984 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2985 "This will severely hurt performance.\n");
2986 }
2987#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002988}
2989
Adrienne Walker4ee88512018-05-17 11:37:14 -07002990void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2991 fStats.incRenderTargetBinds();
2992 GL_CALL(BindFramebuffer(target, fboid));
2993 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2994 fBoundDrawFramebuffer = fboid;
2995 }
2996
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002997 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2998 // The driver forgets the correct scissor when modifying the FBO binding.
2999 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003000 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003001 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003002 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003003 }
3004
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003005 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003006}
3007
Adrienne Walker4ee88512018-05-17 11:37:14 -07003008void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3009 if (fboid == fBoundDrawFramebuffer &&
3010 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3011 // This workaround only applies to deleting currently bound framebuffers
3012 // on Adreno 420. Because this is a somewhat rare case, instead of
3013 // tracking all the attachments of every framebuffer instead just always
3014 // unbind all attachments.
3015 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3016 GR_GL_RENDERBUFFER, 0));
3017 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3018 GR_GL_RENDERBUFFER, 0));
3019 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3020 GR_GL_RENDERBUFFER, 0));
3021 }
3022
3023 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003024
3025 // Deleting the currently bound framebuffer rebinds to 0.
3026 if (fboid == fBoundDrawFramebuffer) {
3027 this->onFBOChanged();
3028 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003029}
3030
Greg Daniel46cfbc62019-06-07 11:43:30 -04003031bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003032 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003033 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3034 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3035 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003036 auto dstFormat = dst->backendFormat().asGLFormat();
3037 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003038 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003039 return true;
3040 }
3041 }
cblume61214052016-01-26 09:10:48 -08003042
Greg Daniel46cfbc62019-06-07 11:43:30 -04003043 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3044 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003045 return true;
3046 }
3047
Greg Daniel46cfbc62019-06-07 11:43:30 -04003048 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3049 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003050 }
3051
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003052 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003053 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003054 return true;
3055 }
bsalomon083617b2016-02-12 12:10:14 -08003056 }
3057
bsalomon6df86402015-06-01 10:41:49 -07003058 return false;
3059}
3060
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003061bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003062 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003063
3064 int progIdx = TextureToCopyProgramIdx(srcTex);
3065 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3066 GrSLType samplerType =
3067 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3068
3069 if (!fCopyProgramArrayBuffer) {
3070 static const GrGLfloat vdata[] = {
3071 0, 0,
3072 0, 1,
3073 1, 0,
3074 1, 1
3075 };
3076 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3077 kStatic_GrAccessPattern, vdata);
3078 }
3079 if (!fCopyProgramArrayBuffer) {
3080 return false;
3081 }
3082
3083 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3084 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3085 if (!fCopyPrograms[progIdx].fProgram) {
3086 return false;
3087 }
3088
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003089 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3090 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3091 GrShaderVar::kUniform_TypeModifier);
3092 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3093 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3094 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3095 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3096
Greg Daniel9e3169b2019-06-06 14:38:17 -04003097 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003098 if (shaderCaps->noperspectiveInterpolationSupport()) {
3099 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3100 vshaderTxt.appendf("#extension %s : require\n", extension);
3101 }
3102 vTexCoord.addModifier("noperspective");
3103 }
3104
3105 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3106 vshaderTxt.append(";");
3107 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3108 vshaderTxt.append(";");
3109 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3110 vshaderTxt.append(";");
3111 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3112 vshaderTxt.append(";");
3113
3114 vshaderTxt.append(
3115 "// Copy Program VS\n"
3116 "void main() {"
3117 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3118 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3119 " sk_Position.zw = half2(0, 1);"
3120 "}"
3121 );
3122
Greg Daniel9e3169b2019-06-06 14:38:17 -04003123 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003124 if (shaderCaps->noperspectiveInterpolationSupport()) {
3125 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3126 fshaderTxt.appendf("#extension %s : require\n", extension);
3127 }
3128 }
3129 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3130 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3131 fshaderTxt.append(";");
3132 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3133 fshaderTxt.append(";");
3134 fshaderTxt.appendf(
3135 "// Copy Program FS\n"
3136 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003137 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003138 "}"
3139 );
3140
3141 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3142 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3143 SkSL::Program::Settings settings;
3144 settings.fCaps = shaderCaps;
3145 SkSL::String glsl;
3146 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3147 sksl, settings, &glsl, errorHandler);
3148 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3149 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3150 SkASSERT(program->fInputs.isEmpty());
3151
3152 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3153 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3154 errorHandler);
3155 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3156 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3157 errorHandler);
3158 SkASSERT(program->fInputs.isEmpty());
3159
3160 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3161
3162 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3163 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3164 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3165 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3166 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3167 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3168
3169 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3170
3171 GL_CALL(DeleteShader(vshader));
3172 GL_CALL(DeleteShader(fshader));
3173
3174 return true;
3175}
3176
brianosman33f6b3f2016-06-02 05:49:21 -07003177bool GrGLGpu::createMipmapProgram(int progIdx) {
3178 const bool oddWidth = SkToBool(progIdx & 0x2);
3179 const bool oddHeight = SkToBool(progIdx & 0x1);
3180 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3181
Brian Salomon1edc5b92016-11-29 13:43:46 -05003182 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003183
3184 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3185 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3186 if (!fMipmapPrograms[progIdx].fProgram) {
3187 return false;
3188 }
3189
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003190 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3191 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003192 GrShaderVar::kUniform_TypeModifier);
3193 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3194 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003195 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003196 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003197 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3198 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3199 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3200 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003201 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003202 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003203
Ethan Nicholasfc994162019-06-06 10:04:27 -04003204 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003205 if (shaderCaps->noperspectiveInterpolationSupport()) {
3206 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003207 vshaderTxt.appendf("#extension %s : require\n", extension);
3208 }
3209 vTexCoords[0].addModifier("noperspective");
3210 vTexCoords[1].addModifier("noperspective");
3211 vTexCoords[2].addModifier("noperspective");
3212 vTexCoords[3].addModifier("noperspective");
3213 }
3214
Brian Salomon1edc5b92016-11-29 13:43:46 -05003215 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003216 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003217 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003218 vshaderTxt.append(";");
3219 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003220 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003221 vshaderTxt.append(";");
3222 }
3223
3224 vshaderTxt.append(
3225 "// Mipmap Program VS\n"
3226 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003227 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3228 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003229 );
3230
3231 // Insert texture coordinate computation:
3232 if (oddWidth && oddHeight) {
3233 vshaderTxt.append(
3234 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003235 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3236 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003237 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3238 );
3239 } else if (oddWidth) {
3240 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003241 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3242 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003243 );
3244 } else if (oddHeight) {
3245 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003246 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3247 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003248 );
3249 } else {
3250 vshaderTxt.append(
3251 " v_texCoord0 = a_vertex.xy;"
3252 );
3253 }
3254
3255 vshaderTxt.append("}");
3256
Ethan Nicholasfc994162019-06-06 10:04:27 -04003257 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003258 if (shaderCaps->noperspectiveInterpolationSupport()) {
3259 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003260 fshaderTxt.appendf("#extension %s : require\n", extension);
3261 }
3262 }
brianosman33f6b3f2016-06-02 05:49:21 -07003263 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003264 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003265 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003266 fshaderTxt.append(";");
3267 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003268 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003269 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003270 fshaderTxt.append(
3271 "// Mipmap Program FS\n"
3272 "void main() {"
3273 );
3274
3275 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003276 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003277 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3278 " sample(u_texture, v_texCoord1) + "
3279 " sample(u_texture, v_texCoord2) + "
3280 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003281 );
3282 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003283 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003284 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3285 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003286 );
3287 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003288 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003289 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003290 );
3291 }
3292
3293 fshaderTxt.append("}");
3294
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003295 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003296 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003297 SkSL::Program::Settings settings;
3298 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003299 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003300 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003301 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003302 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003303 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003304 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003305
Brian Osman6c431d52019-04-15 16:31:54 -04003306 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003307 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3308 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003309 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003310 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003311 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003312 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003313
3314 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3315
3316 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3317 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3318 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3319 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3320
3321 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3322
3323 GL_CALL(DeleteShader(vshader));
3324 GL_CALL(DeleteShader(fshader));
3325
3326 return true;
3327}
3328
Greg Daniel46cfbc62019-06-07 11:43:30 -04003329bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003330 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003331 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3332 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3333 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3334 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003335 return false;
3336 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003337 int progIdx = TextureToCopyProgramIdx(srcTex);
3338 if (!dstRT) {
3339 SkASSERT(dstTex);
3340 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3341 return false;
3342 }
3343 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003344 if (!fCopyPrograms[progIdx].fProgram) {
3345 if (!this->createCopyProgram(srcTex)) {
3346 SkDebugf("Failed to create copy program.\n");
3347 return false;
3348 }
3349 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003350 int w = srcRect.width();
3351 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003352 // We don't swizzle at all in our copies.
3353 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003354 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003355 this->flushViewport(dst->width(), dst->height());
3356 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003357 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003358 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003359 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003360 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3361 attribs->enableVertexArrays(this, 1);
3362 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3363 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003364 // dst rect edges in NDC (-1 to 1)
3365 int dw = dst->width();
3366 int dh = dst->height();
3367 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3368 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3369 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3370 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003371 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3372 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3373 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3374 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3375 int sw = src->width();
3376 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003377 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3378 // src rect edges in normalized texture space (0 to 1)
3379 sx0 /= sw;
3380 sx1 /= sw;
3381 sy0 /= sh;
3382 sy1 /= sh;
3383 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003384 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3385 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3386 sx1 - sx0, sy1 - sy0, sx0, sy0));
3387 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003388 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003389 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003390 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003391 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003392 this->disableScissor();
3393 this->disableWindowRectangles();
3394 this->disableStencil();
3395 if (this->glCaps().srgbWriteControl()) {
3396 this->flushFramebufferSRGB(true);
3397 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003398 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003399 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003400 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3401 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003402 return true;
3403}
3404
Greg Daniel46cfbc62019-06-07 11:43:30 -04003405void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003406 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003407 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003408 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003409 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003410 SkASSERT(dstTex);
3411 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003412 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003413
Brian Salomond978b902019-02-07 15:09:18 -05003414 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003415 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003416 dstPoint.fX, dstPoint.fY,
3417 srcRect.fLeft, srcRect.fTop,
3418 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003419 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003420 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3421 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003422 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3423 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003424}
3425
Greg Daniel46cfbc62019-06-07 11:43:30 -04003426bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003427 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003428 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003429 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3430 srcRect.width(), srcRect.height());
3431 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003432 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003433 return false;
mtklein404b3b22015-05-18 09:29:10 -07003434 }
bsalomon5df6fee2015-05-18 06:26:15 -07003435 }
bsalomon6df86402015-06-01 10:41:49 -07003436
Brian Salomond2a8ae22019-09-10 16:03:59 -04003437 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3438 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003439 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003440 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003441
3442 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003443 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003444 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003445
Greg Daniel46cfbc62019-06-07 11:43:30 -04003446 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3447 srcRect.fTop,
3448 srcRect.fRight,
3449 srcRect.fBottom,
3450 dstRect.fLeft,
3451 dstRect.fTop,
3452 dstRect.fRight,
3453 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003454 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003455 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3456 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003457
3458 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3459 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003460 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003461}
3462
Brian Salomon930f9392018-06-20 16:25:26 -04003463bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3464 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003465 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003466 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003467 return false;
3468 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003469 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003470 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3471 // Uses draw calls to do a series of downsample operations to successive mips.
3472
3473 // The manual approach requires the ability to limit which level we're sampling and that the
3474 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003475 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003476 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003477 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003478 GL_CALL(GenerateMipmap(glTex->target()));
3479 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003480 }
3481
brianosman33f6b3f2016-06-02 05:49:21 -07003482 int width = texture->width();
3483 int height = texture->height();
3484 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003485 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003486
3487 // Create (if necessary), then bind temporary FBO:
3488 if (0 == fTempDstFBOID) {
3489 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3490 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003491 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003492 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003493
3494 // Bind the texture, to get things configured for filtering.
3495 // We'll be changing our base level further below:
3496 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003497 // The mipmap program does not do any swizzling.
3498 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003499
3500 // Vertex data:
3501 if (!fMipmapProgramArrayBuffer) {
3502 static const GrGLfloat vdata[] = {
3503 0, 0,
3504 0, 1,
3505 1, 0,
3506 1, 1
3507 };
Brian Salomonae64c192019-02-05 09:41:37 -05003508 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003509 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003510 }
3511 if (!fMipmapProgramArrayBuffer) {
3512 return false;
3513 }
3514
3515 fHWVertexArrayState.setVertexArrayID(this, 0);
3516
3517 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003518 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003519 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003520 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003521
3522 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003523 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003524 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003525 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003526 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003527 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003528
3529 // Do all the blits:
3530 width = texture->width();
3531 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003532
brianosman33f6b3f2016-06-02 05:49:21 -07003533 for (GrGLint level = 1; level < levelCount; ++level) {
3534 // Get and bind the program for this particular downsample (filter shape can vary):
3535 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3536 if (!fMipmapPrograms[progIdx].fProgram) {
3537 if (!this->createMipmapProgram(progIdx)) {
3538 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003539 // Invalidate all params to cover base level change in a previous iteration.
3540 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003541 return false;
3542 }
3543 }
Brian Salomon802cb312018-06-08 18:05:20 -04003544 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003545
3546 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3547 const float invWidth = 1.0f / width;
3548 const float invHeight = 1.0f / height;
3549 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3550 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3551 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3552
3553 // Only sample from previous mip
3554 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3555
Brian Salomon930f9392018-06-20 16:25:26 -04003556 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3557 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003558
3559 width = SkTMax(1, width / 2);
3560 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003561 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003562
3563 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3564 }
3565
3566 // Unbind:
3567 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3568 GR_GL_TEXTURE_2D, 0, 0));
3569
Brian Salomon930f9392018-06-20 16:25:26 -04003570 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003571 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3572 // We drew the 2nd to last level into the last level.
3573 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3574 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003575
brianosman33f6b3f2016-06-02 05:49:21 -07003576 return true;
3577}
3578
Chris Daltond7291ba2019-03-07 14:17:03 -07003579void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003580 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3581 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003582
3583 int effectiveSampleCnt;
3584 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003585 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003586
3587 sampleLocations->reset(effectiveSampleCnt);
3588 for (int i = 0; i < effectiveSampleCnt; ++i) {
3589 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3590 }
3591}
3592
cdalton231c5fd2015-05-13 12:35:36 -07003593void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003594 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003595 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003596 case kTexture_GrXferBarrierType: {
3597 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003598 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003599 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3600 // The render target uses separate storage so no need for glTextureBarrier.
3601 // FIXME: The render target will resolve automatically when its texture is bound,
3602 // but we could resolve only the bounds that will be read if we do it here instead.
3603 return;
3604 }
cdalton9954bc32015-04-29 14:17:00 -07003605 SkASSERT(this->caps()->textureBarrierSupport());
3606 GL_CALL(TextureBarrier());
3607 return;
cdalton231c5fd2015-05-13 12:35:36 -07003608 }
cdalton8917d622015-05-06 13:40:21 -07003609 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003610 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003611 this->caps()->blendEquationSupport());
3612 GL_CALL(BlendBarrier());
3613 return;
bsalomoncb02b382015-08-12 11:14:50 -07003614 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003615 }
3616}
3617
Brian Salomon5043f1f2019-07-11 21:27:54 -04003618static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003619 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003620 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3621 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3622 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3623 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3624 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3625 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3626 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3627 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3628 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003629 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003630 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003631 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3632 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3633 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3634 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003635
Brian Salomon5043f1f2019-07-11 21:27:54 -04003636 // Configs with multiple equivalent formats.
3637
Robert Phillipsebab03f2019-07-22 08:48:18 -04003638 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3639 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3640
Brian Salomon5043f1f2019-07-11 21:27:54 -04003641 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3642 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3643
3644 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3645 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003646 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003647 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003648}
3649
Brian Salomon85c3d682019-11-04 15:04:54 -05003650GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003651 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003652 GrRenderable renderable,
Brian Salomon85c3d682019-11-04 15:04:54 -05003653 const BackendTextureData* data,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003654 GrMipMapped mipMapped,
Robert Phillips57ef6802019-09-23 10:12:47 -04003655 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003656 // We don't support protected textures in GL.
3657 if (isProtected == GrProtected::kYes) {
3658 return {};
3659 }
3660
Brian Salomon8a375832018-03-14 10:21:40 -04003661 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003662
Brian Salomond4764a12019-08-08 12:08:24 -04003663 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003664 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003665 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003666 }
3667
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003668 int numMipLevels = 1;
3669 if (mipMapped == GrMipMapped::kYes) {
3670 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3671 }
3672
Robert Phillipsd34691b2019-09-24 13:38:43 -04003673 // Compressed formats go through onCreateCompressedBackendTexture
3674 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3675
Greg Daniel15500642019-06-27 03:05:55 +00003676 GrGLTextureInfo info;
3677 GrGLTextureParameters::SamplerOverriddenState initialState;
3678
Robert Phillipsd34691b2019-09-24 13:38:43 -04003679 GrSurfaceDesc desc;
Brian Salomon85c3d682019-11-04 15:04:54 -05003680 desc.fWidth = dimensions.width();
3681 desc.fHeight = dimensions.height();
3682 desc.fConfig = gl_format_to_pixel_config(glFormat);
3683 if (desc.fConfig == kUnknown_GrPixelConfig) {
Robert Phillips42716d42019-12-16 12:19:54 -05003684 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003685 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003686
Robert Phillipsd34691b2019-09-24 13:38:43 -04003687 info.fTarget = GR_GL_TEXTURE_2D;
3688 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003689 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003690 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003691 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003692 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003693
Robert Phillips42716d42019-12-16 12:19:54 -05003694 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003695 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3696 SkTDArray<GrMipLevel> texels;
3697 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3698 // Incorporate the color type into the config to make it "specific" if applicable.
3699 desc.fConfig = this->caps()->getConfigFromBackendFormat(format, colorType);
3700 SkASSERT(desc.fConfig != kUnknown_GrPixelConfig);
3701 texels.append(numMipLevels);
3702 for (int i = 0; i < numMipLevels; ++i) {
3703 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3704 }
3705 if (!this->uploadTexData(glFormat, colorType, desc.fWidth, desc.fHeight, GR_GL_TEXTURE_2D,
3706 0, 0, desc.fWidth, desc.fHeight, colorType, texels.begin(),
3707 texels.count())) {
3708 GL_CALL(DeleteTextures(1, &info.fID));
3709 return {};
3710 }
3711 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3712 // TODO: Unify this with the clear texture code in onCreateTexture().
3713 GrColorType colorType;
3714 GrGLenum externalFormat, externalType;
3715 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3716 &externalType, &colorType);
3717 if (colorType == GrColorType::kUnknown) {
3718 GL_CALL(DeleteTextures(1, &info.fID));
3719 return {};
3720 }
3721
3722 // Make one tight image at the base size and reuse it for smaller levels.
3723 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3724 auto rb = ii.minRowBytes();
3725 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3726 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3727 GL_CALL(DeleteTextures(1, &info.fID));
3728 return {};
3729 }
3730
3731 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3732 SkISize levelDimensions = dimensions;
3733 for (int i = 0; i < numMipLevels; ++i) {
3734 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3735 levelDimensions.height(), externalFormat, externalType,
3736 pixelStorage.get()));
3737 levelDimensions = {SkTMax(1, levelDimensions.width() /2),
3738 SkTMax(1, levelDimensions.height()/2)};
3739 }
3740 }
3741 // Unbind this texture from the scratch texture unit.
3742 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003743
Brian Salomone2826ab2019-06-04 15:58:31 -04003744 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003745 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003746 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3747 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003748
Brian Salomon85c3d682019-11-04 15:04:54 -05003749 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3750 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003751}
3752
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003753void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003754 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3755
3756 GrGLTextureInfo info;
3757 if (tex.getGLTextureInfo(&info)) {
3758 GL_CALL(DeleteTextures(1, &info.fID));
3759 }
3760}
3761
3762#if GR_TEST_UTILS
3763
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003764bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003765 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003766
Greg Daniel52e16d92018-04-10 09:34:07 -04003767 GrGLTextureInfo info;
3768 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003769 return false;
3770 }
3771
3772 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003773 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003774
3775 return (GR_GL_TRUE == result);
3776}
3777
Brian Salomonf865b052018-03-09 09:01:53 -05003778GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003779 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003780 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3781 return GrBackendRenderTarget(); // invalid
3782 }
Brian Salomon8a375832018-03-14 10:21:40 -04003783 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003784 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003785 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003786 return {};
3787 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003788 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003789 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003790 if (sFormatIdx < 0) {
3791 return {};
3792 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003793 GrGLuint colorID = 0;
3794 GrGLuint stencilID = 0;
3795 auto deleteIDs = [&] {
3796 if (colorID) {
3797 if (useTexture) {
3798 GL_CALL(DeleteTextures(1, &colorID));
3799 } else {
3800 GL_CALL(DeleteRenderbuffers(1, &colorID));
3801 }
Brian Salomonf865b052018-03-09 09:01:53 -05003802 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003803 if (stencilID) {
3804 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003805 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003806 };
3807
3808 if (useTexture) {
3809 GL_CALL(GenTextures(1, &colorID));
3810 } else {
3811 GL_CALL(GenRenderbuffers(1, &colorID));
3812 }
3813 GL_CALL(GenRenderbuffers(1, &stencilID));
3814 if (!stencilID || !colorID) {
3815 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003816 return {};
3817 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003818
Brian Salomonf865b052018-03-09 09:01:53 -05003819 GrGLFramebufferInfo info;
3820 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003821 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003822 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3823 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003824 deleteIDs();
3825 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003826 }
3827
3828 this->invalidateBoundRenderTarget();
3829
Adrienne Walker4ee88512018-05-17 11:37:14 -07003830 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003831 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003832 GrGLTextureParameters::SamplerOverriddenState initialState;
3833 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3834 if (!colorID) {
3835 deleteIDs();
3836 return {};
3837 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003838 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3839 colorID, 0));
3840 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003841 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003842 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3843 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003844 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003845 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3846 GR_GL_RENDERBUFFER, colorID));
3847 }
3848 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003849 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3850 GL_ALLOC_CALL(this->glInterface(),
3851 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3852 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003853 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003854 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3855 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003856 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003857 }
3858
Brian Salomon93348dd2018-08-29 12:56:23 -04003859 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3860 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3861 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3862 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003863 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003864 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003865
Adrienne Walker4ee88512018-05-17 11:37:14 -07003866 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003867 GrGLenum status;
3868 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3869 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003870 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003871 return {};
3872 }
3873 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003874
Greg Daniel108bb232018-07-03 16:18:29 -04003875 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003876 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003877 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003878}
3879
3880void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003881 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003882 GrGLFramebufferInfo info;
3883 if (backendRT.getGLFramebufferInfo(&info)) {
3884 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003885 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003886 }
3887 }
joshualitt8fd844f2015-12-02 13:36:47 -08003888}
3889
Greg Daniel26b50a42018-03-08 09:49:58 -05003890void GrGLGpu::testingOnly_flushGpuAndSync() {
3891 GL_CALL(Finish());
3892}
Brian Salomonf865b052018-03-09 09:01:53 -05003893#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003894
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003895///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003896
cdaltone2e71c22016-04-07 18:13:29 -07003897GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003898 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003899 GrGLAttribArrayState* attribState;
3900
cdaltone2e71c22016-04-07 18:13:29 -07003901 if (gpu->glCaps().isCoreProfile()) {
3902 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003903 GrGLuint arrayID;
3904 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3905 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003906 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003907 }
cdaltone2e71c22016-04-07 18:13:29 -07003908 if (ibuf) {
3909 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003910 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003911 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003912 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003913 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003914 if (ibuf) {
3915 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003916 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003917 } else {
3918 this->setVertexArrayID(gpu, 0);
3919 }
3920 int attrCount = gpu->glCaps().maxVertexAttributes();
3921 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3922 fDefaultVertexArrayAttribState.resize(attrCount);
3923 }
3924 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003925 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003926 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003927}
bsalomone179a912016-01-20 06:18:10 -08003928
Greg Daniel30a35e82019-11-19 14:12:25 -05003929bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003930 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003931 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003932 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003933 // We call finish if the client told us to sync or if we have a finished proc but don't support
3934 // GLsync objects.
3935 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3936 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3937 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003938 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003939 // After a finish everything previously sent to GL is done.
3940 for (const auto& cb : fFinishCallbacks) {
3941 cb.fCallback(cb.fContext);
3942 this->deleteSync(cb.fSync);
3943 }
3944 fFinishCallbacks.clear();
3945 if (info.fFinishedProc) {
3946 info.fFinishedProc(info.fFinishedContext);
3947 }
3948 } else {
3949 if (info.fFinishedProc) {
3950 FinishCallback callback;
3951 callback.fCallback = info.fFinishedProc;
3952 callback.fContext = info.fFinishedContext;
3953 callback.fSync = (GrGLsync)this->insertFence();
3954 fFinishCallbacks.push_back(callback);
3955 GL_CALL(Flush());
3956 } else if (insertedSemaphore) {
3957 // Must call flush after semaphores in case they are waited on another GL context.
3958 GL_CALL(Flush());
3959 }
3960 // See if any previously inserted finish procs are good to go.
3961 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003962 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003963 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003964}
3965
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003966void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3967 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3968 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3969 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003970}
3971
Greg Daniel6be35232017-03-01 17:01:09 -05003972GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003973 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003974 GrGLsync sync;
3975 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Brian Salomon4dea72a2019-12-18 10:43:10 -05003976 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003977 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003978}
3979
Brian Salomonb0d8b762019-05-06 16:58:22 -04003980bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
3981 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07003982 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04003983 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3984 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3985}
3986
3987bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3988 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003989}
3990
3991void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003992 this->deleteSync((GrGLsync)fence);
3993}
3994
Greg Daniel301015c2019-11-18 14:06:46 -05003995std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003996 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003997 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003998}
3999
Greg Daniel301015c2019-11-18 14:06:46 -05004000std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4001 const GrBackendSemaphore& semaphore,
4002 GrResourceProvider::SemaphoreWrapType wrapType,
4003 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004004 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004005 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4006}
4007
Greg Daniel301015c2019-11-18 14:06:46 -05004008void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4009 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004010
Greg Daniel48661b82018-01-22 16:11:35 -05004011 GrGLsync sync;
4012 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4013 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004014}
4015
Greg Daniel301015c2019-11-18 14:06:46 -05004016void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4017 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004018
4019 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4020}
4021
Brian Salomonb0d8b762019-05-06 16:58:22 -04004022void GrGLGpu::checkFinishProcs() {
4023 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4024 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4025 /* timeout = */ 0, /* flush = */ false)) {
4026 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4027 this->deleteSync(fFinishCallbacks.front().fSync);
4028 fFinishCallbacks.pop_front();
4029 }
4030}
4031
Greg Daniel6be35232017-03-01 17:01:09 -05004032void GrGLGpu::deleteSync(GrGLsync sync) const {
4033 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004034}
Brian Osman13dddce2017-05-09 13:19:50 -04004035
Greg Daniel301015c2019-11-18 14:06:46 -05004036std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004037 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004038 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004039 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004040 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004041 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4042 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004043
4044 return semaphore;
4045}
Robert Phillips646e4292017-06-13 12:44:56 -04004046
4047int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004048 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004049 case kTexture2DSampler_GrSLType:
4050 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004051 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004052 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004053 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004054 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004055 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004056 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004057 }
4058}
Brian Osman71a18892017-08-10 10:23:25 -04004059
Kevin Lubickf4def342018-10-04 12:52:50 -04004060#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004061#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004062void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4063 // We are called by the base class, which has already called beginObject(). We choose to nest
4064 // all of our caps information in a named sub-object.
4065 writer->beginObject("GL GPU");
4066
4067 const GrGLubyte* str;
4068 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4069 writer->appendString("GL_VERSION", (const char*)(str));
4070 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4071 writer->appendString("GL_RENDERER", (const char*)(str));
4072 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4073 writer->appendString("GL_VENDOR", (const char*)(str));
4074 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4075 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4076
4077 writer->appendName("extensions");
4078 glInterface()->fExtensions.dumpJSON(writer);
4079
4080 writer->endObject();
4081}
Kevin Lubickf4def342018-10-04 12:52:50 -04004082#endif