blob: 2cb182c03d06a7be44fac41c82a7f3b4c645ebb5 [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;
cdaltone2e71c22016-04-07 18:13:29 -0700395 if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500396 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
397 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
398 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
399 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700400 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500401 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
402 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700403 }
Brian Salomonae64c192019-02-05 09:41:37 -0500404 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400405 fHWBufferState[i].invalidate();
406 }
Brian 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
686 desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType);
687 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
688
Brian Salomond17f6582017-07-19 18:28:58 -0400689 return true;
690}
691
692sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400693 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400694 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400695 GrGLTexture::Desc desc;
696 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400697 return nullptr;
698 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400699
Brian Salomond17f6582017-07-19 18:28:58 -0400700 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400701 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400702 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400703 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400704 }
bsalomone5286e02016-01-14 09:24:09 -0800705
Greg Daniel177e6952017-10-12 12:27:11 -0400706 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
707 : GrMipMapsStatus::kNotAllocated;
708
Brian Salomonea4ad302019-08-07 13:04:55 -0400709 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400710 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400711 // We don't know what parameters are already set on wrapped textures.
712 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400713 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400714}
715
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500716static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
717 const GrGLCaps& caps, GrGLTexture::Desc* desc,
718 bool skipRectTexSupportCheck = false) {
719 GrGLTextureInfo info;
720 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
721 return false;
722 }
723
724 desc->fSize = {backendTex.width(), backendTex.height()};
725 desc->fTarget = info.fTarget;
726 desc->fID = info.fID;
727 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
728
729 if (desc->fFormat == GrGLFormat::kUnknown) {
730 return false;
731 }
732
733 if (GR_GL_TEXTURE_2D != desc->fTarget) {
734 return false;
735 }
736 if (backendTex.isProtected()) {
737 // Not supported in GL backend at this time.
738 return false;
739 }
740
741 desc->fConfig = caps.getConfigFromCompressedBackendFormat(backendTex.getBackendFormat());
742 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
743
744 return true;
745}
746
Robert Phillipsb915c942019-12-17 14:44:37 -0500747sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
748 GrWrapOwnership ownership,
749 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500750 GrGLTexture::Desc desc;
751 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
752 return nullptr;
753 }
754
755 if (kBorrow_GrWrapOwnership == ownership) {
756 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
757 } else {
758 desc.fOwnership = GrBackendObjectOwnership::kOwned;
759 }
760
761 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
762 : GrMipMapsStatus::kNotAllocated;
763
764 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
765 backendTex.getGLTextureParams(), cacheable,
766 kRead_GrIOType);
767 // We don't know what parameters are already set on wrapped textures.
768 texture->textureParamsModified();
769 return texture;
Robert Phillipsb915c942019-12-17 14:44:37 -0500770}
771
Brian Salomond17f6582017-07-19 18:28:58 -0400772sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400773 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400774 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500775 GrWrapOwnership ownership,
776 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400777 const GrGLCaps& caps = this->glCaps();
778
Brian Salomonea4ad302019-08-07 13:04:55 -0400779 GrGLTexture::Desc desc;
780 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800781 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800782 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400783 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
784 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800785
Brian Salomond17f6582017-07-19 18:28:58 -0400786 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400787 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800788 return nullptr;
789 }
bsalomon10528f12015-10-14 12:54:52 -0700790
Brian Osman766fcbb2017-03-13 09:33:09 -0400791 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400792 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400793 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400794 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800795 }
bsalomonb15b4c12014-10-29 12:41:57 -0700796
Robert Phillips0902c982019-07-16 07:47:56 -0400797
Greg Daniel6fa62e22019-08-07 15:52:37 -0400798 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
799 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000800
Brian Salomonea4ad302019-08-07 13:04:55 -0400801 GrGLRenderTarget::IDs rtIDs;
802 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400803 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000804 }
Greg Daniel177e6952017-10-12 12:27:11 -0400805
806 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
807 : GrMipMapsStatus::kNotAllocated;
808
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500809 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400810 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400811 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400812 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400813 // We don't know what parameters are already set on wrapped textures.
814 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400815 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000816}
817
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400818sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
819 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400820 GrGLFramebufferInfo info;
821 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000822 return nullptr;
823 }
824
Brian Salomone8a766b2019-07-19 14:24:36 -0400825 if (backendRT.isProtected()) {
826 // Not supported in GL at this time.
827 return nullptr;
828 }
829
Brian Salomond4764a12019-08-08 12:08:24 -0400830 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400831 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
832 return nullptr;
833 }
834
Brian Salomonea4ad302019-08-07 13:04:55 -0400835 GrGLRenderTarget::IDs rtIDs;
836 rtIDs.fRTFBOID = info.fFBOID;
837 rtIDs.fMSColorRenderbufferID = 0;
838 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
839 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000840
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400841 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
842 grColorType);
Brian Salomond4764a12019-08-08 12:08:24 -0400843 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400844
Brian Salomonea4ad302019-08-07 13:04:55 -0400845 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400846 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700847
Brian Salomonea4ad302019-08-07 13:04:55 -0400848 return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400849 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000850}
851
Greg Daniel7ef28f32017-04-20 16:41:55 +0000852sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400853 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400854 GrColorType colorType) {
855 GrGLTexture::Desc desc;
856 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
857 // with a texture rectangle,we assume the necessary support exists.
858 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800859 return nullptr;
860 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400861
862 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
863 return nullptr;
864 }
865
866 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400867 GrGLRenderTarget::IDs rtIDs;
868 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800869 return nullptr;
870 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400871 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount,
872 rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800873}
874
Brian Salomonc320b152018-02-20 14:05:36 -0500875static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700876 if (!glTex) {
877 return false;
878 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000879
bsalomone5286e02016-01-14 09:24:09 -0800880 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
881 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800882 return false;
883 }
884
jvanverth17aa0472016-01-05 10:41:27 -0800885 return true;
886}
887
Brian Salomona9b04b92018-06-01 15:04:28 -0400888bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400889 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400890 const GrMipLevel texels[], int mipLevelCount,
891 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500892 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800893
Brian Salomonc320b152018-02-20 14:05:36 -0500894 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800895 return false;
896 }
897
Brian Salomond978b902019-02-07 15:09:18 -0500898 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000899
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400900 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400901 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400902 glTex->target(), left, top, width, height, srcColorType, texels,
903 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000904}
905
Brian Salomone05ba5a2019-04-08 11:59:07 -0400906bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400907 GrColorType textureColorType, GrColorType bufferColorType,
908 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400909 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400910
Jim Van Verth1676cb92019-01-15 13:24:45 -0500911 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400912 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500913
Brian Salomonc320b152018-02-20 14:05:36 -0500914 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400915 return false;
916 }
917
Hal Canaryc36f7e72018-06-18 15:50:09 -0400918 static_assert(sizeof(int) == sizeof(int32_t), "");
919 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400920 return false;
921 }
922
Brian Salomond978b902019-02-07 15:09:18 -0500923 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400924
925 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500926 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400927 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500928 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400929
Greg Daniel660cc992017-06-26 14:55:05 -0400930 SkDEBUGCODE(
931 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
932 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
933 SkASSERT(bounds.contains(subRect));
934 )
935
Brian Salomonb28cb682019-07-26 12:48:47 -0400936 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400937 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400938 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700939 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400940 return false;
941 }
942
943 bool restoreGLRowLength = false;
944 if (trimRowBytes != rowBytes) {
945 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400946 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400947 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
948 restoreGLRowLength = true;
949 }
950
Greg Danielba88ab62019-07-26 09:14:01 -0400951 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400952 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400953 GrGLenum externalFormat = 0;
954 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400955 this->glCaps().getTexSubImageExternalFormatAndType(
956 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400957 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400958 return false;
959 }
960
Brian Salomon8cbf6622019-07-30 11:03:14 -0400961 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400962 GL_CALL(TexSubImage2D(glTex->target(),
963 0,
964 left, top,
965 width,
966 height,
967 externalFormat, externalType,
968 pixels));
969
970 if (restoreGLRowLength) {
971 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
972 }
973
974 return true;
975}
976
Brian Salomon26de56e2019-04-10 12:14:26 -0400977bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400978 GrColorType surfaceColorType, GrColorType dstColorType,
979 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400980 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
981 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400982 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400983 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
984 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400985}
986
Brian Osman9b560242017-09-05 15:34:52 -0400987void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500988 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
989 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
990 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
991 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400992 }
Brian Osman9b560242017-09-05 15:34:52 -0400993}
994
Brian Salomond2a8ae22019-09-10 16:03:59 -0400995bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
996 int texHeight, GrGLenum target, int left, int top, int width,
997 int height, GrColorType srcColorType, const GrMipLevel texels[],
998 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500999 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -04001000 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001001
Greg Daniel7bfc9132019-08-14 14:23:53 -04001002 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -04001003 SkDEBUGCODE(
1004 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1005 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1006 SkASSERT(bounds.contains(subRect));
1007 )
Robert Phillips590533f2017-07-11 14:22:35 -04001008 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001009 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001010
Brian Osman9b560242017-09-05 15:34:52 -04001011 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001012
cblume55f2d2d2016-02-26 13:20:48 -08001013 const GrGLInterface* interface = this->glInterface();
1014 const GrGLCaps& caps = this->glCaps();
1015
Brian Salomonf77c1462019-08-01 15:19:29 -04001016 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001017
1018 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001019 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001020 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001021
bsalomon5b30c6f2015-12-17 14:17:34 -08001022 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001023 GrGLenum externalFormat;
1024 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001025 this->glCaps().getTexSubImageExternalFormatAndType(
1026 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001027 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001028 return false;
1029 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001030
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001031 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001032 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001033 * because our srcData has extra bytes past each row. If so, we need
1034 * to trim those off here, since GL ES may not let us specify
1035 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001036 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001037 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001038
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001039 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001040 *mipMapsStatus = (mipLevelCount > 1) ?
1041 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001042 }
bsalomone699d0c2016-03-09 06:25:15 -08001043
Brian Salomond2a8ae22019-09-10 16:03:59 -04001044 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001045
Brian Salomond2a8ae22019-09-10 16:03:59 -04001046 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1047 if (!texels[currentMipLevel].fPixels) {
1048 if (mipMapsStatus) {
1049 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001050 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001051 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001052 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001053 int twoToTheMipLevel = 1 << currentMipLevel;
1054 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1055 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1056 const size_t trimRowBytes = currentWidth * bpp;
1057 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1058
1059 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1060 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1061 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1062 restoreGLRowLength = true;
1063 }
1064
1065 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1066 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001067 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001068 if (restoreGLRowLength) {
1069 SkASSERT(caps.writePixelsRowBytesSupport());
1070 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1071 }
1072 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001073}
1074
Greg Daniel7bfc9132019-08-14 14:23:53 -04001075bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001076 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001077 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001078 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001079 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001080 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001081 const GrGLCaps& caps = this->glCaps();
1082
1083 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001084 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001085 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001086 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001087 }
1088
Robert Phillips9f744f72019-12-19 19:14:33 -05001089 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1090 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1091
Greg Daniel7bfc9132019-08-14 14:23:53 -04001092 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001093
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001094 int numMipLevels = 1;
1095 if (mipMapped == GrMipMapped::kYes) {
1096 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1097 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001098
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001100 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001101
1102 if (useTexStorage) {
1103 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001104 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001105 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001106 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001107 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1108 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001109 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001110 }
Robert Phillips42716d42019-12-16 12:19:54 -05001111
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001112 size_t offset = 0;
1113 for (int level = 0; level < numMipLevels; ++level) {
1114
Robert Phillips9f744f72019-12-19 19:14:33 -05001115 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1116 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001117
1118 GL_CALL(CompressedTexSubImage2D(target,
1119 level,
1120 0, // left
1121 0, // top
1122 dimensions.width(),
1123 dimensions.height(),
1124 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001125 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001126 &((char*)data)[offset]));
1127
1128 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1129 if (error != GR_GL_NO_ERROR) {
1130 return false;
1131 }
1132
Robert Phillips9f744f72019-12-19 19:14:33 -05001133 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001134 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001135 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001136 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001137 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001138
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001139 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001140 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1141 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001142
1143 const char* rawLevelData = &((char*)data)[offset];
1144 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1145 level,
1146 internalFormat,
1147 dimensions.width(),
1148 dimensions.height(),
1149 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001150 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001151 rawLevelData));
1152
1153 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1154 if (error != GR_GL_NO_ERROR) {
1155 return false;
1156 }
1157
Robert Phillips9f744f72019-12-19 19:14:33 -05001158 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001159 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001160 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001161 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001162 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001163}
1164
bsalomon424cc262015-05-22 10:37:30 -07001165static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001166 int sampleCount,
1167 GrGLenum format,
1168 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001169 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001170 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001171 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001172 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001173 GL_ALLOC_CALL(ctx.interface(),
1174 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1175 sampleCount,
1176 format,
1177 width, height));
1178 break;
1179 case GrGLCaps::kES_Apple_MSFBOType:
1180 GL_ALLOC_CALL(ctx.interface(),
1181 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1182 sampleCount,
1183 format,
1184 width, height));
1185 break;
1186 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1187 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1188 GL_ALLOC_CALL(ctx.interface(),
1189 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1190 sampleCount,
1191 format,
1192 width, height));
1193 break;
1194 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001195 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001196 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001197 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001198 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001199}
1200
Brian Salomonea4ad302019-08-07 13:04:55 -04001201bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001202 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001203 GrGLRenderTarget::IDs* rtIDs) {
1204 rtIDs->fMSColorRenderbufferID = 0;
1205 rtIDs->fRTFBOID = 0;
1206 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1207 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208
bsalomona11e5fc2015-12-18 07:59:41 -08001209 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001210
Brian Salomonea4ad302019-08-07 13:04:55 -04001211 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001212 goto FAILED;
1213 }
1214
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001215 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001216 goto FAILED;
1217 }
1218
Brian Salomonea4ad302019-08-07 13:04:55 -04001219 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1220 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221 goto FAILED;
1222 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001223
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001224 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1225 // the texture bound to the other. The exception is the IMG multisample extension. With this
1226 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1227 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001228 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001229 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1230 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1231 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 goto FAILED;
1233 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001234 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001236 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 }
1238
egdanield803f272015-03-18 13:01:52 -07001239 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001240 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001242 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001243 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001244 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001245 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 goto FAILED;
1247 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001248 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001249 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001250 GR_GL_COLOR_ATTACHMENT0,
1251 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001252 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001254 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001255
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001256 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001257 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1258 GR_GL_COLOR_ATTACHMENT0,
1259 desc.fTarget,
1260 desc.fID,
1261 0,
1262 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001263 } else {
egdanield803f272015-03-18 13:01:52 -07001264 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001265 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 desc.fTarget,
1267 desc.fID,
1268 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001269 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001270
1271 return true;
1272
1273FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001274 if (rtIDs->fMSColorRenderbufferID) {
1275 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001276 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001277 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1278 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001279 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001280 if (rtIDs->fTexFBOID) {
1281 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001282 }
1283 return false;
1284}
1285
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001286// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001287static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001288// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001289 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001290}
1291
Brian Salomone2826ab2019-06-04 15:58:31 -04001292static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001293 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001294 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1295 // drivers have a bug where an FBO won't be complete if it includes a
1296 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001297 GrGLTextureParameters::SamplerOverriddenState state;
1298 state.fMinFilter = GR_GL_NEAREST;
1299 state.fMagFilter = GR_GL_NEAREST;
1300 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1301 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001302 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1303 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1304 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1305 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001306 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001307}
1308
Robert Phillips67d52cf2017-06-05 13:38:13 -04001309sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001310 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001311 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001312 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001313 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001314 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001315 int mipLevelCount,
1316 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001317 // We don't support protected textures in GL.
1318 if (isProtected == GrProtected::kYes) {
1319 return nullptr;
1320 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001321 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001322
Brian Salomond2a8ae22019-09-10 16:03:59 -04001323 SkASSERT(mipLevelCount > 0);
1324 GrMipMapsStatus mipMapsStatus =
1325 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001326 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001327 GrGLTexture::Desc texDesc;
1328 texDesc.fSize = {desc.fWidth, desc.fHeight};
1329 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001330 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001331 texDesc.fConfig = desc.fConfig;
1332 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001333 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1334 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001335
Brian Salomond2a8ae22019-09-10 16:03:59 -04001336 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1337 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001338
1339 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001340 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001341 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001342
Robert Phillips67d52cf2017-06-05 13:38:13 -04001343 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001344 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001345 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001346 GL_CALL(BindTexture(texDesc.fTarget, 0));
1347 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001348
Brian Salomonea4ad302019-08-07 13:04:55 -04001349 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1350 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 return return_null_texture();
1352 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001353 tex = sk_make_sp<GrGLTextureRenderTarget>(
1354 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001355 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001356 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001357 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001359 // The non-sampler params are still at their default values.
1360 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1361 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001362 if (levelClearMask) {
1363 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001364 GrColorType colorType;
1365 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1366 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001367 if (this->glCaps().clearTextureSupport()) {
1368 for (int i = 0; i < mipLevelCount; ++i) {
1369 if (levelClearMask & (1U << i)) {
1370 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1371 nullptr));
1372 }
1373 }
1374 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1375 !this->glCaps().performColorClearsAsDraws()) {
1376 this->disableScissor();
1377 this->disableWindowRectangles();
1378 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001379 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001380 for (int i = 0; i < mipLevelCount; ++i) {
1381 if (levelClearMask & (1U << i)) {
1382 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1383 kDst_TempFBOTarget);
1384 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1385 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1386 }
1387 }
Brian Salomonc2249852019-09-16 11:08:50 -04001388 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001389 } else {
1390 std::unique_ptr<char[]> zeros;
1391 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1392 for (int i = 0; i < mipLevelCount; ++i) {
1393 if (levelClearMask & (1U << i)) {
1394 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1395 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1396 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1397 // smaller levels that need clearing.
1398 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001399 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001400 size_t size = levelWidth * levelHeight * bpp;
1401 zeros.reset(new char[size]());
1402 }
1403 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1404 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1405 externalFormat, externalType, zeros.get()));
1406 }
Brian Salomona3e29962019-07-16 11:52:08 -04001407 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001408 }
1409 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001410 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001411}
1412
Robert Phillips9f744f72019-12-19 19:14:33 -05001413sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001414 const GrBackendFormat& format,
Robert Phillips9f744f72019-12-19 19:14:33 -05001415 SkBudgeted budgeted,
1416 const void* data, size_t dataSize) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001417 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001418 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001419 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001420 desc.fTarget = GR_GL_TEXTURE_2D;
Robert Phillips9f744f72019-12-19 19:14:33 -05001421 desc.fConfig = this->glCaps().getConfigFromCompressedBackendFormat(format);
Brian Salomonea4ad302019-08-07 13:04:55 -04001422 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001423 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001424 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001425 GrMipMapped::kNo, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001426 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001427 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001428 return nullptr;
1429 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001430
Robert Phillipsee946932019-12-18 11:16:17 -05001431 // Unbind this texture from the scratch texture unit.
1432 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1433
Brian Salomonea4ad302019-08-07 13:04:55 -04001434 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001435 // The non-sampler params are still at their default values.
1436 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1437 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001438 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001439}
1440
Robert Phillipsb915c942019-12-17 14:44:37 -05001441GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1442 const GrBackendFormat& format,
1443 const BackendTextureData* data,
1444 GrMipMapped mipMapped,
1445 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001446 // We don't support protected textures in GL.
1447 if (isProtected == GrProtected::kYes) {
1448 return {};
1449 }
1450
1451 this->handleDirtyContext();
1452
1453 GrGLFormat glFormat = format.asGLFormat();
1454 if (glFormat == GrGLFormat::kUnknown) {
1455 return {};
1456 }
1457
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001458 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001459 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001460 SkAutoMalloc am;
1461
1462 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1463 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1464 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001465 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001466 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001467 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1468 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001469
Robert Phillips9f744f72019-12-19 19:14:33 -05001470 rawDataSize = GrCompressedDataSize(compression, dimensions, nullptr, mipMapped);
1471
1472 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001473
1474 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1475
1476 rawData = (const char*) am.get();
1477 }
1478
1479 GrGLTextureInfo info;
1480 GrGLTextureParameters::SamplerOverriddenState initialState;
1481
1482 info.fTarget = GR_GL_TEXTURE_2D;
1483 info.fFormat = GrGLFormatToEnum(glFormat);
1484 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001485 mipMapped, &initialState,
1486 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001487 if (!info.fID) {
1488 return {};
1489 }
1490
1491 // Unbind this texture from the scratch texture unit.
1492 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1493
1494 auto parameters = sk_make_sp<GrGLTextureParameters>();
1495 // The non-sampler params are still at their default values.
1496 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1497 fResetTimestampForTextureParameters);
1498
1499 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1500 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001501}
1502
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001503namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001504
egdaniel8dc7c3a2015-04-16 11:22:42 -07001505const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001506
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001507void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001508 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001509
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001510 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001511 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001512 (kUnknownBitCount == format->fTotalBits));
1513 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001514 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001515 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1516 (GrGLint*)&format->fStencilBits);
1517 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001518 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001519 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1520 (GrGLint*)&format->fTotalBits);
1521 format->fTotalBits += format->fStencilBits;
1522 } else {
1523 format->fTotalBits = format->fStencilBits;
1524 }
1525 }
1526}
1527}
1528
Brian Salomon5043f1f2019-07-11 21:27:54 -04001529int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001530 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001531 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1532
1533 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001534 // Default to unsupported, set this if we find a stencil format that works.
1535 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001536
Brian Salomond2a8ae22019-09-10 16:03:59 -04001537 GrGLuint colorID =
1538 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1539 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001540 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001541 }
egdanielff1d5472015-09-10 08:37:20 -07001542 // unbind the texture from the texture unit before binding it to the frame buffer
1543 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1544
1545 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001546 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001547 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001548 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001549 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001550 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1551 GR_GL_COLOR_ATTACHMENT0,
1552 GR_GL_TEXTURE_2D,
1553 colorID,
1554 0));
bsalomon30447372015-12-21 09:03:05 -08001555 GrGLuint sbRBID = 0;
1556 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001557
1558 // look over formats till I find a compatible one
1559 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001560 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001561 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001562 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1563 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1564 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1565 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1566 sFmt.fInternalFormat,
1567 kSize, kSize));
1568 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001570 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001571 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001572 if (sFmt.fPacked) {
1573 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1574 GR_GL_DEPTH_ATTACHMENT,
1575 GR_GL_RENDERBUFFER, sbRBID));
1576 } else {
1577 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1578 GR_GL_DEPTH_ATTACHMENT,
1579 GR_GL_RENDERBUFFER, 0));
1580 }
1581 GrGLenum status;
1582 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1583 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1584 firstWorkingStencilFormatIndex = i;
1585 break;
1586 }
egdanielff1d5472015-09-10 08:37:20 -07001587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1588 GR_GL_STENCIL_ATTACHMENT,
1589 GR_GL_RENDERBUFFER, 0));
1590 if (sFmt.fPacked) {
1591 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1592 GR_GL_DEPTH_ATTACHMENT,
1593 GR_GL_RENDERBUFFER, 0));
1594 }
egdanielff1d5472015-09-10 08:37:20 -07001595 }
1596 }
bsalomon30447372015-12-21 09:03:05 -08001597 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001598 }
1599 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001600 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1601 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001602 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001603 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001604 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001605}
1606
Brian Salomonea4ad302019-08-07 13:04:55 -04001607GrGLuint GrGLGpu::createCompressedTexture2D(
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001608 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001609 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001610 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001611 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001612 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001613 if (format == GrGLFormat::kUnknown) {
1614 return 0;
1615 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001616 GrGLuint id = 0;
1617 GL_CALL(GenTextures(1, &id));
1618 if (!id) {
1619 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001620 }
1621
Brian Salomonea4ad302019-08-07 13:04:55 -04001622 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001623
Brian Salomonea4ad302019-08-07 13:04:55 -04001624 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1625
Robert Phillips42716d42019-12-16 12:19:54 -05001626 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001627 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1628 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001629 GL_CALL(DeleteTextures(1, &id));
1630 return 0;
1631 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001632 }
Robert Phillips42716d42019-12-16 12:19:54 -05001633
Brian Salomonea4ad302019-08-07 13:04:55 -04001634 return id;
1635}
1636
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001637GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001638 GrGLFormat format,
1639 GrRenderable renderable,
1640 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001641 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001642 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001643 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001644
Brian Salomonea4ad302019-08-07 13:04:55 -04001645 GrGLuint id = 0;
1646 GL_CALL(GenTextures(1, &id));
1647
1648 if (!id) {
1649 return 0;
1650 }
1651
1652 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001653
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001654 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001655 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001656 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001657 }
1658
Brian Salomond2a8ae22019-09-10 16:03:59 -04001659 if (initialState) {
1660 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1661 } else {
1662 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001663 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001664
1665 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1666
1667 bool success = false;
1668 if (internalFormat) {
1669 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1670 if (this->glCaps().formatSupportsTexStorage(format)) {
1671 GL_ALLOC_CALL(this->glInterface(),
1672 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001673 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001674 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1675 } else {
1676 GrGLenum externalFormat, externalType;
1677 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1678 GrGLenum error = GR_GL_NO_ERROR;
1679 if (externalFormat && externalType) {
1680 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1681 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001682 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1683 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001684 GL_ALLOC_CALL(
1685 this->glInterface(),
1686 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1687 currentHeight, 0, externalFormat, externalType, nullptr));
1688 error = CHECK_ALLOC_ERROR(this->glInterface());
1689 }
1690 success = (GR_GL_NO_ERROR == error);
1691 }
1692 }
1693 }
1694 if (success) {
1695 return id;
1696 }
1697 GL_CALL(DeleteTextures(1, &id));
1698 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001699}
1700
Chris Daltoneffee202019-07-01 22:28:03 -06001701GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1702 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001703 SkASSERT(width >= rt->width());
1704 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001705
egdaniel8dc7c3a2015-04-16 11:22:42 -07001706 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001707
Brian Salomond4764a12019-08-08 12:08:24 -04001708 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001709 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001710 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001711 }
egdanielff1d5472015-09-10 08:37:20 -07001712
1713 if (!sbDesc.fRenderbufferID) {
1714 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1715 }
1716 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001717 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001718 }
1719 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1720 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1721 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1722 // we do this "if" so that we don't call the multisample
1723 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001724 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001725 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001726 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001727 sFmt.fInternalFormat,
1728 width, height));
1729 } else {
1730 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1731 sFmt.fInternalFormat,
1732 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001733 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001734 }
1735 fStats.incStencilAttachmentCreates();
1736 // After sized formats we attempt an unsized format and take
1737 // whatever sizes GL gives us. In that case we query for the size.
1738 GrGLStencilAttachment::Format format = sFmt;
1739 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001740 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1741 sbDesc,
1742 width,
1743 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001744 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001745 format);
1746 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001747}
1748
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001749////////////////////////////////////////////////////////////////////////////////
1750
Brian Salomondbf70722019-02-07 11:31:24 -05001751sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1752 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001753 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001754}
1755
Greg Danielacd66b42019-05-22 16:29:12 -04001756void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001757 GrSurfaceOrigin rtOrigin) {
1758 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001759 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001760 // if the scissor fully contains the viewport then we fall through and
1761 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001762 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001763 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001764 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001765 fHWScissorSettings.fRect = scissor;
1766 }
1767 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1768 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1769 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001770 }
1771 return;
1772 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001774
1775 // See fall through note above
1776 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001777}
1778
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001780 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001781#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001782 typedef GrWindowRectsState::Mode Mode;
1783 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1784 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001785
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001786 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001787 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001788 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001789 }
1790
csmartdalton7535f412016-08-23 06:51:00 -07001791 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1792 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1794 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001795
Chris Daltond6cda8d2019-09-05 02:30:04 -06001796 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001798 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001799 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001800 }
1801
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001802 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001803 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001804
Greg Danielacd66b42019-05-22 16:29:12 -04001805 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001806#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001807}
1808
1809void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001810#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001811 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001812 return;
1813 }
1814 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001815 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001816#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001817}
1818
Robert Phillipsfcaae482019-11-07 10:17:03 -05001819bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001820
Robert Phillipsfcaae482019-11-07 10:17:03 -05001821 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001822 if (!program) {
1823 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1824 return false;
1825 }
brianosman33f6b3f2016-06-02 05:49:21 -07001826
Brian Salomon802cb312018-06-08 18:05:20 -04001827 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001828
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001829 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001830 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1831 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001832
Robert Phillips901aff02019-10-08 12:32:56 -04001833 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001834
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001835 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001836 GrStencilSettings stencil;
1837 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001838 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1839 stencil.reset(*programInfo.pipeline().getUserStencil(),
1840 programInfo.pipeline().hasStencilClip(),
1841 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001842 }
Robert Phillips901aff02019-10-08 12:32:56 -04001843 this->flushStencil(stencil, programInfo.origin());
1844 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001845 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001846 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1847 : kBogusScissor);
1848 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001849 } else {
1850 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001851 }
Robert Phillips901aff02019-10-08 12:32:56 -04001852 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1853 glRT, programInfo.origin());
1854 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001855 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001856 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001857
1858 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001859 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001860 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001861
1862 return true;
1863}
1864
Brian Salomon802cb312018-06-08 18:05:20 -04001865void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1866 if (!program) {
1867 fHWProgram.reset();
1868 fHWProgramID = 0;
1869 return;
1870 }
1871 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1872 if (program == fHWProgram) {
1873 return;
1874 }
1875 auto id = program->programID();
1876 SkASSERT(id);
1877 GL_CALL(UseProgram(id));
1878 fHWProgram = std::move(program);
1879 fHWProgramID = id;
1880}
1881
1882void GrGLGpu::flushProgram(GrGLuint id) {
1883 SkASSERT(id);
1884 if (fHWProgramID == id) {
1885 SkASSERT(!fHWProgram);
1886 return;
1887 }
1888 fHWProgram.reset();
1889 GL_CALL(UseProgram(id));
1890 fHWProgramID = id;
1891}
1892
1893void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001894 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001895 int baseVertex,
1896 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001897 int baseInstance,
1898 GrPrimitiveRestart enablePrimitiveRestart) {
1899 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001900
cdaltone2e71c22016-04-07 18:13:29 -07001901 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001902 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001903 SkASSERT(indexBuffer->isCpuBuffer() ||
1904 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001905 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001906 } else {
1907 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001908 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001909
Brian Salomon92be2f72018-06-19 14:33:47 -04001910 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1911 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001912
Brian Salomon802cb312018-06-08 18:05:20 -04001913 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001914 SkASSERT(vertexBuffer);
1915 SkASSERT(vertexBuffer->isCpuBuffer() ||
1916 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1917 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001918 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1919 const auto& attrib = fHWProgram->vertexAttribute(i);
1920 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001921 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1922 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001923 }
Chris Dalton1d616352017-05-31 12:51:23 -06001924 }
Brian Salomon802cb312018-06-08 18:05:20 -04001925 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001926 SkASSERT(instanceBuffer);
1927 SkASSERT(instanceBuffer->isCpuBuffer() ||
1928 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1929 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001930 int attribIdx = fHWProgram->numVertexAttributes();
1931 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1932 const auto& attrib = fHWProgram->instanceAttribute(i);
1933 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001934 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1935 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1936 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001937 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001938 }
1939}
1940
Brian Salomonae64c192019-02-05 09:41:37 -05001941GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001942 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001943
cdaltone2e71c22016-04-07 18:13:29 -07001944 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001945 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001946 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001947 }
cdaltone2e71c22016-04-07 18:13:29 -07001948
Brian Salomonae64c192019-02-05 09:41:37 -05001949 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001950 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001951 if (!bufferState->fBufferZeroKnownBound) {
1952 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1953 bufferState->fBufferZeroKnownBound = true;
1954 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001955 }
Brian Salomondbf70722019-02-07 11:31:24 -05001956 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1957 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001958 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1959 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1960 bufferState->fBufferZeroKnownBound = false;
1961 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001962 }
1963
Brian Salomonae64c192019-02-05 09:41:37 -05001964 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001965}
Brian Salomond818ebf2018-07-02 14:08:49 +00001966void GrGLGpu::disableScissor() {
1967 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1968 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1969 fHWScissorSettings.fEnabled = kNo_TriState;
1970 return;
1971 }
1972}
1973
Brian Osman9a9baae2018-11-05 15:06:26 -05001974void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001975 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001976 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001977 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001978 SkASSERT(!this->caps()->performColorClearsAsDraws());
1979 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001980
Brian Salomon43f8bf02017-10-18 08:33:29 -04001981 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001982
Brian Salomon43f8bf02017-10-18 08:33:29 -04001983 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1984
Brian Salomond818ebf2018-07-02 14:08:49 +00001985 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001986 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001987 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001988 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001989 }
Greg Danielacd66b42019-05-22 16:29:12 -04001990 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001991 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001992 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001993 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001994 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001995}
1996
Robert Phillips95214472017-08-08 18:00:03 -04001997void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001998 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1999
Robert Phillips95214472017-08-08 18:00:03 -04002000 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002001 return;
2002 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002003
Chris Dalton674f77a2019-09-30 20:49:39 -06002004 // This should only be called internally when we know we have a stencil buffer.
2005 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002006
bsalomonb0bd4f62014-09-03 07:19:50 -07002007 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002008 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002009
Brian Salomond818ebf2018-07-02 14:08:49 +00002010 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002011 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002012
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002013 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002014 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002015 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002016 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002017}
2018
Chris Daltonb50cc812019-10-14 16:29:21 -06002019static bool use_tiled_rendering(const GrGLCaps& glCaps,
2020 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2021 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2022 // Otherwise it's faster to just not use it.
2023 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2024 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2025}
2026
2027void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002028 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2029 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2030 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2031
2032 this->handleDirtyContext();
2033
2034 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2035 this->flushRenderTarget(glRT);
2036 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2037
Chris Daltonb50cc812019-10-14 16:29:21 -06002038 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2039 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2040 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2041 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2042 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2043 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2044 nativeBounds.fHeight, preserveMask));
2045 }
2046
Chris Dalton674f77a2019-09-30 20:49:39 -06002047 GrGLbitfield clearMask = 0;
2048 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2049 SkASSERT(!this->caps()->performColorClearsAsDraws());
2050 this->flushClearColor(colorLoadStore.fClearColor);
2051 this->flushColorWrite(true);
2052 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002053 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002054 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2055 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2056 GL_CALL(StencilMask(0xffffffff));
2057 GL_CALL(ClearStencil(0));
2058 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2059 }
2060 if (clearMask) {
2061 this->disableScissor();
2062 this->disableWindowRectangles();
2063 GL_CALL(Clear(clearMask));
2064 }
2065}
2066
2067void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2068 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2069 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2070 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2071
2072 this->handleDirtyContext();
2073
2074 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2075 // The framebuffer binding changed in the middle of a command buffer. We should have already
2076 // printed a warning during onFBOChanged.
2077 return;
2078 }
2079
2080 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2081 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2082
2083 SkSTArray<2, GrGLenum> discardAttachments;
2084 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2085 discardAttachments.push_back(
2086 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2087 }
2088 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2089 discardAttachments.push_back(
2090 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2091 }
2092
2093 if (!discardAttachments.empty()) {
2094 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2095 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2096 discardAttachments.begin()));
2097 } else {
2098 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2099 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2100 discardAttachments.begin()));
2101 }
2102 }
2103 }
2104
Chris Daltonb50cc812019-10-14 16:29:21 -06002105 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2106 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2107 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2108 // Handled by use_tiled_rendering().
2109 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2110 GL_CALL(EndTiling(preserveMask));
2111 }
2112
Chris Dalton674f77a2019-09-30 20:49:39 -06002113 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002114}
2115
csmartdalton29df7602016-08-31 11:55:52 -07002116void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2117 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002118 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002119 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002120 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002121 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002122
egdaniel8dc7c3a2015-04-16 11:22:42 -07002123 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002124 // this should only be called internally when we know we have a
2125 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002126 SkASSERT(sb);
2127 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002128#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002129 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002130 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002131#else
2132 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002133 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002134 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002135 // changing the clip between stencil passes may or may not
2136 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002137 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002138#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002139 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002140 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002141 value = (1 << (stencilBitCount - 1));
2142 } else {
2143 value = 0;
2144 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002145 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002146 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002147
Greg Danielacd66b42019-05-22 16:29:12 -04002148 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002149 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002150
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002151 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002152 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002153 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002154 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002155}
2156
Brian Salomone05ba5a2019-04-08 11:59:07 -04002157bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002158 GrColorType surfaceColorType, GrColorType dstColorType,
2159 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002160 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002161
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002162 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002163 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002164 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002165 return false;
2166 }
Greg Danielba88ab62019-07-26 09:14:01 -04002167 GrGLenum externalFormat = 0;
2168 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002169 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2170 surfaceColorType,
2171 dstColorType,
2172 &externalFormat,
2173 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002174 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002175 return false;
2176 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002177
Brian Salomon71d9d842016-11-03 13:42:00 -04002178 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002179 if (renderTarget->numSamples() <= 1 ||
2180 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2181 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2182 this->flushRenderTargetNoColorWrites(renderTarget);
2183 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2184 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2185 return false;
2186 } else {
2187 SkASSERT(renderTarget->requiresManualMSAAResolve());
2188 // we don't track the state of the READ FBO ID.
2189 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002190 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002191 } else {
2192 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002193 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002194 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002195 }
2196
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002197 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002198 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002199
Brian Salomona6948702018-06-01 15:33:20 -04002200 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002201 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002202 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002203 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002204 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002205 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002206
Brian Osman1348ed02018-09-12 09:08:39 -04002207 bool reattachStencil = false;
2208 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2209 renderTarget &&
2210 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002211 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002212 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2213 reattachStencil = true;
2214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2215 GR_GL_RENDERBUFFER, 0));
2216 }
2217
Chris Dalton76500e52019-09-05 02:13:05 -06002218 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002219 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002220
2221 if (reattachStencil) {
2222 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2223 renderTarget->renderTargetPriv().getStencilAttachment());
2224 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2225 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2226 }
2227
Brian Salomon26de56e2019-04-10 12:14:26 -04002228 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002229 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002230 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2231 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002232
Brian Salomone05ba5a2019-04-08 11:59:07 -04002233 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002234 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002235 }
2236 return true;
2237}
2238
2239bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002240 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2241 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002242 SkASSERT(surface);
2243
Brian Salomonb28cb682019-07-26 12:48:47 -04002244 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002245
Brian Salomon26de56e2019-04-10 12:14:26 -04002246 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2247 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002248
Brian Salomon1047a492019-07-02 12:25:21 -04002249 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002250 rowPixelWidth = width;
2251 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002252 SkASSERT(!(rowBytes % bytesPerPixel));
2253 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002254 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002255 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2256 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002257}
2258
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002259GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002260 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002261 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002262 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002263 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002264 if (!fCachedOpsRenderPass) {
2265 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002266 }
2267
Chris Daltonb50cc812019-10-14 16:29:21 -06002268 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002269 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002270}
2271
Brian Salomon1fabd512018-02-09 09:54:25 -05002272void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002273 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002274 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002275 this->didWriteToSurface(target, origin, &bounds);
2276}
bsalomon6ba6fa12015-03-04 11:57:37 -08002277
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002278void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2279 this->flushRenderTargetNoColorWrites(target);
2280 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002281}
2282
Brian Osman9aa30c62018-07-02 15:21:46 -04002283void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002284 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002285 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002286 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002287 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002288#ifdef SK_DEBUG
2289 // don't do this check in Chromium -- this is causing
2290 // lots of repeated command buffer flushes when the compositor is
2291 // rendering with Ganesh, which is really slow; even too slow for
2292 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002293 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002294 GrGLenum status;
2295 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2296 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2297 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2298 }
bsalomon160f24c2015-03-17 15:55:42 -07002299 }
egdanield803f272015-03-18 13:01:52 -07002300#endif
2301 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002302 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002303 }
2304
brianosman35b784d2016-05-05 11:52:53 -07002305 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002306 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002307 }
bsalomon083617b2016-02-12 12:10:14 -08002308}
bsalomona9909122016-01-23 10:41:40 -08002309
brianosman33f6b3f2016-06-02 05:49:21 -07002310void GrGLGpu::flushFramebufferSRGB(bool enable) {
2311 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2312 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2313 fHWSRGBFramebuffer = kYes_TriState;
2314 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2315 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2316 fHWSRGBFramebuffer = kNo_TriState;
2317 }
2318}
2319
Greg Danielacd66b42019-05-22 16:29:12 -04002320void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002321 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002322 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002323 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002324 fHWViewport = viewport;
2325 }
2326}
2327
bsalomon@google.comd302f142011-03-03 13:54:13 +00002328#define SWAP_PER_DRAW 0
2329
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002330#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002331 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002332 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002333 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002334 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002335 void SwapBuf() {
2336 DWORD procID = GetCurrentProcessId();
2337 HWND hwnd = GetTopWindow(GetDesktopWindow());
2338 while(hwnd) {
2339 DWORD wndProcID = 0;
2340 GetWindowThreadProcessId(hwnd, &wndProcID);
2341 if(wndProcID == procID) {
2342 SwapBuffers(GetDC(hwnd));
2343 }
2344 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2345 }
2346 }
2347 #endif
2348#endif
2349
Robert Phillips901aff02019-10-08 12:32:56 -04002350void GrGLGpu::draw(GrRenderTarget* renderTarget,
2351 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002352 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002353 int meshCount) {
2354 this->handleDirtyContext();
2355
Robert Phillips2579de42019-10-09 09:51:59 -04002356 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002357
Robert Phillipsfcaae482019-11-07 10:17:03 -05002358 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002359 return;
2360 }
ethannicholas22793252016-01-30 09:59:10 -08002361
Robert Phillips901aff02019-10-08 12:32:56 -04002362 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2363 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2364
Brian Salomonf7232642018-09-19 08:58:08 -04002365 for (int m = 0; m < meshCount; ++m) {
Robert Phillips901aff02019-10-08 12:32:56 -04002366 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2367 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002368 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002369 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002370
Robert Phillips901aff02019-10-08 12:32:56 -04002371 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002372 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002373 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2374 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002375 }
Robert Phillips901aff02019-10-08 12:32:56 -04002376 if (hasDynamicPrimProcTextures) {
2377 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2378 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2379 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002380 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002381 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002382 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002383 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2384 GL_CALL(Enable(GR_GL_CULL_FACE));
2385 GL_CALL(Disable(GR_GL_CULL_FACE));
2386 }
Brian Salomonf7232642018-09-19 08:58:08 -04002387 meshes[m].sendToGpu(this);
2388 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002389 }
ethannicholas22793252016-01-30 09:59:10 -08002390
bsalomon@google.comd302f142011-03-03 13:54:13 +00002391#if SWAP_PER_DRAW
2392 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002393 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002394 aglSwapBuffers(aglGetCurrentContext());
2395 int set_a_break_pt_here = 9;
2396 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002397 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002398 SwapBuf();
2399 int set_a_break_pt_here = 9;
2400 SwapBuf();
2401 #endif
2402#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002403}
2404
Chris Dalton3809bab2017-06-13 10:55:06 -06002405static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2406 switch (primitiveType) {
2407 case GrPrimitiveType::kTriangles:
2408 return GR_GL_TRIANGLES;
2409 case GrPrimitiveType::kTriangleStrip:
2410 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002411 case GrPrimitiveType::kPoints:
2412 return GR_GL_POINTS;
2413 case GrPrimitiveType::kLines:
2414 return GR_GL_LINES;
2415 case GrPrimitiveType::kLineStrip:
2416 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002417 case GrPrimitiveType::kPatches:
2418 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002419 case GrPrimitiveType::kPath:
2420 SK_ABORT("non-mesh-based GrPrimitiveType");
2421 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002422 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002423 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002424}
2425
Chris Dalton34280e22019-12-20 11:08:25 -07002426void GrGLGpu::sendArrayMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex) {
2427 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002428 if (GR_GL_PATCHES == glPrimType) {
2429 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2430 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002431 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Chris Dalton34280e22019-12-20 11:08:25 -07002432 this->setupGeometry(nullptr, mesh.vertexBuffer(), baseVertex, nullptr, 0,
2433 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002434 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2435 } else {
Chris Dalton34280e22019-12-20 11:08:25 -07002436 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002437 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2438 }
2439 fStats.incNumDraws();
2440}
2441
Brian Salomondbf70722019-02-07 11:31:24 -05002442static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2443 size_t baseOffset = baseIndex * sizeof(uint16_t);
2444 if (indexBuffer->isCpuBuffer()) {
2445 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2446 } else {
2447 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2448 }
2449}
2450
Chris Dalton34280e22019-12-20 11:08:25 -07002451void GrGLGpu::sendIndexedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2452 uint16_t minIndexValue, uint16_t maxIndexValue, int baseVertex) {
2453 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002454 if (GR_GL_PATCHES == glPrimType) {
2455 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2456 }
Chris Dalton114a3c02017-05-26 15:17:19 -06002457
Chris Dalton34280e22019-12-20 11:08:25 -07002458 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
2459
2460 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex, nullptr, 0,
2461 mesh.primitiveRestart());
Chris Dalton114a3c02017-05-26 15:17:19 -06002462
2463 if (this->glCaps().drawRangeElementsSupport()) {
2464 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002465 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002466 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002467 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002468 }
2469 fStats.incNumDraws();
2470}
2471
Chris Dalton34280e22019-12-20 11:08:25 -07002472void GrGLGpu::sendInstancedMeshToGpu(const GrMesh& mesh, int vertexCount, int baseVertex,
2473 int instanceCount, int baseInstance) {
2474 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002475 if (GR_GL_PATCHES == glPrimType) {
2476 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2477 }
Chris Dalton1b4ad762018-10-04 11:58:09 -06002478 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002479 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002480 this->setupGeometry(nullptr, mesh.vertexBuffer(), 0, mesh.instanceBuffer(),
2481 baseInstance + i, GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002482 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2483 SkTMin(instanceCount - i, maxInstances)));
2484 fStats.incNumDraws();
2485 }
Chris Dalton1d616352017-05-31 12:51:23 -06002486}
2487
Chris Dalton34280e22019-12-20 11:08:25 -07002488void GrGLGpu::sendIndexedInstancedMeshToGpu(const GrMesh& mesh, int indexCount, int baseIndex,
2489 int baseVertex, int instanceCount, int baseInstance) {
2490 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(mesh.primitiveType());
Chris Dalton5a2f9622019-12-27 14:56:38 -07002491 if (GR_GL_PATCHES == glPrimType) {
2492 this->flushPatchVertexCount(mesh.tessellationPatchVertexCount());
2493 }
Chris Dalton34280e22019-12-20 11:08:25 -07002494 const GrGLvoid* elementPtr = element_ptr(mesh.indexBuffer(), baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002495 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2496 for (int i = 0; i < instanceCount; i += maxInstances) {
Chris Dalton34280e22019-12-20 11:08:25 -07002497 this->setupGeometry(mesh.indexBuffer(), mesh.vertexBuffer(), baseVertex,
2498 mesh.instanceBuffer(), baseInstance + i, mesh.primitiveRestart());
Brian Salomondbf70722019-02-07 11:31:24 -05002499 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002500 SkTMin(instanceCount - i, maxInstances)));
2501 fStats.incNumDraws();
2502 }
Chris Dalton1d616352017-05-31 12:51:23 -06002503}
2504
Chris Dalton16a33c62019-09-24 22:19:17 -06002505void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2506 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002507 // Some extensions automatically resolves the texture when it is read.
2508 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2509
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002510 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002511 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2512 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2513 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2514 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002515
Chris Daltonc139d082019-09-26 14:04:24 -06002516 // make sure we go through flushRenderTarget() since we've modified
2517 // the bound DRAW FBO ID.
2518 fHWBoundRenderTargetUniqueID.makeInvalid();
2519 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2520 // Apple's extension uses the scissor as the blit bounds.
2521 GrScissorState scissorState;
2522 scissorState.set(resolveRect);
2523 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2524 this->disableWindowRectangles();
2525 GL_CALL(ResolveMultisampleFramebuffer());
2526 } else {
2527 int l, b, r, t;
2528 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2529 this->glCaps().blitFramebufferSupportFlags()) {
2530 l = 0;
2531 b = 0;
2532 r = target->width();
2533 t = target->height();
2534 } else {
2535 auto rect = GrNativeRect::MakeRelativeTo(
2536 resolveOrigin, rt->height(), resolveRect);
2537 l = rect.fX;
2538 b = rect.fY;
2539 r = rect.fX + rect.fWidth;
2540 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002541 }
Chris Daltonc139d082019-09-26 14:04:24 -06002542
2543 // BlitFrameBuffer respects the scissor, so disable it.
2544 this->disableScissor();
2545 this->disableWindowRectangles();
2546 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 +00002547 }
2548}
2549
bsalomon@google.com411dad02012-06-05 20:24:20 +00002550namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002551
bsalomon@google.com411dad02012-06-05 20:24:20 +00002552
2553GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002554 static const GrGLenum gTable[kGrStencilOpCount] = {
2555 GR_GL_KEEP, // kKeep
2556 GR_GL_ZERO, // kZero
2557 GR_GL_REPLACE, // kReplace
2558 GR_GL_INVERT, // kInvert
2559 GR_GL_INCR_WRAP, // kIncWrap
2560 GR_GL_DECR_WRAP, // kDecWrap
2561 GR_GL_INCR, // kIncClamp
2562 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002563 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002564 static_assert(0 == (int)GrStencilOp::kKeep);
2565 static_assert(1 == (int)GrStencilOp::kZero);
2566 static_assert(2 == (int)GrStencilOp::kReplace);
2567 static_assert(3 == (int)GrStencilOp::kInvert);
2568 static_assert(4 == (int)GrStencilOp::kIncWrap);
2569 static_assert(5 == (int)GrStencilOp::kDecWrap);
2570 static_assert(6 == (int)GrStencilOp::kIncClamp);
2571 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002572 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2573 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002574}
2575
2576void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002577 const GrStencilSettings::Face& face,
2578 GrGLenum glFace) {
2579 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2580 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2581 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002582
cdalton93a379b2016-05-11 13:58:08 -07002583 GrGLint ref = face.fRef;
2584 GrGLint mask = face.fTestMask;
2585 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002586
2587 if (GR_GL_FRONT_AND_BACK == glFace) {
2588 // we call the combined func just in case separate stencil is not
2589 // supported.
2590 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2591 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002592 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002593 } else {
2594 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2595 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002596 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002597 }
2598}
2599}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002600
Chris Dalton71713f62019-04-18 12:41:03 -06002601void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002602 if (stencilSettings.isDisabled()) {
2603 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002604 } else if (fHWStencilSettings != stencilSettings ||
2605 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002606 if (kYes_TriState != fHWStencilTestEnabled) {
2607 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002608
csmartdaltonc7d85332016-10-26 10:13:46 -07002609 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002610 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002611 if (!stencilSettings.isTwoSided()) {
2612 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2613 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002614 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002615 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2616 GR_GL_FRONT);
2617 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2618 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002619 }
joshualitta58fe352014-10-27 08:39:00 -07002620 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002621 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002622 }
2623}
2624
csmartdaltonc7d85332016-10-26 10:13:46 -07002625void GrGLGpu::disableStencil() {
2626 if (kNo_TriState != fHWStencilTestEnabled) {
2627 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002628
csmartdaltonc7d85332016-10-26 10:13:46 -07002629 fHWStencilTestEnabled = kNo_TriState;
2630 fHWStencilSettings.invalidate();
2631 }
2632}
2633
Chris Dalton4c56b032019-03-04 12:28:42 -07002634void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002635 // rt is only optional if useHWAA is false.
2636 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002637#ifdef SK_DEBUG
2638 if (useHWAA && rt->numSamples() <= 1) {
2639 SkASSERT(this->caps()->mixedSamplesSupport());
2640 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2641 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2642 }
2643#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002644
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002645 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002646 if (useHWAA) {
2647 if (kYes_TriState != fMSAAEnabled) {
2648 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2649 fMSAAEnabled = kYes_TriState;
2650 }
2651 } else {
2652 if (kNo_TriState != fMSAAEnabled) {
2653 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2654 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002655 }
2656 }
2657 }
2658}
2659
Chris Daltonce425af2019-12-16 10:39:03 -07002660void GrGLGpu::flushConservativeRasterState(bool enabled) {
2661 if (this->caps()->conservativeRasterSupport()) {
2662 if (enabled) {
2663 if (kYes_TriState != fHWConservativeRasterEnabled) {
2664 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2665 fHWConservativeRasterEnabled = kYes_TriState;
2666 }
2667 } else {
2668 if (kNo_TriState != fHWConservativeRasterEnabled) {
2669 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2670 fHWConservativeRasterEnabled = kNo_TriState;
2671 }
2672 }
2673 }
2674}
2675
Chris Dalton1215cda2019-12-17 21:44:04 -07002676void GrGLGpu::flushWireframeState(bool enabled) {
2677 if (this->caps()->wireframeSupport()) {
2678 if (this->caps()->wireframeMode() || enabled) {
2679 if (kYes_TriState != fHWWireframeEnabled) {
2680 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2681 fHWWireframeEnabled = kYes_TriState;
2682 }
2683 } else {
2684 if (kNo_TriState != fHWWireframeEnabled) {
2685 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2686 fHWWireframeEnabled = kNo_TriState;
2687 }
2688 }
2689 }
2690}
2691
Chris Daltonbbb3f642019-07-24 12:25:08 -04002692void GrGLGpu::flushBlendAndColorWrite(
2693 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2694 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2695 // We need to work around a driver bug by using a blend state that preserves the dst color,
2696 // rather than disabling color writes.
2697 GrXferProcessor::BlendInfo preserveDstBlend;
2698 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2699 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2700 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2701 return;
2702 }
bsalomonf7cc8772015-05-11 11:21:14 -07002703
cdalton8917d622015-05-06 13:40:21 -07002704 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002705 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2706 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002707
2708 // Any optimization to disable blending should have already been applied and
2709 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002710 bool blendOff =
2711 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2712 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2713 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002714
egdanielb414f252014-07-29 13:15:47 -07002715 if (blendOff) {
2716 if (kNo_TriState != fHWBlendState.fEnabled) {
2717 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002718
2719 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2720 // https://code.google.com/p/skia/issues/detail?id=3943
2721 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2722 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2723 SkASSERT(this->caps()->advancedBlendEquationSupport());
2724 // Set to any basic blending equation.
2725 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2726 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2727 fHWBlendState.fEquation = blend_equation;
2728 }
2729
egdanielb414f252014-07-29 13:15:47 -07002730 fHWBlendState.fEnabled = kNo_TriState;
2731 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002732 } else {
2733 if (kYes_TriState != fHWBlendState.fEnabled) {
2734 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002735
Chris Daltonbbb3f642019-07-24 12:25:08 -04002736 fHWBlendState.fEnabled = kYes_TriState;
2737 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002738
Chris Daltonbbb3f642019-07-24 12:25:08 -04002739 if (fHWBlendState.fEquation != equation) {
2740 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2741 fHWBlendState.fEquation = equation;
2742 }
cdalton8917d622015-05-06 13:40:21 -07002743
Chris Daltonbbb3f642019-07-24 12:25:08 -04002744 if (GrBlendEquationIsAdvanced(equation)) {
2745 SkASSERT(this->caps()->advancedBlendEquationSupport());
2746 // Advanced equations have no other blend state.
2747 return;
2748 }
cdalton8917d622015-05-06 13:40:21 -07002749
Chris Daltonbbb3f642019-07-24 12:25:08 -04002750 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2751 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2752 gXfermodeCoeff2Blend[dstCoeff]));
2753 fHWBlendState.fSrcCoeff = srcCoeff;
2754 fHWBlendState.fDstCoeff = dstCoeff;
2755 }
cdalton8917d622015-05-06 13:40:21 -07002756
Chris Daltonbbb3f642019-07-24 12:25:08 -04002757 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2758 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2759 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2760 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2761 fHWBlendState.fConstColor = blendConst;
2762 fHWBlendState.fConstColorValid = true;
2763 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002764 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002765 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002766
2767 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002768}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002769
Brian Salomondc829942018-10-23 16:07:24 -04002770static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002771 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002772 switch (swizzle[i]) {
2773 case 'r': glValues[i] = GR_GL_RED; break;
2774 case 'g': glValues[i] = GR_GL_GREEN; break;
2775 case 'b': glValues[i] = GR_GL_BLUE; break;
2776 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002777 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002778 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002779 default: SK_ABORT("Unsupported component");
2780 }
Brian Salomon327955b2018-10-22 15:39:22 +00002781 }
2782}
2783
Greg Daniel2c3398d2019-06-19 11:58:01 -04002784void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2785 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002786 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002787
reed856e9d92015-09-30 12:21:45 -07002788#ifdef SK_DEBUG
2789 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002790 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002791 const int w = texture->width();
2792 const int h = texture->height();
2793 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2794 }
2795 }
2796#endif
2797
Robert Phillips294870f2016-11-11 12:38:40 -05002798 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002799 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002800 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002801 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002802 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002803 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002804 }
2805
Brian Salomondc829942018-10-23 16:07:24 -04002806 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002807 if (!this->caps()->mipMapSupport() ||
2808 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002809 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002810 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002811 }
bsalomonefd7d452014-10-23 14:17:46 -07002812
brianosman33f6b3f2016-06-02 05:49:21 -07002813#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002814 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002815 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002816 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002817 }
2818#endif
2819
Brian Salomone2826ab2019-06-04 15:58:31 -04002820 auto timestamp = texture->parameters()->resetTimestamp();
2821 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002822
Brian Salomone2826ab2019-06-04 15:58:31 -04002823 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2824 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002825 if (fSamplerObjectCache) {
2826 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2827 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002828 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2829 texture->parameters()->samplerOverriddenState();
2830 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002831
Brian Salomone2826ab2019-06-04 15:58:31 -04002832 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2833 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002834
Brian Salomone2826ab2019-06-04 15:58:31 -04002835 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2836 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002837
2838 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002839 newSamplerState.fMinLOD = -1000.f;
2840 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002841
Brian Salomone2826ab2019-06-04 15:58:31 -04002842 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002843 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002844 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002845 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002847 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002849 }
Mike Klein1bccae52018-10-19 11:38:44 +00002850 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002851 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002852 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002853 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002854 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002856 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002858 }
2859 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002860 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002861 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002862 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002863 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002864 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002865 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002866 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002867 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002868 if (this->glCaps().clampToBorderSupport()) {
2869 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002870 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002871 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002872 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2873 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002874 }
2875 }
Brian Salomondc829942018-10-23 16:07:24 -04002876 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002877 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2878 newNonsamplerState.fBaseMipMapLevel = 0;
2879 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002880
Brian Salomone2826ab2019-06-04 15:58:31 -04002881 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2882 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002883 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002884 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2885 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002886 GrGLenum glValues[4];
2887 get_gl_swizzle_values(swizzle, glValues);
2888 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002889 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002890 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002891 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2892 reinterpret_cast<const GrGLint*>(glValues)));
2893 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002894 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2895 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2896 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2897 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2898 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002899 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002900 }
2901 }
Brian Salomondc829942018-10-23 16:07:24 -04002902 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002903 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2904 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2905 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002906 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002907 this->setTextureUnit(unitIdx);
2908 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002909 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002910 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002911 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002912 this->setTextureUnit(unitIdx);
2913 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002914 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002915 }
Brian Salomon327955b2018-10-22 15:39:22 +00002916 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002917 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2918 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002919}
2920
Brian Salomon1f05d452019-02-08 12:33:08 -05002921void GrGLGpu::onResetTextureBindings() {
2922 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2923 GR_GL_TEXTURE_EXTERNAL};
2924 for (int i = 0; i < this->numTextureUnits(); ++i) {
2925 this->setTextureUnit(i);
2926 for (auto target : kTargets) {
2927 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2928 GL_CALL(BindTexture(target, 0));
2929 }
2930 }
2931 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2932 }
2933}
2934
Chris Dalton5a2f9622019-12-27 14:56:38 -07002935void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2936 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2937 if (fHWPatchVertexCount != count) {
2938 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2939 fHWPatchVertexCount = count;
2940 }
2941}
2942
egdaniel080e6732014-12-22 07:35:52 -08002943void GrGLGpu::flushColorWrite(bool writeColor) {
2944 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002945 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002946 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2947 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002948 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002949 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002950 } else {
2951 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002952 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002953 fHWWriteToColor = kYes_TriState;
2954 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002955 }
bsalomon3e791242014-12-17 13:43:13 -08002956}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002957
Chris Dalton674f77a2019-09-30 20:49:39 -06002958void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2959 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2960 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2961 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2962 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2963 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2964 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2965 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002966 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2967 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2968 GL_CALL(ClearColor(r, g, b, a));
2969 fHWClearColor[0] = r;
2970 fHWClearColor[1] = g;
2971 fHWClearColor[2] = b;
2972 fHWClearColor[3] = a;
2973 }
2974}
2975
bsalomon861e1032014-12-16 07:33:49 -08002976void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002977 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002978 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002979 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002980 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002981 }
2982}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002983
Brian Salomond978b902019-02-07 15:09:18 -05002984void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002985 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002986 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002987 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2988 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2989 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002990 }
Brian Salomond978b902019-02-07 15:09:18 -05002991 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2992 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002993 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002994 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002995}
2996
Brian Salomone5e7eb12016-10-14 16:18:33 -04002997// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002998static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2999 const GrSurface* src,
3000 const SkIRect& srcRect,
3001 const SkIPoint& dstPoint,
3002 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003003 int dstSampleCnt = 0;
3004 int srcSampleCnt = 0;
3005 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003006 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00003007 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003008 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06003009 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003010 }
3011 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
3012 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
3013
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003014 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3015 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3016
Brian Salomone5e7eb12016-10-14 16:18:33 -04003017 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003018 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003019
Greg Daniel46cfbc62019-06-07 11:43:30 -04003020 GrTextureType dstTexType;
3021 GrTextureType* dstTexTypePtr = nullptr;
3022 GrTextureType srcTexType;
3023 GrTextureType* srcTexTypePtr = nullptr;
3024 if (dstTex) {
3025 dstTexType = dstTex->texturePriv().textureType();
3026 dstTexTypePtr = &dstTexType;
3027 }
3028 if (srcTex) {
3029 srcTexType = srcTex->texturePriv().textureType();
3030 srcTexTypePtr = &srcTexType;
3031 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003032
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003033 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3034 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003035 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003036}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003037
Brian Osmana9c8a052018-01-19 10:31:56 -05003038static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3039 // A RT has a separate MSAA renderbuffer if:
3040 // 1) It's multisampled
3041 // 2) We're using an extension with separate MSAA renderbuffers
3042 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003043 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003044}
3045
Greg Daniel46cfbc62019-06-07 11:43:30 -04003046static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003047 const GrGLCaps& caps) {
3048
bsalomon@google.comeb851172013-04-15 13:51:00 +00003049 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003050 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003051 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003052 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003053
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003054 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3055 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3056
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003057 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3058 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3059
Greg Daniel46cfbc62019-06-07 11:43:30 -04003060 GrTextureType dstTexType;
3061 GrTextureType* dstTexTypePtr = nullptr;
3062 GrTextureType srcTexType;
3063 GrTextureType* srcTexTypePtr = nullptr;
3064 if (dstTex) {
3065 dstTexType = dstTex->texturePriv().textureType();
3066 dstTexTypePtr = &dstTexType;
3067 }
3068 if (srcTex) {
3069 srcTexType = srcTex->texturePriv().textureType();
3070 srcTexTypePtr = &srcTexType;
3071 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003072
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003073 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3074 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003075}
3076
Greg Danielacd66b42019-05-22 16:29:12 -04003077// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003078void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003079 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003080 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003081 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003082 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003083 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3084 GrGLuint texID = texture->textureID();
3085 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003086 GrGLuint* tempFBOID;
3087 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003088
egdanield803f272015-03-18 13:01:52 -07003089 if (0 == *tempFBOID) {
3090 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003091 }
3092
Adrienne Walker4ee88512018-05-17 11:37:14 -07003093 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003094 GR_GL_CALL(
3095 this->glInterface(),
3096 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3097 if (mipLevel == 0) {
3098 texture->baseLevelWasBoundToFBO();
3099 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003100 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003101 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003102 }
egdaniel0f5f9672015-02-03 11:10:51 -08003103}
3104
Brian Salomond2a8ae22019-09-10 16:03:59 -04003105void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003106 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003107 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003108 SkASSERT(surface->asTexture());
3109 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3110 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3111 GR_GL_COLOR_ATTACHMENT0,
3112 textureTarget,
3113 0,
3114 0));
3115 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003116}
3117
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003118void GrGLGpu::onFBOChanged() {
3119 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3120 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3121 GL_CALL(Flush());
3122 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003123#ifdef SK_DEBUG
3124 if (fIsExecutingCommandBuffer_DebugOnly) {
3125 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3126 "This will severely hurt performance.\n");
3127 }
3128#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003129}
3130
Adrienne Walker4ee88512018-05-17 11:37:14 -07003131void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3132 fStats.incRenderTargetBinds();
3133 GL_CALL(BindFramebuffer(target, fboid));
3134 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3135 fBoundDrawFramebuffer = fboid;
3136 }
3137
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003138 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3139 // The driver forgets the correct scissor when modifying the FBO binding.
3140 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003141 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003142 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003143 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003144 }
3145
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003146 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003147}
3148
Adrienne Walker4ee88512018-05-17 11:37:14 -07003149void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3150 if (fboid == fBoundDrawFramebuffer &&
3151 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3152 // This workaround only applies to deleting currently bound framebuffers
3153 // on Adreno 420. Because this is a somewhat rare case, instead of
3154 // tracking all the attachments of every framebuffer instead just always
3155 // unbind all attachments.
3156 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3157 GR_GL_RENDERBUFFER, 0));
3158 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3159 GR_GL_RENDERBUFFER, 0));
3160 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3161 GR_GL_RENDERBUFFER, 0));
3162 }
3163
3164 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003165
3166 // Deleting the currently bound framebuffer rebinds to 0.
3167 if (fboid == fBoundDrawFramebuffer) {
3168 this->onFBOChanged();
3169 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003170}
3171
Greg Daniel46cfbc62019-06-07 11:43:30 -04003172bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003173 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003174 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3175 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3176 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003177 auto dstFormat = dst->backendFormat().asGLFormat();
3178 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003179 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003180 return true;
3181 }
3182 }
cblume61214052016-01-26 09:10:48 -08003183
Greg Daniel46cfbc62019-06-07 11:43:30 -04003184 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3185 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003186 return true;
3187 }
3188
Greg Daniel46cfbc62019-06-07 11:43:30 -04003189 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3190 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003191 }
3192
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003193 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003194 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003195 return true;
3196 }
bsalomon083617b2016-02-12 12:10:14 -08003197 }
3198
bsalomon6df86402015-06-01 10:41:49 -07003199 return false;
3200}
3201
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003202bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003203 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003204
3205 int progIdx = TextureToCopyProgramIdx(srcTex);
3206 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3207 GrSLType samplerType =
3208 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3209
3210 if (!fCopyProgramArrayBuffer) {
3211 static const GrGLfloat vdata[] = {
3212 0, 0,
3213 0, 1,
3214 1, 0,
3215 1, 1
3216 };
3217 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3218 kStatic_GrAccessPattern, vdata);
3219 }
3220 if (!fCopyProgramArrayBuffer) {
3221 return false;
3222 }
3223
3224 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3225 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3226 if (!fCopyPrograms[progIdx].fProgram) {
3227 return false;
3228 }
3229
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003230 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3231 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3232 GrShaderVar::kUniform_TypeModifier);
3233 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3234 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3235 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3236 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3237
Greg Daniel9e3169b2019-06-06 14:38:17 -04003238 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003239 if (shaderCaps->noperspectiveInterpolationSupport()) {
3240 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3241 vshaderTxt.appendf("#extension %s : require\n", extension);
3242 }
3243 vTexCoord.addModifier("noperspective");
3244 }
3245
3246 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3247 vshaderTxt.append(";");
3248 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3249 vshaderTxt.append(";");
3250 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3251 vshaderTxt.append(";");
3252 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3253 vshaderTxt.append(";");
3254
3255 vshaderTxt.append(
3256 "// Copy Program VS\n"
3257 "void main() {"
3258 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3259 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3260 " sk_Position.zw = half2(0, 1);"
3261 "}"
3262 );
3263
Greg Daniel9e3169b2019-06-06 14:38:17 -04003264 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003265 if (shaderCaps->noperspectiveInterpolationSupport()) {
3266 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3267 fshaderTxt.appendf("#extension %s : require\n", extension);
3268 }
3269 }
3270 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3271 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3272 fshaderTxt.append(";");
3273 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3274 fshaderTxt.append(";");
3275 fshaderTxt.appendf(
3276 "// Copy Program FS\n"
3277 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003278 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003279 "}"
3280 );
3281
3282 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3283 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3284 SkSL::Program::Settings settings;
3285 settings.fCaps = shaderCaps;
3286 SkSL::String glsl;
3287 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3288 sksl, settings, &glsl, errorHandler);
3289 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3290 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3291 SkASSERT(program->fInputs.isEmpty());
3292
3293 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3294 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3295 errorHandler);
3296 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3297 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3298 errorHandler);
3299 SkASSERT(program->fInputs.isEmpty());
3300
3301 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3302
3303 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3304 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3305 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3306 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3307 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3308 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3309
3310 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3311
3312 GL_CALL(DeleteShader(vshader));
3313 GL_CALL(DeleteShader(fshader));
3314
3315 return true;
3316}
3317
brianosman33f6b3f2016-06-02 05:49:21 -07003318bool GrGLGpu::createMipmapProgram(int progIdx) {
3319 const bool oddWidth = SkToBool(progIdx & 0x2);
3320 const bool oddHeight = SkToBool(progIdx & 0x1);
3321 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3322
Brian Salomon1edc5b92016-11-29 13:43:46 -05003323 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003324
3325 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3326 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3327 if (!fMipmapPrograms[progIdx].fProgram) {
3328 return false;
3329 }
3330
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003331 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3332 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003333 GrShaderVar::kUniform_TypeModifier);
3334 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3335 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003336 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003337 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003338 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3339 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3340 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3341 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003342 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003343 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003344
Ethan Nicholasfc994162019-06-06 10:04:27 -04003345 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003346 if (shaderCaps->noperspectiveInterpolationSupport()) {
3347 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003348 vshaderTxt.appendf("#extension %s : require\n", extension);
3349 }
3350 vTexCoords[0].addModifier("noperspective");
3351 vTexCoords[1].addModifier("noperspective");
3352 vTexCoords[2].addModifier("noperspective");
3353 vTexCoords[3].addModifier("noperspective");
3354 }
3355
Brian Salomon1edc5b92016-11-29 13:43:46 -05003356 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003357 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003358 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003359 vshaderTxt.append(";");
3360 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003361 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003362 vshaderTxt.append(";");
3363 }
3364
3365 vshaderTxt.append(
3366 "// Mipmap Program VS\n"
3367 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003368 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3369 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003370 );
3371
3372 // Insert texture coordinate computation:
3373 if (oddWidth && oddHeight) {
3374 vshaderTxt.append(
3375 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003376 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3377 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003378 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3379 );
3380 } else if (oddWidth) {
3381 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003382 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3383 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003384 );
3385 } else if (oddHeight) {
3386 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003387 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3388 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003389 );
3390 } else {
3391 vshaderTxt.append(
3392 " v_texCoord0 = a_vertex.xy;"
3393 );
3394 }
3395
3396 vshaderTxt.append("}");
3397
Ethan Nicholasfc994162019-06-06 10:04:27 -04003398 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003399 if (shaderCaps->noperspectiveInterpolationSupport()) {
3400 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003401 fshaderTxt.appendf("#extension %s : require\n", extension);
3402 }
3403 }
brianosman33f6b3f2016-06-02 05:49:21 -07003404 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003405 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003406 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003407 fshaderTxt.append(";");
3408 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003409 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003410 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003411 fshaderTxt.append(
3412 "// Mipmap Program FS\n"
3413 "void main() {"
3414 );
3415
3416 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003417 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003418 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3419 " sample(u_texture, v_texCoord1) + "
3420 " sample(u_texture, v_texCoord2) + "
3421 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003422 );
3423 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003424 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003425 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3426 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003427 );
3428 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003429 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003430 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003431 );
3432 }
3433
3434 fshaderTxt.append("}");
3435
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003436 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003437 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003438 SkSL::Program::Settings settings;
3439 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003440 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003441 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003442 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003443 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003444 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003445 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003446
Brian Osman6c431d52019-04-15 16:31:54 -04003447 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003448 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3449 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003450 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003451 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003452 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003453 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003454
3455 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3456
3457 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3458 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3459 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3460 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3461
3462 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3463
3464 GL_CALL(DeleteShader(vshader));
3465 GL_CALL(DeleteShader(fshader));
3466
3467 return true;
3468}
3469
Greg Daniel46cfbc62019-06-07 11:43:30 -04003470bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003471 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003472 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3473 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3474 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3475 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003476 return false;
3477 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003478 int progIdx = TextureToCopyProgramIdx(srcTex);
3479 if (!dstRT) {
3480 SkASSERT(dstTex);
3481 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3482 return false;
3483 }
3484 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003485 if (!fCopyPrograms[progIdx].fProgram) {
3486 if (!this->createCopyProgram(srcTex)) {
3487 SkDebugf("Failed to create copy program.\n");
3488 return false;
3489 }
3490 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003491 int w = srcRect.width();
3492 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003493 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003494 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003495 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003496 this->flushViewport(dst->width(), dst->height());
3497 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003498 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003499 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003500 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003501 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3502 attribs->enableVertexArrays(this, 1);
3503 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3504 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003505 // dst rect edges in NDC (-1 to 1)
3506 int dw = dst->width();
3507 int dh = dst->height();
3508 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3509 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3510 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3511 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003512 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3513 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3514 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3515 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3516 int sw = src->width();
3517 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003518 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3519 // src rect edges in normalized texture space (0 to 1)
3520 sx0 /= sw;
3521 sx1 /= sw;
3522 sy0 /= sh;
3523 sy1 /= sh;
3524 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003525 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3526 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3527 sx1 - sx0, sy1 - sy0, sx0, sy0));
3528 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003529 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003530 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003531 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003532 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003533 this->disableScissor();
3534 this->disableWindowRectangles();
3535 this->disableStencil();
3536 if (this->glCaps().srgbWriteControl()) {
3537 this->flushFramebufferSRGB(true);
3538 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003539 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003540 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003541 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3542 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003543 return true;
3544}
3545
Greg Daniel46cfbc62019-06-07 11:43:30 -04003546void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003547 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003548 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003549 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003550 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003551 SkASSERT(dstTex);
3552 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003553 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003554
Brian Salomond978b902019-02-07 15:09:18 -05003555 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003556 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003557 dstPoint.fX, dstPoint.fY,
3558 srcRect.fLeft, srcRect.fTop,
3559 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003560 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003561 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3562 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003563 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3564 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003565}
3566
Greg Daniel46cfbc62019-06-07 11:43:30 -04003567bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003568 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003569 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003570 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3571 srcRect.width(), srcRect.height());
3572 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003573 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003574 return false;
mtklein404b3b22015-05-18 09:29:10 -07003575 }
bsalomon5df6fee2015-05-18 06:26:15 -07003576 }
bsalomon6df86402015-06-01 10:41:49 -07003577
Brian Salomond2a8ae22019-09-10 16:03:59 -04003578 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3579 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003580 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003581 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003582
3583 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003584 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003585 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003586
Greg Daniel46cfbc62019-06-07 11:43:30 -04003587 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3588 srcRect.fTop,
3589 srcRect.fRight,
3590 srcRect.fBottom,
3591 dstRect.fLeft,
3592 dstRect.fTop,
3593 dstRect.fRight,
3594 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003595 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003596 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3597 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003598
3599 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3600 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003601 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003602}
3603
Brian Salomon930f9392018-06-20 16:25:26 -04003604bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3605 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003606 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003607 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003608 return false;
3609 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003610 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003611 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3612 // Uses draw calls to do a series of downsample operations to successive mips.
3613
3614 // The manual approach requires the ability to limit which level we're sampling and that the
3615 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003616 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003617 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003618 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003619 GL_CALL(GenerateMipmap(glTex->target()));
3620 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003621 }
3622
brianosman33f6b3f2016-06-02 05:49:21 -07003623 int width = texture->width();
3624 int height = texture->height();
3625 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003626 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003627
3628 // Create (if necessary), then bind temporary FBO:
3629 if (0 == fTempDstFBOID) {
3630 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3631 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003632 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003633 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003634
3635 // Bind the texture, to get things configured for filtering.
3636 // We'll be changing our base level further below:
3637 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003638 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003639 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003640
3641 // Vertex data:
3642 if (!fMipmapProgramArrayBuffer) {
3643 static const GrGLfloat vdata[] = {
3644 0, 0,
3645 0, 1,
3646 1, 0,
3647 1, 1
3648 };
Brian Salomonae64c192019-02-05 09:41:37 -05003649 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003650 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003651 }
3652 if (!fMipmapProgramArrayBuffer) {
3653 return false;
3654 }
3655
3656 fHWVertexArrayState.setVertexArrayID(this, 0);
3657
3658 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003659 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003660 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003661 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003662
3663 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003664 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003665 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003666 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003667 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003668 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003669
3670 // Do all the blits:
3671 width = texture->width();
3672 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003673
brianosman33f6b3f2016-06-02 05:49:21 -07003674 for (GrGLint level = 1; level < levelCount; ++level) {
3675 // Get and bind the program for this particular downsample (filter shape can vary):
3676 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3677 if (!fMipmapPrograms[progIdx].fProgram) {
3678 if (!this->createMipmapProgram(progIdx)) {
3679 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003680 // Invalidate all params to cover base level change in a previous iteration.
3681 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003682 return false;
3683 }
3684 }
Brian Salomon802cb312018-06-08 18:05:20 -04003685 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003686
3687 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3688 const float invWidth = 1.0f / width;
3689 const float invHeight = 1.0f / height;
3690 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3691 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3692 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3693
3694 // Only sample from previous mip
3695 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3696
Brian Salomon930f9392018-06-20 16:25:26 -04003697 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3698 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003699
3700 width = SkTMax(1, width / 2);
3701 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003702 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003703
3704 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3705 }
3706
3707 // Unbind:
3708 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3709 GR_GL_TEXTURE_2D, 0, 0));
3710
Brian Salomon930f9392018-06-20 16:25:26 -04003711 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003712 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3713 // We drew the 2nd to last level into the last level.
3714 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3715 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003716
brianosman33f6b3f2016-06-02 05:49:21 -07003717 return true;
3718}
3719
Chris Daltond7291ba2019-03-07 14:17:03 -07003720void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003721 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3722 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003723
3724 int effectiveSampleCnt;
3725 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003726 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003727
3728 sampleLocations->reset(effectiveSampleCnt);
3729 for (int i = 0; i < effectiveSampleCnt; ++i) {
3730 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3731 }
3732}
3733
cdalton231c5fd2015-05-13 12:35:36 -07003734void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003735 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003736 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003737 case kTexture_GrXferBarrierType: {
3738 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003739 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003740 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3741 // The render target uses separate storage so no need for glTextureBarrier.
3742 // FIXME: The render target will resolve automatically when its texture is bound,
3743 // but we could resolve only the bounds that will be read if we do it here instead.
3744 return;
3745 }
cdalton9954bc32015-04-29 14:17:00 -07003746 SkASSERT(this->caps()->textureBarrierSupport());
3747 GL_CALL(TextureBarrier());
3748 return;
cdalton231c5fd2015-05-13 12:35:36 -07003749 }
cdalton8917d622015-05-06 13:40:21 -07003750 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003751 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003752 this->caps()->blendEquationSupport());
3753 GL_CALL(BlendBarrier());
3754 return;
bsalomoncb02b382015-08-12 11:14:50 -07003755 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003756 }
3757}
3758
Chris Daltone1196c52019-12-28 14:31:09 -07003759void GrGLGpu::insertManualFramebufferBarrier() {
3760 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3761 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3762}
3763
Brian Salomon5043f1f2019-07-11 21:27:54 -04003764static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003765 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003766 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3767 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3768 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3769 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3770 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3771 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3772 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3773 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3774 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003775 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003776 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003777 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3778 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3779 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3780 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003781
Brian Salomon5043f1f2019-07-11 21:27:54 -04003782 // Configs with multiple equivalent formats.
3783
Robert Phillipsebab03f2019-07-22 08:48:18 -04003784 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3785 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3786
Brian Salomon5043f1f2019-07-11 21:27:54 -04003787 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3788 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3789
3790 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3791 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips8f259a02019-12-20 11:32:27 -05003792
3793 case GrGLFormat::kCOMPRESSED_RGB8_BC1: return kRGB_BC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003794 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003795 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003796}
3797
Brian Salomon85c3d682019-11-04 15:04:54 -05003798GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003799 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003800 GrRenderable renderable,
Brian Salomon85c3d682019-11-04 15:04:54 -05003801 const BackendTextureData* data,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003802 GrMipMapped mipMapped,
Robert Phillips57ef6802019-09-23 10:12:47 -04003803 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003804 // We don't support protected textures in GL.
3805 if (isProtected == GrProtected::kYes) {
3806 return {};
3807 }
3808
Brian Salomon8a375832018-03-14 10:21:40 -04003809 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003810
Brian Salomond4764a12019-08-08 12:08:24 -04003811 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003812 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003813 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003814 }
3815
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003816 int numMipLevels = 1;
3817 if (mipMapped == GrMipMapped::kYes) {
3818 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3819 }
3820
Robert Phillipsd34691b2019-09-24 13:38:43 -04003821 // Compressed formats go through onCreateCompressedBackendTexture
3822 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3823
Greg Daniel15500642019-06-27 03:05:55 +00003824 GrGLTextureInfo info;
3825 GrGLTextureParameters::SamplerOverriddenState initialState;
3826
Robert Phillipsd34691b2019-09-24 13:38:43 -04003827 GrSurfaceDesc desc;
Brian Salomon85c3d682019-11-04 15:04:54 -05003828 desc.fWidth = dimensions.width();
3829 desc.fHeight = dimensions.height();
3830 desc.fConfig = gl_format_to_pixel_config(glFormat);
3831 if (desc.fConfig == kUnknown_GrPixelConfig) {
Robert Phillips42716d42019-12-16 12:19:54 -05003832 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003833 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003834
Robert Phillipsd34691b2019-09-24 13:38:43 -04003835 info.fTarget = GR_GL_TEXTURE_2D;
3836 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003837 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003838 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003839 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003840 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003841
Robert Phillips42716d42019-12-16 12:19:54 -05003842 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003843 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3844 SkTDArray<GrMipLevel> texels;
3845 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3846 // Incorporate the color type into the config to make it "specific" if applicable.
3847 desc.fConfig = this->caps()->getConfigFromBackendFormat(format, colorType);
3848 SkASSERT(desc.fConfig != kUnknown_GrPixelConfig);
3849 texels.append(numMipLevels);
3850 for (int i = 0; i < numMipLevels; ++i) {
3851 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3852 }
3853 if (!this->uploadTexData(glFormat, colorType, desc.fWidth, desc.fHeight, GR_GL_TEXTURE_2D,
3854 0, 0, desc.fWidth, desc.fHeight, colorType, texels.begin(),
3855 texels.count())) {
3856 GL_CALL(DeleteTextures(1, &info.fID));
3857 return {};
3858 }
3859 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3860 // TODO: Unify this with the clear texture code in onCreateTexture().
3861 GrColorType colorType;
3862 GrGLenum externalFormat, externalType;
3863 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3864 &externalType, &colorType);
3865 if (colorType == GrColorType::kUnknown) {
3866 GL_CALL(DeleteTextures(1, &info.fID));
3867 return {};
3868 }
3869
3870 // Make one tight image at the base size and reuse it for smaller levels.
3871 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3872 auto rb = ii.minRowBytes();
3873 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3874 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3875 GL_CALL(DeleteTextures(1, &info.fID));
3876 return {};
3877 }
3878
3879 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3880 SkISize levelDimensions = dimensions;
3881 for (int i = 0; i < numMipLevels; ++i) {
3882 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3883 levelDimensions.height(), externalFormat, externalType,
3884 pixelStorage.get()));
3885 levelDimensions = {SkTMax(1, levelDimensions.width() /2),
3886 SkTMax(1, levelDimensions.height()/2)};
3887 }
3888 }
3889 // Unbind this texture from the scratch texture unit.
3890 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003891
Brian Salomone2826ab2019-06-04 15:58:31 -04003892 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003893 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003894 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3895 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003896
Brian Salomon85c3d682019-11-04 15:04:54 -05003897 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3898 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003899}
3900
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003901void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003902 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3903
3904 GrGLTextureInfo info;
3905 if (tex.getGLTextureInfo(&info)) {
3906 GL_CALL(DeleteTextures(1, &info.fID));
3907 }
3908}
3909
3910#if GR_TEST_UTILS
3911
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003912bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003913 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003914
Greg Daniel52e16d92018-04-10 09:34:07 -04003915 GrGLTextureInfo info;
3916 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003917 return false;
3918 }
3919
3920 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003921 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003922
3923 return (GR_GL_TRUE == result);
3924}
3925
Brian Salomonf865b052018-03-09 09:01:53 -05003926GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003927 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003928 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3929 return GrBackendRenderTarget(); // invalid
3930 }
Brian Salomon8a375832018-03-14 10:21:40 -04003931 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003932 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003933 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003934 return {};
3935 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003936 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003937 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003938 if (sFormatIdx < 0) {
3939 return {};
3940 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003941 GrGLuint colorID = 0;
3942 GrGLuint stencilID = 0;
3943 auto deleteIDs = [&] {
3944 if (colorID) {
3945 if (useTexture) {
3946 GL_CALL(DeleteTextures(1, &colorID));
3947 } else {
3948 GL_CALL(DeleteRenderbuffers(1, &colorID));
3949 }
Brian Salomonf865b052018-03-09 09:01:53 -05003950 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003951 if (stencilID) {
3952 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003953 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003954 };
3955
3956 if (useTexture) {
3957 GL_CALL(GenTextures(1, &colorID));
3958 } else {
3959 GL_CALL(GenRenderbuffers(1, &colorID));
3960 }
3961 GL_CALL(GenRenderbuffers(1, &stencilID));
3962 if (!stencilID || !colorID) {
3963 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003964 return {};
3965 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003966
Brian Salomonf865b052018-03-09 09:01:53 -05003967 GrGLFramebufferInfo info;
3968 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003969 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003970 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3971 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003972 deleteIDs();
3973 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003974 }
3975
3976 this->invalidateBoundRenderTarget();
3977
Adrienne Walker4ee88512018-05-17 11:37:14 -07003978 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003979 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003980 GrGLTextureParameters::SamplerOverriddenState initialState;
3981 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3982 if (!colorID) {
3983 deleteIDs();
3984 return {};
3985 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003986 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3987 colorID, 0));
3988 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003989 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003990 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3991 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003992 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003993 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3994 GR_GL_RENDERBUFFER, colorID));
3995 }
3996 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003997 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3998 GL_ALLOC_CALL(this->glInterface(),
3999 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
4000 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04004001 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004002 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
4003 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04004004 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05004005 }
4006
Brian Salomon93348dd2018-08-29 12:56:23 -04004007 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
4008 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
4009 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
4010 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07004011 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04004012 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05004013
Adrienne Walker4ee88512018-05-17 11:37:14 -07004014 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004015 GrGLenum status;
4016 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
4017 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004018 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004019 return {};
4020 }
4021 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04004022
Greg Daniel108bb232018-07-03 16:18:29 -04004023 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04004024 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04004025 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004026}
4027
4028void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004029 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004030 GrGLFramebufferInfo info;
4031 if (backendRT.getGLFramebufferInfo(&info)) {
4032 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004033 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004034 }
4035 }
joshualitt8fd844f2015-12-02 13:36:47 -08004036}
4037
Greg Daniel26b50a42018-03-08 09:49:58 -05004038void GrGLGpu::testingOnly_flushGpuAndSync() {
4039 GL_CALL(Finish());
4040}
Brian Salomonf865b052018-03-09 09:01:53 -05004041#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004042
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004043///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004044
cdaltone2e71c22016-04-07 18:13:29 -07004045GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004046 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004047 GrGLAttribArrayState* attribState;
4048
cdaltone2e71c22016-04-07 18:13:29 -07004049 if (gpu->glCaps().isCoreProfile()) {
4050 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004051 GrGLuint arrayID;
4052 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4053 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004054 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004055 }
cdaltone2e71c22016-04-07 18:13:29 -07004056 if (ibuf) {
4057 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004058 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004059 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004060 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004061 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004062 if (ibuf) {
4063 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004064 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004065 } else {
4066 this->setVertexArrayID(gpu, 0);
4067 }
4068 int attrCount = gpu->glCaps().maxVertexAttributes();
4069 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4070 fDefaultVertexArrayAttribState.resize(attrCount);
4071 }
4072 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004073 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004074 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004075}
bsalomone179a912016-01-20 06:18:10 -08004076
Greg Daniel30a35e82019-11-19 14:12:25 -05004077bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004078 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004079 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004080 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004081 // We call finish if the client told us to sync or if we have a finished proc but don't support
4082 // GLsync objects.
4083 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4084 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4085 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004086 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004087 // After a finish everything previously sent to GL is done.
4088 for (const auto& cb : fFinishCallbacks) {
4089 cb.fCallback(cb.fContext);
4090 this->deleteSync(cb.fSync);
4091 }
4092 fFinishCallbacks.clear();
4093 if (info.fFinishedProc) {
4094 info.fFinishedProc(info.fFinishedContext);
4095 }
4096 } else {
4097 if (info.fFinishedProc) {
4098 FinishCallback callback;
4099 callback.fCallback = info.fFinishedProc;
4100 callback.fContext = info.fFinishedContext;
4101 callback.fSync = (GrGLsync)this->insertFence();
4102 fFinishCallbacks.push_back(callback);
4103 GL_CALL(Flush());
4104 } else if (insertedSemaphore) {
4105 // Must call flush after semaphores in case they are waited on another GL context.
4106 GL_CALL(Flush());
4107 }
4108 // See if any previously inserted finish procs are good to go.
4109 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004110 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004111 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004112}
4113
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004114void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4115 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4116 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4117 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004118}
4119
Greg Daniel6be35232017-03-01 17:01:09 -05004120GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004121 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004122 GrGLsync sync;
4123 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Brian Salomon4dea72a2019-12-18 10:43:10 -05004124 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004125 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004126}
4127
Brian Salomonb0d8b762019-05-06 16:58:22 -04004128bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4129 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004130 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004131 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4132 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4133}
4134
4135bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4136 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004137}
4138
4139void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004140 this->deleteSync((GrGLsync)fence);
4141}
4142
Greg Daniel301015c2019-11-18 14:06:46 -05004143std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004144 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004145 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004146}
4147
Greg Daniel301015c2019-11-18 14:06:46 -05004148std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4149 const GrBackendSemaphore& semaphore,
4150 GrResourceProvider::SemaphoreWrapType wrapType,
4151 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004152 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004153 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4154}
4155
Greg Daniel301015c2019-11-18 14:06:46 -05004156void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4157 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004158
Greg Daniel48661b82018-01-22 16:11:35 -05004159 GrGLsync sync;
4160 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4161 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004162}
4163
Greg Daniel301015c2019-11-18 14:06:46 -05004164void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4165 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004166
4167 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4168}
4169
Brian Salomonb0d8b762019-05-06 16:58:22 -04004170void GrGLGpu::checkFinishProcs() {
4171 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4172 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4173 /* timeout = */ 0, /* flush = */ false)) {
4174 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4175 this->deleteSync(fFinishCallbacks.front().fSync);
4176 fFinishCallbacks.pop_front();
4177 }
4178}
4179
Greg Daniel6be35232017-03-01 17:01:09 -05004180void GrGLGpu::deleteSync(GrGLsync sync) const {
4181 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004182}
Brian Osman13dddce2017-05-09 13:19:50 -04004183
Greg Daniel301015c2019-11-18 14:06:46 -05004184std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004185 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004186 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004187 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004188 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004189 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4190 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004191
4192 return semaphore;
4193}
Robert Phillips646e4292017-06-13 12:44:56 -04004194
4195int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004196 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004197 case kTexture2DSampler_GrSLType:
4198 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004199 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004200 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004201 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004202 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004203 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004204 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004205 }
4206}
Brian Osman71a18892017-08-10 10:23:25 -04004207
Kevin Lubickf4def342018-10-04 12:52:50 -04004208#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004209#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004210void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4211 // We are called by the base class, which has already called beginObject(). We choose to nest
4212 // all of our caps information in a named sub-object.
4213 writer->beginObject("GL GPU");
4214
4215 const GrGLubyte* str;
4216 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4217 writer->appendString("GL_VERSION", (const char*)(str));
4218 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4219 writer->appendString("GL_RENDERER", (const char*)(str));
4220 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4221 writer->appendString("GL_VENDOR", (const char*)(str));
4222 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4223 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4224
4225 writer->appendName("extensions");
4226 glInterface()->fExtensions.dumpJSON(writer);
4227
4228 writer->endObject();
4229}
Kevin Lubickf4def342018-10-04 12:52:50 -04004230#endif