blob: a4084307542b59ec47e1821a14b871ee7b454c16 [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
286 void bindSampler(int unitIdx, const GrSamplerState& state) {
287 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:
335 static int StateToIndex(const GrSamplerState& state) {
336 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();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000622 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500623 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700624 fHWSRGBFramebuffer = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000625 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000626
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000627 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700628 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700629 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000630 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000631 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000632
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000633 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000634 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400635 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000636 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
637 }
Brian Salomon1047a492019-07-02 12:25:21 -0400638 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000639 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
640 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000641 if (this->glCaps().packFlipYSupport()) {
642 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
643 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000644 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000645
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000646 if (resetBits & kProgram_GrGLBackendState) {
647 fHWProgramID = 0;
Brian Salomon802cb312018-06-08 18:05:20 -0400648 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000649 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400650 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000651}
652
Brian Salomonea4ad302019-08-07 13:04:55 -0400653static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType,
654 const GrGLCaps& caps, GrGLTexture::Desc* desc,
655 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400656 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400657 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800659 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000660
Brian Salomonea4ad302019-08-07 13:04:55 -0400661 desc->fSize = {backendTex.width(), backendTex.height()};
662 desc->fTarget = info.fTarget;
663 desc->fID = info.fID;
664 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800665
Brian Salomonea4ad302019-08-07 13:04:55 -0400666 if (desc->fFormat == GrGLFormat::kUnknown) {
667 return false;
668 }
669 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400670 if (!caps.shaderCaps()->externalTextureSupport()) {
671 return false;
672 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400673 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
674 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400675 return false;
676 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400677 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400678 return false;
679 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400680 if (backendTex.isProtected()) {
681 // Not supported in GL backend at this time.
682 return false;
683 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400684
685 desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType);
686 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
687
Brian Salomond17f6582017-07-19 18:28:58 -0400688 return true;
689}
690
691sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonea4ad302019-08-07 13:04:55 -0400692 GrColorType colorType, GrWrapOwnership ownership,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400693 GrWrapCacheable cacheable, GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400694 GrGLTexture::Desc desc;
695 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400696 return nullptr;
697 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400698
Brian Salomond17f6582017-07-19 18:28:58 -0400699 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400700 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400701 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400702 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400703 }
bsalomone5286e02016-01-14 09:24:09 -0800704
Greg Daniel177e6952017-10-12 12:27:11 -0400705 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
706 : GrMipMapsStatus::kNotAllocated;
707
Brian Salomonea4ad302019-08-07 13:04:55 -0400708 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400709 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400710 // We don't know what parameters are already set on wrapped textures.
711 texture->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400712 return texture;
Brian Salomond17f6582017-07-19 18:28:58 -0400713}
714
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500715static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
716 const GrGLCaps& caps, GrGLTexture::Desc* desc,
717 bool skipRectTexSupportCheck = false) {
718 GrGLTextureInfo info;
719 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
720 return false;
721 }
722
723 desc->fSize = {backendTex.width(), backendTex.height()};
724 desc->fTarget = info.fTarget;
725 desc->fID = info.fID;
726 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
727
728 if (desc->fFormat == GrGLFormat::kUnknown) {
729 return false;
730 }
731
732 if (GR_GL_TEXTURE_2D != desc->fTarget) {
733 return false;
734 }
735 if (backendTex.isProtected()) {
736 // Not supported in GL backend at this time.
737 return false;
738 }
739
740 desc->fConfig = caps.getConfigFromCompressedBackendFormat(backendTex.getBackendFormat());
741 SkASSERT(desc->fConfig != kUnknown_GrPixelConfig);
742
743 return true;
744}
745
Robert Phillipsb915c942019-12-17 14:44:37 -0500746sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
747 GrWrapOwnership ownership,
748 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500749 GrGLTexture::Desc desc;
750 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
751 return nullptr;
752 }
753
754 if (kBorrow_GrWrapOwnership == ownership) {
755 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
756 } else {
757 desc.fOwnership = GrBackendObjectOwnership::kOwned;
758 }
759
760 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
761 : GrMipMapsStatus::kNotAllocated;
762
763 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
764 backendTex.getGLTextureParams(), cacheable,
765 kRead_GrIOType);
766 // We don't know what parameters are already set on wrapped textures.
767 texture->textureParamsModified();
768 return texture;
Robert Phillipsb915c942019-12-17 14:44:37 -0500769}
770
Brian Salomond17f6582017-07-19 18:28:58 -0400771sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400772 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -0400773 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500774 GrWrapOwnership ownership,
775 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400776 const GrGLCaps& caps = this->glCaps();
777
Brian Salomonea4ad302019-08-07 13:04:55 -0400778 GrGLTexture::Desc desc;
779 if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800780 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800781 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400782 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
783 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800784
Brian Salomond17f6582017-07-19 18:28:58 -0400785 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400786 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800787 return nullptr;
788 }
bsalomon10528f12015-10-14 12:54:52 -0700789
Brian Osman766fcbb2017-03-13 09:33:09 -0400790 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400791 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400792 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400793 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800794 }
bsalomonb15b4c12014-10-29 12:41:57 -0700795
Robert Phillips0902c982019-07-16 07:47:56 -0400796
Greg Daniel6fa62e22019-08-07 15:52:37 -0400797 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
798 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000799
Brian Salomonea4ad302019-08-07 13:04:55 -0400800 GrGLRenderTarget::IDs rtIDs;
801 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400802 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000803 }
Greg Daniel177e6952017-10-12 12:27:11 -0400804
805 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
806 : GrMipMapsStatus::kNotAllocated;
807
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500808 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400809 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400810 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400811 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400812 // We don't know what parameters are already set on wrapped textures.
813 texRT->textureParamsModified();
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400814 return texRT;
bsalomon@google.come269f212011-11-07 13:29:52 +0000815}
816
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400817sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
818 GrColorType grColorType) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400819 GrGLFramebufferInfo info;
820 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000821 return nullptr;
822 }
823
Brian Salomone8a766b2019-07-19 14:24:36 -0400824 if (backendRT.isProtected()) {
825 // Not supported in GL at this time.
826 return nullptr;
827 }
828
Brian Salomond4764a12019-08-08 12:08:24 -0400829 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400830 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
831 return nullptr;
832 }
833
Brian Salomonea4ad302019-08-07 13:04:55 -0400834 GrGLRenderTarget::IDs rtIDs;
835 rtIDs.fRTFBOID = info.fFBOID;
836 rtIDs.fMSColorRenderbufferID = 0;
837 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
838 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000839
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400840 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
841 grColorType);
Brian Salomond4764a12019-08-08 12:08:24 -0400842 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400843
Brian Salomonea4ad302019-08-07 13:04:55 -0400844 const auto size = SkISize::Make(backendRT.width(), backendRT.height());
Greg Daniel6fa62e22019-08-07 15:52:37 -0400845 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700846
Brian Salomonea4ad302019-08-07 13:04:55 -0400847 return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400848 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000849}
850
Greg Daniel7ef28f32017-04-20 16:41:55 +0000851sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400852 int sampleCnt,
Brian Salomonea4ad302019-08-07 13:04:55 -0400853 GrColorType colorType) {
854 GrGLTexture::Desc desc;
855 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
856 // with a texture rectangle,we assume the necessary support exists.
857 if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800858 return nullptr;
859 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400860
861 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
862 return nullptr;
863 }
864
865 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400866 GrGLRenderTarget::IDs rtIDs;
867 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800868 return nullptr;
869 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400870 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount,
871 rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800872}
873
Brian Salomonc320b152018-02-20 14:05:36 -0500874static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700875 if (!glTex) {
876 return false;
877 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000878
bsalomone5286e02016-01-14 09:24:09 -0800879 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
880 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800881 return false;
882 }
883
jvanverth17aa0472016-01-05 10:41:27 -0800884 return true;
885}
886
Brian Salomona9b04b92018-06-01 15:04:28 -0400887bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400888 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400889 const GrMipLevel texels[], int mipLevelCount,
890 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500891 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800892
Brian Salomonc320b152018-02-20 14:05:36 -0500893 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800894 return false;
895 }
896
Brian Salomond978b902019-02-07 15:09:18 -0500897 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000898
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400899 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon85769bf2019-08-01 15:52:34 -0400900 return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(),
Brian Salomond2a8ae22019-09-10 16:03:59 -0400901 glTex->target(), left, top, width, height, srcColorType, texels,
902 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000903}
904
Brian Salomone05ba5a2019-04-08 11:59:07 -0400905bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400906 GrColorType textureColorType, GrColorType bufferColorType,
907 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400908 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400909
Jim Van Verth1676cb92019-01-15 13:24:45 -0500910 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400911 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500912
Brian Salomonc320b152018-02-20 14:05:36 -0500913 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400914 return false;
915 }
916
Hal Canaryc36f7e72018-06-18 15:50:09 -0400917 static_assert(sizeof(int) == sizeof(int32_t), "");
918 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400919 return false;
920 }
921
Brian Salomond978b902019-02-07 15:09:18 -0500922 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400923
924 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500925 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400926 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500927 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400928
Greg Daniel660cc992017-06-26 14:55:05 -0400929 SkDEBUGCODE(
930 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
931 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
932 SkASSERT(bounds.contains(subRect));
933 )
934
Brian Salomonb28cb682019-07-26 12:48:47 -0400935 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400936 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400937 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700938 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400939 return false;
940 }
941
942 bool restoreGLRowLength = false;
943 if (trimRowBytes != rowBytes) {
944 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400945 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400946 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
947 restoreGLRowLength = true;
948 }
949
Greg Danielba88ab62019-07-26 09:14:01 -0400950 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400951 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400952 GrGLenum externalFormat = 0;
953 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400954 this->glCaps().getTexSubImageExternalFormatAndType(
955 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400956 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400957 return false;
958 }
959
Brian Salomon8cbf6622019-07-30 11:03:14 -0400960 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400961 GL_CALL(TexSubImage2D(glTex->target(),
962 0,
963 left, top,
964 width,
965 height,
966 externalFormat, externalType,
967 pixels));
968
969 if (restoreGLRowLength) {
970 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
971 }
972
973 return true;
974}
975
Brian Salomon26de56e2019-04-10 12:14:26 -0400976bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400977 GrColorType surfaceColorType, GrColorType dstColorType,
978 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400979 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
980 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400981 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400982 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
983 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400984}
985
Brian Osman9b560242017-09-05 15:34:52 -0400986void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500987 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
988 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
989 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
990 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400991 }
Brian Osman9b560242017-09-05 15:34:52 -0400992}
993
Brian Salomond2a8ae22019-09-10 16:03:59 -0400994bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth,
995 int texHeight, GrGLenum target, int left, int top, int width,
996 int height, GrColorType srcColorType, const GrMipLevel texels[],
997 int mipLevelCount, GrMipMapsStatus* mipMapsStatus) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500998 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400999 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -05001000
Greg Daniel7bfc9132019-08-14 14:23:53 -04001001 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
Greg Daniel660cc992017-06-26 14:55:05 -04001002 SkDEBUGCODE(
1003 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
1004 SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight);
1005 SkASSERT(bounds.contains(subRect));
1006 )
Robert Phillips590533f2017-07-11 14:22:35 -04001007 SkASSERT(1 == mipLevelCount ||
Greg Daniel660cc992017-06-26 14:55:05 -04001008 (0 == left && 0 == top && width == texWidth && height == texHeight));
bsalomon5b30c6f2015-12-17 14:17:34 -08001009
Brian Osman9b560242017-09-05 15:34:52 -04001010 this->unbindCpuToGpuXferBuffer();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -04001011
cblume55f2d2d2016-02-26 13:20:48 -08001012 const GrGLInterface* interface = this->glInterface();
1013 const GrGLCaps& caps = this->glCaps();
1014
Brian Salomonf77c1462019-08-01 15:19:29 -04001015 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -08001016
1017 if (width == 0 || height == 0) {
bsalomon@google.com136f55b2011-11-28 18:34:44 +00001018 return false;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001019 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001020
bsalomon5b30c6f2015-12-17 14:17:34 -08001021 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -08001022 GrGLenum externalFormat;
1023 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001024 this->glCaps().getTexSubImageExternalFormatAndType(
1025 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04001026 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08001027 return false;
1028 }
Brian Salomonf77c1462019-08-01 15:19:29 -04001029
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001030 /*
bsalomon5b30c6f2015-12-17 14:17:34 -08001031 * Check whether to allocate a temporary buffer for flipping y or
bsalomon@google.com6f379512011-11-16 20:36:03 +00001032 * because our srcData has extra bytes past each row. If so, we need
1033 * to trim those off here, since GL ES may not let us specify
1034 * GL_UNPACK_ROW_LENGTH.
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001035 */
bsalomon@google.com6f379512011-11-16 20:36:03 +00001036 bool restoreGLRowLength = false;
cblume55f2d2d2016-02-26 13:20:48 -08001037
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001038 if (mipMapsStatus) {
Chris Dalton95d8ceb2019-07-30 11:17:59 -06001039 *mipMapsStatus = (mipLevelCount > 1) ?
1040 GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
cblume55f2d2d2016-02-26 13:20:48 -08001041 }
bsalomone699d0c2016-03-09 06:25:15 -08001042
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
cblume55f2d2d2016-02-26 13:20:48 -08001044
Brian Salomond2a8ae22019-09-10 16:03:59 -04001045 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
1046 if (!texels[currentMipLevel].fPixels) {
1047 if (mipMapsStatus) {
1048 *mipMapsStatus = GrMipMapsStatus::kDirty;
Greg Daniel55afd6d2017-09-29 09:32:44 -04001049 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001050 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001051 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001052 int twoToTheMipLevel = 1 << currentMipLevel;
1053 const int currentWidth = SkTMax(1, width / twoToTheMipLevel);
1054 const int currentHeight = SkTMax(1, height / twoToTheMipLevel);
1055 const size_t trimRowBytes = currentWidth * bpp;
1056 const size_t rowBytes = texels[currentMipLevel].fRowBytes;
1057
1058 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1059 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
1060 GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
1061 restoreGLRowLength = true;
1062 }
1063
1064 GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight,
1065 externalFormat, externalType, texels[currentMipLevel].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001066 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001067 if (restoreGLRowLength) {
1068 SkASSERT(caps.writePixelsRowBytesSupport());
1069 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1070 }
1071 return true;
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001072}
1073
Greg Daniel7bfc9132019-08-14 14:23:53 -04001074bool GrGLGpu::uploadCompressedTexData(GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001075 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001076 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001077 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001078 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001080 const GrGLCaps& caps = this->glCaps();
1081
1082 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001083 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001084 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001085 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001086 }
1087
Robert Phillips9f744f72019-12-19 19:14:33 -05001088 SkImage::CompressionType compressionType = GrGLFormatToCompressionType(format);
1089 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1090
Greg Daniel7bfc9132019-08-14 14:23:53 -04001091 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001092
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001093 int numMipLevels = 1;
1094 if (mipMapped == GrMipMapped::kYes) {
1095 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
1096 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001097
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001098 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001099 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001100
1101 if (useTexStorage) {
1102 // We never resize or change formats of textures.
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001103 GL_ALLOC_CALL(this->glInterface(),
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001104 TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(),
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001105 dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001106 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1107 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001108 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001109 }
Robert Phillips42716d42019-12-16 12:19:54 -05001110
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001111 size_t offset = 0;
1112 for (int level = 0; level < numMipLevels; ++level) {
1113
Robert Phillips9f744f72019-12-19 19:14:33 -05001114 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1115 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001116
1117 GL_CALL(CompressedTexSubImage2D(target,
1118 level,
1119 0, // left
1120 0, // top
1121 dimensions.width(),
1122 dimensions.height(),
1123 internalFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001124 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001125 &((char*)data)[offset]));
1126
1127 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1128 if (error != GR_GL_NO_ERROR) {
1129 return false;
1130 }
1131
Robert Phillips9f744f72019-12-19 19:14:33 -05001132 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001133 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001134 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001135 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001136 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001137
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001138 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001139 size_t levelDataSize = GrCompressedDataSize(compressionType, dimensions,
1140 nullptr, GrMipMapped::kNo);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001141
1142 const char* rawLevelData = &((char*)data)[offset];
1143 GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target,
1144 level,
1145 internalFormat,
1146 dimensions.width(),
1147 dimensions.height(),
1148 0, // border
Robert Phillips9f744f72019-12-19 19:14:33 -05001149 SkToInt(levelDataSize),
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001150 rawLevelData));
1151
1152 GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface());
1153 if (error != GR_GL_NO_ERROR) {
1154 return false;
1155 }
1156
Robert Phillips9f744f72019-12-19 19:14:33 -05001157 offset += levelDataSize;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001158 dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001159 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001160 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001161 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001162}
1163
bsalomon424cc262015-05-22 10:37:30 -07001164static bool renderbuffer_storage_msaa(const GrGLContext& ctx,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001165 int sampleCount,
1166 GrGLenum format,
1167 int width, int height) {
robertphillips@google.com6177e692013-02-28 20:16:25 +00001168 CLEAR_ERROR_BEFORE_ALLOC(ctx.interface());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001169 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001170 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001171 case GrGLCaps::kStandard_MSFBOType:
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001172 GL_ALLOC_CALL(ctx.interface(),
1173 RenderbufferStorageMultisample(GR_GL_RENDERBUFFER,
1174 sampleCount,
1175 format,
1176 width, height));
1177 break;
1178 case GrGLCaps::kES_Apple_MSFBOType:
1179 GL_ALLOC_CALL(ctx.interface(),
1180 RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER,
1181 sampleCount,
1182 format,
1183 width, height));
1184 break;
1185 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1186 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
1187 GL_ALLOC_CALL(ctx.interface(),
1188 RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER,
1189 sampleCount,
1190 format,
1191 width, height));
1192 break;
1193 case GrGLCaps::kNone_MSFBOType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001194 SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers.");
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001195 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001196 }
Brian Salomon9251d4e2015-03-17 16:03:19 -04001197 return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001198}
1199
Brian Salomonea4ad302019-08-07 13:04:55 -04001200bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001201 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001202 GrGLRenderTarget::IDs* rtIDs) {
1203 rtIDs->fMSColorRenderbufferID = 0;
1204 rtIDs->fRTFBOID = 0;
1205 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1206 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001207
bsalomona11e5fc2015-12-18 07:59:41 -08001208 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001209
Brian Salomonea4ad302019-08-07 13:04:55 -04001210 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001211 goto FAILED;
1212 }
1213
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001214 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001215 goto FAILED;
1216 }
1217
Brian Salomonea4ad302019-08-07 13:04:55 -04001218 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1219 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220 goto FAILED;
1221 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001222
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001223 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1224 // the texture bound to the other. The exception is the IMG multisample extension. With this
1225 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1226 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001227 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1229 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1230 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001231 goto FAILED;
1232 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001233 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 }
1237
egdanield803f272015-03-18 13:01:52 -07001238 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001239 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001240 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001241 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001242 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001243 if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat,
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001245 goto FAILED;
1246 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001247 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001248 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001249 GR_GL_COLOR_ATTACHMENT0,
1250 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001251 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001252 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001254
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001255 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001256 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1257 GR_GL_COLOR_ATTACHMENT0,
1258 desc.fTarget,
1259 desc.fID,
1260 0,
1261 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001262 } else {
egdanield803f272015-03-18 13:01:52 -07001263 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001264 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001265 desc.fTarget,
1266 desc.fID,
1267 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001268 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001269
1270 return true;
1271
1272FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001273 if (rtIDs->fMSColorRenderbufferID) {
1274 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001275 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001276 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1277 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001278 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001279 if (rtIDs->fTexFBOID) {
1280 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001281 }
1282 return false;
1283}
1284
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001285// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001286static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001287// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001288 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001289}
1290
Brian Salomone2826ab2019-06-04 15:58:31 -04001291static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001292 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001293 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1294 // drivers have a bug where an FBO won't be complete if it includes a
1295 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001296 GrGLTextureParameters::SamplerOverriddenState state;
1297 state.fMinFilter = GR_GL_NEAREST;
1298 state.fMagFilter = GR_GL_NEAREST;
1299 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1300 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001301 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1302 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1303 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1304 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001305 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001306}
1307
Robert Phillips67d52cf2017-06-05 13:38:13 -04001308sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
Brian Salomon81536f22019-08-08 16:30:49 -04001309 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001310 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001311 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001312 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001313 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001314 int mipLevelCount,
1315 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001316 // We don't support protected textures in GL.
1317 if (isProtected == GrProtected::kYes) {
1318 return nullptr;
1319 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001320 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001321
Brian Salomond2a8ae22019-09-10 16:03:59 -04001322 SkASSERT(mipLevelCount > 0);
1323 GrMipMapsStatus mipMapsStatus =
1324 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001325 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001326 GrGLTexture::Desc texDesc;
1327 texDesc.fSize = {desc.fWidth, desc.fHeight};
1328 texDesc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomon81536f22019-08-08 16:30:49 -04001329 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001330 texDesc.fConfig = desc.fConfig;
1331 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001332 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1333 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001334
Brian Salomond2a8ae22019-09-10 16:03:59 -04001335 texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable,
1336 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001337
1338 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001339 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001340 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001341
Robert Phillips67d52cf2017-06-05 13:38:13 -04001342 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001343 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001344 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001345 GL_CALL(BindTexture(texDesc.fTarget, 0));
1346 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001347
Brian Salomonea4ad302019-08-07 13:04:55 -04001348 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1349 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 return return_null_texture();
1351 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001352 tex = sk_make_sp<GrGLTextureRenderTarget>(
1353 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001354 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001355 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001356 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001358 // The non-sampler params are still at their default values.
1359 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1360 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001361 if (levelClearMask) {
1362 GrGLenum externalFormat, externalType;
Brian Salomon85c3d682019-11-04 15:04:54 -05001363 GrColorType colorType;
1364 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat,
1365 &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001366 if (this->glCaps().clearTextureSupport()) {
1367 for (int i = 0; i < mipLevelCount; ++i) {
1368 if (levelClearMask & (1U << i)) {
1369 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1370 nullptr));
1371 }
1372 }
1373 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1374 !this->glCaps().performColorClearsAsDraws()) {
1375 this->disableScissor();
1376 this->disableWindowRectangles();
1377 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001378 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001379 for (int i = 0; i < mipLevelCount; ++i) {
1380 if (levelClearMask & (1U << i)) {
1381 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1382 kDst_TempFBOTarget);
1383 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1384 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1385 }
1386 }
Brian Salomonc2249852019-09-16 11:08:50 -04001387 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001388 } else {
1389 std::unique_ptr<char[]> zeros;
1390 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1391 for (int i = 0; i < mipLevelCount; ++i) {
1392 if (levelClearMask & (1U << i)) {
1393 int levelWidth = SkTMax(1, texDesc.fSize.width() >> i);
1394 int levelHeight = SkTMax(1, texDesc.fSize.height() >> i);
1395 // Levels only get smaller as we proceed. Once we create a zeros use it for all
1396 // smaller levels that need clearing.
1397 if (!zeros) {
Brian Salomon85c3d682019-11-04 15:04:54 -05001398 size_t bpp = GrColorTypeBytesPerPixel(colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001399 size_t size = levelWidth * levelHeight * bpp;
1400 zeros.reset(new char[size]());
1401 }
1402 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID());
1403 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight,
1404 externalFormat, externalType, zeros.get()));
1405 }
Brian Salomona3e29962019-07-16 11:52:08 -04001406 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001407 }
1408 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001409 return tex;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001410}
1411
Robert Phillips9f744f72019-12-19 19:14:33 -05001412sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001413 const GrBackendFormat& format,
Robert Phillips9f744f72019-12-19 19:14:33 -05001414 SkBudgeted budgeted,
1415 const void* data, size_t dataSize) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001416 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001417 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001418 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001419 desc.fTarget = GR_GL_TEXTURE_2D;
Robert Phillips9f744f72019-12-19 19:14:33 -05001420 desc.fConfig = this->glCaps().getConfigFromCompressedBackendFormat(format);
Brian Salomonea4ad302019-08-07 13:04:55 -04001421 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001422 desc.fFormat = format.asGLFormat();
Robert Phillips9f744f72019-12-19 19:14:33 -05001423 desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001424 GrMipMapped::kNo, &initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001425 data, dataSize);
Brian Salomonea4ad302019-08-07 13:04:55 -04001426 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001427 return nullptr;
1428 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001429
Robert Phillipsee946932019-12-18 11:16:17 -05001430 // Unbind this texture from the scratch texture unit.
1431 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1432
Brian Salomonea4ad302019-08-07 13:04:55 -04001433 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001434 // The non-sampler params are still at their default values.
1435 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1436 fResetTimestampForTextureParameters);
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001437 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001438}
1439
Robert Phillipsb915c942019-12-17 14:44:37 -05001440GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions,
1441 const GrBackendFormat& format,
1442 const BackendTextureData* data,
1443 GrMipMapped mipMapped,
1444 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001445 // We don't support protected textures in GL.
1446 if (isProtected == GrProtected::kYes) {
1447 return {};
1448 }
1449
1450 this->handleDirtyContext();
1451
1452 GrGLFormat glFormat = format.asGLFormat();
1453 if (glFormat == GrGLFormat::kUnknown) {
1454 return {};
1455 }
1456
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001457 const char* rawData = nullptr;
Robert Phillips9f744f72019-12-19 19:14:33 -05001458 size_t rawDataSize = 0;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001459 SkAutoMalloc am;
1460
1461 SkASSERT(!data || data->type() != BackendTextureData::Type::kPixmaps);
1462 if (data && data->type() == BackendTextureData::Type::kCompressed) {
1463 rawData = (const char*) data->compressedData();
Robert Phillips9f744f72019-12-19 19:14:33 -05001464 rawDataSize = data->compressedSize();
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001465 } else if (data && data->type() == BackendTextureData::Type::kColor) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001466 SkImage::CompressionType compression = GrGLFormatToCompressionType(glFormat);
1467 SkASSERT(compression != SkImage::CompressionType::kNone);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001468
Robert Phillips9f744f72019-12-19 19:14:33 -05001469 rawDataSize = GrCompressedDataSize(compression, dimensions, nullptr, mipMapped);
1470
1471 am.reset(rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001472
1473 GrFillInCompressedData(compression, dimensions, mipMapped, (char*)am.get(), data->color());
1474
1475 rawData = (const char*) am.get();
1476 }
1477
1478 GrGLTextureInfo info;
1479 GrGLTextureParameters::SamplerOverriddenState initialState;
1480
1481 info.fTarget = GR_GL_TEXTURE_2D;
1482 info.fFormat = GrGLFormatToEnum(glFormat);
1483 info.fID = this->createCompressedTexture2D(dimensions, glFormat,
Robert Phillips9f744f72019-12-19 19:14:33 -05001484 mipMapped, &initialState,
1485 rawData, rawDataSize);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001486 if (!info.fID) {
1487 return {};
1488 }
1489
1490 // Unbind this texture from the scratch texture unit.
1491 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1492
1493 auto parameters = sk_make_sp<GrGLTextureParameters>();
1494 // The non-sampler params are still at their default values.
1495 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1496 fResetTimestampForTextureParameters);
1497
1498 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1499 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001500}
1501
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001502namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001503
egdaniel8dc7c3a2015-04-16 11:22:42 -07001504const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001506void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001507 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001508
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001509 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001510 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 (kUnknownBitCount == format->fTotalBits));
1512 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001513 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001514 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1515 (GrGLint*)&format->fStencilBits);
1516 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001517 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1519 (GrGLint*)&format->fTotalBits);
1520 format->fTotalBits += format->fStencilBits;
1521 } else {
1522 format->fTotalBits = format->fStencilBits;
1523 }
1524 }
1525}
1526}
1527
Brian Salomon5043f1f2019-07-11 21:27:54 -04001528int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001529 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001530 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1531
1532 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001533 // Default to unsupported, set this if we find a stencil format that works.
1534 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001535
Brian Salomond2a8ae22019-09-10 16:03:59 -04001536 GrGLuint colorID =
1537 this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1);
1538 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001539 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001540 }
egdanielff1d5472015-09-10 08:37:20 -07001541 // unbind the texture from the texture unit before binding it to the frame buffer
1542 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1543
1544 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001545 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001546 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001547 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001548 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001549 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1550 GR_GL_COLOR_ATTACHMENT0,
1551 GR_GL_TEXTURE_2D,
1552 colorID,
1553 0));
bsalomon30447372015-12-21 09:03:05 -08001554 GrGLuint sbRBID = 0;
1555 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001556
1557 // look over formats till I find a compatible one
1558 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001559 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001560 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001561 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1562 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
1563 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1564 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1565 sFmt.fInternalFormat,
1566 kSize, kSize));
1567 if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) {
egdanielff1d5472015-09-10 08:37:20 -07001568 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001569 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001570 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001571 if (sFmt.fPacked) {
1572 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1573 GR_GL_DEPTH_ATTACHMENT,
1574 GR_GL_RENDERBUFFER, sbRBID));
1575 } else {
1576 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1577 GR_GL_DEPTH_ATTACHMENT,
1578 GR_GL_RENDERBUFFER, 0));
1579 }
1580 GrGLenum status;
1581 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1582 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1583 firstWorkingStencilFormatIndex = i;
1584 break;
1585 }
egdanielff1d5472015-09-10 08:37:20 -07001586 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1587 GR_GL_STENCIL_ATTACHMENT,
1588 GR_GL_RENDERBUFFER, 0));
1589 if (sFmt.fPacked) {
1590 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1591 GR_GL_DEPTH_ATTACHMENT,
1592 GR_GL_RENDERBUFFER, 0));
1593 }
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 }
bsalomon30447372015-12-21 09:03:05 -08001596 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001597 }
1598 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001599 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1600 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001601 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001602 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001603 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001604}
1605
Brian Salomonea4ad302019-08-07 13:04:55 -04001606GrGLuint GrGLGpu::createCompressedTexture2D(
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001607 const SkISize& dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001608 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001609 GrMipMapped mipMapped,
Brian Salomonea4ad302019-08-07 13:04:55 -04001610 GrGLTextureParameters::SamplerOverriddenState* initialState,
Robert Phillips9f744f72019-12-19 19:14:33 -05001611 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001612 if (format == GrGLFormat::kUnknown) {
1613 return 0;
1614 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001615 GrGLuint id = 0;
1616 GL_CALL(GenTextures(1, &id));
1617 if (!id) {
1618 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001619 }
1620
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001622
Brian Salomonea4ad302019-08-07 13:04:55 -04001623 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1624
Robert Phillips42716d42019-12-16 12:19:54 -05001625 if (data) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001626 if (!this->uploadCompressedTexData(format, dimensions, mipMapped,
1627 GR_GL_TEXTURE_2D, data, dataSize)) {
Robert Phillips42716d42019-12-16 12:19:54 -05001628 GL_CALL(DeleteTextures(1, &id));
1629 return 0;
1630 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001631 }
Robert Phillips42716d42019-12-16 12:19:54 -05001632
Brian Salomonea4ad302019-08-07 13:04:55 -04001633 return id;
1634}
1635
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001636GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -04001637 GrGLFormat format,
1638 GrRenderable renderable,
1639 GrGLTextureParameters::SamplerOverriddenState* initialState,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001640 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001641 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001642 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001643
Brian Salomonea4ad302019-08-07 13:04:55 -04001644 GrGLuint id = 0;
1645 GL_CALL(GenTextures(1, &id));
1646
1647 if (!id) {
1648 return 0;
1649 }
1650
1651 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001652
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001653 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001654 // provides a hint about how this texture will be used
Brian Salomonea4ad302019-08-07 13:04:55 -04001655 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001656 }
1657
Brian Salomond2a8ae22019-09-10 16:03:59 -04001658 if (initialState) {
1659 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1660 } else {
1661 set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
Brian Salomona7398242019-09-10 09:17:38 -04001662 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001663
1664 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1665
1666 bool success = false;
1667 if (internalFormat) {
1668 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1669 if (this->glCaps().formatSupportsTexStorage(format)) {
1670 GL_ALLOC_CALL(this->glInterface(),
1671 TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001672 dimensions.width(), dimensions.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001673 success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
1674 } else {
1675 GrGLenum externalFormat, externalType;
1676 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1677 GrGLenum error = GR_GL_NO_ERROR;
1678 if (externalFormat && externalType) {
1679 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1680 const int twoToTheMipLevel = 1 << level;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001681 const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel);
1682 const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001683 GL_ALLOC_CALL(
1684 this->glInterface(),
1685 TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth,
1686 currentHeight, 0, externalFormat, externalType, nullptr));
1687 error = CHECK_ALLOC_ERROR(this->glInterface());
1688 }
1689 success = (GR_GL_NO_ERROR == error);
1690 }
1691 }
1692 }
1693 if (success) {
1694 return id;
1695 }
1696 GL_CALL(DeleteTextures(1, &id));
1697 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001698}
1699
Chris Daltoneffee202019-07-01 22:28:03 -06001700GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1701 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001702 SkASSERT(width >= rt->width());
1703 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001704
egdaniel8dc7c3a2015-04-16 11:22:42 -07001705 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001706
Brian Salomond4764a12019-08-08 12:08:24 -04001707 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001708 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001709 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001710 }
egdanielff1d5472015-09-10 08:37:20 -07001711
1712 if (!sbDesc.fRenderbufferID) {
1713 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1714 }
1715 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001716 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001717 }
1718 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1719 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
1720 CLEAR_ERROR_BEFORE_ALLOC(this->glInterface());
1721 // we do this "if" so that we don't call the multisample
1722 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001723 if (numStencilSamples > 1) {
egdanielff1d5472015-09-10 08:37:20 -07001724 SkAssertResult(renderbuffer_storage_msaa(*fGLContext,
Chris Daltoneffee202019-07-01 22:28:03 -06001725 numStencilSamples,
egdanielff1d5472015-09-10 08:37:20 -07001726 sFmt.fInternalFormat,
1727 width, height));
1728 } else {
1729 GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER,
1730 sFmt.fInternalFormat,
1731 width, height));
Brian Salomon0ec981b2017-05-15 13:48:50 -04001732 SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface()));
egdanielff1d5472015-09-10 08:37:20 -07001733 }
1734 fStats.incStencilAttachmentCreates();
1735 // After sized formats we attempt an unsized format and take
1736 // whatever sizes GL gives us. In that case we query for the size.
1737 GrGLStencilAttachment::Format format = sFmt;
1738 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001739 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1740 sbDesc,
1741 width,
1742 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001743 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001744 format);
1745 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001746}
1747
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001748////////////////////////////////////////////////////////////////////////////////
1749
Brian Salomondbf70722019-02-07 11:31:24 -05001750sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1751 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001752 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001753}
1754
Greg Danielacd66b42019-05-22 16:29:12 -04001755void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight,
Brian Salomond818ebf2018-07-02 14:08:49 +00001756 GrSurfaceOrigin rtOrigin) {
1757 if (scissorState.enabled()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06001758 auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect());
Brian Salomond818ebf2018-07-02 14:08:49 +00001759 // if the scissor fully contains the viewport then we fall through and
1760 // disable the scissor test.
Greg Danielacd66b42019-05-22 16:29:12 -04001761 if (!scissor.contains(rtWidth, rtHeight)) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001762 if (fHWScissorSettings.fRect != scissor) {
Chris Dalton76500e52019-09-05 02:13:05 -06001763 GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight));
Brian Salomond818ebf2018-07-02 14:08:49 +00001764 fHWScissorSettings.fRect = scissor;
1765 }
1766 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1767 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1768 fHWScissorSettings.fEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00001769 }
1770 return;
1771 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 }
Brian Salomond818ebf2018-07-02 14:08:49 +00001773
1774 // See fall through note above
1775 this->disableScissor();
joshualitt77b13072014-10-27 14:51:01 -07001776}
1777
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001778void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001779 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001780#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001781 typedef GrWindowRectsState::Mode Mode;
1782 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1783 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001784
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001785 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001786 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001787 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001788 }
1789
csmartdalton7535f412016-08-23 06:51:00 -07001790 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1791 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001792 int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
1793 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001794
Chris Daltond6cda8d2019-09-05 02:30:04 -06001795 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001796 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001797 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001798 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001799 }
1800
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001801 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001802 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001803
Greg Danielacd66b42019-05-22 16:29:12 -04001804 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001805#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001806}
1807
1808void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001809#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001810 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001811 return;
1812 }
1813 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001814 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001815#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001816}
1817
Robert Phillipsfcaae482019-11-07 10:17:03 -05001818bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Greg Daniel9a51a862018-11-30 10:18:14 -05001819
Robert Phillipsfcaae482019-11-07 10:17:03 -05001820 sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo));
Greg Daniel9a51a862018-11-30 10:18:14 -05001821 if (!program) {
1822 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1823 return false;
1824 }
brianosman33f6b3f2016-06-02 05:49:21 -07001825
Brian Salomon802cb312018-06-08 18:05:20 -04001826 this->flushProgram(std::move(program));
bsalomon1f78c0a2014-12-17 09:43:13 -08001827
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001828 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001829 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
1830 programInfo.pipeline().outputSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001831
Robert Phillips901aff02019-10-08 12:32:56 -04001832 fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001833
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001834 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001835 GrStencilSettings stencil;
1836 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001837 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1838 stencil.reset(*programInfo.pipeline().getUserStencil(),
1839 programInfo.pipeline().hasStencilClip(),
1840 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001841 }
Robert Phillips901aff02019-10-08 12:32:56 -04001842 this->flushStencil(stencil, programInfo.origin());
1843 if (programInfo.pipeline().isScissorEnabled()) {
Brian Salomond818ebf2018-07-02 14:08:49 +00001844 static constexpr SkIRect kBogusScissor{0, 0, 1, 1};
Robert Phillips901aff02019-10-08 12:32:56 -04001845 GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor()
1846 : kBogusScissor);
1847 this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin());
Brian Salomond818ebf2018-07-02 14:08:49 +00001848 } else {
1849 this->disableScissor();
Brian Salomon49348902018-06-26 09:12:38 -04001850 }
Robert Phillips901aff02019-10-08 12:32:56 -04001851 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1852 glRT, programInfo.origin());
1853 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001854 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001855 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001856
1857 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001858 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001859 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001860
1861 return true;
1862}
1863
Brian Salomon802cb312018-06-08 18:05:20 -04001864void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1865 if (!program) {
1866 fHWProgram.reset();
1867 fHWProgramID = 0;
1868 return;
1869 }
1870 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1871 if (program == fHWProgram) {
1872 return;
1873 }
1874 auto id = program->programID();
1875 SkASSERT(id);
1876 GL_CALL(UseProgram(id));
1877 fHWProgram = std::move(program);
1878 fHWProgramID = id;
1879}
1880
1881void GrGLGpu::flushProgram(GrGLuint id) {
1882 SkASSERT(id);
1883 if (fHWProgramID == id) {
1884 SkASSERT(!fHWProgram);
1885 return;
1886 }
1887 fHWProgram.reset();
1888 GL_CALL(UseProgram(id));
1889 fHWProgramID = id;
1890}
1891
1892void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
Chris Daltonff926502017-05-03 14:36:54 -04001893 const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06001894 int baseVertex,
1895 const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04001896 int baseInstance,
1897 GrPrimitiveRestart enablePrimitiveRestart) {
1898 SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
Chris Dalton27059d32018-01-23 14:06:50 -07001899
cdaltone2e71c22016-04-07 18:13:29 -07001900 GrGLAttribArrayState* attribState;
Chris Daltonff926502017-05-03 14:36:54 -04001901 if (indexBuffer) {
Brian Salomondbf70722019-02-07 11:31:24 -05001902 SkASSERT(indexBuffer->isCpuBuffer() ||
1903 !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
Chris Daltonff926502017-05-03 14:36:54 -04001904 attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
cdaltone2e71c22016-04-07 18:13:29 -07001905 } else {
1906 attribState = fHWVertexArrayState.bindInternalVertexArray(this);
bsalomonbc3d0de2014-12-15 13:45:03 -08001907 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001908
Brian Salomon92be2f72018-06-19 14:33:47 -04001909 int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1910 attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04001911
Brian Salomon802cb312018-06-08 18:05:20 -04001912 if (int vertexStride = fHWProgram->vertexStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001913 SkASSERT(vertexBuffer);
1914 SkASSERT(vertexBuffer->isCpuBuffer() ||
1915 !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1916 size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001917 for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1918 const auto& attrib = fHWProgram->vertexAttribute(i);
1919 static constexpr int kDivisor = 0;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001920 attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1921 vertexStride, bufferOffset + attrib.fOffset, kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001922 }
Chris Dalton1d616352017-05-31 12:51:23 -06001923 }
Brian Salomon802cb312018-06-08 18:05:20 -04001924 if (int instanceStride = fHWProgram->instanceStride()) {
Brian Salomondbf70722019-02-07 11:31:24 -05001925 SkASSERT(instanceBuffer);
1926 SkASSERT(instanceBuffer->isCpuBuffer() ||
1927 !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1928 size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
Brian Salomon92be2f72018-06-19 14:33:47 -04001929 int attribIdx = fHWProgram->numVertexAttributes();
1930 for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1931 const auto& attrib = fHWProgram->instanceAttribute(i);
1932 static constexpr int kDivisor = 1;
Brian Osman4a3f5c82018-09-18 16:16:38 -04001933 attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1934 attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1935 kDivisor);
Brian Salomon92be2f72018-06-19 14:33:47 -04001936 }
bsalomonbc3d0de2014-12-15 13:45:03 -08001937 }
1938}
1939
Brian Salomonae64c192019-02-05 09:41:37 -05001940GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001941 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001942
cdaltone2e71c22016-04-07 18:13:29 -07001943 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001944 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001945 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001946 }
cdaltone2e71c22016-04-07 18:13:29 -07001947
Brian Salomonae64c192019-02-05 09:41:37 -05001948 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001949 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001950 if (!bufferState->fBufferZeroKnownBound) {
1951 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1952 bufferState->fBufferZeroKnownBound = true;
1953 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001954 }
Brian Salomondbf70722019-02-07 11:31:24 -05001955 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1956 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001957 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1958 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1959 bufferState->fBufferZeroKnownBound = false;
1960 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001961 }
1962
Brian Salomonae64c192019-02-05 09:41:37 -05001963 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001964}
Brian Salomond818ebf2018-07-02 14:08:49 +00001965void GrGLGpu::disableScissor() {
1966 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1967 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1968 fHWScissorSettings.fEnabled = kNo_TriState;
1969 return;
1970 }
1971}
1972
Brian Osman9a9baae2018-11-05 15:06:26 -05001973void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001974 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001975 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001976 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001977 SkASSERT(!this->caps()->performColorClearsAsDraws());
1978 SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001979
Brian Salomon43f8bf02017-10-18 08:33:29 -04001980 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001981
Brian Salomon43f8bf02017-10-18 08:33:29 -04001982 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1983
Brian Salomond818ebf2018-07-02 14:08:49 +00001984 if (clip.scissorEnabled()) {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001985 this->flushRenderTarget(glRT, origin, clip.scissorRect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001986 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001987 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001988 }
Greg Danielacd66b42019-05-22 16:29:12 -04001989 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Brian Salomon43f8bf02017-10-18 08:33:29 -04001990 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
Brian Salomon805cc7a2019-01-28 09:52:34 -05001991 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001992 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001993 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001994}
1995
Robert Phillips95214472017-08-08 18:00:03 -04001996void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001997 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1998
Robert Phillips95214472017-08-08 18:00:03 -04001999 if (!target) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +00002000 return;
2001 }
Robert Phillipscb2e2352017-08-30 16:44:40 -04002002
Chris Dalton674f77a2019-09-30 20:49:39 -06002003 // This should only be called internally when we know we have a stencil buffer.
2004 SkASSERT(target->renderTargetPriv().getStencilAttachment());
Robert Phillipscb2e2352017-08-30 16:44:40 -04002005
bsalomonb0bd4f62014-09-03 07:19:50 -07002006 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002007 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00002008
Brian Salomond818ebf2018-07-02 14:08:49 +00002009 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07002010 this->disableWindowRectangles();
robertphillips@google.com730ebe52012-04-16 16:33:13 +00002011
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002012 GL_CALL(StencilMask(0xffffffff));
Robert Phillips95214472017-08-08 18:00:03 -04002013 GL_CALL(ClearStencil(clearValue));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002014 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002015 fHWStencilSettings.invalidate();
Chris Dalton674f77a2019-09-30 20:49:39 -06002016}
2017
Chris Daltonb50cc812019-10-14 16:29:21 -06002018static bool use_tiled_rendering(const GrGLCaps& glCaps,
2019 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2020 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
2021 // Otherwise it's faster to just not use it.
2022 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
2023 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
2024}
2025
2026void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06002027 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2028 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2029 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
2030
2031 this->handleDirtyContext();
2032
2033 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2034 this->flushRenderTarget(glRT);
2035 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
2036
Chris Daltonb50cc812019-10-14 16:29:21 -06002037 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2038 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
2039 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
2040 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2041 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
2042 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
2043 nativeBounds.fHeight, preserveMask));
2044 }
2045
Chris Dalton674f77a2019-09-30 20:49:39 -06002046 GrGLbitfield clearMask = 0;
2047 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
2048 SkASSERT(!this->caps()->performColorClearsAsDraws());
2049 this->flushClearColor(colorLoadStore.fClearColor);
2050 this->flushColorWrite(true);
2051 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04002052 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002053 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
2054 SkASSERT(!this->caps()->performStencilClearsAsDraws());
2055 GL_CALL(StencilMask(0xffffffff));
2056 GL_CALL(ClearStencil(0));
2057 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
2058 }
2059 if (clearMask) {
2060 this->disableScissor();
2061 this->disableWindowRectangles();
2062 GL_CALL(Clear(clearMask));
2063 }
2064}
2065
2066void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2067 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2068 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2069 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2070
2071 this->handleDirtyContext();
2072
2073 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2074 // The framebuffer binding changed in the middle of a command buffer. We should have already
2075 // printed a warning during onFBOChanged.
2076 return;
2077 }
2078
2079 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2080 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2081
2082 SkSTArray<2, GrGLenum> discardAttachments;
2083 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2084 discardAttachments.push_back(
2085 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2086 }
2087 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2088 discardAttachments.push_back(
2089 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2090 }
2091
2092 if (!discardAttachments.empty()) {
2093 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2094 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2095 discardAttachments.begin()));
2096 } else {
2097 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2098 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2099 discardAttachments.begin()));
2100 }
2101 }
2102 }
2103
Chris Daltonb50cc812019-10-14 16:29:21 -06002104 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2105 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2106 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2107 // Handled by use_tiled_rendering().
2108 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2109 GL_CALL(EndTiling(preserveMask));
2110 }
2111
Chris Dalton674f77a2019-09-30 20:49:39 -06002112 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002113}
2114
csmartdalton29df7602016-08-31 11:55:52 -07002115void GrGLGpu::clearStencilClip(const GrFixedClip& clip,
2116 bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002117 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002118 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002119 SkASSERT(!this->caps()->performStencilClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002120 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002121
egdaniel8dc7c3a2015-04-16 11:22:42 -07002122 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002123 // this should only be called internally when we know we have a
2124 // stencil buffer.
bsalomon6bc1b5f2015-02-23 09:06:38 -08002125 SkASSERT(sb);
2126 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002127#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002128 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002129 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002130#else
2131 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002132 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002133 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002134 // changing the clip between stencil passes may or may not
2135 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002136 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002137#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002138 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002139 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002140 value = (1 << (stencilBitCount - 1));
2141 } else {
2142 value = 0;
2143 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002144 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002145 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002146
Greg Danielacd66b42019-05-22 16:29:12 -04002147 this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin);
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002148 this->flushWindowRectangles(clip.windowRectsState(), glRT, origin);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002149
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002150 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002151 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002152 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002153 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002154}
2155
Brian Salomone05ba5a2019-04-08 11:59:07 -04002156bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002157 GrColorType surfaceColorType, GrColorType dstColorType,
2158 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002159 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002160
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002161 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002162 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002163 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002164 return false;
2165 }
Greg Danielba88ab62019-07-26 09:14:01 -04002166 GrGLenum externalFormat = 0;
2167 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002168 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2169 surfaceColorType,
2170 dstColorType,
2171 &externalFormat,
2172 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002173 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002174 return false;
2175 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002176
Brian Salomon71d9d842016-11-03 13:42:00 -04002177 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002178 if (renderTarget->numSamples() <= 1 ||
2179 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2180 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2181 this->flushRenderTargetNoColorWrites(renderTarget);
2182 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2183 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2184 return false;
2185 } else {
2186 SkASSERT(renderTarget->requiresManualMSAAResolve());
2187 // we don't track the state of the READ FBO ID.
2188 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002189 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002190 } else {
2191 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002192 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002193 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002194 }
2195
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002196 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002197 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002198
Brian Salomona6948702018-06-01 15:33:20 -04002199 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002200 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002201 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002202 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002203 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002204 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002205
Brian Osman1348ed02018-09-12 09:08:39 -04002206 bool reattachStencil = false;
2207 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2208 renderTarget &&
2209 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002210 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002211 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2212 reattachStencil = true;
2213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2214 GR_GL_RENDERBUFFER, 0));
2215 }
2216
Chris Dalton76500e52019-09-05 02:13:05 -06002217 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002218 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002219
2220 if (reattachStencil) {
2221 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2222 renderTarget->renderTargetPriv().getStencilAttachment());
2223 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2224 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2225 }
2226
Brian Salomon26de56e2019-04-10 12:14:26 -04002227 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002228 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002229 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2230 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002231
Brian Salomone05ba5a2019-04-08 11:59:07 -04002232 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002233 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002234 }
2235 return true;
2236}
2237
2238bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002239 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2240 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002241 SkASSERT(surface);
2242
Brian Salomonb28cb682019-07-26 12:48:47 -04002243 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002244
Brian Salomon26de56e2019-04-10 12:14:26 -04002245 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2246 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002247
Brian Salomon1047a492019-07-02 12:25:21 -04002248 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002249 rowPixelWidth = width;
2250 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002251 SkASSERT(!(rowBytes % bytesPerPixel));
2252 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002253 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002254 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2255 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002256}
2257
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002258GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Greg Daniel4a0d36d2019-09-30 12:24:36 -04002259 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002260 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002261 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002262 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002263 if (!fCachedOpsRenderPass) {
2264 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002265 }
2266
Chris Daltonb50cc812019-10-14 16:29:21 -06002267 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002268 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002269}
2270
Brian Salomon1fabd512018-02-09 09:54:25 -05002271void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002272 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002273 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002274 this->didWriteToSurface(target, origin, &bounds);
2275}
bsalomon6ba6fa12015-03-04 11:57:37 -08002276
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002277void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2278 this->flushRenderTargetNoColorWrites(target);
2279 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002280}
2281
Brian Osman9aa30c62018-07-02 15:21:46 -04002282void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002283 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002284 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002285 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002286 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002287#ifdef SK_DEBUG
2288 // don't do this check in Chromium -- this is causing
2289 // lots of repeated command buffer flushes when the compositor is
2290 // rendering with Ganesh, which is really slow; even too slow for
2291 // Debug mode.
cdalton1acea862015-06-02 13:05:52 -07002292 if (kChromium_GrGLDriver != this->glContext().driver()) {
egdanield803f272015-03-18 13:01:52 -07002293 GrGLenum status;
2294 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2295 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2296 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2297 }
bsalomon160f24c2015-03-17 15:55:42 -07002298 }
egdanield803f272015-03-18 13:01:52 -07002299#endif
2300 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002301 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002302 }
2303
brianosman35b784d2016-05-05 11:52:53 -07002304 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002305 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002306 }
bsalomon083617b2016-02-12 12:10:14 -08002307}
bsalomona9909122016-01-23 10:41:40 -08002308
brianosman33f6b3f2016-06-02 05:49:21 -07002309void GrGLGpu::flushFramebufferSRGB(bool enable) {
2310 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2311 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2312 fHWSRGBFramebuffer = kYes_TriState;
2313 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2314 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2315 fHWSRGBFramebuffer = kNo_TriState;
2316 }
2317}
2318
Greg Danielacd66b42019-05-22 16:29:12 -04002319void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002320 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002321 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002322 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002323 fHWViewport = viewport;
2324 }
2325}
2326
bsalomon@google.comd302f142011-03-03 13:54:13 +00002327#define SWAP_PER_DRAW 0
2328
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00002329#if SWAP_PER_DRAW
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002330 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002331 #include <AGL/agl.h>
Mike Klein8f11d4d2018-01-24 12:42:55 -05002332 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comce7357d2012-06-25 17:49:25 +00002333 #include <gl/GL.h>
bsalomon@google.comd302f142011-03-03 13:54:13 +00002334 void SwapBuf() {
2335 DWORD procID = GetCurrentProcessId();
2336 HWND hwnd = GetTopWindow(GetDesktopWindow());
2337 while(hwnd) {
2338 DWORD wndProcID = 0;
2339 GetWindowThreadProcessId(hwnd, &wndProcID);
2340 if(wndProcID == procID) {
2341 SwapBuffers(GetDC(hwnd));
2342 }
2343 hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
2344 }
2345 }
2346 #endif
2347#endif
2348
Robert Phillips901aff02019-10-08 12:32:56 -04002349void GrGLGpu::draw(GrRenderTarget* renderTarget,
2350 const GrProgramInfo& programInfo,
bsalomon2eda5b32016-09-21 10:53:24 -07002351 const GrMesh meshes[],
egdaniel9cb63402016-06-23 08:37:05 -07002352 int meshCount) {
2353 this->handleDirtyContext();
2354
Robert Phillips2579de42019-10-09 09:51:59 -04002355 SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw
Robert Phillips901aff02019-10-08 12:32:56 -04002356
Robert Phillipsfcaae482019-11-07 10:17:03 -05002357 if (!this->flushGLState(renderTarget, programInfo)) {
bsalomond95263c2014-12-16 13:05:12 -08002358 return;
2359 }
ethannicholas22793252016-01-30 09:59:10 -08002360
Robert Phillips901aff02019-10-08 12:32:56 -04002361 bool hasDynamicScissors = programInfo.hasDynamicScissors();
2362 bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures();
2363
Brian Salomonf7232642018-09-19 08:58:08 -04002364 for (int m = 0; m < meshCount; ++m) {
Robert Phillipsfcaae482019-11-07 10:17:03 -05002365 SkASSERT(meshes[m].primitiveType() == programInfo.primitiveType());
Robert Phillips6e54a292019-11-05 15:42:16 -05002366
Robert Phillips901aff02019-10-08 12:32:56 -04002367 if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(),
2368 *this->caps())) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002369 this->xferBarrier(renderTarget, barrierType);
egdaniel0e1853c2016-03-17 11:35:45 -07002370 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002371
Robert Phillips901aff02019-10-08 12:32:56 -04002372 if (hasDynamicScissors) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -05002373 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips901aff02019-10-08 12:32:56 -04002374 this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)),
2375 glRT->width(), glRT->height(), programInfo.origin());
Chris Dalton46983b72017-06-06 12:27:16 -06002376 }
Robert Phillips901aff02019-10-08 12:32:56 -04002377 if (hasDynamicPrimProcTextures) {
2378 auto texProxyArray = programInfo.dynamicPrimProcTextures(m);
2379 fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(),
2380 texProxyArray);
Brian Salomonf7232642018-09-19 08:58:08 -04002381 }
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002382 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
Brian Salomonf7232642018-09-19 08:58:08 -04002383 GrIsPrimTypeLines(meshes[m].primitiveType()) &&
Brian Salomon6d9c88b2017-06-12 10:24:42 -04002384 !GrIsPrimTypeLines(fLastPrimitiveType)) {
2385 GL_CALL(Enable(GR_GL_CULL_FACE));
2386 GL_CALL(Disable(GR_GL_CULL_FACE));
2387 }
Brian Salomonf7232642018-09-19 08:58:08 -04002388 meshes[m].sendToGpu(this);
2389 fLastPrimitiveType = meshes[m].primitiveType();
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002390 }
ethannicholas22793252016-01-30 09:59:10 -08002391
bsalomon@google.comd302f142011-03-03 13:54:13 +00002392#if SWAP_PER_DRAW
2393 glFlush();
commit-bot@chromium.org43823302013-09-25 20:57:51 +00002394 #if defined(SK_BUILD_FOR_MAC)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002395 aglSwapBuffers(aglGetCurrentContext());
2396 int set_a_break_pt_here = 9;
2397 aglSwapBuffers(aglGetCurrentContext());
Mike Klein8f11d4d2018-01-24 12:42:55 -05002398 #elif defined(SK_BUILD_FOR_WIN)
bsalomon@google.comd302f142011-03-03 13:54:13 +00002399 SwapBuf();
2400 int set_a_break_pt_here = 9;
2401 SwapBuf();
2402 #endif
2403#endif
reed@google.comac10a2d2010-12-22 21:39:39 +00002404}
2405
Chris Dalton3809bab2017-06-13 10:55:06 -06002406static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) {
2407 switch (primitiveType) {
2408 case GrPrimitiveType::kTriangles:
2409 return GR_GL_TRIANGLES;
2410 case GrPrimitiveType::kTriangleStrip:
2411 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002412 case GrPrimitiveType::kPoints:
2413 return GR_GL_POINTS;
2414 case GrPrimitiveType::kLines:
2415 return GR_GL_LINES;
2416 case GrPrimitiveType::kLineStrip:
2417 return GR_GL_LINE_STRIP;
Robert Phillips571177f2019-10-04 14:41:49 -04002418 case GrPrimitiveType::kPath:
2419 SK_ABORT("non-mesh-based GrPrimitiveType");
2420 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002421 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002422 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002423}
2424
Brian Salomon802cb312018-06-08 18:05:20 -04002425void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
2426 int vertexCount, int baseVertex) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002427 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton114a3c02017-05-26 15:17:19 -06002428 if (this->glCaps().drawArraysBaseVertexIsBroken()) {
Brian Salomon802cb312018-06-08 18:05:20 -04002429 this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002430 GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2431 } else {
Brian Salomon802cb312018-06-08 18:05:20 -04002432 this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -06002433 GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2434 }
2435 fStats.incNumDraws();
2436}
2437
Brian Salomondbf70722019-02-07 11:31:24 -05002438static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2439 size_t baseOffset = baseIndex * sizeof(uint16_t);
2440 if (indexBuffer->isCpuBuffer()) {
2441 return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2442 } else {
2443 return reinterpret_cast<const GrGLvoid*>(baseOffset);
2444 }
2445}
2446
Brian Salomon802cb312018-06-08 18:05:20 -04002447void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
Chris Dalton114a3c02017-05-26 15:17:19 -06002448 int indexCount, int baseIndex, uint16_t minIndexValue,
2449 uint16_t maxIndexValue, const GrBuffer* vertexBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002450 int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002451 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002452 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton114a3c02017-05-26 15:17:19 -06002453
Brian Salomon802cb312018-06-08 18:05:20 -04002454 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart);
Chris Dalton114a3c02017-05-26 15:17:19 -06002455
2456 if (this->glCaps().drawRangeElementsSupport()) {
2457 GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
Brian Salomondbf70722019-02-07 11:31:24 -05002458 GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002459 } else {
Brian Salomondbf70722019-02-07 11:31:24 -05002460 GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
Chris Dalton114a3c02017-05-26 15:17:19 -06002461 }
2462 fStats.incNumDraws();
2463}
2464
Brian Salomon802cb312018-06-08 18:05:20 -04002465void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer,
Chris Dalton1d616352017-05-31 12:51:23 -06002466 int vertexCount, int baseVertex,
2467 const GrBuffer* instanceBuffer, int instanceCount,
2468 int baseInstance) {
Chris Daltoncc604e52017-10-06 16:27:32 -06002469 GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002470 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
Chris Daltoncc604e52017-10-06 16:27:32 -06002471 for (int i = 0; i < instanceCount; i += maxInstances) {
Brian Salomon802cb312018-06-08 18:05:20 -04002472 this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2473 GrPrimitiveRestart::kNo);
Chris Daltoncc604e52017-10-06 16:27:32 -06002474 GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2475 SkTMin(instanceCount - i, maxInstances)));
2476 fStats.incNumDraws();
2477 }
Chris Dalton1d616352017-05-31 12:51:23 -06002478}
2479
Brian Salomon802cb312018-06-08 18:05:20 -04002480void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType,
Chris Dalton1d616352017-05-31 12:51:23 -06002481 const GrBuffer* indexBuffer, int indexCount,
2482 int baseIndex, const GrBuffer* vertexBuffer,
2483 int baseVertex, const GrBuffer* instanceBuffer,
Brian Salomon802cb312018-06-08 18:05:20 -04002484 int instanceCount, int baseInstance,
2485 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton3809bab2017-06-13 10:55:06 -06002486 const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType);
Brian Salomondbf70722019-02-07 11:31:24 -05002487 const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
Chris Dalton1b4ad762018-10-04 11:58:09 -06002488 int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2489 for (int i = 0; i < instanceCount; i += maxInstances) {
2490 this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i,
2491 enablePrimitiveRestart);
Brian Salomondbf70722019-02-07 11:31:24 -05002492 GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
Chris Dalton1b4ad762018-10-04 11:58:09 -06002493 SkTMin(instanceCount - i, maxInstances)));
2494 fStats.incNumDraws();
2495 }
Chris Dalton1d616352017-05-31 12:51:23 -06002496}
2497
Chris Dalton16a33c62019-09-24 22:19:17 -06002498void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
2499 GrSurfaceOrigin resolveOrigin, ForExternalIO) {
Chris Daltonc139d082019-09-26 14:04:24 -06002500 // Some extensions automatically resolves the texture when it is read.
2501 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2502
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002503 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002504 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2505 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2506 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2507 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002508
Chris Daltonc139d082019-09-26 14:04:24 -06002509 // make sure we go through flushRenderTarget() since we've modified
2510 // the bound DRAW FBO ID.
2511 fHWBoundRenderTargetUniqueID.makeInvalid();
2512 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2513 // Apple's extension uses the scissor as the blit bounds.
2514 GrScissorState scissorState;
2515 scissorState.set(resolveRect);
2516 this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin);
2517 this->disableWindowRectangles();
2518 GL_CALL(ResolveMultisampleFramebuffer());
2519 } else {
2520 int l, b, r, t;
2521 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2522 this->glCaps().blitFramebufferSupportFlags()) {
2523 l = 0;
2524 b = 0;
2525 r = target->width();
2526 t = target->height();
2527 } else {
2528 auto rect = GrNativeRect::MakeRelativeTo(
2529 resolveOrigin, rt->height(), resolveRect);
2530 l = rect.fX;
2531 b = rect.fY;
2532 r = rect.fX + rect.fWidth;
2533 t = rect.fY + rect.fHeight;
reed@google.comac10a2d2010-12-22 21:39:39 +00002534 }
Chris Daltonc139d082019-09-26 14:04:24 -06002535
2536 // BlitFrameBuffer respects the scissor, so disable it.
2537 this->disableScissor();
2538 this->disableWindowRectangles();
2539 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 +00002540 }
2541}
2542
bsalomon@google.com411dad02012-06-05 20:24:20 +00002543namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002544
bsalomon@google.com411dad02012-06-05 20:24:20 +00002545
2546GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002547 static const GrGLenum gTable[kGrStencilOpCount] = {
2548 GR_GL_KEEP, // kKeep
2549 GR_GL_ZERO, // kZero
2550 GR_GL_REPLACE, // kReplace
2551 GR_GL_INVERT, // kInvert
2552 GR_GL_INCR_WRAP, // kIncWrap
2553 GR_GL_DECR_WRAP, // kDecWrap
2554 GR_GL_INCR, // kIncClamp
2555 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002556 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002557 static_assert(0 == (int)GrStencilOp::kKeep);
2558 static_assert(1 == (int)GrStencilOp::kZero);
2559 static_assert(2 == (int)GrStencilOp::kReplace);
2560 static_assert(3 == (int)GrStencilOp::kInvert);
2561 static_assert(4 == (int)GrStencilOp::kIncWrap);
2562 static_assert(5 == (int)GrStencilOp::kDecWrap);
2563 static_assert(6 == (int)GrStencilOp::kIncClamp);
2564 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002565 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2566 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002567}
2568
2569void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002570 const GrStencilSettings::Face& face,
2571 GrGLenum glFace) {
2572 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2573 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2574 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002575
cdalton93a379b2016-05-11 13:58:08 -07002576 GrGLint ref = face.fRef;
2577 GrGLint mask = face.fTestMask;
2578 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002579
2580 if (GR_GL_FRONT_AND_BACK == glFace) {
2581 // we call the combined func just in case separate stencil is not
2582 // supported.
2583 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2584 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002585 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002586 } else {
2587 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2588 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002589 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002590 }
2591}
2592}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002593
Chris Dalton71713f62019-04-18 12:41:03 -06002594void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002595 if (stencilSettings.isDisabled()) {
2596 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002597 } else if (fHWStencilSettings != stencilSettings ||
2598 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002599 if (kYes_TriState != fHWStencilTestEnabled) {
2600 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002601
csmartdaltonc7d85332016-10-26 10:13:46 -07002602 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002603 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002604 if (!stencilSettings.isTwoSided()) {
2605 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2606 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002607 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002608 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2609 GR_GL_FRONT);
2610 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2611 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002612 }
joshualitta58fe352014-10-27 08:39:00 -07002613 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002614 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002615 }
2616}
2617
csmartdaltonc7d85332016-10-26 10:13:46 -07002618void GrGLGpu::disableStencil() {
2619 if (kNo_TriState != fHWStencilTestEnabled) {
2620 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002621
csmartdaltonc7d85332016-10-26 10:13:46 -07002622 fHWStencilTestEnabled = kNo_TriState;
2623 fHWStencilSettings.invalidate();
2624 }
2625}
2626
Chris Dalton4c56b032019-03-04 12:28:42 -07002627void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002628 // rt is only optional if useHWAA is false.
2629 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002630#ifdef SK_DEBUG
2631 if (useHWAA && rt->numSamples() <= 1) {
2632 SkASSERT(this->caps()->mixedSamplesSupport());
2633 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2634 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2635 }
2636#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002637
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002638 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002639 if (useHWAA) {
2640 if (kYes_TriState != fMSAAEnabled) {
2641 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2642 fMSAAEnabled = kYes_TriState;
2643 }
2644 } else {
2645 if (kNo_TriState != fMSAAEnabled) {
2646 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2647 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002648 }
2649 }
2650 }
2651}
2652
Chris Daltonce425af2019-12-16 10:39:03 -07002653void GrGLGpu::flushConservativeRasterState(bool enabled) {
2654 if (this->caps()->conservativeRasterSupport()) {
2655 if (enabled) {
2656 if (kYes_TriState != fHWConservativeRasterEnabled) {
2657 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2658 fHWConservativeRasterEnabled = kYes_TriState;
2659 }
2660 } else {
2661 if (kNo_TriState != fHWConservativeRasterEnabled) {
2662 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2663 fHWConservativeRasterEnabled = kNo_TriState;
2664 }
2665 }
2666 }
2667}
2668
Chris Dalton1215cda2019-12-17 21:44:04 -07002669void GrGLGpu::flushWireframeState(bool enabled) {
2670 if (this->caps()->wireframeSupport()) {
2671 if (this->caps()->wireframeMode() || enabled) {
2672 if (kYes_TriState != fHWWireframeEnabled) {
2673 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2674 fHWWireframeEnabled = kYes_TriState;
2675 }
2676 } else {
2677 if (kNo_TriState != fHWWireframeEnabled) {
2678 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2679 fHWWireframeEnabled = kNo_TriState;
2680 }
2681 }
2682 }
2683}
2684
Chris Daltonbbb3f642019-07-24 12:25:08 -04002685void GrGLGpu::flushBlendAndColorWrite(
2686 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2687 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2688 // We need to work around a driver bug by using a blend state that preserves the dst color,
2689 // rather than disabling color writes.
2690 GrXferProcessor::BlendInfo preserveDstBlend;
2691 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2692 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2693 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2694 return;
2695 }
bsalomonf7cc8772015-05-11 11:21:14 -07002696
cdalton8917d622015-05-06 13:40:21 -07002697 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002698 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2699 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002700
2701 // Any optimization to disable blending should have already been applied and
2702 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07002703 bool blendOff =
2704 ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
2705 kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) ||
2706 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002707
egdanielb414f252014-07-29 13:15:47 -07002708 if (blendOff) {
2709 if (kNo_TriState != fHWBlendState.fEnabled) {
2710 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002711
2712 // Workaround for the ARM KHR_blend_equation_advanced blacklist issue
2713 // https://code.google.com/p/skia/issues/detail?id=3943
2714 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2715 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2716 SkASSERT(this->caps()->advancedBlendEquationSupport());
2717 // Set to any basic blending equation.
2718 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2719 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2720 fHWBlendState.fEquation = blend_equation;
2721 }
2722
egdanielb414f252014-07-29 13:15:47 -07002723 fHWBlendState.fEnabled = kNo_TriState;
2724 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002725 } else {
2726 if (kYes_TriState != fHWBlendState.fEnabled) {
2727 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002728
Chris Daltonbbb3f642019-07-24 12:25:08 -04002729 fHWBlendState.fEnabled = kYes_TriState;
2730 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002731
Chris Daltonbbb3f642019-07-24 12:25:08 -04002732 if (fHWBlendState.fEquation != equation) {
2733 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2734 fHWBlendState.fEquation = equation;
2735 }
cdalton8917d622015-05-06 13:40:21 -07002736
Chris Daltonbbb3f642019-07-24 12:25:08 -04002737 if (GrBlendEquationIsAdvanced(equation)) {
2738 SkASSERT(this->caps()->advancedBlendEquationSupport());
2739 // Advanced equations have no other blend state.
2740 return;
2741 }
cdalton8917d622015-05-06 13:40:21 -07002742
Chris Daltonbbb3f642019-07-24 12:25:08 -04002743 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2744 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2745 gXfermodeCoeff2Blend[dstCoeff]));
2746 fHWBlendState.fSrcCoeff = srcCoeff;
2747 fHWBlendState.fDstCoeff = dstCoeff;
2748 }
cdalton8917d622015-05-06 13:40:21 -07002749
Chris Daltonbbb3f642019-07-24 12:25:08 -04002750 if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) {
2751 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2752 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2753 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2754 fHWBlendState.fConstColor = blendConst;
2755 fHWBlendState.fConstColorValid = true;
2756 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002757 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002758 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002759
2760 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002761}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002762
Brian Salomondc829942018-10-23 16:07:24 -04002763static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002764 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002765 switch (swizzle[i]) {
2766 case 'r': glValues[i] = GR_GL_RED; break;
2767 case 'g': glValues[i] = GR_GL_GREEN; break;
2768 case 'b': glValues[i] = GR_GL_BLUE; break;
2769 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002770 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002771 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002772 default: SK_ABORT("Unsupported component");
2773 }
Brian Salomon327955b2018-10-22 15:39:22 +00002774 }
2775}
2776
Greg Daniel2c3398d2019-06-19 11:58:01 -04002777void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2778 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002779 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002780
reed856e9d92015-09-30 12:21:45 -07002781#ifdef SK_DEBUG
2782 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002783 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002784 const int w = texture->width();
2785 const int h = texture->height();
2786 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2787 }
2788 }
2789#endif
2790
Robert Phillips294870f2016-11-11 12:38:40 -05002791 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002792 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002793 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002794 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002795 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002796 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002797 }
2798
Brian Salomondc829942018-10-23 16:07:24 -04002799 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002800 if (!this->caps()->mipMapSupport() ||
2801 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002802 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002803 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002804 }
bsalomonefd7d452014-10-23 14:17:46 -07002805
brianosman33f6b3f2016-06-02 05:49:21 -07002806#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002807 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002808 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002809 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002810 }
2811#endif
2812
Brian Salomone2826ab2019-06-04 15:58:31 -04002813 auto timestamp = texture->parameters()->resetTimestamp();
2814 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002815
Brian Salomone2826ab2019-06-04 15:58:31 -04002816 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2817 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002818 if (fSamplerObjectCache) {
2819 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
2820 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002821 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2822 texture->parameters()->samplerOverriddenState();
2823 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002824
Brian Salomone2826ab2019-06-04 15:58:31 -04002825 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2826 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002827
Brian Salomone2826ab2019-06-04 15:58:31 -04002828 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2829 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002830
2831 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002832 newSamplerState.fMinLOD = -1000.f;
2833 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002834
Brian Salomone2826ab2019-06-04 15:58:31 -04002835 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002836 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002837 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002838 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002839 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002840 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002841 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002842 }
Mike Klein1bccae52018-10-19 11:38:44 +00002843 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002844 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002845 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002846 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002847 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002848 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002849 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002850 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002851 }
2852 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002853 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002854 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002855 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002856 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002857 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002858 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002859 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002860 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002861 if (this->glCaps().clampToBorderSupport()) {
2862 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002863 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002864 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002865 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2866 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002867 }
2868 }
Brian Salomondc829942018-10-23 16:07:24 -04002869 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002870 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2871 newNonsamplerState.fBaseMipMapLevel = 0;
2872 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002873
Brian Salomone2826ab2019-06-04 15:58:31 -04002874 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2875 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002876 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002877 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2878 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002879 GrGLenum glValues[4];
2880 get_gl_swizzle_values(swizzle, glValues);
2881 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002882 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002883 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002884 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2885 reinterpret_cast<const GrGLint*>(glValues)));
2886 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002887 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2888 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2889 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2890 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2891 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002892 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002893 }
2894 }
Brian Salomondc829942018-10-23 16:07:24 -04002895 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002896 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2897 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2898 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002899 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002900 this->setTextureUnit(unitIdx);
2901 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002902 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002903 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002904 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002905 this->setTextureUnit(unitIdx);
2906 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002907 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002908 }
Brian Salomon327955b2018-10-22 15:39:22 +00002909 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002910 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2911 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002912}
2913
Brian Salomon1f05d452019-02-08 12:33:08 -05002914void GrGLGpu::onResetTextureBindings() {
2915 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2916 GR_GL_TEXTURE_EXTERNAL};
2917 for (int i = 0; i < this->numTextureUnits(); ++i) {
2918 this->setTextureUnit(i);
2919 for (auto target : kTargets) {
2920 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2921 GL_CALL(BindTexture(target, 0));
2922 }
2923 }
2924 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2925 }
2926}
2927
egdaniel080e6732014-12-22 07:35:52 -08002928void GrGLGpu::flushColorWrite(bool writeColor) {
2929 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002930 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002931 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2932 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002933 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002934 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002935 } else {
2936 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002937 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002938 fHWWriteToColor = kYes_TriState;
2939 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002940 }
bsalomon3e791242014-12-17 13:43:13 -08002941}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002942
Chris Dalton674f77a2019-09-30 20:49:39 -06002943void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2944 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2945 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2946 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2947 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2948 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2949 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2950 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002951 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2952 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2953 GL_CALL(ClearColor(r, g, b, a));
2954 fHWClearColor[0] = r;
2955 fHWClearColor[1] = g;
2956 fHWClearColor[2] = b;
2957 fHWClearColor[3] = a;
2958 }
2959}
2960
bsalomon861e1032014-12-16 07:33:49 -08002961void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002962 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002963 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002964 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002965 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002966 }
2967}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002968
Brian Salomond978b902019-02-07 15:09:18 -05002969void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002970 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002971 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002972 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2973 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2974 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002975 }
Brian Salomond978b902019-02-07 15:09:18 -05002976 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2977 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002978 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002979 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002980}
2981
Brian Salomone5e7eb12016-10-14 16:18:33 -04002982// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002983static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2984 const GrSurface* src,
2985 const SkIRect& srcRect,
2986 const SkIPoint& dstPoint,
2987 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002988 int dstSampleCnt = 0;
2989 int srcSampleCnt = 0;
2990 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002991 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002992 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002993 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002994 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002995 }
2996 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2997 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2998
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002999 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3000 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3001
Brian Salomone5e7eb12016-10-14 16:18:33 -04003002 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05003003 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003004
Greg Daniel46cfbc62019-06-07 11:43:30 -04003005 GrTextureType dstTexType;
3006 GrTextureType* dstTexTypePtr = nullptr;
3007 GrTextureType srcTexType;
3008 GrTextureType* srcTexTypePtr = nullptr;
3009 if (dstTex) {
3010 dstTexType = dstTex->texturePriv().textureType();
3011 dstTexTypePtr = &dstTexType;
3012 }
3013 if (srcTex) {
3014 srcTexType = srcTex->texturePriv().textureType();
3015 srcTexTypePtr = &srcTexType;
3016 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003017
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003018 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
3019 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003020 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003021}
bsalomon@google.comeb851172013-04-15 13:51:00 +00003022
Brian Osmana9c8a052018-01-19 10:31:56 -05003023static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
3024 // A RT has a separate MSAA renderbuffer if:
3025 // 1) It's multisampled
3026 // 2) We're using an extension with separate MSAA renderbuffers
3027 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06003028 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05003029}
3030
Greg Daniel46cfbc62019-06-07 11:43:30 -04003031static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003032 const GrGLCaps& caps) {
3033
bsalomon@google.comeb851172013-04-15 13:51:00 +00003034 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00003035 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08003036 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08003037 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08003038
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003039 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
3040 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
3041
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003042 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
3043 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
3044
Greg Daniel46cfbc62019-06-07 11:43:30 -04003045 GrTextureType dstTexType;
3046 GrTextureType* dstTexTypePtr = nullptr;
3047 GrTextureType srcTexType;
3048 GrTextureType* srcTexTypePtr = nullptr;
3049 if (dstTex) {
3050 dstTexType = dstTex->texturePriv().textureType();
3051 dstTexTypePtr = &dstTexType;
3052 }
3053 if (srcTex) {
3054 srcTexType = srcTex->texturePriv().textureType();
3055 srcTexTypePtr = &srcTexType;
3056 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04003057
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003058 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
3059 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00003060}
3061
Greg Danielacd66b42019-05-22 16:29:12 -04003062// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04003063void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04003064 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00003065 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04003066 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07003067 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04003068 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
3069 GrGLuint texID = texture->textureID();
3070 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07003071 GrGLuint* tempFBOID;
3072 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08003073
egdanield803f272015-03-18 13:01:52 -07003074 if (0 == *tempFBOID) {
3075 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08003076 }
3077
Adrienne Walker4ee88512018-05-17 11:37:14 -07003078 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003079 GR_GL_CALL(
3080 this->glInterface(),
3081 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
3082 if (mipLevel == 0) {
3083 texture->baseLevelWasBoundToFBO();
3084 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003085 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003086 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00003087 }
egdaniel0f5f9672015-02-03 11:10:51 -08003088}
3089
Brian Salomond2a8ae22019-09-10 16:03:59 -04003090void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04003091 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04003092 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07003093 SkASSERT(surface->asTexture());
3094 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
3095 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
3096 GR_GL_COLOR_ATTACHMENT0,
3097 textureTarget,
3098 0,
3099 0));
3100 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00003101}
3102
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003103void GrGLGpu::onFBOChanged() {
3104 if (this->caps()->workarounds().flush_on_framebuffer_change ||
3105 this->caps()->workarounds().restore_scissor_on_fbo_change) {
3106 GL_CALL(Flush());
3107 }
Chris Dalton674f77a2019-09-30 20:49:39 -06003108#ifdef SK_DEBUG
3109 if (fIsExecutingCommandBuffer_DebugOnly) {
3110 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
3111 "This will severely hurt performance.\n");
3112 }
3113#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003114}
3115
Adrienne Walker4ee88512018-05-17 11:37:14 -07003116void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
3117 fStats.incRenderTargetBinds();
3118 GL_CALL(BindFramebuffer(target, fboid));
3119 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
3120 fBoundDrawFramebuffer = fboid;
3121 }
3122
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003123 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
3124 // The driver forgets the correct scissor when modifying the FBO binding.
3125 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06003126 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06003127 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003128 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07003129 }
3130
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003131 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07003132}
3133
Adrienne Walker4ee88512018-05-17 11:37:14 -07003134void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
3135 if (fboid == fBoundDrawFramebuffer &&
3136 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
3137 // This workaround only applies to deleting currently bound framebuffers
3138 // on Adreno 420. Because this is a somewhat rare case, instead of
3139 // tracking all the attachments of every framebuffer instead just always
3140 // unbind all attachments.
3141 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3142 GR_GL_RENDERBUFFER, 0));
3143 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
3144 GR_GL_RENDERBUFFER, 0));
3145 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
3146 GR_GL_RENDERBUFFER, 0));
3147 }
3148
3149 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07003150
3151 // Deleting the currently bound framebuffer rebinds to 0.
3152 if (fboid == fBoundDrawFramebuffer) {
3153 this->onFBOChanged();
3154 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003155}
3156
Greg Daniel46cfbc62019-06-07 11:43:30 -04003157bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04003158 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003159 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
3160 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
3161 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003162 auto dstFormat = dst->backendFormat().asGLFormat();
3163 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003164 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003165 return true;
3166 }
3167 }
cblume61214052016-01-26 09:10:48 -08003168
Greg Daniel46cfbc62019-06-07 11:43:30 -04003169 if (can_copy_texsubimage(dst, src, this->glCaps())) {
3170 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07003171 return true;
3172 }
3173
Greg Daniel46cfbc62019-06-07 11:43:30 -04003174 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3175 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003176 }
3177
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003178 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 }
bsalomon083617b2016-02-12 12:10:14 -08003182 }
3183
bsalomon6df86402015-06-01 10:41:49 -07003184 return false;
3185}
3186
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003187bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003188 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003189
3190 int progIdx = TextureToCopyProgramIdx(srcTex);
3191 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3192 GrSLType samplerType =
3193 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3194
3195 if (!fCopyProgramArrayBuffer) {
3196 static const GrGLfloat vdata[] = {
3197 0, 0,
3198 0, 1,
3199 1, 0,
3200 1, 1
3201 };
3202 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3203 kStatic_GrAccessPattern, vdata);
3204 }
3205 if (!fCopyProgramArrayBuffer) {
3206 return false;
3207 }
3208
3209 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3210 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3211 if (!fCopyPrograms[progIdx].fProgram) {
3212 return false;
3213 }
3214
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003215 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3216 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
3217 GrShaderVar::kUniform_TypeModifier);
3218 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier);
3219 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier);
3220 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
3221 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
3222
Greg Daniel9e3169b2019-06-06 14:38:17 -04003223 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003224 if (shaderCaps->noperspectiveInterpolationSupport()) {
3225 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3226 vshaderTxt.appendf("#extension %s : require\n", extension);
3227 }
3228 vTexCoord.addModifier("noperspective");
3229 }
3230
3231 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3232 vshaderTxt.append(";");
3233 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3234 vshaderTxt.append(";");
3235 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3236 vshaderTxt.append(";");
3237 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3238 vshaderTxt.append(";");
3239
3240 vshaderTxt.append(
3241 "// Copy Program VS\n"
3242 "void main() {"
3243 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3244 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3245 " sk_Position.zw = half2(0, 1);"
3246 "}"
3247 );
3248
Greg Daniel9e3169b2019-06-06 14:38:17 -04003249 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003250 if (shaderCaps->noperspectiveInterpolationSupport()) {
3251 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3252 fshaderTxt.appendf("#extension %s : require\n", extension);
3253 }
3254 }
3255 vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
3256 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3257 fshaderTxt.append(";");
3258 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3259 fshaderTxt.append(";");
3260 fshaderTxt.appendf(
3261 "// Copy Program FS\n"
3262 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003263 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003264 "}"
3265 );
3266
3267 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3268 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3269 SkSL::Program::Settings settings;
3270 settings.fCaps = shaderCaps;
3271 SkSL::String glsl;
3272 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3273 sksl, settings, &glsl, errorHandler);
3274 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3275 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3276 SkASSERT(program->fInputs.isEmpty());
3277
3278 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3279 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3280 errorHandler);
3281 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3282 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3283 errorHandler);
3284 SkASSERT(program->fInputs.isEmpty());
3285
3286 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3287
3288 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3289 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3290 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3291 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3292 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3293 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3294
3295 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3296
3297 GL_CALL(DeleteShader(vshader));
3298 GL_CALL(DeleteShader(fshader));
3299
3300 return true;
3301}
3302
brianosman33f6b3f2016-06-02 05:49:21 -07003303bool GrGLGpu::createMipmapProgram(int progIdx) {
3304 const bool oddWidth = SkToBool(progIdx & 0x2);
3305 const bool oddHeight = SkToBool(progIdx & 0x1);
3306 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3307
Brian Salomon1edc5b92016-11-29 13:43:46 -05003308 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003309
3310 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3311 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3312 if (!fMipmapPrograms[progIdx].fProgram) {
3313 return false;
3314 }
3315
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003316 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
3317 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Brian Salomon99938a82016-11-21 13:41:08 -05003318 GrShaderVar::kUniform_TypeModifier);
3319 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
3320 GrShaderVar::kUniform_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003321 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003322 GrShaderVar vTexCoords[] = {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003323 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3324 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3325 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
3326 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier),
brianosman33f6b3f2016-06-02 05:49:21 -07003327 };
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003328 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier);
brianosman33f6b3f2016-06-02 05:49:21 -07003329
Ethan Nicholasfc994162019-06-06 10:04:27 -04003330 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003331 if (shaderCaps->noperspectiveInterpolationSupport()) {
3332 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003333 vshaderTxt.appendf("#extension %s : require\n", extension);
3334 }
3335 vTexCoords[0].addModifier("noperspective");
3336 vTexCoords[1].addModifier("noperspective");
3337 vTexCoords[2].addModifier("noperspective");
3338 vTexCoords[3].addModifier("noperspective");
3339 }
3340
Brian Salomon1edc5b92016-11-29 13:43:46 -05003341 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003342 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003343 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003344 vshaderTxt.append(";");
3345 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003346 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003347 vshaderTxt.append(";");
3348 }
3349
3350 vshaderTxt.append(
3351 "// Mipmap Program VS\n"
3352 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003353 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3354 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003355 );
3356
3357 // Insert texture coordinate computation:
3358 if (oddWidth && oddHeight) {
3359 vshaderTxt.append(
3360 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003361 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3362 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003363 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3364 );
3365 } else if (oddWidth) {
3366 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003367 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3368 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003369 );
3370 } else if (oddHeight) {
3371 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003372 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3373 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003374 );
3375 } else {
3376 vshaderTxt.append(
3377 " v_texCoord0 = a_vertex.xy;"
3378 );
3379 }
3380
3381 vshaderTxt.append("}");
3382
Ethan Nicholasfc994162019-06-06 10:04:27 -04003383 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003384 if (shaderCaps->noperspectiveInterpolationSupport()) {
3385 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003386 fshaderTxt.appendf("#extension %s : require\n", extension);
3387 }
3388 }
brianosman33f6b3f2016-06-02 05:49:21 -07003389 for (int i = 0; i < numTaps; ++i) {
Brian Salomonf31ae492016-11-18 15:35:33 -05003390 vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003391 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003392 fshaderTxt.append(";");
3393 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003394 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003395 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003396 fshaderTxt.append(
3397 "// Mipmap Program FS\n"
3398 "void main() {"
3399 );
3400
3401 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003402 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003403 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3404 " sample(u_texture, v_texCoord1) + "
3405 " sample(u_texture, v_texCoord2) + "
3406 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003407 );
3408 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003409 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003410 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3411 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003412 );
3413 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003414 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003415 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003416 );
3417 }
3418
3419 fshaderTxt.append("}");
3420
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003421 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003422 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003423 SkSL::Program::Settings settings;
3424 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003425 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003426 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003427 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003428 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003429 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003430 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003431
Brian Osman6c431d52019-04-15 16:31:54 -04003432 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003433 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3434 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003435 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003436 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003437 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003438 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003439
3440 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3441
3442 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3443 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3444 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3445 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3446
3447 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3448
3449 GL_CALL(DeleteShader(vshader));
3450 GL_CALL(DeleteShader(fshader));
3451
3452 return true;
3453}
3454
Greg Daniel46cfbc62019-06-07 11:43:30 -04003455bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003456 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003457 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3458 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3459 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3460 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003461 return false;
3462 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003463 int progIdx = TextureToCopyProgramIdx(srcTex);
3464 if (!dstRT) {
3465 SkASSERT(dstTex);
3466 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3467 return false;
3468 }
3469 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003470 if (!fCopyPrograms[progIdx].fProgram) {
3471 if (!this->createCopyProgram(srcTex)) {
3472 SkDebugf("Failed to create copy program.\n");
3473 return false;
3474 }
3475 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003476 int w = srcRect.width();
3477 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003478 // We don't swizzle at all in our copies.
3479 this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003480 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003481 this->flushViewport(dst->width(), dst->height());
3482 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003483 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003484 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003485 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003486 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3487 attribs->enableVertexArrays(this, 1);
3488 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3489 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003490 // dst rect edges in NDC (-1 to 1)
3491 int dw = dst->width();
3492 int dh = dst->height();
3493 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3494 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3495 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3496 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003497 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3498 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3499 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3500 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3501 int sw = src->width();
3502 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003503 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3504 // src rect edges in normalized texture space (0 to 1)
3505 sx0 /= sw;
3506 sx1 /= sw;
3507 sy0 /= sh;
3508 sy1 /= sh;
3509 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003510 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3511 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3512 sx1 - sx0, sy1 - sy0, sx0, sy0));
3513 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003514 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003515 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003516 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003517 this->flushWireframeState(false);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003518 this->disableScissor();
3519 this->disableWindowRectangles();
3520 this->disableStencil();
3521 if (this->glCaps().srgbWriteControl()) {
3522 this->flushFramebufferSRGB(true);
3523 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003524 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003525 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003526 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3527 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003528 return true;
3529}
3530
Greg Daniel46cfbc62019-06-07 11:43:30 -04003531void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003532 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003533 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003534 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003535 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003536 SkASSERT(dstTex);
3537 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003538 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003539
Brian Salomond978b902019-02-07 15:09:18 -05003540 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003541 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003542 dstPoint.fX, dstPoint.fY,
3543 srcRect.fLeft, srcRect.fTop,
3544 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003545 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003546 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3547 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003548 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3549 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003550}
3551
Greg Daniel46cfbc62019-06-07 11:43:30 -04003552bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003553 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003554 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003555 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3556 srcRect.width(), srcRect.height());
3557 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003558 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003559 return false;
mtklein404b3b22015-05-18 09:29:10 -07003560 }
bsalomon5df6fee2015-05-18 06:26:15 -07003561 }
bsalomon6df86402015-06-01 10:41:49 -07003562
Brian Salomond2a8ae22019-09-10 16:03:59 -04003563 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3564 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003565 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003566 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003567
3568 // BlitFrameBuffer respects the scissor, so disable it.
Brian Salomond818ebf2018-07-02 14:08:49 +00003569 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003570 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003571
Greg Daniel46cfbc62019-06-07 11:43:30 -04003572 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3573 srcRect.fTop,
3574 srcRect.fRight,
3575 srcRect.fBottom,
3576 dstRect.fLeft,
3577 dstRect.fTop,
3578 dstRect.fRight,
3579 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003580 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003581 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3582 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003583
3584 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3585 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003586 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003587}
3588
Brian Salomon930f9392018-06-20 16:25:26 -04003589bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3590 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003591 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003592 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003593 return false;
3594 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003595 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003596 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3597 // Uses draw calls to do a series of downsample operations to successive mips.
3598
3599 // The manual approach requires the ability to limit which level we're sampling and that the
3600 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003601 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003602 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003603 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003604 GL_CALL(GenerateMipmap(glTex->target()));
3605 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003606 }
3607
brianosman33f6b3f2016-06-02 05:49:21 -07003608 int width = texture->width();
3609 int height = texture->height();
3610 int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003611 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003612
3613 // Create (if necessary), then bind temporary FBO:
3614 if (0 == fTempDstFBOID) {
3615 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3616 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003617 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003618 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003619
3620 // Bind the texture, to get things configured for filtering.
3621 // We'll be changing our base level further below:
3622 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003623 // The mipmap program does not do any swizzling.
3624 this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003625
3626 // Vertex data:
3627 if (!fMipmapProgramArrayBuffer) {
3628 static const GrGLfloat vdata[] = {
3629 0, 0,
3630 0, 1,
3631 1, 0,
3632 1, 1
3633 };
Brian Salomonae64c192019-02-05 09:41:37 -05003634 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003635 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003636 }
3637 if (!fMipmapProgramArrayBuffer) {
3638 return false;
3639 }
3640
3641 fHWVertexArrayState.setVertexArrayID(this, 0);
3642
3643 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003644 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003645 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003646 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003647
3648 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003649 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003650 this->flushHWAAState(nullptr, false);
Brian Salomond818ebf2018-07-02 14:08:49 +00003651 this->disableScissor();
csmartdalton28341fa2016-08-17 10:00:21 -07003652 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003653 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003654
3655 // Do all the blits:
3656 width = texture->width();
3657 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003658
brianosman33f6b3f2016-06-02 05:49:21 -07003659 for (GrGLint level = 1; level < levelCount; ++level) {
3660 // Get and bind the program for this particular downsample (filter shape can vary):
3661 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3662 if (!fMipmapPrograms[progIdx].fProgram) {
3663 if (!this->createMipmapProgram(progIdx)) {
3664 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003665 // Invalidate all params to cover base level change in a previous iteration.
3666 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003667 return false;
3668 }
3669 }
Brian Salomon802cb312018-06-08 18:05:20 -04003670 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003671
3672 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3673 const float invWidth = 1.0f / width;
3674 const float invHeight = 1.0f / height;
3675 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3676 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3677 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3678
3679 // Only sample from previous mip
3680 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3681
Brian Salomon930f9392018-06-20 16:25:26 -04003682 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3683 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003684
3685 width = SkTMax(1, width / 2);
3686 height = SkTMax(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003687 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003688
3689 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3690 }
3691
3692 // Unbind:
3693 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3694 GR_GL_TEXTURE_2D, 0, 0));
3695
Brian Salomon930f9392018-06-20 16:25:26 -04003696 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003697 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3698 // We drew the 2nd to last level into the last level.
3699 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3700 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003701
brianosman33f6b3f2016-06-02 05:49:21 -07003702 return true;
3703}
3704
Chris Daltond7291ba2019-03-07 14:17:03 -07003705void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003706 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3707 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003708
3709 int effectiveSampleCnt;
3710 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003711 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003712
3713 sampleLocations->reset(effectiveSampleCnt);
3714 for (int i = 0; i < effectiveSampleCnt; ++i) {
3715 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3716 }
3717}
3718
cdalton231c5fd2015-05-13 12:35:36 -07003719void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003720 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003721 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003722 case kTexture_GrXferBarrierType: {
3723 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003724 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003725 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3726 // The render target uses separate storage so no need for glTextureBarrier.
3727 // FIXME: The render target will resolve automatically when its texture is bound,
3728 // but we could resolve only the bounds that will be read if we do it here instead.
3729 return;
3730 }
cdalton9954bc32015-04-29 14:17:00 -07003731 SkASSERT(this->caps()->textureBarrierSupport());
3732 GL_CALL(TextureBarrier());
3733 return;
cdalton231c5fd2015-05-13 12:35:36 -07003734 }
cdalton8917d622015-05-06 13:40:21 -07003735 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003736 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003737 this->caps()->blendEquationSupport());
3738 GL_CALL(BlendBarrier());
3739 return;
bsalomoncb02b382015-08-12 11:14:50 -07003740 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003741 }
3742}
3743
Brian Salomon5043f1f2019-07-11 21:27:54 -04003744static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003745 switch (format) {
Brian Salomon5043f1f2019-07-11 21:27:54 -04003746 case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig;
3747 case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig;
3748 case GrGLFormat::kRG8: return kRG_88_GrPixelConfig;
3749 case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig;
3750 case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig;
3751 case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig;
3752 case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig;
3753 case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig;
3754 case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003755 case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig;
Robert Phillips429f0d32019-09-11 17:03:28 -04003756 case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig;
Brian Salomon5043f1f2019-07-11 21:27:54 -04003757 case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig;
3758 case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig;
3759 case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig;
3760 case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04003761
Brian Salomon5043f1f2019-07-11 21:27:54 -04003762 // Configs with multiple equivalent formats.
3763
Robert Phillipsebab03f2019-07-22 08:48:18 -04003764 case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig;
3765 case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig;
3766
Brian Salomon5043f1f2019-07-11 21:27:54 -04003767 case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig;
3768 case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig;
3769
3770 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig;
3771 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003772 }
Brian Salomon5043f1f2019-07-11 21:27:54 -04003773 SkUNREACHABLE;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003774}
3775
Brian Salomon85c3d682019-11-04 15:04:54 -05003776GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003777 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003778 GrRenderable renderable,
Brian Salomon85c3d682019-11-04 15:04:54 -05003779 const BackendTextureData* data,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003780 GrMipMapped mipMapped,
Robert Phillips57ef6802019-09-23 10:12:47 -04003781 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003782 // We don't support protected textures in GL.
3783 if (isProtected == GrProtected::kYes) {
3784 return {};
3785 }
3786
Brian Salomon8a375832018-03-14 10:21:40 -04003787 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003788
Brian Salomond4764a12019-08-08 12:08:24 -04003789 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003790 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003791 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003792 }
3793
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003794 int numMipLevels = 1;
3795 if (mipMapped == GrMipMapped::kYes) {
3796 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
3797 }
3798
Robert Phillipsd34691b2019-09-24 13:38:43 -04003799 // Compressed formats go through onCreateCompressedBackendTexture
3800 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3801
Greg Daniel15500642019-06-27 03:05:55 +00003802 GrGLTextureInfo info;
3803 GrGLTextureParameters::SamplerOverriddenState initialState;
3804
Robert Phillipsd34691b2019-09-24 13:38:43 -04003805 GrSurfaceDesc desc;
Brian Salomon85c3d682019-11-04 15:04:54 -05003806 desc.fWidth = dimensions.width();
3807 desc.fHeight = dimensions.height();
3808 desc.fConfig = gl_format_to_pixel_config(glFormat);
3809 if (desc.fConfig == kUnknown_GrPixelConfig) {
Robert Phillips42716d42019-12-16 12:19:54 -05003810 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003811 }
Robert Phillips57ef6802019-09-23 10:12:47 -04003812
Robert Phillipsd34691b2019-09-24 13:38:43 -04003813 info.fTarget = GR_GL_TEXTURE_2D;
3814 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon85c3d682019-11-04 15:04:54 -05003815 info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003816 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003817 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003818 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003819
Robert Phillips42716d42019-12-16 12:19:54 -05003820 SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon85c3d682019-11-04 15:04:54 -05003821 if (data && data->type() == BackendTextureData::Type::kPixmaps) {
3822 SkTDArray<GrMipLevel> texels;
3823 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3824 // Incorporate the color type into the config to make it "specific" if applicable.
3825 desc.fConfig = this->caps()->getConfigFromBackendFormat(format, colorType);
3826 SkASSERT(desc.fConfig != kUnknown_GrPixelConfig);
3827 texels.append(numMipLevels);
3828 for (int i = 0; i < numMipLevels; ++i) {
3829 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3830 }
3831 if (!this->uploadTexData(glFormat, colorType, desc.fWidth, desc.fHeight, GR_GL_TEXTURE_2D,
3832 0, 0, desc.fWidth, desc.fHeight, colorType, texels.begin(),
3833 texels.count())) {
3834 GL_CALL(DeleteTextures(1, &info.fID));
3835 return {};
3836 }
3837 } else if (data && data->type() == BackendTextureData::Type::kColor) {
3838 // TODO: Unify this with the clear texture code in onCreateTexture().
3839 GrColorType colorType;
3840 GrGLenum externalFormat, externalType;
3841 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat,
3842 &externalType, &colorType);
3843 if (colorType == GrColorType::kUnknown) {
3844 GL_CALL(DeleteTextures(1, &info.fID));
3845 return {};
3846 }
3847
3848 // Make one tight image at the base size and reuse it for smaller levels.
3849 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions);
3850 auto rb = ii.minRowBytes();
3851 std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]);
3852 if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) {
3853 GL_CALL(DeleteTextures(1, &info.fID));
3854 return {};
3855 }
3856
3857 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
3858 SkISize levelDimensions = dimensions;
3859 for (int i = 0; i < numMipLevels; ++i) {
3860 GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(),
3861 levelDimensions.height(), externalFormat, externalType,
3862 pixelStorage.get()));
3863 levelDimensions = {SkTMax(1, levelDimensions.width() /2),
3864 SkTMax(1, levelDimensions.height()/2)};
3865 }
3866 }
3867 // Unbind this texture from the scratch texture unit.
3868 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003869
Brian Salomone2826ab2019-06-04 15:58:31 -04003870 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003871 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003872 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3873 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003874
Brian Salomon85c3d682019-11-04 15:04:54 -05003875 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3876 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003877}
3878
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003879void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003880 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3881
3882 GrGLTextureInfo info;
3883 if (tex.getGLTextureInfo(&info)) {
3884 GL_CALL(DeleteTextures(1, &info.fID));
3885 }
3886}
3887
3888#if GR_TEST_UTILS
3889
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003890bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003891 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003892
Greg Daniel52e16d92018-04-10 09:34:07 -04003893 GrGLTextureInfo info;
3894 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003895 return false;
3896 }
3897
3898 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003899 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003900
3901 return (GR_GL_TRUE == result);
3902}
3903
Brian Salomonf865b052018-03-09 09:01:53 -05003904GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003905 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003906 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3907 return GrBackendRenderTarget(); // invalid
3908 }
Brian Salomon8a375832018-03-14 10:21:40 -04003909 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003910 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003911 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003912 return {};
3913 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003914 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003915 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003916 if (sFormatIdx < 0) {
3917 return {};
3918 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003919 GrGLuint colorID = 0;
3920 GrGLuint stencilID = 0;
3921 auto deleteIDs = [&] {
3922 if (colorID) {
3923 if (useTexture) {
3924 GL_CALL(DeleteTextures(1, &colorID));
3925 } else {
3926 GL_CALL(DeleteRenderbuffers(1, &colorID));
3927 }
Brian Salomonf865b052018-03-09 09:01:53 -05003928 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003929 if (stencilID) {
3930 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003931 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003932 };
3933
3934 if (useTexture) {
3935 GL_CALL(GenTextures(1, &colorID));
3936 } else {
3937 GL_CALL(GenRenderbuffers(1, &colorID));
3938 }
3939 GL_CALL(GenRenderbuffers(1, &stencilID));
3940 if (!stencilID || !colorID) {
3941 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003942 return {};
3943 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003944
Brian Salomonf865b052018-03-09 09:01:53 -05003945 GrGLFramebufferInfo info;
3946 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003947 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003948 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3949 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003950 deleteIDs();
3951 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003952 }
3953
3954 this->invalidateBoundRenderTarget();
3955
Adrienne Walker4ee88512018-05-17 11:37:14 -07003956 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003957 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003958 GrGLTextureParameters::SamplerOverriddenState initialState;
3959 colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1);
3960 if (!colorID) {
3961 deleteIDs();
3962 return {};
3963 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003964 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3965 colorID, 0));
3966 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003967 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003968 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
3969 GL_ALLOC_CALL(this->glInterface(),
Brian Salomond2a8ae22019-09-10 16:03:59 -04003970 RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003971 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3972 GR_GL_RENDERBUFFER, colorID));
3973 }
3974 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003975 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
3976 GL_ALLOC_CALL(this->glInterface(),
3977 RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
3978 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003979 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003980 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3981 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003982 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003983 }
3984
Brian Salomon93348dd2018-08-29 12:56:23 -04003985 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3986 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3987 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3988 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003989 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003990 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003991
Adrienne Walker4ee88512018-05-17 11:37:14 -07003992 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003993 GrGLenum status;
3994 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3995 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003996 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003997 return {};
3998 }
3999 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04004000
Greg Daniel108bb232018-07-03 16:18:29 -04004001 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04004002 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04004003 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05004004}
4005
4006void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04004007 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04004008 GrGLFramebufferInfo info;
4009 if (backendRT.getGLFramebufferInfo(&info)) {
4010 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07004011 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05004012 }
4013 }
joshualitt8fd844f2015-12-02 13:36:47 -08004014}
4015
Greg Daniel26b50a42018-03-08 09:49:58 -05004016void GrGLGpu::testingOnly_flushGpuAndSync() {
4017 GL_CALL(Finish());
4018}
Brian Salomonf865b052018-03-09 09:01:53 -05004019#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05004020
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004021///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07004022
cdaltone2e71c22016-04-07 18:13:29 -07004023GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07004024 const GrBuffer* ibuf) {
robertphillips@google.com4f65a272013-03-26 19:40:46 +00004025 GrGLAttribArrayState* attribState;
4026
cdaltone2e71c22016-04-07 18:13:29 -07004027 if (gpu->glCaps().isCoreProfile()) {
4028 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00004029 GrGLuint arrayID;
4030 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
4031 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07004032 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004033 }
cdaltone2e71c22016-04-07 18:13:29 -07004034 if (ibuf) {
4035 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07004036 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004037 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07004038 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004039 } else {
cdaltone2e71c22016-04-07 18:13:29 -07004040 if (ibuf) {
4041 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05004042 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00004043 } else {
4044 this->setVertexArrayID(gpu, 0);
4045 }
4046 int attrCount = gpu->glCaps().maxVertexAttributes();
4047 if (fDefaultVertexArrayAttribState.count() != attrCount) {
4048 fDefaultVertexArrayAttribState.resize(attrCount);
4049 }
4050 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00004051 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00004052 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00004053}
bsalomone179a912016-01-20 06:18:10 -08004054
Greg Daniel30a35e82019-11-19 14:12:25 -05004055bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
Greg Daniel797efca2019-05-09 14:04:20 -04004056 const GrFlushInfo& info, const GrPrepareForExternalIORequests&) {
Greg Daniel51316782017-08-02 15:10:09 +00004057 // If we inserted semaphores during the flush, we need to call GLFlush.
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004058 bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004059 // We call finish if the client told us to sync or if we have a finished proc but don't support
4060 // GLsync objects.
4061 bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) ||
4062 (info.fFinishedProc && !this->caps()->fenceSyncSupport());
4063 if (finish) {
Greg Danielbae71212019-03-01 15:24:35 -05004064 GL_CALL(Finish());
Brian Salomonb0d8b762019-05-06 16:58:22 -04004065 // After a finish everything previously sent to GL is done.
4066 for (const auto& cb : fFinishCallbacks) {
4067 cb.fCallback(cb.fContext);
4068 this->deleteSync(cb.fSync);
4069 }
4070 fFinishCallbacks.clear();
4071 if (info.fFinishedProc) {
4072 info.fFinishedProc(info.fFinishedContext);
4073 }
4074 } else {
4075 if (info.fFinishedProc) {
4076 FinishCallback callback;
4077 callback.fCallback = info.fFinishedProc;
4078 callback.fContext = info.fFinishedContext;
4079 callback.fSync = (GrGLsync)this->insertFence();
4080 fFinishCallbacks.push_back(callback);
4081 GL_CALL(Flush());
4082 } else if (insertedSemaphore) {
4083 // Must call flush after semaphores in case they are waited on another GL context.
4084 GL_CALL(Flush());
4085 }
4086 // See if any previously inserted finish procs are good to go.
4087 this->checkFinishProcs();
Greg Daniela3aa75a2019-04-12 14:24:55 -04004088 }
Greg Daniel30a35e82019-11-19 14:12:25 -05004089 return true;
Greg Daniel51316782017-08-02 15:10:09 +00004090}
4091
Greg Daniel2d41d0d2019-08-26 11:08:51 -04004092void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
4093 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
4094 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
4095 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04004096}
4097
Greg Daniel6be35232017-03-01 17:01:09 -05004098GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Greg Danielc64ee462017-06-15 16:59:49 -04004099 SkASSERT(this->caps()->fenceSyncSupport());
Greg Daniel6be35232017-03-01 17:01:09 -05004100 GrGLsync sync;
4101 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
Brian Salomon4dea72a2019-12-18 10:43:10 -05004102 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05004103 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07004104}
4105
Brian Salomonb0d8b762019-05-06 16:58:22 -04004106bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
4107 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
jvanverth84741b32016-09-30 08:39:02 -07004108 GrGLenum result;
Brian Salomonb0d8b762019-05-06 16:58:22 -04004109 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
4110 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
4111}
4112
4113bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) {
4114 return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true);
jvanverth84741b32016-09-30 08:39:02 -07004115}
4116
4117void GrGLGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05004118 this->deleteSync((GrGLsync)fence);
4119}
4120
Greg Daniel301015c2019-11-18 14:06:46 -05004121std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004122 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004123 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004124}
4125
Greg Daniel301015c2019-11-18 14:06:46 -05004126std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4127 const GrBackendSemaphore& semaphore,
4128 GrResourceProvider::SemaphoreWrapType wrapType,
4129 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004130 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004131 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4132}
4133
Greg Daniel301015c2019-11-18 14:06:46 -05004134void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
4135 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004136
Greg Daniel48661b82018-01-22 16:11:35 -05004137 GrGLsync sync;
4138 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4139 glSem->setSync(sync);
Greg Daniel6be35232017-03-01 17:01:09 -05004140}
4141
Greg Daniel301015c2019-11-18 14:06:46 -05004142void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
4143 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004144
4145 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4146}
4147
Brian Salomonb0d8b762019-05-06 16:58:22 -04004148void GrGLGpu::checkFinishProcs() {
4149 // Bail after the first unfinished sync since we expect they signal in the order inserted.
4150 while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync,
4151 /* timeout = */ 0, /* flush = */ false)) {
4152 fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext);
4153 this->deleteSync(fFinishCallbacks.front().fSync);
4154 fFinishCallbacks.pop_front();
4155 }
4156}
4157
Greg Daniel6be35232017-03-01 17:01:09 -05004158void GrGLGpu::deleteSync(GrGLsync sync) const {
4159 GL_CALL(DeleteSync(sync));
jvanverth84741b32016-09-30 08:39:02 -07004160}
Brian Osman13dddce2017-05-09 13:19:50 -04004161
Greg Daniel301015c2019-11-18 14:06:46 -05004162std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004163 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004164 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004165 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004166 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004167 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
4168 GL_CALL(Flush());
Brian Osman13dddce2017-05-09 13:19:50 -04004169
4170 return semaphore;
4171}
Robert Phillips646e4292017-06-13 12:44:56 -04004172
4173int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004174 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004175 case kTexture2DSampler_GrSLType:
4176 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004177 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004178 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004179 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004180 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004181 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004182 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004183 }
4184}
Brian Osman71a18892017-08-10 10:23:25 -04004185
Kevin Lubickf4def342018-10-04 12:52:50 -04004186#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004187#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004188void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4189 // We are called by the base class, which has already called beginObject(). We choose to nest
4190 // all of our caps information in a named sub-object.
4191 writer->beginObject("GL GPU");
4192
4193 const GrGLubyte* str;
4194 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4195 writer->appendString("GL_VERSION", (const char*)(str));
4196 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4197 writer->appendString("GL_RENDERER", (const char*)(str));
4198 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4199 writer->appendString("GL_VENDOR", (const char*)(str));
4200 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4201 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4202
4203 writer->appendName("extensions");
4204 glInterface()->fExtensions.dumpJSON(writer);
4205
4206 writer->endObject();
4207}
Kevin Lubickf4def342018-10-04 12:52:50 -04004208#endif