blob: 58c255a7e3db96a08902c19fee712faf4b6ccedb [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/SkHalf.h"
15#include "include/private/SkTemplates.h"
16#include "include/private/SkTo.h"
17#include "src/core/SkAutoMalloc.h"
18#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkMipMap.h"
20#include "src/core/SkTraceEvent.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040021#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040023#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrFixedClip.h"
25#include "src/gpu/GrGpuResourcePriv.h"
26#include "src/gpu/GrMesh.h"
27#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040028#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
30#include "src/gpu/GrShaderCaps.h"
31#include "src/gpu/GrSurfaceProxyPriv.h"
32#include "src/gpu/GrTexturePriv.h"
33#include "src/gpu/gl/GrGLBuffer.h"
34#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000047#if GR_GL_CHECK_ALLOC_WITH_GET_ERROR
48 #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface)
49 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call)
50 #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051#else
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000052 #define CLEAR_ERROR_BEFORE_ALLOC(iface)
53 #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call)
54 #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR
55#endif
56
Jim Van Verth32ac83e2016-11-28 15:23:57 -050057//#define USE_NSIGHT
58
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000059///////////////////////////////////////////////////////////////////////////////
60
cdalton8917d622015-05-06 13:40:21 -070061static const GrGLenum gXfermodeEquation2Blend[] = {
62 // Basic OpenGL blend equations.
63 GR_GL_FUNC_ADD,
64 GR_GL_FUNC_SUBTRACT,
65 GR_GL_FUNC_REVERSE_SUBTRACT,
66
67 // GL_KHR_blend_equation_advanced.
68 GR_GL_SCREEN,
69 GR_GL_OVERLAY,
70 GR_GL_DARKEN,
71 GR_GL_LIGHTEN,
72 GR_GL_COLORDODGE,
73 GR_GL_COLORBURN,
74 GR_GL_HARDLIGHT,
75 GR_GL_SOFTLIGHT,
76 GR_GL_DIFFERENCE,
77 GR_GL_EXCLUSION,
78 GR_GL_MULTIPLY,
79 GR_GL_HSL_HUE,
80 GR_GL_HSL_SATURATION,
81 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050082 GR_GL_HSL_LUMINOSITY,
83
84 // Illegal... needs to map to something.
85 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070086};
Brian Salomon4dea72a2019-12-18 10:43:10 -050087static_assert(0 == kAdd_GrBlendEquation);
88static_assert(1 == kSubtract_GrBlendEquation);
89static_assert(2 == kReverseSubtract_GrBlendEquation);
90static_assert(3 == kScreen_GrBlendEquation);
91static_assert(4 == kOverlay_GrBlendEquation);
92static_assert(5 == kDarken_GrBlendEquation);
93static_assert(6 == kLighten_GrBlendEquation);
94static_assert(7 == kColorDodge_GrBlendEquation);
95static_assert(8 == kColorBurn_GrBlendEquation);
96static_assert(9 == kHardLight_GrBlendEquation);
97static_assert(10 == kSoftLight_GrBlendEquation);
98static_assert(11 == kDifference_GrBlendEquation);
99static_assert(12 == kExclusion_GrBlendEquation);
100static_assert(13 == kMultiply_GrBlendEquation);
101static_assert(14 == kHSLHue_GrBlendEquation);
102static_assert(15 == kHSLSaturation_GrBlendEquation);
103static_assert(16 == kHSLColor_GrBlendEquation);
104static_assert(17 == kHSLLuminosity_GrBlendEquation);
105static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700106
twiz@google.com0f31ca72011-03-18 17:38:11 +0000107static const GrGLenum gXfermodeCoeff2Blend[] = {
108 GR_GL_ZERO,
109 GR_GL_ONE,
110 GR_GL_SRC_COLOR,
111 GR_GL_ONE_MINUS_SRC_COLOR,
112 GR_GL_DST_COLOR,
113 GR_GL_ONE_MINUS_DST_COLOR,
114 GR_GL_SRC_ALPHA,
115 GR_GL_ONE_MINUS_SRC_ALPHA,
116 GR_GL_DST_ALPHA,
117 GR_GL_ONE_MINUS_DST_ALPHA,
118 GR_GL_CONSTANT_COLOR,
119 GR_GL_ONE_MINUS_CONSTANT_COLOR,
120 GR_GL_CONSTANT_ALPHA,
121 GR_GL_ONE_MINUS_CONSTANT_ALPHA,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
bsalomon861e1032014-12-16 07:33:49 -0800133bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) {
bsalomon@google.com080773c2011-03-15 19:09:25 +0000134 static const bool gCoeffReferencesBlendConst[] = {
135 false,
136 false,
137 false,
138 false,
139 false,
140 false,
141 false,
142 false,
143 false,
144 false,
145 true,
146 true,
147 true,
148 true,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
150 // extended blend coeffs
151 false,
152 false,
153 false,
154 false,
Mike Klein36743362018-11-06 08:23:30 -0500155
156 // Illegal.
157 false,
bsalomon@google.com080773c2011-03-15 19:09:25 +0000158 };
159 return gCoeffReferencesBlendConst[coeff];
Brian Salomon4dea72a2019-12-18 10:43:10 -0500160 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161
Brian Salomon4dea72a2019-12-18 10:43:10 -0500162 static_assert(0 == kZero_GrBlendCoeff);
163 static_assert(1 == kOne_GrBlendCoeff);
164 static_assert(2 == kSC_GrBlendCoeff);
165 static_assert(3 == kISC_GrBlendCoeff);
166 static_assert(4 == kDC_GrBlendCoeff);
167 static_assert(5 == kIDC_GrBlendCoeff);
168 static_assert(6 == kSA_GrBlendCoeff);
169 static_assert(7 == kISA_GrBlendCoeff);
170 static_assert(8 == kDA_GrBlendCoeff);
171 static_assert(9 == kIDA_GrBlendCoeff);
172 static_assert(10 == kConstC_GrBlendCoeff);
173 static_assert(11 == kIConstC_GrBlendCoeff);
174 static_assert(12 == kConstA_GrBlendCoeff);
175 static_assert(13 == kIConstA_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176
Brian Salomon4dea72a2019-12-18 10:43:10 -0500177 static_assert(14 == kS2C_GrBlendCoeff);
178 static_assert(15 == kIS2C_GrBlendCoeff);
179 static_assert(16 == kS2A_GrBlendCoeff);
180 static_assert(17 == kIS2A_GrBlendCoeff);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000181
182 // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope
Brian Salomon4dea72a2019-12-18 10:43:10 -0500183 static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend));
bsalomon@google.com080773c2011-03-15 19:09:25 +0000184}
185
Brian Salomond978b902019-02-07 15:09:18 -0500186//////////////////////////////////////////////////////////////////////////////
187
188static int gl_target_to_binding_index(GrGLenum target) {
189 switch (target) {
190 case GR_GL_TEXTURE_2D:
191 return 0;
192 case GR_GL_TEXTURE_RECTANGLE:
193 return 1;
194 case GR_GL_TEXTURE_EXTERNAL:
195 return 2;
196 }
197 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500198}
199
200GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500201 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
202}
203
204bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
205 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500206}
207
208void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500209 int targetIndex = gl_target_to_binding_index(target);
210 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
211 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500212}
213
Brian Salomon1f05d452019-02-08 12:33:08 -0500214void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
215 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500216}
217
Brian Salomon1f05d452019-02-08 12:33:08 -0500218void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
219 for (auto& targetBinding : fTargetBindings) {
220 targetBinding.fBoundResourceID.makeInvalid();
221 if (markUnmodified) {
222 targetBinding.fHasBeenModified = false;
223 }
Brian Salomond978b902019-02-07 15:09:18 -0500224 }
225}
226
227//////////////////////////////////////////////////////////////////////////////
228
Brian Salomondc829942018-10-23 16:07:24 -0400229static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
230 switch (filter) {
231 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
232 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
233 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
234 }
235 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400236}
237
238static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
239 switch (filter) {
240 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
241 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
242 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
243 }
244 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400245}
246
Michael Ludwigf23a1522018-12-10 11:36:13 -0500247static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
248 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400249 switch (wrapMode) {
250 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
251 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
252 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500253 case GrSamplerState::WrapMode::kClampToBorder:
254 // May not be supported but should have been caught earlier
255 SkASSERT(caps.clampToBorderSupport());
256 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500257 }
Brian Salomondc829942018-10-23 16:07:24 -0400258 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400259}
260
261///////////////////////////////////////////////////////////////////////////////
262
263class GrGLGpu::SamplerObjectCache {
264public:
265 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
266 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
267 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
268 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
269 std::fill_n(fSamplers, kNumSamplers, 0);
270 }
271
272 ~SamplerObjectCache() {
273 if (!fNumTextureUnits) {
274 // We've already been abandoned.
275 return;
276 }
Chris Dalton703fe682019-05-15 12:58:12 -0600277 for (GrGLuint sampler : fSamplers) {
278 // The spec states that "zero" values should be silently ignored, however they still
279 // trigger GL errors on some NVIDIA platforms.
280 if (sampler) {
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
282 }
283 }
Brian Salomondc829942018-10-23 16:07:24 -0400284 }
285
Brian Salomonccb61422020-01-09 10:46:36 -0500286 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400287 int index = StateToIndex(state);
288 if (!fSamplers[index]) {
289 GrGLuint s;
290 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
291 if (!s) {
292 return;
293 }
294 fSamplers[index] = s;
295 auto minFilter = filter_to_gl_min_filter(state.filter());
296 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500297 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
298 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400299 GR_GL_CALL(fGpu->glInterface(),
300 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
301 GR_GL_CALL(fGpu->glInterface(),
302 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
303 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
304 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
305 }
306 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
307 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
308 fHWBoundSamplers[unitIdx] = fSamplers[index];
309 }
310 }
311
312 void invalidateBindings() {
313 // When we have sampler support we always use samplers. So setting these to zero will cause
314 // a rebind on next usage.
315 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
316 }
317
318 void abandon() {
319 fHWBoundSamplers.reset();
320 fNumTextureUnits = 0;
321 }
322
323 void release() {
324 if (!fNumTextureUnits) {
325 // We've already been abandoned.
326 return;
327 }
328 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
329 std::fill_n(fSamplers, kNumSamplers, 0);
330 // Deleting a bound sampler implicitly binds sampler 0.
331 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
332 }
333
334private:
Brian Salomonccb61422020-01-09 10:46:36 -0500335 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400336 int filter = static_cast<int>(state.filter());
337 SkASSERT(filter >= 0 && filter < 3);
338 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500339 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400340 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500341 SkASSERT(wrapY >= 0 && wrapY < 4);
342 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400343 SkASSERT(idx < kNumSamplers);
344 return idx;
345 }
346
347 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500348 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400349 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
350 GrGLuint fSamplers[kNumSamplers];
351 int fNumTextureUnits;
352};
353
reed@google.comac10a2d2010-12-22 21:39:39 +0000354///////////////////////////////////////////////////////////////////////////////
355
Brian Salomon384fab42017-12-07 12:33:05 -0500356sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
357 GrContext* context) {
358 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500359 interface = GrGLMakeNativeInterface();
360 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
361 // to GrGLMakeNativeInterface.
362 if (!interface) {
363 interface = sk_ref_sp(GrGLCreateNativeInterface());
364 }
Brian Salomon384fab42017-12-07 12:33:05 -0500365 if (!interface) {
366 return nullptr;
367 }
bsalomon424cc262015-05-22 10:37:30 -0700368 }
Jim Van Verth76334772017-05-05 16:46:05 -0400369#ifdef USE_NSIGHT
370 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
371#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500372 auto glContext = GrGLContext::Make(std::move(interface), options);
373 if (!glContext) {
374 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700375 }
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500376 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
bsalomon424cc262015-05-22 10:37:30 -0700377}
378
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500379GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
380 : GrGpu(context)
381 , fGLContext(std::move(ctx))
382 , fProgramCache(new ProgramCache(this))
383 , fHWProgramID(0)
384 , fTempSrcFBOID(0)
385 , fTempDstFBOID(0)
Brian Osmana63593a2018-12-06 15:54:53 -0500386 , fStencilClearFBOID(0) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500387 SkASSERT(fGLContext);
Brian Salomondc829942018-10-23 16:07:24 -0400388 GrGLClearErr(this->glInterface());
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500389 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000390
Brian Salomond978b902019-02-07 15:09:18 -0500391 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000392
Brian Salomonae64c192019-02-05 09:41:37 -0500393 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
394 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500395 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500396 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
397 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
398 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
399 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700400 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500401 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
402 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700403 }
Brian Salomonae64c192019-02-05 09:41:37 -0500404 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400405 fHWBufferState[i].invalidate();
406 }
Brian Salomon4dea72a2019-12-18 10:43:10 -0500407 static_assert(4 == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700408
409 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
410 fPathRendering.reset(new GrGLPathRendering(this));
411 }
412
Brian Salomondc829942018-10-23 16:07:24 -0400413 if (this->glCaps().samplerObjectSupport()) {
414 fSamplerObjectCache.reset(new SamplerObjectCache(this));
415 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000416}
417
bsalomon861e1032014-12-16 07:33:49 -0800418GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700419 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
420 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800421 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700422 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700423 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800424
Brian Salomon802cb312018-06-08 18:05:20 -0400425 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400426 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000427 // detach the current program so there is no confusion on OpenGL's part
428 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000429 GL_CALL(UseProgram(0));
430 }
431
Brian Salomon43f8bf02017-10-18 08:33:29 -0400432 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700433 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800434 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400435 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700436 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800437 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400438 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700439 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800440 }
egdaniel0f5f9672015-02-03 11:10:51 -0800441
bsalomon7ea33f52015-11-22 14:51:00 -0800442 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
443 if (0 != fCopyPrograms[i].fProgram) {
444 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
445 }
bsalomon6df86402015-06-01 10:41:49 -0700446 }
bsalomon6dea83f2015-12-03 12:58:06 -0800447
brianosman33f6b3f2016-06-02 05:49:21 -0700448 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
449 if (0 != fMipmapPrograms[i].fProgram) {
450 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
451 }
452 }
453
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000454 delete fProgramCache;
Brian Salomondc829942018-10-23 16:07:24 -0400455 fSamplerObjectCache.reset();
bsalomonc8dc1f72014-08-21 13:02:13 -0700456}
457
bsalomon6e2aad42016-04-01 11:54:31 -0700458void GrGLGpu::disconnect(DisconnectType type) {
459 INHERITED::disconnect(type);
460 if (DisconnectType::kCleanup == type) {
461 if (fHWProgramID) {
462 GL_CALL(UseProgram(0));
463 }
464 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700465 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700466 }
467 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700468 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700469 }
470 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700471 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700472 }
bsalomon6e2aad42016-04-01 11:54:31 -0700473 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
474 if (fCopyPrograms[i].fProgram) {
475 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
476 }
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
479 if (fMipmapPrograms[i].fProgram) {
480 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
481 }
482 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400483
Brian Salomondc829942018-10-23 16:07:24 -0400484 if (fSamplerObjectCache) {
485 fSamplerObjectCache->release();
486 }
bsalomon6e2aad42016-04-01 11:54:31 -0700487 } else {
488 if (fProgramCache) {
489 fProgramCache->abandon();
490 }
Brian Salomondc829942018-10-23 16:07:24 -0400491 if (fSamplerObjectCache) {
492 fSamplerObjectCache->abandon();
493 }
bsalomon6e2aad42016-04-01 11:54:31 -0700494 }
495
Brian Salomon802cb312018-06-08 18:05:20 -0400496 fHWProgram.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700497 delete fProgramCache;
498 fProgramCache = nullptr;
499
bsalomonc8dc1f72014-08-21 13:02:13 -0700500 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700501 fTempSrcFBOID = 0;
502 fTempDstFBOID = 0;
503 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700504 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800505 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
506 fCopyPrograms[i].fProgram = 0;
507 }
brianosman33f6b3f2016-06-02 05:49:21 -0700508 fMipmapProgramArrayBuffer.reset();
509 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
510 fMipmapPrograms[i].fProgram = 0;
511 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400512
jvanverthe9c0fc62015-04-29 11:18:05 -0700513 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700514 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700515 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000516}
517
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000518///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000519
bsalomon861e1032014-12-16 07:33:49 -0800520void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000521 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400522 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 GL_CALL(Disable(GR_GL_DEPTH_TEST));
524 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000525
Brian Salomonf0861672017-05-08 11:10:10 -0400526 // We don't use face culling.
527 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600528 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
529 // just set this to the default for self-consistency.
530 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400531
Brian Salomonae64c192019-02-05 09:41:37 -0500532 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
533 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700534
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400535 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500536#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000537 // Desktop-only state that we never change
538 if (!this->glCaps().isCoreProfile()) {
539 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
540 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
541 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
542 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
543 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
544 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
545 }
546 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
547 // core profile. This seems like a bug since the core spec removes any mention of
548 // GL_ARB_imaging.
549 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
550 GL_CALL(Disable(GR_GL_COLOR_TABLE));
551 }
552 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400553
Chris Dalton1215cda2019-12-17 21:44:04 -0700554 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500555#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000556 // Since ES doesn't support glPointSize at all we always use the VS to
557 // set the point size
558 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
559
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000560 }
joshualitt58162332014-08-01 06:44:53 -0700561
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400562 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500563 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700564 // The arm extension requires specifically enabling MSAA fetching per sample.
565 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500566 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700567 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000568 fHWWriteToColor = kUnknown_TriState;
569 // we only ever use lines in hairline mode
570 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700571 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500572
573 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000574 }
edisonn@google.comba669992013-06-28 16:03:21 +0000575
egdanielb414f252014-07-29 13:15:47 -0700576 if (resetBits & kMSAAEnable_GrGLBackendState) {
577 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700578
Chris Dalton6ce447a2019-06-23 18:07:38 -0600579 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800580 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
581 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700582 GL_CALL(CoverageModulation(GR_GL_RGBA));
583 }
Chris Daltonce425af2019-12-16 10:39:03 -0700584
585 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000587
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000588 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400589 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500592 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500593 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 }
Brian Salomondc829942018-10-23 16:07:24 -0400595 if (fSamplerObjectCache) {
596 fSamplerObjectCache->invalidateBindings();
597 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000598 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000599
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 if (resetBits & kBlend_GrGLBackendState) {
601 fHWBlendState.invalidate();
602 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kView_GrGLBackendState) {
605 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700606 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 fHWViewport.invalidate();
608 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000609
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 if (resetBits & kStencil_GrGLBackendState) {
611 fHWStencilSettings.invalidate();
612 fHWStencilTestEnabled = kUnknown_TriState;
613 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000614
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000615 // Vertex
616 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700617 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500618 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
619 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700620 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000621 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000622
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500624 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700625 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000626 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000627
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000628 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700629 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700630 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000631 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000632 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000633
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000634 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000635 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400636 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000637 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
638 }
Brian Salomon1047a492019-07-02 12:25:21 -0400639 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000640 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
641 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000642 if (this->glCaps().packFlipYSupport()) {
643 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
644 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000645 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000646
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000647 if (resetBits & kProgram_GrGLBackendState) {
648 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400649 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000650 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400651 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000652}
653
Brian Salomonea4ad302019-08-07 13:04:55 -0400654static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
655 const GrGLCaps& caps, GrGLTexture::Desc* desc,
656 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400657 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400658 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400659 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800660 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000661
Brian Salomonea4ad302019-08-07 13:04:55 -0400662 desc->fSize = {backendTex.width(), backendTex.height()};
663 desc->fTarget = info.fTarget;
664 desc->fID = info.fID;
665 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800666
Brian Salomonea4ad302019-08-07 13:04:55 -0400667 if (desc->fFormat == GrGLFormat::kUnknown) {
668 return false;
669 }
670 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400671 if (!caps.shaderCaps()->externalTextureSupport()) {
672 return false;
673 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400674 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
675 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400676 return false;
677 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400678 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400679 return false;
680 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400681 if (backendTex.isProtected()) {
682 // Not supported in GL backend at this time.
683 return false;
684 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400685
Brian Salomond17f6582017-07-19 18:28:58 -0400686 return true;
687}
688
689sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400690 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400691 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400692 GrGLTexture::Desc desc;
693 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400694 return nullptr;
695 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400696
Brian Salomond17f6582017-07-19 18:28:58 -0400697 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400698 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400699 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400700 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400701 }
bsalomone5286e02016-01-14 09:24:09 -0800702
Greg Daniel177e6952017-10-12 12:27:11 -0400703 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
704 : GrMipMapsStatus::kNotAllocated;
705
Brian Salomonea4ad302019-08-07 13:04:55 -0400706 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400707 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400708 // We don't know what parameters are already set on wrapped textures.
709 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400710 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400711}
712
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500713static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
714 const GrGLCaps& caps, GrGLTexture::Desc* desc,
715 bool skipRectTexSupportCheck = false) {
716 GrGLTextureInfo info;
717 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
718 return false;
719 }
720
721 desc->fSize = {backendTex.width(), backendTex.height()};
722 desc->fTarget = info.fTarget;
723 desc->fID = info.fID;
724 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
725
726 if (desc->fFormat == GrGLFormat::kUnknown) {
727 return false;
728 }
729
730 if (GR_GL_TEXTURE_2D != desc->fTarget) {
731 return false;
732 }
733 if (backendTex.isProtected()) {
734 // Not supported in GL backend at this time.
735 return false;
736 }
737
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500738 return true;
739}
740
Robert Phillipsb915c942019-12-17 14:44:37 -0500741sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
742 GrWrapOwnership ownership,
743 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500744 GrGLTexture::Desc desc;
745 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
746 return nullptr;
747 }
748
749 if (kBorrow_GrWrapOwnership == ownership) {
750 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
751 } else {
752 desc.fOwnership = GrBackendObjectOwnership::kOwned;
753 }
754
755 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
756 : GrMipMapsStatus::kNotAllocated;
757
758 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
759 backendTex.getGLTextureParams(), cacheable,
760 kRead_GrIOType);
761 // We don't know what parameters are already set on wrapped textures.
762 texture->textureParamsModified();
763 return texture;
Robert Phillipsb915c942019-12-17 14:44:37 -0500764}
765
Brian Salomond17f6582017-07-19 18:28:58 -0400766sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400767 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400768 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500769 GrWrapOwnership ownership,
770 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400771 const GrGLCaps& caps = this->glCaps();
772
Brian Salomonea4ad302019-08-07 13:04:55 -0400773 GrGLTexture::Desc desc;
774 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800775 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800776 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400777 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
778 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800779
Brian Salomond17f6582017-07-19 18:28:58 -0400780 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400781 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800782 return nullptr;
783 }
bsalomon10528f12015-10-14 12:54:52 -0700784
Brian Osman766fcbb2017-03-13 09:33:09 -0400785 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400786 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400787 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400788 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800789 }
bsalomonb15b4c12014-10-29 12:41:57 -0700790
Robert Phillips0902c982019-07-16 07:47:56 -0400791
Greg Daniel6fa62e22019-08-07 15:52:37 -0400792 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
793 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000794
Brian Salomonea4ad302019-08-07 13:04:55 -0400795 GrGLRenderTarget::IDs rtIDs;
796 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400797 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000798 }
Greg Daniel177e6952017-10-12 12:27:11 -0400799
800 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
801 : GrMipMapsStatus::kNotAllocated;
802
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500803 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400804 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400805 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400806 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400807 // We don't know what parameters are already set on wrapped textures.
808 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400809 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000810}
811
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400812sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
813 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400814 GrGLFramebufferInfo info;
815 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000816 return nullptr;
817 }
818
Brian Salomone8a766b2019-07-19 14:24:36 -0400819 if (backendRT.isProtected()) {
820 // Not supported in GL at this time.
821 return nullptr;
822 }
823
Brian Salomond4764a12019-08-08 12:08:24 -0400824 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400825 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
826 return nullptr;
827 }
828
Brian Salomonea4ad302019-08-07 13:04:55 -0400829 GrGLRenderTarget::IDs rtIDs;
830 rtIDs.fRTFBOID = info.fFBOID;
831 rtIDs.fMSColorRenderbufferID = 0;
832 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
833 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000834
Brian Salomonea4ad302019-08-07 13:04:55 -0400835 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400836 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700837
Greg Danield51fa2f2020-01-22 16:53:38 -0500838 return GrGLRenderTarget::MakeWrapped(this, size, format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400839 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000840}
841
Greg Daniel7ef28f32017-04-20 16:41:55 +0000842sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400843 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400844 GrColorType colorType) {
845 GrGLTexture::Desc desc;
846 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
847 // with a texture rectangle,we assume the necessary support exists.
848 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800849 return nullptr;
850 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400851
852 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
853 return nullptr;
854 }
855
856 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400857 GrGLRenderTarget::IDs rtIDs;
858 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800859 return nullptr;
860 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500861 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800862}
863
Brian Salomonc320b152018-02-20 14:05:36 -0500864static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700865 if (!glTex) {
866 return false;
867 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000868
bsalomone5286e02016-01-14 09:24:09 -0800869 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
870 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800871 return false;
872 }
873
jvanverth17aa0472016-01-05 10:41:27 -0800874 return true;
875}
876
Brian Salomona9b04b92018-06-01 15:04:28 -0400877bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400878 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400879 const GrMipLevel texels[], int mipLevelCount,
880 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500881 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800882
Brian Salomonc320b152018-02-20 14:05:36 -0500883 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800884 return false;
885 }
886
Brian Salomond978b902019-02-07 15:09:18 -0500887 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000888
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400889 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400890 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400891 glTex->target(), left, top, width, height, srcColorType, texels,
892 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000893}
894
Brian Salomone05ba5a2019-04-08 11:59:07 -0400895bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400896 GrColorType textureColorType, GrColorType bufferColorType,
897 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400898 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400899
Jim Van Verth1676cb92019-01-15 13:24:45 -0500900 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400901 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500902
Brian Salomonc320b152018-02-20 14:05:36 -0500903 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400904 return false;
905 }
906
Hal Canaryc36f7e72018-06-18 15:50:09 -0400907 static_assert(sizeof(int) == sizeof(int32_t), "");
908 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400909 return false;
910 }
911
Brian Salomond978b902019-02-07 15:09:18 -0500912 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400913
914 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500915 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400916 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500917 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400918
Greg Daniel660cc992017-06-26 14:55:05 -0400919 SkDEBUGCODE(
920 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
921 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
922 SkASSERT(bounds.contains(subRect));
923 )
924
Brian Salomonb28cb682019-07-26 12:48:47 -0400925 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400926 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400927 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700928 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400929 return false;
930 }
931
932 bool restoreGLRowLength = false;
933 if (trimRowBytes != rowBytes) {
934 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400935 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400936 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
937 restoreGLRowLength = true;
938 }
939
Greg Danielba88ab62019-07-26 09:14:01 -0400940 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400941 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400942 GrGLenum externalFormat = 0;
943 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400944 this->glCaps().getTexSubImageExternalFormatAndType(
945 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400946 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400947 return false;
948 }
949
Brian Salomon8cbf6622019-07-30 11:03:14 -0400950 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400951 GL_CALL(TexSubImage2D(glTex->target(),
952 0,
953 left, top,
954 width,
955 height,
956 externalFormat, externalType,
957 pixels));
958
959 if (restoreGLRowLength) {
960 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
961 }
962
963 return true;
964}
965
Brian Salomon26de56e2019-04-10 12:14:26 -0400966bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400967 GrColorType surfaceColorType, GrColorType dstColorType,
968 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400969 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
970 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400971 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400972 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
973 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400974}
975
Brian Osman9b560242017-09-05 15:34:52 -0400976void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500977 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
978 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
979 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
980 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400981 }
Brian Osman9b560242017-09-05 15:34:52 -0400982}
983
Brian Salomond2a8ae22019-09-10 16:03:59 -0400984bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
985 int texHeight, GrGLenum target, int left, int top, int width,
986 int height, GrColorType srcColorType, const GrMipLevel texels[],
987 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500988 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400989 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500990
Greg Daniel7bfc9132019-08-14 14:23:53 -0400991 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -0400992 SkDEBUGCODE(
993 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
994 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
995 SkASSERT(bounds.contains(subRect));
996 )
Robert Phillips590533f2017-07-11 14:22:35 -0400997 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -0400998 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -0800999
Brian Osman9b560242017-09-05 15:34:52 -04001000 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001001
cblume55f2d2d2016-02-26 13:20:48 -08001002 const GrGLInterface* interface = this->glInterface();
1003 const GrGLCaps& caps = this->glCaps();
1004
Brian Salomonf77c1462019-08-01 15:19:29 -04001005 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001006
1007 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001008 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001009 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001010
bsalomon5b30c6f2015-12-17 14:17:34 -08001011 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001012 GrGLenum externalFormat;
1013 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001014 this->glCaps().getTexSubImageExternalFormatAndType(
1015 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001016 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001017 return false;
1018 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001019
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001020 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001021 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001022 * because our srcData has extra bytes past each row. If so, we need
1023 * to trim those off here, since GL ES may not let us specify
1024 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001025 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001026 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001027
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001028 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001029 *mipMapsStatus = (mipLevelCount > 1) ?
1030 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001031 }
bsalomone699d0c2016-03-09 06:25:15 -08001032
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001034
Brian Salomond2a8ae22019-09-10 16:03:59 -04001035 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1036 if (!texels[currentMipLevel].fPixels) {
1037 if (mipMapsStatus) {
1038 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001039 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001040 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001041 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001042 int twoToTheMipLevel = 1 << currentMipLevel;
1043 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1044 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1045 const size_t trimRowBytes = currentWidth * bpp;
1046 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1047
1048 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1049 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1050 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1051 restoreGLRowLength = true;
1052 }
1053
1054 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1055 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001056 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001057 if (restoreGLRowLength) {
1058 SkASSERT(caps.writePixelsRowBytesSupport());
1059 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1060 }
1061 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001062}
1063
Greg Daniel7bfc9132019-08-14 14:23:53 -04001064bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001065 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001066 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001067 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001068 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001069 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001070 const GrGLCaps& caps = this->glCaps();
1071
1072 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001073 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001074 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001075 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001076 }
1077
Robert Phillips9f744f72019-12-19 19:14:33 -05001078 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1079 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1080
Greg Daniel7bfc9132019-08-14 14:23:53 -04001081 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001082
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001083 int numMipLevels = 1;
1084 if (mipMapped == GrMipMapped::kYes) {
1085 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1086 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001088 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001089 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001090
1091 if (useTexStorage) {
1092 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001093 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001094 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001095 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001096 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1097 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001098 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001099 }
Robert Phillips42716d42019-12-16 12:19:54 -05001100
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001101 size_t offset = 0;
1102 for (int level = 0; level < numMipLevels; ++level) {
1103
Robert Phillips9f744f72019-12-19 19:14:33 -05001104 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1105 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001106
1107 GL_CALL(CompressedTexSubImage2D(target,
1108 level,
1109 0, // left
1110 0, // top
1111 dimensions.width(),
1112 dimensions.height(),
1113 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001114 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001115 &((char*)data)[offset]));
1116
1117 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1118 if (error != GR_GL_NO_ERROR) {
1119 return false;
1120 }
1121
Robert Phillips9f744f72019-12-19 19:14:33 -05001122 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001123 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001124 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001125 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001126 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001127
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001128 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001129 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1130 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001131
1132 const char* rawLevelData = &((char*)data)[offset];
1133 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1134 level,
1135 internalFormat,
1136 dimensions.width(),
1137 dimensions.height(),
1138 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001139 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001140 rawLevelData));
1141
1142 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1143 if (error != GR_GL_NO_ERROR) {
1144 return false;
1145 }
1146
Robert Phillips9f744f72019-12-19 19:14:33 -05001147 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001148 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001149 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001150 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001151 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001152}
1153
bsalomon424cc262015-05-22 10:37:30 -07001154static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001155 int sampleCount,
1156 GrGLenum format,
1157 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001158 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001159 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001160 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001161 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001162 GL_ALLOC_CALL(ctx.interface(),
1163 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1164 sampleCount,
1165 format,
1166 width, height));
1167 break;
1168 case GrGLCaps::kES_Apple_MSFBOType:
1169 GL_ALLOC_CALL(ctx.interface(),
1170 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1171 sampleCount,
1172 format,
1173 width, height));
1174 break;
1175 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1176 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1177 GL_ALLOC_CALL(ctx.interface(),
1178 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1179 sampleCount,
1180 format,
1181 width, height));
1182 break;
1183 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001184 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001185 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001186 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001187 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001188}
1189
Brian Salomonea4ad302019-08-07 13:04:55 -04001190bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001191 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 GrGLRenderTarget::IDs* rtIDs) {
1193 rtIDs->fMSColorRenderbufferID = 0;
1194 rtIDs->fRTFBOID = 0;
1195 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1196 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197
bsalomona11e5fc2015-12-18 07:59:41 -08001198 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001199
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001201 goto FAILED;
1202 }
1203
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001204 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001205 goto FAILED;
1206 }
1207
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1209 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 goto FAILED;
1211 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001212
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001213 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1214 // the texture bound to the other. The exception is the IMG multisample extension. With this
1215 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1216 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001217 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001218 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1219 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1220 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 goto FAILED;
1222 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001225 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 }
1227
egdanield803f272015-03-18 13:01:52 -07001228 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001229 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001231 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001232 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001233 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001234 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 goto FAILED;
1236 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001237 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001238 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001239 GR_GL_COLOR_ATTACHMENT0,
1240 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001242 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001243 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001245 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001246 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1247 GR_GL_COLOR_ATTACHMENT0,
1248 desc.fTarget,
1249 desc.fID,
1250 0,
1251 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001252 } else {
egdanield803f272015-03-18 13:01:52 -07001253 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001254 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001255 desc.fTarget,
1256 desc.fID,
1257 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001258 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001259
1260 return true;
1261
1262FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001263 if (rtIDs->fMSColorRenderbufferID) {
1264 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001265 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1267 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001268 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001269 if (rtIDs->fTexFBOID) {
1270 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001271 }
1272 return false;
1273}
1274
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001275// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001276static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001277// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001278 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001279}
1280
Brian Salomone2826ab2019-06-04 15:58:31 -04001281static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001282 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001283 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1284 // drivers have a bug where an FBO won't be complete if it includes a
1285 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001286 GrGLTextureParameters::SamplerOverriddenState state;
1287 state.fMinFilter = GR_GL_NEAREST;
1288 state.fMagFilter = GR_GL_NEAREST;
1289 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1290 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001291 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1292 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1293 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1294 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001295 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001296}
1297
Robert Phillips67d52cf2017-06-05 13:38:13 -04001298sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001299 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001300 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001301 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001302 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001303 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001304 int mipLevelCount,
1305 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001306 // We don't support protected textures in GL.
1307 if (isProtected == GrProtected::kYes) {
1308 return nullptr;
1309 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001310 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001311
Brian Salomond2a8ae22019-09-10 16:03:59 -04001312 SkASSERT(mipLevelCount > 0);
1313 GrMipMapsStatus mipMapsStatus =
1314 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001315 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 GrGLTexture::Desc texDesc;
1317 texDesc.fSize = {desc.fWidth, desc.fHeight};
1318 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001319 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001320 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001321 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1322 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001323
Brian Salomond2a8ae22019-09-10 16:03:59 -04001324 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1325 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001326
1327 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001328 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001329 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001330
Robert Phillips67d52cf2017-06-05 13:38:13 -04001331 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001332 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001333 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001334 GL_CALL(BindTexture(texDesc.fTarget, 0));
1335 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001336
Brian Salomonea4ad302019-08-07 13:04:55 -04001337 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1338 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 return return_null_texture();
1340 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001341 tex = sk_make_sp<GrGLTextureRenderTarget>(
1342 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001343 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001344 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001345 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001347 // The non-sampler params are still at their default values.
1348 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1349 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001350 if (levelClearMask) {
1351 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001352 GrColorType colorType;
1353 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1354 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001355 if (this->glCaps().clearTextureSupport()) {
1356 for (int i = 0; i < mipLevelCount; ++i) {
1357 if (levelClearMask & (1U << i)) {
1358 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1359 nullptr));
1360 }
1361 }
1362 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1363 !this->glCaps().performColorClearsAsDraws()) {
1364 this->disableScissor();
1365 this->disableWindowRectangles();
1366 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001367 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001368 for (int i = 0; i < mipLevelCount; ++i) {
1369 if (levelClearMask & (1U << i)) {
1370 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1371 kDst_TempFBOTarget);
1372 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1373 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1374 }
1375 }
Brian Salomonc2249852019-09-16 11:08:50 -04001376 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001377 } else {
1378 std::unique_ptr<char[]> zeros;
1379 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1380 for (int i = 0; i < mipLevelCount; ++i) {
1381 if (levelClearMask & (1U << i)) {
1382 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1383 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1384 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1385 // smaller levels that need clearing.
1386 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001387 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001388 size_t size = levelWidth * levelHeight * bpp;
1389 zeros.reset(new char[size]());
1390 }
1391 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1392 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1393 externalFormat, externalType, zeros.get()));
1394 }
Brian Salomona3e29962019-07-16 11:52:08 -04001395 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001396 }
1397 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001398 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001399}
1400
Robert Phillips9f744f72019-12-19 19:14:33 -05001401sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001402 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001403 SkBudgeted budgeted,
1404 GrMipMapped mipMapped,
1405 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001406 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001407 // We don't support protected textures in GL.
1408 if (isProtected == GrProtected::kYes) {
1409 return nullptr;
1410 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001411 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001412 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001413 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001414 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001415 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001416 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001417 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillipse4720c62020-01-14 14:33:24 -05001418 mipMapped, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001419 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001420 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001421 return nullptr;
1422 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001423
Robert Phillipsee946932019-12-18 11:16:17 -05001424 // Unbind this texture from the scratch texture unit.
1425 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1426
Robert Phillipse4720c62020-01-14 14:33:24 -05001427 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1428 ? GrMipMapsStatus::kValid
1429 : GrMipMapsStatus::kNotAllocated;
1430
1431 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001432 // The non-sampler params are still at their default values.
1433 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1434 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001435 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001436}
1437
Robert Phillipsb915c942019-12-17 14:44:37 -05001438GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1439 const GrBackendFormat& format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001440 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05001441 GrProtected isProtected,
1442 const BackendTextureData* data) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001443 // We don't support protected textures in GL.
1444 if (isProtected == GrProtected::kYes) {
1445 return {};
1446 }
1447
1448 this->handleDirtyContext();
1449
1450 GrGLFormat glFormat = format.asGLFormat();
1451 if (glFormat == GrGLFormat::kUnknown) {
1452 return {};
1453 }
1454
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001455 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001456 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001457 SkAutoMalloc am;
1458
1459 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1460 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1461 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001462 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001463 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001464 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1465 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001466
Robert Phillips9f744f72019-12-19 19:14:33 -05001467 rawDataSize = GrCompressedDataSize(compression, dimensions, nullptr, mipMapped);
1468
1469 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001470
1471 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1472
1473 rawData = (const char*) am.get();
1474 }
1475
1476 GrGLTextureInfo info;
1477 GrGLTextureParameters::SamplerOverriddenState initialState;
1478
1479 info.fTarget = GR_GL_TEXTURE_2D;
1480 info.fFormat = GrGLFormatToEnum(glFormat);
1481 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001482 mipMapped, &initialState,
1483 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001484 if (!info.fID) {
1485 return {};
1486 }
1487
1488 // Unbind this texture from the scratch texture unit.
1489 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1490
1491 auto parameters = sk_make_sp<GrGLTextureParameters>();
1492 // The non-sampler params are still at their default values.
1493 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1494 fResetTimestampForTextureParameters);
1495
1496 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1497 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001498}
1499
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001500namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001501
egdaniel8dc7c3a2015-04-16 11:22:42 -07001502const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001503
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001504void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001505 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001506
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001507 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001508 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 (kUnknownBitCount == format->fTotalBits));
1510 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001511 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001512 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1513 (GrGLint*)&format->fStencilBits);
1514 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001515 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001516 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1517 (GrGLint*)&format->fTotalBits);
1518 format->fTotalBits += format->fStencilBits;
1519 } else {
1520 format->fTotalBits = format->fStencilBits;
1521 }
1522 }
1523}
1524}
1525
Brian Salomon5043f1f2019-07-11 21:27:54 -04001526int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001527 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001528 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1529
1530 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001531 // Default to unsupported, set this if we find a stencil format that works.
1532 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001533
Brian Salomond2a8ae22019-09-10 16:03:59 -04001534 GrGLuint colorID =
1535 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1536 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001537 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001538 }
egdanielff1d5472015-09-10 08:37:20 -07001539 // unbind the texture from the texture unit before binding it to the frame buffer
1540 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1541
1542 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001543 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001544 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001545 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001546 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001547 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1548 GR_GL_COLOR_ATTACHMENT0,
1549 GR_GL_TEXTURE_2D,
1550 colorID,
1551 0));
bsalomon30447372015-12-21 09:03:05 -08001552 GrGLuint sbRBID = 0;
1553 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001554
1555 // look over formats till I find a compatible one
1556 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001557 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001558 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001559 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1560 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1561 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1562 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1563 sFmt.fInternalFormat,
1564 kSize, kSize));
1565 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001566 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001567 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001568 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001569 if (sFmt.fPacked) {
1570 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1571 GR_GL_DEPTH_ATTACHMENT,
1572 GR_GL_RENDERBUFFER, sbRBID));
1573 } else {
1574 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1575 GR_GL_DEPTH_ATTACHMENT,
1576 GR_GL_RENDERBUFFER, 0));
1577 }
1578 GrGLenum status;
1579 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1580 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1581 firstWorkingStencilFormatIndex = i;
1582 break;
1583 }
egdanielff1d5472015-09-10 08:37:20 -07001584 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1585 GR_GL_STENCIL_ATTACHMENT,
1586 GR_GL_RENDERBUFFER, 0));
1587 if (sFmt.fPacked) {
1588 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1589 GR_GL_DEPTH_ATTACHMENT,
1590 GR_GL_RENDERBUFFER, 0));
1591 }
egdanielff1d5472015-09-10 08:37:20 -07001592 }
1593 }
bsalomon30447372015-12-21 09:03:05 -08001594 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001595 }
1596 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001597 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1598 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001599 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001600 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001601 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001602}
1603
Brian Salomonea4ad302019-08-07 13:04:55 -04001604GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001605 SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001606 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001607 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001608 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001609 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001610 if (format == GrGLFormat::kUnknown) {
1611 return 0;
1612 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001613 GrGLuint id = 0;
1614 GL_CALL(GenTextures(1, &id));
1615 if (!id) {
1616 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001617 }
1618
Brian Salomonea4ad302019-08-07 13:04:55 -04001619 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001620
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1622
Robert Phillips42716d42019-12-16 12:19:54 -05001623 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001624 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1625 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001626 GL_CALL(DeleteTextures(1, &id));
1627 return 0;
1628 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001629 }
Robert Phillips42716d42019-12-16 12:19:54 -05001630
Brian Salomonea4ad302019-08-07 13:04:55 -04001631 return id;
1632}
1633
Robert Phillips2716daf2020-01-23 12:53:42 -05001634GrGLuint GrGLGpu::createTexture2D(SkISize dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001635 GrGLFormat format,
1636 GrRenderable renderable,
1637 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001638 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001639 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001640 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001641
Brian Salomonea4ad302019-08-07 13:04:55 -04001642 GrGLuint id = 0;
1643 GL_CALL(GenTextures(1, &id));
1644
1645 if (!id) {
1646 return 0;
1647 }
1648
1649 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001650
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001651 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001652 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001653 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001654 }
1655
Brian Salomond2a8ae22019-09-10 16:03:59 -04001656 if (initialState) {
1657 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1658 } else {
1659 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001660 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001661
1662 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1663
1664 bool success = false;
1665 if (internalFormat) {
1666 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1667 if (this->glCaps().formatSupportsTexStorage(format)) {
1668 GL_ALLOC_CALL(this->glInterface(),
1669 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001670 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001671 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1672 } else {
1673 GrGLenum externalFormat, externalType;
1674 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1675 GrGLenum error = GR_GL_NO_ERROR;
1676 if (externalFormat && externalType) {
1677 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1678 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001679 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1680 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001681 GL_ALLOC_CALL(
1682 this->glInterface(),
1683 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1684 currentHeight, 0, externalFormat, externalType, nullptr));
1685 error = CHECK_ALLOC_ERROR(this->glInterface());
1686 }
1687 success = (GR_GL_NO_ERROR == error);
1688 }
1689 }
1690 }
1691 if (success) {
1692 return id;
1693 }
1694 GL_CALL(DeleteTextures(1, &id));
1695 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001696}
1697
Chris Daltoneffee202019-07-01 22:28:03 -06001698GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1699 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001700 SkASSERT(width >= rt->width());
1701 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001702
egdaniel8dc7c3a2015-04-16 11:22:42 -07001703 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001704
Brian Salomond4764a12019-08-08 12:08:24 -04001705 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001706 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001707 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001708 }
egdanielff1d5472015-09-10 08:37:20 -07001709
1710 if (!sbDesc.fRenderbufferID) {
1711 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1712 }
1713 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001714 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001715 }
1716 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1717 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1718 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1719 // we do this "if" so that we don't call the multisample
1720 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001721 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001722 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001723 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001724 sFmt.fInternalFormat,
1725 width, height));
1726 } else {
1727 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1728 sFmt.fInternalFormat,
1729 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001730 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001731 }
1732 fStats.incStencilAttachmentCreates();
1733 // After sized formats we attempt an unsized format and take
1734 // whatever sizes GL gives us. In that case we query for the size.
1735 GrGLStencilAttachment::Format format = sFmt;
1736 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001737 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1738 sbDesc,
1739 width,
1740 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001741 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001742 format);
1743 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001744}
1745
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001746////////////////////////////////////////////////////////////////////////////////
1747
Brian Salomondbf70722019-02-07 11:31:24 -05001748sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1749 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001750 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001751}
1752
Greg Danielacd66b42019-05-22 16:29:12 -04001753void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001754 GrSurfaceOrigin rtOrigin) {
1755 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001756 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001757 // if the scissor fully contains the viewport then we fall through and
1758 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001759 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001760 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001761 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001762 fHWScissorSettings.fRect = scissor;
1763 }
1764 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1765 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1766 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001767 }
1768 return;
1769 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001771
1772 // See fall through note above
1773 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001774}
1775
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001776void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001777 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001778#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779 typedef GrWindowRectsState::Mode Mode;
1780 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1781 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001782
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001783 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001784 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001785 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001786 }
1787
csmartdalton7535f412016-08-23 06:51:00 -07001788 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1789 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1791 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001792
Chris Daltond6cda8d2019-09-05 02:30:04 -06001793 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001794 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001795 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001796 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001797 }
1798
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001799 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001800 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001801
Greg Danielacd66b42019-05-22 16:29:12 -04001802 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001803#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001804}
1805
1806void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001807#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001808 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001809 return;
1810 }
1811 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001812 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001813#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001814}
1815
Robert Phillipsfcaae482019-11-07 10:17:03 -05001816bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001817
Robert Phillipsfcaae482019-11-07 10:17:03 -05001818 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001819 if (!program) {
1820 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1821 return false;
1822 }
brianosman33f6b3f2016-06-02 05:49:21 -07001823
Brian Salomon802cb312018-06-08 18:05:20 -04001824 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001825
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001826 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001827 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1828 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001829
Robert Phillips901aff02019-10-08 12:32:56 -04001830 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001831
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001832 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001833 GrStencilSettings stencil;
1834 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001835 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1836 stencil.reset(*programInfo.pipeline().getUserStencil(),
1837 programInfo.pipeline().hasStencilClip(),
1838 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001839 }
Robert Phillips901aff02019-10-08 12:32:56 -04001840 this->flushStencil(stencil, programInfo.origin());
1841 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001842 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001843 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1844 : kBogusScissor);
1845 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001846 } else {
1847 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001848 }
Robert Phillips901aff02019-10-08 12:32:56 -04001849 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1850 glRT, programInfo.origin());
1851 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001852 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001853 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001854
1855 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001856 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001857 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001858
1859 return true;
1860}
1861
Brian Salomon802cb312018-06-08 18:05:20 -04001862void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1863 if (!program) {
1864 fHWProgram.reset();
1865 fHWProgramID = 0;
1866 return;
1867 }
1868 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1869 if (program == fHWProgram) {
1870 return;
1871 }
1872 auto id = program->programID();
1873 SkASSERT(id);
1874 GL_CALL(UseProgram(id));
1875 fHWProgram = std::move(program);
1876 fHWProgramID = id;
1877}
1878
1879void GrGLGpu::flushProgram(GrGLuint id) {
1880 SkASSERT(id);
1881 if (fHWProgramID == id) {
1882 SkASSERT(!fHWProgram);
1883 return;
1884 }
1885 fHWProgram.reset();
1886 GL_CALL(UseProgram(id));
1887 fHWProgramID = id;
1888}
1889
1890void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001891 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001892 int baseVertex,
1893 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001894 int baseInstance,
1895 GrPrimitiveRestart enablePrimitiveRestart) {
1896 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001897
cdaltone2e71c22016-04-07 18:13:29 -07001898 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001899 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001900 SkASSERT(indexBuffer->isCpuBuffer() ||
1901 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001902 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001903 } else {
1904 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001905 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001906
Brian Salomon92be2f72018-06-19 14:33:47 -04001907 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1908 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001909
Brian Salomon802cb312018-06-08 18:05:20 -04001910 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001911 SkASSERT(vertexBuffer);
1912 SkASSERT(vertexBuffer->isCpuBuffer() ||
1913 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1914 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001915 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1916 const auto& attrib = fHWProgram->vertexAttribute(i);
1917 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001918 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1919 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001920 }
Chris Dalton1d616352017-05-31 12:51:23 -06001921 }
Brian Salomon802cb312018-06-08 18:05:20 -04001922 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001923 SkASSERT(instanceBuffer);
1924 SkASSERT(instanceBuffer->isCpuBuffer() ||
1925 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1926 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001927 int attribIdx = fHWProgram->numVertexAttributes();
1928 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1929 const auto& attrib = fHWProgram->instanceAttribute(i);
1930 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001931 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1932 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1933 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001934 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001935 }
1936}
1937
Brian Salomonae64c192019-02-05 09:41:37 -05001938GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001939 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001940
cdaltone2e71c22016-04-07 18:13:29 -07001941 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001942 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001943 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001944 }
cdaltone2e71c22016-04-07 18:13:29 -07001945
Brian Salomonae64c192019-02-05 09:41:37 -05001946 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001947 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001948 if (!bufferState->fBufferZeroKnownBound) {
1949 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1950 bufferState->fBufferZeroKnownBound = true;
1951 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001952 }
Brian Salomondbf70722019-02-07 11:31:24 -05001953 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1954 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001955 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1956 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1957 bufferState->fBufferZeroKnownBound = false;
1958 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001959 }
1960
Brian Salomonae64c192019-02-05 09:41:37 -05001961 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001962}
Brian Salomond818ebf2018-07-02 14:08:49 +00001963void GrGLGpu::disableScissor() {
1964 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1965 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1966 fHWScissorSettings.fEnabled = kNo_TriState;
1967 return;
1968 }
1969}
1970
Brian Osman9a9baae2018-11-05 15:06:26 -05001971void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001972 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001973 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001974 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001975 SkASSERT(!this->caps()->performColorClearsAsDraws());
1976 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001977
Brian Salomon43f8bf02017-10-18 08:33:29 -04001978 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001979
Brian Salomon43f8bf02017-10-18 08:33:29 -04001980 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1981
Brian Salomond818ebf2018-07-02 14:08:49 +00001982 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001983 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001984 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001985 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001986 }
Greg Danielacd66b42019-05-22 16:29:12 -04001987 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001988 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001989 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001990 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001991 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001992}
1993
Robert Phillips95214472017-08-08 18:00:03 -04001994void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001995 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1996
Robert Phillips95214472017-08-08 18:00:03 -04001997 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00001998 return;
1999 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002000
Chris Dalton674f77a2019-09-30 20:49:39 -06002001 // This should only be called internally when we know we have a stencil buffer.
2002 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002003
bsalomonb0bd4f62014-09-03 07:19:50 -07002004 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002005 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002006
Brian Salomond818ebf2018-07-02 14:08:49 +00002007 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002008 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002009
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002010 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002011 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002013 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002014}
2015
Chris Daltonb50cc812019-10-14 16:29:21 -06002016static bool use_tiled_rendering(const GrGLCaps& glCaps,
2017 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2018 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2019 // Otherwise it's faster to just not use it.
2020 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2021 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2022}
2023
2024void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002025 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2026 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2027 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2028
2029 this->handleDirtyContext();
2030
2031 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2032 this->flushRenderTarget(glRT);
2033 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2034
Chris Daltonb50cc812019-10-14 16:29:21 -06002035 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2036 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2037 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2038 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2039 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2040 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2041 nativeBounds.fHeight, preserveMask));
2042 }
2043
Chris Dalton674f77a2019-09-30 20:49:39 -06002044 GrGLbitfield clearMask = 0;
2045 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2046 SkASSERT(!this->caps()->performColorClearsAsDraws());
2047 this->flushClearColor(colorLoadStore.fClearColor);
2048 this->flushColorWrite(true);
2049 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002050 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002051 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2052 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2053 GL_CALL(StencilMask(0xffffffff));
2054 GL_CALL(ClearStencil(0));
2055 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2056 }
2057 if (clearMask) {
2058 this->disableScissor();
2059 this->disableWindowRectangles();
2060 GL_CALL(Clear(clearMask));
2061 }
2062}
2063
2064void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2065 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2066 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2067 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2068
2069 this->handleDirtyContext();
2070
2071 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2072 // The framebuffer binding changed in the middle of a command buffer. We should have already
2073 // printed a warning during onFBOChanged.
2074 return;
2075 }
2076
2077 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2078 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2079
2080 SkSTArray<2, GrGLenum> discardAttachments;
2081 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2082 discardAttachments.push_back(
2083 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2084 }
2085 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2086 discardAttachments.push_back(
2087 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2088 }
2089
2090 if (!discardAttachments.empty()) {
2091 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2092 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2093 discardAttachments.begin()));
2094 } else {
2095 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2096 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2097 discardAttachments.begin()));
2098 }
2099 }
2100 }
2101
Chris Daltonb50cc812019-10-14 16:29:21 -06002102 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2103 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2104 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2105 // Handled by use_tiled_rendering().
2106 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2107 GL_CALL(EndTiling(preserveMask));
2108 }
2109
Chris Dalton674f77a2019-09-30 20:49:39 -06002110 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002111}
2112
csmartdalton29df7602016-08-31 11:55:52 -07002113void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2114 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002115 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002116 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002117 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002118 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002119
egdaniel8dc7c3a2015-04-16 11:22:42 -07002120 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002121 // this should only be called internally when we know we have a
2122 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002123 SkASSERT(sb);
2124 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002125#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002126 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002127 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002128#else
2129 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002130 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002131 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002132 // changing the clip between stencil passes may or may not
2133 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002134 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002135#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002136 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002137 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002138 value = (1 << (stencilBitCount - 1));
2139 } else {
2140 value = 0;
2141 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002142 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002143 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002144
Greg Danielacd66b42019-05-22 16:29:12 -04002145 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002146 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002147
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002148 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002149 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002150 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002151 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002152}
2153
Brian Salomone05ba5a2019-04-08 11:59:07 -04002154bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002155 GrColorType surfaceColorType, GrColorType dstColorType,
2156 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002157 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002158
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002159 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002160 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002161 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002162 return false;
2163 }
Greg Danielba88ab62019-07-26 09:14:01 -04002164 GrGLenum externalFormat = 0;
2165 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002166 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2167 surfaceColorType,
2168 dstColorType,
2169 &externalFormat,
2170 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002171 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002172 return false;
2173 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002174
Brian Salomon71d9d842016-11-03 13:42:00 -04002175 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002176 if (renderTarget->numSamples() <= 1 ||
2177 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2178 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2179 this->flushRenderTargetNoColorWrites(renderTarget);
2180 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2181 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2182 return false;
2183 } else {
2184 SkASSERT(renderTarget->requiresManualMSAAResolve());
2185 // we don't track the state of the READ FBO ID.
2186 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002187 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002188 } else {
2189 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002190 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002191 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002192 }
2193
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002194 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002195 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002196
Brian Salomona6948702018-06-01 15:33:20 -04002197 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002198 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002199 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002200 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002201 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002202 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002203
Brian Osman1348ed02018-09-12 09:08:39 -04002204 bool reattachStencil = false;
2205 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2206 renderTarget &&
2207 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002208 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002209 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2210 reattachStencil = true;
2211 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2212 GR_GL_RENDERBUFFER, 0));
2213 }
2214
Chris Dalton76500e52019-09-05 02:13:05 -06002215 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002216 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002217
2218 if (reattachStencil) {
2219 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2220 renderTarget->renderTargetPriv().getStencilAttachment());
2221 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2222 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2223 }
2224
Brian Salomon26de56e2019-04-10 12:14:26 -04002225 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002226 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002227 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2228 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002229
Brian Salomone05ba5a2019-04-08 11:59:07 -04002230 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002231 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002232 }
2233 return true;
2234}
2235
2236bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002237 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2238 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002239 SkASSERT(surface);
2240
Brian Salomonb28cb682019-07-26 12:48:47 -04002241 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002242
Brian Salomon26de56e2019-04-10 12:14:26 -04002243 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2244 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002245
Brian Salomon1047a492019-07-02 12:25:21 -04002246 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002247 rowPixelWidth = width;
2248 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002249 SkASSERT(!(rowBytes % bytesPerPixel));
2250 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002251 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002252 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2253 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002254}
2255
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002256GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002257 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002258 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002259 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002260 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002261 if (!fCachedOpsRenderPass) {
2262 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002263 }
2264
Chris Daltonb50cc812019-10-14 16:29:21 -06002265 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002266 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002267}
2268
Brian Salomon1fabd512018-02-09 09:54:25 -05002269void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002270 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002271 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002272 this->didWriteToSurface(target, origin, &bounds);
2273}
bsalomon6ba6fa12015-03-04 11:57:37 -08002274
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002275void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2276 this->flushRenderTargetNoColorWrites(target);
2277 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002278}
2279
Brian Osman9aa30c62018-07-02 15:21:46 -04002280void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002281 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002282 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002283 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002284 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002285#ifdef SK_DEBUG
2286 // don't do this check in Chromium -- this is causing
2287 // lots of repeated command buffer flushes when the compositor is
2288 // rendering with Ganesh, which is really slow; even too slow for
2289 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002290 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002291 GrGLenum status;
2292 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2293 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2294 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2295 }
bsalomon160f24c2015-03-17 15:55:42 -07002296 }
egdanield803f272015-03-18 13:01:52 -07002297#endif
2298 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002299 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002300 }
2301
brianosman35b784d2016-05-05 11:52:53 -07002302 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002303 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002304 }
bsalomon083617b2016-02-12 12:10:14 -08002305}
bsalomona9909122016-01-23 10:41:40 -08002306
brianosman33f6b3f2016-06-02 05:49:21 -07002307void GrGLGpu::flushFramebufferSRGB(bool enable) {
2308 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2309 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2310 fHWSRGBFramebuffer = kYes_TriState;
2311 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2312 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2313 fHWSRGBFramebuffer = kNo_TriState;
2314 }
2315}
2316
Greg Danielacd66b42019-05-22 16:29:12 -04002317void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002318 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002319 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002320 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002321 fHWViewport = viewport;
2322 }
2323}
2324
bsalomon@google.comd302f142011-03-03 13:54:13 +00002325#define SWAP_PER_DRAW 0
2326
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002327#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002328 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002329 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002330 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002331 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002332 void SwapBuf() {
2333 DWORD procID = GetCurrentProcessId();
2334 HWND hwnd = GetTopWindow(GetDesktopWindow());
2335 while(hwnd) {
2336 DWORD wndProcID = 0;
2337 GetWindowThreadProcessId(hwnd, &wndProcID);
2338 if(wndProcID == procID) {
2339 SwapBuffers(GetDC(hwnd));
2340 }
2341 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2342 }
2343 }
2344 #endif
2345#endif
2346
Robert Phillips901aff02019-10-08 12:32:56 -04002347void GrGLGpu::draw(GrRenderTarget* renderTarget,
2348 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002349 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002350 int meshCount) {
2351 this->handleDirtyContext();
2352
Robert Phillips2579de42019-10-09 09:51:59 -04002353 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002354
Robert Phillipsfcaae482019-11-07 10:17:03 -05002355 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002356 return;
2357 }
ethannicholas22793252016-01-30 09:59:10 -08002358
Robert Phillips901aff02019-10-08 12:32:56 -04002359 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2360 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2361
Brian Salomonf7232642018-09-19 08:58:08 -04002362 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002363 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2364 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002365 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002366 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002367
Robert Phillips901aff02019-10-08 12:32:56 -04002368 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002369 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002370 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2371 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002372 }
Robert Phillips901aff02019-10-08 12:32:56 -04002373 if (hasDynamicPrimProcTextures) {
2374 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2375 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2376 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002377 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002378 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002379 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002380 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2381 GL_CALL(Enable(GR_GL_CULL_FACE));
2382 GL_CALL(Disable(GR_GL_CULL_FACE));
2383 }
Brian Salomonf7232642018-09-19 08:58:08 -04002384 meshes[m].sendToGpu(this);
2385 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002386 }
ethannicholas22793252016-01-30 09:59:10 -08002387
bsalomon@google.comd302f142011-03-03 13:54:13 +00002388#if SWAP_PER_DRAW
2389 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002390 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002391 aglSwapBuffers(aglGetCurrentContext());
2392 int set_a_break_pt_here = 9;
2393 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002394 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002395 SwapBuf();
2396 int set_a_break_pt_here = 9;
2397 SwapBuf();
2398 #endif
2399#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002400}
2401
Chris Dalton3809bab2017-06-13 10:55:06 -06002402static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2403 switch (primitiveType) {
2404 case GrPrimitiveType::kTriangles:
2405 return GR_GL_TRIANGLES;
2406 case GrPrimitiveType::kTriangleStrip:
2407 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002408 case GrPrimitiveType::kPoints:
2409 return GR_GL_POINTS;
2410 case GrPrimitiveType::kLines:
2411 return GR_GL_LINES;
2412 case GrPrimitiveType::kLineStrip:
2413 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002414 case GrPrimitiveType::kPatches:
2415 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002416 case GrPrimitiveType::kPath:
2417 SK_ABORT("non-mesh-based GrPrimitiveType");
2418 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002419 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002420 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002421}
2422
Chris Dalton34280e22019-12-20 11:08:25 -07002423void GrGLGpu::sendArrayMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex) {
2424 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002425 if (GR_GL_PATCHES == glPrimType) {
2426 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2427 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002428 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002429 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2430 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002431 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2432 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002433 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002434 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2435 }
2436 fStats.incNumDraws();
2437}
2438
Brian Salomondbf70722019-02-07 11:31:24 -05002439static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2440 size_t baseOffset = baseIndex * sizeof(uint16_t);
2441 if (indexBuffer->isCpuBuffer()) {
2442 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2443 } else {
2444 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2445 }
2446}
2447
Chris Dalton34280e22019-12-20 11:08:25 -07002448void GrGLGpu::sendIndexedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2449 uint16_t minIndexValue, uint16_t maxIndexValue, int baseVertex) {
2450 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002451 if (GR_GL_PATCHES == glPrimType) {
2452 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2453 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002454
Chris Dalton34280e22019-12-20 11:08:25 -07002455 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2456
2457 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2458 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002459
2460 if (this->glCaps().drawRangeElementsSupport()) {
2461 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002462 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002463 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002464 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002465 }
2466 fStats.incNumDraws();
2467}
2468
Chris Dalton34280e22019-12-20 11:08:25 -07002469void GrGLGpu::sendInstancedMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex,
2470 int instanceCount, int baseInstance) {
2471 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002472 if (GR_GL_PATCHES == glPrimType) {
2473 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2474 }
Chris Dalton1b4ad762018-10-04 11:58:09 -06002475 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002476 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002477 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2478 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002479 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2480 SkTMin(instanceCount - i, maxInstances)));
2481 fStats.incNumDraws();
2482 }
Chris Dalton1d616352017-05-31 12:51:23 -06002483}
2484
Chris Dalton34280e22019-12-20 11:08:25 -07002485void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2486 int baseVertex, int instanceCount, int baseInstance) {
2487 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002488 if (GR_GL_PATCHES == glPrimType) {
2489 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2490 }
Chris Dalton34280e22019-12-20 11:08:25 -07002491 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002492 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2493 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002494 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2495 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002496 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002497 SkTMin(instanceCount - i, maxInstances)));
2498 fStats.incNumDraws();
2499 }
Chris Dalton1d616352017-05-31 12:51:23 -06002500}
2501
Chris Dalton16a33c62019-09-24 22:19:17 -06002502void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2503 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002504 // Some extensions automatically resolves the texture when it is read.
2505 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2506
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002507 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002508 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2509 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2510 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2511 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002512
Chris Daltonc139d082019-09-26 14:04:24 -06002513 // make sure we go through flushRenderTarget() since we've modified
2514 // the bound DRAW FBO ID.
2515 fHWBoundRenderTargetUniqueID.makeInvalid();
2516 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2517 // Apple's extension uses the scissor as the blit bounds.
2518 GrScissorState scissorState;
2519 scissorState.set(resolveRect);
2520 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2521 this->disableWindowRectangles();
2522 GL_CALL(ResolveMultisampleFramebuffer());
2523 } else {
2524 int l, b, r, t;
2525 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2526 this->glCaps().blitFramebufferSupportFlags()) {
2527 l = 0;
2528 b = 0;
2529 r = target->width();
2530 t = target->height();
2531 } else {
2532 auto rect = GrNativeRect::MakeRelativeTo(
2533 resolveOrigin, rt->height(), resolveRect);
2534 l = rect.fX;
2535 b = rect.fY;
2536 r = rect.fX + rect.fWidth;
2537 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002538 }
Chris Daltonc139d082019-09-26 14:04:24 -06002539
2540 // BlitFrameBuffer respects the scissor, so disable it.
2541 this->disableScissor();
2542 this->disableWindowRectangles();
2543 GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
reed@google.comac10a2d2010-12-22 21:39:39 +00002544 }
2545}
2546
bsalomon@google.com411dad02012-06-05 20:24:20 +00002547namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002548
bsalomon@google.com411dad02012-06-05 20:24:20 +00002549
2550GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002551 static const GrGLenum gTable[kGrStencilOpCount] = {
2552 GR_GL_KEEP, // kKeep
2553 GR_GL_ZERO, // kZero
2554 GR_GL_REPLACE, // kReplace
2555 GR_GL_INVERT, // kInvert
2556 GR_GL_INCR_WRAP, // kIncWrap
2557 GR_GL_DECR_WRAP, // kDecWrap
2558 GR_GL_INCR, // kIncClamp
2559 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002560 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002561 static_assert(0 == (int)GrStencilOp::kKeep);
2562 static_assert(1 == (int)GrStencilOp::kZero);
2563 static_assert(2 == (int)GrStencilOp::kReplace);
2564 static_assert(3 == (int)GrStencilOp::kInvert);
2565 static_assert(4 == (int)GrStencilOp::kIncWrap);
2566 static_assert(5 == (int)GrStencilOp::kDecWrap);
2567 static_assert(6 == (int)GrStencilOp::kIncClamp);
2568 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002569 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2570 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002571}
2572
2573void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002574 const GrStencilSettings::Face& face,
2575 GrGLenum glFace) {
2576 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2577 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2578 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002579
cdalton93a379b2016-05-11 13:58:08 -07002580 GrGLint ref = face.fRef;
2581 GrGLint mask = face.fTestMask;
2582 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002583
2584 if (GR_GL_FRONT_AND_BACK == glFace) {
2585 // we call the combined func just in case separate stencil is not
2586 // supported.
2587 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2588 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002589 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002590 } else {
2591 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2592 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002593 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002594 }
2595}
2596}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002597
Chris Dalton71713f62019-04-18 12:41:03 -06002598void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002599 if (stencilSettings.isDisabled()) {
2600 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002601 } else if (fHWStencilSettings != stencilSettings ||
2602 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002603 if (kYes_TriState != fHWStencilTestEnabled) {
2604 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002605
csmartdaltonc7d85332016-10-26 10:13:46 -07002606 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002607 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002608 if (!stencilSettings.isTwoSided()) {
2609 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2610 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002611 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002612 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2613 GR_GL_FRONT);
2614 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2615 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002616 }
joshualitta58fe352014-10-27 08:39:00 -07002617 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002618 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002619 }
2620}
2621
csmartdaltonc7d85332016-10-26 10:13:46 -07002622void GrGLGpu::disableStencil() {
2623 if (kNo_TriState != fHWStencilTestEnabled) {
2624 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002625
csmartdaltonc7d85332016-10-26 10:13:46 -07002626 fHWStencilTestEnabled = kNo_TriState;
2627 fHWStencilSettings.invalidate();
2628 }
2629}
2630
Chris Dalton4c56b032019-03-04 12:28:42 -07002631void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002632 // rt is only optional if useHWAA is false.
2633 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002634#ifdef SK_DEBUG
2635 if (useHWAA && rt->numSamples() <= 1) {
2636 SkASSERT(this->caps()->mixedSamplesSupport());
2637 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2638 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2639 }
2640#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002641
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002642 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002643 if (useHWAA) {
2644 if (kYes_TriState != fMSAAEnabled) {
2645 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2646 fMSAAEnabled = kYes_TriState;
2647 }
2648 } else {
2649 if (kNo_TriState != fMSAAEnabled) {
2650 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2651 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002652 }
2653 }
2654 }
2655}
2656
Chris Daltonce425af2019-12-16 10:39:03 -07002657void GrGLGpu::flushConservativeRasterState(bool enabled) {
2658 if (this->caps()->conservativeRasterSupport()) {
2659 if (enabled) {
2660 if (kYes_TriState != fHWConservativeRasterEnabled) {
2661 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2662 fHWConservativeRasterEnabled = kYes_TriState;
2663 }
2664 } else {
2665 if (kNo_TriState != fHWConservativeRasterEnabled) {
2666 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2667 fHWConservativeRasterEnabled = kNo_TriState;
2668 }
2669 }
2670 }
2671}
2672
Chris Dalton1215cda2019-12-17 21:44:04 -07002673void GrGLGpu::flushWireframeState(bool enabled) {
2674 if (this->caps()->wireframeSupport()) {
2675 if (this->caps()->wireframeMode() || enabled) {
2676 if (kYes_TriState != fHWWireframeEnabled) {
2677 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2678 fHWWireframeEnabled = kYes_TriState;
2679 }
2680 } else {
2681 if (kNo_TriState != fHWWireframeEnabled) {
2682 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2683 fHWWireframeEnabled = kNo_TriState;
2684 }
2685 }
2686 }
2687}
2688
Chris Daltonbbb3f642019-07-24 12:25:08 -04002689void GrGLGpu::flushBlendAndColorWrite(
2690 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2691 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2692 // We need to work around a driver bug by using a blend state that preserves the dst color,
2693 // rather than disabling color writes.
2694 GrXferProcessor::BlendInfo preserveDstBlend;
2695 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2696 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2697 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2698 return;
2699 }
bsalomonf7cc8772015-05-11 11:21:14 -07002700
cdalton8917d622015-05-06 13:40:21 -07002701 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002702 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2703 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002704
2705 // Any optimization to disable blending should have already been applied and
2706 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002707 bool blendOff =
2708 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2709 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2710 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002711
egdanielb414f252014-07-29 13:15:47 -07002712 if (blendOff) {
2713 if (kNo_TriState != fHWBlendState.fEnabled) {
2714 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002715
2716 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2717 // https://code.google.com/p/skia/issues/detail?id=3943
2718 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2719 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2720 SkASSERT(this->caps()->advancedBlendEquationSupport());
2721 // Set to any basic blending equation.
2722 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2723 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2724 fHWBlendState.fEquation = blend_equation;
2725 }
2726
egdanielb414f252014-07-29 13:15:47 -07002727 fHWBlendState.fEnabled = kNo_TriState;
2728 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002729 } else {
2730 if (kYes_TriState != fHWBlendState.fEnabled) {
2731 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002732
Chris Daltonbbb3f642019-07-24 12:25:08 -04002733 fHWBlendState.fEnabled = kYes_TriState;
2734 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002735
Chris Daltonbbb3f642019-07-24 12:25:08 -04002736 if (fHWBlendState.fEquation != equation) {
2737 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2738 fHWBlendState.fEquation = equation;
2739 }
cdalton8917d622015-05-06 13:40:21 -07002740
Chris Daltonbbb3f642019-07-24 12:25:08 -04002741 if (GrBlendEquationIsAdvanced(equation)) {
2742 SkASSERT(this->caps()->advancedBlendEquationSupport());
2743 // Advanced equations have no other blend state.
2744 return;
2745 }
cdalton8917d622015-05-06 13:40:21 -07002746
Chris Daltonbbb3f642019-07-24 12:25:08 -04002747 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2748 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2749 gXfermodeCoeff2Blend[dstCoeff]));
2750 fHWBlendState.fSrcCoeff = srcCoeff;
2751 fHWBlendState.fDstCoeff = dstCoeff;
2752 }
cdalton8917d622015-05-06 13:40:21 -07002753
Chris Daltonbbb3f642019-07-24 12:25:08 -04002754 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2755 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2756 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2757 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2758 fHWBlendState.fConstColor = blendConst;
2759 fHWBlendState.fConstColorValid = true;
2760 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002761 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002762 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002763
2764 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002765}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002766
Brian Salomondc829942018-10-23 16:07:24 -04002767static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002768 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002769 switch (swizzle[i]) {
2770 case 'r': glValues[i] = GR_GL_RED; break;
2771 case 'g': glValues[i] = GR_GL_GREEN; break;
2772 case 'b': glValues[i] = GR_GL_BLUE; break;
2773 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002774 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002775 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002776 default: SK_ABORT("Unsupported component");
2777 }
Brian Salomon327955b2018-10-22 15:39:22 +00002778 }
2779}
2780
Greg Daniel2c3398d2019-06-19 11:58:01 -04002781void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2782 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002783 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002784
reed856e9d92015-09-30 12:21:45 -07002785#ifdef SK_DEBUG
2786 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002787 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002788 const int w = texture->width();
2789 const int h = texture->height();
2790 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2791 }
2792 }
2793#endif
2794
Robert Phillips294870f2016-11-11 12:38:40 -05002795 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002796 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002797 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002798 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002799 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002800 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002801 }
2802
Brian Salomondc829942018-10-23 16:07:24 -04002803 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002804 if (!this->caps()->mipMapSupport() ||
2805 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002806 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002807 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002808 }
bsalomonefd7d452014-10-23 14:17:46 -07002809
brianosman33f6b3f2016-06-02 05:49:21 -07002810#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002811 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002812 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002813 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002814 }
2815#endif
2816
Brian Salomone2826ab2019-06-04 15:58:31 -04002817 auto timestamp = texture->parameters()->resetTimestamp();
2818 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002819
Brian Salomone2826ab2019-06-04 15:58:31 -04002820 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2821 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002822 if (fSamplerObjectCache) {
2823 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2824 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002825 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2826 texture->parameters()->samplerOverriddenState();
2827 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002828
Brian Salomone2826ab2019-06-04 15:58:31 -04002829 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2830 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002831
Brian Salomone2826ab2019-06-04 15:58:31 -04002832 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2833 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002834
2835 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002836 newSamplerState.fMinLOD = -1000.f;
2837 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002838
Brian Salomone2826ab2019-06-04 15:58:31 -04002839 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002840 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002841 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002842 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002843 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002844 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002845 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002846 }
Mike Klein1bccae52018-10-19 11:38:44 +00002847 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002849 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002850 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002851 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002852 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002853 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002854 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002855 }
2856 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002858 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002859 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002860 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002861 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002862 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002863 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002864 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002865 if (this->glCaps().clampToBorderSupport()) {
2866 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002867 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002868 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002869 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2870 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002871 }
2872 }
Brian Salomondc829942018-10-23 16:07:24 -04002873 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002874 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2875 newNonsamplerState.fBaseMipMapLevel = 0;
2876 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002877
Brian Salomone2826ab2019-06-04 15:58:31 -04002878 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2879 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002880 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002881 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2882 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002883 GrGLenum glValues[4];
2884 get_gl_swizzle_values(swizzle, glValues);
2885 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002886 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002887 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002888 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2889 reinterpret_cast<const GrGLint*>(glValues)));
2890 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002891 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2892 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2893 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2894 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2895 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002896 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002897 }
2898 }
Brian Salomondc829942018-10-23 16:07:24 -04002899 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002900 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2901 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2902 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002903 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002904 this->setTextureUnit(unitIdx);
2905 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002906 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002907 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002908 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002909 this->setTextureUnit(unitIdx);
2910 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002911 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002912 }
Brian Salomon327955b2018-10-22 15:39:22 +00002913 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002914 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2915 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002916}
2917
Brian Salomon1f05d452019-02-08 12:33:08 -05002918void GrGLGpu::onResetTextureBindings() {
2919 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2920 GR_GL_TEXTURE_EXTERNAL};
2921 for (int i = 0; i < this->numTextureUnits(); ++i) {
2922 this->setTextureUnit(i);
2923 for (auto target : kTargets) {
2924 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2925 GL_CALL(BindTexture(target, 0));
2926 }
2927 }
2928 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2929 }
2930}
2931
Chris Dalton5a2f9622019-12-27 14:56:38 -07002932void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2933 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2934 if (fHWPatchVertexCount != count) {
2935 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2936 fHWPatchVertexCount = count;
2937 }
2938}
2939
egdaniel080e6732014-12-22 07:35:52 -08002940void GrGLGpu::flushColorWrite(bool writeColor) {
2941 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002942 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002943 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2944 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002945 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002946 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002947 } else {
2948 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002949 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002950 fHWWriteToColor = kYes_TriState;
2951 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002952 }
bsalomon3e791242014-12-17 13:43:13 -08002953}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002954
Chris Dalton674f77a2019-09-30 20:49:39 -06002955void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2956 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2957 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2958 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2959 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2960 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2961 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2962 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002963 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2964 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2965 GL_CALL(ClearColor(r, g, b, a));
2966 fHWClearColor[0] = r;
2967 fHWClearColor[1] = g;
2968 fHWClearColor[2] = b;
2969 fHWClearColor[3] = a;
2970 }
2971}
2972
bsalomon861e1032014-12-16 07:33:49 -08002973void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002974 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002975 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002976 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002977 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002978 }
2979}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002980
Brian Salomond978b902019-02-07 15:09:18 -05002981void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002982 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002983 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002984 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2985 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2986 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002987 }
Brian Salomond978b902019-02-07 15:09:18 -05002988 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2989 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002990 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002991 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002992}
2993
Brian Salomone5e7eb12016-10-14 16:18:33 -04002994// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002995static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2996 const GrSurface* src,
2997 const SkIRect& srcRect,
2998 const SkIPoint& dstPoint,
2999 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003000 int dstSampleCnt = 0;
3001 int srcSampleCnt = 0;
3002 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003003 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003004 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003005 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003006 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003007 }
3008 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3009 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3010
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003011 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3012 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3013
Brian Salomone5e7eb12016-10-14 16:18:33 -04003014 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003015 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003016
Greg Daniel46cfbc62019-06-07 11:43:30 -04003017 GrTextureType dstTexType;
3018 GrTextureType* dstTexTypePtr = nullptr;
3019 GrTextureType srcTexType;
3020 GrTextureType* srcTexTypePtr = nullptr;
3021 if (dstTex) {
3022 dstTexType = dstTex->texturePriv().textureType();
3023 dstTexTypePtr = &dstTexType;
3024 }
3025 if (srcTex) {
3026 srcTexType = srcTex->texturePriv().textureType();
3027 srcTexTypePtr = &srcTexType;
3028 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003029
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003030 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3031 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003032 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003033}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003034
Brian Osmana9c8a052018-01-19 10:31:56 -05003035static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3036 // A RT has a separate MSAA renderbuffer if:
3037 // 1) It's multisampled
3038 // 2) We're using an extension with separate MSAA renderbuffers
3039 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003040 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003041}
3042
Greg Daniel46cfbc62019-06-07 11:43:30 -04003043static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003044 const GrGLCaps& caps) {
3045
bsalomon@google.comeb851172013-04-15 13:51:00 +00003046 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003047 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003048 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003049 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003050
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003051 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3052 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3053
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003054 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3055 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3056
Greg Daniel46cfbc62019-06-07 11:43:30 -04003057 GrTextureType dstTexType;
3058 GrTextureType* dstTexTypePtr = nullptr;
3059 GrTextureType srcTexType;
3060 GrTextureType* srcTexTypePtr = nullptr;
3061 if (dstTex) {
3062 dstTexType = dstTex->texturePriv().textureType();
3063 dstTexTypePtr = &dstTexType;
3064 }
3065 if (srcTex) {
3066 srcTexType = srcTex->texturePriv().textureType();
3067 srcTexTypePtr = &srcTexType;
3068 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003069
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003070 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3071 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003072}
3073
Greg Danielacd66b42019-05-22 16:29:12 -04003074// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003075void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003076 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003077 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003078 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003079 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003080 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3081 GrGLuint texID = texture->textureID();
3082 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003083 GrGLuint* tempFBOID;
3084 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003085
egdanield803f272015-03-18 13:01:52 -07003086 if (0 == *tempFBOID) {
3087 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003088 }
3089
Adrienne Walker4ee88512018-05-17 11:37:14 -07003090 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003091 GR_GL_CALL(
3092 this->glInterface(),
3093 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3094 if (mipLevel == 0) {
3095 texture->baseLevelWasBoundToFBO();
3096 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003097 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003098 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003099 }
egdaniel0f5f9672015-02-03 11:10:51 -08003100}
3101
Brian Salomond2a8ae22019-09-10 16:03:59 -04003102void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003103 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003104 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003105 SkASSERT(surface->asTexture());
3106 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3107 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3108 GR_GL_COLOR_ATTACHMENT0,
3109 textureTarget,
3110 0,
3111 0));
3112 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003113}
3114
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003115void GrGLGpu::onFBOChanged() {
3116 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3117 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3118 GL_CALL(Flush());
3119 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003120#ifdef SK_DEBUG
3121 if (fIsExecutingCommandBuffer_DebugOnly) {
3122 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3123 "This will severely hurt performance.\n");
3124 }
3125#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003126}
3127
Adrienne Walker4ee88512018-05-17 11:37:14 -07003128void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3129 fStats.incRenderTargetBinds();
3130 GL_CALL(BindFramebuffer(target, fboid));
3131 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3132 fBoundDrawFramebuffer = fboid;
3133 }
3134
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003135 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3136 // The driver forgets the correct scissor when modifying the FBO binding.
3137 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003138 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003139 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003140 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003141 }
3142
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003143 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003144}
3145
Adrienne Walker4ee88512018-05-17 11:37:14 -07003146void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3147 if (fboid == fBoundDrawFramebuffer &&
3148 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3149 // This workaround only applies to deleting currently bound framebuffers
3150 // on Adreno 420. Because this is a somewhat rare case, instead of
3151 // tracking all the attachments of every framebuffer instead just always
3152 // unbind all attachments.
3153 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3154 GR_GL_RENDERBUFFER, 0));
3155 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3156 GR_GL_RENDERBUFFER, 0));
3157 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3158 GR_GL_RENDERBUFFER, 0));
3159 }
3160
3161 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003162
3163 // Deleting the currently bound framebuffer rebinds to 0.
3164 if (fboid == fBoundDrawFramebuffer) {
3165 this->onFBOChanged();
3166 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003167}
3168
Greg Daniel46cfbc62019-06-07 11:43:30 -04003169bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003170 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003171 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3172 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3173 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003174 auto dstFormat = dst->backendFormat().asGLFormat();
3175 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003176 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003177 return true;
3178 }
3179 }
cblume61214052016-01-26 09:10:48 -08003180
Greg Daniel46cfbc62019-06-07 11:43:30 -04003181 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3182 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003183 return true;
3184 }
3185
Greg Daniel46cfbc62019-06-07 11:43:30 -04003186 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3187 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003188 }
3189
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003190 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003191 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003192 return true;
3193 }
bsalomon083617b2016-02-12 12:10:14 -08003194 }
3195
bsalomon6df86402015-06-01 10:41:49 -07003196 return false;
3197}
3198
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003199bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003200 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003201
3202 int progIdx = TextureToCopyProgramIdx(srcTex);
3203 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3204 GrSLType samplerType =
3205 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3206
3207 if (!fCopyProgramArrayBuffer) {
3208 static const GrGLfloat vdata[] = {
3209 0, 0,
3210 0, 1,
3211 1, 0,
3212 1, 1
3213 };
3214 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3215 kStatic_GrAccessPattern, vdata);
3216 }
3217 if (!fCopyProgramArrayBuffer) {
3218 return false;
3219 }
3220
3221 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3222 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3223 if (!fCopyPrograms[progIdx].fProgram) {
3224 return false;
3225 }
3226
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003227 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3228 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3229 GrShaderVar::kUniform_TypeModifier);
3230 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3231 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3232 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3233 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3234
Greg Daniel9e3169b2019-06-06 14:38:17 -04003235 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003236 if (shaderCaps->noperspectiveInterpolationSupport()) {
3237 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3238 vshaderTxt.appendf("#extension %s : require\n", extension);
3239 }
3240 vTexCoord.addModifier("noperspective");
3241 }
3242
3243 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3244 vshaderTxt.append(";");
3245 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3246 vshaderTxt.append(";");
3247 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3248 vshaderTxt.append(";");
3249 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3250 vshaderTxt.append(";");
3251
3252 vshaderTxt.append(
3253 "// Copy Program VS\n"
3254 "void main() {"
3255 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3256 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3257 " sk_Position.zw = half2(0, 1);"
3258 "}"
3259 );
3260
Greg Daniel9e3169b2019-06-06 14:38:17 -04003261 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003262 if (shaderCaps->noperspectiveInterpolationSupport()) {
3263 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3264 fshaderTxt.appendf("#extension %s : require\n", extension);
3265 }
3266 }
3267 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3268 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3269 fshaderTxt.append(";");
3270 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3271 fshaderTxt.append(";");
3272 fshaderTxt.appendf(
3273 "// Copy Program FS\n"
3274 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003275 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 "}"
3277 );
3278
3279 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3280 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3281 SkSL::Program::Settings settings;
3282 settings.fCaps = shaderCaps;
3283 SkSL::String glsl;
3284 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3285 sksl, settings, &glsl, errorHandler);
3286 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3287 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3288 SkASSERT(program->fInputs.isEmpty());
3289
3290 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3291 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3292 errorHandler);
3293 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3294 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3295 errorHandler);
3296 SkASSERT(program->fInputs.isEmpty());
3297
3298 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3299
3300 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3301 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3302 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3303 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3304 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3305 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3306
3307 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3308
3309 GL_CALL(DeleteShader(vshader));
3310 GL_CALL(DeleteShader(fshader));
3311
3312 return true;
3313}
3314
brianosman33f6b3f2016-06-02 05:49:21 -07003315bool GrGLGpu::createMipmapProgram(int progIdx) {
3316 const bool oddWidth = SkToBool(progIdx & 0x2);
3317 const bool oddHeight = SkToBool(progIdx & 0x1);
3318 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3319
Brian Salomon1edc5b92016-11-29 13:43:46 -05003320 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003321
3322 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3323 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3324 if (!fMipmapPrograms[progIdx].fProgram) {
3325 return false;
3326 }
3327
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003328 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3329 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003330 GrShaderVar::kUniform_TypeModifier);
3331 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3332 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003333 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003334 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003335 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3336 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3337 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3338 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003339 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003340 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003341
Ethan Nicholasfc994162019-06-06 10:04:27 -04003342 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003343 if (shaderCaps->noperspectiveInterpolationSupport()) {
3344 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003345 vshaderTxt.appendf("#extension %s : require\n", extension);
3346 }
3347 vTexCoords[0].addModifier("noperspective");
3348 vTexCoords[1].addModifier("noperspective");
3349 vTexCoords[2].addModifier("noperspective");
3350 vTexCoords[3].addModifier("noperspective");
3351 }
3352
Brian Salomon1edc5b92016-11-29 13:43:46 -05003353 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003354 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003355 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003356 vshaderTxt.append(";");
3357 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003358 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003359 vshaderTxt.append(";");
3360 }
3361
3362 vshaderTxt.append(
3363 "// Mipmap Program VS\n"
3364 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003365 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3366 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003367 );
3368
3369 // Insert texture coordinate computation:
3370 if (oddWidth && oddHeight) {
3371 vshaderTxt.append(
3372 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003373 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3374 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003375 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3376 );
3377 } else if (oddWidth) {
3378 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003379 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3380 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003381 );
3382 } else if (oddHeight) {
3383 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003384 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3385 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003386 );
3387 } else {
3388 vshaderTxt.append(
3389 " v_texCoord0 = a_vertex.xy;"
3390 );
3391 }
3392
3393 vshaderTxt.append("}");
3394
Ethan Nicholasfc994162019-06-06 10:04:27 -04003395 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003396 if (shaderCaps->noperspectiveInterpolationSupport()) {
3397 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003398 fshaderTxt.appendf("#extension %s : require\n", extension);
3399 }
3400 }
brianosman33f6b3f2016-06-02 05:49:21 -07003401 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003402 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003403 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003404 fshaderTxt.append(";");
3405 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003406 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003407 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003408 fshaderTxt.append(
3409 "// Mipmap Program FS\n"
3410 "void main() {"
3411 );
3412
3413 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003414 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003415 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3416 " sample(u_texture, v_texCoord1) + "
3417 " sample(u_texture, v_texCoord2) + "
3418 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003419 );
3420 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003421 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003422 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3423 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003424 );
3425 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003426 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003427 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003428 );
3429 }
3430
3431 fshaderTxt.append("}");
3432
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003433 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003434 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003435 SkSL::Program::Settings settings;
3436 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003437 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003438 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003439 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003440 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003441 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003442 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003443
Brian Osman6c431d52019-04-15 16:31:54 -04003444 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003445 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3446 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003447 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003448 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003449 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003450 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003451
3452 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3453
3454 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3455 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3456 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3457 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3458
3459 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3460
3461 GL_CALL(DeleteShader(vshader));
3462 GL_CALL(DeleteShader(fshader));
3463
3464 return true;
3465}
3466
Greg Daniel46cfbc62019-06-07 11:43:30 -04003467bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003468 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003469 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3470 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3471 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3472 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003473 return false;
3474 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003475 int progIdx = TextureToCopyProgramIdx(srcTex);
3476 if (!dstRT) {
3477 SkASSERT(dstTex);
3478 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3479 return false;
3480 }
3481 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003482 if (!fCopyPrograms[progIdx].fProgram) {
3483 if (!this->createCopyProgram(srcTex)) {
3484 SkDebugf("Failed to create copy program.\n");
3485 return false;
3486 }
3487 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003488 int w = srcRect.width();
3489 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003490 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003491 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003492 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003493 this->flushViewport(dst->width(), dst->height());
3494 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003495 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003496 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003497 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003498 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3499 attribs->enableVertexArrays(this, 1);
3500 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3501 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003502 // dst rect edges in NDC (-1 to 1)
3503 int dw = dst->width();
3504 int dh = dst->height();
3505 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3506 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3507 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3508 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003509 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3510 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3511 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3512 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3513 int sw = src->width();
3514 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003515 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3516 // src rect edges in normalized texture space (0 to 1)
3517 sx0 /= sw;
3518 sx1 /= sw;
3519 sy0 /= sh;
3520 sy1 /= sh;
3521 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003522 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3523 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3524 sx1 - sx0, sy1 - sy0, sx0, sy0));
3525 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003526 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003527 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003528 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003529 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003530 this->disableScissor();
3531 this->disableWindowRectangles();
3532 this->disableStencil();
3533 if (this->glCaps().srgbWriteControl()) {
3534 this->flushFramebufferSRGB(true);
3535 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003536 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003537 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003538 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3539 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003540 return true;
3541}
3542
Greg Daniel46cfbc62019-06-07 11:43:30 -04003543void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003544 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003545 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003546 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003547 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003548 SkASSERT(dstTex);
3549 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003550 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003551
Brian Salomond978b902019-02-07 15:09:18 -05003552 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003553 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003554 dstPoint.fX, dstPoint.fY,
3555 srcRect.fLeft, srcRect.fTop,
3556 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003557 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003558 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3559 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003560 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3561 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003562}
3563
Greg Daniel46cfbc62019-06-07 11:43:30 -04003564bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003565 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003566 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003567 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3568 srcRect.width(), srcRect.height());
3569 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003570 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003571 return false;
mtklein404b3b22015-05-18 09:29:10 -07003572 }
bsalomon5df6fee2015-05-18 06:26:15 -07003573 }
bsalomon6df86402015-06-01 10:41:49 -07003574
Brian Salomond2a8ae22019-09-10 16:03:59 -04003575 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3576 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003577 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003578 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003579
3580 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003581 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003582 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003583
Greg Daniel46cfbc62019-06-07 11:43:30 -04003584 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3585 srcRect.fTop,
3586 srcRect.fRight,
3587 srcRect.fBottom,
3588 dstRect.fLeft,
3589 dstRect.fTop,
3590 dstRect.fRight,
3591 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003592 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003593 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3594 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003595
3596 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3597 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003598 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003599}
3600
Brian Salomon930f9392018-06-20 16:25:26 -04003601bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3602 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003603 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003604 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003605 return false;
3606 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003607 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003608 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3609 // Uses draw calls to do a series of downsample operations to successive mips.
3610
3611 // The manual approach requires the ability to limit which level we're sampling and that the
3612 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003613 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003614 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003615 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003616 GL_CALL(GenerateMipmap(glTex->target()));
3617 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003618 }
3619
brianosman33f6b3f2016-06-02 05:49:21 -07003620 int width = texture->width();
3621 int height = texture->height();
3622 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003623 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003624
3625 // Create (if necessary), then bind temporary FBO:
3626 if (0 == fTempDstFBOID) {
3627 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3628 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003629 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003630 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003631
3632 // Bind the texture, to get things configured for filtering.
3633 // We'll be changing our base level further below:
3634 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003635 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003636 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003637
3638 // Vertex data:
3639 if (!fMipmapProgramArrayBuffer) {
3640 static const GrGLfloat vdata[] = {
3641 0, 0,
3642 0, 1,
3643 1, 0,
3644 1, 1
3645 };
Brian Salomonae64c192019-02-05 09:41:37 -05003646 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003647 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003648 }
3649 if (!fMipmapProgramArrayBuffer) {
3650 return false;
3651 }
3652
3653 fHWVertexArrayState.setVertexArrayID(this, 0);
3654
3655 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003656 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003657 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003658 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003659
3660 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003661 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003662 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003663 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003664 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003665 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003666
3667 // Do all the blits:
3668 width = texture->width();
3669 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003670
brianosman33f6b3f2016-06-02 05:49:21 -07003671 for (GrGLint level = 1; level < levelCount; ++level) {
3672 // Get and bind the program for this particular downsample (filter shape can vary):
3673 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3674 if (!fMipmapPrograms[progIdx].fProgram) {
3675 if (!this->createMipmapProgram(progIdx)) {
3676 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003677 // Invalidate all params to cover base level change in a previous iteration.
3678 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003679 return false;
3680 }
3681 }
Brian Salomon802cb312018-06-08 18:05:20 -04003682 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003683
3684 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3685 const float invWidth = 1.0f / width;
3686 const float invHeight = 1.0f / height;
3687 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3688 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3689 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3690
3691 // Only sample from previous mip
3692 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3693
Brian Salomon930f9392018-06-20 16:25:26 -04003694 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3695 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003696
3697 width = SkTMax(1, width / 2);
3698 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003699 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003700
3701 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3702 }
3703
3704 // Unbind:
3705 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3706 GR_GL_TEXTURE_2D, 0, 0));
3707
Brian Salomon930f9392018-06-20 16:25:26 -04003708 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003709 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3710 // We drew the 2nd to last level into the last level.
3711 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3712 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003713
brianosman33f6b3f2016-06-02 05:49:21 -07003714 return true;
3715}
3716
Chris Daltond7291ba2019-03-07 14:17:03 -07003717void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003718 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3719 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003720
3721 int effectiveSampleCnt;
3722 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003723 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003724
3725 sampleLocations->reset(effectiveSampleCnt);
3726 for (int i = 0; i < effectiveSampleCnt; ++i) {
3727 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3728 }
3729}
3730
cdalton231c5fd2015-05-13 12:35:36 -07003731void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003732 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003733 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003734 case kTexture_GrXferBarrierType: {
3735 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003736 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003737 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3738 // The render target uses separate storage so no need for glTextureBarrier.
3739 // FIXME: The render target will resolve automatically when its texture is bound,
3740 // but we could resolve only the bounds that will be read if we do it here instead.
3741 return;
3742 }
cdalton9954bc32015-04-29 14:17:00 -07003743 SkASSERT(this->caps()->textureBarrierSupport());
3744 GL_CALL(TextureBarrier());
3745 return;
cdalton231c5fd2015-05-13 12:35:36 -07003746 }
cdalton8917d622015-05-06 13:40:21 -07003747 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003748 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003749 this->caps()->blendEquationSupport());
3750 GL_CALL(BlendBarrier());
3751 return;
bsalomoncb02b382015-08-12 11:14:50 -07003752 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003753 }
3754}
3755
Chris Daltone1196c52019-12-28 14:31:09 -07003756void GrGLGpu::insertManualFramebufferBarrier() {
3757 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3758 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3759}
3760
Brian Salomon85c3d682019-11-04 15:04:54 -05003761GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003762 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003763 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003764 GrMipMapped mipMapped,
Robert Phillips4277f012020-01-21 14:28:34 -05003765 GrProtected isProtected,
3766 const BackendTextureData* data) {
Robert Phillips42716d42019-12-16 12:19:54 -05003767 // We don't support protected textures in GL.
3768 if (isProtected == GrProtected::kYes) {
3769 return {};
3770 }
3771
Brian Salomon8a375832018-03-14 10:21:40 -04003772 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003773
Brian Salomond4764a12019-08-08 12:08:24 -04003774 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003775 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003776 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003777 }
3778
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003779 int numMipLevels = 1;
3780 if (mipMapped == GrMipMapped::kYes) {
3781 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3782 }
3783
Robert Phillipsd34691b2019-09-24 13:38:43 -04003784 // Compressed formats go through onCreateCompressedBackendTexture
3785 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3786
Greg Daniel15500642019-06-27 03:05:55 +00003787 GrGLTextureInfo info;
3788 GrGLTextureParameters::SamplerOverriddenState initialState;
3789
Greg Danield51fa2f2020-01-22 16:53:38 -05003790 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003791 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003792 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003793
Robert Phillipsd34691b2019-09-24 13:38:43 -04003794 info.fTarget = GR_GL_TEXTURE_2D;
3795 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003796 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003797 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003798 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003799 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003800
Robert Phillips42716d42019-12-16 12:19:54 -05003801 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003802 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3803 SkTDArray<GrMipLevel> texels;
3804 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
Brian Salomon85c3d682019-11-04 15:04:54 -05003805 texels.append(numMipLevels);
3806 for (int i = 0; i < numMipLevels; ++i) {
3807 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3808 }
Greg Danield51fa2f2020-01-22 16:53:38 -05003809 if (!this->uploadTexData(glFormat, colorType, dimensions.width(), dimensions.height(),
3810 GR_GL_TEXTURE_2D, 0, 0, dimensions.width(), dimensions.height(),
3811 colorType, texels.begin(), texels.count())) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003812 GL_CALL(DeleteTextures(1, &info.fID));
3813 return {};
3814 }
3815 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3816 // TODO: Unify this with the clear texture code in onCreateTexture().
3817 GrColorType colorType;
3818 GrGLenum externalFormat, externalType;
3819 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3820 &externalType, &colorType);
3821 if (colorType == GrColorType::kUnknown) {
3822 GL_CALL(DeleteTextures(1, &info.fID));
3823 return {};
3824 }
3825
3826 // Make one tight image at the base size and reuse it for smaller levels.
3827 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3828 auto rb = ii.minRowBytes();
3829 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3830 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3831 GL_CALL(DeleteTextures(1, &info.fID));
3832 return {};
3833 }
3834
3835 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3836 SkISize levelDimensions = dimensions;
3837 for (int i = 0; i < numMipLevels; ++i) {
3838 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3839 levelDimensions.height(), externalFormat, externalType,
3840 pixelStorage.get()));
3841 levelDimensions = {SkTMax(1, levelDimensions.width() /2),
3842 SkTMax(1, levelDimensions.height()/2)};
3843 }
3844 }
3845 // Unbind this texture from the scratch texture unit.
3846 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003847
Brian Salomone2826ab2019-06-04 15:58:31 -04003848 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003849 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003850 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3851 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003852
Brian Salomon85c3d682019-11-04 15:04:54 -05003853 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3854 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003855}
3856
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003857void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003858 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3859
3860 GrGLTextureInfo info;
3861 if (tex.getGLTextureInfo(&info)) {
3862 GL_CALL(DeleteTextures(1, &info.fID));
3863 }
3864}
3865
3866#if GR_TEST_UTILS
3867
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003868bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003869 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003870
Greg Daniel52e16d92018-04-10 09:34:07 -04003871 GrGLTextureInfo info;
3872 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003873 return false;
3874 }
3875
3876 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003877 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003878
3879 return (GR_GL_TRUE == result);
3880}
3881
Brian Salomonf865b052018-03-09 09:01:53 -05003882GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003883 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003884 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3885 return GrBackendRenderTarget(); // invalid
3886 }
Brian Salomon8a375832018-03-14 10:21:40 -04003887 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003888 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003889 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003890 return {};
3891 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003892 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003893 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003894 if (sFormatIdx < 0) {
3895 return {};
3896 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003897 GrGLuint colorID = 0;
3898 GrGLuint stencilID = 0;
3899 auto deleteIDs = [&] {
3900 if (colorID) {
3901 if (useTexture) {
3902 GL_CALL(DeleteTextures(1, &colorID));
3903 } else {
3904 GL_CALL(DeleteRenderbuffers(1, &colorID));
3905 }
Brian Salomonf865b052018-03-09 09:01:53 -05003906 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003907 if (stencilID) {
3908 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003909 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003910 };
3911
3912 if (useTexture) {
3913 GL_CALL(GenTextures(1, &colorID));
3914 } else {
3915 GL_CALL(GenRenderbuffers(1, &colorID));
3916 }
3917 GL_CALL(GenRenderbuffers(1, &stencilID));
3918 if (!stencilID || !colorID) {
3919 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003920 return {};
3921 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003922
Brian Salomonf865b052018-03-09 09:01:53 -05003923 GrGLFramebufferInfo info;
3924 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003925 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003926 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3927 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003928 deleteIDs();
3929 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003930 }
3931
3932 this->invalidateBoundRenderTarget();
3933
Adrienne Walker4ee88512018-05-17 11:37:14 -07003934 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003935 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003936 GrGLTextureParameters::SamplerOverriddenState initialState;
3937 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3938 if (!colorID) {
3939 deleteIDs();
3940 return {};
3941 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003942 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3943 colorID, 0));
3944 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003945 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003946 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3947 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003948 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003949 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3950 GR_GL_RENDERBUFFER, colorID));
3951 }
3952 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003953 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3954 GL_ALLOC_CALL(this->glInterface(),
3955 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3956 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003957 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003958 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3959 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003960 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003961 }
3962
Brian Salomon93348dd2018-08-29 12:56:23 -04003963 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3964 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3965 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3966 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003967 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003968 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003969
Adrienne Walker4ee88512018-05-17 11:37:14 -07003970 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003971 GrGLenum status;
3972 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3973 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003974 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003975 return {};
3976 }
3977 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003978
Greg Daniel108bb232018-07-03 16:18:29 -04003979 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003980 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003981 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003982}
3983
3984void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003985 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003986 GrGLFramebufferInfo info;
3987 if (backendRT.getGLFramebufferInfo(&info)) {
3988 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003989 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003990 }
3991 }
joshualitt8fd844f2015-12-02 13:36:47 -08003992}
3993
Greg Daniel26b50a42018-03-08 09:49:58 -05003994void GrGLGpu::testingOnly_flushGpuAndSync() {
3995 GL_CALL(Finish());
3996}
Brian Salomonf865b052018-03-09 09:01:53 -05003997#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003998
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003999///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004000
cdaltone2e71c22016-04-07 18:13:29 -07004001GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004002 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004003 GrGLAttribArrayState* attribState;
4004
cdaltone2e71c22016-04-07 18:13:29 -07004005 if (gpu->glCaps().isCoreProfile()) {
4006 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004007 GrGLuint arrayID;
4008 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4009 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004010 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004011 }
cdaltone2e71c22016-04-07 18:13:29 -07004012 if (ibuf) {
4013 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004014 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004015 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004016 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004017 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004018 if (ibuf) {
4019 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004020 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004021 } else {
4022 this->setVertexArrayID(gpu, 0);
4023 }
4024 int attrCount = gpu->glCaps().maxVertexAttributes();
4025 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4026 fDefaultVertexArrayAttribState.resize(attrCount);
4027 }
4028 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004029 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004030 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004031}
bsalomone179a912016-01-20 06:18:10 -08004032
Greg Daniel30a35e82019-11-19 14:12:25 -05004033bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004034 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004035 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004036 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004037 // We call finish if the client told us to sync or if we have a finished proc but don't support
4038 // GLsync objects.
4039 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4040 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4041 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004042 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004043 // After a finish everything previously sent to GL is done.
4044 for (const auto& cb : fFinishCallbacks) {
4045 cb.fCallback(cb.fContext);
4046 this->deleteSync(cb.fSync);
4047 }
4048 fFinishCallbacks.clear();
4049 if (info.fFinishedProc) {
4050 info.fFinishedProc(info.fFinishedContext);
4051 }
4052 } else {
4053 if (info.fFinishedProc) {
4054 FinishCallback callback;
4055 callback.fCallback = info.fFinishedProc;
4056 callback.fContext = info.fFinishedContext;
4057 callback.fSync = (GrGLsync)this->insertFence();
4058 fFinishCallbacks.push_back(callback);
4059 GL_CALL(Flush());
4060 } else if (insertedSemaphore) {
4061 // Must call flush after semaphores in case they are waited on another GL context.
4062 GL_CALL(Flush());
4063 }
4064 // See if any previously inserted finish procs are good to go.
4065 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004066 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004067 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004068}
4069
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004070void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4071 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4072 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4073 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004074}
4075
Greg Daniel6be35232017-03-01 17:01:09 -05004076GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004077 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004078 GrGLsync sync;
4079 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Brian Salomon4dea72a2019-12-18 10:43:10 -05004080 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004081 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004082}
4083
Brian Salomonb0d8b762019-05-06 16:58:22 -04004084bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4085 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004086 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004087 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4088 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4089}
4090
4091bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4092 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004093}
4094
4095void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004096 this->deleteSync((GrGLsync)fence);
4097}
4098
Greg Daniel301015c2019-11-18 14:06:46 -05004099std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004100 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004101 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004102}
4103
Greg Daniel301015c2019-11-18 14:06:46 -05004104std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4105 const GrBackendSemaphore& semaphore,
4106 GrResourceProvider::SemaphoreWrapType wrapType,
4107 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004108 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004109 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4110}
4111
Greg Daniel301015c2019-11-18 14:06:46 -05004112void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4113 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004114
Greg Daniel48661b82018-01-22 16:11:35 -05004115 GrGLsync sync;
4116 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4117 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004118}
4119
Greg Daniel301015c2019-11-18 14:06:46 -05004120void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4121 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004122
4123 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4124}
4125
Brian Salomonb0d8b762019-05-06 16:58:22 -04004126void GrGLGpu::checkFinishProcs() {
4127 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4128 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4129 /* timeout = */ 0, /* flush = */ false)) {
4130 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4131 this->deleteSync(fFinishCallbacks.front().fSync);
4132 fFinishCallbacks.pop_front();
4133 }
4134}
4135
Greg Daniel6be35232017-03-01 17:01:09 -05004136void GrGLGpu::deleteSync(GrGLsync sync) const {
4137 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004138}
Brian Osman13dddce2017-05-09 13:19:50 -04004139
Greg Daniel301015c2019-11-18 14:06:46 -05004140std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004141 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004142 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004143 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004144 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004145 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4146 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004147
4148 return semaphore;
4149}
Robert Phillips646e4292017-06-13 12:44:56 -04004150
4151int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004152 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004153 case kTexture2DSampler_GrSLType:
4154 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004155 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004156 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004157 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004158 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004159 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004160 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004161 }
4162}
Brian Osman71a18892017-08-10 10:23:25 -04004163
Kevin Lubickf4def342018-10-04 12:52:50 -04004164#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004165#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004166void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4167 // We are called by the base class, which has already called beginObject(). We choose to nest
4168 // all of our caps information in a named sub-object.
4169 writer->beginObject("GL GPU");
4170
4171 const GrGLubyte* str;
4172 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4173 writer->appendString("GL_VERSION", (const char*)(str));
4174 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4175 writer->appendString("GL_RENDERER", (const char*)(str));
4176 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4177 writer->appendString("GL_VENDOR", (const char*)(str));
4178 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4179 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4180
4181 writer->appendName("extensions");
4182 glInterface()->fExtensions.dumpJSON(writer);
4183
4184 writer->endObject();
4185}
Kevin Lubickf4def342018-10-04 12:52:50 -04004186#endif