blob: 89c1fcafe8bd551efb376363fb5b39fbf810f724 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
18#include "src/core/SkConvertPixels.h"
19#include "src/core/SkMakeUnique.h"
20#include "src/core/SkMipMap.h"
21#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040022#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040024#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrFixedClip.h"
26#include "src/gpu/GrGpuResourcePriv.h"
27#include "src/gpu/GrMesh.h"
28#include "src/gpu/GrPipeline.h"
29#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};
87GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
88GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
89GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
90GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
91GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
92GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
93GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
94GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
95GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
96GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
97GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
98GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
99GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
100GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
101GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
102GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
103GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
104GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700105GR_STATIC_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];
bsalomonf7cc8772015-05-11 11:21:14 -0700160 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
163 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
164 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
165 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
166 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
167 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
168 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
169 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
170 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
171 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
172 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
173 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
174 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
175 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176
bsalomon@google.com47059542012-06-06 20:51:20 +0000177 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
178 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
179 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
180 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000181
182 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700183 GR_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 Salomonae64c192019-02-05 09:41:37 -0500407 GR_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
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400554 if (this->caps()->wireframeMode()) {
555 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
556 } else {
557 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
558 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500559#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000560 // Since ES doesn't support glPointSize at all we always use the VS to
561 // set the point size
562 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
563
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000564 }
joshualitt58162332014-08-01 06:44:53 -0700565
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400566 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500567 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700568 // The arm extension requires specifically enabling MSAA fetching per sample.
569 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500570 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700571 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000572 fHWWriteToColor = kUnknown_TriState;
573 // we only ever use lines in hairline mode
574 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700575 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500576
577 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000578 }
edisonn@google.comba669992013-06-28 16:03:21 +0000579
egdanielb414f252014-07-29 13:15:47 -0700580 if (resetBits & kMSAAEnable_GrGLBackendState) {
581 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700582
Chris Dalton6ce447a2019-06-23 18:07:38 -0600583 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800584 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
585 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700586 GL_CALL(CoverageModulation(GR_GL_RGBA));
587 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000588 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000589
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000590 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400591 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000592
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000593 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500594 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500595 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 }
Brian Salomondc829942018-10-23 16:07:24 -0400597 if (fSamplerObjectCache) {
598 fSamplerObjectCache->invalidateBindings();
599 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000600 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000601
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 if (resetBits & kBlend_GrGLBackendState) {
603 fHWBlendState.invalidate();
604 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000605
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 if (resetBits & kView_GrGLBackendState) {
607 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700608 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 fHWViewport.invalidate();
610 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000611
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 if (resetBits & kStencil_GrGLBackendState) {
613 fHWStencilSettings.invalidate();
614 fHWStencilTestEnabled = kUnknown_TriState;
615 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000616
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000617 // Vertex
618 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700619 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500620 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
621 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000623
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000624 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500625 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700626 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000627 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000628
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000629 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700630 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700631 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000632 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000633 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000634
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000635 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400637 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000638 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
639 }
Brian Salomon1047a492019-07-02 12:25:21 -0400640 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
642 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000643 if (this->glCaps().packFlipYSupport()) {
644 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
645 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000646 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000647
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000648 if (resetBits & kProgram_GrGLBackendState) {
649 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400650 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000651 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400652 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000653}
654
Brian Salomonea4ad302019-08-07 13:04:55 -0400655static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
656 const GrGLCaps& caps, GrGLTexture::Desc* desc,
657 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400658 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400659 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400660 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800661 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000662
Brian Salomonea4ad302019-08-07 13:04:55 -0400663 desc->fSize = {backendTex.width(), backendTex.height()};
664 desc->fTarget = info.fTarget;
665 desc->fID = info.fID;
666 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800667
Brian Salomonea4ad302019-08-07 13:04:55 -0400668 if (desc->fFormat == GrGLFormat::kUnknown) {
669 return false;
670 }
671 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400672 if (!caps.shaderCaps()->externalTextureSupport()) {
673 return false;
674 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400675 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
676 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400677 return false;
678 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400679 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400680 return false;
681 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400682 if (backendTex.isProtected()) {
683 // Not supported in GL backend at this time.
684 return false;
685 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400686
687 desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType);
688 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
689
Brian Salomond17f6582017-07-19 18:28:58 -0400690 return true;
691}
692
693sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400694 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400695 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400696 GrGLTexture::Desc desc;
697 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400698 return nullptr;
699 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400700
Brian Salomond17f6582017-07-19 18:28:58 -0400701 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400702 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400703 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400704 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400705 }
bsalomone5286e02016-01-14 09:24:09 -0800706
Greg Daniel177e6952017-10-12 12:27:11 -0400707 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
708 : GrMipMapsStatus::kNotAllocated;
709
Brian Salomonea4ad302019-08-07 13:04:55 -0400710 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400711 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400712 // We don't know what parameters are already set on wrapped textures.
713 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400714 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400715}
716
717sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400718 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400719 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500720 GrWrapOwnership ownership,
721 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400722 const GrGLCaps& caps = this->glCaps();
723
Brian Salomonea4ad302019-08-07 13:04:55 -0400724 GrGLTexture::Desc desc;
725 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800726 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800727 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400728 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
729 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800730
Brian Salomond17f6582017-07-19 18:28:58 -0400731 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400732 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800733 return nullptr;
734 }
bsalomon10528f12015-10-14 12:54:52 -0700735
Brian Osman766fcbb2017-03-13 09:33:09 -0400736 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400737 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400738 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800740 }
bsalomonb15b4c12014-10-29 12:41:57 -0700741
Robert Phillips0902c982019-07-16 07:47:56 -0400742
Greg Daniel6fa62e22019-08-07 15:52:37 -0400743 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
744 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000745
Brian Salomonea4ad302019-08-07 13:04:55 -0400746 GrGLRenderTarget::IDs rtIDs;
747 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400748 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000749 }
Greg Daniel177e6952017-10-12 12:27:11 -0400750
751 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
752 : GrMipMapsStatus::kNotAllocated;
753
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500754 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400755 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400756 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400757 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400758 // We don't know what parameters are already set on wrapped textures.
759 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400760 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000761}
762
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400763sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
764 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400765 GrGLFramebufferInfo info;
766 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000767 return nullptr;
768 }
769
Brian Salomone8a766b2019-07-19 14:24:36 -0400770 if (backendRT.isProtected()) {
771 // Not supported in GL at this time.
772 return nullptr;
773 }
774
Brian Salomond4764a12019-08-08 12:08:24 -0400775 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400776 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
777 return nullptr;
778 }
779
Brian Salomonea4ad302019-08-07 13:04:55 -0400780 GrGLRenderTarget::IDs rtIDs;
781 rtIDs.fRTFBOID = info.fFBOID;
782 rtIDs.fMSColorRenderbufferID = 0;
783 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
784 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000785
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400786 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
787 grColorType);
Brian Salomond4764a12019-08-08 12:08:24 -0400788 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400789
Brian Salomonea4ad302019-08-07 13:04:55 -0400790 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400791 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700792
Brian Salomonea4ad302019-08-07 13:04:55 -0400793 return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400794 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000795}
796
Greg Daniel7ef28f32017-04-20 16:41:55 +0000797sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400798 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400799 GrColorType colorType) {
800 GrGLTexture::Desc desc;
801 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
802 // with a texture rectangle,we assume the necessary support exists.
803 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800804 return nullptr;
805 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400806
807 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
808 return nullptr;
809 }
810
811 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400812 GrGLRenderTarget::IDs rtIDs;
813 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800814 return nullptr;
815 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400816 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount,
817 rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800818}
819
Brian Salomonc320b152018-02-20 14:05:36 -0500820static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700821 if (!glTex) {
822 return false;
823 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000824
bsalomone5286e02016-01-14 09:24:09 -0800825 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
826 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800827 return false;
828 }
829
jvanverth17aa0472016-01-05 10:41:27 -0800830 return true;
831}
832
Brian Salomona9b04b92018-06-01 15:04:28 -0400833bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400834 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400835 const GrMipLevel texels[], int mipLevelCount,
836 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500837 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800838
Brian Salomonc320b152018-02-20 14:05:36 -0500839 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800840 return false;
841 }
842
Brian Salomond978b902019-02-07 15:09:18 -0500843 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000844
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400845 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400846 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400847 glTex->target(), left, top, width, height, srcColorType, texels,
848 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000849}
850
Brian Salomone05ba5a2019-04-08 11:59:07 -0400851bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400852 GrColorType textureColorType, GrColorType bufferColorType,
853 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400855
Jim Van Verth1676cb92019-01-15 13:24:45 -0500856 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400857 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500858
Brian Salomonc320b152018-02-20 14:05:36 -0500859 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400860 return false;
861 }
862
Hal Canaryc36f7e72018-06-18 15:50:09 -0400863 static_assert(sizeof(int) == sizeof(int32_t), "");
864 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400865 return false;
866 }
867
Brian Salomond978b902019-02-07 15:09:18 -0500868 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400869
870 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500871 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400872 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500873 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400874
Greg Daniel660cc992017-06-26 14:55:05 -0400875 SkDEBUGCODE(
876 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
877 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
878 SkASSERT(bounds.contains(subRect));
879 )
880
Brian Salomonb28cb682019-07-26 12:48:47 -0400881 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400882 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400883 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700884 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400885 return false;
886 }
887
888 bool restoreGLRowLength = false;
889 if (trimRowBytes != rowBytes) {
890 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400891 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400892 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
893 restoreGLRowLength = true;
894 }
895
Greg Danielba88ab62019-07-26 09:14:01 -0400896 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400898 GrGLenum externalFormat = 0;
899 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400900 this->glCaps().getTexSubImageExternalFormatAndType(
901 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400902 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400903 return false;
904 }
905
Brian Salomon8cbf6622019-07-30 11:03:14 -0400906 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 GL_CALL(TexSubImage2D(glTex->target(),
908 0,
909 left, top,
910 width,
911 height,
912 externalFormat, externalType,
913 pixels));
914
915 if (restoreGLRowLength) {
916 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
917 }
918
919 return true;
920}
921
Brian Salomon26de56e2019-04-10 12:14:26 -0400922bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400923 GrColorType surfaceColorType, GrColorType dstColorType,
924 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400925 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
926 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400927 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400928 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
929 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400930}
931
Brian Osman9b560242017-09-05 15:34:52 -0400932void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500933 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
934 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
935 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
936 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400937 }
Brian Osman9b560242017-09-05 15:34:52 -0400938}
939
Brian Salomond2a8ae22019-09-10 16:03:59 -0400940bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
941 int texHeight, GrGLenum target, int left, int top, int width,
942 int height, GrColorType srcColorType, const GrMipLevel texels[],
943 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500944 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400945 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500946
Greg Daniel7bfc9132019-08-14 14:23:53 -0400947 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400948 SkDEBUGCODE(
949 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
950 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
951 SkASSERT(bounds.contains(subRect));
952 )
Robert Phillips590533f2017-07-11 14:22:35 -0400953 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400954 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800955
Brian Osman9b560242017-09-05 15:34:52 -0400956 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400957
cblume55f2d2d2016-02-26 13:20:48 -0800958 const GrGLInterface* interface = this->glInterface();
959 const GrGLCaps& caps = this->glCaps();
960
Brian Salomonf77c1462019-08-01 15:19:29 -0400961 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800962
963 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000964 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000965 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000966
bsalomon5b30c6f2015-12-17 14:17:34 -0800967 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800968 GrGLenum externalFormat;
969 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400970 this->glCaps().getTexSubImageExternalFormatAndType(
971 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400972 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800973 return false;
974 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400975
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000976 /*
bsalomon5b30c6f2015-12-17 14:17:34 -0800977 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +0000978 * because our srcData has extra bytes past each row. If so, we need
979 * to trim those off here, since GL ES may not let us specify
980 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000981 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000982 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -0800983
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400984 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600985 *mipMapsStatus = (mipLevelCount > 1) ?
986 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -0800987 }
bsalomone699d0c2016-03-09 06:25:15 -0800988
Brian Salomond2a8ae22019-09-10 16:03:59 -0400989 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -0800990
Brian Salomond2a8ae22019-09-10 16:03:59 -0400991 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
992 if (!texels[currentMipLevel].fPixels) {
993 if (mipMapsStatus) {
994 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400995 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400996 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +0000997 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400998 int twoToTheMipLevel = 1 << currentMipLevel;
999 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1000 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1001 const size_t trimRowBytes = currentWidth * bpp;
1002 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1003
1004 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1005 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1006 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1007 restoreGLRowLength = true;
1008 }
1009
1010 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1011 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001012 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001013 if (restoreGLRowLength) {
1014 SkASSERT(caps.writePixelsRowBytesSupport());
1015 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1016 }
1017 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001018}
1019
Greg Daniel7bfc9132019-08-14 14:23:53 -04001020bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
1021 SkImage::CompressionType compressionType,
1022 const SkISize& size,
1023 GrGLenum target,
1024 const void* data) {
1025 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001026 const GrGLCaps& caps = this->glCaps();
1027
1028 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001029 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001030 if (!internalFormat) {
1031 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001032 }
1033
Greg Daniel7bfc9132019-08-14 14:23:53 -04001034 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001035
1036 static constexpr int kMipLevelCount = 1;
1037
1038 // Make sure that the width and height that we pass to OpenGL
1039 // is a multiple of the block size.
Brian Salomonea4ad302019-08-07 13:04:55 -04001040 size_t dataSize = GrCompressedDataSize(compressionType, size.width(), size.height());
Brian Salomonbb8dde82019-06-27 10:52:13 -04001041
1042 if (useTexStorage) {
1043 // We never resize or change formats of textures.
Brian Salomonea4ad302019-08-07 13:04:55 -04001044 GL_ALLOC_CALL(
1045 this->glInterface(),
1046 TexStorage2D(target, kMipLevelCount, internalFormat, size.width(), size.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001047 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1048 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001049 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001050 }
1051 GL_CALL(CompressedTexSubImage2D(target,
1052 0, // level
1053 0, // left
1054 0, // top
Brian Salomonea4ad302019-08-07 13:04:55 -04001055 size.width(),
1056 size.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001057 internalFormat,
1058 SkToInt(dataSize),
1059 data));
1060 } else {
1061 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1062 0, // level
1063 internalFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001064 size.width(),
1065 size.height(),
Brian Salomonbb8dde82019-06-27 10:52:13 -04001066 0, // border
1067 SkToInt(dataSize),
1068 data));
1069
1070 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1071 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 return false;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001073 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001074 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001075 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001076}
1077
bsalomon424cc262015-05-22 10:37:30 -07001078static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001079 int sampleCount,
1080 GrGLenum format,
1081 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001082 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001083 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001084 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001085 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001086 GL_ALLOC_CALL(ctx.interface(),
1087 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1088 sampleCount,
1089 format,
1090 width, height));
1091 break;
1092 case GrGLCaps::kES_Apple_MSFBOType:
1093 GL_ALLOC_CALL(ctx.interface(),
1094 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1095 sampleCount,
1096 format,
1097 width, height));
1098 break;
1099 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1100 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1101 GL_ALLOC_CALL(ctx.interface(),
1102 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1103 sampleCount,
1104 format,
1105 width, height));
1106 break;
1107 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001108 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001109 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001110 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001111 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001112}
1113
Brian Salomonea4ad302019-08-07 13:04:55 -04001114bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001115 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001116 GrGLRenderTarget::IDs* rtIDs) {
1117 rtIDs->fMSColorRenderbufferID = 0;
1118 rtIDs->fRTFBOID = 0;
1119 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1120 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001121
bsalomona11e5fc2015-12-18 07:59:41 -08001122 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001123
Brian Salomonea4ad302019-08-07 13:04:55 -04001124 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001125 goto FAILED;
1126 }
1127
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001128 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001129 goto FAILED;
1130 }
1131
Brian Salomonea4ad302019-08-07 13:04:55 -04001132 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1133 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001134 goto FAILED;
1135 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001136
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001137 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1138 // the texture bound to the other. The exception is the IMG multisample extension. With this
1139 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1140 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001141 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001142 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1143 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1144 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001145 goto FAILED;
1146 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001147 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001148 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001149 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001150 }
1151
egdanield803f272015-03-18 13:01:52 -07001152 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001153 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001154 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001155 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001156 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001157 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001158 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159 goto FAILED;
1160 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001161 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001162 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001163 GR_GL_COLOR_ATTACHMENT0,
1164 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001165 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001168
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001169 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001170 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1171 GR_GL_COLOR_ATTACHMENT0,
1172 desc.fTarget,
1173 desc.fID,
1174 0,
1175 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001176 } else {
egdanield803f272015-03-18 13:01:52 -07001177 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001178 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001179 desc.fTarget,
1180 desc.fID,
1181 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001182 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183
1184 return true;
1185
1186FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001187 if (rtIDs->fMSColorRenderbufferID) {
1188 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1191 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 if (rtIDs->fTexFBOID) {
1194 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 }
1196 return false;
1197}
1198
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001199// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001200static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001201// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001202 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001203}
1204
Brian Salomone2826ab2019-06-04 15:58:31 -04001205static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001207 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1208 // drivers have a bug where an FBO won't be complete if it includes a
1209 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001210 GrGLTextureParameters::SamplerOverriddenState state;
1211 state.fMinFilter = GR_GL_NEAREST;
1212 state.fMagFilter = GR_GL_NEAREST;
1213 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1214 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001215 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1216 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1217 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1218 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001219 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001220}
1221
Robert Phillips67d52cf2017-06-05 13:38:13 -04001222sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001223 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001224 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001225 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001226 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001227 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001228 int mipLevelCount,
1229 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001230 // We don't support protected textures in GL.
1231 if (isProtected == GrProtected::kYes) {
1232 return nullptr;
1233 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001234 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001235
Brian Salomond2a8ae22019-09-10 16:03:59 -04001236 SkASSERT(mipLevelCount > 0);
1237 GrMipMapsStatus mipMapsStatus =
1238 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001239 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001240 GrGLTexture::Desc texDesc;
1241 texDesc.fSize = {desc.fWidth, desc.fHeight};
1242 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001243 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 texDesc.fConfig = desc.fConfig;
1245 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001246 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1247 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001248
Brian Salomond2a8ae22019-09-10 16:03:59 -04001249 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1250 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001251
1252 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001253 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001254 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001255
Robert Phillips67d52cf2017-06-05 13:38:13 -04001256 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001257 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001258 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001259 GL_CALL(BindTexture(texDesc.fTarget, 0));
1260 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001261
Brian Salomonea4ad302019-08-07 13:04:55 -04001262 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1263 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 return return_null_texture();
1265 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 tex = sk_make_sp<GrGLTextureRenderTarget>(
1267 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001268 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001270 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001272 // The non-sampler params are still at their default values.
1273 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1274 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001275 if (levelClearMask) {
1276 GrGLenum externalFormat, externalType;
1277 size_t bpp;
1278 this->glCaps().getTexSubImageZeroFormatTypeAndBpp(texDesc.fFormat, &externalFormat,
1279 &externalType, &bpp);
1280 if (this->glCaps().clearTextureSupport()) {
1281 for (int i = 0; i < mipLevelCount; ++i) {
1282 if (levelClearMask & (1U << i)) {
1283 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1284 nullptr));
1285 }
1286 }
1287 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1288 !this->glCaps().performColorClearsAsDraws()) {
1289 this->disableScissor();
1290 this->disableWindowRectangles();
1291 this->flushColorWrite(true);
1292 this->flushClearColor(0, 0, 0, 0);
1293 for (int i = 0; i < mipLevelCount; ++i) {
1294 if (levelClearMask & (1U << i)) {
1295 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1296 kDst_TempFBOTarget);
1297 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1298 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1299 }
1300 }
Brian Salomonc2249852019-09-16 11:08:50 -04001301 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001302 } else {
1303 std::unique_ptr<char[]> zeros;
1304 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1305 for (int i = 0; i < mipLevelCount; ++i) {
1306 if (levelClearMask & (1U << i)) {
1307 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1308 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1309 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1310 // smaller levels that need clearing.
1311 if (!zeros) {
1312 size_t size = levelWidth * levelHeight * bpp;
1313 zeros.reset(new char[size]());
1314 }
1315 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1316 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1317 externalFormat, externalType, zeros.get()));
1318 }
Brian Salomona3e29962019-07-16 11:52:08 -04001319 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001320 }
1321 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001322 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001323}
1324
Brian Salomonbb8dde82019-06-27 10:52:13 -04001325sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001326 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -04001327 SkImage::CompressionType compression,
1328 SkBudgeted budgeted, const void* data) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001329 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001330 GrGLTexture::Desc desc;
1331 desc.fSize = {width, height};
1332 desc.fTarget = GR_GL_TEXTURE_2D;
1333 desc.fConfig = GrCompressionTypePixelConfig(compression);
1334 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001335 desc.fFormat = format.asGLFormat();
1336 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat, compression, &initialState,
1337 data);
Brian Salomonea4ad302019-08-07 13:04:55 -04001338 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001339 return nullptr;
1340 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001341 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001342 // The non-sampler params are still at their default values.
1343 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1344 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001345 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001346}
1347
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001348namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001349
egdaniel8dc7c3a2015-04-16 11:22:42 -07001350const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001351
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001352void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001353 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001354
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001355 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001356 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357 (kUnknownBitCount == format->fTotalBits));
1358 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001359 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001360 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1361 (GrGLint*)&format->fStencilBits);
1362 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001363 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001364 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1365 (GrGLint*)&format->fTotalBits);
1366 format->fTotalBits += format->fStencilBits;
1367 } else {
1368 format->fTotalBits = format->fStencilBits;
1369 }
1370 }
1371}
1372}
1373
Brian Salomon5043f1f2019-07-11 21:27:54 -04001374int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001375 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001376 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1377
1378 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001379 // Default to unsupported, set this if we find a stencil format that works.
1380 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001381
Brian Salomond2a8ae22019-09-10 16:03:59 -04001382 GrGLuint colorID =
1383 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1384 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001385 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001386 }
egdanielff1d5472015-09-10 08:37:20 -07001387 // unbind the texture from the texture unit before binding it to the frame buffer
1388 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1389
1390 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001391 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001392 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001393 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001394 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001395 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1396 GR_GL_COLOR_ATTACHMENT0,
1397 GR_GL_TEXTURE_2D,
1398 colorID,
1399 0));
bsalomon30447372015-12-21 09:03:05 -08001400 GrGLuint sbRBID = 0;
1401 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001402
1403 // look over formats till I find a compatible one
1404 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001405 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001406 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001407 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1408 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1409 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1410 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1411 sFmt.fInternalFormat,
1412 kSize, kSize));
1413 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001414 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001415 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001416 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001417 if (sFmt.fPacked) {
1418 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1419 GR_GL_DEPTH_ATTACHMENT,
1420 GR_GL_RENDERBUFFER, sbRBID));
1421 } else {
1422 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1423 GR_GL_DEPTH_ATTACHMENT,
1424 GR_GL_RENDERBUFFER, 0));
1425 }
1426 GrGLenum status;
1427 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1428 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1429 firstWorkingStencilFormatIndex = i;
1430 break;
1431 }
egdanielff1d5472015-09-10 08:37:20 -07001432 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1433 GR_GL_STENCIL_ATTACHMENT,
1434 GR_GL_RENDERBUFFER, 0));
1435 if (sFmt.fPacked) {
1436 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1437 GR_GL_DEPTH_ATTACHMENT,
1438 GR_GL_RENDERBUFFER, 0));
1439 }
egdanielff1d5472015-09-10 08:37:20 -07001440 }
1441 }
bsalomon30447372015-12-21 09:03:05 -08001442 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001443 }
1444 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001445 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1446 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001447 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001448 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001449 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001450}
1451
Brian Salomonea4ad302019-08-07 13:04:55 -04001452GrGLuint GrGLGpu::createCompressedTexture2D(
1453 const SkISize& size,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001454 GrGLFormat format,
Brian Salomonea4ad302019-08-07 13:04:55 -04001455 SkImage::CompressionType compression,
1456 GrGLTextureParameters::SamplerOverriddenState* initialState,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001457 const void* data) {
1458 if (format == GrGLFormat::kUnknown) {
1459 return 0;
1460 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001461 GrGLuint id = 0;
1462 GL_CALL(GenTextures(1, &id));
1463 if (!id) {
1464 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001465 }
1466
Brian Salomonea4ad302019-08-07 13:04:55 -04001467 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001468
Brian Salomonea4ad302019-08-07 13:04:55 -04001469 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1470
Greg Daniel7bfc9132019-08-14 14:23:53 -04001471 if (!this->uploadCompressedTexData(format, compression, size, GR_GL_TEXTURE_2D, data)) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001472 GL_CALL(DeleteTextures(1, &id));
1473 return 0;
1474 }
1475 return id;
1476}
1477
1478GrGLuint GrGLGpu::createTexture2D(const SkISize& size,
1479 GrGLFormat format,
1480 GrRenderable renderable,
1481 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001482 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001483 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001484 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001485
Brian Salomonea4ad302019-08-07 13:04:55 -04001486 GrGLuint id = 0;
1487 GL_CALL(GenTextures(1, &id));
1488
1489 if (!id) {
1490 return 0;
1491 }
1492
1493 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001494
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001495 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001496 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001497 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001498 }
1499
Brian Salomond2a8ae22019-09-10 16:03:59 -04001500 if (initialState) {
1501 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1502 } else {
1503 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001504 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001505
1506 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1507
1508 bool success = false;
1509 if (internalFormat) {
1510 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1511 if (this->glCaps().formatSupportsTexStorage(format)) {
1512 GL_ALLOC_CALL(this->glInterface(),
1513 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
1514 size.width(), size.height()));
1515 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1516 } else {
1517 GrGLenum externalFormat, externalType;
1518 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1519 GrGLenum error = GR_GL_NO_ERROR;
1520 if (externalFormat && externalType) {
1521 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1522 const int twoToTheMipLevel = 1 << level;
1523 const int currentWidth = SkTMax(1, size.width() / twoToTheMipLevel);
1524 const int currentHeight = SkTMax(1, size.height() / twoToTheMipLevel);
1525 GL_ALLOC_CALL(
1526 this->glInterface(),
1527 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1528 currentHeight, 0, externalFormat, externalType, nullptr));
1529 error = CHECK_ALLOC_ERROR(this->glInterface());
1530 }
1531 success = (GR_GL_NO_ERROR == error);
1532 }
1533 }
1534 }
1535 if (success) {
1536 return id;
1537 }
1538 GL_CALL(DeleteTextures(1, &id));
1539 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001540}
1541
Chris Daltoneffee202019-07-01 22:28:03 -06001542GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1543 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001544 SkASSERT(width >= rt->width());
1545 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001546
egdaniel8dc7c3a2015-04-16 11:22:42 -07001547 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001548
Brian Salomond4764a12019-08-08 12:08:24 -04001549 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001550 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001551 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552 }
egdanielff1d5472015-09-10 08:37:20 -07001553
1554 if (!sbDesc.fRenderbufferID) {
1555 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1556 }
1557 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001558 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001559 }
1560 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1561 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1562 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1563 // we do this "if" so that we don't call the multisample
1564 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001565 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001566 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001567 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001568 sFmt.fInternalFormat,
1569 width, height));
1570 } else {
1571 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1572 sFmt.fInternalFormat,
1573 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001574 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001575 }
1576 fStats.incStencilAttachmentCreates();
1577 // After sized formats we attempt an unsized format and take
1578 // whatever sizes GL gives us. In that case we query for the size.
1579 GrGLStencilAttachment::Format format = sFmt;
1580 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001581 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1582 sbDesc,
1583 width,
1584 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001585 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001586 format);
1587 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001588}
1589
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001590////////////////////////////////////////////////////////////////////////////////
1591
Brian Salomondbf70722019-02-07 11:31:24 -05001592sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1593 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001594 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001595}
1596
Greg Danielacd66b42019-05-22 16:29:12 -04001597void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001598 GrSurfaceOrigin rtOrigin) {
1599 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001600 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001601 // if the scissor fully contains the viewport then we fall through and
1602 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001603 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001604 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001605 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001606 fHWScissorSettings.fRect = scissor;
1607 }
1608 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1609 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1610 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001611 }
1612 return;
1613 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001614 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001615
1616 // See fall through note above
1617 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001618}
1619
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001620void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001621 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001622#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001623 typedef GrWindowRectsState::Mode Mode;
1624 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1625 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001626
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001627 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001628 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001629 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001630 }
1631
csmartdalton7535f412016-08-23 06:51:00 -07001632 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1633 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001634 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1635 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001636
Chris Daltond6cda8d2019-09-05 02:30:04 -06001637 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001638 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001639 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001640 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001641 }
1642
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001643 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001644 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001645
Greg Danielacd66b42019-05-22 16:29:12 -04001646 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001647#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001648}
1649
1650void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001651#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001652 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001653 return;
1654 }
1655 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001656 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001657#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001658}
1659
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001660bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1661 GrSurfaceOrigin origin,
1662 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04001663 const GrPipeline& pipeline,
1664 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04001665 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
1666 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07001667 bool willDrawPoints) {
Chris Dalton4ece96d2019-08-30 11:26:39 -06001668 const GrTextureProxy* const* primProcProxies = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -04001669 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
Brian Salomonf7232642018-09-19 08:58:08 -04001670 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
Chris Dalton4ece96d2019-08-30 11:26:39 -06001671 primProcProxies = dynamicStateArrays->fPrimitiveProcessorTextures;
Brian Salomonf7232642018-09-19 08:58:08 -04001672 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
Chris Dalton4ece96d2019-08-30 11:26:39 -06001673 primProcProxies = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomonf7232642018-09-19 08:58:08 -04001674 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00001675 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001676
Chris Dalton4ece96d2019-08-30 11:26:39 -06001677 SkASSERT(SkToBool(primProcProxies) == SkToBool(primProc.numTextureSamplers()));
Greg Daniel9a51a862018-11-30 10:18:14 -05001678
Chris Dalton4ece96d2019-08-30 11:26:39 -06001679 sk_sp<GrGLProgram> program(fProgramCache->refProgram(
1680 this, renderTarget, origin, primProc, primProcProxies, pipeline, willDrawPoints));
Greg Daniel9a51a862018-11-30 10:18:14 -05001681 if (!program) {
1682 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1683 return false;
1684 }
brianosman33f6b3f2016-06-02 05:49:21 -07001685
Brian Salomon802cb312018-06-08 18:05:20 -04001686 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001687
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001688 // Swizzle the blend to match what the shader will output.
Chris Daltonbbb3f642019-07-24 12:25:08 -04001689 this->flushBlendAndColorWrite(
1690 pipeline.getXferProcessor().getBlendInfo(), pipeline.outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001691
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001692 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
1693 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08001694
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001695 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07001696 GrStencilSettings stencil;
1697 if (pipeline.isStencilEnabled()) {
1698 // TODO: attach stencil and create settings during render target flush.
1699 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1700 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1701 glRT->renderTargetPriv().numStencilBits());
1702 }
Chris Dalton71713f62019-04-18 12:41:03 -06001703 this->flushStencil(stencil, origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001704 if (pipeline.isScissorEnabled()) {
1705 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
1706 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Greg Danielacd66b42019-05-22 16:29:12 -04001707 this->flushScissor(state, glRT->width(), glRT->height(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001708 } else {
1709 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001710 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001711 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07001712 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001713
1714 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001715 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001716 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001717
1718 return true;
1719}
1720
Brian Salomon802cb312018-06-08 18:05:20 -04001721void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1722 if (!program) {
1723 fHWProgram.reset();
1724 fHWProgramID = 0;
1725 return;
1726 }
1727 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1728 if (program == fHWProgram) {
1729 return;
1730 }
1731 auto id = program->programID();
1732 SkASSERT(id);
1733 GL_CALL(UseProgram(id));
1734 fHWProgram = std::move(program);
1735 fHWProgramID = id;
1736}
1737
1738void GrGLGpu::flushProgram(GrGLuint id) {
1739 SkASSERT(id);
1740 if (fHWProgramID == id) {
1741 SkASSERT(!fHWProgram);
1742 return;
1743 }
1744 fHWProgram.reset();
1745 GL_CALL(UseProgram(id));
1746 fHWProgramID = id;
1747}
1748
1749void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001750 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001751 int baseVertex,
1752 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001753 int baseInstance,
1754 GrPrimitiveRestart enablePrimitiveRestart) {
1755 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001756
cdaltone2e71c22016-04-07 18:13:29 -07001757 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001758 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001759 SkASSERT(indexBuffer->isCpuBuffer() ||
1760 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001761 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001762 } else {
1763 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001764 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001765
Brian Salomon92be2f72018-06-19 14:33:47 -04001766 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1767 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001768
Brian Salomon802cb312018-06-08 18:05:20 -04001769 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001770 SkASSERT(vertexBuffer);
1771 SkASSERT(vertexBuffer->isCpuBuffer() ||
1772 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1773 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001774 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1775 const auto& attrib = fHWProgram->vertexAttribute(i);
1776 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001777 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1778 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001779 }
Chris Dalton1d616352017-05-31 12:51:23 -06001780 }
Brian Salomon802cb312018-06-08 18:05:20 -04001781 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001782 SkASSERT(instanceBuffer);
1783 SkASSERT(instanceBuffer->isCpuBuffer() ||
1784 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1785 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001786 int attribIdx = fHWProgram->numVertexAttributes();
1787 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1788 const auto& attrib = fHWProgram->instanceAttribute(i);
1789 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001790 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1791 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1792 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001793 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001794 }
1795}
1796
Brian Salomonae64c192019-02-05 09:41:37 -05001797GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001798 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001799
cdaltone2e71c22016-04-07 18:13:29 -07001800 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001801 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001802 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001803 }
cdaltone2e71c22016-04-07 18:13:29 -07001804
Brian Salomonae64c192019-02-05 09:41:37 -05001805 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001806 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001807 if (!bufferState->fBufferZeroKnownBound) {
1808 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1809 bufferState->fBufferZeroKnownBound = true;
1810 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001811 }
Brian Salomondbf70722019-02-07 11:31:24 -05001812 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1813 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001814 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1815 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1816 bufferState->fBufferZeroKnownBound = false;
1817 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001818 }
1819
Brian Salomonae64c192019-02-05 09:41:37 -05001820 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001821}
Brian Salomond818ebf2018-07-02 14:08:49 +00001822void GrGLGpu::disableScissor() {
1823 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1824 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1825 fHWScissorSettings.fEnabled = kNo_TriState;
1826 return;
1827 }
1828}
1829
Brian Osman9a9baae2018-11-05 15:06:26 -05001830void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001831 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001832 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001833 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001834 SkASSERT(!this->caps()->performColorClearsAsDraws());
1835 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001836
Brian Salomon43f8bf02017-10-18 08:33:29 -04001837 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001838
Brian Salomon43f8bf02017-10-18 08:33:29 -04001839 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1840
Brian Salomond818ebf2018-07-02 14:08:49 +00001841 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001842 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001843 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001844 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001845 }
Greg Danielacd66b42019-05-22 16:29:12 -04001846 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001847 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001848 this->flushColorWrite(true);
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00001849
1850 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
1851 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
1852 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
1853 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
1854 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
1855 a = (1 == a) ? safeAlpha1 : safeAlpha0;
1856 }
1857 this->flushClearColor(r, g, b, a);
1858
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001859 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001860}
1861
Robert Phillips95214472017-08-08 18:00:03 -04001862void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001863 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1864
Robert Phillips95214472017-08-08 18:00:03 -04001865 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001866 return;
1867 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04001868
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00001869 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
1870 // this should only be called internally when we know we have a
1871 // stencil buffer.
1872 SkASSERT(sb);
Robert Phillipscb2e2352017-08-30 16:44:40 -04001873
bsalomonb0bd4f62014-09-03 07:19:50 -07001874 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001875 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001876
Brian Salomond818ebf2018-07-02 14:08:49 +00001877 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07001878 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001879
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001880 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04001881 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001882 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001883 fHWStencilSettings.invalidate();
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00001884 if (!clearValue) {
1885 sb->cleared();
Robert Phillipscb2e2352017-08-30 16:44:40 -04001886 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001887}
1888
csmartdalton29df7602016-08-31 11:55:52 -07001889void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
1890 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001891 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07001892 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001893 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07001894 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001895
egdaniel8dc7c3a2015-04-16 11:22:42 -07001896 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001897 // this should only be called internally when we know we have a
1898 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08001899 SkASSERT(sb);
1900 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001901#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001902 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00001903 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001904#else
1905 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001906 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04001907 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001908 // changing the clip between stencil passes may or may not
1909 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00001910 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00001911#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001912 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07001913 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001914 value = (1 << (stencilBitCount - 1));
1915 } else {
1916 value = 0;
1917 }
bsalomonb0bd4f62014-09-03 07:19:50 -07001918 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05001919 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001920
Greg Danielacd66b42019-05-22 16:29:12 -04001921 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001922 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00001923
caryclark@google.comcf6285b2012-06-06 12:09:01 +00001924 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00001925 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001926 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00001927 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00001928}
1929
Brian Salomone05ba5a2019-04-08 11:59:07 -04001930bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04001931 GrColorType surfaceColorType, GrColorType dstColorType,
1932 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001933 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07001934
Brian Salomon1c53a9f2019-08-12 14:10:12 -04001935 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08001936 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04001937 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07001938 return false;
1939 }
Greg Danielba88ab62019-07-26 09:14:01 -04001940 GrGLenum externalFormat = 0;
1941 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04001942 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
1943 surfaceColorType,
1944 dstColorType,
1945 &externalFormat,
1946 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001947 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001948 return false;
1949 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001950
Brian Salomon71d9d842016-11-03 13:42:00 -04001951 if (renderTarget) {
1952 // resolve the render target if necessary
1953 switch (renderTarget->getResolveType()) {
1954 case GrGLRenderTarget::kCantResolve_ResolveType:
1955 return false;
1956 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001957 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04001958 break;
1959 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001960 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04001961 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07001962 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04001963 break;
1964 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001965 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04001966 }
Brian Salomon71d9d842016-11-03 13:42:00 -04001967 } else {
1968 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001969 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05001970 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00001971 }
1972
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001973 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06001974 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001975
Brian Salomona6948702018-06-01 15:33:20 -04001976 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04001977 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04001978 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04001979 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00001980 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04001981 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08001982
Brian Osman1348ed02018-09-12 09:08:39 -04001983 bool reattachStencil = false;
1984 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
1985 renderTarget &&
1986 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06001987 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04001988 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
1989 reattachStencil = true;
1990 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
1991 GR_GL_RENDERBUFFER, 0));
1992 }
1993
Chris Dalton76500e52019-09-05 02:13:05 -06001994 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04001995 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04001996
1997 if (reattachStencil) {
1998 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
1999 renderTarget->renderTargetPriv().getStencilAttachment());
2000 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2001 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2002 }
2003
Brian Salomon26de56e2019-04-10 12:14:26 -04002004 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002005 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002006 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2007 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002008
Brian Salomone05ba5a2019-04-08 11:59:07 -04002009 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002010 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002011 }
2012 return true;
2013}
2014
2015bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002016 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2017 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002018 SkASSERT(surface);
2019
Brian Salomonb28cb682019-07-26 12:48:47 -04002020 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002021
Brian Salomon26de56e2019-04-10 12:14:26 -04002022 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2023 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002024
Brian Salomon1047a492019-07-02 12:25:21 -04002025 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002026 rowPixelWidth = width;
2027 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002028 SkASSERT(!(rowBytes % bytesPerPixel));
2029 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002030 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002031 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2032 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002033}
2034
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002035GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002036 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002037 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002038 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
2039 const SkTArray<GrTextureProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002040 if (!fCachedOpsRenderPass) {
2041 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002042 }
2043
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002044 fCachedOpsRenderPass->set(rt, origin, colorInfo, stencilInfo);
2045 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002046}
2047
Brian Salomon1fabd512018-02-09 09:54:25 -05002048void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002049 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002050 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002051 this->didWriteToSurface(target, origin, &bounds);
2052}
bsalomon6ba6fa12015-03-04 11:57:37 -08002053
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002054void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2055 this->flushRenderTargetNoColorWrites(target);
2056 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002057}
2058
Brian Osman9aa30c62018-07-02 15:21:46 -04002059void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002060 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002061 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002062 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002063 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002064#ifdef SK_DEBUG
2065 // don't do this check in Chromium -- this is causing
2066 // lots of repeated command buffer flushes when the compositor is
2067 // rendering with Ganesh, which is really slow; even too slow for
2068 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002069 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002070 GrGLenum status;
2071 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2072 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2073 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2074 }
bsalomon160f24c2015-03-17 15:55:42 -07002075 }
egdanield803f272015-03-18 13:01:52 -07002076#endif
2077 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002078 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002079 }
2080
brianosman35b784d2016-05-05 11:52:53 -07002081 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002082 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002083 }
bsalomon083617b2016-02-12 12:10:14 -08002084}
bsalomona9909122016-01-23 10:41:40 -08002085
brianosman33f6b3f2016-06-02 05:49:21 -07002086void GrGLGpu::flushFramebufferSRGB(bool enable) {
2087 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2088 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2089 fHWSRGBFramebuffer = kYes_TriState;
2090 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2091 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2092 fHWSRGBFramebuffer = kNo_TriState;
2093 }
2094}
2095
Greg Danielacd66b42019-05-22 16:29:12 -04002096void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002097 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002098 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002099 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002100 fHWViewport = viewport;
2101 }
2102}
2103
bsalomon@google.comd302f142011-03-03 13:54:13 +00002104#define SWAP_PER_DRAW 0
2105
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002106#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002107 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002108 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002109 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002110 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002111 void SwapBuf() {
2112 DWORD procID = GetCurrentProcessId();
2113 HWND hwnd = GetTopWindow(GetDesktopWindow());
2114 while(hwnd) {
2115 DWORD wndProcID = 0;
2116 GetWindowThreadProcessId(hwnd, &wndProcID);
2117 if(wndProcID == procID) {
2118 SwapBuffers(GetDC(hwnd));
2119 }
2120 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2121 }
2122 }
2123 #endif
2124#endif
2125
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002126void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2127 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002128 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002129 const GrPipeline::FixedDynamicState* fixedDynamicState,
2130 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002131 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002132 int meshCount) {
2133 this->handleDirtyContext();
2134
bsalomon2eda5b32016-09-21 10:53:24 -07002135 bool hasPoints = false;
2136 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002137 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002138 hasPoints = true;
2139 break;
2140 }
2141 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002142 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2143 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002144 return;
2145 }
ethannicholas22793252016-01-30 09:59:10 -08002146
Brian Salomonf7232642018-09-19 08:58:08 -04002147 bool dynamicScissor = false;
2148 bool dynamicPrimProcTextures = false;
2149 if (dynamicStateArrays) {
2150 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2151 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2152 }
2153 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002154 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2155 *this->caps())) {
2156 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002157 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002158
Brian Salomon49348902018-06-26 09:12:38 -04002159 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002160 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002161 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Greg Danielacd66b42019-05-22 16:29:12 -04002162 glRT->width(), glRT->height(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002163 }
Brian Salomonf7232642018-09-19 08:58:08 -04002164 if (dynamicPrimProcTextures) {
2165 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2166 m * primProc.numTextureSamplers();
2167 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2168 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002169 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002170 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002171 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2172 GL_CALL(Enable(GR_GL_CULL_FACE));
2173 GL_CALL(Disable(GR_GL_CULL_FACE));
2174 }
Brian Salomonf7232642018-09-19 08:58:08 -04002175 meshes[m].sendToGpu(this);
2176 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002177 }
ethannicholas22793252016-01-30 09:59:10 -08002178
bsalomon@google.comd302f142011-03-03 13:54:13 +00002179#if SWAP_PER_DRAW
2180 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002181 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002182 aglSwapBuffers(aglGetCurrentContext());
2183 int set_a_break_pt_here = 9;
2184 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002185 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002186 SwapBuf();
2187 int set_a_break_pt_here = 9;
2188 SwapBuf();
2189 #endif
2190#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002191}
2192
Chris Dalton3809bab2017-06-13 10:55:06 -06002193static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2194 switch (primitiveType) {
2195 case GrPrimitiveType::kTriangles:
2196 return GR_GL_TRIANGLES;
2197 case GrPrimitiveType::kTriangleStrip:
2198 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002199 case GrPrimitiveType::kPoints:
2200 return GR_GL_POINTS;
2201 case GrPrimitiveType::kLines:
2202 return GR_GL_LINES;
2203 case GrPrimitiveType::kLineStrip:
2204 return GR_GL_LINE_STRIP;
2205 case GrPrimitiveType::kLinesAdjacency:
2206 return GR_GL_LINES_ADJACENCY;
2207 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002208 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002209}
2210
Brian Salomon802cb312018-06-08 18:05:20 -04002211void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2212 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002213 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002214 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002215 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002216 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2217 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002218 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002219 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2220 }
2221 fStats.incNumDraws();
2222}
2223
Brian Salomondbf70722019-02-07 11:31:24 -05002224static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2225 size_t baseOffset = baseIndex * sizeof(uint16_t);
2226 if (indexBuffer->isCpuBuffer()) {
2227 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2228 } else {
2229 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2230 }
2231}
2232
Brian Salomon802cb312018-06-08 18:05:20 -04002233void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002234 int indexCount, int baseIndex, uint16_t minIndexValue,
2235 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002236 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002237 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002238 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002239
Brian Salomon802cb312018-06-08 18:05:20 -04002240 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002241
2242 if (this->glCaps().drawRangeElementsSupport()) {
2243 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002244 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002245 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002246 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002247 }
2248 fStats.incNumDraws();
2249}
2250
Brian Salomon802cb312018-06-08 18:05:20 -04002251void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002252 int vertexCount, int baseVertex,
2253 const GrBuffer* instanceBuffer, int instanceCount,
2254 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002255 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002256 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002257 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002258 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2259 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002260 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2261 SkTMin(instanceCount - i, maxInstances)));
2262 fStats.incNumDraws();
2263 }
Chris Dalton1d616352017-05-31 12:51:23 -06002264}
2265
Brian Salomon802cb312018-06-08 18:05:20 -04002266void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002267 const GrBuffer* indexBuffer, int indexCount,
2268 int baseIndex, const GrBuffer* vertexBuffer,
2269 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002270 int instanceCount, int baseInstance,
2271 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002272 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002273 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002274 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2275 for (int i = 0; i < instanceCount; i += maxInstances) {
2276 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2277 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002278 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002279 SkTMin(instanceCount - i, maxInstances)));
2280 fStats.incNumDraws();
2281 }
Chris Dalton1d616352017-05-31 12:51:23 -06002282}
2283
Brian Salomon1fabd512018-02-09 09:54:25 -05002284void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002285 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002286 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002287 // Some extensions automatically resolves the texture when it is read.
2288 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002289 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002290 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002291 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2292 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2293
egdanield803f272015-03-18 13:01:52 -07002294 // make sure we go through flushRenderTarget() since we've modified
2295 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002296 fHWBoundRenderTargetUniqueID.makeInvalid();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002297 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002298 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2299 // surface. Choose kTopLeft so no adjustments are made
2300 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002301 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002302 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002303 GrScissorState scissorState;
2304 scissorState.set(dirtyRect);
Greg Danielacd66b42019-05-22 16:29:12 -04002305 this->flushScissor(scissorState, rt->width(), rt->height(), kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002306 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002307 GL_CALL(ResolveMultisampleFramebuffer());
2308 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002309 int l, b, r, t;
2310 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2311 this->glCaps().blitFramebufferSupportFlags()) {
2312 l = 0;
2313 b = 0;
2314 r = target->width();
2315 t = target->height();
2316 } else {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002317 auto rect = GrNativeRect::MakeRelativeTo(
Chris Dalton76500e52019-09-05 02:13:05 -06002318 kDirtyRectOrigin, rt->height(), dirtyRect);
2319 l = rect.fX;
2320 b = rect.fY;
2321 r = rect.fX + rect.fWidth;
2322 t = rect.fY + rect.fHeight;
Brian Salomone5e7eb12016-10-14 16:18:33 -04002323 }
derekf8c8f71a2014-09-16 06:24:57 -07002324
2325 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002326 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002327 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002328 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002329 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002330 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002331 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002332 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002333 }
2334}
2335
bsalomon@google.com411dad02012-06-05 20:24:20 +00002336namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002337
bsalomon@google.com411dad02012-06-05 20:24:20 +00002338
2339GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002340 static const GrGLenum gTable[kGrStencilOpCount] = {
2341 GR_GL_KEEP, // kKeep
2342 GR_GL_ZERO, // kZero
2343 GR_GL_REPLACE, // kReplace
2344 GR_GL_INVERT, // kInvert
2345 GR_GL_INCR_WRAP, // kIncWrap
2346 GR_GL_DECR_WRAP, // kDecWrap
2347 GR_GL_INCR, // kIncClamp
2348 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002349 };
cdalton93a379b2016-05-11 13:58:08 -07002350 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2351 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2352 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2353 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2354 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2355 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2356 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2357 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2358 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2359 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002360}
2361
2362void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002363 const GrStencilSettings::Face& face,
2364 GrGLenum glFace) {
2365 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2366 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2367 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002368
cdalton93a379b2016-05-11 13:58:08 -07002369 GrGLint ref = face.fRef;
2370 GrGLint mask = face.fTestMask;
2371 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002372
2373 if (GR_GL_FRONT_AND_BACK == glFace) {
2374 // we call the combined func just in case separate stencil is not
2375 // supported.
2376 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2377 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002378 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002379 } else {
2380 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2381 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002382 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002383 }
2384}
2385}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002386
Chris Dalton71713f62019-04-18 12:41:03 -06002387void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002388 if (stencilSettings.isDisabled()) {
2389 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002390 } else if (fHWStencilSettings != stencilSettings ||
2391 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002392 if (kYes_TriState != fHWStencilTestEnabled) {
2393 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002394
csmartdaltonc7d85332016-10-26 10:13:46 -07002395 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002396 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002397 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002398 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2399 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002400 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002401 set_gl_stencil(
2402 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002403 }
joshualitta58fe352014-10-27 08:39:00 -07002404 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002405 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002406 }
2407}
2408
csmartdaltonc7d85332016-10-26 10:13:46 -07002409void GrGLGpu::disableStencil() {
2410 if (kNo_TriState != fHWStencilTestEnabled) {
2411 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002412
csmartdaltonc7d85332016-10-26 10:13:46 -07002413 fHWStencilTestEnabled = kNo_TriState;
2414 fHWStencilSettings.invalidate();
2415 }
2416}
2417
Chris Dalton4c56b032019-03-04 12:28:42 -07002418void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002419 // rt is only optional if useHWAA is false.
2420 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002421#ifdef SK_DEBUG
2422 if (useHWAA && rt->numSamples() <= 1) {
2423 SkASSERT(this->caps()->mixedSamplesSupport());
2424 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2425 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2426 }
2427#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002428
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002429 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002430 if (useHWAA) {
2431 if (kYes_TriState != fMSAAEnabled) {
2432 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2433 fMSAAEnabled = kYes_TriState;
2434 }
2435 } else {
2436 if (kNo_TriState != fMSAAEnabled) {
2437 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2438 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002439 }
2440 }
2441 }
2442}
2443
Chris Daltonbbb3f642019-07-24 12:25:08 -04002444void GrGLGpu::flushBlendAndColorWrite(
2445 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2446 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2447 // We need to work around a driver bug by using a blend state that preserves the dst color,
2448 // rather than disabling color writes.
2449 GrXferProcessor::BlendInfo preserveDstBlend;
2450 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2451 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2452 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2453 return;
2454 }
bsalomonf7cc8772015-05-11 11:21:14 -07002455
cdalton8917d622015-05-06 13:40:21 -07002456 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002457 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2458 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002459
2460 // Any optimization to disable blending should have already been applied and
2461 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002462 bool blendOff =
2463 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2464 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2465 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002466
egdanielb414f252014-07-29 13:15:47 -07002467 if (blendOff) {
2468 if (kNo_TriState != fHWBlendState.fEnabled) {
2469 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002470
2471 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2472 // https://code.google.com/p/skia/issues/detail?id=3943
2473 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2474 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2475 SkASSERT(this->caps()->advancedBlendEquationSupport());
2476 // Set to any basic blending equation.
2477 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2478 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2479 fHWBlendState.fEquation = blend_equation;
2480 }
2481
egdanielb414f252014-07-29 13:15:47 -07002482 fHWBlendState.fEnabled = kNo_TriState;
2483 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002484 } else {
2485 if (kYes_TriState != fHWBlendState.fEnabled) {
2486 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002487
Chris Daltonbbb3f642019-07-24 12:25:08 -04002488 fHWBlendState.fEnabled = kYes_TriState;
2489 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002490
Chris Daltonbbb3f642019-07-24 12:25:08 -04002491 if (fHWBlendState.fEquation != equation) {
2492 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2493 fHWBlendState.fEquation = equation;
2494 }
cdalton8917d622015-05-06 13:40:21 -07002495
Chris Daltonbbb3f642019-07-24 12:25:08 -04002496 if (GrBlendEquationIsAdvanced(equation)) {
2497 SkASSERT(this->caps()->advancedBlendEquationSupport());
2498 // Advanced equations have no other blend state.
2499 return;
2500 }
cdalton8917d622015-05-06 13:40:21 -07002501
Chris Daltonbbb3f642019-07-24 12:25:08 -04002502 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2503 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2504 gXfermodeCoeff2Blend[dstCoeff]));
2505 fHWBlendState.fSrcCoeff = srcCoeff;
2506 fHWBlendState.fDstCoeff = dstCoeff;
2507 }
cdalton8917d622015-05-06 13:40:21 -07002508
Chris Daltonbbb3f642019-07-24 12:25:08 -04002509 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2510 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2511 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2512 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2513 fHWBlendState.fConstColor = blendConst;
2514 fHWBlendState.fConstColorValid = true;
2515 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002516 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002517 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002518
2519 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002520}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002521
Brian Salomondc829942018-10-23 16:07:24 -04002522static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002523 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002524 switch (swizzle[i]) {
2525 case 'r': glValues[i] = GR_GL_RED; break;
2526 case 'g': glValues[i] = GR_GL_GREEN; break;
2527 case 'b': glValues[i] = GR_GL_BLUE; break;
2528 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002529 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002530 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002531 default: SK_ABORT("Unsupported component");
2532 }
Brian Salomon327955b2018-10-22 15:39:22 +00002533 }
2534}
2535
Greg Daniel2c3398d2019-06-19 11:58:01 -04002536void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2537 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002538 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002539
reed856e9d92015-09-30 12:21:45 -07002540#ifdef SK_DEBUG
2541 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002542 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002543 const int w = texture->width();
2544 const int h = texture->height();
2545 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2546 }
2547 }
2548#endif
2549
bsalomon@google.comb8670992012-07-25 21:27:09 +00002550 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2551 // from the rt it will still be the last bound texture, but it needs resolving. So keep this
bsalomon@google.com4c883782012-06-04 19:05:11 +00002552 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002553 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002554 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002555 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002556 }
2557
Robert Phillips294870f2016-11-11 12:38:40 -05002558 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002559 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002560 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002561 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002562 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002563 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002564 }
2565
Brian Salomondc829942018-10-23 16:07:24 -04002566 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002567 if (!this->caps()->mipMapSupport() ||
2568 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002569 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002570 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002571 }
bsalomonefd7d452014-10-23 14:17:46 -07002572
brianosman33f6b3f2016-06-02 05:49:21 -07002573#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002574 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002575 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002576 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002577 }
2578#endif
2579
Brian Salomone2826ab2019-06-04 15:58:31 -04002580 auto timestamp = texture->parameters()->resetTimestamp();
2581 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002582
Brian Salomone2826ab2019-06-04 15:58:31 -04002583 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2584 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002585 if (fSamplerObjectCache) {
2586 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2587 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002588 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2589 texture->parameters()->samplerOverriddenState();
2590 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002591
Brian Salomone2826ab2019-06-04 15:58:31 -04002592 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2593 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002594
Brian Salomone2826ab2019-06-04 15:58:31 -04002595 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2596 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002597
2598 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002599 newSamplerState.fMinLOD = -1000.f;
2600 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002601
Brian Salomone2826ab2019-06-04 15:58:31 -04002602 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002603 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002604 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002605 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002606 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002607 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002608 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002609 }
Mike Klein1bccae52018-10-19 11:38:44 +00002610 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002611 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002612 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002613 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002614 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002615 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002616 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002617 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002618 }
2619 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002620 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002621 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002622 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002623 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002624 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002625 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002626 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002627 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002628 if (this->glCaps().clampToBorderSupport()) {
2629 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002631 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002632 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2633 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002634 }
2635 }
Brian Salomondc829942018-10-23 16:07:24 -04002636 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002637 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2638 newNonsamplerState.fBaseMipMapLevel = 0;
2639 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002640
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2642 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002643 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002644 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2645 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002646 GrGLenum glValues[4];
2647 get_gl_swizzle_values(swizzle, glValues);
2648 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002649 if (GR_IS_GR_GL(this->glStandard())) {
2650 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
2651 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2652 reinterpret_cast<const GrGLint*>(glValues)));
2653 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002654 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2655 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2656 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2657 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2658 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002659 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002660 }
2661 }
Brian Salomondc829942018-10-23 16:07:24 -04002662 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002663 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2664 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2665 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002666 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002667 this->setTextureUnit(unitIdx);
2668 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002669 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002670 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002671 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002672 this->setTextureUnit(unitIdx);
2673 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002674 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002675 }
Brian Salomon327955b2018-10-22 15:39:22 +00002676 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002677 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2678 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002679}
2680
Brian Salomon1f05d452019-02-08 12:33:08 -05002681void GrGLGpu::onResetTextureBindings() {
2682 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2683 GR_GL_TEXTURE_EXTERNAL};
2684 for (int i = 0; i < this->numTextureUnits(); ++i) {
2685 this->setTextureUnit(i);
2686 for (auto target : kTargets) {
2687 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2688 GL_CALL(BindTexture(target, 0));
2689 }
2690 }
2691 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2692 }
2693}
2694
egdaniel080e6732014-12-22 07:35:52 -08002695void GrGLGpu::flushColorWrite(bool writeColor) {
2696 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002697 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002698 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2699 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002700 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002701 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002702 } else {
2703 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002704 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002705 fHWWriteToColor = kYes_TriState;
2706 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002707 }
bsalomon3e791242014-12-17 13:43:13 -08002708}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002709
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002710void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
Brian Salomon805cc7a2019-01-28 09:52:34 -05002711 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2712 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2713 GL_CALL(ClearColor(r, g, b, a));
2714 fHWClearColor[0] = r;
2715 fHWClearColor[1] = g;
2716 fHWClearColor[2] = b;
2717 fHWClearColor[3] = a;
2718 }
2719}
2720
bsalomon861e1032014-12-16 07:33:49 -08002721void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002722 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002723 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002724 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002725 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002726 }
2727}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002728
Brian Salomond978b902019-02-07 15:09:18 -05002729void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002730 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002731 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002732 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2733 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2734 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002735 }
Brian Salomond978b902019-02-07 15:09:18 -05002736 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2737 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002738 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002739 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002740}
2741
Brian Salomone5e7eb12016-10-14 16:18:33 -04002742// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002743static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2744 const GrSurface* src,
2745 const SkIRect& srcRect,
2746 const SkIPoint& dstPoint,
2747 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002748 int dstSampleCnt = 0;
2749 int srcSampleCnt = 0;
2750 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002751 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002752 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002753 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002754 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002755 }
2756 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2757 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2758
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002759 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2760 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2761
Brian Salomone5e7eb12016-10-14 16:18:33 -04002762 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002763 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002764
Greg Daniel46cfbc62019-06-07 11:43:30 -04002765 GrTextureType dstTexType;
2766 GrTextureType* dstTexTypePtr = nullptr;
2767 GrTextureType srcTexType;
2768 GrTextureType* srcTexTypePtr = nullptr;
2769 if (dstTex) {
2770 dstTexType = dstTex->texturePriv().textureType();
2771 dstTexTypePtr = &dstTexType;
2772 }
2773 if (srcTex) {
2774 srcTexType = srcTex->texturePriv().textureType();
2775 srcTexTypePtr = &srcTexType;
2776 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002777
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002778 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2779 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002780 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002781}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002782
Brian Osmana9c8a052018-01-19 10:31:56 -05002783static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2784 // A RT has a separate MSAA renderbuffer if:
2785 // 1) It's multisampled
2786 // 2) We're using an extension with separate MSAA renderbuffers
2787 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002788 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002789}
2790
Greg Daniel46cfbc62019-06-07 11:43:30 -04002791static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002792 const GrGLCaps& caps) {
2793
bsalomon@google.comeb851172013-04-15 13:51:00 +00002794 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002795 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002796 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002797 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002798
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002799 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2800 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2801
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002802 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2803 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2804
Greg Daniel46cfbc62019-06-07 11:43:30 -04002805 GrTextureType dstTexType;
2806 GrTextureType* dstTexTypePtr = nullptr;
2807 GrTextureType srcTexType;
2808 GrTextureType* srcTexTypePtr = nullptr;
2809 if (dstTex) {
2810 dstTexType = dstTex->texturePriv().textureType();
2811 dstTexTypePtr = &dstTexType;
2812 }
2813 if (srcTex) {
2814 srcTexType = srcTex->texturePriv().textureType();
2815 srcTexTypePtr = &srcTexType;
2816 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002817
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002818 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2819 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002820}
2821
Greg Danielacd66b42019-05-22 16:29:12 -04002822// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002823void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002824 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002825 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002826 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002827 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002828 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2829 GrGLuint texID = texture->textureID();
2830 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002831 GrGLuint* tempFBOID;
2832 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002833
egdanield803f272015-03-18 13:01:52 -07002834 if (0 == *tempFBOID) {
2835 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002836 }
2837
Adrienne Walker4ee88512018-05-17 11:37:14 -07002838 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002839 GR_GL_CALL(
2840 this->glInterface(),
2841 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2842 if (mipLevel == 0) {
2843 texture->baseLevelWasBoundToFBO();
2844 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002845 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002846 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002847 }
egdaniel0f5f9672015-02-03 11:10:51 -08002848}
2849
Brian Salomond2a8ae22019-09-10 16:03:59 -04002850void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002851 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002852 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002853 SkASSERT(surface->asTexture());
2854 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2855 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2856 GR_GL_COLOR_ATTACHMENT0,
2857 textureTarget,
2858 0,
2859 0));
2860 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002861}
2862
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002863void GrGLGpu::onFBOChanged() {
2864 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2865 this->caps()->workarounds().restore_scissor_on_fbo_change) {
2866 GL_CALL(Flush());
2867 }
2868}
2869
Adrienne Walker4ee88512018-05-17 11:37:14 -07002870void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2871 fStats.incRenderTargetBinds();
2872 GL_CALL(BindFramebuffer(target, fboid));
2873 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2874 fBoundDrawFramebuffer = fboid;
2875 }
2876
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002877 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2878 // The driver forgets the correct scissor when modifying the FBO binding.
2879 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002880 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002881 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002882 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002883 }
2884
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002885 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002886}
2887
Adrienne Walker4ee88512018-05-17 11:37:14 -07002888void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
2889 if (fboid == fBoundDrawFramebuffer &&
2890 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2891 // This workaround only applies to deleting currently bound framebuffers
2892 // on Adreno 420. Because this is a somewhat rare case, instead of
2893 // tracking all the attachments of every framebuffer instead just always
2894 // unbind all attachments.
2895 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2896 GR_GL_RENDERBUFFER, 0));
2897 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2898 GR_GL_RENDERBUFFER, 0));
2899 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2900 GR_GL_RENDERBUFFER, 0));
2901 }
2902
2903 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002904
2905 // Deleting the currently bound framebuffer rebinds to 0.
2906 if (fboid == fBoundDrawFramebuffer) {
2907 this->onFBOChanged();
2908 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002909}
2910
Greg Daniel46cfbc62019-06-07 11:43:30 -04002911bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002912 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002913 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2914 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2915 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002916 auto dstFormat = dst->backendFormat().asGLFormat();
2917 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002918 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002919 return true;
2920 }
2921 }
cblume61214052016-01-26 09:10:48 -08002922
Greg Daniel46cfbc62019-06-07 11:43:30 -04002923 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2924 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002925 return true;
2926 }
2927
Greg Daniel46cfbc62019-06-07 11:43:30 -04002928 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2929 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002930 }
2931
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002932 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002933 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002934 return true;
2935 }
bsalomon083617b2016-02-12 12:10:14 -08002936 }
2937
bsalomon6df86402015-06-01 10:41:49 -07002938 return false;
2939}
2940
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002941bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002942 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002943
2944 int progIdx = TextureToCopyProgramIdx(srcTex);
2945 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2946 GrSLType samplerType =
2947 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2948
2949 if (!fCopyProgramArrayBuffer) {
2950 static const GrGLfloat vdata[] = {
2951 0, 0,
2952 0, 1,
2953 1, 0,
2954 1, 1
2955 };
2956 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2957 kStatic_GrAccessPattern, vdata);
2958 }
2959 if (!fCopyProgramArrayBuffer) {
2960 return false;
2961 }
2962
2963 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2964 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
2965 if (!fCopyPrograms[progIdx].fProgram) {
2966 return false;
2967 }
2968
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002969 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
2970 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
2971 GrShaderVar::kUniform_TypeModifier);
2972 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
2973 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
2974 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
2975 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
2976
Greg Daniel9e3169b2019-06-06 14:38:17 -04002977 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002978 if (shaderCaps->noperspectiveInterpolationSupport()) {
2979 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
2980 vshaderTxt.appendf("#extension %s : require\n", extension);
2981 }
2982 vTexCoord.addModifier("noperspective");
2983 }
2984
2985 aVertex.appendDecl(shaderCaps, &vshaderTxt);
2986 vshaderTxt.append(";");
2987 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
2988 vshaderTxt.append(";");
2989 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
2990 vshaderTxt.append(";");
2991 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
2992 vshaderTxt.append(";");
2993
2994 vshaderTxt.append(
2995 "// Copy Program VS\n"
2996 "void main() {"
2997 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
2998 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
2999 " sk_Position.zw = half2(0, 1);"
3000 "}"
3001 );
3002
Greg Daniel9e3169b2019-06-06 14:38:17 -04003003 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003004 if (shaderCaps->noperspectiveInterpolationSupport()) {
3005 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3006 fshaderTxt.appendf("#extension %s : require\n", extension);
3007 }
3008 }
3009 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3010 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3011 fshaderTxt.append(";");
3012 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3013 fshaderTxt.append(";");
3014 fshaderTxt.appendf(
3015 "// Copy Program FS\n"
3016 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003017 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003018 "}"
3019 );
3020
3021 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3022 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3023 SkSL::Program::Settings settings;
3024 settings.fCaps = shaderCaps;
3025 SkSL::String glsl;
3026 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3027 sksl, settings, &glsl, errorHandler);
3028 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3029 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3030 SkASSERT(program->fInputs.isEmpty());
3031
3032 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3033 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3034 errorHandler);
3035 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3036 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3037 errorHandler);
3038 SkASSERT(program->fInputs.isEmpty());
3039
3040 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3041
3042 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3043 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3044 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3045 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3046 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3047 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3048
3049 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3050
3051 GL_CALL(DeleteShader(vshader));
3052 GL_CALL(DeleteShader(fshader));
3053
3054 return true;
3055}
3056
brianosman33f6b3f2016-06-02 05:49:21 -07003057bool GrGLGpu::createMipmapProgram(int progIdx) {
3058 const bool oddWidth = SkToBool(progIdx & 0x2);
3059 const bool oddHeight = SkToBool(progIdx & 0x1);
3060 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3061
Brian Salomon1edc5b92016-11-29 13:43:46 -05003062 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003063
3064 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3065 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3066 if (!fMipmapPrograms[progIdx].fProgram) {
3067 return false;
3068 }
3069
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003070 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3071 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003072 GrShaderVar::kUniform_TypeModifier);
3073 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3074 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003075 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003076 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003077 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3078 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3079 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3080 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003081 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003082 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003083
Ethan Nicholasfc994162019-06-06 10:04:27 -04003084 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003085 if (shaderCaps->noperspectiveInterpolationSupport()) {
3086 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003087 vshaderTxt.appendf("#extension %s : require\n", extension);
3088 }
3089 vTexCoords[0].addModifier("noperspective");
3090 vTexCoords[1].addModifier("noperspective");
3091 vTexCoords[2].addModifier("noperspective");
3092 vTexCoords[3].addModifier("noperspective");
3093 }
3094
Brian Salomon1edc5b92016-11-29 13:43:46 -05003095 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003096 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003097 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003098 vshaderTxt.append(";");
3099 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003100 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003101 vshaderTxt.append(";");
3102 }
3103
3104 vshaderTxt.append(
3105 "// Mipmap Program VS\n"
3106 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003107 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3108 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003109 );
3110
3111 // Insert texture coordinate computation:
3112 if (oddWidth && oddHeight) {
3113 vshaderTxt.append(
3114 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003115 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3116 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003117 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3118 );
3119 } else if (oddWidth) {
3120 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003121 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3122 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003123 );
3124 } else if (oddHeight) {
3125 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003126 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3127 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003128 );
3129 } else {
3130 vshaderTxt.append(
3131 " v_texCoord0 = a_vertex.xy;"
3132 );
3133 }
3134
3135 vshaderTxt.append("}");
3136
Ethan Nicholasfc994162019-06-06 10:04:27 -04003137 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003138 if (shaderCaps->noperspectiveInterpolationSupport()) {
3139 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003140 fshaderTxt.appendf("#extension %s : require\n", extension);
3141 }
3142 }
brianosman33f6b3f2016-06-02 05:49:21 -07003143 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003144 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003145 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003146 fshaderTxt.append(";");
3147 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003148 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003149 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003150 fshaderTxt.append(
3151 "// Mipmap Program FS\n"
3152 "void main() {"
3153 );
3154
3155 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003156 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003157 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3158 " sample(u_texture, v_texCoord1) + "
3159 " sample(u_texture, v_texCoord2) + "
3160 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003161 );
3162 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003163 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003164 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3165 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003166 );
3167 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003168 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003169 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003170 );
3171 }
3172
3173 fshaderTxt.append("}");
3174
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003175 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003176 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003177 SkSL::Program::Settings settings;
3178 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003179 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003180 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003181 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003182 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003183 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003184 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003185
Brian Osman6c431d52019-04-15 16:31:54 -04003186 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003187 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3188 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003189 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003190 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003191 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003192 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003193
3194 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3195
3196 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3197 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3198 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3199 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3200
3201 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3202
3203 GL_CALL(DeleteShader(vshader));
3204 GL_CALL(DeleteShader(fshader));
3205
3206 return true;
3207}
3208
Greg Daniel46cfbc62019-06-07 11:43:30 -04003209bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003210 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003211 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3212 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3213 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3214 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003215 return false;
3216 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003217 int progIdx = TextureToCopyProgramIdx(srcTex);
3218 if (!dstRT) {
3219 SkASSERT(dstTex);
3220 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3221 return false;
3222 }
3223 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003224 if (!fCopyPrograms[progIdx].fProgram) {
3225 if (!this->createCopyProgram(srcTex)) {
3226 SkDebugf("Failed to create copy program.\n");
3227 return false;
3228 }
3229 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003230 int w = srcRect.width();
3231 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003232 // We don't swizzle at all in our copies.
3233 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003234 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003235 this->flushViewport(dst->width(), dst->height());
3236 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003237 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003238 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003239 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003240 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3241 attribs->enableVertexArrays(this, 1);
3242 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3243 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003244 // dst rect edges in NDC (-1 to 1)
3245 int dw = dst->width();
3246 int dh = dst->height();
3247 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3248 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3249 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3250 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003251 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3252 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3253 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3254 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3255 int sw = src->width();
3256 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003257 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3258 // src rect edges in normalized texture space (0 to 1)
3259 sx0 /= sw;
3260 sx1 /= sw;
3261 sy0 /= sh;
3262 sy1 /= sh;
3263 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003264 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3265 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3266 sx1 - sx0, sy1 - sy0, sx0, sy0));
3267 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003268 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003269 this->flushHWAAState(nullptr, false);
3270 this->disableScissor();
3271 this->disableWindowRectangles();
3272 this->disableStencil();
3273 if (this->glCaps().srgbWriteControl()) {
3274 this->flushFramebufferSRGB(true);
3275 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003277 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003278 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3279 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003280 return true;
3281}
3282
Greg Daniel46cfbc62019-06-07 11:43:30 -04003283void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003284 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003285 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003286 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003287 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003288 SkASSERT(dstTex);
3289 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003290 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003291
Brian Salomond978b902019-02-07 15:09:18 -05003292 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003293 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003294 dstPoint.fX, dstPoint.fY,
3295 srcRect.fLeft, srcRect.fTop,
3296 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003297 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003298 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3299 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003300 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3301 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003302}
3303
Greg Daniel46cfbc62019-06-07 11:43:30 -04003304bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003305 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003306 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003307 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3308 srcRect.width(), srcRect.height());
3309 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003310 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003311 return false;
mtklein404b3b22015-05-18 09:29:10 -07003312 }
bsalomon5df6fee2015-05-18 06:26:15 -07003313 }
bsalomon6df86402015-06-01 10:41:49 -07003314
Brian Salomond2a8ae22019-09-10 16:03:59 -04003315 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3316 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003317 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003318 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003319
3320 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003321 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003322 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003323
Greg Daniel46cfbc62019-06-07 11:43:30 -04003324 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3325 srcRect.fTop,
3326 srcRect.fRight,
3327 srcRect.fBottom,
3328 dstRect.fLeft,
3329 dstRect.fTop,
3330 dstRect.fRight,
3331 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003332 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003333 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3334 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003335
3336 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3337 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003338 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003339}
3340
Brian Salomon930f9392018-06-20 16:25:26 -04003341bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3342 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003343 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003344 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003345 return false;
3346 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003347 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003348 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3349 // Uses draw calls to do a series of downsample operations to successive mips.
3350
3351 // The manual approach requires the ability to limit which level we're sampling and that the
3352 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003353 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003354 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003355 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003356 GL_CALL(GenerateMipmap(glTex->target()));
3357 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003358 }
3359
brianosman33f6b3f2016-06-02 05:49:21 -07003360 int width = texture->width();
3361 int height = texture->height();
3362 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003363 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003364
3365 // Create (if necessary), then bind temporary FBO:
3366 if (0 == fTempDstFBOID) {
3367 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3368 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003369 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003370 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003371
3372 // Bind the texture, to get things configured for filtering.
3373 // We'll be changing our base level further below:
3374 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003375 // The mipmap program does not do any swizzling.
3376 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003377
3378 // Vertex data:
3379 if (!fMipmapProgramArrayBuffer) {
3380 static const GrGLfloat vdata[] = {
3381 0, 0,
3382 0, 1,
3383 1, 0,
3384 1, 1
3385 };
Brian Salomonae64c192019-02-05 09:41:37 -05003386 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003387 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003388 }
3389 if (!fMipmapProgramArrayBuffer) {
3390 return false;
3391 }
3392
3393 fHWVertexArrayState.setVertexArrayID(this, 0);
3394
3395 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003396 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003397 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003398 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003399
3400 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003401 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003402 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003403 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003404 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003405 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003406
3407 // Do all the blits:
3408 width = texture->width();
3409 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003410
brianosman33f6b3f2016-06-02 05:49:21 -07003411 for (GrGLint level = 1; level < levelCount; ++level) {
3412 // Get and bind the program for this particular downsample (filter shape can vary):
3413 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3414 if (!fMipmapPrograms[progIdx].fProgram) {
3415 if (!this->createMipmapProgram(progIdx)) {
3416 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003417 // Invalidate all params to cover base level change in a previous iteration.
3418 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003419 return false;
3420 }
3421 }
Brian Salomon802cb312018-06-08 18:05:20 -04003422 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003423
3424 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3425 const float invWidth = 1.0f / width;
3426 const float invHeight = 1.0f / height;
3427 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3428 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3429 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3430
3431 // Only sample from previous mip
3432 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3433
Brian Salomon930f9392018-06-20 16:25:26 -04003434 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3435 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003436
3437 width = SkTMax(1, width / 2);
3438 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003439 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003440
3441 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3442 }
3443
3444 // Unbind:
3445 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3446 GR_GL_TEXTURE_2D, 0, 0));
3447
Brian Salomon930f9392018-06-20 16:25:26 -04003448 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003449 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3450 // We drew the 2nd to last level into the last level.
3451 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3452 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003453
brianosman33f6b3f2016-06-02 05:49:21 -07003454 return true;
3455}
3456
Chris Daltond7291ba2019-03-07 14:17:03 -07003457void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003458 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3459 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003460
3461 int effectiveSampleCnt;
3462 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003463 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003464
3465 sampleLocations->reset(effectiveSampleCnt);
3466 for (int i = 0; i < effectiveSampleCnt; ++i) {
3467 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3468 }
3469}
3470
cdalton231c5fd2015-05-13 12:35:36 -07003471void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003472 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003473 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003474 case kTexture_GrXferBarrierType: {
3475 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003476 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003477 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3478 // The render target uses separate storage so no need for glTextureBarrier.
3479 // FIXME: The render target will resolve automatically when its texture is bound,
3480 // but we could resolve only the bounds that will be read if we do it here instead.
3481 return;
3482 }
cdalton9954bc32015-04-29 14:17:00 -07003483 SkASSERT(this->caps()->textureBarrierSupport());
3484 GL_CALL(TextureBarrier());
3485 return;
cdalton231c5fd2015-05-13 12:35:36 -07003486 }
cdalton8917d622015-05-06 13:40:21 -07003487 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003488 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003489 this->caps()->blendEquationSupport());
3490 GL_CALL(BlendBarrier());
3491 return;
bsalomoncb02b382015-08-12 11:14:50 -07003492 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003493 }
3494}
3495
Brian Salomon5043f1f2019-07-11 21:27:54 -04003496static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003497 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003498 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3499 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3500 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3501 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3502 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3503 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3504 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3505 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3506 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
3507 case GrGLFormat::kRGBA32F: return kRGBA_float_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003508 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003509 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003510 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3511 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3512 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3513 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003514
Brian Salomon5043f1f2019-07-11 21:27:54 -04003515 // Configs with multiple equivalent formats.
3516
Robert Phillipsebab03f2019-07-22 08:48:18 -04003517 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3518 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3519
Brian Salomon5043f1f2019-07-11 21:27:54 -04003520 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3521 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3522
3523 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3524 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003525 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003526 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003527}
3528
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003529GrBackendTexture GrGLGpu::createBackendTexture(int w, int h,
3530 const GrBackendFormat& format,
3531 GrMipMapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -04003532 GrRenderable renderable,
Robert Phillips28a5a432019-06-07 12:46:21 -04003533 const void* srcPixels, size_t rowBytes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04003534 const SkColor4f* color,
3535 GrProtected isProtected) {
Brian Salomon8a375832018-03-14 10:21:40 -04003536 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003537
Brian Salomond4764a12019-08-08 12:08:24 -04003538 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003539 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003540 return GrBackendTexture(); // invalid
3541 }
3542
Brian Salomon5043f1f2019-07-11 21:27:54 -04003543 GrPixelConfig config = gl_format_to_pixel_config(glFormat);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003544
Brian Salomon5043f1f2019-07-11 21:27:54 -04003545 if (config == kUnknown_GrPixelConfig) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003546 return GrBackendTexture(); // invalid
3547 }
3548
Greg Daniel7bfc9132019-08-14 14:23:53 -04003549 auto textureColorType = GrPixelConfigToColorType(config);
3550
3551 if (!this->caps()->isFormatTexturableAndUploadable(textureColorType, format)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003552 return GrBackendTexture(); // invalid
3553 }
3554
Brian Salomonbb8dde82019-06-27 10:52:13 -04003555 if (w < 1 || w > this->caps()->maxTextureSize() ||
3556 h < 1 || h > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003557 return GrBackendTexture(); // invalid
3558 }
3559
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003560 // Currently we don't support uploading pixel data when mipped.
Robert Phillips28a5a432019-06-07 12:46:21 -04003561 if (srcPixels && GrMipMapped::kYes == mipMapped) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003562 return GrBackendTexture(); // invalid
3563 }
3564
Brian Salomon4687bdd2019-05-09 16:28:04 -04003565 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillipse3bd6732019-05-29 14:20:35 -04003566 return GrBackendTexture(); // invalid
Brian Salomon4687bdd2019-05-09 16:28:04 -04003567 }
Robert Phillips646f6372018-09-25 09:31:10 -04003568
Greg Daniel15500642019-06-27 03:05:55 +00003569 GrGLTextureInfo info;
3570 GrGLTextureParameters::SamplerOverriddenState initialState;
3571
Brian Salomond2a8ae22019-09-10 16:03:59 -04003572 SkTDArray<GrMipLevel> texels;
Brian Salomonbb8dde82019-06-27 10:52:13 -04003573 SkAutoMalloc pixelStorage;
3574 SkImage::CompressionType compressionType;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003575 if (GrGLFormatToCompressionType(glFormat, &compressionType)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04003576 // Compressed textures currently must be non-MIP mapped and have initial data.
3577 if (mipMapped == GrMipMapped::kYes) {
3578 return GrBackendTexture();
3579 }
3580 if (!srcPixels) {
3581 if (!color) {
3582 return GrBackendTexture();
3583 }
3584 SkASSERT(0 == rowBytes);
3585 size_t size = GrCompressedDataSize(compressionType, w, h);
3586 srcPixels = pixelStorage.reset(size);
3587 GrFillInCompressedData(compressionType, w, h, (char*)srcPixels, *color);
3588 }
Brian Salomonea4ad302019-08-07 13:04:55 -04003589 info.fID = this->createCompressedTexture2D(
Greg Daniel7bfc9132019-08-14 14:23:53 -04003590 {w, h}, glFormat, compressionType, &initialState, srcPixels);
Brian Salomonea4ad302019-08-07 13:04:55 -04003591 if (!info.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04003592 return GrBackendTexture();
3593 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04003594 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -04003595 info.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonbb8dde82019-06-27 10:52:13 -04003596 } else {
3597 if (srcPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003598 texels.append(1);
3599 texels[0] = {srcPixels, rowBytes};
Brian Salomonbb8dde82019-06-27 10:52:13 -04003600 } else if (color) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003601 int mipLevelCount = 1;
Brian Salomonbb8dde82019-06-27 10:52:13 -04003602 if (GrMipMapped::kYes == mipMapped) {
3603 mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
3604 }
3605
Brian Salomond2a8ae22019-09-10 16:03:59 -04003606 texels.append(mipLevelCount);
Brian Salomonbb8dde82019-06-27 10:52:13 -04003607 SkTArray<size_t> individualMipOffsets(mipLevelCount);
3608
3609 size_t bytesPerPixel = GrBytesPerPixel(config);
3610
3611 size_t totalSize = GrComputeTightCombinedBufferSize(
3612 bytesPerPixel, w, h, &individualMipOffsets, mipLevelCount);
3613
3614 char* tmpPixels = (char*)pixelStorage.reset(totalSize);
3615
3616 GrFillInData(config, w, h, individualMipOffsets, tmpPixels, *color);
3617 for (int i = 0; i < mipLevelCount; ++i) {
3618 size_t offset = individualMipOffsets[i];
3619
3620 int twoToTheMipLevel = 1 << i;
3621 int currentWidth = SkTMax(1, w / twoToTheMipLevel);
3622
Brian Salomond2a8ae22019-09-10 16:03:59 -04003623 texels[i] = {&(tmpPixels[offset]), currentWidth * bytesPerPixel};
Brian Salomonbb8dde82019-06-27 10:52:13 -04003624 }
3625 }
3626 GrSurfaceDesc desc;
3627 desc.fWidth = w;
3628 desc.fHeight = h;
3629 desc.fConfig = config;
Brian Salomonea4ad302019-08-07 13:04:55 -04003630
3631 info.fTarget = GR_GL_TEXTURE_2D;
3632 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003633 info.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, glFormat, renderable,
3634 &initialState, SkTMax(1, texels.count()));
Brian Salomonea4ad302019-08-07 13:04:55 -04003635 if (!info.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04003636 return GrBackendTexture(); // invalid
3637 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003638 auto srcColorType = GrPixelConfigToColorType(desc.fConfig);
3639 if (!texels.empty() &&
3640 !this->uploadTexData(glFormat, textureColorType, desc.fWidth, desc.fHeight,
3641 GR_GL_TEXTURE_2D, 0, 0, desc.fWidth, desc.fHeight, srcColorType,
3642 texels.begin(), texels.count())) {
3643 GL_CALL(DeleteTextures(1, &info.fID));
3644 return GrBackendTexture();
3645 }
Robert Phillipse3bd6732019-05-29 14:20:35 -04003646 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003647
Robert Phillipse8fabb22018-02-04 14:33:21 -05003648 // unbind the texture from the texture unit to avoid asserts
3649 GL_CALL(BindTexture(info.fTarget, 0));
3650
Brian Salomone2826ab2019-06-04 15:58:31 -04003651 auto parameters = sk_make_sp<GrGLTextureParameters>();
3652 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3653 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003654
3655 return GrBackendTexture(w, h, mipMapped, info, std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003656}
3657
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003658void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003659 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3660
3661 GrGLTextureInfo info;
3662 if (tex.getGLTextureInfo(&info)) {
3663 GL_CALL(DeleteTextures(1, &info.fID));
3664 }
3665}
3666
3667#if GR_TEST_UTILS
3668
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003669bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003670 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003671
Greg Daniel52e16d92018-04-10 09:34:07 -04003672 GrGLTextureInfo info;
3673 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003674 return false;
3675 }
3676
3677 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003678 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003679
3680 return (GR_GL_TRUE == result);
3681}
3682
Brian Salomonf865b052018-03-09 09:01:53 -05003683GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003684 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003685 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3686 return GrBackendRenderTarget(); // invalid
3687 }
Brian Salomon8a375832018-03-14 10:21:40 -04003688 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003689 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003690 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003691 return {};
3692 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003693 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003694 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003695 if (sFormatIdx < 0) {
3696 return {};
3697 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003698 GrGLuint colorID = 0;
3699 GrGLuint stencilID = 0;
3700 auto deleteIDs = [&] {
3701 if (colorID) {
3702 if (useTexture) {
3703 GL_CALL(DeleteTextures(1, &colorID));
3704 } else {
3705 GL_CALL(DeleteRenderbuffers(1, &colorID));
3706 }
Brian Salomonf865b052018-03-09 09:01:53 -05003707 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003708 if (stencilID) {
3709 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003710 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003711 };
3712
3713 if (useTexture) {
3714 GL_CALL(GenTextures(1, &colorID));
3715 } else {
3716 GL_CALL(GenRenderbuffers(1, &colorID));
3717 }
3718 GL_CALL(GenRenderbuffers(1, &stencilID));
3719 if (!stencilID || !colorID) {
3720 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003721 return {};
3722 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003723
Brian Salomonf865b052018-03-09 09:01:53 -05003724 GrGLFramebufferInfo info;
3725 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003726 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003727 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3728 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003729 deleteIDs();
3730 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003731 }
3732
3733 this->invalidateBoundRenderTarget();
3734
Adrienne Walker4ee88512018-05-17 11:37:14 -07003735 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003736 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003737 GrGLTextureParameters::SamplerOverriddenState initialState;
3738 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3739 if (!colorID) {
3740 deleteIDs();
3741 return {};
3742 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003743 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3744 colorID, 0));
3745 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003746 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003747 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3748 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003749 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003750 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3751 GR_GL_RENDERBUFFER, colorID));
3752 }
3753 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003754 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3755 GL_ALLOC_CALL(this->glInterface(),
3756 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3757 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003758 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003759 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3760 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003761 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003762 }
3763
Brian Salomon93348dd2018-08-29 12:56:23 -04003764 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3765 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3766 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3767 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003768 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003769 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003770
Adrienne Walker4ee88512018-05-17 11:37:14 -07003771 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003772 GrGLenum status;
3773 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3774 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003775 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003776 return {};
3777 }
3778 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003779
Greg Daniel108bb232018-07-03 16:18:29 -04003780 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003781 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003782 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003783}
3784
3785void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003786 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003787 GrGLFramebufferInfo info;
3788 if (backendRT.getGLFramebufferInfo(&info)) {
3789 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003790 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003791 }
3792 }
joshualitt8fd844f2015-12-02 13:36:47 -08003793}
3794
Greg Daniel26b50a42018-03-08 09:49:58 -05003795void GrGLGpu::testingOnly_flushGpuAndSync() {
3796 GL_CALL(Finish());
3797}
Brian Salomonf865b052018-03-09 09:01:53 -05003798#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003799
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003800///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003801
cdaltone2e71c22016-04-07 18:13:29 -07003802GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003803 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003804 GrGLAttribArrayState* attribState;
3805
cdaltone2e71c22016-04-07 18:13:29 -07003806 if (gpu->glCaps().isCoreProfile()) {
3807 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003808 GrGLuint arrayID;
3809 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3810 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003811 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003812 }
cdaltone2e71c22016-04-07 18:13:29 -07003813 if (ibuf) {
3814 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003815 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003816 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003817 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003818 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003819 if (ibuf) {
3820 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003821 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003822 } else {
3823 this->setVertexArrayID(gpu, 0);
3824 }
3825 int attrCount = gpu->glCaps().maxVertexAttributes();
3826 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3827 fDefaultVertexArrayAttribState.resize(attrCount);
3828 }
3829 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003830 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003831 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003832}
bsalomone179a912016-01-20 06:18:10 -08003833
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04003834void GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04003835 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00003836 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003837 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003838 // We call finish if the client told us to sync or if we have a finished proc but don't support
3839 // GLsync objects.
3840 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
3841 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
3842 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05003843 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04003844 // After a finish everything previously sent to GL is done.
3845 for (const auto& cb : fFinishCallbacks) {
3846 cb.fCallback(cb.fContext);
3847 this->deleteSync(cb.fSync);
3848 }
3849 fFinishCallbacks.clear();
3850 if (info.fFinishedProc) {
3851 info.fFinishedProc(info.fFinishedContext);
3852 }
3853 } else {
3854 if (info.fFinishedProc) {
3855 FinishCallback callback;
3856 callback.fCallback = info.fFinishedProc;
3857 callback.fContext = info.fFinishedContext;
3858 callback.fSync = (GrGLsync)this->insertFence();
3859 fFinishCallbacks.push_back(callback);
3860 GL_CALL(Flush());
3861 } else if (insertedSemaphore) {
3862 // Must call flush after semaphores in case they are waited on another GL context.
3863 GL_CALL(Flush());
3864 }
3865 // See if any previously inserted finish procs are good to go.
3866 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003867 }
Greg Daniel51316782017-08-02 15:10:09 +00003868}
3869
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003870void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3871 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3872 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3873 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003874}
3875
Greg Daniel6be35232017-03-01 17:01:09 -05003876GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04003877 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05003878 GrGLsync sync;
3879 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3880 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
3881 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003882}
3883
Brian Salomonb0d8b762019-05-06 16:58:22 -04003884bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
3885 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07003886 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04003887 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3888 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3889}
3890
3891bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
3892 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07003893}
3894
3895void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05003896 this->deleteSync((GrGLsync)fence);
3897}
3898
Greg Daniela5cb7812017-06-16 09:45:32 -04003899sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003900 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003901 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003902}
3903
Greg Daniel48661b82018-01-22 16:11:35 -05003904sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
3905 GrResourceProvider::SemaphoreWrapType wrapType,
3906 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003907 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003908 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3909}
3910
Greg Daniel858e12c2018-12-06 11:11:37 -05003911void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05003912 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
3913
Greg Daniel48661b82018-01-22 16:11:35 -05003914 GrGLsync sync;
3915 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3916 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05003917}
3918
Greg Daniel48661b82018-01-22 16:11:35 -05003919void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05003920 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
3921
3922 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3923}
3924
Brian Salomonb0d8b762019-05-06 16:58:22 -04003925void GrGLGpu::checkFinishProcs() {
3926 // Bail after the first unfinished sync since we expect they signal in the order inserted.
3927 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
3928 /* timeout = */ 0, /* flush = */ false)) {
3929 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
3930 this->deleteSync(fFinishCallbacks.front().fSync);
3931 fFinishCallbacks.pop_front();
3932 }
3933}
3934
Greg Daniel6be35232017-03-01 17:01:09 -05003935void GrGLGpu::deleteSync(GrGLsync sync) const {
3936 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07003937}
Brian Osman13dddce2017-05-09 13:19:50 -04003938
Robert Phillips65a88fa2017-08-08 08:36:22 -04003939void GrGLGpu::insertEventMarker(const char* msg) {
3940 GL_CALL(InsertEventMarker(strlen(msg), msg));
3941}
3942
Brian Osman13dddce2017-05-09 13:19:50 -04003943sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
3944 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04003945 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05003946 this->insertSemaphore(semaphore);
3947 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
3948 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04003949
3950 return semaphore;
3951}
Robert Phillips646e4292017-06-13 12:44:56 -04003952
3953int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04003954 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04003955 case kTexture2DSampler_GrSLType:
3956 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04003957 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003958 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04003959 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05003960 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04003961 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04003962 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04003963 }
3964}
Brian Osman71a18892017-08-10 10:23:25 -04003965
Kevin Lubickf4def342018-10-04 12:52:50 -04003966#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05003967#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04003968void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
3969 // We are called by the base class, which has already called beginObject(). We choose to nest
3970 // all of our caps information in a named sub-object.
3971 writer->beginObject("GL GPU");
3972
3973 const GrGLubyte* str;
3974 GL_CALL_RET(str, GetString(GR_GL_VERSION));
3975 writer->appendString("GL_VERSION", (const char*)(str));
3976 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
3977 writer->appendString("GL_RENDERER", (const char*)(str));
3978 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
3979 writer->appendString("GL_VENDOR", (const char*)(str));
3980 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
3981 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
3982
3983 writer->appendName("extensions");
3984 glInterface()->fExtensions.dumpJSON(writer);
3985
3986 writer->endObject();
3987}
Kevin Lubickf4def342018-10-04 12:52:50 -04003988#endif