blob: 8821814246a66bb9c0bcd5516dd5bd71afa8a5e1 [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"
35#include "src/gpu/gl/GrGLGpuCommandBuffer.h"
36#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.");
198 return 0;
199}
200
201GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500202 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
203}
204
205bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
206 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500207}
208
209void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500210 int targetIndex = gl_target_to_binding_index(target);
211 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
212 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500213}
214
Brian Salomon1f05d452019-02-08 12:33:08 -0500215void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
216 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500217}
218
Brian Salomon1f05d452019-02-08 12:33:08 -0500219void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
220 for (auto& targetBinding : fTargetBindings) {
221 targetBinding.fBoundResourceID.makeInvalid();
222 if (markUnmodified) {
223 targetBinding.fHasBeenModified = false;
224 }
Brian Salomond978b902019-02-07 15:09:18 -0500225 }
226}
227
228//////////////////////////////////////////////////////////////////////////////
229
Brian Salomondc829942018-10-23 16:07:24 -0400230static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
231 switch (filter) {
232 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
233 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
234 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
235 }
236 SK_ABORT("Unknown filter");
237 return 0;
238}
239
240static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
241 switch (filter) {
242 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
243 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
244 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
245 }
246 SK_ABORT("Unknown filter");
247 return 0;
248}
249
Michael Ludwigf23a1522018-12-10 11:36:13 -0500250static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
251 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400252 switch (wrapMode) {
253 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
254 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
255 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500256 case GrSamplerState::WrapMode::kClampToBorder:
257 // May not be supported but should have been caught earlier
258 SkASSERT(caps.clampToBorderSupport());
259 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500260 }
Brian Salomondc829942018-10-23 16:07:24 -0400261 SK_ABORT("Unknown wrap mode");
262 return 0;
263}
264
265///////////////////////////////////////////////////////////////////////////////
266
267class GrGLGpu::SamplerObjectCache {
268public:
269 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
270 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
271 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
272 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
273 std::fill_n(fSamplers, kNumSamplers, 0);
274 }
275
276 ~SamplerObjectCache() {
277 if (!fNumTextureUnits) {
278 // We've already been abandoned.
279 return;
280 }
Chris Dalton703fe682019-05-15 12:58:12 -0600281 for (GrGLuint sampler : fSamplers) {
282 // The spec states that "zero" values should be silently ignored, however they still
283 // trigger GL errors on some NVIDIA platforms.
284 if (sampler) {
285 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
286 }
287 }
Brian Salomondc829942018-10-23 16:07:24 -0400288 }
289
290 void bindSampler(int unitIdx, const GrSamplerState& state) {
291 int index = StateToIndex(state);
292 if (!fSamplers[index]) {
293 GrGLuint s;
294 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
295 if (!s) {
296 return;
297 }
298 fSamplers[index] = s;
299 auto minFilter = filter_to_gl_min_filter(state.filter());
300 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500301 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
302 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400303 GR_GL_CALL(fGpu->glInterface(),
304 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
305 GR_GL_CALL(fGpu->glInterface(),
306 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
307 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
308 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
309 }
310 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
311 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
312 fHWBoundSamplers[unitIdx] = fSamplers[index];
313 }
314 }
315
316 void invalidateBindings() {
317 // When we have sampler support we always use samplers. So setting these to zero will cause
318 // a rebind on next usage.
319 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
320 }
321
322 void abandon() {
323 fHWBoundSamplers.reset();
324 fNumTextureUnits = 0;
325 }
326
327 void release() {
328 if (!fNumTextureUnits) {
329 // We've already been abandoned.
330 return;
331 }
332 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
333 std::fill_n(fSamplers, kNumSamplers, 0);
334 // Deleting a bound sampler implicitly binds sampler 0.
335 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
336 }
337
338private:
339 static int StateToIndex(const GrSamplerState& state) {
340 int filter = static_cast<int>(state.filter());
341 SkASSERT(filter >= 0 && filter < 3);
342 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500343 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400344 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500345 SkASSERT(wrapY >= 0 && wrapY < 4);
346 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400347 SkASSERT(idx < kNumSamplers);
348 return idx;
349 }
350
351 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500352 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400353 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
354 GrGLuint fSamplers[kNumSamplers];
355 int fNumTextureUnits;
356};
357
reed@google.comac10a2d2010-12-22 21:39:39 +0000358///////////////////////////////////////////////////////////////////////////////
359
Brian Salomon384fab42017-12-07 12:33:05 -0500360sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
361 GrContext* context) {
362 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500363 interface = GrGLMakeNativeInterface();
364 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
365 // to GrGLMakeNativeInterface.
366 if (!interface) {
367 interface = sk_ref_sp(GrGLCreateNativeInterface());
368 }
Brian Salomon384fab42017-12-07 12:33:05 -0500369 if (!interface) {
370 return nullptr;
371 }
bsalomon424cc262015-05-22 10:37:30 -0700372 }
Jim Van Verth76334772017-05-05 16:46:05 -0400373#ifdef USE_NSIGHT
374 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
375#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500376 auto glContext = GrGLContext::Make(std::move(interface), options);
377 if (!glContext) {
378 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700379 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500380 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700381}
382
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500383GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
384 : GrGpu(context)
385 , fGLContext(std::move(ctx))
386 , fProgramCache(new ProgramCache(this))
387 , fHWProgramID(0)
388 , fTempSrcFBOID(0)
389 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500390 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500391 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400392 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500393 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000394
Brian Salomond978b902019-02-07 15:09:18 -0500395 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000396
Brian Salomonae64c192019-02-05 09:41:37 -0500397 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
398 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700399 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500400 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
401 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
402 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
403 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700404 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500405 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
406 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700407 }
Brian Salomonae64c192019-02-05 09:41:37 -0500408 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400409 fHWBufferState[i].invalidate();
410 }
Brian Salomonae64c192019-02-05 09:41:37 -0500411 GR_STATIC_ASSERT(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700412
413 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
414 fPathRendering.reset(new GrGLPathRendering(this));
415 }
416
Brian Salomondc829942018-10-23 16:07:24 -0400417 if (this->glCaps().samplerObjectSupport()) {
418 fSamplerObjectCache.reset(new SamplerObjectCache(this));
419 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000420}
421
bsalomon861e1032014-12-16 07:33:49 -0800422GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700423 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
424 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800425 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700426 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700427 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800428
Brian Salomon802cb312018-06-08 18:05:20 -0400429 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400430 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000431 // detach the current program so there is no confusion on OpenGL's part
432 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000433 GL_CALL(UseProgram(0));
434 }
435
Brian Salomon43f8bf02017-10-18 08:33:29 -0400436 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700437 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800438 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400439 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700440 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800441 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400442 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700443 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800444 }
egdaniel0f5f9672015-02-03 11:10:51 -0800445
bsalomon7ea33f52015-11-22 14:51:00 -0800446 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
447 if (0 != fCopyPrograms[i].fProgram) {
448 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
449 }
bsalomon6df86402015-06-01 10:41:49 -0700450 }
bsalomon6dea83f2015-12-03 12:58:06 -0800451
brianosman33f6b3f2016-06-02 05:49:21 -0700452 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
453 if (0 != fMipmapPrograms[i].fProgram) {
454 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
455 }
456 }
457
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000458 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400459 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700460}
461
bsalomon6e2aad42016-04-01 11:54:31 -0700462void GrGLGpu::disconnect(DisconnectType type) {
463 INHERITED::disconnect(type);
464 if (DisconnectType::kCleanup == type) {
465 if (fHWProgramID) {
466 GL_CALL(UseProgram(0));
467 }
468 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700469 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700470 }
471 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700472 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700473 }
474 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700475 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700476 }
bsalomon6e2aad42016-04-01 11:54:31 -0700477 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
478 if (fCopyPrograms[i].fProgram) {
479 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
480 }
481 }
brianosman33f6b3f2016-06-02 05:49:21 -0700482 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
483 if (fMipmapPrograms[i].fProgram) {
484 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
485 }
486 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400487
Brian Salomondc829942018-10-23 16:07:24 -0400488 if (fSamplerObjectCache) {
489 fSamplerObjectCache->release();
490 }
bsalomon6e2aad42016-04-01 11:54:31 -0700491 } else {
492 if (fProgramCache) {
493 fProgramCache->abandon();
494 }
Brian Salomondc829942018-10-23 16:07:24 -0400495 if (fSamplerObjectCache) {
496 fSamplerObjectCache->abandon();
497 }
bsalomon6e2aad42016-04-01 11:54:31 -0700498 }
499
Brian Salomon802cb312018-06-08 18:05:20 -0400500 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700501 delete fProgramCache;
502 fProgramCache = nullptr;
503
bsalomonc8dc1f72014-08-21 13:02:13 -0700504 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700505 fTempSrcFBOID = 0;
506 fTempDstFBOID = 0;
507 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700508 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800509 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
510 fCopyPrograms[i].fProgram = 0;
511 }
brianosman33f6b3f2016-06-02 05:49:21 -0700512 fMipmapProgramArrayBuffer.reset();
513 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
514 fMipmapPrograms[i].fProgram = 0;
515 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400516
jvanverthe9c0fc62015-04-29 11:18:05 -0700517 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700518 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700519 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000520}
521
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000522///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000523
bsalomon861e1032014-12-16 07:33:49 -0800524void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000525 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400526 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000527 GL_CALL(Disable(GR_GL_DEPTH_TEST));
528 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000529
Brian Salomonf0861672017-05-08 11:10:10 -0400530 // We don't use face culling.
531 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600532 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
533 // just set this to the default for self-consistency.
534 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400535
Brian Salomonae64c192019-02-05 09:41:37 -0500536 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
537 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700538
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400539 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500540#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000541 // Desktop-only state that we never change
542 if (!this->glCaps().isCoreProfile()) {
543 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
544 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
545 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
546 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
547 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
548 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
549 }
550 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
551 // core profile. This seems like a bug since the core spec removes any mention of
552 // GL_ARB_imaging.
553 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
554 GL_CALL(Disable(GR_GL_COLOR_TABLE));
555 }
556 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400557
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400558 if (this->caps()->wireframeMode()) {
559 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
560 } else {
561 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
562 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500563#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000564 // Since ES doesn't support glPointSize at all we always use the VS to
565 // set the point size
566 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
567
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000568 }
joshualitt58162332014-08-01 06:44:53 -0700569
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400570 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500571 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700572 // The arm extension requires specifically enabling MSAA fetching per sample.
573 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500574 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700575 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000576 fHWWriteToColor = kUnknown_TriState;
577 // we only ever use lines in hairline mode
578 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700579 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500580
581 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000582 }
edisonn@google.comba669992013-06-28 16:03:21 +0000583
egdanielb414f252014-07-29 13:15:47 -0700584 if (resetBits & kMSAAEnable_GrGLBackendState) {
585 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700586
Chris Dalton6ce447a2019-06-23 18:07:38 -0600587 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800588 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
589 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700590 GL_CALL(CoverageModulation(GR_GL_RGBA));
591 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000594 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400595 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000596
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500598 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500599 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 }
Brian Salomondc829942018-10-23 16:07:24 -0400601 if (fSamplerObjectCache) {
602 fSamplerObjectCache->invalidateBindings();
603 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000604 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000605
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 if (resetBits & kBlend_GrGLBackendState) {
607 fHWBlendState.invalidate();
608 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kView_GrGLBackendState) {
611 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700612 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000613 fHWViewport.invalidate();
614 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000615
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000616 if (resetBits & kStencil_GrGLBackendState) {
617 fHWStencilSettings.invalidate();
618 fHWStencilTestEnabled = kUnknown_TriState;
619 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000620
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000621 // Vertex
622 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700623 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500624 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
625 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000626 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000628 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500629 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700630 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000631 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000632
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000633 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700634 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700635 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000637 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000638
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000639 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000640 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400641 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000642 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
643 }
Brian Salomon1047a492019-07-02 12:25:21 -0400644 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000645 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
646 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000647 if (this->glCaps().packFlipYSupport()) {
648 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
649 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000650 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000651
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000652 if (resetBits & kProgram_GrGLBackendState) {
653 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400654 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000655 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400656 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
Brian Salomond17f6582017-07-19 18:28:58 -0400659static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
660 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400661 GrGLTextureInfo info;
662 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400663 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800664 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000665
Greg Daniel52e16d92018-04-10 09:34:07 -0400666 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800667
Brian Salomond17f6582017-07-19 18:28:58 -0400668 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
669 if (!caps.shaderCaps()->externalTextureSupport()) {
670 return false;
671 }
672 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
673 if (!caps.rectangleTextureSupport()) {
674 return false;
675 }
676 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
677 return false;
678 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400679 if (backendTex.isProtected()) {
680 // Not supported in GL backend at this time.
681 return false;
682 }
Brian Salomond17f6582017-07-19 18:28:58 -0400683 return true;
684}
685
686sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsdd399802019-07-18 12:28:00 +0000687 GrWrapOwnership ownership, GrWrapCacheable cacheable,
688 GrIOType ioType) {
bsalomonb15b4c12014-10-29 12:41:57 -0700689 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400690 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
691 return nullptr;
692 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500693 if (!idDesc.fInfo.fFormat) {
694 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
695 }
Brian Salomond17f6582017-07-19 18:28:58 -0400696 if (kBorrow_GrWrapOwnership == ownership) {
697 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
698 } else {
699 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
700 }
bsalomone5286e02016-01-14 09:24:09 -0800701
Brian Salomond17f6582017-07-19 18:28:58 -0400702 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400703 surfDesc.fWidth = backendTex.width();
704 surfDesc.fHeight = backendTex.height();
Robert Phillipsdd399802019-07-18 12:28:00 +0000705 surfDesc.fConfig = backendTex.config();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400706
Greg Daniel177e6952017-10-12 12:27:11 -0400707 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
708 : GrMipMapsStatus::kNotAllocated;
709
Brian Salomone2826ab2019-06-04 15:58:31 -0400710 auto texture = GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc,
711 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();
714 return std::move(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 Salomond17f6582017-07-19 18:28:58 -0400722 GrGLTexture::IDDesc idDesc;
723 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800724 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800725 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500726 if (!idDesc.fInfo.fFormat) {
727 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
728 }
bsalomone5286e02016-01-14 09:24:09 -0800729
Brian Salomond17f6582017-07-19 18:28:58 -0400730 // We don't support rendering to a EXTERNAL texture.
731 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800732 return nullptr;
733 }
bsalomon10528f12015-10-14 12:54:52 -0700734
Brian Osman766fcbb2017-03-13 09:33:09 -0400735 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000736 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400737 } else {
738 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800739 }
bsalomonb15b4c12014-10-29 12:41:57 -0700740
Robert Phillips0902c982019-07-16 07:47:56 -0400741 const GrCaps* caps = this->caps();
742
Ben Wagner18b61f92016-10-25 10:44:02 -0400743 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000744 surfDesc.fWidth = backendTex.width();
745 surfDesc.fHeight = backendTex.height();
Robert Phillipsdd399802019-07-18 12:28:00 +0000746 surfDesc.fConfig = backendTex.config();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400747
748 sampleCnt =
749 caps->getRenderTargetSampleCount(sampleCnt, colorType, backendTex.getBackendFormat());
750 if (sampleCnt < 1) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500751 return nullptr;
752 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000753
Brian Salomond17f6582017-07-19 18:28:58 -0400754 GrGLRenderTarget::IDDesc rtIDDesc;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400755 if (!this->createRenderTargetObjects(surfDesc, sampleCnt, idDesc.fInfo, &rtIDDesc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400756 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000757 }
Greg Daniel177e6952017-10-12 12:27:11 -0400758
759 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
760 : GrMipMapsStatus::kNotAllocated;
761
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500762 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400763 this, surfDesc, sampleCnt, idDesc, backendTex.getGLTextureParams(), rtIDDesc, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400764 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400765 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400766 // We don't know what parameters are already set on wrapped textures.
767 texRT->textureParamsModified();
Brian Salomond17f6582017-07-19 18:28:58 -0400768 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000769}
770
Robert Phillipsdd399802019-07-18 12:28:00 +0000771sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400772 GrGLFramebufferInfo info;
773 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000774 return nullptr;
775 }
776
Brian Salomone8a766b2019-07-19 14:24:36 -0400777 if (backendRT.isProtected()) {
778 // Not supported in GL at this time.
779 return nullptr;
780 }
781
bsalomonb15b4c12014-10-29 12:41:57 -0700782 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400783 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700784 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700785 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500786 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000787
bsalomonb15b4c12014-10-29 12:41:57 -0700788 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000789 desc.fWidth = backendRT.width();
790 desc.fHeight = backendRT.height();
Robert Phillipsdd399802019-07-18 12:28:00 +0000791 desc.fConfig = backendRT.config();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400792 int sampleCount =
Robert Phillipsdd399802019-07-18 12:28:00 +0000793 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700794
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400795 return GrGLRenderTarget::MakeWrapped(this, desc, sampleCount, info.fFormat, idDesc,
796 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000797}
798
Greg Daniel7ef28f32017-04-20 16:41:55 +0000799sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsdd399802019-07-18 12:28:00 +0000800 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400801 GrGLTextureInfo info;
802 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800803 return nullptr;
804 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800805
Greg Daniel52e16d92018-04-10 09:34:07 -0400806 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
807 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800808 // Only texture rectangle and texture 2d are supported. We do not check whether texture
809 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
810 // we assume the necessary support exists.
811 return nullptr;
812 }
813
Ben Wagner18b61f92016-10-25 10:44:02 -0400814 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000815 surfDesc.fWidth = tex.width();
816 surfDesc.fHeight = tex.height();
Robert Phillipsdd399802019-07-18 12:28:00 +0000817 surfDesc.fConfig = tex.config();
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400818 int sampleCount = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800819
820 GrGLRenderTarget::IDDesc rtIDDesc;
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400821 if (!this->createRenderTargetObjects(surfDesc, sampleCount, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800822 return nullptr;
823 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400824 return GrGLRenderTarget::MakeWrapped(this, surfDesc, sampleCount, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800825}
826
Brian Salomonc320b152018-02-20 14:05:36 -0500827static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700828 if (!glTex) {
829 return false;
830 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000831
bsalomone5286e02016-01-14 09:24:09 -0800832 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
833 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800834 return false;
835 }
836
jvanverth17aa0472016-01-05 10:41:27 -0800837 return true;
838}
839
Brian Salomona9b04b92018-06-01 15:04:28 -0400840bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
841 GrColorType srcColorType, const GrMipLevel texels[],
842 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500843 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800844
Brian Salomonc320b152018-02-20 14:05:36 -0500845 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800846 return false;
847 }
848
Brian Salomond978b902019-02-07 15:09:18 -0500849 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000850
Brian Salomonc320b152018-02-20 14:05:36 -0500851 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
852 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
853 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
854 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
855 // caps knows to make the external format be GL_RGBA.
Greg Daniele877dce2019-07-11 10:52:43 -0400856 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500857
858 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400859 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
860 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
861 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000862}
863
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400864// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800865static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500866 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800867 switch (config) {
868 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500869 case kAlpha_8_as_Alpha_GrPixelConfig:
870 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500871 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500872 case kGray_8_as_Lum_GrPixelConfig:
873 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800874 return 1;
875 case kRGB_565_GrPixelConfig:
876 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500877 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800878 case kAlpha_half_GrPixelConfig:
Robert Phillipsebab03f2019-07-22 08:48:18 -0400879 case kAlpha_half_as_Lum_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500880 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800881 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400882 case kRGBA_half_Clamped_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -0400883 case kR_16_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800884 return 2;
885 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400886 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
Greg Danielf259b8b2019-02-14 09:03:43 -0500887 case kRGB_888X_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800888 case kBGRA_8888_GrPixelConfig:
889 case kSRGBA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500890 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800891 case kRGBA_float_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -0400892 case kRG_1616_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800893 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500894 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500895 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800896 return 0;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400897
Robert Phillips66a46032019-06-18 08:00:42 -0400898 // Experimental (for Y416 and mutant P016/P010)
899 case kRGBA_16161616_GrPixelConfig:
900 return 8;
901 case kRG_half_GrPixelConfig:
Robert Phillipsfe18de52019-06-06 17:21:50 -0400902 return 4;
bsalomonf46a1242015-12-15 12:37:38 -0800903 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400904 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500905 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800906}
907
Brian Salomone05ba5a2019-04-08 11:59:07 -0400908bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
909 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
910 size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400911 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
912 GrPixelConfig texConfig = glTex->config();
913 SkASSERT(this->caps()->isConfigTexturable(texConfig));
914
Jim Van Verth1676cb92019-01-15 13:24:45 -0500915 // Can't transfer compressed data
916 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
917
Brian Salomonc320b152018-02-20 14:05:36 -0500918 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400919 return false;
920 }
921
Hal Canaryc36f7e72018-06-18 15:50:09 -0400922 static_assert(sizeof(int) == sizeof(int32_t), "");
923 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 return false;
925 }
926
Brian Salomond978b902019-02-07 15:09:18 -0500927 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400928
929 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500930 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400931 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500932 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400933
Greg Daniel660cc992017-06-26 14:55:05 -0400934 SkDEBUGCODE(
935 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
936 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
937 SkASSERT(bounds.contains(subRect));
938 )
939
Brian Salomonc320b152018-02-20 14:05:36 -0500940 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400941 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400942 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700943 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400944 return false;
945 }
946
947 bool restoreGLRowLength = false;
948 if (trimRowBytes != rowBytes) {
949 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400950 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400951 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
952 restoreGLRowLength = true;
953 }
954
955 // Internal format comes from the texture desc.
956 GrGLenum internalFormat;
957 // External format and type come from the upload data.
958 GrGLenum externalFormat;
959 GrGLenum externalType;
Greg Daniele877dce2019-07-11 10:52:43 -0400960 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType);
Brian Salomonc320b152018-02-20 14:05:36 -0500961 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400962 &externalFormat, &externalType)) {
963 return false;
964 }
965
966 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
967 GL_CALL(TexSubImage2D(glTex->target(),
968 0,
969 left, top,
970 width,
971 height,
972 externalFormat, externalType,
973 pixels));
974
975 if (restoreGLRowLength) {
976 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
977 }
978
979 return true;
980}
981
Brian Salomon26de56e2019-04-10 12:14:26 -0400982bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
983 GrColorType dstColorType, GrGpuBuffer* transferBuffer,
984 size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400985 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
986 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400987 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomon26de56e2019-04-10 12:14:26 -0400988 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType,
989 offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400990}
991
cblume55f2d2d2016-02-26 13:20:48 -0800992/**
993 * Creates storage space for the texture and fills it with texels.
994 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400995 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800996 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800997 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500998 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700999 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
1000 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -08001001 * @param externalFormat The data format used for the external storage of the texture.
1002 * @param externalType The type of the data used for the external storage of the texture.
1003 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -05001004 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -08001005 * @param baseWidth The width of the texture's base mipmap level
1006 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -08001007 */
Robert Phillips92de6312017-05-23 07:43:48 -04001008static bool allocate_and_populate_texture(GrPixelConfig config,
1009 const GrGLInterface& interface,
1010 const GrGLCaps& caps,
1011 GrGLenum target,
1012 GrGLenum internalFormat,
1013 GrGLenum internalFormatForTexStorage,
1014 GrGLenum externalFormat,
1015 GrGLenum externalType,
Brian Salomon1047a492019-07-02 12:25:21 -04001016 const GrMipLevel texels[],
1017 int mipLevelCount,
1018 int baseWidth,
1019 int baseHeight,
Brian Salomonc38cfa92019-07-10 15:31:48 -04001020 bool* changedUnpackRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001021 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -08001022
Brian Salomon1047a492019-07-02 12:25:21 -04001023 if (caps.configSupportsTexStorage(config)) {
cblume790d5132016-02-29 11:13:29 -08001024 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001025 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -04001026 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -04001027 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001028 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001029 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001030 return false;
cblume790d5132016-02-29 11:13:29 -08001031 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04001032 size_t bpp = GrBytesPerPixel(config);
Robert Phillips590533f2017-07-11 14:22:35 -04001033 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001034 const void* currentMipData = texels[currentMipLevel].fPixels;
1035 if (currentMipData == nullptr) {
1036 continue;
1037 }
1038 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001039 const int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1040 const int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Brian Salomon1047a492019-07-02 12:25:21 -04001041
Brian Salomonc38cfa92019-07-10 15:31:48 -04001042 if (texels[currentMipLevel].fPixels) {
1043 const size_t trimRowBytes = currentWidth * bpp;
1044 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1045 if (rowBytes != trimRowBytes) {
1046 SkASSERT(caps.writePixelsRowBytesSupport());
1047 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1048 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1049 *changedUnpackRowLength = true;
1050 } else if (*changedUnpackRowLength) {
1051 SkASSERT(caps.writePixelsRowBytesSupport());
1052 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1053 *changedUnpackRowLength = false;
1054 }
Brian Salomon1047a492019-07-02 12:25:21 -04001055 }
cblume790d5132016-02-29 11:13:29 -08001056
1057 GR_GL_CALL(&interface,
1058 TexSubImage2D(target,
1059 currentMipLevel,
1060 0, // left
1061 0, // top
1062 currentWidth,
1063 currentHeight,
1064 externalFormat, externalType,
1065 currentMipData));
1066 }
bsalomone699d0c2016-03-09 06:25:15 -08001067 return true;
cblume790d5132016-02-29 11:13:29 -08001068 }
1069 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001070 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001071 GL_ALLOC_CALL(&interface,
1072 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001073 0,
cblume790d5132016-02-29 11:13:29 -08001074 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001075 baseWidth,
1076 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001077 0, // border
1078 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001079 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001080 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001081 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001082 return false;
1083 }
1084 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04001085 size_t bpp = GrBytesPerPixel(config);
Robert Phillips590533f2017-07-11 14:22:35 -04001086 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001087 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001088 const int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1089 const int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Brian Salomon1047a492019-07-02 12:25:21 -04001090
Brian Salomonc38cfa92019-07-10 15:31:48 -04001091 if (texels[currentMipLevel].fPixels) {
1092 const size_t trimRowBytes = currentWidth * bpp;
1093 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1094 if (rowBytes != trimRowBytes) {
1095 SkASSERT(caps.writePixelsRowBytesSupport());
1096 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1097 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1098 *changedUnpackRowLength = true;
1099 } else if (*changedUnpackRowLength) {
1100 SkASSERT(caps.writePixelsRowBytesSupport());
1101 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1102 *changedUnpackRowLength = false;
1103 }
Brian Salomon1047a492019-07-02 12:25:21 -04001104 }
1105
Greg Daniel8b059bd2017-09-28 20:46:45 +00001106 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001107 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1108 // This will allocate texture memory which we can later populate.
1109 GL_ALLOC_CALL(&interface,
1110 TexImage2D(target,
1111 currentMipLevel,
1112 internalFormat,
1113 currentWidth,
1114 currentHeight,
1115 0, // border
1116 externalFormat, externalType,
1117 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001118 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001119 if (error != GR_GL_NO_ERROR) {
1120 return false;
1121 }
cblume790d5132016-02-29 11:13:29 -08001122 }
cblume55f2d2d2016-02-26 13:20:48 -08001123 }
1124 }
bsalomone699d0c2016-03-09 06:25:15 -08001125 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001126}
1127
1128/**
cblume55f2d2d2016-02-26 13:20:48 -08001129 * After a texture is created, any state which was altered during its creation
1130 * needs to be restored.
1131 *
1132 * @param interface The GL interface to use.
1133 * @param caps The capabilities of the GL device.
1134 * @param restoreGLRowLength Should the row length unpacking be restored?
1135 * @param glFlipY Did GL flip the texture vertically?
1136 */
1137static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001138 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001139 if (restoreGLRowLength) {
Brian Salomon1047a492019-07-02 12:25:21 -04001140 SkASSERT(caps.writePixelsRowBytesSupport());
cblume55f2d2d2016-02-26 13:20:48 -08001141 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1142 }
cblume55f2d2d2016-02-26 13:20:48 -08001143}
1144
Brian Osman9b560242017-09-05 15:34:52 -04001145void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001146 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1147 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1148 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1149 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001150 }
Brian Osman9b560242017-09-05 15:34:52 -04001151}
1152
Brian Salomonc320b152018-02-20 14:05:36 -05001153// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1154// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001155bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1156 UploadType uploadType, int left, int top, int width, int height,
1157 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001158 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001159 // If we're uploading compressed data then we should be using uploadCompressedTexData
1160 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1161
Brian Salomond1eaf492017-05-18 10:02:08 -04001162 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001163 SkDEBUGCODE(
1164 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1165 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1166 SkASSERT(bounds.contains(subRect));
1167 )
Robert Phillips590533f2017-07-11 14:22:35 -04001168 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001169 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001170
Brian Osman9b560242017-09-05 15:34:52 -04001171 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001172
cblume55f2d2d2016-02-26 13:20:48 -08001173 const GrGLInterface* interface = this->glInterface();
1174 const GrGLCaps& caps = this->glCaps();
1175
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001176 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001177
1178 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001179 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001180 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001181
bsalomon5b30c6f2015-12-17 14:17:34 -08001182 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001183 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001184 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001185 GrGLenum externalFormat;
1186 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001187 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1188 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001189 return false;
1190 }
brianosman81a84852016-09-12 09:05:14 -07001191 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001192 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001193
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001194 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001195 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001196 * because our srcData has extra bytes past each row. If so, we need
1197 * to trim those off here, since GL ES may not let us specify
1198 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001199 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001200 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001201
1202 // in case we need a temporary, trimmed copy of the src pixels
1203 SkAutoSMalloc<128 * 128> tempStorage;
1204
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001205 if (mipMapsStatus) {
1206 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001207 }
1208
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001209 if (mipMapsStatus && mipLevelCount <= 1) {
1210 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001211 }
bsalomone699d0c2016-03-09 06:25:15 -08001212
Robert Phillips590533f2017-07-11 14:22:35 -04001213 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001214 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001215 }
cblume55f2d2d2016-02-26 13:20:48 -08001216
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001217 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001218 if (kNewTexture_UploadType == uploadType) {
1219 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001220 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001221 texConfig, *interface, caps, target, internalFormat,
Brian Salomon1047a492019-07-02 12:25:21 -04001222 internalFormatForTexStorage, externalFormat, externalType, texels,
1223 mipLevelCount, width, height, &restoreGLRowLength);
Brian Salomond1eaf492017-05-18 10:02:08 -04001224 } else {
1225 succeeded = false;
1226 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001227 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001228 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Brian Salomon1047a492019-07-02 12:25:21 -04001229 if (!texels[currentMipLevel].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001230 continue;
1231 }
cblume55f2d2d2016-02-26 13:20:48 -08001232 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001233 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1234 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1235 const size_t trimRowBytes = currentWidth * bpp;
1236 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1237
1238 if (caps.writePixelsRowBytesSupport() && rowBytes != trimRowBytes) {
1239 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1240 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1241 restoreGLRowLength = true;
1242 }
cblume55f2d2d2016-02-26 13:20:48 -08001243
1244 GL_CALL(TexSubImage2D(target,
1245 currentMipLevel,
1246 left, top,
1247 currentWidth,
1248 currentHeight,
1249 externalFormat, externalType,
Brian Salomon1047a492019-07-02 12:25:21 -04001250 texels[currentMipLevel].fPixels));
cblume55f2d2d2016-02-26 13:20:48 -08001251 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001252 }
1253
Brian Salomona9b04b92018-06-01 15:04:28 -04001254 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001255
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001256 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001257}
1258
Brian Salomonbb8dde82019-06-27 10:52:13 -04001259GrGLenum GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType, int width,
1260 int height, GrGLenum target, const void* data) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001261 const GrGLCaps& caps = this->glCaps();
1262
Brian Salomonbb8dde82019-06-27 10:52:13 -04001263 GrPixelConfig config = GrCompressionTypePixelConfig(compressionType);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001264 // We only need the internal format for compressed 2D textures.
1265 GrGLenum internalFormat;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001266 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
1267 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001268 }
1269
Brian Salomon1047a492019-07-02 12:25:21 -04001270 bool useTexStorage = caps.configSupportsTexStorage(config);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001271
1272 static constexpr int kMipLevelCount = 1;
1273
1274 // Make sure that the width and height that we pass to OpenGL
1275 // is a multiple of the block size.
1276 size_t dataSize = GrCompressedDataSize(compressionType, width, height);
1277
1278 if (useTexStorage) {
1279 // We never resize or change formats of textures.
1280 GL_ALLOC_CALL(this->glInterface(),
1281 TexStorage2D(target, kMipLevelCount, internalFormat, width, height));
1282 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1283 if (error != GR_GL_NO_ERROR) {
1284 return 0;
1285 }
1286 GL_CALL(CompressedTexSubImage2D(target,
1287 0, // level
1288 0, // left
1289 0, // top
1290 width,
1291 height,
1292 internalFormat,
1293 SkToInt(dataSize),
1294 data));
1295 } else {
1296 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1297 0, // level
1298 internalFormat,
1299 width,
1300 height,
1301 0, // border
1302 SkToInt(dataSize),
1303 data));
1304
1305 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1306 if (error != GR_GL_NO_ERROR) {
1307 return 0;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001308 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001309 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001310 return internalFormat;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001311}
1312
bsalomon424cc262015-05-22 10:37:30 -07001313static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001314 int sampleCount,
1315 GrGLenum format,
1316 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001317 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001318 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001319 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001320 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001321 GL_ALLOC_CALL(ctx.interface(),
1322 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1323 sampleCount,
1324 format,
1325 width, height));
1326 break;
1327 case GrGLCaps::kES_Apple_MSFBOType:
1328 GL_ALLOC_CALL(ctx.interface(),
1329 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1330 sampleCount,
1331 format,
1332 width, height));
1333 break;
1334 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1335 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1336 GL_ALLOC_CALL(ctx.interface(),
1337 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1338 sampleCount,
1339 format,
1340 width, height));
1341 break;
1342 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001343 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001344 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001345 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001346 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001347}
1348
egdanielb0e1be22015-04-22 13:27:39 -07001349bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001350 int sampleCount,
bsalomon091f60c2015-11-10 11:54:56 -08001351 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001352 GrGLRenderTarget::IDDesc* idDesc) {
1353 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001354 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001355 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001356 idDesc->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357
1358 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001359
bsalomona11e5fc2015-12-18 07:59:41 -08001360 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001361
Greg Daniel9bb268b2019-07-17 10:40:13 -04001362 GrGLFormat format = GrGLFormatFromGLEnum(texInfo.fFormat);
1363 if (format == GrGLFormat::kUnknown) {
1364 goto FAILED;
1365 }
1366
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001367 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001368 goto FAILED;
1369 }
1370
egdanield803f272015-03-18 13:01:52 -07001371 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1372 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001373 goto FAILED;
1374 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001375
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001376 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1377 // the texture bound to the other. The exception is the IMG multisample extension. With this
1378 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1379 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001380 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001381 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001382 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001383 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001384 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001385 goto FAILED;
1386 }
Greg Daniel9bb268b2019-07-17 10:40:13 -04001387 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001388 } else {
egdanield803f272015-03-18 13:01:52 -07001389 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001390 }
1391
egdanield803f272015-03-18 13:01:52 -07001392 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001393 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001394 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001395 SkASSERT(sampleCount > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001396 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001397 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001398 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399 goto FAILED;
1400 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001401 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001402 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001403 GR_GL_COLOR_ATTACHMENT0,
1404 GR_GL_RENDERBUFFER,
1405 idDesc->fMSColorRenderbufferID));
Greg Daniel9bb268b2019-07-17 10:40:13 -04001406 if (!this->glCaps().isFormatVerifiedColorAttachment(format)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001407 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1408 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1409 goto FAILED;
1410 }
Greg Daniel9bb268b2019-07-17 10:40:13 -04001411 fGLContext->caps()->markFormatAsValidColorAttachment(format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001412 }
1413 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001414 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001415
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001416 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
1417 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
1418 texInfo.fTarget, texInfo.fID, 0, sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001419 } else {
egdanield803f272015-03-18 13:01:52 -07001420 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001421 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001422 texInfo.fTarget,
1423 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001424 }
Greg Daniel9bb268b2019-07-17 10:40:13 -04001425 if (!this->glCaps().isFormatVerifiedColorAttachment(format)) {
egdanield803f272015-03-18 13:01:52 -07001426 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001427 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1428 goto FAILED;
1429 }
Greg Daniel9bb268b2019-07-17 10:40:13 -04001430 fGLContext->caps()->markFormatAsValidColorAttachment(format);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001431 }
1432
1433 return true;
1434
1435FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001436 if (idDesc->fMSColorRenderbufferID) {
1437 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001438 }
egdanield803f272015-03-18 13:01:52 -07001439 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001440 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001441 }
egdanield803f272015-03-18 13:01:52 -07001442 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001443 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001444 }
1445 return false;
1446}
1447
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001448// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001449static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001450// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001451 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001452}
1453
Brian Salomone2826ab2019-06-04 15:58:31 -04001454static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
1455 const GrGLInterface* interface, const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001456 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1457 // drivers have a bug where an FBO won't be complete if it includes a
1458 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001459 GrGLTextureParameters::SamplerOverriddenState state;
1460 state.fMinFilter = GR_GL_NEAREST;
1461 state.fMagFilter = GR_GL_NEAREST;
1462 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1463 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
1464 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1465 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1466 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1467 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
1468 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001469}
1470
Robert Phillips67d52cf2017-06-05 13:38:13 -04001471sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001472 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001473 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001474 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001475 GrProtected isProtected,
Robert Phillips590533f2017-07-11 14:22:35 -04001476 const GrMipLevel texels[],
1477 int mipLevelCount) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001478 // We don't support protected textures in GL.
1479 if (isProtected == GrProtected::kYes) {
1480 return nullptr;
1481 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001482 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001483
bsalomonb15b4c12014-10-29 12:41:57 -07001484 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001485 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001486 GrMipMapsStatus mipMapsStatus;
Brian Salomone2826ab2019-06-04 15:58:31 -04001487 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001488 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderable, &initialState, texels,
1489 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001490 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001491 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001492
Robert Phillips67d52cf2017-06-05 13:38:13 -04001493 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001494 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001495 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001496 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001497 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001498
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001499 if (!this->createRenderTargetObjects(desc, renderTargetSampleCnt, idDesc.fInfo,
1500 &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001501 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001502 return return_null_texture();
1503 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001504 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, renderTargetSampleCnt,
1505 idDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001506 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001508 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001510 // The non-sampler params are still at their default values.
1511 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1512 fResetTimestampForTextureParameters);
Brian Salomona3e29962019-07-16 11:52:08 -04001513 bool clearLevelsWithoutData =
1514 this->caps()->shouldInitializeTextures() && this->glCaps().clearTextureSupport();
1515
1516 if (clearLevelsWithoutData) {
1517 static constexpr uint32_t kZero = 0;
1518 int levelCnt = SkTMax(1, tex->texturePriv().maxMipMapLevel());
1519 for (int i = 0; i < levelCnt; ++i) {
1520 if (i >= mipLevelCount || !texels[i].fPixels) {
1521 GL_CALL(ClearTexImage(tex->textureID(), i, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
1522 &kZero));
1523 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001524 }
1525 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001526 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001527}
1528
Brian Salomonbb8dde82019-06-27 10:52:13 -04001529sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
1530 SkImage::CompressionType compression,
1531 SkBudgeted budgeted, const void* data) {
1532 GrGLTexture::IDDesc idDesc;
1533 GrGLTextureParameters::SamplerOverriddenState initialState;
1534 if (!this->createCompressedTextureImpl(&idDesc.fInfo, width, height, compression, &initialState,
1535 data)) {
1536 return nullptr;
1537 }
1538 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
1539
1540 GrSurfaceDesc desc;
1541 desc.fConfig = GrCompressionTypePixelConfig(compression);
1542 desc.fWidth = width;
1543 desc.fHeight = height;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001544 auto tex =
1545 sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, GrMipMapsStatus::kNotAllocated);
1546 // The non-sampler params are still at their default values.
1547 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1548 fResetTimestampForTextureParameters);
1549 return std::move(tex);
1550}
1551
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001553
egdaniel8dc7c3a2015-04-16 11:22:42 -07001554const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001555
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001556void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001557 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001558
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001559 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001560 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001561 (kUnknownBitCount == format->fTotalBits));
1562 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001563 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001564 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1565 (GrGLint*)&format->fStencilBits);
1566 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001567 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001568 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1569 (GrGLint*)&format->fTotalBits);
1570 format->fTotalBits += format->fStencilBits;
1571 } else {
1572 format->fTotalBits = format->fStencilBits;
1573 }
1574 }
1575}
1576}
1577
Brian Salomon5043f1f2019-07-11 21:27:54 -04001578int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001579 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001580 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1581
1582 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001583 // Default to unsupported, set this if we find a stencil format that works.
1584 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001585
egdanielff1d5472015-09-10 08:37:20 -07001586 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001587 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001588 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001589 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001590 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1591 GR_GL_TEXTURE_MAG_FILTER,
1592 GR_GL_NEAREST));
1593 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1594 GR_GL_TEXTURE_MIN_FILTER,
1595 GR_GL_NEAREST));
1596 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1597 GR_GL_TEXTURE_WRAP_S,
1598 GR_GL_CLAMP_TO_EDGE));
1599 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1600 GR_GL_TEXTURE_WRAP_T,
1601 GR_GL_CLAMP_TO_EDGE));
1602
Brian Salomonf6da1462019-07-11 14:21:59 -04001603 GrGLenum internalFormat = this->glCaps().getTexImageInternalFormat(format);
1604 GrGLenum externalFormat = this->glCaps().getBaseInternalFormat(format);
1605 GrGLenum externalType = this->glCaps().getFormatDefaultExternalType(format);
Greg Kaiser98ac0f92019-07-12 05:53:12 -07001606 if (!internalFormat || !externalFormat || !externalType) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001607 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001608 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001609
Brian Osman9b560242017-09-05 15:34:52 -04001610 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001611 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1612 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001613 0,
bsalomon76148af2016-01-12 11:13:47 -08001614 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001615 kSize,
1616 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001617 0,
bsalomon76148af2016-01-12 11:13:47 -08001618 externalFormat,
1619 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001620 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001621 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001622 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001623 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001624 }
1625
1626 // unbind the texture from the texture unit before binding it to the frame buffer
1627 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1628
1629 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001630 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001631 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001632 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001633 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001634 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1635 GR_GL_COLOR_ATTACHMENT0,
1636 GR_GL_TEXTURE_2D,
1637 colorID,
1638 0));
bsalomon30447372015-12-21 09:03:05 -08001639 GrGLuint sbRBID = 0;
1640 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001641
1642 // look over formats till I find a compatible one
1643 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001644 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001645 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001646 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1647 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1648 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1649 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1650 sFmt.fInternalFormat,
1651 kSize, kSize));
1652 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001653 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001654 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001655 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001656 if (sFmt.fPacked) {
1657 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1658 GR_GL_DEPTH_ATTACHMENT,
1659 GR_GL_RENDERBUFFER, sbRBID));
1660 } else {
1661 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1662 GR_GL_DEPTH_ATTACHMENT,
1663 GR_GL_RENDERBUFFER, 0));
1664 }
1665 GrGLenum status;
1666 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1667 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1668 firstWorkingStencilFormatIndex = i;
1669 break;
1670 }
egdanielff1d5472015-09-10 08:37:20 -07001671 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1672 GR_GL_STENCIL_ATTACHMENT,
1673 GR_GL_RENDERBUFFER, 0));
1674 if (sFmt.fPacked) {
1675 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1676 GR_GL_DEPTH_ATTACHMENT,
1677 GR_GL_RENDERBUFFER, 0));
1678 }
egdanielff1d5472015-09-10 08:37:20 -07001679 }
1680 }
bsalomon30447372015-12-21 09:03:05 -08001681 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001682 }
1683 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001684 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1685 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001686 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001687 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001688 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001689}
1690
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001691bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
1692 GrRenderable renderable,
Brian Salomone2826ab2019-06-04 15:58:31 -04001693 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomondc829942018-10-23 16:07:24 -04001694 const GrMipLevel texels[], int mipLevelCount,
1695 GrMipMapsStatus* mipMapsStatus) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001696 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
erikchen9a1ed5d2016-02-10 16:32:34 -08001697 info->fID = 0;
1698 info->fTarget = GR_GL_TEXTURE_2D;
1699 GL_CALL(GenTextures(1, &(info->fID)));
1700
1701 if (!info->fID) {
1702 return false;
1703 }
1704
Robert Phillips8043f322019-05-31 08:11:36 -04001705 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
1706
Brian Salomond978b902019-02-07 15:09:18 -05001707 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001708
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001709 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001710 // provides a hint about how this texture will be used
1711 GL_CALL(TexParameteri(info->fTarget,
1712 GR_GL_TEXTURE_USAGE,
1713 GR_GL_FRAMEBUFFER_ATTACHMENT));
1714 }
1715
Brian Salomone2826ab2019-06-04 15:58:31 -04001716 *initialState = set_initial_texture_params(this->glInterface(), *info);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001717
Brian Salomonbb8dde82019-06-27 10:52:13 -04001718 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1719 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
1720 texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001721 GL_CALL(DeleteTextures(1, &(info->fID)));
1722 return false;
1723 }
1724 return true;
1725}
1726
Brian Salomonbb8dde82019-06-27 10:52:13 -04001727bool GrGLGpu::createCompressedTextureImpl(
1728 GrGLTextureInfo* info, int width, int height, SkImage::CompressionType compression,
1729 GrGLTextureParameters::SamplerOverriddenState* initialState, const void* data) {
1730 info->fID = 0;
1731 GL_CALL(GenTextures(1, &info->fID));
1732 if (!info->fID) {
1733 return false;
1734 }
1735
1736 info->fTarget = GR_GL_TEXTURE_2D;
1737 this->bindTextureToScratchUnit(info->fTarget, info->fID);
1738
1739 *initialState = set_initial_texture_params(this->glInterface(), *info);
1740
1741 info->fFormat = this->uploadCompressedTexData(compression, width, height, info->fTarget, data);
1742 if (!info->fFormat) {
1743 GL_CALL(DeleteTextures(1, &info->fID));
1744 return false;
1745 }
1746 return true;
1747}
1748
Chris Daltoneffee202019-07-01 22:28:03 -06001749GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1750 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001751 SkASSERT(width >= rt->width());
1752 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001753
egdaniel8dc7c3a2015-04-16 11:22:42 -07001754 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001755
Brian Salomon5043f1f2019-07-11 21:27:54 -04001756 auto rtFormat = GrGLBackendFormatToGLFormat(rt->backendFormat());
Brian Salomonf6da1462019-07-11 14:21:59 -04001757 int sIdx = this->getCompatibleStencilIndex(rtFormat);
bsalomon62a627b2015-12-17 09:50:47 -08001758 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001759 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001760 }
egdanielff1d5472015-09-10 08:37:20 -07001761
1762 if (!sbDesc.fRenderbufferID) {
1763 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1764 }
1765 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001766 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001767 }
1768 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1769 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1770 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1771 // we do this "if" so that we don't call the multisample
1772 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001773 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001774 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001775 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001776 sFmt.fInternalFormat,
1777 width, height));
1778 } else {
1779 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1780 sFmt.fInternalFormat,
1781 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001782 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001783 }
1784 fStats.incStencilAttachmentCreates();
1785 // After sized formats we attempt an unsized format and take
1786 // whatever sizes GL gives us. In that case we query for the size.
1787 GrGLStencilAttachment::Format format = sFmt;
1788 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001789 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1790 sbDesc,
1791 width,
1792 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001793 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001794 format);
1795 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001796}
1797
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001798////////////////////////////////////////////////////////////////////////////////
1799
Brian Salomondbf70722019-02-07 11:31:24 -05001800sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1801 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001802 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001803}
1804
Greg Danielacd66b42019-05-22 16:29:12 -04001805void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001806 GrSurfaceOrigin rtOrigin) {
1807 if (scissorState.enabled()) {
1808 GrGLIRect scissor;
Greg Danielacd66b42019-05-22 16:29:12 -04001809 scissor.setRelativeTo(rtHeight, scissorState.rect(), rtOrigin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001810 // if the scissor fully contains the viewport then we fall through and
1811 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001812 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001813 if (fHWScissorSettings.fRect != scissor) {
1814 scissor.pushToGLScissor(this->glInterface());
1815 fHWScissorSettings.fRect = scissor;
1816 }
1817 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1818 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1819 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001820 }
1821 return;
1822 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001824
1825 // See fall through note above
1826 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001827}
1828
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001829void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001830 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001831#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001832 typedef GrWindowRectsState::Mode Mode;
1833 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1834 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001835
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001836 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001837 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001838 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001839 }
1840
csmartdalton7535f412016-08-23 06:51:00 -07001841 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1842 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001843 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1844 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001845
csmartdalton28341fa2016-08-17 10:00:21 -07001846 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001847 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001848 for (int i = 0; i < numWindows; ++i) {
Greg Danielacd66b42019-05-22 16:29:12 -04001849 glwindows[i].setRelativeTo(rt->height(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001850 }
1851
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001852 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001853 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001854
Greg Danielacd66b42019-05-22 16:29:12 -04001855 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001856#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001857}
1858
1859void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001860#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001861 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001862 return;
1863 }
1864 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001865 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001866#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001867}
1868
Brian Salomonf7232642018-09-19 08:58:08 -04001869void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1870 const GrPrimitiveProcessor& primProc,
1871 const GrPipeline& pipeline,
1872 const GrTextureProxy* const primProcTextures[],
1873 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001874 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001875 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001876 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1877 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1878 tex->texturePriv().mipMapsAreDirty()) {
1879 SkASSERT(this->caps()->mipMapSupport());
1880 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001881 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1882 } else if (auto* rt = tex->asRenderTarget()) {
1883 if (rt->needsResolve()) {
1884 this->resolveRenderTarget(rt);
1885 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001886 }
1887 };
1888
Brian Salomonf7232642018-09-19 08:58:08 -04001889 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1890 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1891 GrTexture* texture = primProcTextures[tex]->peekTexture();
1892 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
1893 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001894 }
1895
1896 GrFragmentProcessor::Iter iter(pipeline);
1897 while (const GrFragmentProcessor* fp = iter.next()) {
1898 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
1899 const auto& textureSampler = fp->textureSampler(i);
1900 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
1901 }
1902 }
1903}
1904
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001905bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1906 GrSurfaceOrigin origin,
1907 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04001908 const GrPipeline& pipeline,
1909 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04001910 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
1911 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07001912 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04001913 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
1914 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
1915 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
1916 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
1917 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
1918 numPrimProcTextureSets = dynamicStateArraysLength;
1919 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
1920 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
1921 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00001922 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001923
1924 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
1925
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001926 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
1927 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05001928 pipeline, willDrawPoints));
1929 if (!program) {
1930 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1931 return false;
1932 }
Brian Salomonf7232642018-09-19 08:58:08 -04001933 this->resolveAndGenerateMipMapsForProcessorTextures(
1934 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07001935
egdaniel080e6732014-12-22 07:35:52 -08001936 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001937 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001938
egdaniel080e6732014-12-22 07:35:52 -08001939 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001940
Brian Salomon802cb312018-06-08 18:05:20 -04001941 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001942
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001943 // Swizzle the blend to match what the shader will output.
Greg Daniel2c3398d2019-06-19 11:58:01 -04001944 this->flushBlend(blendInfo, pipeline.outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001945
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001946 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
1947 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08001948
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001949 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07001950 GrStencilSettings stencil;
1951 if (pipeline.isStencilEnabled()) {
1952 // TODO: attach stencil and create settings during render target flush.
1953 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1954 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1955 glRT->renderTargetPriv().numStencilBits());
1956 }
Chris Dalton71713f62019-04-18 12:41:03 -06001957 this->flushStencil(stencil, origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001958 if (pipeline.isScissorEnabled()) {
1959 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
1960 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Greg Danielacd66b42019-05-22 16:29:12 -04001961 this->flushScissor(state, glRT->width(), glRT->height(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001962 } else {
1963 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001964 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001965 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07001966 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001967
1968 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001969 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001970 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001971
1972 return true;
1973}
1974
Brian Salomon802cb312018-06-08 18:05:20 -04001975void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1976 if (!program) {
1977 fHWProgram.reset();
1978 fHWProgramID = 0;
1979 return;
1980 }
1981 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1982 if (program == fHWProgram) {
1983 return;
1984 }
1985 auto id = program->programID();
1986 SkASSERT(id);
1987 GL_CALL(UseProgram(id));
1988 fHWProgram = std::move(program);
1989 fHWProgramID = id;
1990}
1991
1992void GrGLGpu::flushProgram(GrGLuint id) {
1993 SkASSERT(id);
1994 if (fHWProgramID == id) {
1995 SkASSERT(!fHWProgram);
1996 return;
1997 }
1998 fHWProgram.reset();
1999 GL_CALL(UseProgram(id));
2000 fHWProgramID = id;
2001}
2002
2003void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002004 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002005 int baseVertex,
2006 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002007 int baseInstance,
2008 GrPrimitiveRestart enablePrimitiveRestart) {
2009 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002010
cdaltone2e71c22016-04-07 18:13:29 -07002011 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002012 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002013 SkASSERT(indexBuffer->isCpuBuffer() ||
2014 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002015 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002016 } else {
2017 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002018 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002019
Brian Salomon92be2f72018-06-19 14:33:47 -04002020 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2021 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002022
Brian Salomon802cb312018-06-08 18:05:20 -04002023 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002024 SkASSERT(vertexBuffer);
2025 SkASSERT(vertexBuffer->isCpuBuffer() ||
2026 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2027 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002028 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2029 const auto& attrib = fHWProgram->vertexAttribute(i);
2030 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002031 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2032 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002033 }
Chris Dalton1d616352017-05-31 12:51:23 -06002034 }
Brian Salomon802cb312018-06-08 18:05:20 -04002035 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002036 SkASSERT(instanceBuffer);
2037 SkASSERT(instanceBuffer->isCpuBuffer() ||
2038 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2039 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002040 int attribIdx = fHWProgram->numVertexAttributes();
2041 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2042 const auto& attrib = fHWProgram->instanceAttribute(i);
2043 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002044 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2045 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2046 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002047 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002048 }
2049}
2050
Brian Salomonae64c192019-02-05 09:41:37 -05002051GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002052 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002053
cdaltone2e71c22016-04-07 18:13:29 -07002054 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002055 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002056 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002057 }
cdaltone2e71c22016-04-07 18:13:29 -07002058
Brian Salomonae64c192019-02-05 09:41:37 -05002059 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002060 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002061 if (!bufferState->fBufferZeroKnownBound) {
2062 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2063 bufferState->fBufferZeroKnownBound = true;
2064 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002065 }
Brian Salomondbf70722019-02-07 11:31:24 -05002066 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2067 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002068 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2069 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2070 bufferState->fBufferZeroKnownBound = false;
2071 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002072 }
2073
Brian Salomonae64c192019-02-05 09:41:37 -05002074 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002075}
Brian Salomond818ebf2018-07-02 14:08:49 +00002076void GrGLGpu::disableScissor() {
2077 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2078 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2079 fHWScissorSettings.fEnabled = kNo_TriState;
2080 return;
2081 }
2082}
2083
Brian Osman9a9baae2018-11-05 15:06:26 -05002084void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002085 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002086 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002087 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002088 SkASSERT(!this->caps()->performColorClearsAsDraws());
2089 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002090
Brian Salomon43f8bf02017-10-18 08:33:29 -04002091 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002092
Brian Salomon43f8bf02017-10-18 08:33:29 -04002093 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2094
Brian Salomond818ebf2018-07-02 14:08:49 +00002095 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002096 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002097 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002098 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002099 }
Greg Danielacd66b42019-05-22 16:29:12 -04002100 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002101 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002102 this->flushColorWrite(true);
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002103
2104 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2105 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2106 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2107 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2108 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2109 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2110 }
2111 this->flushClearColor(r, g, b, a);
2112
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002113 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002114}
2115
Robert Phillips95214472017-08-08 18:00:03 -04002116void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002117 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2118
Robert Phillips95214472017-08-08 18:00:03 -04002119 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002120 return;
2121 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002122
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002123 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2124 // this should only be called internally when we know we have a
2125 // stencil buffer.
2126 SkASSERT(sb);
Robert Phillipscb2e2352017-08-30 16:44:40 -04002127
bsalomonb0bd4f62014-09-03 07:19:50 -07002128 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002129 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002130
Brian Salomond818ebf2018-07-02 14:08:49 +00002131 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002132 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002133
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002134 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002135 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002136 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002137 fHWStencilSettings.invalidate();
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002138 if (!clearValue) {
2139 sb->cleared();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002140 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002141}
2142
csmartdalton29df7602016-08-31 11:55:52 -07002143void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2144 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002145 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002146 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002147 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002148 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002149
egdaniel8dc7c3a2015-04-16 11:22:42 -07002150 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002151 // this should only be called internally when we know we have a
2152 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002153 SkASSERT(sb);
2154 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002155#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002156 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002157 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002158#else
2159 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002160 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002161 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002162 // changing the clip between stencil passes may or may not
2163 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002164 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002165#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002166 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002167 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002168 value = (1 << (stencilBitCount - 1));
2169 } else {
2170 value = 0;
2171 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002172 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002173 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002174
Greg Danielacd66b42019-05-22 16:29:12 -04002175 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002176 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002177
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002178 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002179 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002180 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002181 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002182}
2183
bsalomon1aa20292016-01-22 08:16:09 -08002184
Brian Salomone05ba5a2019-04-08 11:59:07 -04002185bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
2186 GrColorType dstColorType, void* offsetOrPtr,
Brian Salomon26de56e2019-04-10 12:14:26 -04002187 int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002188 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002189
bsalomone9573312016-01-25 14:33:25 -08002190 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002191 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002192 return false;
2193 }
2194
Brian Salomonc320b152018-02-20 14:05:36 -05002195 // TODO: Avoid this conversion by making GrGLCaps work with color types.
Greg Daniele877dce2019-07-11 10:52:43 -04002196 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType);
Brian Salomonc320b152018-02-20 14:05:36 -05002197
bsalomon76148af2016-01-12 11:13:47 -08002198 GrGLenum externalFormat;
2199 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002200 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002201 &externalType)) {
2202 return false;
2203 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002204
Brian Salomon71d9d842016-11-03 13:42:00 -04002205 if (renderTarget) {
2206 // resolve the render target if necessary
2207 switch (renderTarget->getResolveType()) {
2208 case GrGLRenderTarget::kCantResolve_ResolveType:
2209 return false;
2210 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002211 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002212 break;
2213 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002214 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002215 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002216 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002217 break;
2218 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002219 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002220 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002221 } else {
2222 // Use a temporary FBO.
Greg Danielacd66b42019-05-22 16:29:12 -04002223 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002224 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002225 }
2226
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002227 // the read rect is viewport-relative
2228 GrGLIRect readRect;
Greg Danielacd66b42019-05-22 16:29:12 -04002229 readRect.setRelativeTo(surface->height(), left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002230
Brian Salomona6948702018-06-01 15:33:20 -04002231 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002232 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002233 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002234 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002235 }
Brian Salomonc320b152018-02-20 14:05:36 -05002236 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002237
Brian Osman1348ed02018-09-12 09:08:39 -04002238 bool reattachStencil = false;
2239 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2240 renderTarget &&
2241 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002242 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002243 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2244 reattachStencil = true;
2245 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2246 GR_GL_RENDERBUFFER, 0));
2247 }
2248
Brian Salomone05ba5a2019-04-08 11:59:07 -04002249 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, readRect.fWidth, readRect.fHeight,
2250 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002251
2252 if (reattachStencil) {
2253 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2254 renderTarget->renderTargetPriv().getStencilAttachment());
2255 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2256 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2257 }
2258
Brian Salomon26de56e2019-04-10 12:14:26 -04002259 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002260 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002261 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2262 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002263
Brian Salomone05ba5a2019-04-08 11:59:07 -04002264 if (!renderTarget) {
2265 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2266 }
2267 return true;
2268}
2269
2270bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2271 GrColorType dstColorType, void* buffer, size_t rowBytes) {
2272 SkASSERT(surface);
2273
Brian Salomon26de56e2019-04-10 12:14:26 -04002274 int bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002275
Brian Salomon26de56e2019-04-10 12:14:26 -04002276 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2277 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002278
Brian Salomon1047a492019-07-02 12:25:21 -04002279 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002280 rowPixelWidth = width;
2281 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002282 SkASSERT(!(rowBytes % bytesPerPixel));
2283 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002284 }
Brian Salomon1047a492019-07-02 12:25:21 -04002285 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, buffer,
2286 rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002287}
2288
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002289GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002290 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002291 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002292 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002293 if (!fCachedRTCommandBuffer) {
2294 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2295 }
2296
2297 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2298 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002299}
2300
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002301GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2302 if (!fCachedTexCommandBuffer) {
2303 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2304 }
2305
2306 fCachedTexCommandBuffer->set(texture, origin);
2307 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002308}
2309
Brian Salomon1fabd512018-02-09 09:54:25 -05002310void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002311 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002312 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002313 this->didWriteToSurface(target, origin, &bounds);
2314}
bsalomon6ba6fa12015-03-04 11:57:37 -08002315
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002316void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2317 this->flushRenderTargetNoColorWrites(target);
2318 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002319}
2320
Brian Osman9aa30c62018-07-02 15:21:46 -04002321void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002322 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002323 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002324 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002325 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002326#ifdef SK_DEBUG
2327 // don't do this check in Chromium -- this is causing
2328 // lots of repeated command buffer flushes when the compositor is
2329 // rendering with Ganesh, which is really slow; even too slow for
2330 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002331 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002332 GrGLenum status;
2333 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2334 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2335 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2336 }
bsalomon160f24c2015-03-17 15:55:42 -07002337 }
egdanield803f272015-03-18 13:01:52 -07002338#endif
2339 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002340 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002341 }
2342
brianosman35b784d2016-05-05 11:52:53 -07002343 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002344 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002345 }
bsalomon083617b2016-02-12 12:10:14 -08002346}
bsalomona9909122016-01-23 10:41:40 -08002347
brianosman33f6b3f2016-06-02 05:49:21 -07002348void GrGLGpu::flushFramebufferSRGB(bool enable) {
2349 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2350 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2351 fHWSRGBFramebuffer = kYes_TriState;
2352 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2353 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2354 fHWSRGBFramebuffer = kNo_TriState;
2355 }
2356}
2357
Greg Danielacd66b42019-05-22 16:29:12 -04002358void GrGLGpu::flushViewport(int width, int height) {
2359 GrGLIRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002360 if (fHWViewport != viewport) {
2361 viewport.pushToGLViewport(this->glInterface());
2362 fHWViewport = viewport;
2363 }
2364}
2365
bsalomon@google.comd302f142011-03-03 13:54:13 +00002366#define SWAP_PER_DRAW 0
2367
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002368#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002369 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002370 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002371 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002372 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002373 void SwapBuf() {
2374 DWORD procID = GetCurrentProcessId();
2375 HWND hwnd = GetTopWindow(GetDesktopWindow());
2376 while(hwnd) {
2377 DWORD wndProcID = 0;
2378 GetWindowThreadProcessId(hwnd, &wndProcID);
2379 if(wndProcID == procID) {
2380 SwapBuffers(GetDC(hwnd));
2381 }
2382 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2383 }
2384 }
2385 #endif
2386#endif
2387
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002388void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2389 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002390 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002391 const GrPipeline::FixedDynamicState* fixedDynamicState,
2392 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002393 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002394 int meshCount) {
2395 this->handleDirtyContext();
2396
bsalomon2eda5b32016-09-21 10:53:24 -07002397 bool hasPoints = false;
2398 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002399 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002400 hasPoints = true;
2401 break;
2402 }
2403 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002404 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2405 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002406 return;
2407 }
ethannicholas22793252016-01-30 09:59:10 -08002408
Brian Salomonf7232642018-09-19 08:58:08 -04002409 bool dynamicScissor = false;
2410 bool dynamicPrimProcTextures = false;
2411 if (dynamicStateArrays) {
2412 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2413 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2414 }
2415 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002416 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2417 *this->caps())) {
2418 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002419 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002420
Brian Salomon49348902018-06-26 09:12:38 -04002421 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002422 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002423 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Greg Danielacd66b42019-05-22 16:29:12 -04002424 glRT->width(), glRT->height(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002425 }
Brian Salomonf7232642018-09-19 08:58:08 -04002426 if (dynamicPrimProcTextures) {
2427 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2428 m * primProc.numTextureSamplers();
2429 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2430 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002431 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002432 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002433 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2434 GL_CALL(Enable(GR_GL_CULL_FACE));
2435 GL_CALL(Disable(GR_GL_CULL_FACE));
2436 }
Brian Salomonf7232642018-09-19 08:58:08 -04002437 meshes[m].sendToGpu(this);
2438 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002439 }
ethannicholas22793252016-01-30 09:59:10 -08002440
bsalomon@google.comd302f142011-03-03 13:54:13 +00002441#if SWAP_PER_DRAW
2442 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002443 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002444 aglSwapBuffers(aglGetCurrentContext());
2445 int set_a_break_pt_here = 9;
2446 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002447 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002448 SwapBuf();
2449 int set_a_break_pt_here = 9;
2450 SwapBuf();
2451 #endif
2452#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002453}
2454
Chris Dalton3809bab2017-06-13 10:55:06 -06002455static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2456 switch (primitiveType) {
2457 case GrPrimitiveType::kTriangles:
2458 return GR_GL_TRIANGLES;
2459 case GrPrimitiveType::kTriangleStrip:
2460 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002461 case GrPrimitiveType::kPoints:
2462 return GR_GL_POINTS;
2463 case GrPrimitiveType::kLines:
2464 return GR_GL_LINES;
2465 case GrPrimitiveType::kLineStrip:
2466 return GR_GL_LINE_STRIP;
2467 case GrPrimitiveType::kLinesAdjacency:
2468 return GR_GL_LINES_ADJACENCY;
2469 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002470 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002471 return GR_GL_TRIANGLES;
2472}
2473
Brian Salomon802cb312018-06-08 18:05:20 -04002474void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2475 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002476 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002477 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002478 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002479 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2480 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002481 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002482 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2483 }
2484 fStats.incNumDraws();
2485}
2486
Brian Salomondbf70722019-02-07 11:31:24 -05002487static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2488 size_t baseOffset = baseIndex * sizeof(uint16_t);
2489 if (indexBuffer->isCpuBuffer()) {
2490 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2491 } else {
2492 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2493 }
2494}
2495
Brian Salomon802cb312018-06-08 18:05:20 -04002496void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002497 int indexCount, int baseIndex, uint16_t minIndexValue,
2498 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002499 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002500 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002501 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002502
Brian Salomon802cb312018-06-08 18:05:20 -04002503 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002504
2505 if (this->glCaps().drawRangeElementsSupport()) {
2506 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002507 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002508 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002509 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002510 }
2511 fStats.incNumDraws();
2512}
2513
Brian Salomon802cb312018-06-08 18:05:20 -04002514void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002515 int vertexCount, int baseVertex,
2516 const GrBuffer* instanceBuffer, int instanceCount,
2517 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002518 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002519 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002520 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002521 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2522 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002523 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2524 SkTMin(instanceCount - i, maxInstances)));
2525 fStats.incNumDraws();
2526 }
Chris Dalton1d616352017-05-31 12:51:23 -06002527}
2528
Brian Salomon802cb312018-06-08 18:05:20 -04002529void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002530 const GrBuffer* indexBuffer, int indexCount,
2531 int baseIndex, const GrBuffer* vertexBuffer,
2532 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002533 int instanceCount, int baseInstance,
2534 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002535 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002536 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002537 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2538 for (int i = 0; i < instanceCount; i += maxInstances) {
2539 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2540 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002541 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002542 SkTMin(instanceCount - i, maxInstances)));
2543 fStats.incNumDraws();
2544 }
Chris Dalton1d616352017-05-31 12:51:23 -06002545}
2546
Brian Salomon1fabd512018-02-09 09:54:25 -05002547void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002548 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002549 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002550 // Some extensions automatically resolves the texture when it is read.
2551 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002552 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002553 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002554 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2555 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2556
egdanield803f272015-03-18 13:01:52 -07002557 // make sure we go through flushRenderTarget() since we've modified
2558 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002559 fHWBoundRenderTargetUniqueID.makeInvalid();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002560 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002561 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2562 // surface. Choose kTopLeft so no adjustments are made
2563 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002564 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002565 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002566 GrScissorState scissorState;
2567 scissorState.set(dirtyRect);
Greg Danielacd66b42019-05-22 16:29:12 -04002568 this->flushScissor(scissorState, rt->width(), rt->height(), kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002569 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002570 GL_CALL(ResolveMultisampleFramebuffer());
2571 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002572 int l, b, r, t;
2573 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2574 this->glCaps().blitFramebufferSupportFlags()) {
2575 l = 0;
2576 b = 0;
2577 r = target->width();
2578 t = target->height();
2579 } else {
2580 GrGLIRect rect;
Greg Danielacd66b42019-05-22 16:29:12 -04002581 rect.setRelativeTo(rt->height(), dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002582 l = rect.fLeft;
2583 b = rect.fBottom;
2584 r = rect.fLeft + rect.fWidth;
2585 t = rect.fBottom + rect.fHeight;
2586 }
derekf8c8f71a2014-09-16 06:24:57 -07002587
2588 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002589 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002590 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002591 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002592 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002593 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002594 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002595 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002596 }
2597}
2598
bsalomon@google.com411dad02012-06-05 20:24:20 +00002599namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002600
bsalomon@google.com411dad02012-06-05 20:24:20 +00002601
2602GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002603 static const GrGLenum gTable[kGrStencilOpCount] = {
2604 GR_GL_KEEP, // kKeep
2605 GR_GL_ZERO, // kZero
2606 GR_GL_REPLACE, // kReplace
2607 GR_GL_INVERT, // kInvert
2608 GR_GL_INCR_WRAP, // kIncWrap
2609 GR_GL_DECR_WRAP, // kDecWrap
2610 GR_GL_INCR, // kIncClamp
2611 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002612 };
cdalton93a379b2016-05-11 13:58:08 -07002613 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2614 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2615 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2616 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2617 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2618 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2619 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2620 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2621 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2622 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002623}
2624
2625void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002626 const GrStencilSettings::Face& face,
2627 GrGLenum glFace) {
2628 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2629 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2630 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002631
cdalton93a379b2016-05-11 13:58:08 -07002632 GrGLint ref = face.fRef;
2633 GrGLint mask = face.fTestMask;
2634 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002635
2636 if (GR_GL_FRONT_AND_BACK == glFace) {
2637 // we call the combined func just in case separate stencil is not
2638 // supported.
2639 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2640 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002641 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002642 } else {
2643 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2644 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002645 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002646 }
2647}
2648}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002649
Chris Dalton71713f62019-04-18 12:41:03 -06002650void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002651 if (stencilSettings.isDisabled()) {
2652 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002653 } else if (fHWStencilSettings != stencilSettings ||
2654 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002655 if (kYes_TriState != fHWStencilTestEnabled) {
2656 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002657
csmartdaltonc7d85332016-10-26 10:13:46 -07002658 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002659 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002660 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002661 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2662 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002663 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002664 set_gl_stencil(
2665 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002666 }
joshualitta58fe352014-10-27 08:39:00 -07002667 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002668 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002669 }
2670}
2671
csmartdaltonc7d85332016-10-26 10:13:46 -07002672void GrGLGpu::disableStencil() {
2673 if (kNo_TriState != fHWStencilTestEnabled) {
2674 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002675
csmartdaltonc7d85332016-10-26 10:13:46 -07002676 fHWStencilTestEnabled = kNo_TriState;
2677 fHWStencilSettings.invalidate();
2678 }
2679}
2680
Chris Dalton4c56b032019-03-04 12:28:42 -07002681void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002682 // rt is only optional if useHWAA is false.
2683 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002684#ifdef SK_DEBUG
2685 if (useHWAA && rt->numSamples() <= 1) {
2686 SkASSERT(this->caps()->mixedSamplesSupport());
2687 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2688 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2689 }
2690#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002691
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002692 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002693 if (useHWAA) {
2694 if (kYes_TriState != fMSAAEnabled) {
2695 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2696 fMSAAEnabled = kYes_TriState;
2697 }
2698 } else {
2699 if (kNo_TriState != fMSAAEnabled) {
2700 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2701 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002702 }
2703 }
2704 }
2705}
2706
bsalomon7f9b2e42016-01-12 13:29:26 -08002707void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002708 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002709 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002710
cdalton8917d622015-05-06 13:40:21 -07002711 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002712 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2713 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002714 bool blendOff =
2715 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2716 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2717 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002718 if (blendOff) {
2719 if (kNo_TriState != fHWBlendState.fEnabled) {
2720 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002721
2722 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2723 // https://code.google.com/p/skia/issues/detail?id=3943
2724 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2725 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2726 SkASSERT(this->caps()->advancedBlendEquationSupport());
2727 // Set to any basic blending equation.
2728 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2729 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2730 fHWBlendState.fEquation = blend_equation;
2731 }
2732
egdanielb414f252014-07-29 13:15:47 -07002733 fHWBlendState.fEnabled = kNo_TriState;
2734 }
cdalton8917d622015-05-06 13:40:21 -07002735 return;
2736 }
2737
2738 if (kYes_TriState != fHWBlendState.fEnabled) {
2739 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002740
cdalton8917d622015-05-06 13:40:21 -07002741 fHWBlendState.fEnabled = kYes_TriState;
2742 }
2743
Mike Kleinab5fec92018-08-16 19:43:31 +00002744 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002745 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2746 fHWBlendState.fEquation = equation;
2747 }
2748
2749 if (GrBlendEquationIsAdvanced(equation)) {
2750 SkASSERT(this->caps()->advancedBlendEquationSupport());
2751 // Advanced equations have no other blend state.
2752 return;
2753 }
2754
Mike Kleinab5fec92018-08-16 19:43:31 +00002755 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002756 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2757 gXfermodeCoeff2Blend[dstCoeff]));
2758 fHWBlendState.fSrcCoeff = srcCoeff;
2759 fHWBlendState.fDstCoeff = dstCoeff;
2760 }
2761
bsalomon7f9b2e42016-01-12 13:29:26 -08002762 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002763 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002764 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002765 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002766 fHWBlendState.fConstColor = blendConst;
2767 fHWBlendState.fConstColorValid = true;
2768 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002769 }
2770}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002771
Brian Salomondc829942018-10-23 16:07:24 -04002772static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002773 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002774 switch (swizzle[i]) {
2775 case 'r': glValues[i] = GR_GL_RED; break;
2776 case 'g': glValues[i] = GR_GL_GREEN; break;
2777 case 'b': glValues[i] = GR_GL_BLUE; break;
2778 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002779 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002780 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002781 default: SK_ABORT("Unsupported component");
2782 }
Brian Salomon327955b2018-10-22 15:39:22 +00002783 }
2784}
2785
Greg Daniel2c3398d2019-06-19 11:58:01 -04002786void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2787 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002788 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002789
reed856e9d92015-09-30 12:21:45 -07002790#ifdef SK_DEBUG
2791 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002792 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002793 const int w = texture->width();
2794 const int h = texture->height();
2795 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2796 }
2797 }
2798#endif
2799
bsalomon@google.comb8670992012-07-25 21:27:09 +00002800 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2801 // 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 +00002802 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002803 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002804 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002805 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002806 }
2807
Robert Phillips294870f2016-11-11 12:38:40 -05002808 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002809 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002810 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002811 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002812 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002813 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002814 }
2815
Brian Salomondc829942018-10-23 16:07:24 -04002816 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002817 if (!this->caps()->mipMapSupport() ||
2818 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002819 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002820 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002821 }
bsalomonefd7d452014-10-23 14:17:46 -07002822
brianosman33f6b3f2016-06-02 05:49:21 -07002823#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002824 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002825 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002826 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002827 }
2828#endif
2829
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 auto timestamp = texture->parameters()->resetTimestamp();
2831 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002832
Brian Salomone2826ab2019-06-04 15:58:31 -04002833 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2834 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002835 if (fSamplerObjectCache) {
2836 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2837 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002838 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2839 texture->parameters()->samplerOverriddenState();
2840 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002841
Brian Salomone2826ab2019-06-04 15:58:31 -04002842 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2843 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002844
Brian Salomone2826ab2019-06-04 15:58:31 -04002845 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2846 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002847
2848 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002849 newSamplerState.fMinLOD = -1000.f;
2850 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002851
Brian Salomone2826ab2019-06-04 15:58:31 -04002852 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002853 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002854 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002855 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002856 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002857 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002858 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002859 }
Mike Klein1bccae52018-10-19 11:38:44 +00002860 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002861 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002862 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002863 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002864 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002865 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002866 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002867 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002868 }
2869 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002870 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002871 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002872 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002873 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002874 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002875 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002876 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002877 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002878 if (this->glCaps().clampToBorderSupport()) {
2879 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002880 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002881 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002882 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2883 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002884 }
2885 }
Brian Salomondc829942018-10-23 16:07:24 -04002886 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002887 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2888 newNonsamplerState.fBaseMipMapLevel = 0;
2889 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002890
Brian Salomone2826ab2019-06-04 15:58:31 -04002891 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2892 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002893 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002894 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2895 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002896 GrGLenum glValues[4];
2897 get_gl_swizzle_values(swizzle, glValues);
2898 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002899 if (GR_IS_GR_GL(this->glStandard())) {
2900 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
2901 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2902 reinterpret_cast<const GrGLint*>(glValues)));
2903 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002904 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2905 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2906 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2907 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2908 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002909 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002910 }
2911 }
Brian Salomondc829942018-10-23 16:07:24 -04002912 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002913 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2914 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2915 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002916 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002917 this->setTextureUnit(unitIdx);
2918 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002919 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002920 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002921 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002922 this->setTextureUnit(unitIdx);
2923 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002924 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002925 }
Brian Salomon327955b2018-10-22 15:39:22 +00002926 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002927 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2928 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002929}
2930
Brian Salomon1f05d452019-02-08 12:33:08 -05002931void GrGLGpu::onResetTextureBindings() {
2932 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2933 GR_GL_TEXTURE_EXTERNAL};
2934 for (int i = 0; i < this->numTextureUnits(); ++i) {
2935 this->setTextureUnit(i);
2936 for (auto target : kTargets) {
2937 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2938 GL_CALL(BindTexture(target, 0));
2939 }
2940 }
2941 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2942 }
2943}
2944
egdaniel080e6732014-12-22 07:35:52 -08002945void GrGLGpu::flushColorWrite(bool writeColor) {
2946 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002947 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002948 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2949 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002950 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002951 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002952 } else {
2953 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002954 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002955 fHWWriteToColor = kYes_TriState;
2956 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002957 }
bsalomon3e791242014-12-17 13:43:13 -08002958}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002959
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002960void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
Brian Salomon805cc7a2019-01-28 09:52:34 -05002961 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2962 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2963 GL_CALL(ClearColor(r, g, b, a));
2964 fHWClearColor[0] = r;
2965 fHWClearColor[1] = g;
2966 fHWClearColor[2] = b;
2967 fHWClearColor[3] = a;
2968 }
2969}
2970
bsalomon861e1032014-12-16 07:33:49 -08002971void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002972 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002973 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002974 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002975 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002976 }
2977}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002978
Brian Salomond978b902019-02-07 15:09:18 -05002979void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002980 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002981 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002982 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2983 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2984 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002985 }
Brian Salomond978b902019-02-07 15:09:18 -05002986 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2987 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002988 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002989 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002990}
2991
Brian Salomone5e7eb12016-10-14 16:18:33 -04002992// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002993static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2994 const GrSurface* src,
2995 const SkIRect& srcRect,
2996 const SkIPoint& dstPoint,
2997 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002998 int dstSampleCnt = 0;
2999 int srcSampleCnt = 0;
3000 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003001 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003002 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003003 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003004 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003005 }
3006 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3007 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3008
Brian Salomone5e7eb12016-10-14 16:18:33 -04003009 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003010 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003011
Greg Daniel46cfbc62019-06-07 11:43:30 -04003012 GrTextureType dstTexType;
3013 GrTextureType* dstTexTypePtr = nullptr;
3014 GrTextureType srcTexType;
3015 GrTextureType* srcTexTypePtr = nullptr;
3016 if (dstTex) {
3017 dstTexType = dstTex->texturePriv().textureType();
3018 dstTexTypePtr = &dstTexType;
3019 }
3020 if (srcTex) {
3021 srcTexType = srcTex->texturePriv().textureType();
3022 srcTexTypePtr = &srcTexType;
3023 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003024
Greg Daniel46cfbc62019-06-07 11:43:30 -04003025 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, dstTexTypePtr,
3026 src->config(), srcSampleCnt, srcTexTypePtr,
3027 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003028}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003029
Brian Osmana9c8a052018-01-19 10:31:56 -05003030static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3031 // A RT has a separate MSAA renderbuffer if:
3032 // 1) It's multisampled
3033 // 2) We're using an extension with separate MSAA renderbuffers
3034 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003035 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003036}
3037
Greg Daniel46cfbc62019-06-07 11:43:30 -04003038static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003039 const GrGLCaps& caps) {
3040
bsalomon@google.comeb851172013-04-15 13:51:00 +00003041 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003042 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003043 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003044 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003045
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003046 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3047 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3048
Greg Daniel46cfbc62019-06-07 11:43:30 -04003049 GrTextureType dstTexType;
3050 GrTextureType* dstTexTypePtr = nullptr;
3051 GrTextureType srcTexType;
3052 GrTextureType* srcTexTypePtr = nullptr;
3053 if (dstTex) {
3054 dstTexType = dstTex->texturePriv().textureType();
3055 dstTexTypePtr = &dstTexType;
3056 }
3057 if (srcTex) {
3058 srcTexType = srcTex->texturePriv().textureType();
3059 srcTexTypePtr = &srcTexType;
3060 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003061
Greg Daniel46cfbc62019-06-07 11:43:30 -04003062 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, dstTexTypePtr,
3063 src->config(), srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003064}
3065
Greg Danielacd66b42019-05-22 16:29:12 -04003066// If a temporary FBO was created, its non-zero ID is returned.
3067void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003068 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003069 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003070 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003071 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003072 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3073 GrGLuint texID = texture->textureID();
3074 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003075 GrGLuint* tempFBOID;
3076 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003077
egdanield803f272015-03-18 13:01:52 -07003078 if (0 == *tempFBOID) {
3079 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003080 }
3081
Adrienne Walker4ee88512018-05-17 11:37:14 -07003082 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003083 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003084 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003085 target,
robertphillips754f4e92014-09-18 13:52:08 -07003086 texID,
3087 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003088 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003089 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003090 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003091 }
egdaniel0f5f9672015-02-03 11:10:51 -08003092}
3093
Brian Salomon71d9d842016-11-03 13:42:00 -04003094void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3095 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003096 if (!surface->asRenderTarget()) {
3097 SkASSERT(surface->asTexture());
3098 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3099 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3100 GR_GL_COLOR_ATTACHMENT0,
3101 textureTarget,
3102 0,
3103 0));
3104 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003105}
3106
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003107void GrGLGpu::onFBOChanged() {
3108 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3109 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3110 GL_CALL(Flush());
3111 }
3112}
3113
Adrienne Walker4ee88512018-05-17 11:37:14 -07003114void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3115 fStats.incRenderTargetBinds();
3116 GL_CALL(BindFramebuffer(target, fboid));
3117 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3118 fBoundDrawFramebuffer = fboid;
3119 }
3120
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003121 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3122 // The driver forgets the correct scissor when modifying the FBO binding.
3123 if (!fHWScissorSettings.fRect.isInvalid()) {
3124 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3125 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003126 }
3127
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003128 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003129}
3130
Adrienne Walker4ee88512018-05-17 11:37:14 -07003131void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3132 if (fboid == fBoundDrawFramebuffer &&
3133 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3134 // This workaround only applies to deleting currently bound framebuffers
3135 // on Adreno 420. Because this is a somewhat rare case, instead of
3136 // tracking all the attachments of every framebuffer instead just always
3137 // unbind all attachments.
3138 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3139 GR_GL_RENDERBUFFER, 0));
3140 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3141 GR_GL_RENDERBUFFER, 0));
3142 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3143 GR_GL_RENDERBUFFER, 0));
3144 }
3145
3146 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003147
3148 // Deleting the currently bound framebuffer rebinds to 0.
3149 if (fboid == fBoundDrawFramebuffer) {
3150 this->onFBOChanged();
3151 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003152}
3153
Greg Daniel46cfbc62019-06-07 11:43:30 -04003154bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
3155 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003156 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3157 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3158 bool preferCopy = SkToBool(dst->asRenderTarget());
3159 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003160 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003161 return true;
3162 }
3163 }
cblume61214052016-01-26 09:10:48 -08003164
Greg Daniel46cfbc62019-06-07 11:43:30 -04003165 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3166 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003167 return true;
3168 }
3169
Greg Daniel46cfbc62019-06-07 11:43:30 -04003170 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3171 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003172 }
3173
3174 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003175 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003176 return true;
3177 }
bsalomon083617b2016-02-12 12:10:14 -08003178 }
3179
bsalomon6df86402015-06-01 10:41:49 -07003180 return false;
3181}
3182
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003183bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003184 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003185
3186 int progIdx = TextureToCopyProgramIdx(srcTex);
3187 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3188 GrSLType samplerType =
3189 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3190
3191 if (!fCopyProgramArrayBuffer) {
3192 static const GrGLfloat vdata[] = {
3193 0, 0,
3194 0, 1,
3195 1, 0,
3196 1, 1
3197 };
3198 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3199 kStatic_GrAccessPattern, vdata);
3200 }
3201 if (!fCopyProgramArrayBuffer) {
3202 return false;
3203 }
3204
3205 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3206 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3207 if (!fCopyPrograms[progIdx].fProgram) {
3208 return false;
3209 }
3210
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003211 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3212 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3213 GrShaderVar::kUniform_TypeModifier);
3214 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3215 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3216 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3217 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3218
Greg Daniel9e3169b2019-06-06 14:38:17 -04003219 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003220 if (shaderCaps->noperspectiveInterpolationSupport()) {
3221 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3222 vshaderTxt.appendf("#extension %s : require\n", extension);
3223 }
3224 vTexCoord.addModifier("noperspective");
3225 }
3226
3227 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3228 vshaderTxt.append(";");
3229 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3230 vshaderTxt.append(";");
3231 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3232 vshaderTxt.append(";");
3233 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3234 vshaderTxt.append(";");
3235
3236 vshaderTxt.append(
3237 "// Copy Program VS\n"
3238 "void main() {"
3239 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3240 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3241 " sk_Position.zw = half2(0, 1);"
3242 "}"
3243 );
3244
Greg Daniel9e3169b2019-06-06 14:38:17 -04003245 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003246 if (shaderCaps->noperspectiveInterpolationSupport()) {
3247 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3248 fshaderTxt.appendf("#extension %s : require\n", extension);
3249 }
3250 }
3251 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3252 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3253 fshaderTxt.append(";");
3254 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3255 fshaderTxt.append(";");
3256 fshaderTxt.appendf(
3257 "// Copy Program FS\n"
3258 "void main() {"
3259 " sk_FragColor = texture(u_texture, v_texCoord);"
3260 "}"
3261 );
3262
3263 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3264 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3265 SkSL::Program::Settings settings;
3266 settings.fCaps = shaderCaps;
3267 SkSL::String glsl;
3268 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3269 sksl, settings, &glsl, errorHandler);
3270 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3271 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3272 SkASSERT(program->fInputs.isEmpty());
3273
3274 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3275 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3276 errorHandler);
3277 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3278 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3279 errorHandler);
3280 SkASSERT(program->fInputs.isEmpty());
3281
3282 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3283
3284 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3285 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3286 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3287 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3288 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3289 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3290
3291 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3292
3293 GL_CALL(DeleteShader(vshader));
3294 GL_CALL(DeleteShader(fshader));
3295
3296 return true;
3297}
3298
brianosman33f6b3f2016-06-02 05:49:21 -07003299bool GrGLGpu::createMipmapProgram(int progIdx) {
3300 const bool oddWidth = SkToBool(progIdx & 0x2);
3301 const bool oddHeight = SkToBool(progIdx & 0x1);
3302 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3303
Brian Salomon1edc5b92016-11-29 13:43:46 -05003304 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003305
3306 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3307 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3308 if (!fMipmapPrograms[progIdx].fProgram) {
3309 return false;
3310 }
3311
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003312 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3313 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003314 GrShaderVar::kUniform_TypeModifier);
3315 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3316 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003317 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003318 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003319 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3320 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3321 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3322 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003323 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003324 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003325
Ethan Nicholasfc994162019-06-06 10:04:27 -04003326 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003327 if (shaderCaps->noperspectiveInterpolationSupport()) {
3328 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003329 vshaderTxt.appendf("#extension %s : require\n", extension);
3330 }
3331 vTexCoords[0].addModifier("noperspective");
3332 vTexCoords[1].addModifier("noperspective");
3333 vTexCoords[2].addModifier("noperspective");
3334 vTexCoords[3].addModifier("noperspective");
3335 }
3336
Brian Salomon1edc5b92016-11-29 13:43:46 -05003337 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003338 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003339 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003340 vshaderTxt.append(";");
3341 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003342 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003343 vshaderTxt.append(";");
3344 }
3345
3346 vshaderTxt.append(
3347 "// Mipmap Program VS\n"
3348 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003349 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3350 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003351 );
3352
3353 // Insert texture coordinate computation:
3354 if (oddWidth && oddHeight) {
3355 vshaderTxt.append(
3356 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003357 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3358 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003359 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3360 );
3361 } else if (oddWidth) {
3362 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003363 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3364 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003365 );
3366 } else if (oddHeight) {
3367 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003368 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3369 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003370 );
3371 } else {
3372 vshaderTxt.append(
3373 " v_texCoord0 = a_vertex.xy;"
3374 );
3375 }
3376
3377 vshaderTxt.append("}");
3378
Ethan Nicholasfc994162019-06-06 10:04:27 -04003379 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003380 if (shaderCaps->noperspectiveInterpolationSupport()) {
3381 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003382 fshaderTxt.appendf("#extension %s : require\n", extension);
3383 }
3384 }
brianosman33f6b3f2016-06-02 05:49:21 -07003385 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003386 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003387 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003388 fshaderTxt.append(";");
3389 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003390 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003391 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003392 fshaderTxt.append(
3393 "// Mipmap Program FS\n"
3394 "void main() {"
3395 );
3396
3397 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003398 fshaderTxt.append(
3399 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3400 " texture(u_texture, v_texCoord1) + "
3401 " texture(u_texture, v_texCoord2) + "
3402 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003403 );
3404 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003405 fshaderTxt.append(
3406 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3407 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003408 );
3409 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003410 fshaderTxt.append(
3411 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003412 );
3413 }
3414
3415 fshaderTxt.append("}");
3416
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003417 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003418 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003419 SkSL::Program::Settings settings;
3420 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003421 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003422 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003423 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003424 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003425 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003426 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003427
Brian Osman6c431d52019-04-15 16:31:54 -04003428 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003429 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3430 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003431 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003432 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003433 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003434 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003435
3436 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3437
3438 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3439 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3440 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3441 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3442
3443 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3444
3445 GL_CALL(DeleteShader(vshader));
3446 GL_CALL(DeleteShader(fshader));
3447
3448 return true;
3449}
3450
Greg Daniel46cfbc62019-06-07 11:43:30 -04003451bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003452 const SkIPoint& dstPoint) {
3453 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3454 int progIdx = TextureToCopyProgramIdx(srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003455 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3456 return false;
3457 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003458 if (!fCopyPrograms[progIdx].fProgram) {
3459 if (!this->createCopyProgram(srcTex)) {
3460 SkDebugf("Failed to create copy program.\n");
3461 return false;
3462 }
3463 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003464 int w = srcRect.width();
3465 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003466 // We don't swizzle at all in our copies.
3467 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003468 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
3469 this->flushViewport(dst->width(), dst->height());
3470 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003471 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003472 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003473 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003474 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3475 attribs->enableVertexArrays(this, 1);
3476 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3477 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003478 // dst rect edges in NDC (-1 to 1)
3479 int dw = dst->width();
3480 int dh = dst->height();
3481 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3482 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3483 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3484 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003485 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3486 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3487 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3488 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3489 int sw = src->width();
3490 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003491 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3492 // src rect edges in normalized texture space (0 to 1)
3493 sx0 /= sw;
3494 sx1 /= sw;
3495 sy0 /= sh;
3496 sy1 /= sh;
3497 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003498 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3499 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3500 sx1 - sx0, sy1 - sy0, sx0, sy0));
3501 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003502 GrXferProcessor::BlendInfo blendInfo;
3503 blendInfo.reset();
3504 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3505 this->flushColorWrite(true);
3506 this->flushHWAAState(nullptr, false);
3507 this->disableScissor();
3508 this->disableWindowRectangles();
3509 this->disableStencil();
3510 if (this->glCaps().srgbWriteControl()) {
3511 this->flushFramebufferSRGB(true);
3512 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003513 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3514 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003515 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3516 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003517 return true;
3518}
3519
Greg Daniel46cfbc62019-06-07 11:43:30 -04003520void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003521 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003522 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Greg Danielacd66b42019-05-22 16:29:12 -04003523 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003524 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003525 SkASSERT(dstTex);
3526 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003527 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003528
Brian Salomond978b902019-02-07 15:09:18 -05003529 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003530 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003531 dstPoint.fX, dstPoint.fY,
3532 srcRect.fLeft, srcRect.fTop,
3533 srcRect.width(), srcRect.height()));
Brian Salomon71d9d842016-11-03 13:42:00 -04003534 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003535 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3536 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003537 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3538 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003539}
3540
Greg Daniel46cfbc62019-06-07 11:43:30 -04003541bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003542 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003543 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003544 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3545 srcRect.width(), srcRect.height());
3546 if (dst == src) {
3547 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3548 return false;
mtklein404b3b22015-05-18 09:29:10 -07003549 }
bsalomon5df6fee2015-05-18 06:26:15 -07003550 }
bsalomon6df86402015-06-01 10:41:49 -07003551
Greg Danielacd66b42019-05-22 16:29:12 -04003552 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3553 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003554 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003555 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003556
3557 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003558 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003559 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003560
Greg Daniel46cfbc62019-06-07 11:43:30 -04003561 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3562 srcRect.fTop,
3563 srcRect.fRight,
3564 srcRect.fBottom,
3565 dstRect.fLeft,
3566 dstRect.fTop,
3567 dstRect.fRight,
3568 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003569 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003570 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3571 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003572
3573 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3574 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003575 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003576}
3577
Brian Salomon930f9392018-06-20 16:25:26 -04003578bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3579 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003580 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003581 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003582 return false;
3583 }
3584
Brian Salomon930f9392018-06-20 16:25:26 -04003585 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3586 // Uses draw calls to do a series of downsample operations to successive mips.
3587
3588 // The manual approach requires the ability to limit which level we're sampling and that the
3589 // destination can be bound to a FBO:
3590 if (!this->glCaps().doManualMipmapping() ||
3591 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3592 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003593 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003594 GL_CALL(GenerateMipmap(glTex->target()));
3595 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003596 }
3597
brianosman33f6b3f2016-06-02 05:49:21 -07003598 int width = texture->width();
3599 int height = texture->height();
3600 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003601 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003602
3603 // Create (if necessary), then bind temporary FBO:
3604 if (0 == fTempDstFBOID) {
3605 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3606 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003607 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003608 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003609
3610 // Bind the texture, to get things configured for filtering.
3611 // We'll be changing our base level further below:
3612 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003613 // The mipmap program does not do any swizzling.
3614 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003615
3616 // Vertex data:
3617 if (!fMipmapProgramArrayBuffer) {
3618 static const GrGLfloat vdata[] = {
3619 0, 0,
3620 0, 1,
3621 1, 0,
3622 1, 1
3623 };
Brian Salomonae64c192019-02-05 09:41:37 -05003624 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003625 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003626 }
3627 if (!fMipmapProgramArrayBuffer) {
3628 return false;
3629 }
3630
3631 fHWVertexArrayState.setVertexArrayID(this, 0);
3632
3633 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003634 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003635 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003636 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003637
3638 // Set "simple" state once:
3639 GrXferProcessor::BlendInfo blendInfo;
3640 blendInfo.reset();
3641 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3642 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003643 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003644 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003645 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003646 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003647
3648 // Do all the blits:
3649 width = texture->width();
3650 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003651
brianosman33f6b3f2016-06-02 05:49:21 -07003652 for (GrGLint level = 1; level < levelCount; ++level) {
3653 // Get and bind the program for this particular downsample (filter shape can vary):
3654 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3655 if (!fMipmapPrograms[progIdx].fProgram) {
3656 if (!this->createMipmapProgram(progIdx)) {
3657 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003658 // Invalidate all params to cover base level change in a previous iteration.
3659 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003660 return false;
3661 }
3662 }
Brian Salomon802cb312018-06-08 18:05:20 -04003663 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003664
3665 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3666 const float invWidth = 1.0f / width;
3667 const float invHeight = 1.0f / height;
3668 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3669 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3670 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3671
3672 // Only sample from previous mip
3673 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3674
Brian Salomon930f9392018-06-20 16:25:26 -04003675 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3676 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003677
3678 width = SkTMax(1, width / 2);
3679 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003680 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003681
3682 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3683 }
3684
3685 // Unbind:
3686 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3687 GR_GL_TEXTURE_2D, 0, 0));
3688
Brian Salomon930f9392018-06-20 16:25:26 -04003689 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003690 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3691 // We drew the 2nd to last level into the last level.
3692 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3693 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003694
brianosman33f6b3f2016-06-02 05:49:21 -07003695 return true;
3696}
3697
Chris Daltond7291ba2019-03-07 14:17:03 -07003698void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003699 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3700 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003701
3702 int effectiveSampleCnt;
3703 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003704 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003705
3706 sampleLocations->reset(effectiveSampleCnt);
3707 for (int i = 0; i < effectiveSampleCnt; ++i) {
3708 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3709 }
3710}
3711
cdalton231c5fd2015-05-13 12:35:36 -07003712void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003713 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003714 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003715 case kTexture_GrXferBarrierType: {
3716 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003717 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003718 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3719 // The render target uses separate storage so no need for glTextureBarrier.
3720 // FIXME: The render target will resolve automatically when its texture is bound,
3721 // but we could resolve only the bounds that will be read if we do it here instead.
3722 return;
3723 }
cdalton9954bc32015-04-29 14:17:00 -07003724 SkASSERT(this->caps()->textureBarrierSupport());
3725 GL_CALL(TextureBarrier());
3726 return;
cdalton231c5fd2015-05-13 12:35:36 -07003727 }
cdalton8917d622015-05-06 13:40:21 -07003728 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003729 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003730 this->caps()->blendEquationSupport());
3731 GL_CALL(BlendBarrier());
3732 return;
bsalomoncb02b382015-08-12 11:14:50 -07003733 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003734 }
3735}
3736
Brian Salomon5043f1f2019-07-11 21:27:54 -04003737static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003738 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003739 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3740 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3741 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3742 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3743 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3744 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3745 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3746 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3747 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
3748 case GrGLFormat::kRGBA32F: return kRGBA_float_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003749 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003750 case GrGLFormat::kR16: return kR_16_GrPixelConfig;
3751 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3752 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3753 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3754 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003755
Brian Salomon5043f1f2019-07-11 21:27:54 -04003756 // Configs with multiple equivalent formats.
3757
Robert Phillipsebab03f2019-07-22 08:48:18 -04003758 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3759 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3760
Brian Salomon5043f1f2019-07-11 21:27:54 -04003761 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3762 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3763
3764 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3765 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003766 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003767 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003768}
3769
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003770GrBackendTexture GrGLGpu::createBackendTexture(int w, int h,
3771 const GrBackendFormat& format,
3772 GrMipMapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -04003773 GrRenderable renderable,
Robert Phillips28a5a432019-06-07 12:46:21 -04003774 const void* srcPixels, size_t rowBytes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04003775 const SkColor4f* color,
3776 GrProtected isProtected) {
Brian Salomon8a375832018-03-14 10:21:40 -04003777 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003778
Brian Salomon5043f1f2019-07-11 21:27:54 -04003779 GrGLFormat glFormat = GrGLBackendFormatToGLFormat(format);
3780 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003781 return GrBackendTexture(); // invalid
3782 }
3783
Brian Salomon5043f1f2019-07-11 21:27:54 -04003784 GrPixelConfig config = gl_format_to_pixel_config(glFormat);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003785
Brian Salomon5043f1f2019-07-11 21:27:54 -04003786 if (config == kUnknown_GrPixelConfig) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003787 return GrBackendTexture(); // invalid
3788 }
3789
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003790 if (!this->caps()->isConfigTexturable(config)) {
3791 return GrBackendTexture(); // invalid
3792 }
3793
Brian Salomonbb8dde82019-06-27 10:52:13 -04003794 if (w < 1 || w > this->caps()->maxTextureSize() ||
3795 h < 1 || h > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003796 return GrBackendTexture(); // invalid
3797 }
3798
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003799 // Currently we don't support uploading pixel data when mipped.
Robert Phillips28a5a432019-06-07 12:46:21 -04003800 if (srcPixels && GrMipMapped::kYes == mipMapped) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003801 return GrBackendTexture(); // invalid
3802 }
3803
Brian Salomon4687bdd2019-05-09 16:28:04 -04003804 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillipse3bd6732019-05-29 14:20:35 -04003805 return GrBackendTexture(); // invalid
Brian Salomon4687bdd2019-05-09 16:28:04 -04003806 }
Robert Phillips646f6372018-09-25 09:31:10 -04003807
Greg Daniel15500642019-06-27 03:05:55 +00003808 GrGLTextureInfo info;
3809 GrGLTextureParameters::SamplerOverriddenState initialState;
3810
Brian Salomonbb8dde82019-06-27 10:52:13 -04003811 int mipLevelCount = 0;
3812 SkAutoTMalloc<GrMipLevel> texels;
3813 SkAutoMalloc pixelStorage;
3814 SkImage::CompressionType compressionType;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003815 if (GrGLFormatToCompressionType(glFormat, &compressionType)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04003816 // Compressed textures currently must be non-MIP mapped and have initial data.
3817 if (mipMapped == GrMipMapped::kYes) {
3818 return GrBackendTexture();
3819 }
3820 if (!srcPixels) {
3821 if (!color) {
3822 return GrBackendTexture();
3823 }
3824 SkASSERT(0 == rowBytes);
3825 size_t size = GrCompressedDataSize(compressionType, w, h);
3826 srcPixels = pixelStorage.reset(size);
3827 GrFillInCompressedData(compressionType, w, h, (char*)srcPixels, *color);
3828 }
3829 if (!this->createCompressedTextureImpl(&info, w, h, compressionType, &initialState,
3830 srcPixels)) {
3831 return GrBackendTexture();
3832 }
3833 } else {
3834 if (srcPixels) {
3835 mipLevelCount = 1;
3836 texels.reset(mipLevelCount);
3837 texels.get()[0] = {srcPixels, rowBytes};
3838 } else if (color) {
3839 mipLevelCount = 1;
3840 if (GrMipMapped::kYes == mipMapped) {
3841 mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
3842 }
3843
3844 texels.reset(mipLevelCount);
3845 SkTArray<size_t> individualMipOffsets(mipLevelCount);
3846
3847 size_t bytesPerPixel = GrBytesPerPixel(config);
3848
3849 size_t totalSize = GrComputeTightCombinedBufferSize(
3850 bytesPerPixel, w, h, &individualMipOffsets, mipLevelCount);
3851
3852 char* tmpPixels = (char*)pixelStorage.reset(totalSize);
3853
3854 GrFillInData(config, w, h, individualMipOffsets, tmpPixels, *color);
3855 for (int i = 0; i < mipLevelCount; ++i) {
3856 size_t offset = individualMipOffsets[i];
3857
3858 int twoToTheMipLevel = 1 << i;
3859 int currentWidth = SkTMax(1, w / twoToTheMipLevel);
3860
3861 texels.get()[i] = {&(tmpPixels[offset]), currentWidth * bytesPerPixel};
3862 }
3863 }
3864 GrSurfaceDesc desc;
3865 desc.fWidth = w;
3866 desc.fHeight = h;
3867 desc.fConfig = config;
3868 if (!this->createTextureImpl(desc, &info, renderable, &initialState, texels.get(),
3869 mipLevelCount, nullptr)) {
3870 return GrBackendTexture(); // invalid
3871 }
Robert Phillipse3bd6732019-05-29 14:20:35 -04003872 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003873
Robert Phillipse8fabb22018-02-04 14:33:21 -05003874 // unbind the texture from the texture unit to avoid asserts
3875 GL_CALL(BindTexture(info.fTarget, 0));
3876
Brian Salomone2826ab2019-06-04 15:58:31 -04003877 auto parameters = sk_make_sp<GrGLTextureParameters>();
3878 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3879 fResetTimestampForTextureParameters);
3880 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info, std::move(parameters));
Robert Phillipsf0ced622019-05-16 09:06:25 -04003881#if GR_TEST_UTILS
Robert Phillipse3bd6732019-05-29 14:20:35 -04003882 // Lots of tests don't go through Skia's public interface, which will set the config, so for
Greg Daniel108bb232018-07-03 16:18:29 -04003883 // testing we make sure we set a config here.
3884 beTex.setPixelConfig(config);
Robert Phillipsf0ced622019-05-16 09:06:25 -04003885#endif
Greg Daniel108bb232018-07-03 16:18:29 -04003886 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003887}
3888
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003889void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003890 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3891
3892 GrGLTextureInfo info;
3893 if (tex.getGLTextureInfo(&info)) {
3894 GL_CALL(DeleteTextures(1, &info.fID));
3895 }
3896}
3897
3898#if GR_TEST_UTILS
3899
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003900bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003901 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003902
Greg Daniel52e16d92018-04-10 09:34:07 -04003903 GrGLTextureInfo info;
3904 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003905 return false;
3906 }
3907
3908 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003909 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003910
3911 return (GR_GL_TRUE == result);
3912}
3913
Brian Salomonf865b052018-03-09 09:01:53 -05003914GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003915 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003916 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3917 return GrBackendRenderTarget(); // invalid
3918 }
Brian Salomon8a375832018-03-14 10:21:40 -04003919 this->handleDirtyContext();
Greg Daniele877dce2019-07-11 10:52:43 -04003920 auto config = GrColorTypeToPixelConfig(colorType);
Brian Salomon93348dd2018-08-29 12:56:23 -04003921 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003922 return {};
3923 }
Greg Daniel24b67662019-07-15 16:55:53 -04003924 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon93348dd2018-08-29 12:56:23 -04003925 bool useTexture = false;
3926 GrGLenum colorBufferFormat;
3927 GrGLenum externalFormat = 0, externalType = 0;
3928 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
3929 // BGRA render buffers are not supported.
3930 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
3931 &externalType);
3932 useTexture = true;
3933 } else {
Greg Daniel24b67662019-07-15 16:55:53 -04003934 colorBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003935 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003936 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003937 if (sFormatIdx < 0) {
3938 return {};
3939 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003940 GrGLuint colorID = 0;
3941 GrGLuint stencilID = 0;
3942 auto deleteIDs = [&] {
3943 if (colorID) {
3944 if (useTexture) {
3945 GL_CALL(DeleteTextures(1, &colorID));
3946 } else {
3947 GL_CALL(DeleteRenderbuffers(1, &colorID));
3948 }
Brian Salomonf865b052018-03-09 09:01:53 -05003949 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003950 if (stencilID) {
3951 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003952 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003953 };
3954
3955 if (useTexture) {
3956 GL_CALL(GenTextures(1, &colorID));
3957 } else {
3958 GL_CALL(GenRenderbuffers(1, &colorID));
3959 }
3960 GL_CALL(GenRenderbuffers(1, &stencilID));
3961 if (!stencilID || !colorID) {
3962 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003963 return {};
3964 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003965
Brian Salomonf865b052018-03-09 09:01:53 -05003966 GrGLFramebufferInfo info;
3967 info.fFBOID = 0;
Greg Daniel24b67662019-07-15 16:55:53 -04003968 info.fFormat = this->glCaps().configSizedInternalFormat(config);
Brian Salomonf865b052018-03-09 09:01:53 -05003969 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3970 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003971 deleteIDs();
3972 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003973 }
3974
3975 this->invalidateBoundRenderTarget();
3976
Adrienne Walker4ee88512018-05-17 11:37:14 -07003977 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003978 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05003979 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003980 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
3981 externalType, nullptr));
3982 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3983 colorID, 0));
3984 } else {
3985 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3986 GL_ALLOC_CALL(this->glInterface(),
3987 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
3988 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3989 GR_GL_RENDERBUFFER, colorID));
3990 }
3991 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003992 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3993 GL_ALLOC_CALL(this->glInterface(),
3994 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3995 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003996 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003997 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3998 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003999 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004000 }
4001
Brian Salomon93348dd2018-08-29 12:56:23 -04004002 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4003 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4004 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4005 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004006 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004007 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004008
Adrienne Walker4ee88512018-05-17 11:37:14 -07004009 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004010 GrGLenum status;
4011 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4012 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004013 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004014 return {};
4015 }
4016 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004017 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4018 // Lots of tests don't go through Skia's public interface which will set the config so for
4019 // testing we make sure we set a config here.
4020 beRT.setPixelConfig(config);
Robert Phillips1e2cb442019-07-02 15:51:28 -04004021 SkASSERT(kUnknown_GrPixelConfig != this->caps()->validateBackendRenderTarget(beRT, colorType));
Greg Daniel108bb232018-07-03 16:18:29 -04004022 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004023}
4024
4025void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004026 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004027 GrGLFramebufferInfo info;
4028 if (backendRT.getGLFramebufferInfo(&info)) {
4029 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004030 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004031 }
4032 }
joshualitt8fd844f2015-12-02 13:36:47 -08004033}
4034
Greg Daniel26b50a42018-03-08 09:49:58 -05004035void GrGLGpu::testingOnly_flushGpuAndSync() {
4036 GL_CALL(Finish());
4037}
Brian Salomonf865b052018-03-09 09:01:53 -05004038#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004039
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004040///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004041
cdaltone2e71c22016-04-07 18:13:29 -07004042GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004043 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004044 GrGLAttribArrayState* attribState;
4045
cdaltone2e71c22016-04-07 18:13:29 -07004046 if (gpu->glCaps().isCoreProfile()) {
4047 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004048 GrGLuint arrayID;
4049 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4050 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004051 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004052 }
cdaltone2e71c22016-04-07 18:13:29 -07004053 if (ibuf) {
4054 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004055 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004056 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004057 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004058 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004059 if (ibuf) {
4060 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004061 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004062 } else {
4063 this->setVertexArrayID(gpu, 0);
4064 }
4065 int attrCount = gpu->glCaps().maxVertexAttributes();
4066 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4067 fDefaultVertexArrayAttribState.resize(attrCount);
4068 }
4069 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004070 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004071 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004072}
bsalomone179a912016-01-20 06:18:10 -08004073
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04004074void GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004075 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004076 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004077 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004078 // We call finish if the client told us to sync or if we have a finished proc but don't support
4079 // GLsync objects.
4080 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4081 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4082 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004083 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004084 // After a finish everything previously sent to GL is done.
4085 for (const auto& cb : fFinishCallbacks) {
4086 cb.fCallback(cb.fContext);
4087 this->deleteSync(cb.fSync);
4088 }
4089 fFinishCallbacks.clear();
4090 if (info.fFinishedProc) {
4091 info.fFinishedProc(info.fFinishedContext);
4092 }
4093 } else {
4094 if (info.fFinishedProc) {
4095 FinishCallback callback;
4096 callback.fCallback = info.fFinishedProc;
4097 callback.fContext = info.fFinishedContext;
4098 callback.fSync = (GrGLsync)this->insertFence();
4099 fFinishCallbacks.push_back(callback);
4100 GL_CALL(Flush());
4101 } else if (insertedSemaphore) {
4102 // Must call flush after semaphores in case they are waited on another GL context.
4103 GL_CALL(Flush());
4104 }
4105 // See if any previously inserted finish procs are good to go.
4106 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004107 }
Greg Daniel51316782017-08-02 15:10:09 +00004108}
4109
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004110void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4111 if (buffer->asRTCommandBuffer()) {
4112 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4113 fCachedRTCommandBuffer->reset();
4114 } else {
4115 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4116 fCachedTexCommandBuffer->reset();
4117 }
4118}
4119
Greg Daniel6be35232017-03-01 17:01:09 -05004120GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004121 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004122 GrGLsync sync;
4123 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4124 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4125 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004126}
4127
Brian Salomonb0d8b762019-05-06 16:58:22 -04004128bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4129 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004130 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004131 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4132 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4133}
4134
4135bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4136 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004137}
4138
4139void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004140 this->deleteSync((GrGLsync)fence);
4141}
4142
Greg Daniela5cb7812017-06-16 09:45:32 -04004143sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004144 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004145 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004146}
4147
Greg Daniel48661b82018-01-22 16:11:35 -05004148sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4149 GrResourceProvider::SemaphoreWrapType wrapType,
4150 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004151 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004152 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4153}
4154
Greg Daniel858e12c2018-12-06 11:11:37 -05004155void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004156 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4157
Greg Daniel48661b82018-01-22 16:11:35 -05004158 GrGLsync sync;
4159 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4160 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004161}
4162
Greg Daniel48661b82018-01-22 16:11:35 -05004163void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004164 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4165
4166 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4167}
4168
Brian Salomonb0d8b762019-05-06 16:58:22 -04004169void GrGLGpu::checkFinishProcs() {
4170 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4171 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4172 /* timeout = */ 0, /* flush = */ false)) {
4173 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4174 this->deleteSync(fFinishCallbacks.front().fSync);
4175 fFinishCallbacks.pop_front();
4176 }
4177}
4178
Greg Daniel6be35232017-03-01 17:01:09 -05004179void GrGLGpu::deleteSync(GrGLsync sync) const {
4180 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004181}
Brian Osman13dddce2017-05-09 13:19:50 -04004182
Robert Phillips65a88fa2017-08-08 08:36:22 -04004183void GrGLGpu::insertEventMarker(const char* msg) {
4184 GL_CALL(InsertEventMarker(strlen(msg), msg));
4185}
4186
Brian Osman13dddce2017-05-09 13:19:50 -04004187sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4188 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004189 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004190 this->insertSemaphore(semaphore);
4191 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4192 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004193
4194 return semaphore;
4195}
Robert Phillips646e4292017-06-13 12:44:56 -04004196
4197int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004198 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004199 case kTexture2DSampler_GrSLType:
4200 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004201 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004202 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004203 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004204 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004205 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004206 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004207 return 0;
4208 }
4209}
Brian Osman71a18892017-08-10 10:23:25 -04004210
Kevin Lubickf4def342018-10-04 12:52:50 -04004211#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004212#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004213void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4214 // We are called by the base class, which has already called beginObject(). We choose to nest
4215 // all of our caps information in a named sub-object.
4216 writer->beginObject("GL GPU");
4217
4218 const GrGLubyte* str;
4219 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4220 writer->appendString("GL_VERSION", (const char*)(str));
4221 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4222 writer->appendString("GL_RENDERER", (const char*)(str));
4223 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4224 writer->appendString("GL_VENDOR", (const char*)(str));
4225 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4226 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4227
4228 writer->appendName("extensions");
4229 glInterface()->fExtensions.dumpJSON(writer);
4230
4231 writer->endObject();
4232}
Kevin Lubickf4def342018-10-04 12:52:50 -04004233#endif