blob: d2b18f1a80b20d3eaccc976dd7a425adcf872bba [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
reed@google.comac10a2d2010-12-22 21:39:39 +000047#define SKIP_CACHE_CHECK true
48
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000049#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
50 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
51 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
52 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000054 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
55 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
56 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
57#endif
58
Jim Van Verth32ac83e2016-11-28 15:23:57 -050059//#define USE_NSIGHT
60
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000061///////////////////////////////////////////////////////////////////////////////
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050084 GR_GL_HSL_LUMINOSITY,
85
86 // Illegal... needs to map to something.
87 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070088};
89GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
90GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
91GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
92GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
93GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
94GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
95GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
96GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
97GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
98GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
99GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
100GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
101GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
102GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
103GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
104GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
105GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
106GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
bsalomonf7cc8772015-05-11 11:21:14 -0700107GR_STATIC_ASSERT(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
122 GR_GL_CONSTANT_ALPHA,
123 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124
125 // extended blend coeffs
126 GR_GL_SRC1_COLOR,
127 GR_GL_ONE_MINUS_SRC1_COLOR,
128 GR_GL_SRC1_ALPHA,
129 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500130
131 // Illegal... needs to map to something.
132 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000133};
134
bsalomon861e1032014-12-16 07:33:49 -0800135bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000136 static const bool gCoeffReferencesBlendConst[] = {
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 false,
146 false,
147 true,
148 true,
149 true,
150 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151
152 // extended blend coeffs
153 false,
154 false,
155 false,
156 false,
Mike Klein36743362018-11-06 08:23:30 -0500157
158 // Illegal.
159 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000160 };
161 return gCoeffReferencesBlendConst[coeff];
bsalomonf7cc8772015-05-11 11:21:14 -0700162 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000163
bsalomon@google.com47059542012-06-06 20:51:20 +0000164 GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
165 GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
166 GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
167 GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
168 GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
169 GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
170 GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
171 GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
172 GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
173 GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
174 GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
175 GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
176 GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
177 GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000178
bsalomon@google.com47059542012-06-06 20:51:20 +0000179 GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
180 GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
181 GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
182 GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000183
184 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
bsalomonf7cc8772015-05-11 11:21:14 -0700185 GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000186}
187
Brian Salomond978b902019-02-07 15:09:18 -0500188//////////////////////////////////////////////////////////////////////////////
189
190static int gl_target_to_binding_index(GrGLenum target) {
191 switch (target) {
192 case GR_GL_TEXTURE_2D:
193 return 0;
194 case GR_GL_TEXTURE_RECTANGLE:
195 return 1;
196 case GR_GL_TEXTURE_EXTERNAL:
197 return 2;
198 }
199 SK_ABORT("Unexpected GL texture target.");
200 return 0;
201}
202
203GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500204 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
205}
206
207bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
208 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500209}
210
211void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500212 int targetIndex = gl_target_to_binding_index(target);
213 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
214 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500215}
216
Brian Salomon1f05d452019-02-08 12:33:08 -0500217void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
218 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500219}
220
Brian Salomon1f05d452019-02-08 12:33:08 -0500221void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
222 for (auto& targetBinding : fTargetBindings) {
223 targetBinding.fBoundResourceID.makeInvalid();
224 if (markUnmodified) {
225 targetBinding.fHasBeenModified = false;
226 }
Brian Salomond978b902019-02-07 15:09:18 -0500227 }
228}
229
230//////////////////////////////////////////////////////////////////////////////
231
Brian Salomondc829942018-10-23 16:07:24 -0400232static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
233 switch (filter) {
234 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
235 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
236 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
237 }
238 SK_ABORT("Unknown filter");
239 return 0;
240}
241
242static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
243 switch (filter) {
244 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
245 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
246 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
247 }
248 SK_ABORT("Unknown filter");
249 return 0;
250}
251
Michael Ludwigf23a1522018-12-10 11:36:13 -0500252static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
253 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400254 switch (wrapMode) {
255 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
256 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
257 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500258 case GrSamplerState::WrapMode::kClampToBorder:
259 // May not be supported but should have been caught earlier
260 SkASSERT(caps.clampToBorderSupport());
261 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500262 }
Brian Salomondc829942018-10-23 16:07:24 -0400263 SK_ABORT("Unknown wrap mode");
264 return 0;
265}
266
267///////////////////////////////////////////////////////////////////////////////
268
269class GrGLGpu::SamplerObjectCache {
270public:
271 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
272 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
273 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
274 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
275 std::fill_n(fSamplers, kNumSamplers, 0);
276 }
277
278 ~SamplerObjectCache() {
279 if (!fNumTextureUnits) {
280 // We've already been abandoned.
281 return;
282 }
Chris Dalton703fe682019-05-15 12:58:12 -0600283 for (GrGLuint sampler : fSamplers) {
284 // The spec states that "zero" values should be silently ignored, however they still
285 // trigger GL errors on some NVIDIA platforms.
286 if (sampler) {
287 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
288 }
289 }
Brian Salomondc829942018-10-23 16:07:24 -0400290 }
291
292 void bindSampler(int unitIdx, const GrSamplerState& state) {
293 int index = StateToIndex(state);
294 if (!fSamplers[index]) {
295 GrGLuint s;
296 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
297 if (!s) {
298 return;
299 }
300 fSamplers[index] = s;
301 auto minFilter = filter_to_gl_min_filter(state.filter());
302 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500303 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
304 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400305 GR_GL_CALL(fGpu->glInterface(),
306 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
307 GR_GL_CALL(fGpu->glInterface(),
308 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
309 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
310 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
311 }
312 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
313 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
314 fHWBoundSamplers[unitIdx] = fSamplers[index];
315 }
316 }
317
318 void invalidateBindings() {
319 // When we have sampler support we always use samplers. So setting these to zero will cause
320 // a rebind on next usage.
321 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
322 }
323
324 void abandon() {
325 fHWBoundSamplers.reset();
326 fNumTextureUnits = 0;
327 }
328
329 void release() {
330 if (!fNumTextureUnits) {
331 // We've already been abandoned.
332 return;
333 }
334 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
335 std::fill_n(fSamplers, kNumSamplers, 0);
336 // Deleting a bound sampler implicitly binds sampler 0.
337 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
338 }
339
340private:
341 static int StateToIndex(const GrSamplerState& state) {
342 int filter = static_cast<int>(state.filter());
343 SkASSERT(filter >= 0 && filter < 3);
344 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500345 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400346 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500347 SkASSERT(wrapY >= 0 && wrapY < 4);
348 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400349 SkASSERT(idx < kNumSamplers);
350 return idx;
351 }
352
353 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500354 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400355 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
356 GrGLuint fSamplers[kNumSamplers];
357 int fNumTextureUnits;
358};
359
reed@google.comac10a2d2010-12-22 21:39:39 +0000360///////////////////////////////////////////////////////////////////////////////
361
Brian Salomon384fab42017-12-07 12:33:05 -0500362sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
363 GrContext* context) {
364 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500365 interface = GrGLMakeNativeInterface();
366 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
367 // to GrGLMakeNativeInterface.
368 if (!interface) {
369 interface = sk_ref_sp(GrGLCreateNativeInterface());
370 }
Brian Salomon384fab42017-12-07 12:33:05 -0500371 if (!interface) {
372 return nullptr;
373 }
bsalomon424cc262015-05-22 10:37:30 -0700374 }
Jim Van Verth76334772017-05-05 16:46:05 -0400375#ifdef USE_NSIGHT
376 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
377#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500378 auto glContext = GrGLContext::Make(std::move(interface), options);
379 if (!glContext) {
380 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700381 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500382 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700383}
384
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500385GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
386 : GrGpu(context)
387 , fGLContext(std::move(ctx))
388 , fProgramCache(new ProgramCache(this))
389 , fHWProgramID(0)
390 , fTempSrcFBOID(0)
391 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500392 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500393 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400394 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500395 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000396
Brian Salomond978b902019-02-07 15:09:18 -0500397 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000398
Brian Salomonae64c192019-02-05 09:41:37 -0500399 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
400 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700401 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500402 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
403 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
404 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
405 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700406 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500407 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
408 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700409 }
Brian Salomonae64c192019-02-05 09:41:37 -0500410 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400411 fHWBufferState[i].invalidate();
412 }
Brian Salomonae64c192019-02-05 09:41:37 -0500413 GR_STATIC_ASSERT(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700414
415 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
416 fPathRendering.reset(new GrGLPathRendering(this));
417 }
418
Brian Salomondc829942018-10-23 16:07:24 -0400419 if (this->glCaps().samplerObjectSupport()) {
420 fSamplerObjectCache.reset(new SamplerObjectCache(this));
421 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000422}
423
bsalomon861e1032014-12-16 07:33:49 -0800424GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700425 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
426 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800427 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700428 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700429 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800430
Brian Salomon802cb312018-06-08 18:05:20 -0400431 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400432 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000433 // detach the current program so there is no confusion on OpenGL's part
434 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000435 GL_CALL(UseProgram(0));
436 }
437
Brian Salomon43f8bf02017-10-18 08:33:29 -0400438 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700439 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800440 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400441 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700442 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800443 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400444 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700445 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800446 }
egdaniel0f5f9672015-02-03 11:10:51 -0800447
bsalomon7ea33f52015-11-22 14:51:00 -0800448 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
449 if (0 != fCopyPrograms[i].fProgram) {
450 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
451 }
bsalomon6df86402015-06-01 10:41:49 -0700452 }
bsalomon6dea83f2015-12-03 12:58:06 -0800453
brianosman33f6b3f2016-06-02 05:49:21 -0700454 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
455 if (0 != fMipmapPrograms[i].fProgram) {
456 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
457 }
458 }
459
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000460 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400461 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700462}
463
bsalomon6e2aad42016-04-01 11:54:31 -0700464void GrGLGpu::disconnect(DisconnectType type) {
465 INHERITED::disconnect(type);
466 if (DisconnectType::kCleanup == type) {
467 if (fHWProgramID) {
468 GL_CALL(UseProgram(0));
469 }
470 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700471 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700472 }
473 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700474 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700475 }
476 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700477 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700478 }
bsalomon6e2aad42016-04-01 11:54:31 -0700479 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
480 if (fCopyPrograms[i].fProgram) {
481 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
482 }
483 }
brianosman33f6b3f2016-06-02 05:49:21 -0700484 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
485 if (fMipmapPrograms[i].fProgram) {
486 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
487 }
488 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400489
Brian Salomondc829942018-10-23 16:07:24 -0400490 if (fSamplerObjectCache) {
491 fSamplerObjectCache->release();
492 }
bsalomon6e2aad42016-04-01 11:54:31 -0700493 } else {
494 if (fProgramCache) {
495 fProgramCache->abandon();
496 }
Brian Salomondc829942018-10-23 16:07:24 -0400497 if (fSamplerObjectCache) {
498 fSamplerObjectCache->abandon();
499 }
bsalomon6e2aad42016-04-01 11:54:31 -0700500 }
501
Brian Salomon802cb312018-06-08 18:05:20 -0400502 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700503 delete fProgramCache;
504 fProgramCache = nullptr;
505
bsalomonc8dc1f72014-08-21 13:02:13 -0700506 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700507 fTempSrcFBOID = 0;
508 fTempDstFBOID = 0;
509 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700510 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800511 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
512 fCopyPrograms[i].fProgram = 0;
513 }
brianosman33f6b3f2016-06-02 05:49:21 -0700514 fMipmapProgramArrayBuffer.reset();
515 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
516 fMipmapPrograms[i].fProgram = 0;
517 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400518
jvanverthe9c0fc62015-04-29 11:18:05 -0700519 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700520 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700521 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000522}
523
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000524///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000525
bsalomon861e1032014-12-16 07:33:49 -0800526void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000527 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400528 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000529 GL_CALL(Disable(GR_GL_DEPTH_TEST));
530 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000531
Brian Salomonf0861672017-05-08 11:10:10 -0400532 // We don't use face culling.
533 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600534 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
535 // just set this to the default for self-consistency.
536 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400537
Brian Salomonae64c192019-02-05 09:41:37 -0500538 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
539 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700540
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400541 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500542#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000543 // Desktop-only state that we never change
544 if (!this->glCaps().isCoreProfile()) {
545 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
546 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
547 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
548 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
549 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
550 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
551 }
552 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
553 // core profile. This seems like a bug since the core spec removes any mention of
554 // GL_ARB_imaging.
555 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
556 GL_CALL(Disable(GR_GL_COLOR_TABLE));
557 }
558 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400559
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400560 if (this->caps()->wireframeMode()) {
561 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
562 } else {
563 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
564 }
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500565#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000566 // Since ES doesn't support glPointSize at all we always use the VS to
567 // set the point size
568 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
569
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000570 }
joshualitt58162332014-08-01 06:44:53 -0700571
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400572 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500573 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700574 // The arm extension requires specifically enabling MSAA fetching per sample.
575 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500576 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700577 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000578 fHWWriteToColor = kUnknown_TriState;
579 // we only ever use lines in hairline mode
580 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700581 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500582
583 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000584 }
edisonn@google.comba669992013-06-28 16:03:21 +0000585
egdanielb414f252014-07-29 13:15:47 -0700586 if (resetBits & kMSAAEnable_GrGLBackendState) {
587 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700588
egdanieleed519e2016-01-15 11:36:18 -0800589 if (this->caps()->usesMixedSamples()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800590 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
591 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700592 GL_CALL(CoverageModulation(GR_GL_RGBA));
593 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000596 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400597 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000598
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500600 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500601 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000602 }
Brian Salomondc829942018-10-23 16:07:24 -0400603 if (fSamplerObjectCache) {
604 fSamplerObjectCache->invalidateBindings();
605 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000606 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000607
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000608 if (resetBits & kBlend_GrGLBackendState) {
609 fHWBlendState.invalidate();
610 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000611
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 if (resetBits & kView_GrGLBackendState) {
613 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700614 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 fHWViewport.invalidate();
616 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000617
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000618 if (resetBits & kStencil_GrGLBackendState) {
619 fHWStencilSettings.invalidate();
620 fHWStencilTestEnabled = kUnknown_TriState;
621 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000622
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 // Vertex
624 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700625 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500626 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
627 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000628 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000629
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500631 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700632 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000633 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000634
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000635 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700636 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700637 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000638 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000639 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000640
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000641 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000642 if (resetBits & kPixelStore_GrGLBackendState) {
643 if (this->glCaps().unpackRowLengthSupport()) {
644 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
645 }
646 if (this->glCaps().packRowLengthSupport()) {
647 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
648 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 if (this->glCaps().packFlipYSupport()) {
650 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
651 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000652 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000653
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000654 if (resetBits & kProgram_GrGLBackendState) {
655 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400656 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000657 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400658 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
Brian Salomond17f6582017-07-19 18:28:58 -0400661static bool check_backend_texture(const GrBackendTexture& backendTex, const GrGLCaps& caps,
662 GrGLTexture::IDDesc* idDesc) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400663 GrGLTextureInfo info;
664 if (!backendTex.getGLTextureInfo(&info) || !info.fID) {
Brian Salomond17f6582017-07-19 18:28:58 -0400665 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800666 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000667
Greg Daniel52e16d92018-04-10 09:34:07 -0400668 idDesc->fInfo = info;
bsalomon7ea33f52015-11-22 14:51:00 -0800669
Brian Salomond17f6582017-07-19 18:28:58 -0400670 if (GR_GL_TEXTURE_EXTERNAL == idDesc->fInfo.fTarget) {
671 if (!caps.shaderCaps()->externalTextureSupport()) {
672 return false;
673 }
674 } else if (GR_GL_TEXTURE_RECTANGLE == idDesc->fInfo.fTarget) {
675 if (!caps.rectangleTextureSupport()) {
676 return false;
677 }
678 } else if (GR_GL_TEXTURE_2D != idDesc->fInfo.fTarget) {
679 return false;
680 }
681 return true;
682}
683
684sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500685 GrWrapOwnership ownership, GrWrapCacheable cacheable,
686 GrIOType ioType) {
bsalomonb15b4c12014-10-29 12:41:57 -0700687 GrGLTexture::IDDesc idDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400688 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
689 return nullptr;
690 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500691 if (!idDesc.fInfo.fFormat) {
692 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
693 }
Brian Salomond17f6582017-07-19 18:28:58 -0400694 if (kBorrow_GrWrapOwnership == ownership) {
695 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
696 } else {
697 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
698 }
bsalomone5286e02016-01-14 09:24:09 -0800699
Brian Salomond17f6582017-07-19 18:28:58 -0400700 GrSurfaceDesc surfDesc;
701 surfDesc.fFlags = kNone_GrSurfaceFlags;
702 surfDesc.fWidth = backendTex.width();
703 surfDesc.fHeight = backendTex.height();
704 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500705 surfDesc.fSampleCnt = 1;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400706
Greg Daniel177e6952017-10-12 12:27:11 -0400707 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
708 : GrMipMapsStatus::kNotAllocated;
709
Brian Salomone2826ab2019-06-04 15:58:31 -0400710 auto texture = GrGLTexture::MakeWrapped(this, surfDesc, mipMapsStatus, idDesc,
711 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400712 // We don't know what parameters are already set on wrapped textures.
713 texture->textureParamsModified();
714 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400715}
716
717sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400718 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500719 GrWrapOwnership ownership,
720 GrWrapCacheable cacheable) {
Brian Salomond17f6582017-07-19 18:28:58 -0400721 GrGLTexture::IDDesc idDesc;
722 if (!check_backend_texture(backendTex, this->glCaps(), &idDesc)) {
bsalomone5286e02016-01-14 09:24:09 -0800723 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800724 }
Greg Daniele7d8da42017-12-04 11:23:19 -0500725 if (!idDesc.fInfo.fFormat) {
726 idDesc.fInfo.fFormat = this->glCaps().configSizedInternalFormat(backendTex.config());
727 }
bsalomone5286e02016-01-14 09:24:09 -0800728
Brian Salomond17f6582017-07-19 18:28:58 -0400729 // We don't support rendering to a EXTERNAL texture.
730 if (GR_GL_TEXTURE_EXTERNAL == idDesc.fInfo.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800731 return nullptr;
732 }
bsalomon10528f12015-10-14 12:54:52 -0700733
Brian Osman766fcbb2017-03-13 09:33:09 -0400734 if (kBorrow_GrWrapOwnership == ownership) {
Brian Osmana6953f22017-03-10 20:14:05 +0000735 idDesc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400736 } else {
737 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800738 }
bsalomonb15b4c12014-10-29 12:41:57 -0700739
Ben Wagner18b61f92016-10-25 10:44:02 -0400740 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400741 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000742 surfDesc.fWidth = backendTex.width();
743 surfDesc.fHeight = backendTex.height();
744 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500745 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
746 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;
csmartdaltonf9635992016-08-10 11:09:07 -0700778 idDesc.fIsMixedSampled = false;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000779
bsalomonb15b4c12014-10-29 12:41:57 -0700780 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400781 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Danielbcf612b2017-05-01 13:50:58 +0000782 desc.fWidth = backendRT.width();
783 desc.fHeight = backendRT.height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400784 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500785 desc.fSampleCnt =
786 this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config());
bsalomonb15b4c12014-10-29 12:41:57 -0700787
Greg Daniel4065d452018-11-16 15:43:41 -0500788 return GrGLRenderTarget::MakeWrapped(this, desc, info.fFormat, idDesc, backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000789}
790
Greg Daniel7ef28f32017-04-20 16:41:55 +0000791sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000792 int sampleCnt) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400793 GrGLTextureInfo info;
794 if (!tex.getGLTextureInfo(&info) || !info.fID) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800795 return nullptr;
796 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800797
Greg Daniel52e16d92018-04-10 09:34:07 -0400798 if (GR_GL_TEXTURE_RECTANGLE != info.fTarget &&
799 GR_GL_TEXTURE_2D != info.fTarget) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800800 // Only texture rectangle and texture 2d are supported. We do not check whether texture
801 // rectangle is supported by Skia - if the caller provided us with a texture rectangle,
802 // we assume the necessary support exists.
803 return nullptr;
804 }
805
Ben Wagner18b61f92016-10-25 10:44:02 -0400806 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000807 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
808 surfDesc.fWidth = tex.width();
809 surfDesc.fHeight = tex.height();
810 surfDesc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500811 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
ericrkf7b8b8a2016-02-24 14:49:51 -0800812
813 GrGLRenderTarget::IDDesc rtIDDesc;
Greg Daniel52e16d92018-04-10 09:34:07 -0400814 if (!this->createRenderTargetObjects(surfDesc, info, &rtIDDesc)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800815 return nullptr;
816 }
Greg Daniel4065d452018-11-16 15:43:41 -0500817 return GrGLRenderTarget::MakeWrapped(this, surfDesc, info.fFormat, rtIDDesc, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800818}
819
Brian Salomonc320b152018-02-20 14:05:36 -0500820static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700821 if (!glTex) {
822 return false;
823 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000824
bsalomone5286e02016-01-14 09:24:09 -0800825 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
826 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800827 return false;
828 }
829
jvanverth17aa0472016-01-05 10:41:27 -0800830 return true;
831}
832
Brian Salomona9b04b92018-06-01 15:04:28 -0400833bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
834 GrColorType srcColorType, const GrMipLevel texels[],
835 int mipLevelCount) {
Brian Salomonc320b152018-02-20 14:05:36 -0500836 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800837
Brian Salomonc320b152018-02-20 14:05:36 -0500838 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800839 return false;
840 }
841
Brian Salomond978b902019-02-07 15:09:18 -0500842 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000843
Brian Salomonc320b152018-02-20 14:05:36 -0500844 // No sRGB transformation occurs in uploadTexData. We choose to make the src config match the
845 // srgb-ness of the surface to avoid issues in ES2 where internal/external formats must match.
846 // When we're on ES2 and the dst is GL_SRGB_ALPHA by making the config be kSRGB_8888 we know
847 // that our caps will choose GL_SRGB_ALPHA as the external format, too. On ES3 or regular GL our
848 // caps knows to make the external format be GL_RGBA.
849 auto srgbEncoded = GrPixelConfigIsSRGBEncoded(surface->config());
850 auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srgbEncoded);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500851
852 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
Brian Salomona9b04b92018-06-01 15:04:28 -0400853 return this->uploadTexData(glTex->config(), glTex->width(), glTex->height(), glTex->target(),
854 kWrite_UploadType, left, top, width, height, srcAsConfig, texels,
855 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000856}
857
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400858// For GL_[UN]PACK_ALIGNMENT. TODO: This really wants to be GrColorType.
bsalomonf46a1242015-12-15 12:37:38 -0800859static inline GrGLint config_alignment(GrPixelConfig config) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500860 SkASSERT(!GrPixelConfigIsCompressed(config));
bsalomonf46a1242015-12-15 12:37:38 -0800861 switch (config) {
862 case kAlpha_8_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500863 case kAlpha_8_as_Alpha_GrPixelConfig:
864 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman986563b2017-01-10 14:20:02 -0500865 case kGray_8_GrPixelConfig:
Greg Daniel7af060a2017-12-05 16:27:11 -0500866 case kGray_8_as_Lum_GrPixelConfig:
867 case kGray_8_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800868 return 1;
869 case kRGB_565_GrPixelConfig:
870 case kRGBA_4444_GrPixelConfig:
Jim Van Verth69e57852018-12-05 13:38:59 -0500871 case kRG_88_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800872 case kAlpha_half_GrPixelConfig:
Greg Danielef59d872017-11-17 16:47:21 -0500873 case kAlpha_half_as_Red_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800874 case kRGBA_half_GrPixelConfig:
Brian Osmand0626aa2019-03-11 15:28:06 -0400875 case kRGBA_half_Clamped_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800876 return 2;
877 case kRGBA_8888_GrPixelConfig:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400878 case kRGB_888_GrPixelConfig: // We're really talking about GrColorType::kRGB_888x here.
Greg Danielf259b8b2019-02-14 09:03:43 -0500879 case kRGB_888X_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800880 case kBGRA_8888_GrPixelConfig:
881 case kSRGBA_8888_GrPixelConfig:
brianosmana6359362016-03-21 06:55:37 -0700882 case kSBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500883 case kRGBA_1010102_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800884 case kRGBA_float_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500885 case kRG_float_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800886 return 4;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500887 case kRGB_ETC1_GrPixelConfig:
csmartdalton6aa0e112017-02-08 16:14:11 -0500888 case kUnknown_GrPixelConfig:
bsalomonf46a1242015-12-15 12:37:38 -0800889 return 0;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400890
891 // Experimental (for P016 and P010)
892 case kR_16_GrPixelConfig:
893 return 2;
894 case kRG_1616_GrPixelConfig:
895 return 4;
bsalomonf46a1242015-12-15 12:37:38 -0800896 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400897 SK_ABORT("Invalid pixel config");
csmartdalton6aa0e112017-02-08 16:14:11 -0500898 return 0;
bsalomonf46a1242015-12-15 12:37:38 -0800899}
900
Brian Salomone05ba5a2019-04-08 11:59:07 -0400901bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
902 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
903 size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400904 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
905 GrPixelConfig texConfig = glTex->config();
906 SkASSERT(this->caps()->isConfigTexturable(texConfig));
907
Jim Van Verth1676cb92019-01-15 13:24:45 -0500908 // Can't transfer compressed data
909 SkASSERT(!GrPixelConfigIsCompressed(glTex->config()));
910
Brian Salomonc320b152018-02-20 14:05:36 -0500911 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400912 return false;
913 }
914
Hal Canaryc36f7e72018-06-18 15:50:09 -0400915 static_assert(sizeof(int) == sizeof(int32_t), "");
916 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400917 return false;
918 }
919
Brian Salomond978b902019-02-07 15:09:18 -0500920 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400921
922 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500923 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500925 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400926
Greg Daniel660cc992017-06-26 14:55:05 -0400927 SkDEBUGCODE(
928 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
929 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
930 SkASSERT(bounds.contains(subRect));
931 )
932
Brian Salomonc320b152018-02-20 14:05:36 -0500933 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400934 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400935 if (!rowBytes) {
936 rowBytes = trimRowBytes;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400937 }
Greg Daniel660cc992017-06-26 14:55:05 -0400938 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700939 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400940 return false;
941 }
942
943 bool restoreGLRowLength = false;
944 if (trimRowBytes != rowBytes) {
945 // we should have checked for this support already
946 SkASSERT(this->glCaps().unpackRowLengthSupport());
947 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
948 restoreGLRowLength = true;
949 }
950
951 // Internal format comes from the texture desc.
952 GrGLenum internalFormat;
953 // External format and type come from the upload data.
954 GrGLenum externalFormat;
955 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -0500956 auto bufferAsConfig = GrColorTypeToPixelConfig(bufferColorType, GrSRGBEncoded::kNo);
957 if (!this->glCaps().getTexImageFormats(texConfig, bufferAsConfig, &internalFormat,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400958 &externalFormat, &externalType)) {
959 return false;
960 }
961
962 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
963 GL_CALL(TexSubImage2D(glTex->target(),
964 0,
965 left, top,
966 width,
967 height,
968 externalFormat, externalType,
969 pixels));
970
971 if (restoreGLRowLength) {
972 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
973 }
974
975 return true;
976}
977
Brian Salomon26de56e2019-04-10 12:14:26 -0400978bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
979 GrColorType dstColorType, GrGpuBuffer* transferBuffer,
980 size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400981 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
982 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400983 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomon26de56e2019-04-10 12:14:26 -0400984 return this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType,
985 offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400986}
987
cblume55f2d2d2016-02-26 13:20:48 -0800988/**
989 * Creates storage space for the texture and fills it with texels.
990 *
Brian Salomond1eaf492017-05-18 10:02:08 -0400991 * @param config Pixel config of the texture.
cblume55f2d2d2016-02-26 13:20:48 -0800992 * @param interface The GL interface in use.
cblume790d5132016-02-29 11:13:29 -0800993 * @param caps The capabilities of the GL device.
Jim Van Verth1676cb92019-01-15 13:24:45 -0500994 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
brianosman81a84852016-09-12 09:05:14 -0700995 * @param internalFormat The data format used for the internal storage of the texture. May be sized.
996 * @param internalFormatForTexStorage The data format used for the TexStorage API. Must be sized.
cblume55f2d2d2016-02-26 13:20:48 -0800997 * @param externalFormat The data format used for the external storage of the texture.
998 * @param externalType The type of the data used for the external storage of the texture.
999 * @param texels The texel data of the texture being created.
Jim Van Verth1676cb92019-01-15 13:24:45 -05001000 * @param mipLevelCount Number of mipmap levels
cblume55f2d2d2016-02-26 13:20:48 -08001001 * @param baseWidth The width of the texture's base mipmap level
1002 * @param baseHeight The height of the texture's base mipmap level
cblume55f2d2d2016-02-26 13:20:48 -08001003 */
Robert Phillips92de6312017-05-23 07:43:48 -04001004static bool allocate_and_populate_texture(GrPixelConfig config,
1005 const GrGLInterface& interface,
1006 const GrGLCaps& caps,
1007 GrGLenum target,
1008 GrGLenum internalFormat,
1009 GrGLenum internalFormatForTexStorage,
1010 GrGLenum externalFormat,
1011 GrGLenum externalType,
Robert Phillips590533f2017-07-11 14:22:35 -04001012 const GrMipLevel texels[], int mipLevelCount,
Robert Phillips92de6312017-05-23 07:43:48 -04001013 int baseWidth, int baseHeight) {
cblume55f2d2d2016-02-26 13:20:48 -08001014 CLEAR_ERROR_BEFORE_ALLOC(&interface);
cblume790d5132016-02-29 11:13:29 -08001015
Brian Salomon7609ac62019-03-22 12:17:27 -04001016 if (caps.isConfigTexSupportEnabled(config)) {
cblume790d5132016-02-29 11:13:29 -08001017 // We never resize or change formats of textures.
cblume55f2d2d2016-02-26 13:20:48 -08001018 GL_ALLOC_CALL(&interface,
Robert Phillips590533f2017-07-11 14:22:35 -04001019 TexStorage2D(target, SkTMax(mipLevelCount, 1), internalFormatForTexStorage,
Brian Salomond1eaf492017-05-18 10:02:08 -04001020 baseWidth, baseHeight));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001021 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume55f2d2d2016-02-26 13:20:48 -08001022 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001023 return false;
cblume790d5132016-02-29 11:13:29 -08001024 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001025 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
cblume790d5132016-02-29 11:13:29 -08001026 const void* currentMipData = texels[currentMipLevel].fPixels;
1027 if (currentMipData == nullptr) {
1028 continue;
1029 }
1030 int twoToTheMipLevel = 1 << currentMipLevel;
Brian Salomond1eaf492017-05-18 10:02:08 -04001031 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1032 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
cblume790d5132016-02-29 11:13:29 -08001033
1034 GR_GL_CALL(&interface,
1035 TexSubImage2D(target,
1036 currentMipLevel,
1037 0, // left
1038 0, // top
1039 currentWidth,
1040 currentHeight,
1041 externalFormat, externalType,
1042 currentMipData));
1043 }
bsalomone699d0c2016-03-09 06:25:15 -08001044 return true;
cblume790d5132016-02-29 11:13:29 -08001045 }
1046 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001047 if (!mipLevelCount) {
cblume790d5132016-02-29 11:13:29 -08001048 GL_ALLOC_CALL(&interface,
1049 TexImage2D(target,
bsalomone699d0c2016-03-09 06:25:15 -08001050 0,
cblume790d5132016-02-29 11:13:29 -08001051 internalFormat,
bsalomone699d0c2016-03-09 06:25:15 -08001052 baseWidth,
1053 baseHeight,
cblume790d5132016-02-29 11:13:29 -08001054 0, // border
1055 externalFormat, externalType,
bsalomone699d0c2016-03-09 06:25:15 -08001056 nullptr));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001057 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
cblume790d5132016-02-29 11:13:29 -08001058 if (error != GR_GL_NO_ERROR) {
bsalomone699d0c2016-03-09 06:25:15 -08001059 return false;
1060 }
1061 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001062 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -08001063 int twoToTheMipLevel = 1 << currentMipLevel;
1064 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1065 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
Greg Daniel8b059bd2017-09-28 20:46:45 +00001066 const void* currentMipData = texels[currentMipLevel].fPixels;
bsalomone699d0c2016-03-09 06:25:15 -08001067 // Even if curremtMipData is nullptr, continue to call TexImage2D.
1068 // This will allocate texture memory which we can later populate.
1069 GL_ALLOC_CALL(&interface,
1070 TexImage2D(target,
1071 currentMipLevel,
1072 internalFormat,
1073 currentWidth,
1074 currentHeight,
1075 0, // border
1076 externalFormat, externalType,
1077 currentMipData));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001078 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
bsalomone699d0c2016-03-09 06:25:15 -08001079 if (error != GR_GL_NO_ERROR) {
1080 return false;
1081 }
cblume790d5132016-02-29 11:13:29 -08001082 }
cblume55f2d2d2016-02-26 13:20:48 -08001083 }
1084 }
bsalomone699d0c2016-03-09 06:25:15 -08001085 return true;
cblume55f2d2d2016-02-26 13:20:48 -08001086}
1087
1088/**
Jim Van Verth1676cb92019-01-15 13:24:45 -05001089 * Creates storage space for the texture and fills it with texels.
1090 *
1091 * @param config Compressed pixel config of the texture.
1092 * @param interface The GL interface in use.
1093 * @param caps The capabilities of the GL device.
1094 * @param target Which bound texture to target (GR_GL_TEXTURE_2D, e.g.)
1095 * @param internalFormat The data format used for the internal storage of the texture.
1096 * @param texels The texel data of the texture being created.
1097 * @param mipLevelCount Number of mipmap levels
1098 * @param baseWidth The width of the texture's base mipmap level
1099 * @param baseHeight The height of the texture's base mipmap level
1100 */
1101static bool allocate_and_populate_compressed_texture(GrPixelConfig config,
1102 const GrGLInterface& interface,
1103 const GrGLCaps& caps,
1104 GrGLenum target, GrGLenum internalFormat,
1105 const GrMipLevel texels[], int mipLevelCount,
1106 int baseWidth, int baseHeight) {
1107 CLEAR_ERROR_BEFORE_ALLOC(&interface);
Robert Phillips8043f322019-05-31 08:11:36 -04001108 SkASSERT(GrGLFormatIsCompressed(internalFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001109
1110 bool useTexStorage = caps.isConfigTexSupportEnabled(config);
1111 // We can only use TexStorage if we know we will not later change the storage requirements.
1112 // This means if we may later want to add mipmaps, we cannot use TexStorage.
1113 // Right now, we cannot know if we will later add mipmaps or not.
1114 // The only time we can use TexStorage is when we already have the
1115 // mipmaps.
1116 useTexStorage &= mipLevelCount > 1;
1117
1118 if (useTexStorage) {
1119 // We never resize or change formats of textures.
1120 GL_ALLOC_CALL(&interface,
1121 TexStorage2D(target,
1122 mipLevelCount,
1123 internalFormat,
1124 baseWidth, baseHeight));
1125 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1126 if (error != GR_GL_NO_ERROR) {
1127 return false;
1128 } else {
1129 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1130 const void* currentMipData = texels[currentMipLevel].fPixels;
1131 if (currentMipData == nullptr) {
1132 // Compressed textures require data for every level
1133 return false;
1134 }
1135
1136 int twoToTheMipLevel = 1 << currentMipLevel;
1137 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1138 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1139
1140 // Make sure that the width and height that we pass to OpenGL
1141 // is a multiple of the block size.
Robert Phillips8043f322019-05-31 08:11:36 -04001142 size_t dataSize = GrGLFormatCompressedDataSize(internalFormat,
1143 currentWidth, currentHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001144 GR_GL_CALL(&interface, CompressedTexSubImage2D(target,
1145 currentMipLevel,
1146 0, // left
1147 0, // top
1148 currentWidth,
1149 currentHeight,
1150 internalFormat,
1151 SkToInt(dataSize),
1152 currentMipData));
1153 }
1154 }
1155 } else {
1156 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1157 const void* currentMipData = texels[currentMipLevel].fPixels;
1158 if (currentMipData == nullptr) {
1159 // Compressed textures require data for every level
1160 return false;
1161 }
1162
1163 int twoToTheMipLevel = 1 << currentMipLevel;
1164 int currentWidth = SkTMax(1, baseWidth / twoToTheMipLevel);
1165 int currentHeight = SkTMax(1, baseHeight / twoToTheMipLevel);
1166
1167 // Make sure that the width and height that we pass to OpenGL
1168 // is a multiple of the block size.
Robert Phillips8043f322019-05-31 08:11:36 -04001169 size_t dataSize = GrGLFormatCompressedDataSize(internalFormat, baseWidth, baseHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001170
1171 GL_ALLOC_CALL(&interface,
1172 CompressedTexImage2D(target,
1173 currentMipLevel,
1174 internalFormat,
1175 currentWidth,
1176 currentHeight,
1177 0, // border
1178 SkToInt(dataSize),
1179 currentMipData));
1180
1181 GrGLenum error = CHECK_ALLOC_ERROR(&interface);
1182 if (error != GR_GL_NO_ERROR) {
1183 return false;
1184 }
1185 }
1186 }
1187
1188 return true;
1189}
1190/**
cblume55f2d2d2016-02-26 13:20:48 -08001191 * After a texture is created, any state which was altered during its creation
1192 * needs to be restored.
1193 *
1194 * @param interface The GL interface to use.
1195 * @param caps The capabilities of the GL device.
1196 * @param restoreGLRowLength Should the row length unpacking be restored?
1197 * @param glFlipY Did GL flip the texture vertically?
1198 */
1199static void restore_pixelstore_state(const GrGLInterface& interface, const GrGLCaps& caps,
Brian Salomona9b04b92018-06-01 15:04:28 -04001200 bool restoreGLRowLength) {
cblume55f2d2d2016-02-26 13:20:48 -08001201 if (restoreGLRowLength) {
1202 SkASSERT(caps.unpackRowLengthSupport());
1203 GR_GL_CALL(&interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1204 }
cblume55f2d2d2016-02-26 13:20:48 -08001205}
1206
Brian Osman9b560242017-09-05 15:34:52 -04001207void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -05001208 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
1209 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
1210 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
1211 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -04001212 }
Brian Osman9b560242017-09-05 15:34:52 -04001213}
1214
Brian Salomonc320b152018-02-20 14:05:36 -05001215// TODO: Make this take a GrColorType instead of dataConfig. This requires updating GrGLCaps to
1216// convert from GrColorType to externalFormat/externalType GLenum values.
Brian Salomona9b04b92018-06-01 15:04:28 -04001217bool GrGLGpu::uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
1218 UploadType uploadType, int left, int top, int width, int height,
1219 GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001220 GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001221 // If we're uploading compressed data then we should be using uploadCompressedTexData
1222 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
1223
Brian Salomond1eaf492017-05-18 10:02:08 -04001224 SkASSERT(this->caps()->isConfigTexturable(texConfig));
Greg Daniel660cc992017-06-26 14:55:05 -04001225 SkDEBUGCODE(
1226 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1227 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1228 SkASSERT(bounds.contains(subRect));
1229 )
Robert Phillips590533f2017-07-11 14:22:35 -04001230 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001231 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001232
Brian Osman9b560242017-09-05 15:34:52 -04001233 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001234
cblume55f2d2d2016-02-26 13:20:48 -08001235 // texels is const.
1236 // But we may need to flip the texture vertically to prepare it.
1237 // Rather than flip in place and alter the incoming data,
1238 // we allocate a new buffer to flip into.
1239 // This means we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -04001240 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
1241
1242 if (mipLevelCount) {
1243 texelsShallowCopy.reset(mipLevelCount);
1244 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
1245 }
cblume55f2d2d2016-02-26 13:20:48 -08001246
cblume55f2d2d2016-02-26 13:20:48 -08001247 const GrGLInterface* interface = this->glInterface();
1248 const GrGLCaps& caps = this->glCaps();
1249
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001250 size_t bpp = GrBytesPerPixel(dataConfig);
cblume55f2d2d2016-02-26 13:20:48 -08001251
1252 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001253 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001254 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001255
bsalomon5b30c6f2015-12-17 14:17:34 -08001256 // Internal format comes from the texture desc.
bsalomon76148af2016-01-12 11:13:47 -08001257 GrGLenum internalFormat;
bsalomon5b30c6f2015-12-17 14:17:34 -08001258 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001259 GrGLenum externalFormat;
1260 GrGLenum externalType;
Brian Salomond1eaf492017-05-18 10:02:08 -04001261 if (!this->glCaps().getTexImageFormats(texConfig, dataConfig, &internalFormat, &externalFormat,
1262 &externalType)) {
bsalomon76148af2016-01-12 11:13:47 -08001263 return false;
1264 }
brianosman81a84852016-09-12 09:05:14 -07001265 // TexStorage requires a sized format, and internalFormat may or may not be
Brian Salomond1eaf492017-05-18 10:02:08 -04001266 GrGLenum internalFormatForTexStorage = this->glCaps().configSizedInternalFormat(texConfig);
brianosman81a84852016-09-12 09:05:14 -07001267
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001268 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001269 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001270 * because our srcData has extra bytes past each row. If so, we need
1271 * to trim those off here, since GL ES may not let us specify
1272 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001274 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001275
1276 // in case we need a temporary, trimmed copy of the src pixels
1277 SkAutoSMalloc<128 * 128> tempStorage;
1278
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001279 if (mipMapsStatus) {
1280 *mipMapsStatus = GrMipMapsStatus::kValid;
Greg Daniel834f1202017-10-09 15:06:20 -04001281 }
1282
Robert Phillips931f7512017-11-02 12:57:18 -04001283 const bool usesMips = mipLevelCount > 1;
1284
cblume55f2d2d2016-02-26 13:20:48 -08001285 // find the combined size of all the mip levels and the relative offset of
1286 // each into the collective buffer
Robert Phillips931f7512017-11-02 12:57:18 -04001287 bool willNeedData = false;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001288 size_t combinedBufferSize = 0;
1289 SkTArray<size_t> individualMipOffsets(mipLevelCount);
Robert Phillips590533f2017-07-11 14:22:35 -04001290 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001291 if (texelsShallowCopy[currentMipLevel].fPixels) {
1292 int twoToTheMipLevel = 1 << currentMipLevel;
1293 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1294 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
Robert Phillips931f7512017-11-02 12:57:18 -04001295 const size_t trimRowBytes = currentWidth * bpp;
1296 const size_t trimmedSize = trimRowBytes * currentHeight;
1297
1298 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1299 ? texelsShallowCopy[currentMipLevel].fRowBytes
1300 : trimRowBytes;
1301
Brian Salomona9b04b92018-06-01 15:04:28 -04001302 if (((!caps.unpackRowLengthSupport() || usesMips) && trimRowBytes != rowBytes)) {
Robert Phillips931f7512017-11-02 12:57:18 -04001303 willNeedData = true;
1304 }
1305
Greg Daniel55afd6d2017-09-29 09:32:44 -04001306 individualMipOffsets.push_back(combinedBufferSize);
1307 combinedBufferSize += trimmedSize;
1308 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001309 if (mipMapsStatus) {
1310 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel834f1202017-10-09 15:06:20 -04001311 }
Greg Daniel55afd6d2017-09-29 09:32:44 -04001312 individualMipOffsets.push_back(0);
1313 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001314 }
1315 if (mipMapsStatus && mipLevelCount <= 1) {
1316 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001317 }
Robert Phillips931f7512017-11-02 12:57:18 -04001318 char* buffer = nullptr;
1319 if (willNeedData) {
1320 buffer = (char*)tempStorage.reset(combinedBufferSize);
1321 }
cblume55f2d2d2016-02-26 13:20:48 -08001322
Robert Phillips590533f2017-07-11 14:22:35 -04001323 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001324 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1325 continue;
1326 }
cblume55f2d2d2016-02-26 13:20:48 -08001327 int twoToTheMipLevel = 1 << currentMipLevel;
1328 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1329 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1330 const size_t trimRowBytes = currentWidth * bpp;
1331
1332 /*
1333 * check whether to allocate a temporary buffer for flipping y or
1334 * because our srcData has extra bytes past each row. If so, we need
1335 * to trim those off here, since GL ES may not let us specify
1336 * GL_UNPACK_ROW_LENGTH.
1337 */
1338 restoreGLRowLength = false;
1339
Greg Daniel55afd6d2017-09-29 09:32:44 -04001340 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
1341 ? texelsShallowCopy[currentMipLevel].fRowBytes
1342 : trimRowBytes;
Greg Daniel660cc992017-06-26 14:55:05 -04001343
ericrk154349b2016-05-10 14:36:53 -07001344 // TODO: This optimization should be enabled with or without mips.
1345 // For use with mips, we must set GR_GL_UNPACK_ROW_LENGTH once per
1346 // mip level, before calling glTexImage2D.
Brian Salomona9b04b92018-06-01 15:04:28 -04001347 if (caps.unpackRowLengthSupport() && !usesMips) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001348 // can't use this for flipping, only non-neg values allowed. :(
1349 if (rowBytes != trimRowBytes) {
1350 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
cblume55f2d2d2016-02-26 13:20:48 -08001351 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001352 restoreGLRowLength = true;
1353 }
Brian Salomona9b04b92018-06-01 15:04:28 -04001354 } else if (trimRowBytes != rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001355 // copy data into our new storage, skipping the trailing bytes
1356 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001357 char* dst = buffer + individualMipOffsets[currentMipLevel];
Brian Salomona6948702018-06-01 15:33:20 -04001358 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001359 // now point data to our copied version
1360 texelsShallowCopy[currentMipLevel].fPixels = buffer +
Greg Daniel55afd6d2017-09-29 09:32:44 -04001361 individualMipOffsets[currentMipLevel];
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001362 texelsShallowCopy[currentMipLevel].fRowBytes = trimRowBytes;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001363 }
bsalomone699d0c2016-03-09 06:25:15 -08001364 }
1365
Robert Phillips590533f2017-07-11 14:22:35 -04001366 if (mipLevelCount) {
Brian Salomond1eaf492017-05-18 10:02:08 -04001367 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, config_alignment(texConfig)));
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001368 }
cblume55f2d2d2016-02-26 13:20:48 -08001369
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001370 bool succeeded = true;
Brian Salomond1eaf492017-05-18 10:02:08 -04001371 if (kNewTexture_UploadType == uploadType) {
1372 if (0 == left && 0 == top && texWidth == width && texHeight == height) {
Robert Phillips92de6312017-05-23 07:43:48 -04001373 succeeded = allocate_and_populate_texture(
Brian Salomond1eaf492017-05-18 10:02:08 -04001374 texConfig, *interface, caps, target, internalFormat,
Robert Phillips590533f2017-07-11 14:22:35 -04001375 internalFormatForTexStorage, externalFormat, externalType,
1376 texelsShallowCopy, mipLevelCount, width, height);
Brian Salomond1eaf492017-05-18 10:02:08 -04001377 } else {
1378 succeeded = false;
1379 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001380 } else {
Robert Phillips590533f2017-07-11 14:22:35 -04001381 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -04001382 if (!texelsShallowCopy[currentMipLevel].fPixels) {
1383 continue;
1384 }
cblume55f2d2d2016-02-26 13:20:48 -08001385 int twoToTheMipLevel = 1 << currentMipLevel;
1386 int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1387 int currentHeight = SkTMax(1, height / twoToTheMipLevel);
cblume55f2d2d2016-02-26 13:20:48 -08001388
1389 GL_CALL(TexSubImage2D(target,
1390 currentMipLevel,
1391 left, top,
1392 currentWidth,
1393 currentHeight,
1394 externalFormat, externalType,
1395 texelsShallowCopy[currentMipLevel].fPixels));
1396 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001397 }
1398
Brian Salomona9b04b92018-06-01 15:04:28 -04001399 restore_pixelstore_state(*interface, caps, restoreGLRowLength);
cblume55f2d2d2016-02-26 13:20:48 -08001400
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001401 return succeeded;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001402}
1403
Jim Van Verth1676cb92019-01-15 13:24:45 -05001404bool GrGLGpu::uploadCompressedTexData(GrPixelConfig texConfig, int texWidth, int texHeight,
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001405 GrGLenum target,
Jim Van Verth1676cb92019-01-15 13:24:45 -05001406 const GrMipLevel texels[], int mipLevelCount,
1407 GrMipMapsStatus* mipMapsStatus) {
1408 SkASSERT(this->caps()->isConfigTexturable(texConfig));
1409
1410 const GrGLInterface* interface = this->glInterface();
1411 const GrGLCaps& caps = this->glCaps();
1412
1413 // We only need the internal format for compressed 2D textures.
1414 GrGLenum internalFormat;
1415 if (!caps.getCompressedTexImageFormats(texConfig, &internalFormat)) {
1416 return false;
1417 }
1418
Greg Kaiser73bfb892019-02-11 09:03:41 -08001419 if (mipMapsStatus) {
1420 if (mipLevelCount <= 1) {
1421 *mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1422 } else {
1423 *mipMapsStatus = GrMipMapsStatus::kValid;
1424 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001425 }
1426
1427 return allocate_and_populate_compressed_texture(texConfig, *interface, caps, target,
1428 internalFormat, texels, mipLevelCount,
1429 texWidth, texHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001430}
1431
bsalomon424cc262015-05-22 10:37:30 -07001432static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001433 int sampleCount,
1434 GrGLenum format,
1435 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001436 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001437 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001438 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001439 case GrGLCaps::kStandard_MSFBOType:
vbuzinovdded6962015-06-12 08:59:45 -07001440 case GrGLCaps::kMixedSamples_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001441 GL_ALLOC_CALL(ctx.interface(),
1442 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1443 sampleCount,
1444 format,
1445 width, height));
1446 break;
1447 case GrGLCaps::kES_Apple_MSFBOType:
1448 GL_ALLOC_CALL(ctx.interface(),
1449 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1450 sampleCount,
1451 format,
1452 width, height));
1453 break;
1454 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1455 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1456 GL_ALLOC_CALL(ctx.interface(),
1457 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1458 sampleCount,
1459 format,
1460 width, height));
1461 break;
1462 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001463 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001464 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001465 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001466 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001467}
1468
egdanielb0e1be22015-04-22 13:27:39 -07001469bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
bsalomon091f60c2015-11-10 11:54:56 -08001470 const GrGLTextureInfo& texInfo,
bsalomonb15b4c12014-10-29 12:41:57 -07001471 GrGLRenderTarget::IDDesc* idDesc) {
1472 idDesc->fMSColorRenderbufferID = 0;
egdanield803f272015-03-18 13:01:52 -07001473 idDesc->fRTFBOID = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -07001474 idDesc->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
egdanield803f272015-03-18 13:01:52 -07001475 idDesc->fTexFBOID = 0;
robertphillips76948d42016-05-04 12:47:41 -07001476 SkASSERT((GrGLCaps::kMixedSamples_MSFBOType == this->glCaps().msFBOType()) ==
1477 this->caps()->usesMixedSamples());
Brian Salomonbdecacf2018-02-02 20:32:49 -05001478 idDesc->fIsMixedSampled = desc.fSampleCnt > 1 && this->caps()->usesMixedSamples();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001479
1480 GrGLenum status;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001481
bsalomona11e5fc2015-12-18 07:59:41 -08001482 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001483
Brian Salomonbdecacf2018-02-02 20:32:49 -05001484 if (desc.fSampleCnt > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001485 goto FAILED;
1486 }
1487
egdanield803f272015-03-18 13:01:52 -07001488 GL_CALL(GenFramebuffers(1, &idDesc->fTexFBOID));
1489 if (!idDesc->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001490 goto FAILED;
1491 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001492
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001493 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1494 // the texture bound to the other. The exception is the IMG multisample extension. With this
1495 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1496 // rendered from.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001497 if (desc.fSampleCnt > 1 && this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07001498 GL_CALL(GenFramebuffers(1, &idDesc->fRTFBOID));
bsalomonb15b4c12014-10-29 12:41:57 -07001499 GL_CALL(GenRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
egdanield803f272015-03-18 13:01:52 -07001500 if (!idDesc->fRTFBOID ||
bsalomona11e5fc2015-12-18 07:59:41 -08001501 !idDesc->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502 goto FAILED;
1503 }
Brian Salomon93348dd2018-08-29 12:56:23 -04001504 this->glCaps().getRenderbufferFormat(desc.fConfig, &colorRenderbufferFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001505 } else {
egdanield803f272015-03-18 13:01:52 -07001506 idDesc->fRTFBOID = idDesc->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 }
1508
egdanield803f272015-03-18 13:01:52 -07001509 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001510 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanield803f272015-03-18 13:01:52 -07001511 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001512 SkASSERT(desc.fSampleCnt > 1);
bsalomonb15b4c12014-10-29 12:41:57 -07001513 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, idDesc->fMSColorRenderbufferID));
bsalomon424cc262015-05-22 10:37:30 -07001514 if (!renderbuffer_storage_msaa(*fGLContext,
bsalomonb15b4c12014-10-29 12:41:57 -07001515 desc.fSampleCnt,
bsalomona11e5fc2015-12-18 07:59:41 -08001516 colorRenderbufferFormat,
bsalomonb15b4c12014-10-29 12:41:57 -07001517 desc.fWidth, desc.fHeight)) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 goto FAILED;
1519 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001520 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001521 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001522 GR_GL_COLOR_ATTACHMENT0,
1523 GR_GL_RENDERBUFFER,
1524 idDesc->fMSColorRenderbufferID));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001525 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001526 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1527 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1528 goto FAILED;
1529 }
bsalomon424cc262015-05-22 10:37:30 -07001530 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001531 }
1532 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07001533 this->bindFramebuffer(GR_GL_FRAMEBUFFER, idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001534
Brian Salomonbdecacf2018-02-02 20:32:49 -05001535 if (this->glCaps().usesImplicitMSAAResolve() && desc.fSampleCnt > 1) {
egdanield803f272015-03-18 13:01:52 -07001536 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001537 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001538 texInfo.fTarget,
1539 texInfo.fID, 0, desc.fSampleCnt));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001540 } else {
egdanield803f272015-03-18 13:01:52 -07001541 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001542 GR_GL_COLOR_ATTACHMENT0,
bsalomon091f60c2015-11-10 11:54:56 -08001543 texInfo.fTarget,
1544 texInfo.fID, 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001545 }
Brian Salomon0ec981b2017-05-15 13:48:50 -04001546 if (!this->glCaps().isConfigVerifiedColorAttachment(desc.fConfig)) {
egdanield803f272015-03-18 13:01:52 -07001547 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +00001548 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
1549 goto FAILED;
1550 }
bsalomon424cc262015-05-22 10:37:30 -07001551 fGLContext->caps()->markConfigAsValidColorAttachment(desc.fConfig);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001552 }
1553
1554 return true;
1555
1556FAILED:
bsalomonb15b4c12014-10-29 12:41:57 -07001557 if (idDesc->fMSColorRenderbufferID) {
1558 GL_CALL(DeleteRenderbuffers(1, &idDesc->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001559 }
egdanield803f272015-03-18 13:01:52 -07001560 if (idDesc->fRTFBOID != idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001561 this->deleteFramebuffer(idDesc->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001562 }
egdanield803f272015-03-18 13:01:52 -07001563 if (idDesc->fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07001564 this->deleteFramebuffer(idDesc->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001565 }
1566 return false;
1567}
1568
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001569// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001570static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001571// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001572 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001573}
1574
Brian Salomone2826ab2019-06-04 15:58:31 -04001575static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
1576 const GrGLInterface* interface, const GrGLTextureInfo& info) {
cblume55f2d2d2016-02-26 13:20:48 -08001577 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1578 // drivers have a bug where an FBO won't be complete if it includes a
1579 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001580 GrGLTextureParameters::SamplerOverriddenState state;
1581 state.fMinFilter = GR_GL_NEAREST;
1582 state.fMagFilter = GR_GL_NEAREST;
1583 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1584 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
1585 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1586 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1587 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1588 GR_GL_CALL(interface, TexParameteri(info.fTarget, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
1589 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001590}
1591
Robert Phillips67d52cf2017-06-05 13:38:13 -04001592sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
1593 SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -04001594 const GrMipLevel texels[],
1595 int mipLevelCount) {
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001596 // We fail if the MSAA was requested and is not available.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001597 if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt > 1) {
tfarina38406c82014-10-31 07:11:12 -07001598 //SkDebugf("MSAA RT requested but not supported on this platform.");
bsalomon@google.com8a70eef2013-03-19 13:58:55 +00001599 return return_null_texture();
1600 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001601
Robert Phillips42dda082019-05-14 13:29:45 -04001602 GrGLenum glFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
1603
Jim Van Verth1676cb92019-01-15 13:24:45 -05001604 bool performClear = (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) &&
Robert Phillips8043f322019-05-31 08:11:36 -04001605 !GrGLFormatIsCompressed(glFormat);
Brian Salomond17b4a62017-05-23 16:53:47 -04001606
Robert Phillips590533f2017-07-11 14:22:35 -04001607 GrMipLevel zeroLevel;
Brian Salomond17b4a62017-05-23 16:53:47 -04001608 std::unique_ptr<uint8_t[]> zeros;
Brian Salomond17b4a62017-05-23 16:53:47 -04001609 if (performClear && !this->glCaps().clearTextureSupport() &&
Brian Salomon57111332018-02-05 15:55:54 -05001610 !this->glCaps().canConfigBeFBOColorAttachment(desc.fConfig)) {
Robert Phillips28a5a432019-06-07 12:46:21 -04001611 size_t rowSize = GrGLBytesPerFormat(glFormat) * desc.fWidth;
Brian Salomond17b4a62017-05-23 16:53:47 -04001612 size_t size = rowSize * desc.fHeight;
1613 zeros.reset(new uint8_t[size]);
1614 memset(zeros.get(), 0, size);
Robert Phillips590533f2017-07-11 14:22:35 -04001615 zeroLevel.fPixels = zeros.get();
1616 zeroLevel.fRowBytes = 0;
1617 texels = &zeroLevel;
1618 mipLevelCount = 1;
Brian Salomond17b4a62017-05-23 16:53:47 -04001619 performClear = false;
1620 }
1621
Brian Salomond34edf32017-05-19 15:45:48 -04001622 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623
bsalomonb15b4c12014-10-29 12:41:57 -07001624 GrGLTexture::IDDesc idDesc;
kkinnunen2e6055b2016-04-22 01:48:29 -07001625 idDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001626 GrMipMapsStatus mipMapsStatus;
Brian Salomone2826ab2019-06-04 15:58:31 -04001627 GrGLTextureParameters::SamplerOverriddenState initialState;
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001628 if (!this->createTextureImpl(desc, &idDesc.fInfo,
1629 isRenderTarget ? GrRenderable::kYes : GrRenderable::kNo,
Brian Salomone2826ab2019-06-04 15:58:31 -04001630 &initialState, texels, mipLevelCount, &mipMapsStatus)) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001631 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001632 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001633
Robert Phillips67d52cf2017-06-05 13:38:13 -04001634 sk_sp<GrGLTexture> tex;
Brian Salomond34edf32017-05-19 15:45:48 -04001635 if (isRenderTarget) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001636 // unbind the texture from the texture unit before binding it to the frame buffer
bsalomon091f60c2015-11-10 11:54:56 -08001637 GL_CALL(BindTexture(idDesc.fInfo.fTarget, 0));
bsalomon5236cf42015-01-14 10:42:08 -08001638 GrGLRenderTarget::IDDesc rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001639
kkinnunen2e6055b2016-04-22 01:48:29 -07001640 if (!this->createRenderTargetObjects(desc, idDesc.fInfo, &rtIDDesc)) {
bsalomon091f60c2015-11-10 11:54:56 -08001641 GL_CALL(DeleteTextures(1, &idDesc.fInfo.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 return return_null_texture();
1643 }
Robert Phillips67d52cf2017-06-05 13:38:13 -04001644 tex = sk_make_sp<GrGLTextureRenderTarget>(this, budgeted, desc, idDesc, rtIDDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001645 mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001646 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001647 } else {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001648 tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, idDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001650 // The non-sampler params are still at their default values.
1651 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1652 fResetTimestampForTextureParameters);
reed@google.comac10a2d2010-12-22 21:39:39 +00001653#ifdef TRACE_TEXTURE_CREATION
tfarina38406c82014-10-31 07:11:12 -07001654 SkDebugf("--- new texture [%d] size=(%d %d) config=%d\n",
brianosmanc4459f62016-07-19 08:02:20 -07001655 idDesc.fInfo.fID, desc.fWidth, desc.fHeight, desc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +00001656#endif
Brian Salomond17b4a62017-05-23 16:53:47 -04001657 if (tex && performClear) {
1658 if (this->glCaps().clearTextureSupport()) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001659 static constexpr uint32_t kZero = 0;
Brian Salomon57111332018-02-05 15:55:54 -05001660 GL_CALL(ClearTexImage(tex->textureID(), 0, GR_GL_RGBA, GR_GL_UNSIGNED_BYTE, &kZero));
Brian Salomond17b4a62017-05-23 16:53:47 -04001661 } else {
Greg Danielacd66b42019-05-22 16:29:12 -04001662 this->bindSurfaceFBOForPixelOps(tex.get(), GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Brian Salomond818ebf2018-07-02 14:08:49 +00001663 this->disableScissor();
Brian Salomond17b4a62017-05-23 16:53:47 -04001664 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001665 this->flushColorWrite(true);
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00001666 this->flushClearColor(0, 0, 0, 0);
Brian Salomond17b4a62017-05-23 16:53:47 -04001667 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
Robert Phillips67d52cf2017-06-05 13:38:13 -04001668 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, tex.get());
Brian Salomond17b4a62017-05-23 16:53:47 -04001669 fHWBoundRenderTargetUniqueID.makeInvalid();
1670 }
1671 }
Kevin Lubickf7621cb2018-04-16 15:51:44 -04001672 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001673}
1674
1675namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001676
egdaniel8dc7c3a2015-04-16 11:22:42 -07001677const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001678
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001679void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001680 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001681
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001682 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001683 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001684 (kUnknownBitCount == format->fTotalBits));
1685 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001686 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001687 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1688 (GrGLint*)&format->fStencilBits);
1689 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001690 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001691 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1692 (GrGLint*)&format->fTotalBits);
1693 format->fTotalBits += format->fStencilBits;
1694 } else {
1695 format->fTotalBits = format->fStencilBits;
1696 }
1697 }
1698}
1699}
1700
egdanielff1d5472015-09-10 08:37:20 -07001701int GrGLGpu::getCompatibleStencilIndex(GrPixelConfig config) {
bsalomon100b8f82015-10-28 08:37:44 -07001702 static const int kSize = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001703 SkASSERT(this->caps()->isConfigRenderable(config));
bsalomon30447372015-12-21 09:03:05 -08001704 if (!this->glCaps().hasStencilFormatBeenDeterminedForConfig(config)) {
1705 // Default to unsupported, set this if we find a stencil format that works.
1706 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001707
egdanielff1d5472015-09-10 08:37:20 -07001708 // Create color texture
kkinnunen546eb5c2015-12-11 00:05:33 -08001709 GrGLuint colorID = 0;
egdanielff1d5472015-09-10 08:37:20 -07001710 GL_CALL(GenTextures(1, &colorID));
Brian Salomond978b902019-02-07 15:09:18 -05001711 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
egdanielff1d5472015-09-10 08:37:20 -07001712 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1713 GR_GL_TEXTURE_MAG_FILTER,
1714 GR_GL_NEAREST));
1715 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1716 GR_GL_TEXTURE_MIN_FILTER,
1717 GR_GL_NEAREST));
1718 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1719 GR_GL_TEXTURE_WRAP_S,
1720 GR_GL_CLAMP_TO_EDGE));
1721 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D,
1722 GR_GL_TEXTURE_WRAP_T,
1723 GR_GL_CLAMP_TO_EDGE));
1724
bsalomon76148af2016-01-12 11:13:47 -08001725 GrGLenum internalFormat;
1726 GrGLenum externalFormat;
1727 GrGLenum externalType;
1728 if (!this->glCaps().getTexImageFormats(config, config, &internalFormat, &externalFormat,
1729 &externalType)) {
1730 return false;
1731 }
Brian Osman9b560242017-09-05 15:34:52 -04001732 this->unbindCpuToGpuXferBuffer();
egdanielff1d5472015-09-10 08:37:20 -07001733 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1734 GL_ALLOC_CALL(this->glInterface(), TexImage2D(GR_GL_TEXTURE_2D,
bsalomon926cb022015-12-17 18:15:11 -08001735 0,
bsalomon76148af2016-01-12 11:13:47 -08001736 internalFormat,
bsalomon100b8f82015-10-28 08:37:44 -07001737 kSize,
1738 kSize,
egdanielff1d5472015-09-10 08:37:20 -07001739 0,
bsalomon76148af2016-01-12 11:13:47 -08001740 externalFormat,
1741 externalType,
Ben Wagnera93a14a2017-08-28 10:34:05 -04001742 nullptr));
bsalomon30447372015-12-21 09:03:05 -08001743 if (GR_GL_NO_ERROR != CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001744 GL_CALL(DeleteTextures(1, &colorID));
bsalomon30447372015-12-21 09:03:05 -08001745 return -1;
egdanielff1d5472015-09-10 08:37:20 -07001746 }
1747
1748 // unbind the texture from the texture unit before binding it to the frame buffer
1749 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1750
1751 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001752 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001753 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001754 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001755 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001756 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1757 GR_GL_COLOR_ATTACHMENT0,
1758 GR_GL_TEXTURE_2D,
1759 colorID,
1760 0));
bsalomon30447372015-12-21 09:03:05 -08001761 GrGLuint sbRBID = 0;
1762 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001763
1764 // look over formats till I find a compatible one
1765 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001766 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001767 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001768 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1769 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1770 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1771 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1772 sFmt.fInternalFormat,
1773 kSize, kSize));
1774 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001775 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001776 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001777 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001778 if (sFmt.fPacked) {
1779 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1780 GR_GL_DEPTH_ATTACHMENT,
1781 GR_GL_RENDERBUFFER, sbRBID));
1782 } else {
1783 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1784 GR_GL_DEPTH_ATTACHMENT,
1785 GR_GL_RENDERBUFFER, 0));
1786 }
1787 GrGLenum status;
1788 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1789 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1790 firstWorkingStencilFormatIndex = i;
1791 break;
1792 }
egdanielff1d5472015-09-10 08:37:20 -07001793 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1794 GR_GL_STENCIL_ATTACHMENT,
1795 GR_GL_RENDERBUFFER, 0));
1796 if (sFmt.fPacked) {
1797 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1798 GR_GL_DEPTH_ATTACHMENT,
1799 GR_GL_RENDERBUFFER, 0));
1800 }
egdanielff1d5472015-09-10 08:37:20 -07001801 }
1802 }
bsalomon30447372015-12-21 09:03:05 -08001803 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001804 }
1805 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001806 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1807 this->deleteFramebuffer(fb);
bsalomon30447372015-12-21 09:03:05 -08001808 fGLContext->caps()->setStencilFormatIndexForConfig(config, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001809 }
bsalomon30447372015-12-21 09:03:05 -08001810 return this->glCaps().getStencilFormatIndexForConfig(config);
egdanielff1d5472015-09-10 08:37:20 -07001811}
1812
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001813bool GrGLGpu::createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
1814 GrRenderable renderable,
Brian Salomone2826ab2019-06-04 15:58:31 -04001815 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomondc829942018-10-23 16:07:24 -04001816 const GrMipLevel texels[], int mipLevelCount,
1817 GrMipMapsStatus* mipMapsStatus) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001818 info->fID = 0;
1819 info->fTarget = GR_GL_TEXTURE_2D;
1820 GL_CALL(GenTextures(1, &(info->fID)));
1821
1822 if (!info->fID) {
1823 return false;
1824 }
1825
Robert Phillips8043f322019-05-31 08:11:36 -04001826 info->fFormat = this->glCaps().configSizedInternalFormat(desc.fConfig);
1827
Brian Salomond978b902019-02-07 15:09:18 -05001828 this->bindTextureToScratchUnit(info->fTarget, info->fID);
erikchen9a1ed5d2016-02-10 16:32:34 -08001829
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001830 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001831 // provides a hint about how this texture will be used
1832 GL_CALL(TexParameteri(info->fTarget,
1833 GR_GL_TEXTURE_USAGE,
1834 GR_GL_FRAMEBUFFER_ATTACHMENT));
1835 }
1836
Brian Salomone2826ab2019-06-04 15:58:31 -04001837 *initialState = set_initial_texture_params(this->glInterface(), *info);
Brian Salomon2a4f9832018-03-03 22:43:43 -05001838
Jim Van Verth1676cb92019-01-15 13:24:45 -05001839 bool success = false;
Robert Phillips8043f322019-05-31 08:11:36 -04001840 if (GrGLFormatIsCompressed(info->fFormat)) {
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001841 SkASSERT(GrRenderable::kNo == renderable);
Robert Phillipsb04b6942019-05-21 17:24:31 -04001842
Jim Van Verth1676cb92019-01-15 13:24:45 -05001843 success = this->uploadCompressedTexData(desc.fConfig, desc.fWidth, desc.fHeight,
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001844 info->fTarget,
Jim Van Verth1676cb92019-01-15 13:24:45 -05001845 texels, mipLevelCount, mipMapsStatus);
1846 } else {
1847 success = this->uploadTexData(desc.fConfig, desc.fWidth, desc.fHeight, info->fTarget,
1848 kNewTexture_UploadType, 0, 0, desc.fWidth, desc.fHeight,
1849 desc.fConfig, texels, mipLevelCount, mipMapsStatus);
1850 }
1851 if (!success) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001852 GL_CALL(DeleteTextures(1, &(info->fID)));
1853 return false;
1854 }
1855 return true;
1856}
1857
egdanielec00d942015-09-14 12:56:10 -07001858GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
Brian Salomond1eaf492017-05-18 10:02:08 -04001859 int width, int height) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001860 SkASSERT(width >= rt->width());
1861 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001862
vbuzinovdded6962015-06-12 08:59:45 -07001863 int samples = rt->numStencilSamples();
egdaniel8dc7c3a2015-04-16 11:22:42 -07001864 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001865
egdanielff1d5472015-09-10 08:37:20 -07001866 int sIdx = this->getCompatibleStencilIndex(rt->config());
bsalomon62a627b2015-12-17 09:50:47 -08001867 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001868 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001869 }
egdanielff1d5472015-09-10 08:37:20 -07001870
1871 if (!sbDesc.fRenderbufferID) {
1872 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1873 }
1874 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001875 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001876 }
1877 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1878 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1879 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1880 // we do this "if" so that we don't call the multisample
1881 // version on a GL that doesn't have an MSAA extension.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001882 if (samples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001883 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
1884 samples,
1885 sFmt.fInternalFormat,
1886 width, height));
1887 } else {
1888 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1889 sFmt.fInternalFormat,
1890 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001891 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001892 }
1893 fStats.incStencilAttachmentCreates();
1894 // After sized formats we attempt an unsized format and take
1895 // whatever sizes GL gives us. In that case we query for the size.
1896 GrGLStencilAttachment::Format format = sFmt;
1897 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001898 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1899 sbDesc,
1900 width,
1901 height,
1902 samples,
1903 format);
1904 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001905}
1906
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001907////////////////////////////////////////////////////////////////////////////////
1908
Brian Salomondbf70722019-02-07 11:31:24 -05001909sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1910 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001911 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001912}
1913
Greg Danielacd66b42019-05-22 16:29:12 -04001914void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001915 GrSurfaceOrigin rtOrigin) {
1916 if (scissorState.enabled()) {
1917 GrGLIRect scissor;
Greg Danielacd66b42019-05-22 16:29:12 -04001918 scissor.setRelativeTo(rtHeight, scissorState.rect(), rtOrigin);
Brian Salomond818ebf2018-07-02 14:08:49 +00001919 // if the scissor fully contains the viewport then we fall through and
1920 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001921 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001922 if (fHWScissorSettings.fRect != scissor) {
1923 scissor.pushToGLScissor(this->glInterface());
1924 fHWScissorSettings.fRect = scissor;
1925 }
1926 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1927 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1928 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001929 }
1930 return;
1931 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001932 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001933
1934 // See fall through note above
1935 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001936}
1937
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001938void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001939 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001940#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001941 typedef GrWindowRectsState::Mode Mode;
1942 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1943 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001944
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001945 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001946 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001947 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001948 }
1949
csmartdalton7535f412016-08-23 06:51:00 -07001950 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1951 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001952 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1953 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001954
csmartdalton28341fa2016-08-17 10:00:21 -07001955 GrGLIRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001956 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001957 for (int i = 0; i < numWindows; ++i) {
Greg Danielacd66b42019-05-22 16:29:12 -04001958 glwindows[i].setRelativeTo(rt->height(), skwindows[i], origin);
csmartdalton28341fa2016-08-17 10:00:21 -07001959 }
1960
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001961 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001962 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001963
Greg Danielacd66b42019-05-22 16:29:12 -04001964 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001965#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001966}
1967
1968void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001969#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001970 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001971 return;
1972 }
1973 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001974 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001975#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001976}
1977
Brian Salomonf7232642018-09-19 08:58:08 -04001978void GrGLGpu::resolveAndGenerateMipMapsForProcessorTextures(
1979 const GrPrimitiveProcessor& primProc,
1980 const GrPipeline& pipeline,
1981 const GrTextureProxy* const primProcTextures[],
1982 int numPrimitiveProcessorTextureSets) {
Brian Salomondfec99f2018-08-02 10:40:05 -04001983 auto genLevelsIfNeeded = [this](GrTexture* tex, const GrSamplerState& sampler) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001984 SkASSERT(tex);
Brian Salomondfec99f2018-08-02 10:40:05 -04001985 if (sampler.filter() == GrSamplerState::Filter::kMipMap &&
1986 tex->texturePriv().mipMapped() == GrMipMapped::kYes &&
1987 tex->texturePriv().mipMapsAreDirty()) {
1988 SkASSERT(this->caps()->mipMapSupport());
1989 this->regenerateMipMapLevels(static_cast<GrGLTexture*>(tex));
Brian Salomonf7232642018-09-19 08:58:08 -04001990 SkASSERT(!tex->asRenderTarget() || !tex->asRenderTarget()->needsResolve());
1991 } else if (auto* rt = tex->asRenderTarget()) {
1992 if (rt->needsResolve()) {
1993 this->resolveRenderTarget(rt);
1994 }
Brian Salomondfec99f2018-08-02 10:40:05 -04001995 }
1996 };
1997
Brian Salomonf7232642018-09-19 08:58:08 -04001998 for (int set = 0, tex = 0; set < numPrimitiveProcessorTextureSets; ++set) {
1999 for (int sampler = 0; sampler < primProc.numTextureSamplers(); ++sampler, ++tex) {
2000 GrTexture* texture = primProcTextures[tex]->peekTexture();
2001 genLevelsIfNeeded(texture, primProc.textureSampler(sampler).samplerState());
2002 }
Brian Salomondfec99f2018-08-02 10:40:05 -04002003 }
2004
2005 GrFragmentProcessor::Iter iter(pipeline);
2006 while (const GrFragmentProcessor* fp = iter.next()) {
2007 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
2008 const auto& textureSampler = fp->textureSampler(i);
2009 genLevelsIfNeeded(textureSampler.peekTexture(), textureSampler.samplerState());
2010 }
2011 }
2012}
2013
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002014bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
2015 GrSurfaceOrigin origin,
2016 const GrPrimitiveProcessor& primProc,
Brian Salomon49348902018-06-26 09:12:38 -04002017 const GrPipeline& pipeline,
2018 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -04002019 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
2020 int dynamicStateArraysLength,
bsalomon2eda5b32016-09-21 10:53:24 -07002021 bool willDrawPoints) {
Brian Salomonf7232642018-09-19 08:58:08 -04002022 const GrTextureProxy* const* primProcProxiesForMipRegen = nullptr;
2023 const GrTextureProxy* const* primProcProxiesToBind = nullptr;
2024 int numPrimProcTextureSets = 1; // number of texture per prim proc sampler.
2025 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
2026 primProcProxiesForMipRegen = dynamicStateArrays->fPrimitiveProcessorTextures;
2027 numPrimProcTextureSets = dynamicStateArraysLength;
2028 } else if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
2029 primProcProxiesForMipRegen = fixedDynamicState->fPrimitiveProcessorTextures;
2030 primProcProxiesToBind = fixedDynamicState->fPrimitiveProcessorTextures;
Brian Salomon7eae3e02018-08-07 14:02:38 +00002031 }
Greg Daniel9a51a862018-11-30 10:18:14 -05002032
2033 SkASSERT(SkToBool(primProcProxiesForMipRegen) == SkToBool(primProc.numTextureSamplers()));
2034
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002035 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, origin, primProc,
2036 primProcProxiesForMipRegen,
Greg Daniel9a51a862018-11-30 10:18:14 -05002037 pipeline, willDrawPoints));
2038 if (!program) {
2039 GrCapsDebugf(this->caps(), "Failed to create program!\n");
2040 return false;
2041 }
Brian Salomonf7232642018-09-19 08:58:08 -04002042 this->resolveAndGenerateMipMapsForProcessorTextures(
2043 primProc, pipeline, primProcProxiesForMipRegen, numPrimProcTextureSets);
brianosman33f6b3f2016-06-02 05:49:21 -07002044
egdaniel080e6732014-12-22 07:35:52 -08002045 GrXferProcessor::BlendInfo blendInfo;
egdaniel0e1853c2016-03-17 11:35:45 -07002046 pipeline.getXferProcessor().getBlendInfo(&blendInfo);
egdaniel080e6732014-12-22 07:35:52 -08002047
egdaniel080e6732014-12-22 07:35:52 -08002048 this->flushColorWrite(blendInfo.fWriteColor);
bsalomonbc3d0de2014-12-15 13:45:03 -08002049
Brian Salomon802cb312018-06-08 18:05:20 -04002050 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08002051
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002052 // Swizzle the blend to match what the shader will output.
2053 const GrSwizzle& swizzle = this->caps()->shaderCaps()->configOutputSwizzle(
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002054 renderTarget->config());
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002055 this->flushBlend(blendInfo, swizzle);
bsalomon1f78c0a2014-12-17 09:43:13 -08002056
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002057 fHWProgram->updateUniformsAndTextureBindings(renderTarget, origin,
2058 primProc, pipeline, primProcProxiesToBind);
bsalomon1f78c0a2014-12-17 09:43:13 -08002059
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002060 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
csmartdaltonc633abb2016-11-01 08:55:55 -07002061 GrStencilSettings stencil;
2062 if (pipeline.isStencilEnabled()) {
2063 // TODO: attach stencil and create settings during render target flush.
2064 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
2065 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
2066 glRT->renderTargetPriv().numStencilBits());
2067 }
Chris Dalton71713f62019-04-18 12:41:03 -06002068 this->flushStencil(stencil, origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002069 if (pipeline.isScissorEnabled()) {
2070 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
2071 GrScissorState state(fixedDynamicState ? fixedDynamicState->fScissorRect : kBogusScissor);
Greg Danielacd66b42019-05-22 16:29:12 -04002072 this->flushScissor(state, glRT->width(), glRT->height(), origin);
Brian Salomond818ebf2018-07-02 14:08:49 +00002073 } else {
2074 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04002075 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002076 this->flushWindowRectangles(pipeline.getWindowRectsState(), glRT, origin);
Chris Dalton4c56b032019-03-04 12:28:42 -07002077 this->flushHWAAState(glRT, pipeline.isHWAntialiasState());
bsalomonbc3d0de2014-12-15 13:45:03 -08002078
2079 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07002080 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002081 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08002082
2083 return true;
2084}
2085
Brian Salomon802cb312018-06-08 18:05:20 -04002086void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
2087 if (!program) {
2088 fHWProgram.reset();
2089 fHWProgramID = 0;
2090 return;
2091 }
2092 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
2093 if (program == fHWProgram) {
2094 return;
2095 }
2096 auto id = program->programID();
2097 SkASSERT(id);
2098 GL_CALL(UseProgram(id));
2099 fHWProgram = std::move(program);
2100 fHWProgramID = id;
2101}
2102
2103void GrGLGpu::flushProgram(GrGLuint id) {
2104 SkASSERT(id);
2105 if (fHWProgramID == id) {
2106 SkASSERT(!fHWProgram);
2107 return;
2108 }
2109 fHWProgram.reset();
2110 GL_CALL(UseProgram(id));
2111 fHWProgramID = id;
2112}
2113
2114void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04002115 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002116 int baseVertex,
2117 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002118 int baseInstance,
2119 GrPrimitiveRestart enablePrimitiveRestart) {
2120 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07002121
cdaltone2e71c22016-04-07 18:13:29 -07002122 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04002123 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05002124 SkASSERT(indexBuffer->isCpuBuffer() ||
2125 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04002126 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07002127 } else {
2128 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08002129 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002130
Brian Salomon92be2f72018-06-19 14:33:47 -04002131 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
2132 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04002133
Brian Salomon802cb312018-06-08 18:05:20 -04002134 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002135 SkASSERT(vertexBuffer);
2136 SkASSERT(vertexBuffer->isCpuBuffer() ||
2137 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
2138 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002139 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
2140 const auto& attrib = fHWProgram->vertexAttribute(i);
2141 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002142 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
2143 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002144 }
Chris Dalton1d616352017-05-31 12:51:23 -06002145 }
Brian Salomon802cb312018-06-08 18:05:20 -04002146 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05002147 SkASSERT(instanceBuffer);
2148 SkASSERT(instanceBuffer->isCpuBuffer() ||
2149 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
2150 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04002151 int attribIdx = fHWProgram->numVertexAttributes();
2152 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
2153 const auto& attrib = fHWProgram->instanceAttribute(i);
2154 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04002155 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
2156 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
2157 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04002158 }
bsalomonbc3d0de2014-12-15 13:45:03 -08002159 }
2160}
2161
Brian Salomonae64c192019-02-05 09:41:37 -05002162GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07002163 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07002164
cdaltone2e71c22016-04-07 18:13:29 -07002165 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05002166 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07002167 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07002168 }
cdaltone2e71c22016-04-07 18:13:29 -07002169
Brian Salomonae64c192019-02-05 09:41:37 -05002170 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05002171 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05002172 if (!bufferState->fBufferZeroKnownBound) {
2173 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
2174 bufferState->fBufferZeroKnownBound = true;
2175 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07002176 }
Brian Salomondbf70722019-02-07 11:31:24 -05002177 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
2178 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05002179 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
2180 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
2181 bufferState->fBufferZeroKnownBound = false;
2182 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07002183 }
2184
Brian Salomonae64c192019-02-05 09:41:37 -05002185 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07002186}
Brian Salomond818ebf2018-07-02 14:08:49 +00002187void GrGLGpu::disableScissor() {
2188 if (kNo_TriState != fHWScissorSettings.fEnabled) {
2189 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2190 fHWScissorSettings.fEnabled = kNo_TriState;
2191 return;
2192 }
2193}
2194
Brian Osman9a9baae2018-11-05 15:06:26 -05002195void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002196 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002197 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07002198 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002199 SkASSERT(!this->caps()->performColorClearsAsDraws());
2200 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00002201
Brian Salomon43f8bf02017-10-18 08:33:29 -04002202 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00002203
Brian Salomon43f8bf02017-10-18 08:33:29 -04002204 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
2205
Brian Salomond818ebf2018-07-02 14:08:49 +00002206 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002207 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05002208 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002209 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05002210 }
Greg Danielacd66b42019-05-22 16:29:12 -04002211 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04002212 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05002213 this->flushColorWrite(true);
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002214
2215 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2216 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2217 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2218 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2219 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2220 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2221 }
2222 this->flushClearColor(r, g, b, a);
2223
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002224 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00002225}
2226
Robert Phillips95214472017-08-08 18:00:03 -04002227void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002228 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2229
Robert Phillips95214472017-08-08 18:00:03 -04002230 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002231 return;
2232 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002233
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002234 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
2235 // this should only be called internally when we know we have a
2236 // stencil buffer.
2237 SkASSERT(sb);
Robert Phillipscb2e2352017-08-30 16:44:40 -04002238
bsalomonb0bd4f62014-09-03 07:19:50 -07002239 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002240 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002241
Brian Salomond818ebf2018-07-02 14:08:49 +00002242 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002243 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002244
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002245 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002246 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002247 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002248 fHWStencilSettings.invalidate();
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00002249 if (!clearValue) {
2250 sb->cleared();
Robert Phillipscb2e2352017-08-30 16:44:40 -04002251 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002252}
2253
csmartdalton29df7602016-08-31 11:55:52 -07002254void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2255 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002256 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002257 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002258 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002259 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002260
egdaniel8dc7c3a2015-04-16 11:22:42 -07002261 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002262 // this should only be called internally when we know we have a
2263 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002264 SkASSERT(sb);
2265 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002266#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002267 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002268 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002269#else
2270 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002271 // ANGLE a partial stencil mask will cause clears to be
Robert Phillipsf2361d22016-10-25 14:20:06 -04002272 // turned into draws. Our contract on GrOpList says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002273 // changing the clip between stencil passes may or may not
2274 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002275 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002276#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002277 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002278 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002279 value = (1 << (stencilBitCount - 1));
2280 } else {
2281 value = 0;
2282 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002283 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002284 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002285
Greg Danielacd66b42019-05-22 16:29:12 -04002286 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002287 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002288
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002289 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002290 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002291 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002292 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002293}
2294
bsalomon1aa20292016-01-22 08:16:09 -08002295bool GrGLGpu::readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig) {
Brian Salomon625cd9e2016-12-15 09:35:19 -05002296#ifdef SK_BUILD_FOR_MAC
2297 // Chromium may ask us to read back from locked IOSurfaces. Calling the command buffer's
2298 // glGetIntegerv() with GL_IMPLEMENTATION_COLOR_READ_FORMAT/_TYPE causes the command buffer
2299 // to make a call to check the framebuffer status which can hang the driver. So in Mac Chromium
2300 // we always use a temporary surface to test for read pixels support.
2301 // https://www.crbug.com/662802
2302 if (this->glContext().driver() == kChromium_GrGLDriver) {
2303 return this->readPixelsSupported(target->config(), readConfig);
2304 }
2305#endif
bsalomon1aa20292016-01-22 08:16:09 -08002306 auto bindRenderTarget = [this, target]() -> bool {
Brian Salomon1fabd512018-02-09 09:54:25 -05002307 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(target));
bsalomon1aa20292016-01-22 08:16:09 -08002308 return true;
2309 };
bsalomon2c3db322016-11-08 13:26:24 -08002310 auto unbindRenderTarget = []{};
bsalomon1aa20292016-01-22 08:16:09 -08002311 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2312 GR_GL_GetIntegerv(this->glInterface(), query, value);
2313 };
2314 GrPixelConfig rtConfig = target->config();
bsalomon2c3db322016-11-08 13:26:24 -08002315 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2316 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002317}
2318
2319bool GrGLGpu::readPixelsSupported(GrPixelConfig rtConfig, GrPixelConfig readConfig) {
bsalomon2c3db322016-11-08 13:26:24 -08002320 sk_sp<GrTexture> temp;
2321 auto bindRenderTarget = [this, rtConfig, &temp]() -> bool {
Robert Phillips16d8ec62017-07-27 16:16:25 -04002322 GrSurfaceDesc desc;
bsalomon1aa20292016-01-22 08:16:09 -08002323 desc.fConfig = rtConfig;
2324 desc.fWidth = desc.fHeight = 16;
Brian Salomonbdecacf2018-02-02 20:32:49 -05002325 if (this->glCaps().isConfigRenderable(rtConfig)) {
bsalomon2c3db322016-11-08 13:26:24 -08002326 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips67d52cf2017-06-05 13:38:13 -04002327 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002328 if (!temp) {
2329 return false;
2330 }
2331 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(temp->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002332 this->flushRenderTargetNoColorWrites(glrt);
bsalomon2c3db322016-11-08 13:26:24 -08002333 return true;
2334 } else if (this->glCaps().canConfigBeFBOColorAttachment(rtConfig)) {
Robert Phillips67d52cf2017-06-05 13:38:13 -04002335 temp = this->createTexture(desc, SkBudgeted::kNo);
bsalomon2c3db322016-11-08 13:26:24 -08002336 if (!temp) {
2337 return false;
2338 }
Greg Danielacd66b42019-05-22 16:29:12 -04002339 this->bindSurfaceFBOForPixelOps(temp.get(), GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002340 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon2c3db322016-11-08 13:26:24 -08002341 return true;
bsalomon1aa20292016-01-22 08:16:09 -08002342 }
bsalomon2c3db322016-11-08 13:26:24 -08002343 return false;
2344 };
2345 auto unbindRenderTarget = [this, &temp]() {
2346 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, temp.get());
bsalomon1aa20292016-01-22 08:16:09 -08002347 };
2348 auto getIntegerv = [this](GrGLenum query, GrGLint* value) {
2349 GR_GL_GetIntegerv(this->glInterface(), query, value);
2350 };
bsalomon2c3db322016-11-08 13:26:24 -08002351 return this->glCaps().readPixelsSupported(rtConfig, readConfig, getIntegerv, bindRenderTarget,
2352 unbindRenderTarget);
bsalomon1aa20292016-01-22 08:16:09 -08002353}
2354
2355bool GrGLGpu::readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig) {
2356 if (GrRenderTarget* rt = surfaceForConfig->asRenderTarget()) {
2357 return this->readPixelsSupported(rt, readConfig);
2358 } else {
2359 GrPixelConfig config = surfaceForConfig->config();
2360 return this->readPixelsSupported(config, readConfig);
2361 }
2362}
2363
Brian Salomone05ba5a2019-04-08 11:59:07 -04002364bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
2365 GrColorType dstColorType, void* offsetOrPtr,
Brian Salomon26de56e2019-04-10 12:14:26 -04002366 int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002367 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002368
bsalomone9573312016-01-25 14:33:25 -08002369 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon71d9d842016-11-03 13:42:00 -04002370 if (!renderTarget && !this->glCaps().canConfigBeFBOColorAttachment(surface->config())) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002371 return false;
2372 }
2373
Brian Salomonc320b152018-02-20 14:05:36 -05002374 // TODO: Avoid this conversion by making GrGLCaps work with color types.
2375 auto dstAsConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
2376
Brian Salomonc320b152018-02-20 14:05:36 -05002377 if (!this->readPixelsSupported(surface, dstAsConfig)) {
bsalomon16921ec2015-07-30 15:34:56 -07002378 return false;
2379 }
2380
bsalomon76148af2016-01-12 11:13:47 -08002381 GrGLenum externalFormat;
2382 GrGLenum externalType;
Brian Salomonc320b152018-02-20 14:05:36 -05002383 if (!this->glCaps().getReadPixelsFormat(surface->config(), dstAsConfig, &externalFormat,
bsalomon76148af2016-01-12 11:13:47 -08002384 &externalType)) {
2385 return false;
2386 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002387
Brian Salomon71d9d842016-11-03 13:42:00 -04002388 if (renderTarget) {
2389 // resolve the render target if necessary
2390 switch (renderTarget->getResolveType()) {
2391 case GrGLRenderTarget::kCantResolve_ResolveType:
2392 return false;
2393 case GrGLRenderTarget::kAutoResolves_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002394 this->flushRenderTargetNoColorWrites(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002395 break;
2396 case GrGLRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05002397 this->onResolveRenderTarget(renderTarget);
Brian Salomon71d9d842016-11-03 13:42:00 -04002398 // we don't track the state of the READ FBO ID.
Adrienne Walker4ee88512018-05-17 11:37:14 -07002399 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002400 break;
2401 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002402 SK_ABORT("Unknown resolve type");
Brian Salomon71d9d842016-11-03 13:42:00 -04002403 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002404 } else {
2405 // Use a temporary FBO.
Greg Danielacd66b42019-05-22 16:29:12 -04002406 this->bindSurfaceFBOForPixelOps(surface, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002407 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002408 }
2409
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002410 // the read rect is viewport-relative
2411 GrGLIRect readRect;
Greg Danielacd66b42019-05-22 16:29:12 -04002412 readRect.setRelativeTo(surface->height(), left, top, width, height, kTopLeft_GrSurfaceOrigin);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002413
Brian Salomona6948702018-06-01 15:33:20 -04002414 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002415 if (rowWidthInPixels != width) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002416 SkASSERT(this->glCaps().packRowLengthSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002417 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002418 }
Brian Salomonc320b152018-02-20 14:05:36 -05002419 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, config_alignment(dstAsConfig)));
bsalomonf46a1242015-12-15 12:37:38 -08002420
Brian Osman1348ed02018-09-12 09:08:39 -04002421 bool reattachStencil = false;
2422 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2423 renderTarget &&
2424 renderTarget->renderTargetPriv().getStencilAttachment() &&
2425 renderTarget->numColorSamples() > 1) {
2426 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2427 reattachStencil = true;
2428 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2429 GR_GL_RENDERBUFFER, 0));
2430 }
2431
Brian Salomone05ba5a2019-04-08 11:59:07 -04002432 GL_CALL(ReadPixels(readRect.fLeft, readRect.fBottom, readRect.fWidth, readRect.fHeight,
2433 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002434
2435 if (reattachStencil) {
2436 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2437 renderTarget->renderTargetPriv().getStencilAttachment());
2438 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2439 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2440 }
2441
Brian Salomon26de56e2019-04-10 12:14:26 -04002442 if (rowWidthInPixels != width) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002443 SkASSERT(this->glCaps().packRowLengthSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002444 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2445 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002446
Brian Salomone05ba5a2019-04-08 11:59:07 -04002447 if (!renderTarget) {
2448 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, surface);
2449 }
2450 return true;
2451}
2452
2453bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2454 GrColorType dstColorType, void* buffer, size_t rowBytes) {
2455 SkASSERT(surface);
2456
Brian Salomon26de56e2019-04-10 12:14:26 -04002457 int bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002458
Brian Salomon26de56e2019-04-10 12:14:26 -04002459 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2460 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002461 void* readDst = buffer;
2462
2463 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
2464 SkAutoSMalloc<32 * sizeof(GrColor)> scratch;
Brian Salomon26de56e2019-04-10 12:14:26 -04002465 if (!rowBytes || rowBytes == (size_t)(width * bytesPerPixel)) {
2466 rowPixelWidth = width;
2467 } else {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002468 if (this->glCaps().packRowLengthSupport() && !(rowBytes % bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002469 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002470 } else {
Brian Salomon26de56e2019-04-10 12:14:26 -04002471 scratch.reset(width * bytesPerPixel * height);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002472 readDst = scratch.get();
Brian Salomon26de56e2019-04-10 12:14:26 -04002473 rowPixelWidth = width;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002474 }
2475 }
2476 if (!this->readOrTransferPixelsFrom(surface, left, top, width, height, dstColorType, readDst,
Brian Salomon26de56e2019-04-10 12:14:26 -04002477 rowPixelWidth)) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002478 return false;
2479 }
2480
Brian Salomona6948702018-06-01 15:33:20 -04002481 if (readDst != buffer) {
bsalomon9d02b262016-02-01 12:49:30 -08002482 SkASSERT(readDst != buffer);
Brian Salomon26de56e2019-04-10 12:14:26 -04002483 SkASSERT(rowBytes != (size_t)(rowPixelWidth * bytesPerPixel));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002484 const char* src = reinterpret_cast<const char*>(readDst);
bsalomon@google.comc4364992011-11-07 15:54:49 +00002485 char* dst = reinterpret_cast<char*>(buffer);
Brian Salomon26de56e2019-04-10 12:14:26 -04002486 SkRectMemcpy(dst, rowBytes, src, rowPixelWidth * bytesPerPixel, width * bytesPerPixel,
2487 height);
reed@google.comac10a2d2010-12-22 21:39:39 +00002488 }
2489 return true;
2490}
2491
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002492GrGpuRTCommandBuffer* GrGLGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -04002493 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips6b47c7d2017-08-29 07:24:09 -04002494 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
Greg Daniel500d58b2017-08-24 15:59:33 -04002495 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002496 if (!fCachedRTCommandBuffer) {
2497 fCachedRTCommandBuffer.reset(new GrGLGpuRTCommandBuffer(this));
2498 }
2499
2500 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
2501 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -04002502}
2503
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002504GrGpuTextureCommandBuffer* GrGLGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
2505 if (!fCachedTexCommandBuffer) {
2506 fCachedTexCommandBuffer.reset(new GrGLGpuTextureCommandBuffer(this));
2507 }
2508
2509 fCachedTexCommandBuffer->set(texture, origin);
2510 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -07002511}
2512
Brian Salomon1fabd512018-02-09 09:54:25 -05002513void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002514 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002515 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002516 this->didWriteToSurface(target, origin, &bounds);
2517}
bsalomon6ba6fa12015-03-04 11:57:37 -08002518
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002519void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2520 this->flushRenderTargetNoColorWrites(target);
2521 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002522}
2523
Brian Osman9aa30c62018-07-02 15:21:46 -04002524void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002525 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002526 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002527 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002528 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002529#ifdef SK_DEBUG
2530 // don't do this check in Chromium -- this is causing
2531 // lots of repeated command buffer flushes when the compositor is
2532 // rendering with Ganesh, which is really slow; even too slow for
2533 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002534 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002535 GrGLenum status;
2536 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2537 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2538 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2539 }
bsalomon160f24c2015-03-17 15:55:42 -07002540 }
egdanield803f272015-03-18 13:01:52 -07002541#endif
2542 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002543 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002544 }
2545
brianosman35b784d2016-05-05 11:52:53 -07002546 if (this->glCaps().srgbWriteControl()) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002547 this->flushFramebufferSRGB(GrPixelConfigIsSRGB(target->config()));
bsalomon5cd020f2015-03-17 12:46:56 -07002548 }
bsalomon083617b2016-02-12 12:10:14 -08002549}
bsalomona9909122016-01-23 10:41:40 -08002550
brianosman33f6b3f2016-06-02 05:49:21 -07002551void GrGLGpu::flushFramebufferSRGB(bool enable) {
2552 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2553 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2554 fHWSRGBFramebuffer = kYes_TriState;
2555 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2556 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2557 fHWSRGBFramebuffer = kNo_TriState;
2558 }
2559}
2560
Greg Danielacd66b42019-05-22 16:29:12 -04002561void GrGLGpu::flushViewport(int width, int height) {
2562 GrGLIRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002563 if (fHWViewport != viewport) {
2564 viewport.pushToGLViewport(this->glInterface());
2565 fHWViewport = viewport;
2566 }
2567}
2568
bsalomon@google.comd302f142011-03-03 13:54:13 +00002569#define SWAP_PER_DRAW 0
2570
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002571#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002572 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002573 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002574 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002575 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002576 void SwapBuf() {
2577 DWORD procID = GetCurrentProcessId();
2578 HWND hwnd = GetTopWindow(GetDesktopWindow());
2579 while(hwnd) {
2580 DWORD wndProcID = 0;
2581 GetWindowThreadProcessId(hwnd, &wndProcID);
2582 if(wndProcID == procID) {
2583 SwapBuffers(GetDC(hwnd));
2584 }
2585 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2586 }
2587 }
2588 #endif
2589#endif
2590
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002591void GrGLGpu::draw(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
2592 const GrPrimitiveProcessor& primProc,
Brian Salomonff168d92018-06-23 15:17:27 -04002593 const GrPipeline& pipeline,
Brian Salomon49348902018-06-26 09:12:38 -04002594 const GrPipeline::FixedDynamicState* fixedDynamicState,
2595 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
bsalomon2eda5b32016-09-21 10:53:24 -07002596 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002597 int meshCount) {
2598 this->handleDirtyContext();
2599
bsalomon2eda5b32016-09-21 10:53:24 -07002600 bool hasPoints = false;
2601 for (int i = 0; i < meshCount; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002602 if (meshes[i].primitiveType() == GrPrimitiveType::kPoints) {
bsalomon2eda5b32016-09-21 10:53:24 -07002603 hasPoints = true;
2604 break;
2605 }
2606 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002607 if (!this->flushGLState(renderTarget, origin, primProc, pipeline, fixedDynamicState,
2608 dynamicStateArrays, meshCount, hasPoints)) {
bsalomond95263c2014-12-16 13:05:12 -08002609 return;
2610 }
ethannicholas22793252016-01-30 09:59:10 -08002611
Brian Salomonf7232642018-09-19 08:58:08 -04002612 bool dynamicScissor = false;
2613 bool dynamicPrimProcTextures = false;
2614 if (dynamicStateArrays) {
2615 dynamicScissor = pipeline.isScissorEnabled() && dynamicStateArrays->fScissorRects;
2616 dynamicPrimProcTextures = dynamicStateArrays->fPrimitiveProcessorTextures;
2617 }
2618 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002619 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(renderTarget->asTexture(),
2620 *this->caps())) {
2621 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002622 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002623
Brian Salomon49348902018-06-26 09:12:38 -04002624 if (dynamicScissor) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002625 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Brian Salomonf7232642018-09-19 08:58:08 -04002626 this->flushScissor(GrScissorState(dynamicStateArrays->fScissorRects[m]),
Greg Danielacd66b42019-05-22 16:29:12 -04002627 glRT->width(), glRT->height(), origin);
Chris Dalton46983b72017-06-06 12:27:16 -06002628 }
Brian Salomonf7232642018-09-19 08:58:08 -04002629 if (dynamicPrimProcTextures) {
2630 auto texProxyArray = dynamicStateArrays->fPrimitiveProcessorTextures +
2631 m * primProc.numTextureSamplers();
2632 fHWProgram->updatePrimitiveProcessorTextureBindings(primProc, texProxyArray);
2633 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002634 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002635 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002636 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2637 GL_CALL(Enable(GR_GL_CULL_FACE));
2638 GL_CALL(Disable(GR_GL_CULL_FACE));
2639 }
Brian Salomonf7232642018-09-19 08:58:08 -04002640 meshes[m].sendToGpu(this);
2641 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002642 }
ethannicholas22793252016-01-30 09:59:10 -08002643
bsalomon@google.comd302f142011-03-03 13:54:13 +00002644#if SWAP_PER_DRAW
2645 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002646 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002647 aglSwapBuffers(aglGetCurrentContext());
2648 int set_a_break_pt_here = 9;
2649 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002650 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002651 SwapBuf();
2652 int set_a_break_pt_here = 9;
2653 SwapBuf();
2654 #endif
2655#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002656}
2657
Chris Dalton3809bab2017-06-13 10:55:06 -06002658static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2659 switch (primitiveType) {
2660 case GrPrimitiveType::kTriangles:
2661 return GR_GL_TRIANGLES;
2662 case GrPrimitiveType::kTriangleStrip:
2663 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002664 case GrPrimitiveType::kPoints:
2665 return GR_GL_POINTS;
2666 case GrPrimitiveType::kLines:
2667 return GR_GL_LINES;
2668 case GrPrimitiveType::kLineStrip:
2669 return GR_GL_LINE_STRIP;
2670 case GrPrimitiveType::kLinesAdjacency:
2671 return GR_GL_LINES_ADJACENCY;
2672 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002673 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002674 return GR_GL_TRIANGLES;
2675}
2676
Brian Salomon802cb312018-06-08 18:05:20 -04002677void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2678 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002679 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002680 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002681 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002682 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2683 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002684 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002685 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2686 }
2687 fStats.incNumDraws();
2688}
2689
Brian Salomondbf70722019-02-07 11:31:24 -05002690static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2691 size_t baseOffset = baseIndex * sizeof(uint16_t);
2692 if (indexBuffer->isCpuBuffer()) {
2693 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2694 } else {
2695 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2696 }
2697}
2698
Brian Salomon802cb312018-06-08 18:05:20 -04002699void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002700 int indexCount, int baseIndex, uint16_t minIndexValue,
2701 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002702 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002703 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002704 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002705
Brian Salomon802cb312018-06-08 18:05:20 -04002706 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002707
2708 if (this->glCaps().drawRangeElementsSupport()) {
2709 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002710 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002711 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002712 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002713 }
2714 fStats.incNumDraws();
2715}
2716
Brian Salomon802cb312018-06-08 18:05:20 -04002717void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002718 int vertexCount, int baseVertex,
2719 const GrBuffer* instanceBuffer, int instanceCount,
2720 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002721 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002722 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002723 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002724 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2725 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002726 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2727 SkTMin(instanceCount - i, maxInstances)));
2728 fStats.incNumDraws();
2729 }
Chris Dalton1d616352017-05-31 12:51:23 -06002730}
2731
Brian Salomon802cb312018-06-08 18:05:20 -04002732void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002733 const GrBuffer* indexBuffer, int indexCount,
2734 int baseIndex, const GrBuffer* vertexBuffer,
2735 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002736 int instanceCount, int baseInstance,
2737 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002738 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002739 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002740 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2741 for (int i = 0; i < instanceCount; i += maxInstances) {
2742 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2743 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002744 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002745 SkTMin(instanceCount - i, maxInstances)));
2746 fStats.incNumDraws();
2747 }
Chris Dalton1d616352017-05-31 12:51:23 -06002748}
2749
Brian Salomon1fabd512018-02-09 09:54:25 -05002750void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target) {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002751 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002752 if (rt->needsResolve()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00002753 // Some extensions automatically resolves the texture when it is read.
2754 if (this->glCaps().usesMSAARenderBuffers()) {
egdanield803f272015-03-18 13:01:52 -07002755 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
Brian Osmancfe83d12018-01-19 11:13:45 -05002756 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
Adrienne Walker4ee88512018-05-17 11:37:14 -07002757 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2758 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
2759
egdanield803f272015-03-18 13:01:52 -07002760 // make sure we go through flushRenderTarget() since we've modified
2761 // the bound DRAW FBO ID.
Robert Phillips294870f2016-11-11 12:38:40 -05002762 fHWBoundRenderTargetUniqueID.makeInvalid();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00002763 const SkIRect dirtyRect = rt->getResolveRect();
Brian Salomon1fabd512018-02-09 09:54:25 -05002764 // The dirty rect tracked on the RT is always stored in the native coordinates of the
2765 // surface. Choose kTopLeft so no adjustments are made
2766 static constexpr auto kDirtyRectOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.com347c3822013-05-01 20:10:01 +00002767 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002768 // Apple's extension uses the scissor as the blit bounds.
Brian Salomond818ebf2018-07-02 14:08:49 +00002769 GrScissorState scissorState;
2770 scissorState.set(dirtyRect);
Greg Danielacd66b42019-05-22 16:29:12 -04002771 this->flushScissor(scissorState, rt->width(), rt->height(), kDirtyRectOrigin);
csmartdalton28341fa2016-08-17 10:00:21 -07002772 this->disableWindowRectangles();
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002773 GL_CALL(ResolveMultisampleFramebuffer());
2774 } else {
Brian Salomone5e7eb12016-10-14 16:18:33 -04002775 int l, b, r, t;
2776 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2777 this->glCaps().blitFramebufferSupportFlags()) {
2778 l = 0;
2779 b = 0;
2780 r = target->width();
2781 t = target->height();
2782 } else {
2783 GrGLIRect rect;
Greg Danielacd66b42019-05-22 16:29:12 -04002784 rect.setRelativeTo(rt->height(), dirtyRect, kDirtyRectOrigin);
Brian Salomone5e7eb12016-10-14 16:18:33 -04002785 l = rect.fLeft;
2786 b = rect.fBottom;
2787 r = rect.fLeft + rect.fWidth;
2788 t = rect.fBottom + rect.fHeight;
2789 }
derekf8c8f71a2014-09-16 06:24:57 -07002790
2791 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00002792 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002793 this->disableWindowRectangles();
Brian Salomone5e7eb12016-10-14 16:18:33 -04002794 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00002795 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +00002796 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002797 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +00002798 rt->flagAsResolved();
reed@google.comac10a2d2010-12-22 21:39:39 +00002799 }
2800}
2801
bsalomon@google.com411dad02012-06-05 20:24:20 +00002802namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002803
bsalomon@google.com411dad02012-06-05 20:24:20 +00002804
2805GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002806 static const GrGLenum gTable[kGrStencilOpCount] = {
2807 GR_GL_KEEP, // kKeep
2808 GR_GL_ZERO, // kZero
2809 GR_GL_REPLACE, // kReplace
2810 GR_GL_INVERT, // kInvert
2811 GR_GL_INCR_WRAP, // kIncWrap
2812 GR_GL_DECR_WRAP, // kDecWrap
2813 GR_GL_INCR, // kIncClamp
2814 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002815 };
cdalton93a379b2016-05-11 13:58:08 -07002816 GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
2817 GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
2818 GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
2819 GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
2820 GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
2821 GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
2822 GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
2823 GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
2824 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2825 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002826}
2827
2828void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002829 const GrStencilSettings::Face& face,
2830 GrGLenum glFace) {
2831 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2832 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2833 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002834
cdalton93a379b2016-05-11 13:58:08 -07002835 GrGLint ref = face.fRef;
2836 GrGLint mask = face.fTestMask;
2837 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002838
2839 if (GR_GL_FRONT_AND_BACK == glFace) {
2840 // we call the combined func just in case separate stencil is not
2841 // supported.
2842 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2843 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002844 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002845 } else {
2846 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2847 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002848 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002849 }
2850}
2851}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002852
Chris Dalton71713f62019-04-18 12:41:03 -06002853void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002854 if (stencilSettings.isDisabled()) {
2855 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002856 } else if (fHWStencilSettings != stencilSettings ||
2857 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002858 if (kYes_TriState != fHWStencilTestEnabled) {
2859 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002860
csmartdaltonc7d85332016-10-26 10:13:46 -07002861 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002862 }
csmartdaltonc7d85332016-10-26 10:13:46 -07002863 if (stencilSettings.isTwoSided()) {
Chris Dalton71713f62019-04-18 12:41:03 -06002864 set_gl_stencil(this->glInterface(), stencilSettings.front(origin), GR_GL_FRONT);
2865 set_gl_stencil(this->glInterface(), stencilSettings.back(origin), GR_GL_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002866 } else {
Chris Dalton71713f62019-04-18 12:41:03 -06002867 set_gl_stencil(
2868 this->glInterface(), stencilSettings.frontAndBack(), GR_GL_FRONT_AND_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002869 }
joshualitta58fe352014-10-27 08:39:00 -07002870 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002871 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002872 }
2873}
2874
csmartdaltonc7d85332016-10-26 10:13:46 -07002875void GrGLGpu::disableStencil() {
2876 if (kNo_TriState != fHWStencilTestEnabled) {
2877 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002878
csmartdaltonc7d85332016-10-26 10:13:46 -07002879 fHWStencilTestEnabled = kNo_TriState;
2880 fHWStencilSettings.invalidate();
2881 }
2882}
2883
Chris Dalton4c56b032019-03-04 12:28:42 -07002884void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002885 // rt is only optional if useHWAA is false.
2886 SkASSERT(rt || !useHWAA);
vbuzinovdded6962015-06-12 08:59:45 -07002887 SkASSERT(!useHWAA || rt->isStencilBufferMultisampled());
bsalomon@google.com202d1392013-03-19 18:58:08 +00002888
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002889 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002890 if (useHWAA) {
2891 if (kYes_TriState != fMSAAEnabled) {
2892 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2893 fMSAAEnabled = kYes_TriState;
2894 }
2895 } else {
2896 if (kNo_TriState != fMSAAEnabled) {
2897 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2898 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002899 }
2900 }
2901 }
2902}
2903
bsalomon7f9b2e42016-01-12 13:29:26 -08002904void GrGLGpu::flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
egdanielb414f252014-07-29 13:15:47 -07002905 // Any optimization to disable blending should have already been applied and
cdalton8917d622015-05-06 13:40:21 -07002906 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
bsalomonf7cc8772015-05-11 11:21:14 -07002907
cdalton8917d622015-05-06 13:40:21 -07002908 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002909 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2910 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002911 bool blendOff =
2912 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2913 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2914 !blendInfo.fWriteColor;
egdanielb414f252014-07-29 13:15:47 -07002915 if (blendOff) {
2916 if (kNo_TriState != fHWBlendState.fEnabled) {
2917 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002918
2919 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2920 // https://code.google.com/p/skia/issues/detail?id=3943
2921 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2922 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2923 SkASSERT(this->caps()->advancedBlendEquationSupport());
2924 // Set to any basic blending equation.
2925 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2926 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2927 fHWBlendState.fEquation = blend_equation;
2928 }
2929
egdanielb414f252014-07-29 13:15:47 -07002930 fHWBlendState.fEnabled = kNo_TriState;
2931 }
cdalton8917d622015-05-06 13:40:21 -07002932 return;
2933 }
2934
2935 if (kYes_TriState != fHWBlendState.fEnabled) {
2936 GL_CALL(Enable(GR_GL_BLEND));
Brian Salomonaf971de2017-06-08 16:11:33 -04002937
cdalton8917d622015-05-06 13:40:21 -07002938 fHWBlendState.fEnabled = kYes_TriState;
2939 }
2940
Mike Kleinab5fec92018-08-16 19:43:31 +00002941 if (fHWBlendState.fEquation != equation) {
cdalton8917d622015-05-06 13:40:21 -07002942 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2943 fHWBlendState.fEquation = equation;
2944 }
2945
2946 if (GrBlendEquationIsAdvanced(equation)) {
2947 SkASSERT(this->caps()->advancedBlendEquationSupport());
2948 // Advanced equations have no other blend state.
2949 return;
2950 }
2951
Mike Kleinab5fec92018-08-16 19:43:31 +00002952 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
cdalton8917d622015-05-06 13:40:21 -07002953 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2954 gXfermodeCoeff2Blend[dstCoeff]));
2955 fHWBlendState.fSrcCoeff = srcCoeff;
2956 fHWBlendState.fDstCoeff = dstCoeff;
2957 }
2958
bsalomon7f9b2e42016-01-12 13:29:26 -08002959 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
Brian Osman422f95b2018-11-05 16:49:04 -05002960 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
bsalomon7f9b2e42016-01-12 13:29:26 -08002961 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
Brian Osman422f95b2018-11-05 16:49:04 -05002962 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
bsalomon7f9b2e42016-01-12 13:29:26 -08002963 fHWBlendState.fConstColor = blendConst;
2964 fHWBlendState.fConstColorValid = true;
2965 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002966 }
2967}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002968
Brian Salomondc829942018-10-23 16:07:24 -04002969static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002970 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002971 switch (swizzle[i]) {
2972 case 'r': glValues[i] = GR_GL_RED; break;
2973 case 'g': glValues[i] = GR_GL_GREEN; break;
2974 case 'b': glValues[i] = GR_GL_BLUE; break;
2975 case 'a': glValues[i] = GR_GL_ALPHA; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002976 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002977 default: SK_ABORT("Unsupported component");
2978 }
Brian Salomon327955b2018-10-22 15:39:22 +00002979 }
2980}
2981
Brian Salomondc829942018-10-23 16:07:24 -04002982void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002983 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002984
reed856e9d92015-09-30 12:21:45 -07002985#ifdef SK_DEBUG
2986 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002987 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002988 const int w = texture->width();
2989 const int h = texture->height();
2990 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2991 }
2992 }
2993#endif
2994
bsalomon@google.comb8670992012-07-25 21:27:09 +00002995 // If we created a rt/tex and rendered to it without using a texture and now we're texturing
2996 // 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 +00002997 // out of the "last != next" check.
bsalomon37dd3312014-11-03 08:47:23 -08002998 GrGLRenderTarget* texRT = static_cast<GrGLRenderTarget*>(texture->asRenderTarget());
bsalomon49f085d2014-09-05 13:34:00 -07002999 if (texRT) {
Brian Salomon1fabd512018-02-09 09:54:25 -05003000 this->onResolveRenderTarget(texRT);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003001 }
3002
Robert Phillips294870f2016-11-11 12:38:40 -05003003 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07003004 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05003005 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00003006 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07003007 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05003008 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00003009 }
3010
Brian Salomondc829942018-10-23 16:07:24 -04003011 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04003012 if (!this->caps()->mipMapSupport() ||
3013 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04003014 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07003015 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00003016 }
bsalomonefd7d452014-10-23 14:17:46 -07003017
brianosman33f6b3f2016-06-02 05:49:21 -07003018#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04003019 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04003020 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07003021 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07003022 }
3023#endif
3024
Brian Salomone2826ab2019-06-04 15:58:31 -04003025 auto timestamp = texture->parameters()->resetTimestamp();
3026 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08003027
Brian Salomone2826ab2019-06-04 15:58:31 -04003028 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
3029 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04003030 if (fSamplerObjectCache) {
3031 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
3032 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04003033 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
3034 texture->parameters()->samplerOverriddenState();
3035 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04003036
Brian Salomone2826ab2019-06-04 15:58:31 -04003037 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
3038 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04003039
Brian Salomone2826ab2019-06-04 15:58:31 -04003040 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
3041 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04003042
3043 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003044 newSamplerState.fMinLOD = -1000.f;
3045 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04003046
Brian Salomone2826ab2019-06-04 15:58:31 -04003047 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04003048 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003049 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04003050 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003051 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04003052 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003053 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04003054 }
Mike Klein1bccae52018-10-19 11:38:44 +00003055 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04003056 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00003057 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003058 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003059 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003060 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04003061 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003062 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04003063 }
3064 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003065 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04003066 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003067 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04003068 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003069 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04003070 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04003071 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04003072 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05003073 if (this->glCaps().clampToBorderSupport()) {
3074 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04003075 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05003076 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05003077 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
3078 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05003079 }
3080 }
Brian Salomondc829942018-10-23 16:07:24 -04003081 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003082 GrGLTextureParameters::NonsamplerState newNonsamplerState;
3083 newNonsamplerState.fBaseMipMapLevel = 0;
3084 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04003085
Brian Salomone2826ab2019-06-04 15:58:31 -04003086 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
3087 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04003088 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
3089 const auto& swizzle = this->caps()->shaderCaps()->configTextureSwizzle(texture->config());
Brian Salomone2826ab2019-06-04 15:58:31 -04003090 newNonsamplerState.fSwizzleKey = swizzle.asKey();
3091 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04003092 GrGLenum glValues[4];
3093 get_gl_swizzle_values(swizzle, glValues);
3094 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04003095 if (GR_IS_GR_GL(this->glStandard())) {
3096 GR_STATIC_ASSERT(sizeof(glValues[0]) == sizeof(GrGLint));
3097 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
3098 reinterpret_cast<const GrGLint*>(glValues)));
3099 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04003100 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
3101 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
3102 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
3103 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
3104 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00003105 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00003106 }
3107 }
Brian Salomondc829942018-10-23 16:07:24 -04003108 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05003109 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
3110 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
3111 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04003112 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04003113 this->setTextureUnit(unitIdx);
3114 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04003115 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04003116 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003117 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04003118 this->setTextureUnit(unitIdx);
3119 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04003120 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04003121 }
Brian Salomon327955b2018-10-22 15:39:22 +00003122 }
Brian Salomone2826ab2019-06-04 15:58:31 -04003123 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
3124 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07003125}
3126
Brian Salomon1f05d452019-02-08 12:33:08 -05003127void GrGLGpu::onResetTextureBindings() {
3128 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
3129 GR_GL_TEXTURE_EXTERNAL};
3130 for (int i = 0; i < this->numTextureUnits(); ++i) {
3131 this->setTextureUnit(i);
3132 for (auto target : kTargets) {
3133 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
3134 GL_CALL(BindTexture(target, 0));
3135 }
3136 }
3137 fHWTextureUnitBindings[i].invalidateAllTargets(true);
3138 }
3139}
3140
egdaniel080e6732014-12-22 07:35:52 -08003141void GrGLGpu::flushColorWrite(bool writeColor) {
3142 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003143 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00003144 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
3145 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003146 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00003147 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003148 } else {
3149 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00003150 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00003151 fHWWriteToColor = kYes_TriState;
3152 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00003153 }
bsalomon3e791242014-12-17 13:43:13 -08003154}
bsalomon@google.comd302f142011-03-03 13:54:13 +00003155
Michael Ludwig6e17f1d2019-05-15 14:00:20 +00003156void GrGLGpu::flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a) {
Brian Salomon805cc7a2019-01-28 09:52:34 -05003157 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
3158 b != fHWClearColor[2] || a != fHWClearColor[3]) {
3159 GL_CALL(ClearColor(r, g, b, a));
3160 fHWClearColor[0] = r;
3161 fHWClearColor[1] = g;
3162 fHWClearColor[2] = b;
3163 fHWClearColor[3] = a;
3164 }
3165}
3166
bsalomon861e1032014-12-16 07:33:49 -08003167void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05003168 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003169 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00003170 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00003171 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003172 }
3173}
bsalomon@google.com316f99232011-01-13 21:28:12 +00003174
Brian Salomond978b902019-02-07 15:09:18 -05003175void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003176 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05003177 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00003178 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
3179 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
3180 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003181 }
Brian Salomond978b902019-02-07 15:09:18 -05003182 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
3183 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05003184 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05003185 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00003186}
3187
Brian Salomone5e7eb12016-10-14 16:18:33 -04003188// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04003189static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
3190 const GrSurface* src,
3191 const SkIRect& srcRect,
3192 const SkIPoint& dstPoint,
3193 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003194 int dstSampleCnt = 0;
3195 int srcSampleCnt = 0;
3196 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
3197 dstSampleCnt = rt->numColorSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003198 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003199 if (const GrRenderTarget* rt = src->asRenderTarget()) {
3200 srcSampleCnt = rt->numColorSamples();
3201 }
3202 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3203 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3204
Brian Salomone5e7eb12016-10-14 16:18:33 -04003205 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003206 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003207
Greg Daniel46cfbc62019-06-07 11:43:30 -04003208 GrTextureType dstTexType;
3209 GrTextureType* dstTexTypePtr = nullptr;
3210 GrTextureType srcTexType;
3211 GrTextureType* srcTexTypePtr = nullptr;
3212 if (dstTex) {
3213 dstTexType = dstTex->texturePriv().textureType();
3214 dstTexTypePtr = &dstTexType;
3215 }
3216 if (srcTex) {
3217 srcTexType = srcTex->texturePriv().textureType();
3218 srcTexTypePtr = &srcTexType;
3219 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003220
Greg Daniel46cfbc62019-06-07 11:43:30 -04003221 return caps.canCopyAsBlit(dst->config(), dstSampleCnt, dstTexTypePtr,
3222 src->config(), srcSampleCnt, srcTexTypePtr,
3223 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003224}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003225
Brian Osmana9c8a052018-01-19 10:31:56 -05003226static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3227 // A RT has a separate MSAA renderbuffer if:
3228 // 1) It's multisampled
3229 // 2) We're using an extension with separate MSAA renderbuffers
3230 // 3) It's not FBO 0, which is special and always auto-resolves
Brian Salomonbdecacf2018-02-02 20:32:49 -05003231 return rt->numColorSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003232}
3233
Greg Daniel46cfbc62019-06-07 11:43:30 -04003234static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003235 const GrGLCaps& caps) {
3236
bsalomon@google.comeb851172013-04-15 13:51:00 +00003237 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003238 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003239 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003240 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003241
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003242 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3243 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3244
Greg Daniel46cfbc62019-06-07 11:43:30 -04003245 GrTextureType dstTexType;
3246 GrTextureType* dstTexTypePtr = nullptr;
3247 GrTextureType srcTexType;
3248 GrTextureType* srcTexTypePtr = nullptr;
3249 if (dstTex) {
3250 dstTexType = dstTex->texturePriv().textureType();
3251 dstTexTypePtr = &dstTexType;
3252 }
3253 if (srcTex) {
3254 srcTexType = srcTex->texturePriv().textureType();
3255 srcTexTypePtr = &srcTexType;
3256 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003257
Greg Daniel46cfbc62019-06-07 11:43:30 -04003258 return caps.canCopyTexSubImage(dst->config(), dstHasMSAARenderBuffer, dstTexTypePtr,
3259 src->config(), srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003260}
3261
Greg Danielacd66b42019-05-22 16:29:12 -04003262// If a temporary FBO was created, its non-zero ID is returned.
3263void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003264 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003265 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
bsalomon083617b2016-02-12 12:10:14 -08003266 if (!rt) {
bsalomon49f085d2014-09-05 13:34:00 -07003267 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003268 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3269 GrGLuint texID = texture->textureID();
3270 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003271 GrGLuint* tempFBOID;
3272 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003273
egdanield803f272015-03-18 13:01:52 -07003274 if (0 == *tempFBOID) {
3275 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003276 }
3277
Adrienne Walker4ee88512018-05-17 11:37:14 -07003278 this->bindFramebuffer(fboTarget, *tempFBOID);
egdanield803f272015-03-18 13:01:52 -07003279 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
robertphillips754f4e92014-09-18 13:52:08 -07003280 GR_GL_COLOR_ATTACHMENT0,
bsalomon10528f12015-10-14 12:54:52 -07003281 target,
robertphillips754f4e92014-09-18 13:52:08 -07003282 texID,
3283 0));
Brian Salomon9bada542017-06-12 12:09:30 -04003284 texture->baseLevelWasBoundToFBO();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003285 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003286 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003287 }
egdaniel0f5f9672015-02-03 11:10:51 -08003288}
3289
Brian Salomon71d9d842016-11-03 13:42:00 -04003290void GrGLGpu::unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface) {
3291 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
bsalomon10528f12015-10-14 12:54:52 -07003292 if (!surface->asRenderTarget()) {
3293 SkASSERT(surface->asTexture());
3294 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3295 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3296 GR_GL_COLOR_ATTACHMENT0,
3297 textureTarget,
3298 0,
3299 0));
3300 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003301}
3302
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003303void GrGLGpu::onFBOChanged() {
3304 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3305 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3306 GL_CALL(Flush());
3307 }
3308}
3309
Adrienne Walker4ee88512018-05-17 11:37:14 -07003310void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3311 fStats.incRenderTargetBinds();
3312 GL_CALL(BindFramebuffer(target, fboid));
3313 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3314 fBoundDrawFramebuffer = fboid;
3315 }
3316
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003317 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3318 // The driver forgets the correct scissor when modifying the FBO binding.
3319 if (!fHWScissorSettings.fRect.isInvalid()) {
3320 fHWScissorSettings.fRect.pushToGLScissor(this->glInterface());
3321 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003322 }
3323
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003324 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003325}
3326
Adrienne Walker4ee88512018-05-17 11:37:14 -07003327void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3328 if (fboid == fBoundDrawFramebuffer &&
3329 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3330 // This workaround only applies to deleting currently bound framebuffers
3331 // on Adreno 420. Because this is a somewhat rare case, instead of
3332 // tracking all the attachments of every framebuffer instead just always
3333 // unbind all attachments.
3334 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3335 GR_GL_RENDERBUFFER, 0));
3336 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3337 GR_GL_RENDERBUFFER, 0));
3338 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3339 GR_GL_RENDERBUFFER, 0));
3340 }
3341
3342 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003343
3344 // Deleting the currently bound framebuffer rebinds to 0.
3345 if (fboid == fBoundDrawFramebuffer) {
3346 this->onFBOChanged();
3347 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003348}
3349
Greg Daniel46cfbc62019-06-07 11:43:30 -04003350bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
3351 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
3352 // None of our copy methods can handle a swizzle.
Brian Salomon1edc5b92016-11-29 13:43:46 -05003353 if (this->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
3354 this->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
bsalomon7f9b2e42016-01-12 13:29:26 -08003355 return false;
3356 }
Greg Daniel46cfbc62019-06-07 11:43:30 -04003357
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003358 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3359 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3360 bool preferCopy = SkToBool(dst->asRenderTarget());
3361 if (preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003362 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003363 return true;
3364 }
3365 }
cblume61214052016-01-26 09:10:48 -08003366
Greg Daniel46cfbc62019-06-07 11:43:30 -04003367 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3368 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003369 return true;
3370 }
3371
Greg Daniel46cfbc62019-06-07 11:43:30 -04003372 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3373 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003374 }
3375
3376 if (!preferCopy && this->glCaps().canCopyAsDraw(dst->config(), SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003377 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003378 return true;
3379 }
bsalomon083617b2016-02-12 12:10:14 -08003380 }
3381
bsalomon6df86402015-06-01 10:41:49 -07003382 return false;
3383}
3384
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003385bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
3386 TRACE_EVENT0("skia", TRACE_FUNC);
3387
3388 int progIdx = TextureToCopyProgramIdx(srcTex);
3389 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3390 GrSLType samplerType =
3391 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3392
3393 if (!fCopyProgramArrayBuffer) {
3394 static const GrGLfloat vdata[] = {
3395 0, 0,
3396 0, 1,
3397 1, 0,
3398 1, 1
3399 };
3400 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3401 kStatic_GrAccessPattern, vdata);
3402 }
3403 if (!fCopyProgramArrayBuffer) {
3404 return false;
3405 }
3406
3407 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3408 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3409 if (!fCopyPrograms[progIdx].fProgram) {
3410 return false;
3411 }
3412
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003413 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3414 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3415 GrShaderVar::kUniform_TypeModifier);
3416 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3417 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3418 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3419 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3420
Greg Daniel9e3169b2019-06-06 14:38:17 -04003421 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003422 if (shaderCaps->noperspectiveInterpolationSupport()) {
3423 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3424 vshaderTxt.appendf("#extension %s : require\n", extension);
3425 }
3426 vTexCoord.addModifier("noperspective");
3427 }
3428
3429 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3430 vshaderTxt.append(";");
3431 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3432 vshaderTxt.append(";");
3433 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3434 vshaderTxt.append(";");
3435 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3436 vshaderTxt.append(";");
3437
3438 vshaderTxt.append(
3439 "// Copy Program VS\n"
3440 "void main() {"
3441 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3442 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3443 " sk_Position.zw = half2(0, 1);"
3444 "}"
3445 );
3446
Greg Daniel9e3169b2019-06-06 14:38:17 -04003447 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003448 if (shaderCaps->noperspectiveInterpolationSupport()) {
3449 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3450 fshaderTxt.appendf("#extension %s : require\n", extension);
3451 }
3452 }
3453 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3454 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3455 fshaderTxt.append(";");
3456 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3457 fshaderTxt.append(";");
3458 fshaderTxt.appendf(
3459 "// Copy Program FS\n"
3460 "void main() {"
3461 " sk_FragColor = texture(u_texture, v_texCoord);"
3462 "}"
3463 );
3464
3465 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3466 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3467 SkSL::Program::Settings settings;
3468 settings.fCaps = shaderCaps;
3469 SkSL::String glsl;
3470 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3471 sksl, settings, &glsl, errorHandler);
3472 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3473 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3474 SkASSERT(program->fInputs.isEmpty());
3475
3476 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3477 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3478 errorHandler);
3479 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3480 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3481 errorHandler);
3482 SkASSERT(program->fInputs.isEmpty());
3483
3484 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3485
3486 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3487 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3488 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3489 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3490 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3491 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3492
3493 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3494
3495 GL_CALL(DeleteShader(vshader));
3496 GL_CALL(DeleteShader(fshader));
3497
3498 return true;
3499}
3500
brianosman33f6b3f2016-06-02 05:49:21 -07003501bool GrGLGpu::createMipmapProgram(int progIdx) {
3502 const bool oddWidth = SkToBool(progIdx & 0x2);
3503 const bool oddHeight = SkToBool(progIdx & 0x1);
3504 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3505
Brian Salomon1edc5b92016-11-29 13:43:46 -05003506 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003507
3508 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3509 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3510 if (!fMipmapPrograms[progIdx].fProgram) {
3511 return false;
3512 }
3513
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003514 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3515 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003516 GrShaderVar::kUniform_TypeModifier);
3517 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3518 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003519 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003520 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003521 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3522 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3523 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3524 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003525 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003526 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003527
Ethan Nicholasfc994162019-06-06 10:04:27 -04003528 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003529 if (shaderCaps->noperspectiveInterpolationSupport()) {
3530 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003531 vshaderTxt.appendf("#extension %s : require\n", extension);
3532 }
3533 vTexCoords[0].addModifier("noperspective");
3534 vTexCoords[1].addModifier("noperspective");
3535 vTexCoords[2].addModifier("noperspective");
3536 vTexCoords[3].addModifier("noperspective");
3537 }
3538
Brian Salomon1edc5b92016-11-29 13:43:46 -05003539 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003540 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003541 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003542 vshaderTxt.append(";");
3543 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003544 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003545 vshaderTxt.append(";");
3546 }
3547
3548 vshaderTxt.append(
3549 "// Mipmap Program VS\n"
3550 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003551 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3552 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003553 );
3554
3555 // Insert texture coordinate computation:
3556 if (oddWidth && oddHeight) {
3557 vshaderTxt.append(
3558 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003559 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3560 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003561 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3562 );
3563 } else if (oddWidth) {
3564 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003565 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3566 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003567 );
3568 } else if (oddHeight) {
3569 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003570 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3571 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003572 );
3573 } else {
3574 vshaderTxt.append(
3575 " v_texCoord0 = a_vertex.xy;"
3576 );
3577 }
3578
3579 vshaderTxt.append("}");
3580
Ethan Nicholasfc994162019-06-06 10:04:27 -04003581 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003582 if (shaderCaps->noperspectiveInterpolationSupport()) {
3583 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003584 fshaderTxt.appendf("#extension %s : require\n", extension);
3585 }
3586 }
brianosman33f6b3f2016-06-02 05:49:21 -07003587 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003588 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003589 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003590 fshaderTxt.append(";");
3591 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003592 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003593 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003594 fshaderTxt.append(
3595 "// Mipmap Program FS\n"
3596 "void main() {"
3597 );
3598
3599 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003600 fshaderTxt.append(
3601 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3602 " texture(u_texture, v_texCoord1) + "
3603 " texture(u_texture, v_texCoord2) + "
3604 " texture(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003605 );
3606 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003607 fshaderTxt.append(
3608 " sk_FragColor = (texture(u_texture, v_texCoord0) + "
3609 " texture(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003610 );
3611 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003612 fshaderTxt.append(
3613 " sk_FragColor = texture(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003614 );
3615 }
3616
3617 fshaderTxt.append("}");
3618
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003619 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003620 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003621 SkSL::Program::Settings settings;
3622 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003623 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003624 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003625 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003626 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003627 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003628 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003629
Brian Osman6c431d52019-04-15 16:31:54 -04003630 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003631 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3632 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003633 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003634 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003635 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003636 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003637
3638 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3639
3640 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3641 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3642 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3643 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3644
3645 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3646
3647 GL_CALL(DeleteShader(vshader));
3648 GL_CALL(DeleteShader(fshader));
3649
3650 return true;
3651}
3652
Greg Daniel46cfbc62019-06-07 11:43:30 -04003653bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003654 const SkIPoint& dstPoint) {
3655 GrGLTexture* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3656 int progIdx = TextureToCopyProgramIdx(srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003657 if (!this->glCaps().canConfigBeFBOColorAttachment(dst->config())) {
3658 return false;
3659 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003660 if (!fCopyPrograms[progIdx].fProgram) {
3661 if (!this->createCopyProgram(srcTex)) {
3662 SkDebugf("Failed to create copy program.\n");
3663 return false;
3664 }
3665 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003666 int w = srcRect.width();
3667 int h = srcRect.height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003668 this->bindTexture(0, GrSamplerState::ClampNearest(), srcTex);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003669 this->bindSurfaceFBOForPixelOps(dst, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
3670 this->flushViewport(dst->width(), dst->height());
3671 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003672 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003673 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003674 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003675 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3676 attribs->enableVertexArrays(this, 1);
3677 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3678 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003679 // dst rect edges in NDC (-1 to 1)
3680 int dw = dst->width();
3681 int dh = dst->height();
3682 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3683 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3684 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3685 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003686 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3687 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3688 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3689 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3690 int sw = src->width();
3691 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003692 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3693 // src rect edges in normalized texture space (0 to 1)
3694 sx0 /= sw;
3695 sx1 /= sw;
3696 sy0 /= sh;
3697 sy1 /= sh;
3698 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003699 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3700 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3701 sx1 - sx0, sy1 - sy0, sx0, sy0));
3702 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003703 GrXferProcessor::BlendInfo blendInfo;
3704 blendInfo.reset();
3705 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3706 this->flushColorWrite(true);
3707 this->flushHWAAState(nullptr, false);
3708 this->disableScissor();
3709 this->disableWindowRectangles();
3710 this->disableStencil();
3711 if (this->glCaps().srgbWriteControl()) {
3712 this->flushFramebufferSRGB(true);
3713 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003714 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3715 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, dst);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003716 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3717 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003718 return true;
3719}
3720
Greg Daniel46cfbc62019-06-07 11:43:30 -04003721void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003722 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003723 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Greg Danielacd66b42019-05-22 16:29:12 -04003724 this->bindSurfaceFBOForPixelOps(src, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003725 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003726 SkASSERT(dstTex);
3727 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003728 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003729
Brian Salomond978b902019-02-07 15:09:18 -05003730 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003731 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003732 dstPoint.fX, dstPoint.fY,
3733 srcRect.fLeft, srcRect.fTop,
3734 srcRect.width(), srcRect.height()));
Brian Salomon71d9d842016-11-03 13:42:00 -04003735 this->unbindTextureFBOForPixelOps(GR_GL_FRAMEBUFFER, src);
bsalomon083617b2016-02-12 12:10:14 -08003736 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3737 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003738 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3739 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003740}
3741
Greg Daniel46cfbc62019-06-07 11:43:30 -04003742bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003743 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003744 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003745 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3746 srcRect.width(), srcRect.height());
3747 if (dst == src) {
3748 if (SkIRect::IntersectsNoEmptyCheck(dstRect, srcRect)) {
3749 return false;
mtklein404b3b22015-05-18 09:29:10 -07003750 }
bsalomon5df6fee2015-05-18 06:26:15 -07003751 }
bsalomon6df86402015-06-01 10:41:49 -07003752
Greg Danielacd66b42019-05-22 16:29:12 -04003753 this->bindSurfaceFBOForPixelOps(dst, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3754 this->bindSurfaceFBOForPixelOps(src, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003755 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003756 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003757
3758 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003759 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003760 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003761
Greg Daniel46cfbc62019-06-07 11:43:30 -04003762 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3763 srcRect.fTop,
3764 srcRect.fRight,
3765 srcRect.fBottom,
3766 dstRect.fLeft,
3767 dstRect.fTop,
3768 dstRect.fRight,
3769 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003770 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomon71d9d842016-11-03 13:42:00 -04003771 this->unbindTextureFBOForPixelOps(GR_GL_DRAW_FRAMEBUFFER, dst);
3772 this->unbindTextureFBOForPixelOps(GR_GL_READ_FRAMEBUFFER, src);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003773
3774 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3775 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003776 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003777}
3778
Brian Salomon930f9392018-06-20 16:25:26 -04003779bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3780 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003781 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003782 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003783 return false;
3784 }
3785
Brian Salomon930f9392018-06-20 16:25:26 -04003786 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3787 // Uses draw calls to do a series of downsample operations to successive mips.
3788
3789 // The manual approach requires the ability to limit which level we're sampling and that the
3790 // destination can be bound to a FBO:
3791 if (!this->glCaps().doManualMipmapping() ||
3792 !this->glCaps().canConfigBeFBOColorAttachment(texture->config())) {
3793 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003794 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003795 GL_CALL(GenerateMipmap(glTex->target()));
3796 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003797 }
3798
brianosman33f6b3f2016-06-02 05:49:21 -07003799 int width = texture->width();
3800 int height = texture->height();
3801 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003802 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003803
3804 // Create (if necessary), then bind temporary FBO:
3805 if (0 == fTempDstFBOID) {
3806 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3807 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003808 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003809 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003810
3811 // Bind the texture, to get things configured for filtering.
3812 // We'll be changing our base level further below:
3813 this->setTextureUnit(0);
Brian Salomon930f9392018-06-20 16:25:26 -04003814 this->bindTexture(0, GrSamplerState::ClampBilerp(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003815
3816 // Vertex data:
3817 if (!fMipmapProgramArrayBuffer) {
3818 static const GrGLfloat vdata[] = {
3819 0, 0,
3820 0, 1,
3821 1, 0,
3822 1, 1
3823 };
Brian Salomonae64c192019-02-05 09:41:37 -05003824 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003825 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003826 }
3827 if (!fMipmapProgramArrayBuffer) {
3828 return false;
3829 }
3830
3831 fHWVertexArrayState.setVertexArrayID(this, 0);
3832
3833 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003834 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003835 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003836 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003837
3838 // Set "simple" state once:
3839 GrXferProcessor::BlendInfo blendInfo;
3840 blendInfo.reset();
3841 this->flushBlend(blendInfo, GrSwizzle::RGBA());
3842 this->flushColorWrite(true);
Chris Dalton4c56b032019-03-04 12:28:42 -07003843 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003844 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003845 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003846 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003847
3848 // Do all the blits:
3849 width = texture->width();
3850 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003851
brianosman33f6b3f2016-06-02 05:49:21 -07003852 for (GrGLint level = 1; level < levelCount; ++level) {
3853 // Get and bind the program for this particular downsample (filter shape can vary):
3854 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3855 if (!fMipmapPrograms[progIdx].fProgram) {
3856 if (!this->createMipmapProgram(progIdx)) {
3857 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003858 // Invalidate all params to cover base level change in a previous iteration.
3859 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003860 return false;
3861 }
3862 }
Brian Salomon802cb312018-06-08 18:05:20 -04003863 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003864
3865 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3866 const float invWidth = 1.0f / width;
3867 const float invHeight = 1.0f / height;
3868 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3869 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3870 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3871
3872 // Only sample from previous mip
3873 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3874
Brian Salomon930f9392018-06-20 16:25:26 -04003875 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3876 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003877
3878 width = SkTMax(1, width / 2);
3879 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003880 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003881
3882 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3883 }
3884
3885 // Unbind:
3886 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3887 GR_GL_TEXTURE_2D, 0, 0));
3888
Brian Salomon930f9392018-06-20 16:25:26 -04003889 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003890 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3891 // We drew the 2nd to last level into the last level.
3892 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3893 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003894
brianosman33f6b3f2016-06-02 05:49:21 -07003895 return true;
3896}
3897
Chris Daltond7291ba2019-03-07 14:17:03 -07003898void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003899 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3900 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003901
3902 int effectiveSampleCnt;
3903 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
3904 SkASSERT(effectiveSampleCnt >= renderTarget->numStencilSamples());
3905
3906 sampleLocations->reset(effectiveSampleCnt);
3907 for (int i = 0; i < effectiveSampleCnt; ++i) {
3908 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3909 }
3910}
3911
cdalton231c5fd2015-05-13 12:35:36 -07003912void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003913 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003914 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003915 case kTexture_GrXferBarrierType: {
3916 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003917 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003918 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3919 // The render target uses separate storage so no need for glTextureBarrier.
3920 // FIXME: The render target will resolve automatically when its texture is bound,
3921 // but we could resolve only the bounds that will be read if we do it here instead.
3922 return;
3923 }
cdalton9954bc32015-04-29 14:17:00 -07003924 SkASSERT(this->caps()->textureBarrierSupport());
3925 GL_CALL(TextureBarrier());
3926 return;
cdalton231c5fd2015-05-13 12:35:36 -07003927 }
cdalton8917d622015-05-06 13:40:21 -07003928 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003929 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003930 this->caps()->blendEquationSupport());
3931 GL_CALL(BlendBarrier());
3932 return;
bsalomoncb02b382015-08-12 11:14:50 -07003933 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003934 }
3935}
3936
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003937static bool gl_format_to_pixel_config(GrGLenum format, GrPixelConfig* config) {
3938 GrPixelConfig dontCare;
3939 if (!config) {
3940 config = &dontCare;
3941 }
3942
3943 switch (format) {
3944 case GR_GL_RGBA8:
3945 *config = kRGBA_8888_GrPixelConfig;
3946 return true;
3947 case GR_GL_RGB8:
3948 *config = kRGB_888_GrPixelConfig;
3949 return true;
3950 case GR_GL_RG8:
3951 *config = kRG_88_GrPixelConfig;
3952 return true;
3953 case GR_GL_BGRA8:
3954 *config = kBGRA_8888_GrPixelConfig;
3955 return true;
3956 case GR_GL_LUMINANCE8:
3957 *config = kGray_8_GrPixelConfig;
3958 return true;
3959 case GR_GL_SRGB8_ALPHA8:
3960 *config = kSRGBA_8888_GrPixelConfig; // aliasing kSBGRA_8888 here
3961 return true;
3962 case GR_GL_RGB10_A2:
3963 *config = kRGBA_1010102_GrPixelConfig;
3964 return true;
3965 case GR_GL_RGB565:
3966 *config = kRGB_565_GrPixelConfig;
3967 return true;
3968 case GR_GL_RGBA4:
3969 *config = kRGBA_4444_GrPixelConfig;
3970 return true;
3971 case GR_GL_ALPHA8: // fall through
3972 case GR_GL_R8:
3973 *config = kAlpha_8_GrPixelConfig;
3974 return true;
3975 case GR_GL_RGBA32F:
3976 *config = kRGBA_float_GrPixelConfig;
3977 return true;
3978 case GR_GL_RG32F:
3979 *config = kRG_float_GrPixelConfig;
3980 return true;
3981 case GR_GL_RGBA16F:
3982 *config = kRGBA_half_GrPixelConfig;
3983 return true;
Robert Phillips8043f322019-05-31 08:11:36 -04003984 case GR_GL_COMPRESSED_RGB8_ETC2: // fall through
3985 case GR_GL_COMPRESSED_ETC1_RGB8:
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003986 *config = kRGB_ETC1_GrPixelConfig;
3987 return true;
3988 case GR_GL_R16F:
3989 *config = kAlpha_half_GrPixelConfig;
3990 return true;
Robert Phillipsfe18de52019-06-06 17:21:50 -04003991
3992 // Experimental (for P016 and P010)
3993 case GR_GL_R16:
3994 *config = kR_16_GrPixelConfig;
3995 return true;
3996 case GR_GL_RG16:
3997 *config = kRG_1616_GrPixelConfig;
3998 return true;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003999 }
4000
4001 SK_ABORT("Unexpected config");
4002 return false;
4003}
4004
Robert Phillipsf0313ee2019-05-21 13:51:11 -04004005GrBackendTexture GrGLGpu::createBackendTexture(int w, int h,
4006 const GrBackendFormat& format,
4007 GrMipMapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -04004008 GrRenderable renderable,
Robert Phillips28a5a432019-06-07 12:46:21 -04004009 const void* srcPixels, size_t rowBytes,
Robert Phillips459b2952019-05-23 09:38:27 -04004010 const SkColor4f& colorf) {
Brian Salomon8a375832018-03-14 10:21:40 -04004011 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04004012
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04004013 const GrGLenum* glFormat = format.getGLFormat();
4014 if (!glFormat) {
4015 return GrBackendTexture(); // invalid
4016 }
4017
4018 GrPixelConfig config;
4019
4020 if (!gl_format_to_pixel_config(*glFormat, &config)) {
4021 return GrBackendTexture(); // invalid
4022 }
4023
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004024 if (!this->caps()->isConfigTexturable(config)) {
4025 return GrBackendTexture(); // invalid
4026 }
4027
Robert Phillipsb04b6942019-05-21 17:24:31 -04004028 if (w == 0 || w > this->caps()->maxTextureSize() ||
4029 h == 0 || h > this->caps()->maxTextureSize()) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004030 return GrBackendTexture(); // invalid
4031 }
4032
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004033 // Currently we don't support uploading pixel data when mipped.
Robert Phillips28a5a432019-06-07 12:46:21 -04004034 if (srcPixels && GrMipMapped::kYes == mipMapped) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004035 return GrBackendTexture(); // invalid
4036 }
4037
Brian Salomon4687bdd2019-05-09 16:28:04 -04004038 if (mipMapped == GrMipMapped::kYes && !this->caps()->mipMapSupport()) {
Robert Phillipse3bd6732019-05-29 14:20:35 -04004039 return GrBackendTexture(); // invalid
Brian Salomon4687bdd2019-05-09 16:28:04 -04004040 }
Robert Phillips646f6372018-09-25 09:31:10 -04004041
Robert Phillipsb04b6942019-05-21 17:24:31 -04004042 // Figure out the number of mip levels.
4043 int mipLevelCount = 1;
4044 if (GrMipMapped::kYes == mipMapped) {
4045 mipLevelCount = SkMipMap::ComputeLevelCount(w, h) + 1;
4046 }
4047
Greg Danielaee57142019-06-06 15:24:51 -04004048 SkAutoTMalloc<GrMipLevel> texels(mipLevelCount);
4049
Robert Phillips28a5a432019-06-07 12:46:21 -04004050 SkAutoMalloc pixelStorage;
4051
4052 if (!srcPixels) {
4053 GrCompression compression = GrGLFormat2Compression(*glFormat);
4054
4055 SkTArray<size_t> individualMipOffsets(mipLevelCount);
4056 size_t bytesPerPixel = GrBytesPerPixel(config);
4057
4058 size_t totalSize = GrComputeTightCombinedBufferSize(compression, bytesPerPixel, w, h,
4059 &individualMipOffsets, mipLevelCount);
4060
4061 char* tmpPixels = (char *) pixelStorage.reset(totalSize);
4062
4063 GrFillInData(compression, config, w, h, individualMipOffsets, tmpPixels, colorf);
4064
4065 for (int i = 0; i < mipLevelCount; ++i) {
4066 size_t offset = individualMipOffsets[i];
4067
4068 int twoToTheMipLevel = 1 << i;
4069 int currentWidth = SkTMax(1, w / twoToTheMipLevel);
4070
4071 texels.get()[i] = { &(tmpPixels[offset]), currentWidth*bytesPerPixel };
4072 }
4073 } else {
4074 SkASSERT(1 == mipLevelCount);
4075
4076 if (GrGLFormatIsCompressed(*glFormat)) {
4077 SkASSERT(0 == rowBytes);
4078 }
4079
4080 texels.get()[0] = { srcPixels, rowBytes };
Robert Phillipsb04b6942019-05-21 17:24:31 -04004081 }
4082
Robert Phillipse3bd6732019-05-29 14:20:35 -04004083 GrSurfaceDesc desc;
4084 desc.fWidth = w;
4085 desc.fHeight = h;
4086 desc.fConfig = config;
4087
4088 GrGLTextureInfo info;
Brian Salomone2826ab2019-06-04 15:58:31 -04004089 GrGLTextureParameters::SamplerOverriddenState initialState;
Robert Phillipse3bd6732019-05-29 14:20:35 -04004090
Brian Salomone2826ab2019-06-04 15:58:31 -04004091 if (!this->createTextureImpl(desc, &info, renderable, &initialState, texels.get(),
4092 mipLevelCount, nullptr)) {
Robert Phillipse3bd6732019-05-29 14:20:35 -04004093 return GrBackendTexture(); // invalid
4094 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04004095
Robert Phillipse8fabb22018-02-04 14:33:21 -05004096 // unbind the texture from the texture unit to avoid asserts
4097 GL_CALL(BindTexture(info.fTarget, 0));
4098
Brian Salomone2826ab2019-06-04 15:58:31 -04004099 auto parameters = sk_make_sp<GrGLTextureParameters>();
4100 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
4101 fResetTimestampForTextureParameters);
4102 GrBackendTexture beTex = GrBackendTexture(w, h, mipMapped, info, std::move(parameters));
Robert Phillipsf0ced622019-05-16 09:06:25 -04004103#if GR_TEST_UTILS
Robert Phillipse3bd6732019-05-29 14:20:35 -04004104 // 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 -04004105 // testing we make sure we set a config here.
4106 beTex.setPixelConfig(config);
Robert Phillipsf0ced622019-05-16 09:06:25 -04004107#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004108 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004109}
4110
Robert Phillipsf0313ee2019-05-21 13:51:11 -04004111void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04004112 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
4113
4114 GrGLTextureInfo info;
4115 if (tex.getGLTextureInfo(&info)) {
4116 GL_CALL(DeleteTextures(1, &info.fID));
4117 }
4118}
4119
4120#if GR_TEST_UTILS
4121
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004122bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004123 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004124
Greg Daniel52e16d92018-04-10 09:34:07 -04004125 GrGLTextureInfo info;
4126 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004127 return false;
4128 }
4129
4130 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04004131 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05004132
4133 return (GR_GL_TRUE == result);
4134}
4135
Brian Salomonf865b052018-03-09 09:01:53 -05004136GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04004137 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04004138 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
4139 return GrBackendRenderTarget(); // invalid
4140 }
Brian Salomon8a375832018-03-14 10:21:40 -04004141 this->handleDirtyContext();
Brian Osman2d010b62018-08-09 10:55:09 -04004142 auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
Brian Salomon93348dd2018-08-29 12:56:23 -04004143 if (!this->glCaps().isConfigRenderable(config)) {
Brian Salomonf865b052018-03-09 09:01:53 -05004144 return {};
4145 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004146 bool useTexture = false;
4147 GrGLenum colorBufferFormat;
4148 GrGLenum externalFormat = 0, externalType = 0;
4149 if (config == kBGRA_8888_GrPixelConfig && this->glCaps().bgraIsInternalFormat()) {
4150 // BGRA render buffers are not supported.
4151 this->glCaps().getTexImageFormats(config, config, &colorBufferFormat, &externalFormat,
4152 &externalType);
4153 useTexture = true;
4154 } else {
4155 this->glCaps().getRenderbufferFormat(config, &colorBufferFormat);
4156 }
Brian Salomonf865b052018-03-09 09:01:53 -05004157 int sFormatIdx = this->getCompatibleStencilIndex(config);
4158 if (sFormatIdx < 0) {
4159 return {};
4160 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004161 GrGLuint colorID = 0;
4162 GrGLuint stencilID = 0;
4163 auto deleteIDs = [&] {
4164 if (colorID) {
4165 if (useTexture) {
4166 GL_CALL(DeleteTextures(1, &colorID));
4167 } else {
4168 GL_CALL(DeleteRenderbuffers(1, &colorID));
4169 }
Brian Salomonf865b052018-03-09 09:01:53 -05004170 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004171 if (stencilID) {
4172 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004173 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004174 };
4175
4176 if (useTexture) {
4177 GL_CALL(GenTextures(1, &colorID));
4178 } else {
4179 GL_CALL(GenRenderbuffers(1, &colorID));
4180 }
4181 GL_CALL(GenRenderbuffers(1, &stencilID));
4182 if (!stencilID || !colorID) {
4183 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004184 return {};
4185 }
Brian Salomon93348dd2018-08-29 12:56:23 -04004186
Brian Salomonf865b052018-03-09 09:01:53 -05004187 GrGLFramebufferInfo info;
4188 info.fFBOID = 0;
Brian Salomon93348dd2018-08-29 12:56:23 -04004189 this->glCaps().getSizedInternalFormat(config, &info.fFormat);
Brian Salomonf865b052018-03-09 09:01:53 -05004190 GL_CALL(GenFramebuffers(1, &info.fFBOID));
4191 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04004192 deleteIDs();
4193 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05004194 }
4195
4196 this->invalidateBoundRenderTarget();
4197
Adrienne Walker4ee88512018-05-17 11:37:14 -07004198 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004199 if (useTexture) {
Brian Salomond978b902019-02-07 15:09:18 -05004200 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, colorID);
Brian Salomon93348dd2018-08-29 12:56:23 -04004201 GL_CALL(TexImage2D(GR_GL_TEXTURE_2D, 0, colorBufferFormat, w, h, 0, externalFormat,
4202 externalType, nullptr));
4203 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
4204 colorID, 0));
4205 } else {
4206 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
4207 GL_ALLOC_CALL(this->glInterface(),
4208 RenderbufferStorage(GR_GL_RENDERBUFFER, colorBufferFormat, w, h));
4209 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
4210 GR_GL_RENDERBUFFER, colorID));
4211 }
4212 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004213 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
4214 GL_ALLOC_CALL(this->glInterface(),
4215 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004217 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004218 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4219 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004220 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004221 }
4222
Brian Salomon93348dd2018-08-29 12:56:23 -04004223 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4224 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4225 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4226 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004227 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004228 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004229
Adrienne Walker4ee88512018-05-17 11:37:14 -07004230 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004231 GrGLenum status;
4232 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4233 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004234 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004235 return {};
4236 }
4237 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Greg Daniel108bb232018-07-03 16:18:29 -04004238 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
4239 // Lots of tests don't go through Skia's public interface which will set the config so for
4240 // testing we make sure we set a config here.
4241 beRT.setPixelConfig(config);
Brian Salomon93348dd2018-08-29 12:56:23 -04004242#ifdef SK_DEBUG
4243 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
4244 if (skColorType != kUnknown_SkColorType) {
4245 SkASSERT(this->caps()->validateBackendRenderTarget(
Brian Salomonf391d0f2018-12-14 09:18:50 -05004246 beRT, GrColorTypeToSkColorType(colorType)) != kUnknown_GrPixelConfig);
Brian Salomon93348dd2018-08-29 12:56:23 -04004247 }
4248#endif
Greg Daniel108bb232018-07-03 16:18:29 -04004249 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004250}
4251
4252void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004253 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004254 GrGLFramebufferInfo info;
4255 if (backendRT.getGLFramebufferInfo(&info)) {
4256 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004257 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004258 }
4259 }
joshualitt8fd844f2015-12-02 13:36:47 -08004260}
4261
Greg Daniel26b50a42018-03-08 09:49:58 -05004262void GrGLGpu::testingOnly_flushGpuAndSync() {
4263 GL_CALL(Finish());
4264}
Brian Salomonf865b052018-03-09 09:01:53 -05004265#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004266
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004267///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004268
cdaltone2e71c22016-04-07 18:13:29 -07004269GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004270 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004271 GrGLAttribArrayState* attribState;
4272
cdaltone2e71c22016-04-07 18:13:29 -07004273 if (gpu->glCaps().isCoreProfile()) {
4274 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004275 GrGLuint arrayID;
4276 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4277 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004278 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004279 }
cdaltone2e71c22016-04-07 18:13:29 -07004280 if (ibuf) {
4281 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004282 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004283 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004284 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004285 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004286 if (ibuf) {
4287 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004288 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004289 } else {
4290 this->setVertexArrayID(gpu, 0);
4291 }
4292 int attrCount = gpu->glCaps().maxVertexAttributes();
4293 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4294 fDefaultVertexArrayAttribState.resize(attrCount);
4295 }
4296 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004297 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004298 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004299}
bsalomone179a912016-01-20 06:18:10 -08004300
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04004301void GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004302 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004303 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004304 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004305 // We call finish if the client told us to sync or if we have a finished proc but don't support
4306 // GLsync objects.
4307 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4308 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4309 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004310 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004311 // After a finish everything previously sent to GL is done.
4312 for (const auto& cb : fFinishCallbacks) {
4313 cb.fCallback(cb.fContext);
4314 this->deleteSync(cb.fSync);
4315 }
4316 fFinishCallbacks.clear();
4317 if (info.fFinishedProc) {
4318 info.fFinishedProc(info.fFinishedContext);
4319 }
4320 } else {
4321 if (info.fFinishedProc) {
4322 FinishCallback callback;
4323 callback.fCallback = info.fFinishedProc;
4324 callback.fContext = info.fFinishedContext;
4325 callback.fSync = (GrGLsync)this->insertFence();
4326 fFinishCallbacks.push_back(callback);
4327 GL_CALL(Flush());
4328 } else if (insertedSemaphore) {
4329 // Must call flush after semaphores in case they are waited on another GL context.
4330 GL_CALL(Flush());
4331 }
4332 // See if any previously inserted finish procs are good to go.
4333 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004334 }
Greg Daniel51316782017-08-02 15:10:09 +00004335}
4336
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004337void GrGLGpu::submit(GrGpuCommandBuffer* buffer) {
4338 if (buffer->asRTCommandBuffer()) {
4339 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
4340 fCachedRTCommandBuffer->reset();
4341 } else {
4342 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
4343 fCachedTexCommandBuffer->reset();
4344 }
4345}
4346
Greg Daniel6be35232017-03-01 17:01:09 -05004347GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004348 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004349 GrGLsync sync;
4350 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4351 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(GrGLsync));
4352 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004353}
4354
Brian Salomonb0d8b762019-05-06 16:58:22 -04004355bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4356 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004357 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004358 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4359 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4360}
4361
4362bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4363 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004364}
4365
4366void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004367 this->deleteSync((GrGLsync)fence);
4368}
4369
Greg Daniela5cb7812017-06-16 09:45:32 -04004370sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004371 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004372 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004373}
4374
Greg Daniel48661b82018-01-22 16:11:35 -05004375sk_sp<GrSemaphore> GrGLGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
4376 GrResourceProvider::SemaphoreWrapType wrapType,
4377 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004378 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004379 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4380}
4381
Greg Daniel858e12c2018-12-06 11:11:37 -05004382void GrGLGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004383 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4384
Greg Daniel48661b82018-01-22 16:11:35 -05004385 GrGLsync sync;
4386 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4387 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004388}
4389
Greg Daniel48661b82018-01-22 16:11:35 -05004390void GrGLGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05004391 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore.get());
4392
4393 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4394}
4395
Brian Salomonb0d8b762019-05-06 16:58:22 -04004396void GrGLGpu::checkFinishProcs() {
4397 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4398 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4399 /* timeout = */ 0, /* flush = */ false)) {
4400 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4401 this->deleteSync(fFinishCallbacks.front().fSync);
4402 fFinishCallbacks.pop_front();
4403 }
4404}
4405
Greg Daniel6be35232017-03-01 17:01:09 -05004406void GrGLGpu::deleteSync(GrGLsync sync) const {
4407 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004408}
Brian Osman13dddce2017-05-09 13:19:50 -04004409
Robert Phillips65a88fa2017-08-08 08:36:22 -04004410void GrGLGpu::insertEventMarker(const char* msg) {
4411 GL_CALL(InsertEventMarker(strlen(msg), msg));
4412}
4413
Brian Osman13dddce2017-05-09 13:19:50 -04004414sk_sp<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
4415 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniela5cb7812017-06-16 09:45:32 -04004416 sk_sp<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel858e12c2018-12-06 11:11:37 -05004417 this->insertSemaphore(semaphore);
4418 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4419 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004420
4421 return semaphore;
4422}
Robert Phillips646e4292017-06-13 12:44:56 -04004423
4424int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004425 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004426 case kTexture2DSampler_GrSLType:
4427 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004428 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004429 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004430 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004431 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004432 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004433 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004434 return 0;
4435 }
4436}
Brian Osman71a18892017-08-10 10:23:25 -04004437
Kevin Lubickf4def342018-10-04 12:52:50 -04004438#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004439#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004440void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4441 // We are called by the base class, which has already called beginObject(). We choose to nest
4442 // all of our caps information in a named sub-object.
4443 writer->beginObject("GL GPU");
4444
4445 const GrGLubyte* str;
4446 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4447 writer->appendString("GL_VERSION", (const char*)(str));
4448 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4449 writer->appendString("GL_RENDERER", (const char*)(str));
4450 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4451 writer->appendString("GL_VENDOR", (const char*)(str));
4452 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4453 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4454
4455 writer->appendName("extensions");
4456 glInterface()->fExtensions.dumpJSON(writer);
4457
4458 writer->endObject();
4459}
Kevin Lubickf4def342018-10-04 12:52:50 -04004460#endif