blob: 4f0718c39e348ec99029f25d1cf1b8cf607a6b76 [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 }
679 return true;
680}
681
682sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500683 GrWrapOwnership ownership, GrWrapCacheable cacheable,
684 GrIOType ioType) {
bsalomonb15b4c12014-10-29 12:41:57 -0700685 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400686 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
687 return nullptr;
688 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500689 if (!idDesc.fInfo.fFormat) {
690 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
691 }
Brian Salomond17f6582017-07-19 18:28:58 -0400692 if (kBorrow_GrWrapOwnership == ownership) {
693 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
694 } else {
695 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
696 }
bsalomone5286e02016-01-14 09:24:09 -0800697
Brian Salomond17f6582017-07-19 18:28:58 -0400698 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400699 surfDesc.fWidth = backendTex.width();
700 surfDesc.fHeight = backendTex.height();
701 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500702 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400703
Greg Daniel177e6952017-10-12 12:27:11 -0400704 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
705 : GrMipMapsStatus::kNotAllocated;
706
Brian Salomone2826ab2019-06-04 15:58:31 -0400707 auto texture = GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc,
708 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400709 // We don't know what parameters are already set on wrapped textures.
710 texture->textureParamsModified();
711 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400712}
713
714sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400715 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400716 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500717 GrWrapOwnership ownership,
718 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400719 GrGLTexture::IDDesc idDesc;
720 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800721 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800722 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500723 if (!idDesc.fInfo.fFormat) {
724 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
725 }
bsalomone5286e02016-01-14 09:24:09 -0800726
Brian Salomond17f6582017-07-19 18:28:58 -0400727 // We don't support rendering to a EXTERNAL texture.
728 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800729 return nullptr;
730 }
bsalomon10528f12015-10-14 12:54:52 -0700731
Brian Osman766fcbb2017-03-13 09:33:09 -0400732 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000733 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400734 } else {
735 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800736 }
bsalomonb15b4c12014-10-29 12:41:57 -0700737
Robert Phillips0902c982019-07-16 07:47:56 -0400738 const GrCaps* caps = this->caps();
739
Ben Wagner18b61f92016-10-25 10:44:02 -0400740 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000741 surfDesc.fWidth = backendTex.width();
742 surfDesc.fHeight = backendTex.height();
743 surfDesc.fConfig = backendTex.config();
Robert Phillips0902c982019-07-16 07:47:56 -0400744 surfDesc.fSampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
745 backendTex.getBackendFormat());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500746 if (surfDesc.fSampleCnt < 1) {
747 return nullptr;
748 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000749
Brian Salomond17f6582017-07-19 18:28:58 -0400750 GrGLRenderTarget::IDDesc rtIDDesc;
751 if (!this->createRenderTargetObjects(surfDesc, idDesc.fInfo, &rtIDDesc)) {
752 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000753 }
Greg Daniel177e6952017-10-12 12:27:11 -0400754
755 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
756 : GrMipMapsStatus::kNotAllocated;
757
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500758 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomone2826ab2019-06-04 15:58:31 -0400759 this, surfDesc, idDesc, backendTex.getGLTextureParams(), rtIDDesc, cacheable,
760 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400761 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400762 // We don't know what parameters are already set on wrapped textures.
763 texRT->textureParamsModified();
Brian Salomond17f6582017-07-19 18:28:58 -0400764 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000765}
766
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400767sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400768 GrGLFramebufferInfo info;
769 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000770 return nullptr;
771 }
772
bsalomonb15b4c12014-10-29 12:41:57 -0700773 GrGLRenderTarget::IDDesc idDesc;
Greg Daniel323fbcf2018-04-10 13:46:30 -0400774 idDesc.fRTFBOID = info.fFBOID;
bsalomonb15b4c12014-10-29 12:41:57 -0700775 idDesc.fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -0700776 idDesc.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
Brian Osman0b791f52017-03-10 08:30:22 -0500777 idDesc.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000778
bsalomonb15b4c12014-10-29 12:41:57 -0700779 GrSurfaceDesc desc;
Greg Danielbcf612b2017-05-01 13:50:58 +0000780 desc.fWidth = backendRT.width();
781 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400782 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500783 desc.fSampleCnt =
784 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700785
Greg Daniel4065d452018-11-16 15:43:41 -0500786 return GrGLRenderTarget::MakeWrapped(this, desc, info.fFormat, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000787}
788
Greg Daniel7ef28f32017-04-20 16:41:55 +0000789sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000790 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400791 GrGLTextureInfo info;
792 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800793 return nullptr;
794 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800795
Greg Daniel52e16d92018-04-10 09:34:07 -0400796 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
797 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800798 // Only texture rectangle and texture 2d are supported. We do not check whether texture
799 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
800 // we assume the necessary support exists.
801 return nullptr;
802 }
803
Ben Wagner18b61f92016-10-25 10:44:02 -0400804 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000805 surfDesc.fWidth = tex.width();
806 surfDesc.fHeight = tex.height();
807 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500808 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800809
810 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400811 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800812 return nullptr;
813 }
Greg Daniel4065d452018-11-16 15:43:41 -0500814 return GrGLRenderTarget::MakeWrapped(this, surfDesc, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800815}
816
Brian Salomonc320b152018-02-20 14:05:36 -0500817static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700818 if (!glTex) {
819 return false;
820 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000821
bsalomone5286e02016-01-14 09:24:09 -0800822 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
823 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800824 return false;
825 }
826
jvanverth17aa0472016-01-05 10:41:27 -0800827 return true;
828}
829
Brian Salomona9b04b92018-06-01 15:04:28 -0400830bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
831 GrColorType srcColorType, const GrMipLevel texels[],
832 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500833 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800834
Brian Salomonc320b152018-02-20 14:05:36 -0500835 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800836 return false;
837 }
838
Brian Salomond978b902019-02-07 15:09:18 -0500839 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000840
Brian Salomonc320b152018-02-20 14:05:36 -0500841 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
842 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
843 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
844 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
845 // caps knows to make the external format be GL_RGBA.
Greg Daniele877dce2019-07-11 10:52:43 -0400846 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500847
848 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400849 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
850 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
851 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000852}
853
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400854// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800855static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500856 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800857 switch (config) {
858 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500859 case kAlpha_8_as_Alpha_GrPixelConfig:
860 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500861 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500862 case kGray_8_as_Lum_GrPixelConfig:
863 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800864 return 1;
865 case kRGB_565_GrPixelConfig:
866 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500867 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800868 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500869 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800870 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400871 case kRGBA_half_Clamped_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -0400872 case kR_16_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800873 return 2;
874 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400875 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
Greg Danielf259b8b2019-02-14 09:03:43 -0500876 case kRGB_888X_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800877 case kBGRA_8888_GrPixelConfig:
878 case kSRGBA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500879 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800880 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500881 case kRG_float_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -0400882 case kRG_1616_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800883 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500884 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500885 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800886 return 0;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400887
Robert Phillips66a46032019-06-18 08:00:42 -0400888 // Experimental (for Y416 and mutant P016/P010)
889 case kRGBA_16161616_GrPixelConfig:
890 return 8;
891 case kRG_half_GrPixelConfig:
Robert Phillipsfe18de52019-06-06 17:21:50 -0400892 return 4;
bsalomonf46a1242015-12-15 12:37:38 -0800893 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400894 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500895 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800896}
897
Brian Salomone05ba5a2019-04-08 11:59:07 -0400898bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
899 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
900 size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
902 GrPixelConfig texConfig = glTex->config();
903 SkASSERT(this->caps()->isConfigTexturable(texConfig));
904
Jim Van Verth1676cb92019-01-15 13:24:45 -0500905 // Can't transfer compressed data
906 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
907
Brian Salomonc320b152018-02-20 14:05:36 -0500908 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400909 return false;
910 }
911
Hal Canaryc36f7e72018-06-18 15:50:09 -0400912 static_assert(sizeof(int) == sizeof(int32_t), "");
913 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914 return false;
915 }
916
Brian Salomond978b902019-02-07 15:09:18 -0500917 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400918
919 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500920 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500922 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400923
Greg Daniel660cc992017-06-26 14:55:05 -0400924 SkDEBUGCODE(
925 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
926 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
927 SkASSERT(bounds.contains(subRect));
928 )
929
Brian Salomonc320b152018-02-20 14:05:36 -0500930 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400931 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400932 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700933 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400934 return false;
935 }
936
937 bool restoreGLRowLength = false;
938 if (trimRowBytes != rowBytes) {
939 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400940 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400941 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
942 restoreGLRowLength = true;
943 }
944
945 // Internal format comes from the texture desc.
946 GrGLenum internalFormat;
947 // External format and type come from the upload data.
948 GrGLenum externalFormat;
949 GrGLenum externalType;
Greg Daniele877dce2019-07-11 10:52:43 -0400950 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType);
Brian Salomonc320b152018-02-20 14:05:36 -0500951 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400952 &externalFormat, &externalType)) {
953 return false;
954 }
955
956 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
957 GL_CALL(TexSubImage2D(glTex->target(),
958 0,
959 left, top,
960 width,
961 height,
962 externalFormat, externalType,
963 pixels));
964
965 if (restoreGLRowLength) {
966 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
967 }
968
969 return true;
970}
971
Brian Salomon26de56e2019-04-10 12:14:26 -0400972bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
973 GrColorType dstColorType, GrGpuBuffer* transferBuffer,
974 size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400975 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
976 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400977 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomon26de56e2019-04-10 12:14:26 -0400978 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType,
979 offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400980}
981
cblume55f2d2d2016-02-26 13:20:48 -0800982/**
983 * Creates storage space for the texture and fills it with texels.
984 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400985 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800986 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800987 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500988 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700989 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
990 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800991 * @param externalFormat The data format used for the external storage of the texture.
992 * @param externalType The type of the data used for the external storage of the texture.
993 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500994 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -0800995 * @param baseWidth The width of the texture's base mipmap level
996 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -0800997 */
Robert Phillips92de6312017-05-23 07:43:48 -0400998static bool allocate_and_populate_texture(GrPixelConfig config,
999 const GrGLInterface& interface,
1000 const GrGLCaps& caps,
1001 GrGLenum target,
1002 GrGLenum internalFormat,
1003 GrGLenum internalFormatForTexStorage,
1004 GrGLenum externalFormat,
1005 GrGLenum externalType,
Brian Salomon1047a492019-07-02 12:25:21 -04001006 const GrMipLevel texels[],
1007 int mipLevelCount,
1008 int baseWidth,
1009 int baseHeight,
Brian Salomonc38cfa92019-07-10 15:31:48 -04001010 bool* changedUnpackRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001011 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -08001012
Brian Salomon1047a492019-07-02 12:25:21 -04001013 if (caps.configSupportsTexStorage(config)) {
cblume790d5132016-02-29 11:13:29 -08001014 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001015 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -04001016 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -04001017 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001018 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001019 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001020 return false;
cblume790d5132016-02-29 11:13:29 -08001021 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04001022 size_t bpp = GrBytesPerPixel(config);
Robert Phillips590533f2017-07-11 14:22:35 -04001023 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001024 const void* currentMipData = texels[currentMipLevel].fPixels;
1025 if (currentMipData == nullptr) {
1026 continue;
1027 }
1028 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001029 const int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1030 const int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Brian Salomon1047a492019-07-02 12:25:21 -04001031
Brian Salomonc38cfa92019-07-10 15:31:48 -04001032 if (texels[currentMipLevel].fPixels) {
1033 const size_t trimRowBytes = currentWidth * bpp;
1034 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1035 if (rowBytes != trimRowBytes) {
1036 SkASSERT(caps.writePixelsRowBytesSupport());
1037 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1038 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1039 *changedUnpackRowLength = true;
1040 } else if (*changedUnpackRowLength) {
1041 SkASSERT(caps.writePixelsRowBytesSupport());
1042 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1043 *changedUnpackRowLength = false;
1044 }
Brian Salomon1047a492019-07-02 12:25:21 -04001045 }
cblume790d5132016-02-29 11:13:29 -08001046
1047 GR_GL_CALL(&interface,
1048 TexSubImage2D(target,
1049 currentMipLevel,
1050 0, // left
1051 0, // top
1052 currentWidth,
1053 currentHeight,
1054 externalFormat, externalType,
1055 currentMipData));
1056 }
bsalomone699d0c2016-03-09 06:25:15 -08001057 return true;
cblume790d5132016-02-29 11:13:29 -08001058 }
1059 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001060 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001061 GL_ALLOC_CALL(&interface,
1062 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001063 0,
cblume790d5132016-02-29 11:13:29 -08001064 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001065 baseWidth,
1066 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001067 0, // border
1068 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001069 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001070 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001071 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001072 return false;
1073 }
1074 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04001075 size_t bpp = GrBytesPerPixel(config);
Robert Phillips590533f2017-07-11 14:22:35 -04001076 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001077 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001078 const int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1079 const int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Brian Salomon1047a492019-07-02 12:25:21 -04001080
Brian Salomonc38cfa92019-07-10 15:31:48 -04001081 if (texels[currentMipLevel].fPixels) {
1082 const size_t trimRowBytes = currentWidth * bpp;
1083 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1084 if (rowBytes != trimRowBytes) {
1085 SkASSERT(caps.writePixelsRowBytesSupport());
1086 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1087 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1088 *changedUnpackRowLength = true;
1089 } else if (*changedUnpackRowLength) {
1090 SkASSERT(caps.writePixelsRowBytesSupport());
1091 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1092 *changedUnpackRowLength = false;
1093 }
Brian Salomon1047a492019-07-02 12:25:21 -04001094 }
1095
Greg Daniel8b059bd2017-09-28 20:46:45 +00001096 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001097 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1098 // This will allocate texture memory which we can later populate.
1099 GL_ALLOC_CALL(&interface,
1100 TexImage2D(target,
1101 currentMipLevel,
1102 internalFormat,
1103 currentWidth,
1104 currentHeight,
1105 0, // border
1106 externalFormat, externalType,
1107 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001108 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001109 if (error != GR_GL_NO_ERROR) {
1110 return false;
1111 }
cblume790d5132016-02-29 11:13:29 -08001112 }
cblume55f2d2d2016-02-26 13:20:48 -08001113 }
1114 }
bsalomone699d0c2016-03-09 06:25:15 -08001115 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001116}
1117
1118/**
cblume55f2d2d2016-02-26 13:20:48 -08001119 * After a texture is created, any state which was altered during its creation
1120 * needs to be restored.
1121 *
1122 * @param interface The GL interface to use.
1123 * @param caps The capabilities of the GL device.
1124 * @param restoreGLRowLength Should the row length unpacking be restored?
1125 * @param glFlipY Did GL flip the texture vertically?
1126 */
1127static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001128 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001129 if (restoreGLRowLength) {
Brian Salomon1047a492019-07-02 12:25:21 -04001130 SkASSERT(caps.writePixelsRowBytesSupport());
cblume55f2d2d2016-02-26 13:20:48 -08001131 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1132 }
cblume55f2d2d2016-02-26 13:20:48 -08001133}
1134
Brian Osman9b560242017-09-05 15:34:52 -04001135void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001136 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1137 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1138 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1139 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001140 }
Brian Osman9b560242017-09-05 15:34:52 -04001141}
1142
Brian Salomonc320b152018-02-20 14:05:36 -05001143// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1144// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001145bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1146 UploadType uploadType, int left, int top, int width, int height,
1147 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001148 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001149 // If we're uploading compressed data then we should be using uploadCompressedTexData
1150 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1151
Brian Salomond1eaf492017-05-18 10:02:08 -04001152 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001153 SkDEBUGCODE(
1154 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1155 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1156 SkASSERT(bounds.contains(subRect));
1157 )
Robert Phillips590533f2017-07-11 14:22:35 -04001158 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001159 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001160
Brian Osman9b560242017-09-05 15:34:52 -04001161 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001162
cblume55f2d2d2016-02-26 13:20:48 -08001163 const GrGLInterface* interface = this->glInterface();
1164 const GrGLCaps& caps = this->glCaps();
1165
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001166 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001167
1168 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001169 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001170 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001171
bsalomon5b30c6f2015-12-17 14:17:34 -08001172 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001173 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001174 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001175 GrGLenum externalFormat;
1176 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001177 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1178 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001179 return false;
1180 }
brianosman81a84852016-09-12 09:05:14 -07001181 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001182 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001183
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001184 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001185 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001186 * because our srcData has extra bytes past each row. If so, we need
1187 * to trim those off here, since GL ES may not let us specify
1188 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001189 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001190 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001191
1192 // in case we need a temporary, trimmed copy of the src pixels
1193 SkAutoSMalloc<128 * 128> tempStorage;
1194
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001195 if (mipMapsStatus) {
1196 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001197 }
1198
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001199 if (mipMapsStatus && mipLevelCount <= 1) {
1200 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001201 }
bsalomone699d0c2016-03-09 06:25:15 -08001202
Robert Phillips590533f2017-07-11 14:22:35 -04001203 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001204 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001205 }
cblume55f2d2d2016-02-26 13:20:48 -08001206
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001207 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001208 if (kNewTexture_UploadType == uploadType) {
1209 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001210 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001211 texConfig, *interface, caps, target, internalFormat,
Brian Salomon1047a492019-07-02 12:25:21 -04001212 internalFormatForTexStorage, externalFormat, externalType, texels,
1213 mipLevelCount, width, height, &restoreGLRowLength);
Brian Salomond1eaf492017-05-18 10:02:08 -04001214 } else {
1215 succeeded = false;
1216 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001217 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001218 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Brian Salomon1047a492019-07-02 12:25:21 -04001219 if (!texels[currentMipLevel].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001220 continue;
1221 }
cblume55f2d2d2016-02-26 13:20:48 -08001222 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomon1047a492019-07-02 12:25:21 -04001223 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1224 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1225 const size_t trimRowBytes = currentWidth * bpp;
1226 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1227
1228 if (caps.writePixelsRowBytesSupport() && rowBytes != trimRowBytes) {
1229 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1230 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1231 restoreGLRowLength = true;
1232 }
cblume55f2d2d2016-02-26 13:20:48 -08001233
1234 GL_CALL(TexSubImage2D(target,
1235 currentMipLevel,
1236 left, top,
1237 currentWidth,
1238 currentHeight,
1239 externalFormat, externalType,
Brian Salomon1047a492019-07-02 12:25:21 -04001240 texels[currentMipLevel].fPixels));
cblume55f2d2d2016-02-26 13:20:48 -08001241 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001242 }
1243
Brian Salomona9b04b92018-06-01 15:04:28 -04001244 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001245
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001246 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001247}
1248
Brian Salomonbb8dde82019-06-27 10:52:13 -04001249GrGLenum GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType, int width,
1250 int height, GrGLenum target, const void* data) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001251 const GrGLCaps& caps = this->glCaps();
1252
Brian Salomonbb8dde82019-06-27 10:52:13 -04001253 GrPixelConfig config = GrCompressionTypePixelConfig(compressionType);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001254 // We only need the internal format for compressed 2D textures.
1255 GrGLenum internalFormat;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001256 if (!caps.getCompressedTexImageFormats(config, &internalFormat)) {
1257 return 0;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001258 }
1259
Brian Salomon1047a492019-07-02 12:25:21 -04001260 bool useTexStorage = caps.configSupportsTexStorage(config);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001261
1262 static constexpr int kMipLevelCount = 1;
1263
1264 // Make sure that the width and height that we pass to OpenGL
1265 // is a multiple of the block size.
1266 size_t dataSize = GrCompressedDataSize(compressionType, width, height);
1267
1268 if (useTexStorage) {
1269 // We never resize or change formats of textures.
1270 GL_ALLOC_CALL(this->glInterface(),
1271 TexStorage2D(target, kMipLevelCount, internalFormat, width, height));
1272 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1273 if (error != GR_GL_NO_ERROR) {
1274 return 0;
1275 }
1276 GL_CALL(CompressedTexSubImage2D(target,
1277 0, // level
1278 0, // left
1279 0, // top
1280 width,
1281 height,
1282 internalFormat,
1283 SkToInt(dataSize),
1284 data));
1285 } else {
1286 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1287 0, // level
1288 internalFormat,
1289 width,
1290 height,
1291 0, // border
1292 SkToInt(dataSize),
1293 data));
1294
1295 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1296 if (error != GR_GL_NO_ERROR) {
1297 return 0;
Greg Kaiser73bfb892019-02-11 09:03:41 -08001298 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001299 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001300 return internalFormat;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001301}
1302
bsalomon424cc262015-05-22 10:37:30 -07001303static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001304 int sampleCount,
1305 GrGLenum format,
1306 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001307 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001308 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001309 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001310 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001311 GL_ALLOC_CALL(ctx.interface(),
1312 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1313 sampleCount,
1314 format,
1315 width, height));
1316 break;
1317 case GrGLCaps::kES_Apple_MSFBOType:
1318 GL_ALLOC_CALL(ctx.interface(),
1319 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1320 sampleCount,
1321 format,
1322 width, height));
1323 break;
1324 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1325 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1326 GL_ALLOC_CALL(ctx.interface(),
1327 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1328 sampleCount,
1329 format,
1330 width, height));
1331 break;
1332 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001333 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001334 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001335 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001336 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001337}
1338
egdanielb0e1be22015-04-22 13:27:39 -07001339bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001340 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001341 GrGLRenderTarget::IDDesc* idDesc) {
1342 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001343 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001344 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001345 idDesc->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001346
1347 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001348
bsalomona11e5fc2015-12-18 07:59:41 -08001349 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001350
Brian Salomonbdecacf2018-02-02 20:32:49 -05001351 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001352 goto FAILED;
1353 }
1354
egdanield803f272015-03-18 13:01:52 -07001355 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1356 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001357 goto FAILED;
1358 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001359
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001360 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1361 // the texture bound to the other. The exception is the IMG multisample extension. With this
1362 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1363 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001364 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001365 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001366 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001367 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001368 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001369 goto FAILED;
1370 }
Greg Daniel24b67662019-07-15 16:55:53 -04001371 GrGLFormat glFormat =
1372 GrGLFormatFromGLEnum(this->glCaps().configSizedInternalFormat(desc.fConfig));
1373 colorRenderbufferFormat =
1374 this->glCaps().getRenderbufferInternalFormat(glFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001375 } else {
egdanield803f272015-03-18 13:01:52 -07001376 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001377 }
1378
egdanield803f272015-03-18 13:01:52 -07001379 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001380 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001381 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001382 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001383 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001384 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001385 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001386 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001387 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001388 goto FAILED;
1389 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001390 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001391 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001392 GR_GL_COLOR_ATTACHMENT0,
1393 GR_GL_RENDERBUFFER,
1394 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001395 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001396 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1397 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1398 goto FAILED;
1399 }
bsalomon424cc262015-05-22 10:37:30 -07001400 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001401 }
1402 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001403 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001404
Brian Salomonbdecacf2018-02-02 20:32:49 -05001405 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001406 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001407 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001408 texInfo.fTarget,
1409 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001410 } else {
egdanield803f272015-03-18 13:01:52 -07001411 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001412 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001413 texInfo.fTarget,
1414 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001415 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001416 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001417 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001418 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1419 goto FAILED;
1420 }
bsalomon424cc262015-05-22 10:37:30 -07001421 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001422 }
1423
1424 return true;
1425
1426FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001427 if (idDesc->fMSColorRenderbufferID) {
1428 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001429 }
egdanield803f272015-03-18 13:01:52 -07001430 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001431 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001432 }
egdanield803f272015-03-18 13:01:52 -07001433 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001434 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001435 }
1436 return false;
1437}
1438
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001439// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001440static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001441// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001442 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001443}
1444
Brian Salomone2826ab2019-06-04 15:58:31 -04001445static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
1446 const GrGLInterface* interface, const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001447 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1448 // drivers have a bug where an FBO won't be complete if it includes a
1449 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001450 GrGLTextureParameters::SamplerOverriddenState state;
1451 state.fMinFilter = GR_GL_NEAREST;
1452 state.fMagFilter = GR_GL_NEAREST;
1453 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1454 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
1455 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1456 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1457 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1458 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
1459 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001460}
1461
Robert Phillips67d52cf2017-06-05 13:38:13 -04001462sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001463 GrRenderable renderable,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001464 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001465 const GrMipLevel texels[],
1466 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001467 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001468 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001469 return return_null_texture();
1470 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001471
bsalomonb15b4c12014-10-29 12:41:57 -07001472 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001473 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001474 GrMipMapsStatus mipMapsStatus;
Brian Salomone2826ab2019-06-04 15:58:31 -04001475 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001476 if (!this->createTextureImpl(desc, &idDesc.fInfo, renderable, &initialState, texels,
1477 mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001478 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001479 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001480
Robert Phillips67d52cf2017-06-05 13:38:13 -04001481 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001482 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001483 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001484 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001485 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001486
kkinnunen2e6055b2016-04-22 01:48:29 -07001487 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001488 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001489 return return_null_texture();
1490 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001491 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001492 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001493 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001494 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001495 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001496 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001497 // The non-sampler params are still at their default values.
1498 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1499 fResetTimestampForTextureParameters);
Brian Salomona3e29962019-07-16 11:52:08 -04001500 bool clearLevelsWithoutData =
1501 this->caps()->shouldInitializeTextures() && this->glCaps().clearTextureSupport();
1502
1503 if (clearLevelsWithoutData) {
1504 static constexpr uint32_t kZero = 0;
1505 int levelCnt = SkTMax(1, tex->texturePriv().maxMipMapLevel());
1506 for (int i = 0; i < levelCnt; ++i) {
1507 if (i >= mipLevelCount || !texels[i].fPixels) {
1508 GL_CALL(ClearTexImage(tex->textureID(), i, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE,
1509 &kZero));
1510 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001511 }
1512 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001513 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514}
1515
Brian Salomonbb8dde82019-06-27 10:52:13 -04001516sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height,
1517 SkImage::CompressionType compression,
1518 SkBudgeted budgeted, const void* data) {
1519 GrGLTexture::IDDesc idDesc;
1520 GrGLTextureParameters::SamplerOverriddenState initialState;
1521 if (!this->createCompressedTextureImpl(&idDesc.fInfo, width, height, compression, &initialState,
1522 data)) {
1523 return nullptr;
1524 }
1525 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
1526
1527 GrSurfaceDesc desc;
1528 desc.fConfig = GrCompressionTypePixelConfig(compression);
1529 desc.fWidth = width;
1530 desc.fHeight = height;
1531 desc.fSampleCnt = 1;
1532 auto tex =
1533 sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, GrMipMapsStatus::kNotAllocated);
1534 // The non-sampler params are still at their default values.
1535 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1536 fResetTimestampForTextureParameters);
1537 return std::move(tex);
1538}
1539
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001540namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001541
egdaniel8dc7c3a2015-04-16 11:22:42 -07001542const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001543
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001544void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001545 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001546
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001547 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001548 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001549 (kUnknownBitCount == format->fTotalBits));
1550 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001551 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1553 (GrGLint*)&format->fStencilBits);
1554 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001555 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001556 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1557 (GrGLint*)&format->fTotalBits);
1558 format->fTotalBits += format->fStencilBits;
1559 } else {
1560 format->fTotalBits = format->fStencilBits;
1561 }
1562 }
1563}
1564}
1565
Brian Salomon5043f1f2019-07-11 21:27:54 -04001566int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001567 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001568 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1569
1570 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001571 // Default to unsupported, set this if we find a stencil format that works.
1572 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001573
egdanielff1d5472015-09-10 08:37:20 -07001574 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001575 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001576 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001577 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001578 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1579 GR_GL_TEXTURE_MAG_FILTER,
1580 GR_GL_NEAREST));
1581 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1582 GR_GL_TEXTURE_MIN_FILTER,
1583 GR_GL_NEAREST));
1584 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1585 GR_GL_TEXTURE_WRAP_S,
1586 GR_GL_CLAMP_TO_EDGE));
1587 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1588 GR_GL_TEXTURE_WRAP_T,
1589 GR_GL_CLAMP_TO_EDGE));
1590
Brian Salomonf6da1462019-07-11 14:21:59 -04001591 GrGLenum internalFormat = this->glCaps().getTexImageInternalFormat(format);
1592 GrGLenum externalFormat = this->glCaps().getBaseInternalFormat(format);
1593 GrGLenum externalType = this->glCaps().getFormatDefaultExternalType(format);
Greg Kaiser98ac0f92019-07-12 05:53:12 -07001594 if (!internalFormat || !externalFormat || !externalType) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001595 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001596 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001597
Brian Osman9b560242017-09-05 15:34:52 -04001598 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001599 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1600 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001601 0,
bsalomon76148af2016-01-12 11:13:47 -08001602 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001603 kSize,
1604 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001605 0,
bsalomon76148af2016-01-12 11:13:47 -08001606 externalFormat,
1607 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001608 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001609 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001610 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001611 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001612 }
1613
1614 // unbind the texture from the texture unit before binding it to the frame buffer
1615 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1616
1617 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001618 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001619 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001620 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001621 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001622 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1623 GR_GL_COLOR_ATTACHMENT0,
1624 GR_GL_TEXTURE_2D,
1625 colorID,
1626 0));
bsalomon30447372015-12-21 09:03:05 -08001627 GrGLuint sbRBID = 0;
1628 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001629
1630 // look over formats till I find a compatible one
1631 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001632 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001633 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001634 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1635 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1636 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1637 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1638 sFmt.fInternalFormat,
1639 kSize, kSize));
1640 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001641 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001642 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001643 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001644 if (sFmt.fPacked) {
1645 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1646 GR_GL_DEPTH_ATTACHMENT,
1647 GR_GL_RENDERBUFFER, sbRBID));
1648 } else {
1649 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1650 GR_GL_DEPTH_ATTACHMENT,
1651 GR_GL_RENDERBUFFER, 0));
1652 }
1653 GrGLenum status;
1654 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1655 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1656 firstWorkingStencilFormatIndex = i;
1657 break;
1658 }
egdanielff1d5472015-09-10 08:37:20 -07001659 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1660 GR_GL_STENCIL_ATTACHMENT,
1661 GR_GL_RENDERBUFFER, 0));
1662 if (sFmt.fPacked) {
1663 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1664 GR_GL_DEPTH_ATTACHMENT,
1665 GR_GL_RENDERBUFFER, 0));
1666 }
egdanielff1d5472015-09-10 08:37:20 -07001667 }
1668 }
bsalomon30447372015-12-21 09:03:05 -08001669 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001670 }
1671 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001672 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1673 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001674 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001675 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001676 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001677}
1678
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001679bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
1680 GrRenderable renderable,
Brian Salomone2826ab2019-06-04 15:58:31 -04001681 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomondc829942018-10-23 16:07:24 -04001682 const GrMipLevel texels[], int mipLevelCount,
1683 GrMipMapsStatus* mipMapsStatus) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001684 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
erikchen9a1ed5d2016-02-10 16:32:34 -08001685 info->fID = 0;
1686 info->fTarget = GR_GL_TEXTURE_2D;
1687 GL_CALL(GenTextures(1, &(info->fID)));
1688
1689 if (!info->fID) {
1690 return false;
1691 }
1692
Robert Phillips8043f322019-05-31 08:11:36 -04001693 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
1694
Brian Salomond978b902019-02-07 15:09:18 -05001695 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001696
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001697 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001698 // provides a hint about how this texture will be used
1699 GL_CALL(TexParameteri(info->fTarget,
1700 GR_GL_TEXTURE_USAGE,
1701 GR_GL_FRAMEBUFFER_ATTACHMENT));
1702 }
1703
Brian Salomone2826ab2019-06-04 15:58:31 -04001704 *initialState = set_initial_texture_params(this->glInterface(), *info);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001705
Brian Salomonbb8dde82019-06-27 10:52:13 -04001706 if (!this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1707 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
1708 texels, mipLevelCount, mipMapsStatus)) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001709 GL_CALL(DeleteTextures(1, &(info->fID)));
1710 return false;
1711 }
1712 return true;
1713}
1714
Brian Salomonbb8dde82019-06-27 10:52:13 -04001715bool GrGLGpu::createCompressedTextureImpl(
1716 GrGLTextureInfo* info, int width, int height, SkImage::CompressionType compression,
1717 GrGLTextureParameters::SamplerOverriddenState* initialState, const void* data) {
1718 info->fID = 0;
1719 GL_CALL(GenTextures(1, &info->fID));
1720 if (!info->fID) {
1721 return false;
1722 }
1723
1724 info->fTarget = GR_GL_TEXTURE_2D;
1725 this->bindTextureToScratchUnit(info->fTarget, info->fID);
1726
1727 *initialState = set_initial_texture_params(this->glInterface(), *info);
1728
1729 info->fFormat = this->uploadCompressedTexData(compression, width, height, info->fTarget, data);
1730 if (!info->fFormat) {
1731 GL_CALL(DeleteTextures(1, &info->fID));
1732 return false;
1733 }
1734 return true;
1735}
1736
Chris Daltoneffee202019-07-01 22:28:03 -06001737GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1738 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001739 SkASSERT(width >= rt->width());
1740 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001741
egdaniel8dc7c3a2015-04-16 11:22:42 -07001742 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001743
Brian Salomon5043f1f2019-07-11 21:27:54 -04001744 auto rtFormat = GrGLBackendFormatToGLFormat(rt->backendFormat());
Brian Salomonf6da1462019-07-11 14:21:59 -04001745 int sIdx = this->getCompatibleStencilIndex(rtFormat);
bsalomon62a627b2015-12-17 09:50:47 -08001746 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001747 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001748 }
egdanielff1d5472015-09-10 08:37:20 -07001749
1750 if (!sbDesc.fRenderbufferID) {
1751 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1752 }
1753 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001754 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001755 }
1756 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1757 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1758 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1759 // we do this "if" so that we don't call the multisample
1760 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001761 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001762 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001763 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001764 sFmt.fInternalFormat,
1765 width, height));
1766 } else {
1767 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1768 sFmt.fInternalFormat,
1769 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001770 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001771 }
1772 fStats.incStencilAttachmentCreates();
1773 // After sized formats we attempt an unsized format and take
1774 // whatever sizes GL gives us. In that case we query for the size.
1775 GrGLStencilAttachment::Format format = sFmt;
1776 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001777 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1778 sbDesc,
1779 width,
1780 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001781 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001782 format);
1783 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001784}
1785
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001786////////////////////////////////////////////////////////////////////////////////
1787
Brian Salomondbf70722019-02-07 11:31:24 -05001788sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1789 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001790 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001791}
1792
Greg Danielacd66b42019-05-22 16:29:12 -04001793void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001794 GrSurfaceOrigin rtOrigin) {
1795 if (scissorState.enabled()) {
1796 GrGLIRect scissor;
Greg Danielacd66b42019-05-22 16:29:12 -04001797 scissor.setRelativeTo(rtHeight, scissorState.rect(), rtOrigin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001798 // if the scissor fully contains the viewport then we fall through and
1799 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001800 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001801 if (fHWScissorSettings.fRect != scissor) {
1802 scissor.pushToGLScissor(this->glInterface());
1803 fHWScissorSettings.fRect = scissor;
1804 }
1805 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1806 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1807 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001808 }
1809 return;
1810 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001812
1813 // See fall through note above
1814 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001815}
1816
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001817void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001818 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001819#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001820 typedef GrWindowRectsState::Mode Mode;
1821 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1822 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001823
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001824 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001825 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001826 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001827 }
1828
csmartdalton7535f412016-08-23 06:51:00 -07001829 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1830 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001831 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1832 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001833
csmartdalton28341fa2016-08-17 10:00:21 -07001834 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001835 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001836 for (int i = 0; i < numWindows; ++i) {
Greg Danielacd66b42019-05-22 16:29:12 -04001837 glwindows[i].setRelativeTo(rt->height(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001838 }
1839
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001840 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001841 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001842
Greg Danielacd66b42019-05-22 16:29:12 -04001843 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001844#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001845}
1846
1847void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001848#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001849 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001850 return;
1851 }
1852 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001853 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001854#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001855}
1856
Brian Salomonf7232642018-09-19 08:58:08 -04001857void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1858 const GrPrimitiveProcessor& primProc,
1859 const GrPipeline& pipeline,
1860 const GrTextureProxy* const primProcTextures[],
1861 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001862 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001863 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001864 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1865 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1866 tex->texturePriv().mipMapsAreDirty()) {
1867 SkASSERT(this->caps()->mipMapSupport());
1868 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001869 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1870 } else if (auto* rt = tex->asRenderTarget()) {
1871 if (rt->needsResolve()) {
1872 this->resolveRenderTarget(rt);
1873 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001874 }
1875 };
1876
Brian Salomonf7232642018-09-19 08:58:08 -04001877 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1878 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
1879 GrTexture* texture = primProcTextures[tex]->peekTexture();
1880 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
1881 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001882 }
1883
1884 GrFragmentProcessor::Iter iter(pipeline);
1885 while (const GrFragmentProcessor* fp = iter.next()) {
1886 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
1887 const auto& textureSampler = fp->textureSampler(i);
1888 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
1889 }
1890 }
1891}
1892
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001893bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1894 GrSurfaceOrigin origin,
1895 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04001896 const GrPipeline& pipeline,
1897 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04001898 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
1899 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07001900 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04001901 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
1902 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
1903 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
1904 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
1905 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
1906 numPrimProcTextureSets = dynamicStateArraysLength;
1907 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
1908 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
1909 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00001910 }
Greg Daniel9a51a862018-11-30 10:18:14 -05001911
1912 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
1913
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001914 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
1915 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05001916 pipeline, willDrawPoints));
1917 if (!program) {
1918 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1919 return false;
1920 }
Brian Salomonf7232642018-09-19 08:58:08 -04001921 this->resolveAndGenerateMipMapsForProcessorTextures(
1922 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07001923
egdaniel080e6732014-12-22 07:35:52 -08001924 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07001925 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08001926
egdaniel080e6732014-12-22 07:35:52 -08001927 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08001928
Brian Salomon802cb312018-06-08 18:05:20 -04001929 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001930
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001931 // Swizzle the blend to match what the shader will output.
Greg Daniel2c3398d2019-06-19 11:58:01 -04001932 this->flushBlend(blendInfo, pipeline.outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001933
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001934 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
1935 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08001936
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001937 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07001938 GrStencilSettings stencil;
1939 if (pipeline.isStencilEnabled()) {
1940 // TODO: attach stencil and create settings during render target flush.
1941 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1942 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
1943 glRT->renderTargetPriv().numStencilBits());
1944 }
Chris Dalton71713f62019-04-18 12:41:03 -06001945 this->flushStencil(stencil, origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001946 if (pipeline.isScissorEnabled()) {
1947 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
1948 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Greg Danielacd66b42019-05-22 16:29:12 -04001949 this->flushScissor(state, glRT->width(), glRT->height(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001950 } else {
1951 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001952 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001953 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07001954 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08001955
1956 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001957 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001958 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001959
1960 return true;
1961}
1962
Brian Salomon802cb312018-06-08 18:05:20 -04001963void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1964 if (!program) {
1965 fHWProgram.reset();
1966 fHWProgramID = 0;
1967 return;
1968 }
1969 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1970 if (program == fHWProgram) {
1971 return;
1972 }
1973 auto id = program->programID();
1974 SkASSERT(id);
1975 GL_CALL(UseProgram(id));
1976 fHWProgram = std::move(program);
1977 fHWProgramID = id;
1978}
1979
1980void GrGLGpu::flushProgram(GrGLuint id) {
1981 SkASSERT(id);
1982 if (fHWProgramID == id) {
1983 SkASSERT(!fHWProgram);
1984 return;
1985 }
1986 fHWProgram.reset();
1987 GL_CALL(UseProgram(id));
1988 fHWProgramID = id;
1989}
1990
1991void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001992 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001993 int baseVertex,
1994 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001995 int baseInstance,
1996 GrPrimitiveRestart enablePrimitiveRestart) {
1997 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001998
cdaltone2e71c22016-04-07 18:13:29 -07001999 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002000 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002001 SkASSERT(indexBuffer->isCpuBuffer() ||
2002 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002003 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002004 } else {
2005 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002006 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002007
Brian Salomon92be2f72018-06-19 14:33:47 -04002008 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2009 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002010
Brian Salomon802cb312018-06-08 18:05:20 -04002011 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002012 SkASSERT(vertexBuffer);
2013 SkASSERT(vertexBuffer->isCpuBuffer() ||
2014 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2015 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002016 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2017 const auto& attrib = fHWProgram->vertexAttribute(i);
2018 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002019 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2020 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002021 }
Chris Dalton1d616352017-05-31 12:51:23 -06002022 }
Brian Salomon802cb312018-06-08 18:05:20 -04002023 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002024 SkASSERT(instanceBuffer);
2025 SkASSERT(instanceBuffer->isCpuBuffer() ||
2026 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2027 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002028 int attribIdx = fHWProgram->numVertexAttributes();
2029 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2030 const auto& attrib = fHWProgram->instanceAttribute(i);
2031 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002032 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2033 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2034 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002035 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002036 }
2037}
2038
Brian Salomonae64c192019-02-05 09:41:37 -05002039GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002040 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002041
cdaltone2e71c22016-04-07 18:13:29 -07002042 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002043 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002044 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002045 }
cdaltone2e71c22016-04-07 18:13:29 -07002046
Brian Salomonae64c192019-02-05 09:41:37 -05002047 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002048 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002049 if (!bufferState->fBufferZeroKnownBound) {
2050 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2051 bufferState->fBufferZeroKnownBound = true;
2052 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002053 }
Brian Salomondbf70722019-02-07 11:31:24 -05002054 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2055 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002056 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2057 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2058 bufferState->fBufferZeroKnownBound = false;
2059 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002060 }
2061
Brian Salomonae64c192019-02-05 09:41:37 -05002062 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002063}
Brian Salomond818ebf2018-07-02 14:08:49 +00002064void GrGLGpu::disableScissor() {
2065 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2066 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2067 fHWScissorSettings.fEnabled = kNo_TriState;
2068 return;
2069 }
2070}
2071
Brian Osman9a9baae2018-11-05 15:06:26 -05002072void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002073 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002074 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002075 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002076 SkASSERT(!this->caps()->performColorClearsAsDraws());
2077 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002078
Brian Salomon43f8bf02017-10-18 08:33:29 -04002079 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002080
Brian Salomon43f8bf02017-10-18 08:33:29 -04002081 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2082
Brian Salomond818ebf2018-07-02 14:08:49 +00002083 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002084 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002085 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002086 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002087 }
Greg Danielacd66b42019-05-22 16:29:12 -04002088 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002089 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002090 this->flushColorWrite(true);
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002091
2092 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2093 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2094 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2095 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2096 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2097 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2098 }
2099 this->flushClearColor(r, g, b, a);
2100
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002101 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002102}
2103
Robert Phillips95214472017-08-08 18:00:03 -04002104void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002105 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2106
Robert Phillips95214472017-08-08 18:00:03 -04002107 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002108 return;
2109 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002110
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002111 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2112 // this should only be called internally when we know we have a
2113 // stencil buffer.
2114 SkASSERT(sb);
Robert Phillipscb2e2352017-08-30 16:44:40 -04002115
bsalomonb0bd4f62014-09-03 07:19:50 -07002116 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002117 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002118
Brian Salomond818ebf2018-07-02 14:08:49 +00002119 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002120 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002121
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002122 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002123 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002124 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002125 fHWStencilSettings.invalidate();
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002126 if (!clearValue) {
2127 sb->cleared();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002128 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002129}
2130
csmartdalton29df7602016-08-31 11:55:52 -07002131void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2132 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002133 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002134 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002135 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002136 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002137
egdaniel8dc7c3a2015-04-16 11:22:42 -07002138 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002139 // this should only be called internally when we know we have a
2140 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002141 SkASSERT(sb);
2142 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002143#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002144 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002145 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002146#else
2147 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002148 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002149 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002150 // changing the clip between stencil passes may or may not
2151 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002152 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002153#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002154 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002155 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002156 value = (1 << (stencilBitCount - 1));
2157 } else {
2158 value = 0;
2159 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002160 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002161 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002162
Greg Danielacd66b42019-05-22 16:29:12 -04002163 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002164 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002165
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002166 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002167 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002168 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002169 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002170}
2171
bsalomon1aa20292016-01-22 08:16:09 -08002172
Brian Salomone05ba5a2019-04-08 11:59:07 -04002173bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
2174 GrColorType dstColorType, void* offsetOrPtr,
Brian Salomon26de56e2019-04-10 12:14:26 -04002175 int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002176 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002177
bsalomone9573312016-01-25 14:33:25 -08002178 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002179 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002180 return false;
2181 }
2182
Brian Salomonc320b152018-02-20 14:05:36 -05002183 // TODO: Avoid this conversion by making GrGLCaps work with color types.
Greg Daniele877dce2019-07-11 10:52:43 -04002184 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType);
Brian Salomonc320b152018-02-20 14:05:36 -05002185
bsalomon76148af2016-01-12 11:13:47 -08002186 GrGLenum externalFormat;
2187 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002188 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002189 &externalType)) {
2190 return false;
2191 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002192
Brian Salomon71d9d842016-11-03 13:42:00 -04002193 if (renderTarget) {
2194 // resolve the render target if necessary
2195 switch (renderTarget->getResolveType()) {
2196 case GrGLRenderTarget::kCantResolve_ResolveType:
2197 return false;
2198 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002199 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002200 break;
2201 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002202 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002203 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002204 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002205 break;
2206 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002207 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002208 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002209 } else {
2210 // Use a temporary FBO.
Greg Danielacd66b42019-05-22 16:29:12 -04002211 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002212 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002213 }
2214
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002215 // the read rect is viewport-relative
2216 GrGLIRect readRect;
Greg Danielacd66b42019-05-22 16:29:12 -04002217 readRect.setRelativeTo(surface->height(), left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002218
Brian Salomona6948702018-06-01 15:33:20 -04002219 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002220 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002221 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002222 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002223 }
Brian Salomonc320b152018-02-20 14:05:36 -05002224 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002225
Brian Osman1348ed02018-09-12 09:08:39 -04002226 bool reattachStencil = false;
2227 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2228 renderTarget &&
2229 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002230 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002231 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2232 reattachStencil = true;
2233 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2234 GR_GL_RENDERBUFFER, 0));
2235 }
2236
Brian Salomone05ba5a2019-04-08 11:59:07 -04002237 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, readRect.fWidth, readRect.fHeight,
2238 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002239
2240 if (reattachStencil) {
2241 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2242 renderTarget->renderTargetPriv().getStencilAttachment());
2243 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2244 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2245 }
2246
Brian Salomon26de56e2019-04-10 12:14:26 -04002247 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002248 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002249 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2250 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002251
Brian Salomone05ba5a2019-04-08 11:59:07 -04002252 if (!renderTarget) {
2253 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2254 }
2255 return true;
2256}
2257
2258bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2259 GrColorType dstColorType, void* buffer, size_t rowBytes) {
2260 SkASSERT(surface);
2261
Brian Salomon26de56e2019-04-10 12:14:26 -04002262 int bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002263
Brian Salomon26de56e2019-04-10 12:14:26 -04002264 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2265 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002266
Brian Salomon1047a492019-07-02 12:25:21 -04002267 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002268 rowPixelWidth = width;
2269 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002270 SkASSERT(!(rowBytes % bytesPerPixel));
2271 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002272 }
Brian Salomon1047a492019-07-02 12:25:21 -04002273 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, buffer,
2274 rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002275}
2276
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002277GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002278 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002279 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002280 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002281 if (!fCachedRTCommandBuffer) {
2282 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2283 }
2284
2285 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2286 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002287}
2288
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002289GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2290 if (!fCachedTexCommandBuffer) {
2291 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2292 }
2293
2294 fCachedTexCommandBuffer->set(texture, origin);
2295 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002296}
2297
Brian Salomon1fabd512018-02-09 09:54:25 -05002298void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002299 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002300 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002301 this->didWriteToSurface(target, origin, &bounds);
2302}
bsalomon6ba6fa12015-03-04 11:57:37 -08002303
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002304void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2305 this->flushRenderTargetNoColorWrites(target);
2306 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002307}
2308
Brian Osman9aa30c62018-07-02 15:21:46 -04002309void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002310 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002311 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002312 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002313 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002314#ifdef SK_DEBUG
2315 // don't do this check in Chromium -- this is causing
2316 // lots of repeated command buffer flushes when the compositor is
2317 // rendering with Ganesh, which is really slow; even too slow for
2318 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002319 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002320 GrGLenum status;
2321 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2322 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2323 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2324 }
bsalomon160f24c2015-03-17 15:55:42 -07002325 }
egdanield803f272015-03-18 13:01:52 -07002326#endif
2327 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002328 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002329 }
2330
brianosman35b784d2016-05-05 11:52:53 -07002331 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002332 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002333 }
bsalomon083617b2016-02-12 12:10:14 -08002334}
bsalomona9909122016-01-23 10:41:40 -08002335
brianosman33f6b3f2016-06-02 05:49:21 -07002336void GrGLGpu::flushFramebufferSRGB(bool enable) {
2337 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2338 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2339 fHWSRGBFramebuffer = kYes_TriState;
2340 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2341 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2342 fHWSRGBFramebuffer = kNo_TriState;
2343 }
2344}
2345
Greg Danielacd66b42019-05-22 16:29:12 -04002346void GrGLGpu::flushViewport(int width, int height) {
2347 GrGLIRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002348 if (fHWViewport != viewport) {
2349 viewport.pushToGLViewport(this->glInterface());
2350 fHWViewport = viewport;
2351 }
2352}
2353
bsalomon@google.comd302f142011-03-03 13:54:13 +00002354#define SWAP_PER_DRAW 0
2355
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002356#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002357 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002358 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002359 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002360 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002361 void SwapBuf() {
2362 DWORD procID = GetCurrentProcessId();
2363 HWND hwnd = GetTopWindow(GetDesktopWindow());
2364 while(hwnd) {
2365 DWORD wndProcID = 0;
2366 GetWindowThreadProcessId(hwnd, &wndProcID);
2367 if(wndProcID == procID) {
2368 SwapBuffers(GetDC(hwnd));
2369 }
2370 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2371 }
2372 }
2373 #endif
2374#endif
2375
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002376void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2377 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002378 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002379 const GrPipeline::FixedDynamicState* fixedDynamicState,
2380 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002381 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002382 int meshCount) {
2383 this->handleDirtyContext();
2384
bsalomon2eda5b32016-09-21 10:53:24 -07002385 bool hasPoints = false;
2386 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002387 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002388 hasPoints = true;
2389 break;
2390 }
2391 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002392 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2393 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002394 return;
2395 }
ethannicholas22793252016-01-30 09:59:10 -08002396
Brian Salomonf7232642018-09-19 08:58:08 -04002397 bool dynamicScissor = false;
2398 bool dynamicPrimProcTextures = false;
2399 if (dynamicStateArrays) {
2400 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2401 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2402 }
2403 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002404 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2405 *this->caps())) {
2406 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002407 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002408
Brian Salomon49348902018-06-26 09:12:38 -04002409 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002410 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002411 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Greg Danielacd66b42019-05-22 16:29:12 -04002412 glRT->width(), glRT->height(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002413 }
Brian Salomonf7232642018-09-19 08:58:08 -04002414 if (dynamicPrimProcTextures) {
2415 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2416 m * primProc.numTextureSamplers();
2417 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2418 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002419 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002420 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002421 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2422 GL_CALL(Enable(GR_GL_CULL_FACE));
2423 GL_CALL(Disable(GR_GL_CULL_FACE));
2424 }
Brian Salomonf7232642018-09-19 08:58:08 -04002425 meshes[m].sendToGpu(this);
2426 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002427 }
ethannicholas22793252016-01-30 09:59:10 -08002428
bsalomon@google.comd302f142011-03-03 13:54:13 +00002429#if SWAP_PER_DRAW
2430 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002431 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002432 aglSwapBuffers(aglGetCurrentContext());
2433 int set_a_break_pt_here = 9;
2434 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002435 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002436 SwapBuf();
2437 int set_a_break_pt_here = 9;
2438 SwapBuf();
2439 #endif
2440#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002441}
2442
Chris Dalton3809bab2017-06-13 10:55:06 -06002443static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2444 switch (primitiveType) {
2445 case GrPrimitiveType::kTriangles:
2446 return GR_GL_TRIANGLES;
2447 case GrPrimitiveType::kTriangleStrip:
2448 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002449 case GrPrimitiveType::kPoints:
2450 return GR_GL_POINTS;
2451 case GrPrimitiveType::kLines:
2452 return GR_GL_LINES;
2453 case GrPrimitiveType::kLineStrip:
2454 return GR_GL_LINE_STRIP;
2455 case GrPrimitiveType::kLinesAdjacency:
2456 return GR_GL_LINES_ADJACENCY;
2457 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002458 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002459 return GR_GL_TRIANGLES;
2460}
2461
Brian Salomon802cb312018-06-08 18:05:20 -04002462void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2463 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002464 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002465 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002466 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002467 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2468 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002469 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002470 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2471 }
2472 fStats.incNumDraws();
2473}
2474
Brian Salomondbf70722019-02-07 11:31:24 -05002475static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2476 size_t baseOffset = baseIndex * sizeof(uint16_t);
2477 if (indexBuffer->isCpuBuffer()) {
2478 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2479 } else {
2480 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2481 }
2482}
2483
Brian Salomon802cb312018-06-08 18:05:20 -04002484void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002485 int indexCount, int baseIndex, uint16_t minIndexValue,
2486 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002487 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002488 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002489 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002490
Brian Salomon802cb312018-06-08 18:05:20 -04002491 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002492
2493 if (this->glCaps().drawRangeElementsSupport()) {
2494 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002495 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002496 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002497 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002498 }
2499 fStats.incNumDraws();
2500}
2501
Brian Salomon802cb312018-06-08 18:05:20 -04002502void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002503 int vertexCount, int baseVertex,
2504 const GrBuffer* instanceBuffer, int instanceCount,
2505 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002506 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002507 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002508 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002509 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2510 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002511 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2512 SkTMin(instanceCount - i, maxInstances)));
2513 fStats.incNumDraws();
2514 }
Chris Dalton1d616352017-05-31 12:51:23 -06002515}
2516
Brian Salomon802cb312018-06-08 18:05:20 -04002517void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002518 const GrBuffer* indexBuffer, int indexCount,
2519 int baseIndex, const GrBuffer* vertexBuffer,
2520 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002521 int instanceCount, int baseInstance,
2522 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002523 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002524 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002525 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2526 for (int i = 0; i < instanceCount; i += maxInstances) {
2527 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2528 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002529 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002530 SkTMin(instanceCount - i, maxInstances)));
2531 fStats.incNumDraws();
2532 }
Chris Dalton1d616352017-05-31 12:51:23 -06002533}
2534
Brian Salomon1fabd512018-02-09 09:54:25 -05002535void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002536 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002537 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002538 // Some extensions automatically resolves the texture when it is read.
2539 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002540 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002541 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002542 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2543 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2544
egdanield803f272015-03-18 13:01:52 -07002545 // make sure we go through flushRenderTarget() since we've modified
2546 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002547 fHWBoundRenderTargetUniqueID.makeInvalid();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002548 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002549 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2550 // surface. Choose kTopLeft so no adjustments are made
2551 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002552 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002553 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002554 GrScissorState scissorState;
2555 scissorState.set(dirtyRect);
Greg Danielacd66b42019-05-22 16:29:12 -04002556 this->flushScissor(scissorState, rt->width(), rt->height(), kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002557 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002558 GL_CALL(ResolveMultisampleFramebuffer());
2559 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002560 int l, b, r, t;
2561 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2562 this->glCaps().blitFramebufferSupportFlags()) {
2563 l = 0;
2564 b = 0;
2565 r = target->width();
2566 t = target->height();
2567 } else {
2568 GrGLIRect rect;
Greg Danielacd66b42019-05-22 16:29:12 -04002569 rect.setRelativeTo(rt->height(), dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002570 l = rect.fLeft;
2571 b = rect.fBottom;
2572 r = rect.fLeft + rect.fWidth;
2573 t = rect.fBottom + rect.fHeight;
2574 }
derekf8c8f71a2014-09-16 06:24:57 -07002575
2576 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002577 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002578 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002579 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002580 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002581 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002582 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002583 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002584 }
2585}
2586
bsalomon@google.com411dad02012-06-05 20:24:20 +00002587namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002588
bsalomon@google.com411dad02012-06-05 20:24:20 +00002589
2590GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002591 static const GrGLenum gTable[kGrStencilOpCount] = {
2592 GR_GL_KEEP, // kKeep
2593 GR_GL_ZERO, // kZero
2594 GR_GL_REPLACE, // kReplace
2595 GR_GL_INVERT, // kInvert
2596 GR_GL_INCR_WRAP, // kIncWrap
2597 GR_GL_DECR_WRAP, // kDecWrap
2598 GR_GL_INCR, // kIncClamp
2599 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002600 };
cdalton93a379b2016-05-11 13:58:08 -07002601 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2602 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2603 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2604 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2605 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2606 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2607 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2608 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2609 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2610 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002611}
2612
2613void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002614 const GrStencilSettings::Face& face,
2615 GrGLenum glFace) {
2616 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2617 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2618 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002619
cdalton93a379b2016-05-11 13:58:08 -07002620 GrGLint ref = face.fRef;
2621 GrGLint mask = face.fTestMask;
2622 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002623
2624 if (GR_GL_FRONT_AND_BACK == glFace) {
2625 // we call the combined func just in case separate stencil is not
2626 // supported.
2627 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2628 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002629 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002630 } else {
2631 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2632 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002633 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002634 }
2635}
2636}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002637
Chris Dalton71713f62019-04-18 12:41:03 -06002638void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002639 if (stencilSettings.isDisabled()) {
2640 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002641 } else if (fHWStencilSettings != stencilSettings ||
2642 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002643 if (kYes_TriState != fHWStencilTestEnabled) {
2644 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002645
csmartdaltonc7d85332016-10-26 10:13:46 -07002646 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002647 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002648 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002649 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2650 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002651 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002652 set_gl_stencil(
2653 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002654 }
joshualitta58fe352014-10-27 08:39:00 -07002655 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002656 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002657 }
2658}
2659
csmartdaltonc7d85332016-10-26 10:13:46 -07002660void GrGLGpu::disableStencil() {
2661 if (kNo_TriState != fHWStencilTestEnabled) {
2662 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002663
csmartdaltonc7d85332016-10-26 10:13:46 -07002664 fHWStencilTestEnabled = kNo_TriState;
2665 fHWStencilSettings.invalidate();
2666 }
2667}
2668
Chris Dalton4c56b032019-03-04 12:28:42 -07002669void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002670 // rt is only optional if useHWAA is false.
2671 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002672#ifdef SK_DEBUG
2673 if (useHWAA && rt->numSamples() <= 1) {
2674 SkASSERT(this->caps()->mixedSamplesSupport());
2675 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2676 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2677 }
2678#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002679
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002680 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002681 if (useHWAA) {
2682 if (kYes_TriState != fMSAAEnabled) {
2683 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2684 fMSAAEnabled = kYes_TriState;
2685 }
2686 } else {
2687 if (kNo_TriState != fMSAAEnabled) {
2688 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2689 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002690 }
2691 }
2692 }
2693}
2694
bsalomon7f9b2e42016-01-12 13:29:26 -08002695void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002696 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002697 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002698
cdalton8917d622015-05-06 13:40:21 -07002699 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002700 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2701 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002702 bool blendOff =
2703 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2704 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2705 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002706 if (blendOff) {
2707 if (kNo_TriState != fHWBlendState.fEnabled) {
2708 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002709
2710 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2711 // https://code.google.com/p/skia/issues/detail?id=3943
2712 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2713 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2714 SkASSERT(this->caps()->advancedBlendEquationSupport());
2715 // Set to any basic blending equation.
2716 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2717 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2718 fHWBlendState.fEquation = blend_equation;
2719 }
2720
egdanielb414f252014-07-29 13:15:47 -07002721 fHWBlendState.fEnabled = kNo_TriState;
2722 }
cdalton8917d622015-05-06 13:40:21 -07002723 return;
2724 }
2725
2726 if (kYes_TriState != fHWBlendState.fEnabled) {
2727 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002728
cdalton8917d622015-05-06 13:40:21 -07002729 fHWBlendState.fEnabled = kYes_TriState;
2730 }
2731
Mike Kleinab5fec92018-08-16 19:43:31 +00002732 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002733 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2734 fHWBlendState.fEquation = equation;
2735 }
2736
2737 if (GrBlendEquationIsAdvanced(equation)) {
2738 SkASSERT(this->caps()->advancedBlendEquationSupport());
2739 // Advanced equations have no other blend state.
2740 return;
2741 }
2742
Mike Kleinab5fec92018-08-16 19:43:31 +00002743 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002744 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2745 gXfermodeCoeff2Blend[dstCoeff]));
2746 fHWBlendState.fSrcCoeff = srcCoeff;
2747 fHWBlendState.fDstCoeff = dstCoeff;
2748 }
2749
bsalomon7f9b2e42016-01-12 13:29:26 -08002750 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002751 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002752 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002753 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002754 fHWBlendState.fConstColor = blendConst;
2755 fHWBlendState.fConstColorValid = true;
2756 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002757 }
2758}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002759
Brian Salomondc829942018-10-23 16:07:24 -04002760static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002761 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002762 switch (swizzle[i]) {
2763 case 'r': glValues[i] = GR_GL_RED; break;
2764 case 'g': glValues[i] = GR_GL_GREEN; break;
2765 case 'b': glValues[i] = GR_GL_BLUE; break;
2766 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002767 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002768 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002769 default: SK_ABORT("Unsupported component");
2770 }
Brian Salomon327955b2018-10-22 15:39:22 +00002771 }
2772}
2773
Greg Daniel2c3398d2019-06-19 11:58:01 -04002774void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2775 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002776 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002777
reed856e9d92015-09-30 12:21:45 -07002778#ifdef SK_DEBUG
2779 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002780 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002781 const int w = texture->width();
2782 const int h = texture->height();
2783 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2784 }
2785 }
2786#endif
2787
bsalomon@google.comb8670992012-07-25 21:27:09 +00002788 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2789 // 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 +00002790 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002791 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002792 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002793 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002794 }
2795
Robert Phillips294870f2016-11-11 12:38:40 -05002796 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002797 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002798 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002799 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002800 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002801 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002802 }
2803
Brian Salomondc829942018-10-23 16:07:24 -04002804 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002805 if (!this->caps()->mipMapSupport() ||
2806 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002807 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002808 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002809 }
bsalomonefd7d452014-10-23 14:17:46 -07002810
brianosman33f6b3f2016-06-02 05:49:21 -07002811#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002812 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002813 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002814 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002815 }
2816#endif
2817
Brian Salomone2826ab2019-06-04 15:58:31 -04002818 auto timestamp = texture->parameters()->resetTimestamp();
2819 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002820
Brian Salomone2826ab2019-06-04 15:58:31 -04002821 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2822 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002823 if (fSamplerObjectCache) {
2824 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2825 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002826 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2827 texture->parameters()->samplerOverriddenState();
2828 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002829
Brian Salomone2826ab2019-06-04 15:58:31 -04002830 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2831 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002832
Brian Salomone2826ab2019-06-04 15:58:31 -04002833 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2834 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002835
2836 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002837 newSamplerState.fMinLOD = -1000.f;
2838 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002839
Brian Salomone2826ab2019-06-04 15:58:31 -04002840 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002841 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002842 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002843 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002844 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002845 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002847 }
Mike Klein1bccae52018-10-19 11:38:44 +00002848 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002849 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002850 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002851 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002852 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002853 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002854 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002856 }
2857 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002858 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002859 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002860 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002861 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002862 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002863 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002864 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002865 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002866 if (this->glCaps().clampToBorderSupport()) {
2867 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002868 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002869 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002870 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2871 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002872 }
2873 }
Brian Salomondc829942018-10-23 16:07:24 -04002874 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002875 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2876 newNonsamplerState.fBaseMipMapLevel = 0;
2877 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002878
Brian Salomone2826ab2019-06-04 15:58:31 -04002879 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2880 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002881 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002882 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2883 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002884 GrGLenum glValues[4];
2885 get_gl_swizzle_values(swizzle, glValues);
2886 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002887 if (GR_IS_GR_GL(this->glStandard())) {
2888 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
2889 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2890 reinterpret_cast<const GrGLint*>(glValues)));
2891 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002892 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2893 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2894 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2895 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2896 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002897 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002898 }
2899 }
Brian Salomondc829942018-10-23 16:07:24 -04002900 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002901 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2902 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2903 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002904 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002905 this->setTextureUnit(unitIdx);
2906 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002907 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002908 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002909 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002910 this->setTextureUnit(unitIdx);
2911 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002912 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002913 }
Brian Salomon327955b2018-10-22 15:39:22 +00002914 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002915 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2916 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002917}
2918
Brian Salomon1f05d452019-02-08 12:33:08 -05002919void GrGLGpu::onResetTextureBindings() {
2920 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2921 GR_GL_TEXTURE_EXTERNAL};
2922 for (int i = 0; i < this->numTextureUnits(); ++i) {
2923 this->setTextureUnit(i);
2924 for (auto target : kTargets) {
2925 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2926 GL_CALL(BindTexture(target, 0));
2927 }
2928 }
2929 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2930 }
2931}
2932
egdaniel080e6732014-12-22 07:35:52 -08002933void GrGLGpu::flushColorWrite(bool writeColor) {
2934 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002935 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002936 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2937 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002938 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002939 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002940 } else {
2941 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002942 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002943 fHWWriteToColor = kYes_TriState;
2944 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002945 }
bsalomon3e791242014-12-17 13:43:13 -08002946}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002947
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002948void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
Brian Salomon805cc7a2019-01-28 09:52:34 -05002949 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2950 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2951 GL_CALL(ClearColor(r, g, b, a));
2952 fHWClearColor[0] = r;
2953 fHWClearColor[1] = g;
2954 fHWClearColor[2] = b;
2955 fHWClearColor[3] = a;
2956 }
2957}
2958
bsalomon861e1032014-12-16 07:33:49 -08002959void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002960 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002961 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002962 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002963 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002964 }
2965}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002966
Brian Salomond978b902019-02-07 15:09:18 -05002967void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002968 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002969 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002970 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2971 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2972 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002973 }
Brian Salomond978b902019-02-07 15:09:18 -05002974 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2975 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002976 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002977 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002978}
2979
Brian Salomone5e7eb12016-10-14 16:18:33 -04002980// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002981static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2982 const GrSurface* src,
2983 const SkIRect& srcRect,
2984 const SkIPoint& dstPoint,
2985 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002986 int dstSampleCnt = 0;
2987 int srcSampleCnt = 0;
2988 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002989 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002990 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002991 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002992 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002993 }
2994 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2995 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2996
Brian Salomone5e7eb12016-10-14 16:18:33 -04002997 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002998 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002999
Greg Daniel46cfbc62019-06-07 11:43:30 -04003000 GrTextureType dstTexType;
3001 GrTextureType* dstTexTypePtr = nullptr;
3002 GrTextureType srcTexType;
3003 GrTextureType* srcTexTypePtr = nullptr;
3004 if (dstTex) {
3005 dstTexType = dstTex->texturePriv().textureType();
3006 dstTexTypePtr = &dstTexType;
3007 }
3008 if (srcTex) {
3009 srcTexType = srcTex->texturePriv().textureType();
3010 srcTexTypePtr = &srcTexType;
3011 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003012
Greg Daniel46cfbc62019-06-07 11:43:30 -04003013 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, dstTexTypePtr,
3014 src->config(), srcSampleCnt, srcTexTypePtr,
3015 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003016}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003017
Brian Osmana9c8a052018-01-19 10:31:56 -05003018static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3019 // A RT has a separate MSAA renderbuffer if:
3020 // 1) It's multisampled
3021 // 2) We're using an extension with separate MSAA renderbuffers
3022 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003023 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003024}
3025
Greg Daniel46cfbc62019-06-07 11:43:30 -04003026static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003027 const GrGLCaps& caps) {
3028
bsalomon@google.comeb851172013-04-15 13:51:00 +00003029 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003030 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003031 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003032 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003033
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003034 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3035 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3036
Greg Daniel46cfbc62019-06-07 11:43:30 -04003037 GrTextureType dstTexType;
3038 GrTextureType* dstTexTypePtr = nullptr;
3039 GrTextureType srcTexType;
3040 GrTextureType* srcTexTypePtr = nullptr;
3041 if (dstTex) {
3042 dstTexType = dstTex->texturePriv().textureType();
3043 dstTexTypePtr = &dstTexType;
3044 }
3045 if (srcTex) {
3046 srcTexType = srcTex->texturePriv().textureType();
3047 srcTexTypePtr = &srcTexType;
3048 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003049
Greg Daniel46cfbc62019-06-07 11:43:30 -04003050 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, dstTexTypePtr,
3051 src->config(), srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003052}
3053
Greg Danielacd66b42019-05-22 16:29:12 -04003054// If a temporary FBO was created, its non-zero ID is returned.
3055void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003056 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003057 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003058 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003059 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003060 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3061 GrGLuint texID = texture->textureID();
3062 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003063 GrGLuint* tempFBOID;
3064 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003065
egdanield803f272015-03-18 13:01:52 -07003066 if (0 == *tempFBOID) {
3067 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003068 }
3069
Adrienne Walker4ee88512018-05-17 11:37:14 -07003070 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003071 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003072 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003073 target,
robertphillips754f4e92014-09-18 13:52:08 -07003074 texID,
3075 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003076 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003077 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003078 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003079 }
egdaniel0f5f9672015-02-03 11:10:51 -08003080}
3081
Brian Salomon71d9d842016-11-03 13:42:00 -04003082void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3083 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003084 if (!surface->asRenderTarget()) {
3085 SkASSERT(surface->asTexture());
3086 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3087 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3088 GR_GL_COLOR_ATTACHMENT0,
3089 textureTarget,
3090 0,
3091 0));
3092 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003093}
3094
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003095void GrGLGpu::onFBOChanged() {
3096 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3097 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3098 GL_CALL(Flush());
3099 }
3100}
3101
Adrienne Walker4ee88512018-05-17 11:37:14 -07003102void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3103 fStats.incRenderTargetBinds();
3104 GL_CALL(BindFramebuffer(target, fboid));
3105 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3106 fBoundDrawFramebuffer = fboid;
3107 }
3108
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003109 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3110 // The driver forgets the correct scissor when modifying the FBO binding.
3111 if (!fHWScissorSettings.fRect.isInvalid()) {
3112 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3113 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003114 }
3115
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003116 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003117}
3118
Adrienne Walker4ee88512018-05-17 11:37:14 -07003119void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3120 if (fboid == fBoundDrawFramebuffer &&
3121 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3122 // This workaround only applies to deleting currently bound framebuffers
3123 // on Adreno 420. Because this is a somewhat rare case, instead of
3124 // tracking all the attachments of every framebuffer instead just always
3125 // unbind all attachments.
3126 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3127 GR_GL_RENDERBUFFER, 0));
3128 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3129 GR_GL_RENDERBUFFER, 0));
3130 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3131 GR_GL_RENDERBUFFER, 0));
3132 }
3133
3134 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003135
3136 // Deleting the currently bound framebuffer rebinds to 0.
3137 if (fboid == fBoundDrawFramebuffer) {
3138 this->onFBOChanged();
3139 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003140}
3141
Greg Daniel46cfbc62019-06-07 11:43:30 -04003142bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
3143 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003144 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3145 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3146 bool preferCopy = SkToBool(dst->asRenderTarget());
3147 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003148 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003149 return true;
3150 }
3151 }
cblume61214052016-01-26 09:10:48 -08003152
Greg Daniel46cfbc62019-06-07 11:43:30 -04003153 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3154 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003155 return true;
3156 }
3157
Greg Daniel46cfbc62019-06-07 11:43:30 -04003158 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3159 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003160 }
3161
3162 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003163 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003164 return true;
3165 }
bsalomon083617b2016-02-12 12:10:14 -08003166 }
3167
bsalomon6df86402015-06-01 10:41:49 -07003168 return false;
3169}
3170
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003171bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003172 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003173
3174 int progIdx = TextureToCopyProgramIdx(srcTex);
3175 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3176 GrSLType samplerType =
3177 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3178
3179 if (!fCopyProgramArrayBuffer) {
3180 static const GrGLfloat vdata[] = {
3181 0, 0,
3182 0, 1,
3183 1, 0,
3184 1, 1
3185 };
3186 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3187 kStatic_GrAccessPattern, vdata);
3188 }
3189 if (!fCopyProgramArrayBuffer) {
3190 return false;
3191 }
3192
3193 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3194 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3195 if (!fCopyPrograms[progIdx].fProgram) {
3196 return false;
3197 }
3198
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003199 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3200 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3201 GrShaderVar::kUniform_TypeModifier);
3202 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3203 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3204 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3205 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3206
Greg Daniel9e3169b2019-06-06 14:38:17 -04003207 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003208 if (shaderCaps->noperspectiveInterpolationSupport()) {
3209 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3210 vshaderTxt.appendf("#extension %s : require\n", extension);
3211 }
3212 vTexCoord.addModifier("noperspective");
3213 }
3214
3215 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3216 vshaderTxt.append(";");
3217 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3218 vshaderTxt.append(";");
3219 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3220 vshaderTxt.append(";");
3221 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3222 vshaderTxt.append(";");
3223
3224 vshaderTxt.append(
3225 "// Copy Program VS\n"
3226 "void main() {"
3227 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3228 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3229 " sk_Position.zw = half2(0, 1);"
3230 "}"
3231 );
3232
Greg Daniel9e3169b2019-06-06 14:38:17 -04003233 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003234 if (shaderCaps->noperspectiveInterpolationSupport()) {
3235 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3236 fshaderTxt.appendf("#extension %s : require\n", extension);
3237 }
3238 }
3239 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3240 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3241 fshaderTxt.append(";");
3242 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3243 fshaderTxt.append(";");
3244 fshaderTxt.appendf(
3245 "// Copy Program FS\n"
3246 "void main() {"
3247 " sk_FragColor = texture(u_texture, v_texCoord);"
3248 "}"
3249 );
3250
3251 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3252 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3253 SkSL::Program::Settings settings;
3254 settings.fCaps = shaderCaps;
3255 SkSL::String glsl;
3256 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3257 sksl, settings, &glsl, errorHandler);
3258 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3259 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3260 SkASSERT(program->fInputs.isEmpty());
3261
3262 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3263 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3264 errorHandler);
3265 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3266 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3267 errorHandler);
3268 SkASSERT(program->fInputs.isEmpty());
3269
3270 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3271
3272 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3273 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3274 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3275 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3276 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3277 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3278
3279 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3280
3281 GL_CALL(DeleteShader(vshader));
3282 GL_CALL(DeleteShader(fshader));
3283
3284 return true;
3285}
3286
brianosman33f6b3f2016-06-02 05:49:21 -07003287bool GrGLGpu::createMipmapProgram(int progIdx) {
3288 const bool oddWidth = SkToBool(progIdx & 0x2);
3289 const bool oddHeight = SkToBool(progIdx & 0x1);
3290 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3291
Brian Salomon1edc5b92016-11-29 13:43:46 -05003292 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003293
3294 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3295 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3296 if (!fMipmapPrograms[progIdx].fProgram) {
3297 return false;
3298 }
3299
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003300 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3301 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003302 GrShaderVar::kUniform_TypeModifier);
3303 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3304 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003305 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003306 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003307 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3308 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3309 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3310 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003311 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003312 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003313
Ethan Nicholasfc994162019-06-06 10:04:27 -04003314 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003315 if (shaderCaps->noperspectiveInterpolationSupport()) {
3316 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003317 vshaderTxt.appendf("#extension %s : require\n", extension);
3318 }
3319 vTexCoords[0].addModifier("noperspective");
3320 vTexCoords[1].addModifier("noperspective");
3321 vTexCoords[2].addModifier("noperspective");
3322 vTexCoords[3].addModifier("noperspective");
3323 }
3324
Brian Salomon1edc5b92016-11-29 13:43:46 -05003325 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003326 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003327 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003328 vshaderTxt.append(";");
3329 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003330 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003331 vshaderTxt.append(";");
3332 }
3333
3334 vshaderTxt.append(
3335 "// Mipmap Program VS\n"
3336 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003337 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3338 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003339 );
3340
3341 // Insert texture coordinate computation:
3342 if (oddWidth && oddHeight) {
3343 vshaderTxt.append(
3344 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003345 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3346 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003347 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3348 );
3349 } else if (oddWidth) {
3350 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003351 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3352 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003353 );
3354 } else if (oddHeight) {
3355 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003356 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3357 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003358 );
3359 } else {
3360 vshaderTxt.append(
3361 " v_texCoord0 = a_vertex.xy;"
3362 );
3363 }
3364
3365 vshaderTxt.append("}");
3366
Ethan Nicholasfc994162019-06-06 10:04:27 -04003367 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003368 if (shaderCaps->noperspectiveInterpolationSupport()) {
3369 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003370 fshaderTxt.appendf("#extension %s : require\n", extension);
3371 }
3372 }
brianosman33f6b3f2016-06-02 05:49:21 -07003373 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003374 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003375 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003376 fshaderTxt.append(";");
3377 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003378 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003379 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003380 fshaderTxt.append(
3381 "// Mipmap Program FS\n"
3382 "void main() {"
3383 );
3384
3385 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003386 fshaderTxt.append(
3387 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3388 " texture(u_texture, v_texCoord1) + "
3389 " texture(u_texture, v_texCoord2) + "
3390 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003391 );
3392 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003393 fshaderTxt.append(
3394 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3395 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003396 );
3397 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003398 fshaderTxt.append(
3399 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003400 );
3401 }
3402
3403 fshaderTxt.append("}");
3404
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003405 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003406 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003407 SkSL::Program::Settings settings;
3408 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003409 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003410 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003411 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003412 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003413 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003414 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003415
Brian Osman6c431d52019-04-15 16:31:54 -04003416 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003417 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3418 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003419 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003420 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003421 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003422 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003423
3424 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3425
3426 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3427 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3428 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3429 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3430
3431 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3432
3433 GL_CALL(DeleteShader(vshader));
3434 GL_CALL(DeleteShader(fshader));
3435
3436 return true;
3437}
3438
Greg Daniel46cfbc62019-06-07 11:43:30 -04003439bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003440 const SkIPoint& dstPoint) {
3441 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3442 int progIdx = TextureToCopyProgramIdx(srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003443 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3444 return false;
3445 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003446 if (!fCopyPrograms[progIdx].fProgram) {
3447 if (!this->createCopyProgram(srcTex)) {
3448 SkDebugf("Failed to create copy program.\n");
3449 return false;
3450 }
3451 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003452 int w = srcRect.width();
3453 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003454 // We don't swizzle at all in our copies.
3455 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003456 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
3457 this->flushViewport(dst->width(), dst->height());
3458 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003459 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003460 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003461 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003462 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3463 attribs->enableVertexArrays(this, 1);
3464 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3465 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003466 // dst rect edges in NDC (-1 to 1)
3467 int dw = dst->width();
3468 int dh = dst->height();
3469 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3470 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3471 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3472 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003473 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3474 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3475 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3476 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3477 int sw = src->width();
3478 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003479 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3480 // src rect edges in normalized texture space (0 to 1)
3481 sx0 /= sw;
3482 sx1 /= sw;
3483 sy0 /= sh;
3484 sy1 /= sh;
3485 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003486 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3487 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3488 sx1 - sx0, sy1 - sy0, sx0, sy0));
3489 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003490 GrXferProcessor::BlendInfo blendInfo;
3491 blendInfo.reset();
3492 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3493 this->flushColorWrite(true);
3494 this->flushHWAAState(nullptr, false);
3495 this->disableScissor();
3496 this->disableWindowRectangles();
3497 this->disableStencil();
3498 if (this->glCaps().srgbWriteControl()) {
3499 this->flushFramebufferSRGB(true);
3500 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003501 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3502 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003503 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3504 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003505 return true;
3506}
3507
Greg Daniel46cfbc62019-06-07 11:43:30 -04003508void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003509 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003510 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Greg Danielacd66b42019-05-22 16:29:12 -04003511 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003512 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003513 SkASSERT(dstTex);
3514 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003515 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003516
Brian Salomond978b902019-02-07 15:09:18 -05003517 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003518 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003519 dstPoint.fX, dstPoint.fY,
3520 srcRect.fLeft, srcRect.fTop,
3521 srcRect.width(), srcRect.height()));
Brian Salomon71d9d842016-11-03 13:42:00 -04003522 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003523 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3524 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003525 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3526 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003527}
3528
Greg Daniel46cfbc62019-06-07 11:43:30 -04003529bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003530 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003531 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003532 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3533 srcRect.width(), srcRect.height());
3534 if (dst == src) {
3535 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3536 return false;
mtklein404b3b22015-05-18 09:29:10 -07003537 }
bsalomon5df6fee2015-05-18 06:26:15 -07003538 }
bsalomon6df86402015-06-01 10:41:49 -07003539
Greg Danielacd66b42019-05-22 16:29:12 -04003540 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3541 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003542 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003543 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003544
3545 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003546 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003547 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003548
Greg Daniel46cfbc62019-06-07 11:43:30 -04003549 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3550 srcRect.fTop,
3551 srcRect.fRight,
3552 srcRect.fBottom,
3553 dstRect.fLeft,
3554 dstRect.fTop,
3555 dstRect.fRight,
3556 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003557 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003558 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3559 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003560
3561 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3562 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003563 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003564}
3565
Brian Salomon930f9392018-06-20 16:25:26 -04003566bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3567 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003568 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003569 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003570 return false;
3571 }
3572
Brian Salomon930f9392018-06-20 16:25:26 -04003573 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3574 // Uses draw calls to do a series of downsample operations to successive mips.
3575
3576 // The manual approach requires the ability to limit which level we're sampling and that the
3577 // destination can be bound to a FBO:
3578 if (!this->glCaps().doManualMipmapping() ||
3579 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3580 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003581 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003582 GL_CALL(GenerateMipmap(glTex->target()));
3583 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003584 }
3585
brianosman33f6b3f2016-06-02 05:49:21 -07003586 int width = texture->width();
3587 int height = texture->height();
3588 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003589 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003590
3591 // Create (if necessary), then bind temporary FBO:
3592 if (0 == fTempDstFBOID) {
3593 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3594 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003595 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003596 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003597
3598 // Bind the texture, to get things configured for filtering.
3599 // We'll be changing our base level further below:
3600 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003601 // The mipmap program does not do any swizzling.
3602 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003603
3604 // Vertex data:
3605 if (!fMipmapProgramArrayBuffer) {
3606 static const GrGLfloat vdata[] = {
3607 0, 0,
3608 0, 1,
3609 1, 0,
3610 1, 1
3611 };
Brian Salomonae64c192019-02-05 09:41:37 -05003612 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003613 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003614 }
3615 if (!fMipmapProgramArrayBuffer) {
3616 return false;
3617 }
3618
3619 fHWVertexArrayState.setVertexArrayID(this, 0);
3620
3621 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003622 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003623 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003624 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003625
3626 // Set "simple" state once:
3627 GrXferProcessor::BlendInfo blendInfo;
3628 blendInfo.reset();
3629 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3630 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003631 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003632 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003633 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003634 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003635
3636 // Do all the blits:
3637 width = texture->width();
3638 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003639
brianosman33f6b3f2016-06-02 05:49:21 -07003640 for (GrGLint level = 1; level < levelCount; ++level) {
3641 // Get and bind the program for this particular downsample (filter shape can vary):
3642 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3643 if (!fMipmapPrograms[progIdx].fProgram) {
3644 if (!this->createMipmapProgram(progIdx)) {
3645 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003646 // Invalidate all params to cover base level change in a previous iteration.
3647 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003648 return false;
3649 }
3650 }
Brian Salomon802cb312018-06-08 18:05:20 -04003651 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003652
3653 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3654 const float invWidth = 1.0f / width;
3655 const float invHeight = 1.0f / height;
3656 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3657 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3658 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3659
3660 // Only sample from previous mip
3661 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3662
Brian Salomon930f9392018-06-20 16:25:26 -04003663 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3664 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003665
3666 width = SkTMax(1, width / 2);
3667 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003668 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003669
3670 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3671 }
3672
3673 // Unbind:
3674 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3675 GR_GL_TEXTURE_2D, 0, 0));
3676
Brian Salomon930f9392018-06-20 16:25:26 -04003677 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003678 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3679 // We drew the 2nd to last level into the last level.
3680 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3681 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003682
brianosman33f6b3f2016-06-02 05:49:21 -07003683 return true;
3684}
3685
Chris Daltond7291ba2019-03-07 14:17:03 -07003686void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003687 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3688 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003689
3690 int effectiveSampleCnt;
3691 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003692 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003693
3694 sampleLocations->reset(effectiveSampleCnt);
3695 for (int i = 0; i < effectiveSampleCnt; ++i) {
3696 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3697 }
3698}
3699
cdalton231c5fd2015-05-13 12:35:36 -07003700void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003701 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003702 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003703 case kTexture_GrXferBarrierType: {
3704 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003705 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003706 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3707 // The render target uses separate storage so no need for glTextureBarrier.
3708 // FIXME: The render target will resolve automatically when its texture is bound,
3709 // but we could resolve only the bounds that will be read if we do it here instead.
3710 return;
3711 }
cdalton9954bc32015-04-29 14:17:00 -07003712 SkASSERT(this->caps()->textureBarrierSupport());
3713 GL_CALL(TextureBarrier());
3714 return;
cdalton231c5fd2015-05-13 12:35:36 -07003715 }
cdalton8917d622015-05-06 13:40:21 -07003716 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003717 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003718 this->caps()->blendEquationSupport());
3719 GL_CALL(BlendBarrier());
3720 return;
bsalomoncb02b382015-08-12 11:14:50 -07003721 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003722 }
3723}
3724
Brian Salomon5043f1f2019-07-11 21:27:54 -04003725static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003726 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003727 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3728 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3729 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3730 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3731 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3732 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3733 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3734 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3735 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
3736 case GrGLFormat::kRGBA32F: return kRGBA_float_GrPixelConfig;
3737 case GrGLFormat::kRG32F: return kRG_float_GrPixelConfig;
3738 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
3739 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3740 case GrGLFormat::kR16: return kR_16_GrPixelConfig;
3741 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3742 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3743 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3744 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003745
Brian Salomon5043f1f2019-07-11 21:27:54 -04003746 // Configs with multiple equivalent formats.
3747
3748 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3749 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3750
3751 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3752 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003753 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003754 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003755}
3756
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003757GrBackendTexture GrGLGpu::createBackendTexture(int w, int h,
3758 const GrBackendFormat& format,
3759 GrMipMapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -04003760 GrRenderable renderable,
Robert Phillips28a5a432019-06-07 12:46:21 -04003761 const void* srcPixels, size_t rowBytes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04003762 const SkColor4f* color,
3763 GrProtected isProtected) {
Brian Salomon8a375832018-03-14 10:21:40 -04003764 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003765
Brian Salomon5043f1f2019-07-11 21:27:54 -04003766 GrGLFormat glFormat = GrGLBackendFormatToGLFormat(format);
3767 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003768 return GrBackendTexture(); // invalid
3769 }
3770
Brian Salomon5043f1f2019-07-11 21:27:54 -04003771 GrPixelConfig config = gl_format_to_pixel_config(glFormat);
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003772
Brian Salomon5043f1f2019-07-11 21:27:54 -04003773 if (config == kUnknown_GrPixelConfig) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003774 return GrBackendTexture(); // invalid
3775 }
3776
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003777 if (!this->caps()->isConfigTexturable(config)) {
3778 return GrBackendTexture(); // invalid
3779 }
3780
Brian Salomonbb8dde82019-06-27 10:52:13 -04003781 if (w < 1 || w > this->caps()->maxTextureSize() ||
3782 h < 1 || h > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003783 return GrBackendTexture(); // invalid
3784 }
3785
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003786 // Currently we don't support uploading pixel data when mipped.
Robert Phillips28a5a432019-06-07 12:46:21 -04003787 if (srcPixels && GrMipMapped::kYes == mipMapped) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003788 return GrBackendTexture(); // invalid
3789 }
3790
Brian Salomon4687bdd2019-05-09 16:28:04 -04003791 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillipse3bd6732019-05-29 14:20:35 -04003792 return GrBackendTexture(); // invalid
Brian Salomon4687bdd2019-05-09 16:28:04 -04003793 }
Robert Phillips646f6372018-09-25 09:31:10 -04003794
Greg Daniel15500642019-06-27 03:05:55 +00003795 GrGLTextureInfo info;
3796 GrGLTextureParameters::SamplerOverriddenState initialState;
3797
Brian Salomonbb8dde82019-06-27 10:52:13 -04003798 int mipLevelCount = 0;
3799 SkAutoTMalloc<GrMipLevel> texels;
3800 SkAutoMalloc pixelStorage;
3801 SkImage::CompressionType compressionType;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003802 if (GrGLFormatToCompressionType(glFormat, &compressionType)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04003803 // Compressed textures currently must be non-MIP mapped and have initial data.
3804 if (mipMapped == GrMipMapped::kYes) {
3805 return GrBackendTexture();
3806 }
3807 if (!srcPixels) {
3808 if (!color) {
3809 return GrBackendTexture();
3810 }
3811 SkASSERT(0 == rowBytes);
3812 size_t size = GrCompressedDataSize(compressionType, w, h);
3813 srcPixels = pixelStorage.reset(size);
3814 GrFillInCompressedData(compressionType, w, h, (char*)srcPixels, *color);
3815 }
3816 if (!this->createCompressedTextureImpl(&info, w, h, compressionType, &initialState,
3817 srcPixels)) {
3818 return GrBackendTexture();
3819 }
3820 } else {
3821 if (srcPixels) {
3822 mipLevelCount = 1;
3823 texels.reset(mipLevelCount);
3824 texels.get()[0] = {srcPixels, rowBytes};
3825 } else if (color) {
3826 mipLevelCount = 1;
3827 if (GrMipMapped::kYes == mipMapped) {
3828 mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
3829 }
3830
3831 texels.reset(mipLevelCount);
3832 SkTArray<size_t> individualMipOffsets(mipLevelCount);
3833
3834 size_t bytesPerPixel = GrBytesPerPixel(config);
3835
3836 size_t totalSize = GrComputeTightCombinedBufferSize(
3837 bytesPerPixel, w, h, &individualMipOffsets, mipLevelCount);
3838
3839 char* tmpPixels = (char*)pixelStorage.reset(totalSize);
3840
3841 GrFillInData(config, w, h, individualMipOffsets, tmpPixels, *color);
3842 for (int i = 0; i < mipLevelCount; ++i) {
3843 size_t offset = individualMipOffsets[i];
3844
3845 int twoToTheMipLevel = 1 << i;
3846 int currentWidth = SkTMax(1, w / twoToTheMipLevel);
3847
3848 texels.get()[i] = {&(tmpPixels[offset]), currentWidth * bytesPerPixel};
3849 }
3850 }
3851 GrSurfaceDesc desc;
3852 desc.fWidth = w;
3853 desc.fHeight = h;
3854 desc.fConfig = config;
3855 if (!this->createTextureImpl(desc, &info, renderable, &initialState, texels.get(),
3856 mipLevelCount, nullptr)) {
3857 return GrBackendTexture(); // invalid
3858 }
Robert Phillipse3bd6732019-05-29 14:20:35 -04003859 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003860
Robert Phillipse8fabb22018-02-04 14:33:21 -05003861 // unbind the texture from the texture unit to avoid asserts
3862 GL_CALL(BindTexture(info.fTarget, 0));
3863
Brian Salomone2826ab2019-06-04 15:58:31 -04003864 auto parameters = sk_make_sp<GrGLTextureParameters>();
3865 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3866 fResetTimestampForTextureParameters);
3867 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info, std::move(parameters));
Robert Phillipsf0ced622019-05-16 09:06:25 -04003868#if GR_TEST_UTILS
Robert Phillipse3bd6732019-05-29 14:20:35 -04003869 // 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 -04003870 // testing we make sure we set a config here.
3871 beTex.setPixelConfig(config);
Robert Phillipsf0ced622019-05-16 09:06:25 -04003872#endif
Greg Daniel108bb232018-07-03 16:18:29 -04003873 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003874}
3875
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003876void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003877 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3878
3879 GrGLTextureInfo info;
3880 if (tex.getGLTextureInfo(&info)) {
3881 GL_CALL(DeleteTextures(1, &info.fID));
3882 }
3883}
3884
3885#if GR_TEST_UTILS
3886
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003887bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003888 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003889
Greg Daniel52e16d92018-04-10 09:34:07 -04003890 GrGLTextureInfo info;
3891 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003892 return false;
3893 }
3894
3895 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003896 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003897
3898 return (GR_GL_TRUE == result);
3899}
3900
Brian Salomonf865b052018-03-09 09:01:53 -05003901GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003902 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003903 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3904 return GrBackendRenderTarget(); // invalid
3905 }
Brian Salomon8a375832018-03-14 10:21:40 -04003906 this->handleDirtyContext();
Greg Daniele877dce2019-07-11 10:52:43 -04003907 auto config = GrColorTypeToPixelConfig(colorType);
Brian Salomon93348dd2018-08-29 12:56:23 -04003908 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003909 return {};
3910 }
Greg Daniel24b67662019-07-15 16:55:53 -04003911 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon93348dd2018-08-29 12:56:23 -04003912 bool useTexture = false;
3913 GrGLenum colorBufferFormat;
3914 GrGLenum externalFormat = 0, externalType = 0;
3915 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
3916 // BGRA render buffers are not supported.
3917 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
3918 &externalType);
3919 useTexture = true;
3920 } else {
Greg Daniel24b67662019-07-15 16:55:53 -04003921 colorBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003922 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003923 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003924 if (sFormatIdx < 0) {
3925 return {};
3926 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003927 GrGLuint colorID = 0;
3928 GrGLuint stencilID = 0;
3929 auto deleteIDs = [&] {
3930 if (colorID) {
3931 if (useTexture) {
3932 GL_CALL(DeleteTextures(1, &colorID));
3933 } else {
3934 GL_CALL(DeleteRenderbuffers(1, &colorID));
3935 }
Brian Salomonf865b052018-03-09 09:01:53 -05003936 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003937 if (stencilID) {
3938 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003939 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003940 };
3941
3942 if (useTexture) {
3943 GL_CALL(GenTextures(1, &colorID));
3944 } else {
3945 GL_CALL(GenRenderbuffers(1, &colorID));
3946 }
3947 GL_CALL(GenRenderbuffers(1, &stencilID));
3948 if (!stencilID || !colorID) {
3949 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003950 return {};
3951 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003952
Brian Salomonf865b052018-03-09 09:01:53 -05003953 GrGLFramebufferInfo info;
3954 info.fFBOID = 0;
Greg Daniel24b67662019-07-15 16:55:53 -04003955 info.fFormat = this->glCaps().configSizedInternalFormat(config);
Brian Salomonf865b052018-03-09 09:01:53 -05003956 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3957 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003958 deleteIDs();
3959 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003960 }
3961
3962 this->invalidateBoundRenderTarget();
3963
Adrienne Walker4ee88512018-05-17 11:37:14 -07003964 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003965 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05003966 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003967 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
3968 externalType, nullptr));
3969 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3970 colorID, 0));
3971 } else {
3972 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3973 GL_ALLOC_CALL(this->glInterface(),
3974 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
3975 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3976 GR_GL_RENDERBUFFER, colorID));
3977 }
3978 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003979 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3980 GL_ALLOC_CALL(this->glInterface(),
3981 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3982 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003983 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003984 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3985 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003986 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003987 }
3988
Brian Salomon93348dd2018-08-29 12:56:23 -04003989 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3990 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3991 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3992 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003993 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003994 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003995
Adrienne Walker4ee88512018-05-17 11:37:14 -07003996 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003997 GrGLenum status;
3998 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3999 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004000 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004001 return {};
4002 }
4003 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004004 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4005 // Lots of tests don't go through Skia's public interface which will set the config so for
4006 // testing we make sure we set a config here.
4007 beRT.setPixelConfig(config);
Robert Phillips1e2cb442019-07-02 15:51:28 -04004008 SkASSERT(kUnknown_GrPixelConfig != this->caps()->validateBackendRenderTarget(beRT, colorType));
Greg Daniel108bb232018-07-03 16:18:29 -04004009 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004010}
4011
4012void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004013 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004014 GrGLFramebufferInfo info;
4015 if (backendRT.getGLFramebufferInfo(&info)) {
4016 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004017 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004018 }
4019 }
joshualitt8fd844f2015-12-02 13:36:47 -08004020}
4021
Greg Daniel26b50a42018-03-08 09:49:58 -05004022void GrGLGpu::testingOnly_flushGpuAndSync() {
4023 GL_CALL(Finish());
4024}
Brian Salomonf865b052018-03-09 09:01:53 -05004025#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004026
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004027///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004028
cdaltone2e71c22016-04-07 18:13:29 -07004029GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004030 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004031 GrGLAttribArrayState* attribState;
4032
cdaltone2e71c22016-04-07 18:13:29 -07004033 if (gpu->glCaps().isCoreProfile()) {
4034 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004035 GrGLuint arrayID;
4036 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4037 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004038 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004039 }
cdaltone2e71c22016-04-07 18:13:29 -07004040 if (ibuf) {
4041 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004042 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004043 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004044 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004045 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004046 if (ibuf) {
4047 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004048 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004049 } else {
4050 this->setVertexArrayID(gpu, 0);
4051 }
4052 int attrCount = gpu->glCaps().maxVertexAttributes();
4053 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4054 fDefaultVertexArrayAttribState.resize(attrCount);
4055 }
4056 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004057 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004058 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004059}
bsalomone179a912016-01-20 06:18:10 -08004060
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04004061void GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004062 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004063 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004064 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004065 // We call finish if the client told us to sync or if we have a finished proc but don't support
4066 // GLsync objects.
4067 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4068 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4069 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004070 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004071 // After a finish everything previously sent to GL is done.
4072 for (const auto& cb : fFinishCallbacks) {
4073 cb.fCallback(cb.fContext);
4074 this->deleteSync(cb.fSync);
4075 }
4076 fFinishCallbacks.clear();
4077 if (info.fFinishedProc) {
4078 info.fFinishedProc(info.fFinishedContext);
4079 }
4080 } else {
4081 if (info.fFinishedProc) {
4082 FinishCallback callback;
4083 callback.fCallback = info.fFinishedProc;
4084 callback.fContext = info.fFinishedContext;
4085 callback.fSync = (GrGLsync)this->insertFence();
4086 fFinishCallbacks.push_back(callback);
4087 GL_CALL(Flush());
4088 } else if (insertedSemaphore) {
4089 // Must call flush after semaphores in case they are waited on another GL context.
4090 GL_CALL(Flush());
4091 }
4092 // See if any previously inserted finish procs are good to go.
4093 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004094 }
Greg Daniel51316782017-08-02 15:10:09 +00004095}
4096
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004097void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4098 if (buffer->asRTCommandBuffer()) {
4099 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4100 fCachedRTCommandBuffer->reset();
4101 } else {
4102 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4103 fCachedTexCommandBuffer->reset();
4104 }
4105}
4106
Greg Daniel6be35232017-03-01 17:01:09 -05004107GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004108 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004109 GrGLsync sync;
4110 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4111 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4112 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004113}
4114
Brian Salomonb0d8b762019-05-06 16:58:22 -04004115bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4116 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004117 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004118 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4119 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4120}
4121
4122bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4123 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004124}
4125
4126void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004127 this->deleteSync((GrGLsync)fence);
4128}
4129
Greg Daniela5cb7812017-06-16 09:45:32 -04004130sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004131 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004132 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004133}
4134
Greg Daniel48661b82018-01-22 16:11:35 -05004135sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4136 GrResourceProvider::SemaphoreWrapType wrapType,
4137 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004138 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004139 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4140}
4141
Greg Daniel858e12c2018-12-06 11:11:37 -05004142void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004143 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4144
Greg Daniel48661b82018-01-22 16:11:35 -05004145 GrGLsync sync;
4146 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4147 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004148}
4149
Greg Daniel48661b82018-01-22 16:11:35 -05004150void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004151 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4152
4153 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4154}
4155
Brian Salomonb0d8b762019-05-06 16:58:22 -04004156void GrGLGpu::checkFinishProcs() {
4157 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4158 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4159 /* timeout = */ 0, /* flush = */ false)) {
4160 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4161 this->deleteSync(fFinishCallbacks.front().fSync);
4162 fFinishCallbacks.pop_front();
4163 }
4164}
4165
Greg Daniel6be35232017-03-01 17:01:09 -05004166void GrGLGpu::deleteSync(GrGLsync sync) const {
4167 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004168}
Brian Osman13dddce2017-05-09 13:19:50 -04004169
Robert Phillips65a88fa2017-08-08 08:36:22 -04004170void GrGLGpu::insertEventMarker(const char* msg) {
4171 GL_CALL(InsertEventMarker(strlen(msg), msg));
4172}
4173
Brian Osman13dddce2017-05-09 13:19:50 -04004174sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4175 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004176 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004177 this->insertSemaphore(semaphore);
4178 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4179 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004180
4181 return semaphore;
4182}
Robert Phillips646e4292017-06-13 12:44:56 -04004183
4184int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004185 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004186 case kTexture2DSampler_GrSLType:
4187 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004188 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004189 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004190 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004191 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004192 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004193 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004194 return 0;
4195 }
4196}
Brian Osman71a18892017-08-10 10:23:25 -04004197
Kevin Lubickf4def342018-10-04 12:52:50 -04004198#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004199#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004200void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4201 // We are called by the base class, which has already called beginObject(). We choose to nest
4202 // all of our caps information in a named sub-object.
4203 writer->beginObject("GL GPU");
4204
4205 const GrGLubyte* str;
4206 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4207 writer->appendString("GL_VERSION", (const char*)(str));
4208 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4209 writer->appendString("GL_RENDERER", (const char*)(str));
4210 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4211 writer->appendString("GL_VENDOR", (const char*)(str));
4212 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4213 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4214
4215 writer->appendName("extensions");
4216 glInterface()->fExtensions.dumpJSON(writer);
4217
4218 writer->endObject();
4219}
Kevin Lubickf4def342018-10-04 12:52:50 -04004220#endif