blob: 61d3ac1eace906925ee28baac8240cca74d123f9 [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
Brian Salomon4cfae3b2020-07-23 10:33:24 -04008#include "src/gpu/gl/GrGLGpu.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPixmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/gpu/GrBackendSemaphore.h"
13#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrTypes.h"
16#include "include/private/SkHalf.h"
17#include "include/private/SkTemplates.h"
18#include "include/private/SkTo.h"
19#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050020#include "src/core/SkCompressedDataUtils.h"
Mike Reed13711eb2020-07-14 17:16:32 -040021#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkTraceEvent.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040023#include "src/gpu/GrBackendUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040025#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040029#include "src/gpu/GrProgramInfo.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040030#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040033#include "src/gpu/GrTexture.h"
Greg Danielc0d69152020-10-08 14:59:00 -040034#include "src/gpu/gl/GrGLAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/gl/GrGLBuffer.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040036#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/gl/GrGLSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#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>
John Stilesfbd050b2020-08-03 13:21:46 -040043#include <memory>
Hal Canaryc640d0d2018-06-13 09:59:02 -040044
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000046#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000047
Brian Salomond8575452020-02-25 12:13:29 -050048#define GL_ALLOC_CALL(call) \
49 [&] { \
50 if (this->glCaps().skipErrorChecks()) { \
51 GR_GL_CALL(this->glInterface(), call); \
52 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
53 } else { \
Brian Salomon24069eb2020-06-24 10:19:52 -040054 this->clearErrorsAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050055 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
Brian Salomon24069eb2020-06-24 10:19:52 -040056 return this->getErrorAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050057 } \
58 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000059
Jim Van Verth32ac83e2016-11-28 15:23:57 -050060//#define USE_NSIGHT
61
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000062///////////////////////////////////////////////////////////////////////////////
63
cdalton8917d622015-05-06 13:40:21 -070064static const GrGLenum gXfermodeEquation2Blend[] = {
65 // Basic OpenGL blend equations.
66 GR_GL_FUNC_ADD,
67 GR_GL_FUNC_SUBTRACT,
68 GR_GL_FUNC_REVERSE_SUBTRACT,
69
70 // GL_KHR_blend_equation_advanced.
71 GR_GL_SCREEN,
72 GR_GL_OVERLAY,
73 GR_GL_DARKEN,
74 GR_GL_LIGHTEN,
75 GR_GL_COLORDODGE,
76 GR_GL_COLORBURN,
77 GR_GL_HARDLIGHT,
78 GR_GL_SOFTLIGHT,
79 GR_GL_DIFFERENCE,
80 GR_GL_EXCLUSION,
81 GR_GL_MULTIPLY,
82 GR_GL_HSL_HUE,
83 GR_GL_HSL_SATURATION,
84 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050085 GR_GL_HSL_LUMINOSITY,
86
87 // Illegal... needs to map to something.
88 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070089};
Brian Salomon4dea72a2019-12-18 10:43:10 -050090static_assert(0 == kAdd_GrBlendEquation);
91static_assert(1 == kSubtract_GrBlendEquation);
92static_assert(2 == kReverseSubtract_GrBlendEquation);
93static_assert(3 == kScreen_GrBlendEquation);
94static_assert(4 == kOverlay_GrBlendEquation);
95static_assert(5 == kDarken_GrBlendEquation);
96static_assert(6 == kLighten_GrBlendEquation);
97static_assert(7 == kColorDodge_GrBlendEquation);
98static_assert(8 == kColorBurn_GrBlendEquation);
99static_assert(9 == kHardLight_GrBlendEquation);
100static_assert(10 == kSoftLight_GrBlendEquation);
101static_assert(11 == kDifference_GrBlendEquation);
102static_assert(12 == kExclusion_GrBlendEquation);
103static_assert(13 == kMultiply_GrBlendEquation);
104static_assert(14 == kHSLHue_GrBlendEquation);
105static_assert(15 == kHSLSaturation_GrBlendEquation);
106static_assert(16 == kHSLColor_GrBlendEquation);
107static_assert(17 == kHSLLuminosity_GrBlendEquation);
108static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700109
twiz@google.com0f31ca72011-03-18 17:38:11 +0000110static const GrGLenum gXfermodeCoeff2Blend[] = {
111 GR_GL_ZERO,
112 GR_GL_ONE,
113 GR_GL_SRC_COLOR,
114 GR_GL_ONE_MINUS_SRC_COLOR,
115 GR_GL_DST_COLOR,
116 GR_GL_ONE_MINUS_DST_COLOR,
117 GR_GL_SRC_ALPHA,
118 GR_GL_ONE_MINUS_SRC_ALPHA,
119 GR_GL_DST_ALPHA,
120 GR_GL_ONE_MINUS_DST_ALPHA,
121 GR_GL_CONSTANT_COLOR,
122 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000123
124 // extended blend coeffs
125 GR_GL_SRC1_COLOR,
126 GR_GL_ONE_MINUS_SRC1_COLOR,
127 GR_GL_SRC1_ALPHA,
128 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500129
130 // Illegal... needs to map to something.
131 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
Brian Salomond978b902019-02-07 15:09:18 -0500134//////////////////////////////////////////////////////////////////////////////
135
136static int gl_target_to_binding_index(GrGLenum target) {
137 switch (target) {
138 case GR_GL_TEXTURE_2D:
139 return 0;
140 case GR_GL_TEXTURE_RECTANGLE:
141 return 1;
142 case GR_GL_TEXTURE_EXTERNAL:
143 return 2;
144 }
145 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500146}
147
148GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500149 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
150}
151
152bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
153 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500154}
155
156void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500157 int targetIndex = gl_target_to_binding_index(target);
158 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
159 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500160}
161
Brian Salomon1f05d452019-02-08 12:33:08 -0500162void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
163 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500164}
165
Brian Salomon1f05d452019-02-08 12:33:08 -0500166void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
167 for (auto& targetBinding : fTargetBindings) {
168 targetBinding.fBoundResourceID.makeInvalid();
169 if (markUnmodified) {
170 targetBinding.fHasBeenModified = false;
171 }
Brian Salomond978b902019-02-07 15:09:18 -0500172 }
173}
174
175//////////////////////////////////////////////////////////////////////////////
176
Brian Salomondc829942018-10-23 16:07:24 -0400177static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
178 switch (filter) {
179 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
Brian Salomona3b02f52020-07-15 16:02:01 -0400180 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR;
Brian Salomondc829942018-10-23 16:07:24 -0400181 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400182 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400183}
184
Brian Salomone69b9ef2020-07-22 11:18:06 -0400185static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter,
186 GrSamplerState::MipmapMode mm) {
187 switch (mm) {
188 case GrSamplerState::MipmapMode::kNone:
189 return filter_to_gl_mag_filter(filter);
Brian Salomonf7353512020-07-22 19:26:48 -0400190 case GrSamplerState::MipmapMode::kNearest:
191 switch (filter) {
192 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_NEAREST;
193 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_NEAREST;
194 }
195 SkUNREACHABLE;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400196 case GrSamplerState::MipmapMode::kLinear:
197 switch (filter) {
198 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_LINEAR;
199 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_LINEAR;
200 }
201 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400202 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400203 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400204}
205
Michael Ludwigf23a1522018-12-10 11:36:13 -0500206static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
207 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400208 switch (wrapMode) {
209 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
210 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
211 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500212 case GrSamplerState::WrapMode::kClampToBorder:
213 // May not be supported but should have been caught earlier
214 SkASSERT(caps.clampToBorderSupport());
215 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500216 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400217 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400218}
219
220///////////////////////////////////////////////////////////////////////////////
221
222class GrGLGpu::SamplerObjectCache {
223public:
224 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
225 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
Brian Salomona0d913b2020-09-01 12:42:26 -0400226 fTextureUnitStates = std::make_unique<UnitState[]>(fNumTextureUnits);
Brian Salomondc829942018-10-23 16:07:24 -0400227 std::fill_n(fSamplers, kNumSamplers, 0);
228 }
229
230 ~SamplerObjectCache() {
231 if (!fNumTextureUnits) {
232 // We've already been abandoned.
233 return;
234 }
Chris Dalton703fe682019-05-15 12:58:12 -0600235 for (GrGLuint sampler : fSamplers) {
236 // The spec states that "zero" values should be silently ignored, however they still
237 // trigger GL errors on some NVIDIA platforms.
238 if (sampler) {
239 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
240 }
241 }
Brian Salomondc829942018-10-23 16:07:24 -0400242 }
243
Brian Salomonccb61422020-01-09 10:46:36 -0500244 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomona7d9f302020-07-15 13:25:30 -0400245 int index = state.asIndex();
Brian Salomondc829942018-10-23 16:07:24 -0400246 if (!fSamplers[index]) {
247 GrGLuint s;
248 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
249 if (!s) {
250 return;
251 }
252 fSamplers[index] = s;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400253 GrGLenum minFilter = filter_to_gl_min_filter(state.filter(), state.mipmapMode());
254 GrGLenum magFilter = filter_to_gl_mag_filter(state.filter());
255 GrGLenum wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
256 GrGLenum wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400257 GR_GL_CALL(fGpu->glInterface(),
258 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
259 GR_GL_CALL(fGpu->glInterface(),
260 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
261 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
262 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
263 }
Brian Salomona0d913b2020-09-01 12:42:26 -0400264 if (!fTextureUnitStates[unitIdx].fKnown ||
265 fTextureUnitStates[unitIdx].fSamplerIDIfKnown != fSamplers[index]) {
Brian Salomondc829942018-10-23 16:07:24 -0400266 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
Brian Salomona0d913b2020-09-01 12:42:26 -0400267 fTextureUnitStates[unitIdx].fSamplerIDIfKnown = fSamplers[index];
268 fTextureUnitStates[unitIdx].fKnown = true;
269 }
270 }
271
272 void unbindSampler(int unitIdx) {
273 if (!fTextureUnitStates[unitIdx].fKnown ||
274 fTextureUnitStates[unitIdx].fSamplerIDIfKnown != 0) {
275 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, 0));
276 fTextureUnitStates[unitIdx].fSamplerIDIfKnown = 0;
277 fTextureUnitStates[unitIdx].fKnown = true;
Brian Salomondc829942018-10-23 16:07:24 -0400278 }
279 }
280
281 void invalidateBindings() {
Brian Salomona0d913b2020-09-01 12:42:26 -0400282 std::fill_n(fTextureUnitStates.get(), fNumTextureUnits, UnitState{});
Brian Salomondc829942018-10-23 16:07:24 -0400283 }
284
285 void abandon() {
Brian Salomona0d913b2020-09-01 12:42:26 -0400286 fTextureUnitStates.reset();
Brian Salomondc829942018-10-23 16:07:24 -0400287 fNumTextureUnits = 0;
288 }
289
290 void release() {
291 if (!fNumTextureUnits) {
292 // We've already been abandoned.
293 return;
294 }
295 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
296 std::fill_n(fSamplers, kNumSamplers, 0);
Brian Salomona0d913b2020-09-01 12:42:26 -0400297 // Deleting a bound sampler implicitly binds sampler 0. We just invalidate all of our
298 // knowledge.
299 std::fill_n(fTextureUnitStates.get(), fNumTextureUnits, UnitState{});
Brian Salomondc829942018-10-23 16:07:24 -0400300 }
301
302private:
Brian Salomona7d9f302020-07-15 13:25:30 -0400303 static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
Brian Salomona0d913b2020-09-01 12:42:26 -0400304 struct UnitState {
305 bool fKnown = false;
306 GrGLuint fSamplerIDIfKnown = 0;
307 };
Brian Salomondc829942018-10-23 16:07:24 -0400308 GrGLGpu* fGpu;
Brian Salomona0d913b2020-09-01 12:42:26 -0400309 std::unique_ptr<UnitState[]> fTextureUnitStates;
Brian Salomondc829942018-10-23 16:07:24 -0400310 GrGLuint fSamplers[kNumSamplers];
311 int fNumTextureUnits;
312};
313
reed@google.comac10a2d2010-12-22 21:39:39 +0000314///////////////////////////////////////////////////////////////////////////////
315
Brian Salomon384fab42017-12-07 12:33:05 -0500316sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
Adlai Holler3d0359a2020-07-09 15:35:55 -0400317 GrDirectContext* direct) {
Brian Salomon384fab42017-12-07 12:33:05 -0500318 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500319 interface = GrGLMakeNativeInterface();
320 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
321 // to GrGLMakeNativeInterface.
322 if (!interface) {
323 interface = sk_ref_sp(GrGLCreateNativeInterface());
324 }
Brian Salomon384fab42017-12-07 12:33:05 -0500325 if (!interface) {
326 return nullptr;
327 }
bsalomon424cc262015-05-22 10:37:30 -0700328 }
Jim Van Verth76334772017-05-05 16:46:05 -0400329#ifdef USE_NSIGHT
330 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
331#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500332 auto glContext = GrGLContext::Make(std::move(interface), options);
333 if (!glContext) {
334 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700335 }
Adlai Holler3d0359a2020-07-09 15:35:55 -0400336 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
bsalomon424cc262015-05-22 10:37:30 -0700337}
338
Adlai Holler3d0359a2020-07-09 15:35:55 -0400339GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* direct)
340 : GrGpu(direct)
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500341 , fGLContext(std::move(ctx))
342 , fProgramCache(new ProgramCache(this))
343 , fHWProgramID(0)
344 , fTempSrcFBOID(0)
345 , fTempDstFBOID(0)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400346 , fStencilClearFBOID(0)
347 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500348 SkASSERT(fGLContext);
Brian Salomon24069eb2020-06-24 10:19:52 -0400349 // Clear errors so we don't get confused whether we caused an error.
350 this->clearErrorsAndCheckForOOM();
351 // Toss out any pre-existing OOM that was hanging around before we got started.
352 this->checkAndResetOOMed();
353
Brian Osmancbdc2612020-11-23 15:30:48 -0500354 this->initCapsAndCompiler(sk_ref_sp(fGLContext->caps()));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000355
Brian Salomond978b902019-02-07 15:09:18 -0500356 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000357
Brian Salomonae64c192019-02-05 09:41:37 -0500358 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
359 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600360 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500361 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500362 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
363 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
364 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
365 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700366 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500367 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
368 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700369 }
Brian Salomonae64c192019-02-05 09:41:37 -0500370 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400371 fHWBufferState[i].invalidate();
372 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600373 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700374
375 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400376 fPathRendering = std::make_unique<GrGLPathRendering>(this);
cdaltone2e71c22016-04-07 18:13:29 -0700377 }
378
Brian Salomona0d913b2020-09-01 12:42:26 -0400379 if (this->glCaps().useSamplerObjects()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400380 fSamplerObjectCache = std::make_unique<SamplerObjectCache>(this);
Brian Salomondc829942018-10-23 16:07:24 -0400381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382}
383
bsalomon861e1032014-12-16 07:33:49 -0800384GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700385 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
386 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800387 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700388 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700389 fMipmapProgramArrayBuffer.reset();
Robert Phillipsae67c522021-03-03 11:03:38 -0500390 if (fProgramCache) {
391 fProgramCache->reset();
392 }
kkinnunen702501d2016-01-13 23:36:45 -0800393
Chris Dalton20e7a532020-02-28 15:37:53 +0000394 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400395 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000396 // detach the current program so there is no confusion on OpenGL's part
397 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000398 GL_CALL(UseProgram(0));
399 }
400
Brian Salomon43f8bf02017-10-18 08:33:29 -0400401 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700402 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800403 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400404 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700405 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800406 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400407 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700408 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800409 }
egdaniel0f5f9672015-02-03 11:10:51 -0800410
bsalomon7ea33f52015-11-22 14:51:00 -0800411 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
412 if (0 != fCopyPrograms[i].fProgram) {
413 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
414 }
bsalomon6df86402015-06-01 10:41:49 -0700415 }
bsalomon6dea83f2015-12-03 12:58:06 -0800416
brianosman33f6b3f2016-06-02 05:49:21 -0700417 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
418 if (0 != fMipmapPrograms[i].fProgram) {
419 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
420 }
421 }
422
Brian Salomondc829942018-10-23 16:07:24 -0400423 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500424
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400425 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700426}
427
bsalomon6e2aad42016-04-01 11:54:31 -0700428void GrGLGpu::disconnect(DisconnectType type) {
429 INHERITED::disconnect(type);
430 if (DisconnectType::kCleanup == type) {
431 if (fHWProgramID) {
432 GL_CALL(UseProgram(0));
433 }
434 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700435 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700436 }
437 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700438 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700439 }
440 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700441 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700442 }
bsalomon6e2aad42016-04-01 11:54:31 -0700443 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
444 if (fCopyPrograms[i].fProgram) {
445 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
446 }
447 }
brianosman33f6b3f2016-06-02 05:49:21 -0700448 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
449 if (fMipmapPrograms[i].fProgram) {
450 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
451 }
452 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400453
Brian Salomondc829942018-10-23 16:07:24 -0400454 if (fSamplerObjectCache) {
455 fSamplerObjectCache->release();
456 }
bsalomon6e2aad42016-04-01 11:54:31 -0700457 } else {
458 if (fProgramCache) {
459 fProgramCache->abandon();
460 }
Brian Salomondc829942018-10-23 16:07:24 -0400461 if (fSamplerObjectCache) {
462 fSamplerObjectCache->abandon();
463 }
bsalomon6e2aad42016-04-01 11:54:31 -0700464 }
465
Chris Dalton20e7a532020-02-28 15:37:53 +0000466 fHWProgram.reset();
Robert Phillipsae67c522021-03-03 11:03:38 -0500467 fProgramCache->reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500468 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700469
bsalomonc8dc1f72014-08-21 13:02:13 -0700470 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700471 fTempSrcFBOID = 0;
472 fTempDstFBOID = 0;
473 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700474 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800475 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
476 fCopyPrograms[i].fProgram = 0;
477 }
brianosman33f6b3f2016-06-02 05:49:21 -0700478 fMipmapProgramArrayBuffer.reset();
479 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
480 fMipmapPrograms[i].fProgram = 0;
481 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400482
jvanverthe9c0fc62015-04-29 11:18:05 -0700483 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700484 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700485 }
Adlai Holler61a591c2020-10-12 12:38:33 -0400486 fFinishCallbacks.callAll(/* doDelete */ DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000487}
488
Robert Phillipsae67c522021-03-03 11:03:38 -0500489GrThreadSafePipelineBuilder* GrGLGpu::pipelineBuilder() {
490 return fProgramCache.get();
491}
492
493sk_sp<GrThreadSafePipelineBuilder> GrGLGpu::refPipelineBuilder() {
494 return fProgramCache;
495}
496
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000497///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000498
bsalomon861e1032014-12-16 07:33:49 -0800499void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000500 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400501 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000502 GL_CALL(Disable(GR_GL_DEPTH_TEST));
503 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000504
Brian Salomonf0861672017-05-08 11:10:10 -0400505 // We don't use face culling.
506 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600507 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
508 // just set this to the default for self-consistency.
509 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400510
Brian Salomonae64c192019-02-05 09:41:37 -0500511 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
512 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700513
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400514 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500515#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000516 // Desktop-only state that we never change
517 if (!this->glCaps().isCoreProfile()) {
518 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
519 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
520 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
521 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
522 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
523 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
524 }
525 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
526 // core profile. This seems like a bug since the core spec removes any mention of
527 // GL_ARB_imaging.
528 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
529 GL_CALL(Disable(GR_GL_COLOR_TABLE));
530 }
531 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400532
Chris Dalton1215cda2019-12-17 21:44:04 -0700533 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500534#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 // Since ES doesn't support glPointSize at all we always use the VS to
536 // set the point size
537 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
538
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000539 }
joshualitt58162332014-08-01 06:44:53 -0700540
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400541 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500542 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700543 // The arm extension requires specifically enabling MSAA fetching per sample.
544 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500545 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700546 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000547 fHWWriteToColor = kUnknown_TriState;
548 // we only ever use lines in hairline mode
549 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700550 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500551
552 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000553 }
edisonn@google.comba669992013-06-28 16:03:21 +0000554
egdanielb414f252014-07-29 13:15:47 -0700555 if (resetBits & kMSAAEnable_GrGLBackendState) {
556 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700557
Chris Dalton6ce447a2019-06-23 18:07:38 -0600558 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800559 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
560 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700561 GL_CALL(CoverageModulation(GR_GL_RGBA));
562 }
Chris Daltonce425af2019-12-16 10:39:03 -0700563
564 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000565 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000566
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000567 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400568 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000569
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500571 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500572 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000573 }
Brian Salomondc829942018-10-23 16:07:24 -0400574 if (fSamplerObjectCache) {
575 fSamplerObjectCache->invalidateBindings();
576 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000577 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000578
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000579 if (resetBits & kBlend_GrGLBackendState) {
580 fHWBlendState.invalidate();
581 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000582
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 if (resetBits & kView_GrGLBackendState) {
584 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700585 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 fHWViewport.invalidate();
587 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 if (resetBits & kStencil_GrGLBackendState) {
590 fHWStencilSettings.invalidate();
591 fHWStencilTestEnabled = kUnknown_TriState;
592 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000593
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 // Vertex
595 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700596 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500597 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
598 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600599 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700600 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000601 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000603 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500604 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700605 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500606 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000608
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000609 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700610 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700611 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000613 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000614
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000615 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000616 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400617 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000618 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
619 }
Brian Salomon1047a492019-07-02 12:25:21 -0400620 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000621 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
622 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 if (this->glCaps().packFlipYSupport()) {
624 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
625 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000626 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000627
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000628 if (resetBits & kProgram_GrGLBackendState) {
629 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000630 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000631 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400632 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000633}
634
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400635static bool check_backend_texture(const GrBackendTexture& backendTex,
636 const GrGLCaps& caps,
637 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400638 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400639 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400640 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400641 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800642 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000643
Brian Salomonea4ad302019-08-07 13:04:55 -0400644 desc->fSize = {backendTex.width(), backendTex.height()};
645 desc->fTarget = info.fTarget;
646 desc->fID = info.fID;
647 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800648
Brian Salomonea4ad302019-08-07 13:04:55 -0400649 if (desc->fFormat == GrGLFormat::kUnknown) {
650 return false;
651 }
652 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400653 if (!caps.shaderCaps()->externalTextureSupport()) {
654 return false;
655 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400656 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
657 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400658 return false;
659 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400660 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400661 return false;
662 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400663 if (backendTex.isProtected()) {
664 // Not supported in GL backend at this time.
665 return false;
666 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400667
Brian Salomond17f6582017-07-19 18:28:58 -0400668 return true;
669}
670
671sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400672 GrWrapOwnership ownership,
673 GrWrapCacheable cacheable,
674 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400675 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400676 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400677 return nullptr;
678 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400679
Brian Salomond17f6582017-07-19 18:28:58 -0400680 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400681 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400682 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400683 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400684 }
bsalomone5286e02016-01-14 09:24:09 -0800685
Brian Salomon40a40622020-07-21 10:32:07 -0400686 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400687 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400688
Brian Salomona6db5102020-07-21 09:56:23 -0400689 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400690 backendTex.getGLTextureParams(), cacheable, ioType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600691 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400692}
693
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500694static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
695 const GrGLCaps& caps, GrGLTexture::Desc* desc,
696 bool skipRectTexSupportCheck = false) {
697 GrGLTextureInfo info;
698 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
699 return false;
700 }
701
702 desc->fSize = {backendTex.width(), backendTex.height()};
703 desc->fTarget = info.fTarget;
704 desc->fID = info.fID;
705 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
706
707 if (desc->fFormat == GrGLFormat::kUnknown) {
708 return false;
709 }
710
711 if (GR_GL_TEXTURE_2D != desc->fTarget) {
712 return false;
713 }
714 if (backendTex.isProtected()) {
715 // Not supported in GL backend at this time.
716 return false;
717 }
718
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500719 return true;
720}
721
Robert Phillipsb915c942019-12-17 14:44:37 -0500722sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
723 GrWrapOwnership ownership,
724 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500725 GrGLTexture::Desc desc;
726 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
727 return nullptr;
728 }
729
730 if (kBorrow_GrWrapOwnership == ownership) {
731 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
732 } else {
733 desc.fOwnership = GrBackendObjectOwnership::kOwned;
734 }
735
Brian Salomon40a40622020-07-21 10:32:07 -0400736 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400737 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500738
Brian Salomona6db5102020-07-21 09:56:23 -0400739 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500740 backendTex.getGLTextureParams(), cacheable,
741 kRead_GrIOType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600742 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500743}
744
Brian Salomond17f6582017-07-19 18:28:58 -0400745sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400746 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500747 GrWrapOwnership ownership,
748 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400749 const GrGLCaps& caps = this->glCaps();
750
Brian Salomonea4ad302019-08-07 13:04:55 -0400751 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400752 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800753 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800754 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400755 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
756 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800757
Brian Salomond17f6582017-07-19 18:28:58 -0400758 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400759 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800760 return nullptr;
761 }
bsalomon10528f12015-10-14 12:54:52 -0700762
Brian Osman766fcbb2017-03-13 09:33:09 -0400763 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400764 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400765 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400766 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800767 }
bsalomonb15b4c12014-10-29 12:41:57 -0700768
Robert Phillips0902c982019-07-16 07:47:56 -0400769
Greg Daniel6fa62e22019-08-07 15:52:37 -0400770 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
771 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000772
Brian Salomonea4ad302019-08-07 13:04:55 -0400773 GrGLRenderTarget::IDs rtIDs;
774 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400775 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000776 }
Greg Daniel177e6952017-10-12 12:27:11 -0400777
Brian Salomon40a40622020-07-21 10:32:07 -0400778 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400779 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400780
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500781 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400782 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400783 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400784 texRT->baseLevelWasBoundToFBO();
Mike Kleina9609ea2020-02-18 13:33:23 -0600785 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000786}
787
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400788sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400789 GrGLFramebufferInfo info;
790 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000791 return nullptr;
792 }
793
Brian Salomone8a766b2019-07-19 14:24:36 -0400794 if (backendRT.isProtected()) {
795 // Not supported in GL at this time.
796 return nullptr;
797 }
798
Brian Salomond4764a12019-08-08 12:08:24 -0400799 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400800 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
801 return nullptr;
802 }
803
Brian Salomonea4ad302019-08-07 13:04:55 -0400804 GrGLRenderTarget::IDs rtIDs;
805 rtIDs.fRTFBOID = info.fFBOID;
806 rtIDs.fMSColorRenderbufferID = 0;
807 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
808 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000809
Greg Daniel6fa62e22019-08-07 15:52:37 -0400810 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700811
Brian Salomona56a7462020-02-07 14:17:25 -0500812 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400813 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000814}
815
Brian Salomonc320b152018-02-20 14:05:36 -0500816static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700817 if (!glTex) {
818 return false;
819 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000820
bsalomone5286e02016-01-14 09:24:09 -0800821 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
822 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800823 return false;
824 }
825
jvanverth17aa0472016-01-05 10:41:27 -0800826 return true;
827}
828
Brian Salomona9b04b92018-06-01 15:04:28 -0400829bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400830 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400831 const GrMipLevel texels[], int mipLevelCount,
832 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500833 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800834
Brian Salomonc320b152018-02-20 14:05:36 -0500835 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800836 return false;
837 }
838
Brian Salomond978b902019-02-07 15:09:18 -0500839 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000840
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400841 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400842 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
843 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
844 glTex->target(), dstRect, srcColorType, texels,
845 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000846}
847
Brian Salomone05ba5a2019-04-08 11:59:07 -0400848bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400849 GrColorType textureColorType, GrColorType bufferColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500850 sk_sp<GrGpuBuffer> transferBuffer, size_t offset,
851 size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400852 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400853
Jim Van Verth1676cb92019-01-15 13:24:45 -0500854 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400855 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500856
Brian Salomonc320b152018-02-20 14:05:36 -0500857 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858 return false;
859 }
860
Hal Canaryc36f7e72018-06-18 15:50:09 -0400861 static_assert(sizeof(int) == sizeof(int32_t), "");
862 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400863 return false;
864 }
865
Brian Salomond978b902019-02-07 15:09:18 -0500866 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400867
868 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500869 SkASSERT(!transferBuffer->isCpuBuffer());
Greg Daniel5ad5a372021-02-01 13:34:04 -0500870 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer.get());
Brian Salomonae64c192019-02-05 09:41:37 -0500871 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400872
Greg Daniel660cc992017-06-26 14:55:05 -0400873 SkDEBUGCODE(
874 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
875 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
876 SkASSERT(bounds.contains(subRect));
877 )
878
Brian Salomonb28cb682019-07-26 12:48:47 -0400879 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400880 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400881 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700882 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400883 return false;
884 }
885
886 bool restoreGLRowLength = false;
887 if (trimRowBytes != rowBytes) {
888 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400889 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400890 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
891 restoreGLRowLength = true;
892 }
893
Greg Danielba88ab62019-07-26 09:14:01 -0400894 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400895 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400896 GrGLenum externalFormat = 0;
897 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400898 this->glCaps().getTexSubImageExternalFormatAndType(
899 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400900 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400901 return false;
902 }
903
Brian Salomon8cbf6622019-07-30 11:03:14 -0400904 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400905 GL_CALL(TexSubImage2D(glTex->target(),
906 0,
907 left, top,
908 width,
909 height,
910 externalFormat, externalType,
911 pixels));
912
913 if (restoreGLRowLength) {
914 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
915 }
916
917 return true;
918}
919
Brian Salomon26de56e2019-04-10 12:14:26 -0400920bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400921 GrColorType surfaceColorType, GrColorType dstColorType,
Greg Daniel5ad5a372021-02-01 13:34:04 -0500922 sk_sp<GrGpuBuffer> transferBuffer, size_t offset) {
923 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer.get());
Brian Salomone05ba5a2019-04-08 11:59:07 -0400924 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400925 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400926 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
927 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400928}
929
Greg Danielf76259e2021-02-04 12:14:04 -0500930void GrGLGpu::unbindXferBuffer(GrGpuBufferType type) {
931 SkASSERT(type == GrGpuBufferType::kXferCpuToGpu || type == GrGpuBufferType::kXferGpuToCpu);
932 auto* xferBufferState = this->hwBufferState(type);
Brian Salomonae64c192019-02-05 09:41:37 -0500933 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
934 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
935 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400936 }
Brian Osman9b560242017-09-05 15:34:52 -0400937}
938
Brian Salomon22558932020-05-15 14:46:34 -0400939bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
940 GrColorType textureColorType,
941 SkISize texDims,
942 GrGLenum target,
943 SkIRect dstRect,
944 GrColorType srcColorType,
945 const GrMipLevel texels[],
946 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400948 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500949
Greg Daniel7bfc9132019-08-14 14:23:53 -0400950 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800951
Brian Salomonf77c1462019-08-01 15:19:29 -0400952 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800953
bsalomon5b30c6f2015-12-17 14:17:34 -0800954 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800955 GrGLenum externalFormat;
956 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400957 this->glCaps().getTexSubImageExternalFormatAndType(
958 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400959 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800960 return false;
961 }
Brian Salomon22558932020-05-15 14:46:34 -0400962 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
963 mipLevelCount);
964 return true;
965}
Brian Salomonf77c1462019-08-01 15:19:29 -0400966
Brian Salomon22558932020-05-15 14:46:34 -0400967bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
968 SkISize texDims,
969 GrGLenum target,
970 SkColor4f color,
971 uint32_t levelMask) {
972 GrColorType colorType;
973 GrGLenum externalFormat, externalType;
974 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
975 &externalType, &colorType);
976 if (colorType == GrColorType::kUnknown) {
977 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000978 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400979
Brian Salomon22558932020-05-15 14:46:34 -0400980 std::unique_ptr<char[]> pixelStorage;
981 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400982 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400983 SkSTArray<16, GrMipLevel> levels;
984 levels.resize(numLevels);
985 SkISize levelDims = texDims;
986 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
987 std::max(levelDims.height() >> 1, 1)}) {
988 if (levelMask & (1 << i)) {
989 if (!pixelStorage) {
990 // Make one tight image at the first size and reuse it for smaller levels.
991 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
992 size_t rb = ii.minRowBytes();
993 pixelStorage.reset(new char[rb * levelDims.height()]);
994 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
995 return false;
996 }
997 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000998 }
Brian Salomon22558932020-05-15 14:46:34 -0400999 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
1000 }
1001 }
1002 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
1003 bpp, levels.begin(), levels.count());
1004 return true;
1005}
1006
1007void GrGLGpu::uploadTexData(SkISize texDims,
1008 GrGLenum target,
1009 SkIRect dstRect,
1010 GrGLenum externalFormat,
1011 GrGLenum externalType,
1012 size_t bpp,
1013 const GrMipLevel texels[],
1014 int mipLevelCount) {
1015 SkASSERT(!texDims.isEmpty());
1016 SkASSERT(!dstRect.isEmpty());
1017 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001018 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001019 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1020
1021 const GrGLCaps& caps = this->glCaps();
1022
1023 bool restoreGLRowLength = false;
1024
Greg Danielf76259e2021-02-04 12:14:04 -05001025 this->unbindXferBuffer(GrGpuBufferType::kXferCpuToGpu);
Brian Salomon22558932020-05-15 14:46:34 -04001026 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1027
1028 SkISize dims = dstRect.size();
1029 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1030 std::max(dims.height() >> 1, 1)}) {
1031 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001032 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001033 }
Brian Salomon22558932020-05-15 14:46:34 -04001034 const size_t trimRowBytes = dims.width() * bpp;
1035 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001036
1037 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1038 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001039 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001040 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001041 } else {
1042 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 }
1044
Brian Salomon22558932020-05-15 14:46:34 -04001045 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1046 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001047 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001048 if (restoreGLRowLength) {
1049 SkASSERT(caps.writePixelsRowBytesSupport());
1050 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1051 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001052}
1053
Greg Daniel01f278c2020-06-12 16:58:17 -04001054bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1055 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001056 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001057 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001058 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001059 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001060 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001061 const GrGLCaps& caps = this->glCaps();
1062
1063 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001064 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001066 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001067 }
1068
Robert Phillips9f744f72019-12-19 19:14:33 -05001069 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1070
Greg Daniel7bfc9132019-08-14 14:23:53 -04001071 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001072
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001073 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001074 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001075 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001076 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001077
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001078 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001079 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080
1081 if (useTexStorage) {
1082 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001083 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1084 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001085 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001086 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087 }
Robert Phillips42716d42019-12-16 12:19:54 -05001088
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001089 size_t offset = 0;
1090 for (int level = 0; level < numMipLevels; ++level) {
1091
Robert Phillips99dead92020-01-27 16:11:57 -05001092 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1093 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001094
Brian Salomond8575452020-02-25 12:13:29 -05001095 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1096 level,
1097 0, // left
1098 0, // top
1099 dimensions.width(),
1100 dimensions.height(),
1101 internalFormat,
1102 SkToInt(levelDataSize),
1103 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001104
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001105 if (error != GR_GL_NO_ERROR) {
1106 return false;
1107 }
1108
Robert Phillips9f744f72019-12-19 19:14:33 -05001109 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001110 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001111 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001112 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001113 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001114
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001115 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001116 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1117 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001118
1119 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001120 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1121 level,
1122 internalFormat,
1123 dimensions.width(),
1124 dimensions.height(),
1125 0, // border
1126 SkToInt(levelDataSize),
1127 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001128
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129 if (error != GR_GL_NO_ERROR) {
1130 return false;
1131 }
1132
Robert Phillips9f744f72019-12-19 19:14:33 -05001133 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001134 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001135 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001136 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001137 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001138}
1139
Brian Salomond8575452020-02-25 12:13:29 -05001140bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001141 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001142 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001143 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001144 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001145 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001146 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1147 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001148 break;
1149 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001150 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1151 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001152 break;
1153 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1154 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001155 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1156 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001157 break;
1158 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001159 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001160 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 }
Brian Salomond8575452020-02-25 12:13:29 -05001162 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001163}
1164
Brian Salomonea4ad302019-08-07 13:04:55 -04001165bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001166 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 GrGLRenderTarget::IDs* rtIDs) {
1168 rtIDs->fMSColorRenderbufferID = 0;
1169 rtIDs->fRTFBOID = 0;
1170 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1171 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172
bsalomona11e5fc2015-12-18 07:59:41 -08001173 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001174
Brian Salomonea4ad302019-08-07 13:04:55 -04001175 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001176 goto FAILED;
1177 }
1178
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001179 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001180 goto FAILED;
1181 }
1182
Brian Salomonea4ad302019-08-07 13:04:55 -04001183 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1184 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 goto FAILED;
1186 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001187
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001188 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1189 // the texture bound to the other. The exception is the IMG multisample extension. With this
1190 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1191 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001192 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1194 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1195 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 goto FAILED;
1197 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 }
1202
egdanield803f272015-03-18 13:01:52 -07001203 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001204 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001206 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001208 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1209 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 goto FAILED;
1211 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001212 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001214 GR_GL_COLOR_ATTACHMENT0,
1215 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001216 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001218 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001220 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001221 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1222 GR_GL_COLOR_ATTACHMENT0,
1223 desc.fTarget,
1224 desc.fID,
1225 0,
1226 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001227 } else {
egdanield803f272015-03-18 13:01:52 -07001228 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001229 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 desc.fTarget,
1231 desc.fID,
1232 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234
1235 return true;
1236
1237FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001238 if (rtIDs->fMSColorRenderbufferID) {
1239 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1242 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 if (rtIDs->fTexFBOID) {
1245 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 }
1247 return false;
1248}
1249
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001250// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001251static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001252// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001253 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001254}
1255
Brian Salomone2826ab2019-06-04 15:58:31 -04001256static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001257 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001258 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1259 // drivers have a bug where an FBO won't be complete if it includes a
1260 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001261 GrGLTextureParameters::SamplerOverriddenState state;
1262 state.fMinFilter = GR_GL_NEAREST;
1263 state.fMagFilter = GR_GL_NEAREST;
1264 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1265 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1267 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1268 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1269 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001270 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001271}
1272
Brian Salomona56a7462020-02-07 14:17:25 -05001273sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001274 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001275 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001276 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001277 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001278 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001279 int mipLevelCount,
1280 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001281 // We don't support protected textures in GL.
1282 if (isProtected == GrProtected::kYes) {
1283 return nullptr;
1284 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001285 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001286
Brian Salomond2a8ae22019-09-10 16:03:59 -04001287 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001288 GrMipmapStatus mipmapStatus =
1289 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001290 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001291 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001292 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001293 switch (format.textureType()) {
1294 case GrTextureType::kExternal:
1295 case GrTextureType::kNone:
1296 return nullptr;
1297 case GrTextureType::k2D:
1298 texDesc.fTarget = GR_GL_TEXTURE_2D;
1299 break;
1300 case GrTextureType::kRectangle:
1301 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1302 return nullptr;
1303 }
1304 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1305 break;
1306 }
Brian Salomon81536f22019-08-08 16:30:49 -04001307 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001308 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001309 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1310 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001311
Brian Salomon0f396992020-06-19 19:51:21 -04001312 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1313 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001314
1315 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001316 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
Robert Phillips67d52cf2017-06-05 13:38:13 -04001319 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001320 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001321 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001322 GL_CALL(BindTexture(texDesc.fTarget, 0));
1323 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001324
Brian Salomonea4ad302019-08-07 13:04:55 -04001325 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1326 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 return return_null_texture();
1328 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001329 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001330 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001331 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001332 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001333 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001335 // The non-sampler params are still at their default values.
1336 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1337 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001338 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001339 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001340 GrGLenum externalFormat, externalType;
1341 GrColorType colorType;
1342 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1343 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001344 for (int i = 0; i < mipLevelCount; ++i) {
1345 if (levelClearMask & (1U << i)) {
1346 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1347 nullptr));
1348 }
1349 }
1350 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1351 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001352 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001353 this->disableWindowRectangles();
1354 this->flushColorWrite(true);
Brian Salomon07bc9a22020-12-02 13:37:16 -05001355 this->flushClearColor({0, 0, 0, 0});
Brian Salomond2a8ae22019-09-10 16:03:59 -04001356 for (int i = 0; i < mipLevelCount; ++i) {
1357 if (levelClearMask & (1U << i)) {
1358 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1359 kDst_TempFBOTarget);
1360 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1361 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1362 }
1363 }
Brian Salomonc2249852019-09-16 11:08:50 -04001364 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001365 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001366 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001367 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001368 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001369 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001370 }
1371 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001372 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001373}
1374
Robert Phillips9f744f72019-12-19 19:14:33 -05001375sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001376 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001377 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001378 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001379 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001380 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001381 // We don't support protected textures in GL.
1382 if (isProtected == GrProtected::kYes) {
1383 return nullptr;
1384 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001385 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1386
Brian Salomonbb8dde82019-06-27 10:52:13 -04001387 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001388 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001389 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001390 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001391 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001392 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001393 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001394 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001395 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001396 return nullptr;
1397 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001398
Greg Danielaaf738c2020-07-10 09:30:33 -04001399 if (data) {
1400 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1401 GR_GL_TEXTURE_2D, data, dataSize)) {
1402 GL_CALL(DeleteTextures(1, &desc.fID));
1403 return nullptr;
1404 }
1405 }
1406
Robert Phillipsee946932019-12-18 11:16:17 -05001407 // Unbind this texture from the scratch texture unit.
1408 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1409
Brian Salomona6db5102020-07-21 09:56:23 -04001410 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1411 ? GrMipmapStatus::kValid
1412 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001413
Brian Salomona6db5102020-07-21 09:56:23 -04001414 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001415 // The non-sampler params are still at their default values.
1416 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1417 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001418 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001419}
1420
Greg Danielc1ad77c2020-05-06 11:40:03 -04001421GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001422 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001423 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001424 // We don't support protected textures in GL.
1425 if (isProtected == GrProtected::kYes) {
1426 return {};
1427 }
1428
1429 this->handleDirtyContext();
1430
1431 GrGLFormat glFormat = format.asGLFormat();
1432 if (glFormat == GrGLFormat::kUnknown) {
1433 return {};
1434 }
1435
Greg Daniel01f278c2020-06-12 16:58:17 -04001436 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1437
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001438 GrGLTextureInfo info;
1439 GrGLTextureParameters::SamplerOverriddenState initialState;
1440
1441 info.fTarget = GR_GL_TEXTURE_2D;
1442 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001443 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001444 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001445 if (!info.fID) {
1446 return {};
1447 }
1448
1449 // Unbind this texture from the scratch texture unit.
1450 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1451
1452 auto parameters = sk_make_sp<GrGLTextureParameters>();
1453 // The non-sampler params are still at their default values.
1454 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1455 fResetTimestampForTextureParameters);
1456
1457 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1458 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001459}
1460
Greg Danielaaf738c2020-07-10 09:30:33 -04001461bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1462 sk_sp<GrRefCntedCallback> finishedCallback,
1463 const BackendTextureData* data) {
1464 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1465
Robert Phillips489e5b92021-01-26 09:42:03 -05001466 this->handleDirtyContext();
1467
Greg Danielaaf738c2020-07-10 09:30:33 -04001468 GrGLTextureInfo info;
1469 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1470
1471 GrBackendFormat format = backendTexture.getBackendFormat();
1472 GrGLFormat glFormat = format.asGLFormat();
1473 if (glFormat == GrGLFormat::kUnknown) {
1474 return false;
1475 }
1476 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1477
Brian Salomon40a40622020-07-21 10:32:07 -04001478 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001479
1480 const char* rawData = nullptr;
1481 size_t rawDataSize = 0;
1482 SkAutoMalloc am;
1483 if (data->type() == BackendTextureData::Type::kCompressed) {
1484 rawData = (const char*)data->compressedData();
1485 rawDataSize = data->compressedSize();
1486 } else {
1487 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1488 SkASSERT(compression != SkImage::CompressionType::kNone);
1489
1490 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001491 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001492
1493 am.reset(rawDataSize);
1494
1495 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1496 data->color());
1497
1498 rawData = (const char*)am.get();
1499 }
1500
1501 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001502
1503 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1504 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04001505 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelControlSupport()) {
Greg Daniel95afafb2020-07-22 12:09:26 -04001506 auto params = backendTexture.getGLTextureParams();
1507 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1508 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1509 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1510 nonsamplerState.fBaseMipMapLevel = 0;
1511 }
1512 int numMipLevels =
1513 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1514 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1515 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1516 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1517 }
1518 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1519 }
1520
Greg Danielaaf738c2020-07-10 09:30:33 -04001521 bool result = this->uploadCompressedTexData(
1522 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1523 rawData, rawDataSize);
1524
1525 // Unbind this texture from the scratch texture unit.
1526 this->bindTextureToScratchUnit(info.fTarget, 0);
1527
1528 return result;
1529}
1530
Brian Salomon5043f1f2019-07-11 21:27:54 -04001531int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001532 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001533 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1534
1535 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001536 // Default to unsupported, set this if we find a stencil format that works.
1537 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001538
Brian Salomon0f396992020-06-19 19:51:21 -04001539 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1540 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001541 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001542 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001543 }
egdanielff1d5472015-09-10 08:37:20 -07001544 // unbind the texture from the texture unit before binding it to the frame buffer
1545 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1546
1547 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001548 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001549 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001550 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001551 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001552 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1553 GR_GL_COLOR_ATTACHMENT0,
1554 GR_GL_TEXTURE_2D,
1555 colorID,
1556 0));
bsalomon30447372015-12-21 09:03:05 -08001557 GrGLuint sbRBID = 0;
1558 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001559
1560 // look over formats till I find a compatible one
1561 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001562 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001563 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001564 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001565 GrGLFormat sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001566 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
Greg Daniel8ade5e82020-10-07 13:09:48 -04001567 GR_GL_RENDERBUFFER, GrGLFormatToEnum(sFmt), kSize, kSize));
Brian Salomond8575452020-02-25 12:13:29 -05001568 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001570 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001571 GR_GL_RENDERBUFFER, sbRBID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001572 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
bsalomon30447372015-12-21 09:03:05 -08001573 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1574 GR_GL_DEPTH_ATTACHMENT,
1575 GR_GL_RENDERBUFFER, sbRBID));
1576 } else {
1577 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1578 GR_GL_DEPTH_ATTACHMENT,
1579 GR_GL_RENDERBUFFER, 0));
1580 }
1581 GrGLenum status;
1582 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1583 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1584 firstWorkingStencilFormatIndex = i;
1585 break;
1586 }
egdanielff1d5472015-09-10 08:37:20 -07001587 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1588 GR_GL_STENCIL_ATTACHMENT,
1589 GR_GL_RENDERBUFFER, 0));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001590 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
egdanielff1d5472015-09-10 08:37:20 -07001591 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1592 GR_GL_DEPTH_ATTACHMENT,
1593 GR_GL_RENDERBUFFER, 0));
1594 }
egdanielff1d5472015-09-10 08:37:20 -07001595 }
1596 }
bsalomon30447372015-12-21 09:03:05 -08001597 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001598 }
1599 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001600 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1601 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001602 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001603 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001604 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001605}
1606
Brian Salomonea4ad302019-08-07 13:04:55 -04001607GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001608 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001609 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001610 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001611 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001612 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001613 if (format == GrGLFormat::kUnknown) {
1614 return 0;
1615 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001616 GrGLuint id = 0;
1617 GL_CALL(GenTextures(1, &id));
1618 if (!id) {
1619 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001620 }
1621
Brian Salomonea4ad302019-08-07 13:04:55 -04001622 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001623
Brian Salomonea4ad302019-08-07 13:04:55 -04001624 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1625
Brian Salomonea4ad302019-08-07 13:04:55 -04001626 return id;
1627}
1628
Brian Salomon0f396992020-06-19 19:51:21 -04001629GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1630 GrGLFormat format,
1631 GrGLenum target,
1632 GrRenderable renderable,
1633 GrGLTextureParameters::SamplerOverriddenState* initialState,
1634 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001635 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001636 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001637
Brian Salomonea4ad302019-08-07 13:04:55 -04001638 GrGLuint id = 0;
1639 GL_CALL(GenTextures(1, &id));
1640
1641 if (!id) {
1642 return 0;
1643 }
1644
Brian Salomon0f396992020-06-19 19:51:21 -04001645 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001646
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001647 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001648 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001649 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001650 }
1651
Brian Salomond2a8ae22019-09-10 16:03:59 -04001652 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001653 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001654 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001655 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001656 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001657
1658 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1659
1660 bool success = false;
1661 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001662 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001663 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001664 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1665 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001666 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001667 } else {
1668 GrGLenum externalFormat, externalType;
1669 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1670 GrGLenum error = GR_GL_NO_ERROR;
1671 if (externalFormat && externalType) {
1672 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1673 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001674 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1675 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001676 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1677 currentHeight, 0, externalFormat, externalType,
1678 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001679 }
Brian Salomond8575452020-02-25 12:13:29 -05001680 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001681 }
1682 }
1683 }
1684 if (success) {
1685 return id;
1686 }
1687 GL_CALL(DeleteTextures(1, &id));
1688 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001689}
1690
Greg Danielc0d69152020-10-08 14:59:00 -04001691sk_sp<GrAttachment> GrGLGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1692 SkISize dimensions,
1693 int numStencilSamples) {
Greg Daniele77162e2020-09-21 15:32:11 -04001694 SkASSERT(dimensions.width() >= rt->width());
1695 SkASSERT(dimensions.height() >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001696
Greg Danielc0d69152020-10-08 14:59:00 -04001697 GrGLAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001698
Brian Salomond4764a12019-08-08 12:08:24 -04001699 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001700 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001701 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001702 }
egdanielff1d5472015-09-10 08:37:20 -07001703
1704 if (!sbDesc.fRenderbufferID) {
1705 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1706 }
1707 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001708 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001709 }
1710 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001711 GrGLFormat sFmt = this->glCaps().stencilFormats()[sIdx];
1712 GrGLenum glFmt = GrGLFormatToEnum(sFmt);
egdanielff1d5472015-09-10 08:37:20 -07001713 // we do this "if" so that we don't call the multisample
1714 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001715 if (numStencilSamples > 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001716 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001717 dimensions.width(), dimensions.height())) {
Brian Salomond8575452020-02-25 12:13:29 -05001718 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1719 return nullptr;
1720 }
egdanielff1d5472015-09-10 08:37:20 -07001721 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001722 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001723 dimensions.width(),
1724 dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001725 if (error != GR_GL_NO_ERROR) {
1726 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1727 return nullptr;
1728 }
egdanielff1d5472015-09-10 08:37:20 -07001729 }
1730 fStats.incStencilAttachmentCreates();
Greg Daniel17b0c752020-10-01 16:06:37 -04001731
Greg Danielc0d69152020-10-08 14:59:00 -04001732 return sk_sp<GrAttachment>(new GrGLAttachment(
Greg Daniel25ebd952020-11-03 11:17:05 -05001733 this, sbDesc, dimensions, GrAttachment::UsageFlags::kStencilAttachment,
1734 numStencilSamples, sFmt));
reed@google.comac10a2d2010-12-22 21:39:39 +00001735}
1736
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001737////////////////////////////////////////////////////////////////////////////////
1738
Brian Salomondbf70722019-02-07 11:31:24 -05001739sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1740 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001741 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001742}
1743
Chris Dalton2e7ed262020-02-21 15:17:59 -07001744void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1745 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001746 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1747 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1748 fHWScissorSettings.fEnabled = kYes_TriState;
1749 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001750 } else {
1751 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1752 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1753 fHWScissorSettings.fEnabled = kNo_TriState;
1754 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001756}
Brian Salomond818ebf2018-07-02 14:08:49 +00001757
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001758void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtHeight, GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001759 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001760 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1761 if (fHWScissorSettings.fRect != nativeScissor) {
1762 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1763 nativeScissor.fHeight));
1764 fHWScissorSettings.fRect = nativeScissor;
1765 }
joshualitt77b13072014-10-27 14:51:01 -07001766}
1767
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001768void GrGLGpu::flushViewport(const SkIRect& viewport, int rtHeight, GrSurfaceOrigin rtOrigin) {
1769 auto nativeViewport = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, viewport);
1770 if (fHWViewport != nativeViewport) {
1771 GL_CALL(Viewport(nativeViewport.fX, nativeViewport.fY,
1772 nativeViewport.fWidth, nativeViewport.fHeight));
1773 fHWViewport = nativeViewport;
1774 }
1775}
1776
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001777void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001778 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001779#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001780 typedef GrWindowRectsState::Mode Mode;
1781 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1782 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001783
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001784 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001785 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001786 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001787 }
1788
csmartdalton7535f412016-08-23 06:51:00 -07001789 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1790 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001791 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001792 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001793
Chris Daltond6cda8d2019-09-05 02:30:04 -06001794 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001795 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001796 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001797 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001798 }
1799
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001800 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001801 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001802
Greg Danielacd66b42019-05-22 16:29:12 -04001803 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001804#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001805}
1806
1807void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001808#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001809 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001810 return;
1811 }
1812 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001813 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001814#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001815}
1816
Robert Phillipsdf546db2020-05-29 09:42:54 -04001817bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001818 this->handleDirtyContext();
1819
Chris Daltond42d2002020-02-28 12:05:04 -07001820 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001821 if (!program) {
1822 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1823 return false;
1824 }
1825
1826 this->flushProgram(std::move(program));
1827
Chris Daltond42d2002020-02-28 12:05:04 -07001828 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1829 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1830 }
1831
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001832 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001833 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001834 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001835
Chris Dalton20e7a532020-02-28 15:37:53 +00001836 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001837
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001838 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001839 GrStencilSettings stencil;
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001840 if (programInfo.isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001841 SkASSERT(glRT->getStencilAttachment());
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001842 stencil.reset(*programInfo.userStencilSettings(),
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001843 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001844 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001845 }
Robert Phillips901aff02019-10-08 12:32:56 -04001846 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001847 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001848 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1849 glRT, programInfo.origin());
1850 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001851 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001852 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001853
1854 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001855 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001856 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001857
Chris Dalton20e7a532020-02-28 15:37:53 +00001858 return true;
1859}
1860
1861void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1862 if (!program) {
1863 fHWProgram.reset();
1864 fHWProgramID = 0;
1865 return;
1866 }
1867 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1868 if (program == fHWProgram) {
1869 return;
1870 }
1871 auto id = program->programID();
1872 SkASSERT(id);
1873 GL_CALL(UseProgram(id));
1874 fHWProgram = std::move(program);
1875 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001876}
1877
1878void GrGLGpu::flushProgram(GrGLuint id) {
1879 SkASSERT(id);
1880 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001881 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001882 return;
1883 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001884 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001885 GL_CALL(UseProgram(id));
1886 fHWProgramID = id;
1887}
1888
Brian Salomonae64c192019-02-05 09:41:37 -05001889GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001890 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001891
cdaltone2e71c22016-04-07 18:13:29 -07001892 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001893 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001894 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001895 }
cdaltone2e71c22016-04-07 18:13:29 -07001896
Brian Salomonae64c192019-02-05 09:41:37 -05001897 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001898 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001899 if (!bufferState->fBufferZeroKnownBound) {
1900 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1901 bufferState->fBufferZeroKnownBound = true;
1902 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001903 }
Brian Salomondbf70722019-02-07 11:31:24 -05001904 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1905 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001906 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1907 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1908 bufferState->fBufferZeroKnownBound = false;
1909 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001910 }
1911
Brian Salomonae64c192019-02-05 09:41:37 -05001912 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001913}
Brian Salomond818ebf2018-07-02 14:08:49 +00001914
Brian Salomon07bc9a22020-12-02 13:37:16 -05001915void GrGLGpu::clear(const GrScissorState& scissor,
1916 std::array<float, 4> color,
1917 GrRenderTarget* target,
1918 GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001919 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001920 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001921 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001922 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001923
Brian Salomon43f8bf02017-10-18 08:33:29 -04001924 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001925
Brian Salomon43f8bf02017-10-18 08:33:29 -04001926 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1927
Michael Ludwig1e632792020-05-21 12:45:31 -04001928 if (scissor.enabled()) {
1929 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001930 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001931 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001932 }
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001933 this->flushScissor(scissor, glRT->height(), origin);
Michael Ludwig1e632792020-05-21 12:45:31 -04001934 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001935 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001936 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001938}
1939
Chris Daltonb50cc812019-10-14 16:29:21 -06001940static bool use_tiled_rendering(const GrGLCaps& glCaps,
1941 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1942 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1943 // Otherwise it's faster to just not use it.
1944 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1945 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1946}
1947
1948void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001949 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1950 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1951 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1952
1953 this->handleDirtyContext();
1954
1955 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1956 this->flushRenderTarget(glRT);
1957 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1958
Chris Daltonb50cc812019-10-14 16:29:21 -06001959 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1960 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1961 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1962 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1963 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1964 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1965 nativeBounds.fHeight, preserveMask));
1966 }
1967
Chris Dalton674f77a2019-09-30 20:49:39 -06001968 GrGLbitfield clearMask = 0;
1969 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1970 SkASSERT(!this->caps()->performColorClearsAsDraws());
1971 this->flushClearColor(colorLoadStore.fClearColor);
1972 this->flushColorWrite(true);
1973 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001974 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001975 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1976 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1977 GL_CALL(StencilMask(0xffffffff));
1978 GL_CALL(ClearStencil(0));
1979 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1980 }
1981 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001982 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001983 this->disableWindowRectangles();
1984 GL_CALL(Clear(clearMask));
1985 }
1986}
1987
1988void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1989 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1990 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1991 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1992
1993 this->handleDirtyContext();
1994
1995 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1996 // The framebuffer binding changed in the middle of a command buffer. We should have already
1997 // printed a warning during onFBOChanged.
1998 return;
1999 }
2000
2001 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2002 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2003
2004 SkSTArray<2, GrGLenum> discardAttachments;
2005 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2006 discardAttachments.push_back(
2007 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2008 }
2009 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2010 discardAttachments.push_back(
2011 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2012 }
2013
2014 if (!discardAttachments.empty()) {
2015 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2016 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2017 discardAttachments.begin()));
2018 } else {
2019 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2020 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2021 discardAttachments.begin()));
2022 }
2023 }
2024 }
2025
Chris Daltonb50cc812019-10-14 16:29:21 -06002026 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2027 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2028 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2029 // Handled by use_tiled_rendering().
2030 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2031 GL_CALL(EndTiling(preserveMask));
2032 }
2033
Chris Dalton674f77a2019-09-30 20:49:39 -06002034 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002035}
2036
Michael Ludwig1e632792020-05-21 12:45:31 -04002037void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002038 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002039 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002040 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002041 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002042 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002043
Greg Danielc0d69152020-10-08 14:59:00 -04002044 GrAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002045 if (!sb) {
2046 // We should only get here if we marked a proxy as requiring a SB. However,
2047 // the SB creation could later fail. Likely clipping is going to go awry now.
2048 return;
2049 }
2050
Greg Daniel8ade5e82020-10-07 13:09:48 -04002051 GrGLint stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat());
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002052#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002053 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002054 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002055#else
2056 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002057 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002058 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002059 // changing the clip between stencil passes may or may not
2060 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002061 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002062#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002063 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002064 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002065 value = (1 << (stencilBitCount - 1));
2066 } else {
2067 value = 0;
2068 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002069 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002070 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002071
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002072 this->flushScissor(scissor, glRT->height(), origin);
Michael Ludwig1e632792020-05-21 12:45:31 -04002073 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002074
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002075 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002076 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002077 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002078 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002079}
2080
Brian Salomone05ba5a2019-04-08 11:59:07 -04002081bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002082 GrColorType surfaceColorType, GrColorType dstColorType,
2083 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002084 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002085
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002086 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002087 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002088 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002089 return false;
2090 }
Greg Danielba88ab62019-07-26 09:14:01 -04002091 GrGLenum externalFormat = 0;
2092 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002093 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2094 surfaceColorType,
2095 dstColorType,
2096 &externalFormat,
2097 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002098 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002099 return false;
2100 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002101
Brian Salomon71d9d842016-11-03 13:42:00 -04002102 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002103 if (renderTarget->numSamples() <= 1 ||
2104 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2105 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2106 this->flushRenderTargetNoColorWrites(renderTarget);
2107 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2108 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2109 return false;
2110 } else {
2111 SkASSERT(renderTarget->requiresManualMSAAResolve());
2112 // we don't track the state of the READ FBO ID.
2113 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002114 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002115 } else {
2116 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002117 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002118 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002119 }
2120
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002121 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002122 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002123
Brian Salomona6948702018-06-01 15:33:20 -04002124 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002125 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002126 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002127 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002128 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002129 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002130
Brian Osman1348ed02018-09-12 09:08:39 -04002131 bool reattachStencil = false;
2132 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2133 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002134 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002135 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002136 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2137 reattachStencil = true;
2138 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2139 GR_GL_RENDERBUFFER, 0));
2140 }
2141
Chris Dalton76500e52019-09-05 02:13:05 -06002142 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002143 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002144
2145 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002146 auto* stencilAttachment =
Greg Danielc0d69152020-10-08 14:59:00 -04002147 static_cast<GrGLAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002148 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2149 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2150 }
2151
Brian Salomon26de56e2019-04-10 12:14:26 -04002152 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002153 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002154 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2155 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002156
Brian Salomone05ba5a2019-04-08 11:59:07 -04002157 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002158 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002159 }
2160 return true;
2161}
2162
2163bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002164 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2165 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002166 SkASSERT(surface);
2167
Brian Salomonb28cb682019-07-26 12:48:47 -04002168 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002169
Brian Salomon26de56e2019-04-10 12:14:26 -04002170 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2171 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002172
Brian Salomon1047a492019-07-02 12:25:21 -04002173 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002174 rowPixelWidth = width;
2175 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002176 SkASSERT(!(rowBytes % bytesPerPixel));
2177 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002178 }
Greg Danielf76259e2021-02-04 12:14:04 -05002179 this->unbindXferBuffer(GrGpuBufferType::kXferGpuToCpu);
Brian Salomonf77c1462019-08-01 15:19:29 -04002180 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2181 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002182}
2183
Greg Daniel65476e02020-10-27 09:20:20 -04002184GrOpsRenderPass* GrGLGpu::onGetOpsRenderPass(
Greg Danielc0d69152020-10-08 14:59:00 -04002185 GrRenderTarget* rt,
2186 GrAttachment*,
2187 GrSurfaceOrigin origin,
2188 const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002189 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002190 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Greg Daniel9a18b082020-08-14 14:03:50 -04002191 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -04002192 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002193 if (!fCachedOpsRenderPass) {
John Stilesfbd050b2020-08-03 13:21:46 -04002194 fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002195 }
2196
Chris Daltonb50cc812019-10-14 16:29:21 -06002197 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002198 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002199}
2200
Brian Salomon1fabd512018-02-09 09:54:25 -05002201void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002202 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002203 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002204 this->didWriteToSurface(target, origin, &bounds);
2205}
bsalomon6ba6fa12015-03-04 11:57:37 -08002206
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002207void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2208 this->flushRenderTargetNoColorWrites(target);
2209 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002210}
2211
Brian Osman9aa30c62018-07-02 15:21:46 -04002212void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002213 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002214 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002215 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002216 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002217#ifdef SK_DEBUG
2218 // don't do this check in Chromium -- this is causing
2219 // lots of repeated command buffer flushes when the compositor is
2220 // rendering with Ganesh, which is really slow; even too slow for
2221 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002222 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002223 GrGLenum status;
2224 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2225 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002226 SkDebugf("GrGLGpu::flushRenderTargetNoColorWrites glCheckFramebufferStatus %x\n",
2227 status);
egdanield803f272015-03-18 13:01:52 -07002228 }
bsalomon160f24c2015-03-17 15:55:42 -07002229 }
egdanield803f272015-03-18 13:01:52 -07002230#endif
2231 fHWBoundRenderTargetUniqueID = rtID;
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002232 this->flushViewport(SkIRect::MakeSize(target->dimensions()),
2233 target->height(),
2234 kTopLeft_GrSurfaceOrigin); // the origin is irrelevant in this case
brianosman64d094d2016-03-25 06:01:59 -07002235 }
Brian Salomona2984312020-12-15 11:49:19 -05002236 if (this->caps()->workarounds().force_update_scissor_state_when_binding_fbo0) {
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002237 // The driver forgets the correct scissor state when using FBO 0.
2238 if (!fHWScissorSettings.fRect.isInvalid()) {
2239 const GrNativeRect& r = fHWScissorSettings.fRect;
2240 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
2241 }
2242 if (fHWScissorSettings.fEnabled == kYes_TriState) {
2243 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2244 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2245 } else if (fHWScissorSettings.fEnabled == kNo_TriState) {
2246 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2247 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2248 }
2249 }
brianosman64d094d2016-03-25 06:01:59 -07002250
brianosman35b784d2016-05-05 11:52:53 -07002251 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002252 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002253 }
Brian Salomon9b553272020-01-24 14:38:37 -05002254
2255 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2256 GrGLint format;
2257 GrGLint type;
2258 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2259 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2260 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2261 }
bsalomon083617b2016-02-12 12:10:14 -08002262}
bsalomona9909122016-01-23 10:41:40 -08002263
brianosman33f6b3f2016-06-02 05:49:21 -07002264void GrGLGpu::flushFramebufferSRGB(bool enable) {
2265 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2266 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2267 fHWSRGBFramebuffer = kYes_TriState;
2268 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2269 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2270 fHWSRGBFramebuffer = kNo_TriState;
2271 }
2272}
2273
Chris Dalton33db9fc2020-02-25 18:52:12 -07002274GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002275 fStats.incNumDraws();
2276
Chris Dalton2e7ed262020-02-21 15:17:59 -07002277 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2278 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2279 GL_CALL(Enable(GR_GL_CULL_FACE));
2280 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002281 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002282 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002283
Chris Dalton3809bab2017-06-13 10:55:06 -06002284 switch (primitiveType) {
2285 case GrPrimitiveType::kTriangles:
2286 return GR_GL_TRIANGLES;
2287 case GrPrimitiveType::kTriangleStrip:
2288 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002289 case GrPrimitiveType::kPoints:
2290 return GR_GL_POINTS;
2291 case GrPrimitiveType::kLines:
2292 return GR_GL_LINES;
2293 case GrPrimitiveType::kLineStrip:
2294 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002295 case GrPrimitiveType::kPatches:
2296 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002297 case GrPrimitiveType::kPath:
2298 SK_ABORT("non-mesh-based GrPrimitiveType");
2299 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002300 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002301 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002302}
2303
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002304void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002305 // Some extensions automatically resolves the texture when it is read.
2306 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2307
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002308 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002309 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2310 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2311 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2312 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002313
Chris Daltonc139d082019-09-26 14:04:24 -06002314 // make sure we go through flushRenderTarget() since we've modified
2315 // the bound DRAW FBO ID.
2316 fHWBoundRenderTargetUniqueID.makeInvalid();
2317 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2318 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002319 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2320 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002321 GrScissorState scissor(rt->dimensions());
2322 SkAssertResult(scissor.set(resolveRect));
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002323 this->flushScissor(scissor, rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002324 this->disableWindowRectangles();
2325 GL_CALL(ResolveMultisampleFramebuffer());
2326 } else {
2327 int l, b, r, t;
2328 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2329 this->glCaps().blitFramebufferSupportFlags()) {
2330 l = 0;
2331 b = 0;
2332 r = target->width();
2333 t = target->height();
2334 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002335 l = resolveRect.x();
2336 b = resolveRect.y();
2337 r = resolveRect.x() + resolveRect.width();
2338 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002339 }
Chris Daltonc139d082019-09-26 14:04:24 -06002340
2341 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002342 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002343 this->disableWindowRectangles();
2344 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 +00002345 }
2346}
2347
bsalomon@google.com411dad02012-06-05 20:24:20 +00002348namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002349
bsalomon@google.com411dad02012-06-05 20:24:20 +00002350
2351GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002352 static const GrGLenum gTable[kGrStencilOpCount] = {
2353 GR_GL_KEEP, // kKeep
2354 GR_GL_ZERO, // kZero
2355 GR_GL_REPLACE, // kReplace
2356 GR_GL_INVERT, // kInvert
2357 GR_GL_INCR_WRAP, // kIncWrap
2358 GR_GL_DECR_WRAP, // kDecWrap
2359 GR_GL_INCR, // kIncClamp
2360 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002361 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002362 static_assert(0 == (int)GrStencilOp::kKeep);
2363 static_assert(1 == (int)GrStencilOp::kZero);
2364 static_assert(2 == (int)GrStencilOp::kReplace);
2365 static_assert(3 == (int)GrStencilOp::kInvert);
2366 static_assert(4 == (int)GrStencilOp::kIncWrap);
2367 static_assert(5 == (int)GrStencilOp::kDecWrap);
2368 static_assert(6 == (int)GrStencilOp::kIncClamp);
2369 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002370 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2371 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002372}
2373
2374void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002375 const GrStencilSettings::Face& face,
2376 GrGLenum glFace) {
2377 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2378 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2379 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002380
cdalton93a379b2016-05-11 13:58:08 -07002381 GrGLint ref = face.fRef;
2382 GrGLint mask = face.fTestMask;
2383 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002384
2385 if (GR_GL_FRONT_AND_BACK == glFace) {
2386 // we call the combined func just in case separate stencil is not
2387 // supported.
2388 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2389 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002390 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002391 } else {
2392 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2393 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002394 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002395 }
2396}
John Stilesa6841be2020-08-06 14:11:56 -04002397} // namespace
bsalomon@google.comd302f142011-03-03 13:54:13 +00002398
Chris Dalton71713f62019-04-18 12:41:03 -06002399void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002400 if (stencilSettings.isDisabled()) {
2401 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002402 } else if (fHWStencilSettings != stencilSettings ||
2403 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002404 if (kYes_TriState != fHWStencilTestEnabled) {
2405 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002406
csmartdaltonc7d85332016-10-26 10:13:46 -07002407 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002408 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002409 if (!stencilSettings.isTwoSided()) {
2410 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2411 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002412 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002413 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2414 GR_GL_FRONT);
2415 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2416 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002417 }
joshualitta58fe352014-10-27 08:39:00 -07002418 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002419 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002420 }
2421}
2422
csmartdaltonc7d85332016-10-26 10:13:46 -07002423void GrGLGpu::disableStencil() {
2424 if (kNo_TriState != fHWStencilTestEnabled) {
2425 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002426
csmartdaltonc7d85332016-10-26 10:13:46 -07002427 fHWStencilTestEnabled = kNo_TriState;
2428 fHWStencilSettings.invalidate();
2429 }
2430}
2431
Chris Dalton4c56b032019-03-04 12:28:42 -07002432void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002433 // rt is only optional if useHWAA is false.
2434 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002435#ifdef SK_DEBUG
2436 if (useHWAA && rt->numSamples() <= 1) {
2437 SkASSERT(this->caps()->mixedSamplesSupport());
2438 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002439 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002440 }
2441#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002442
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002443 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002444 if (useHWAA) {
2445 if (kYes_TriState != fMSAAEnabled) {
2446 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2447 fMSAAEnabled = kYes_TriState;
2448 }
2449 } else {
2450 if (kNo_TriState != fMSAAEnabled) {
2451 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2452 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002453 }
2454 }
2455 }
2456}
2457
Chris Daltonce425af2019-12-16 10:39:03 -07002458void GrGLGpu::flushConservativeRasterState(bool enabled) {
2459 if (this->caps()->conservativeRasterSupport()) {
2460 if (enabled) {
2461 if (kYes_TriState != fHWConservativeRasterEnabled) {
2462 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2463 fHWConservativeRasterEnabled = kYes_TriState;
2464 }
2465 } else {
2466 if (kNo_TriState != fHWConservativeRasterEnabled) {
2467 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2468 fHWConservativeRasterEnabled = kNo_TriState;
2469 }
2470 }
2471 }
2472}
2473
Chris Dalton1215cda2019-12-17 21:44:04 -07002474void GrGLGpu::flushWireframeState(bool enabled) {
2475 if (this->caps()->wireframeSupport()) {
2476 if (this->caps()->wireframeMode() || enabled) {
2477 if (kYes_TriState != fHWWireframeEnabled) {
2478 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2479 fHWWireframeEnabled = kYes_TriState;
2480 }
2481 } else {
2482 if (kNo_TriState != fHWWireframeEnabled) {
2483 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2484 fHWWireframeEnabled = kNo_TriState;
2485 }
2486 }
2487 }
2488}
2489
Chris Daltonbbb3f642019-07-24 12:25:08 -04002490void GrGLGpu::flushBlendAndColorWrite(
2491 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2492 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2493 // We need to work around a driver bug by using a blend state that preserves the dst color,
2494 // rather than disabling color writes.
2495 GrXferProcessor::BlendInfo preserveDstBlend;
2496 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2497 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2498 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2499 return;
2500 }
bsalomonf7cc8772015-05-11 11:21:14 -07002501
cdalton8917d622015-05-06 13:40:21 -07002502 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002503 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2504 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002505
2506 // Any optimization to disable blending should have already been applied and
2507 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002508 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2509 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002510
egdanielb414f252014-07-29 13:15:47 -07002511 if (blendOff) {
2512 if (kNo_TriState != fHWBlendState.fEnabled) {
2513 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002514
Jim Van Verth1bef9792020-07-09 08:09:13 -04002515 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002516 // https://code.google.com/p/skia/issues/detail?id=3943
2517 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2518 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2519 SkASSERT(this->caps()->advancedBlendEquationSupport());
2520 // Set to any basic blending equation.
2521 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2522 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2523 fHWBlendState.fEquation = blend_equation;
2524 }
2525
egdanielb414f252014-07-29 13:15:47 -07002526 fHWBlendState.fEnabled = kNo_TriState;
2527 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002528 } else {
2529 if (kYes_TriState != fHWBlendState.fEnabled) {
2530 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002531
Chris Daltonbbb3f642019-07-24 12:25:08 -04002532 fHWBlendState.fEnabled = kYes_TriState;
2533 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002534
Chris Daltonbbb3f642019-07-24 12:25:08 -04002535 if (fHWBlendState.fEquation != equation) {
2536 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2537 fHWBlendState.fEquation = equation;
2538 }
cdalton8917d622015-05-06 13:40:21 -07002539
Chris Daltonbbb3f642019-07-24 12:25:08 -04002540 if (GrBlendEquationIsAdvanced(equation)) {
2541 SkASSERT(this->caps()->advancedBlendEquationSupport());
2542 // Advanced equations have no other blend state.
2543 return;
2544 }
cdalton8917d622015-05-06 13:40:21 -07002545
Chris Daltonbbb3f642019-07-24 12:25:08 -04002546 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2547 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2548 gXfermodeCoeff2Blend[dstCoeff]));
2549 fHWBlendState.fSrcCoeff = srcCoeff;
2550 fHWBlendState.fDstCoeff = dstCoeff;
2551 }
cdalton8917d622015-05-06 13:40:21 -07002552
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002553 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002554 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2555 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2556 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2557 fHWBlendState.fConstColor = blendConst;
2558 fHWBlendState.fConstColorValid = true;
2559 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002560 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002561 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002562
2563 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002564}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002565
Greg Daniel2c3398d2019-06-19 11:58:01 -04002566void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2567 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002568 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002569
reed856e9d92015-09-30 12:21:45 -07002570#ifdef SK_DEBUG
2571 if (!this->caps()->npotTextureTileSupport()) {
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002572 if (samplerState.isRepeatedX()) {
reed856e9d92015-09-30 12:21:45 -07002573 const int w = texture->width();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002574 SkASSERT(SkIsPow2(w));
2575 }
2576 if (samplerState.isRepeatedY()) {
reed856e9d92015-09-30 12:21:45 -07002577 const int h = texture->height();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002578 SkASSERT(SkIsPow2(h));
reed856e9d92015-09-30 12:21:45 -07002579 }
2580 }
2581#endif
2582
Robert Phillips294870f2016-11-11 12:38:40 -05002583 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002584 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002585 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002586 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002587 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002588 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002589 }
2590
Brian Salomone69b9ef2020-07-22 11:18:06 -04002591 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002592 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002593 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2594 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002595 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002596 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002597 }
bsalomonefd7d452014-10-23 14:17:46 -07002598
Brian Salomone2826ab2019-06-04 15:58:31 -04002599 auto timestamp = texture->parameters()->resetTimestamp();
2600 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002601 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2602 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomona0d913b2020-09-01 12:42:26 -04002603 if (this->glCaps().useSamplerObjects()) {
Brian Salomondc829942018-10-23 16:07:24 -04002604 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002605 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002606 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2607 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2608 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002609 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2610 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002611 this->setTextureUnit(unitIdx);
2612 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2613 newSamplerState = oldSamplerState;
2614 newSamplerState.fMinFilter = minFilter;
2615 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002616 }
2617 }
Brian Salomondc829942018-10-23 16:07:24 -04002618 } else {
Brian Salomona0d913b2020-09-01 12:42:26 -04002619 if (fSamplerObjectCache) {
2620 fSamplerObjectCache->unbindSampler(unitIdx);
2621 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002622 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2623 texture->parameters()->samplerOverriddenState();
2624 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002625
Brian Salomone69b9ef2020-07-22 11:18:06 -04002626 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2627 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002629
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2631 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002632
2633 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002634 newSamplerState.fMinLOD = -1000.f;
2635 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002636
Brian Salomone2826ab2019-06-04 15:58:31 -04002637 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002638 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002639 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002640 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002642 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002644 }
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002645 if (this->glCaps().mipmapLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002646 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002647 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002649 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002651 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002653 }
2654 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002655 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002656 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002657 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002658 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002659 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002660 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002662 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002663 if (this->glCaps().clampToBorderSupport()) {
2664 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002665 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002666 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002667 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2668 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002669 }
2670 }
Brian Salomondc829942018-10-23 16:07:24 -04002671 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002672 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2673 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002674 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002675 newNonsamplerState.fSwizzleIsRGBA = true;
Brian Salomondc829942018-10-23 16:07:24 -04002676
Brian Salomone2826ab2019-06-04 15:58:31 -04002677 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2678 texture->parameters()->nonsamplerState();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002679 if (this->glCaps().textureSwizzleSupport()) {
2680 if (setAll || !oldNonsamplerState.fSwizzleIsRGBA) {
2681 static constexpr GrGLenum kRGBA[4] {
2682 GR_GL_RED,
2683 GR_GL_GREEN,
2684 GR_GL_BLUE,
2685 GR_GL_ALPHA
2686 };
Brian Salomondc829942018-10-23 16:07:24 -04002687 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002688 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon280fa3d2020-08-27 17:16:49 -04002689 static_assert(sizeof(kRGBA[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002690 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
Brian Salomon280fa3d2020-08-27 17:16:49 -04002691 reinterpret_cast<const GrGLint*>(kRGBA)));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002692 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002693 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
Brian Salomon280fa3d2020-08-27 17:16:49 -04002694 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, kRGBA[0]));
2695 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, kRGBA[1]));
2696 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, kRGBA[2]));
2697 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, kRGBA[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002698 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002699 }
2700 }
Brian Salomondc829942018-10-23 16:07:24 -04002701 // These are not supported in ES2 contexts
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002702 if (this->glCaps().mipmapLevelControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002703 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002704 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002705 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002706 this->setTextureUnit(unitIdx);
2707 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002708 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002709 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002710 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002711 this->setTextureUnit(unitIdx);
2712 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002713 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002714 }
Brian Salomon327955b2018-10-22 15:39:22 +00002715 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002716 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2717 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002718}
2719
Brian Salomon1f05d452019-02-08 12:33:08 -05002720void GrGLGpu::onResetTextureBindings() {
2721 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2722 GR_GL_TEXTURE_EXTERNAL};
2723 for (int i = 0; i < this->numTextureUnits(); ++i) {
2724 this->setTextureUnit(i);
2725 for (auto target : kTargets) {
2726 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2727 GL_CALL(BindTexture(target, 0));
2728 }
2729 }
2730 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2731 }
2732}
2733
Chris Dalton5a2f9622019-12-27 14:56:38 -07002734void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2735 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2736 if (fHWPatchVertexCount != count) {
2737 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2738 fHWPatchVertexCount = count;
2739 }
2740}
2741
egdaniel080e6732014-12-22 07:35:52 -08002742void GrGLGpu::flushColorWrite(bool writeColor) {
2743 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002744 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002745 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2746 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002747 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002748 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002749 } else {
2750 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002751 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002752 fHWWriteToColor = kYes_TriState;
2753 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002754 }
bsalomon3e791242014-12-17 13:43:13 -08002755}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002756
Brian Salomon07bc9a22020-12-02 13:37:16 -05002757void GrGLGpu::flushClearColor(std::array<float, 4> color) {
2758 GrGLfloat r = color[0], g = color[1], b = color[2], a = color[3];
Chris Dalton674f77a2019-09-30 20:49:39 -06002759 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2760 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2761 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2762 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2763 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2764 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002765 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2766 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2767 GL_CALL(ClearColor(r, g, b, a));
2768 fHWClearColor[0] = r;
2769 fHWClearColor[1] = g;
2770 fHWClearColor[2] = b;
2771 fHWClearColor[3] = a;
2772 }
2773}
2774
bsalomon861e1032014-12-16 07:33:49 -08002775void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002776 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002777 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002778 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002779 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002780 }
2781}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002782
Brian Salomond978b902019-02-07 15:09:18 -05002783void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002784 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002785 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002786 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2787 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2788 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002789 }
Brian Salomond978b902019-02-07 15:09:18 -05002790 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2791 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002792 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002793 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002794}
2795
Brian Salomone5e7eb12016-10-14 16:18:33 -04002796// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002797static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2798 const GrSurface* src,
2799 const SkIRect& srcRect,
2800 const SkIPoint& dstPoint,
2801 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002802 int dstSampleCnt = 0;
2803 int srcSampleCnt = 0;
2804 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002805 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002806 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002807 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002808 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002809 }
2810 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2811 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2812
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002813 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2814 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2815
Brian Salomone5e7eb12016-10-14 16:18:33 -04002816 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002817 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002818
Greg Daniel46cfbc62019-06-07 11:43:30 -04002819 GrTextureType dstTexType;
2820 GrTextureType* dstTexTypePtr = nullptr;
2821 GrTextureType srcTexType;
2822 GrTextureType* srcTexTypePtr = nullptr;
2823 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002824 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002825 dstTexTypePtr = &dstTexType;
2826 }
2827 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002828 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002829 srcTexTypePtr = &srcTexType;
2830 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002831
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002832 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2833 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002834 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002835}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002836
Brian Osmana9c8a052018-01-19 10:31:56 -05002837static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2838 // A RT has a separate MSAA renderbuffer if:
2839 // 1) It's multisampled
2840 // 2) We're using an extension with separate MSAA renderbuffers
2841 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002842 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002843}
2844
Greg Daniel46cfbc62019-06-07 11:43:30 -04002845static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002846 const GrGLCaps& caps) {
2847
bsalomon@google.comeb851172013-04-15 13:51:00 +00002848 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002849 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002850 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002851 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002852
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002853 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2854 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2855
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002856 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2857 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2858
Greg Daniel46cfbc62019-06-07 11:43:30 -04002859 GrTextureType dstTexType;
2860 GrTextureType* dstTexTypePtr = nullptr;
2861 GrTextureType srcTexType;
2862 GrTextureType* srcTexTypePtr = nullptr;
2863 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002864 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002865 dstTexTypePtr = &dstTexType;
2866 }
2867 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002868 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002869 srcTexTypePtr = &srcTexType;
2870 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002871
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002872 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2873 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002874}
2875
Greg Danielacd66b42019-05-22 16:29:12 -04002876// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002877void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002878 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002879 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002880 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002881 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002882 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2883 GrGLuint texID = texture->textureID();
2884 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002885 GrGLuint* tempFBOID;
2886 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002887
egdanield803f272015-03-18 13:01:52 -07002888 if (0 == *tempFBOID) {
2889 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002890 }
2891
Adrienne Walker4ee88512018-05-17 11:37:14 -07002892 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002893 GR_GL_CALL(
2894 this->glInterface(),
2895 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2896 if (mipLevel == 0) {
2897 texture->baseLevelWasBoundToFBO();
2898 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002899 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002900 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002901 }
egdaniel0f5f9672015-02-03 11:10:51 -08002902}
2903
Brian Salomond2a8ae22019-09-10 16:03:59 -04002904void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002905 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002906 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002907 SkASSERT(surface->asTexture());
2908 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2909 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2910 GR_GL_COLOR_ATTACHMENT0,
2911 textureTarget,
2912 0,
2913 0));
2914 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002915}
2916
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002917void GrGLGpu::onFBOChanged() {
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002918 if (this->caps()->workarounds().flush_on_framebuffer_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002919 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002920 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002921#ifdef SK_DEBUG
2922 if (fIsExecutingCommandBuffer_DebugOnly) {
2923 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2924 "This will severely hurt performance.\n");
2925 }
2926#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002927}
2928
Adrienne Walker4ee88512018-05-17 11:37:14 -07002929void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2930 fStats.incRenderTargetBinds();
2931 GL_CALL(BindFramebuffer(target, fboid));
2932 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2933 fBoundDrawFramebuffer = fboid;
2934 }
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002935 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002936}
2937
Adrienne Walker4ee88512018-05-17 11:37:14 -07002938void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002939 // We're relying on the GL state shadowing being correct in the workaround code below so we
2940 // need to handle a dirty context.
2941 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002942 if (fboid == fBoundDrawFramebuffer &&
2943 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2944 // This workaround only applies to deleting currently bound framebuffers
2945 // on Adreno 420. Because this is a somewhat rare case, instead of
2946 // tracking all the attachments of every framebuffer instead just always
2947 // unbind all attachments.
2948 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2949 GR_GL_RENDERBUFFER, 0));
2950 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2951 GR_GL_RENDERBUFFER, 0));
2952 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2953 GR_GL_RENDERBUFFER, 0));
2954 }
2955
2956 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002957
2958 // Deleting the currently bound framebuffer rebinds to 0.
2959 if (fboid == fBoundDrawFramebuffer) {
2960 this->onFBOChanged();
2961 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002962}
2963
Greg Daniel46cfbc62019-06-07 11:43:30 -04002964bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002965 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002966 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2967 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2968 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002969 auto dstFormat = dst->backendFormat().asGLFormat();
2970 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002971 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002972 return true;
2973 }
2974 }
cblume61214052016-01-26 09:10:48 -08002975
Greg Daniel46cfbc62019-06-07 11:43:30 -04002976 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2977 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002978 return true;
2979 }
2980
Greg Daniel46cfbc62019-06-07 11:43:30 -04002981 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2982 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002983 }
2984
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002985 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002986 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002987 return true;
2988 }
bsalomon083617b2016-02-12 12:10:14 -08002989 }
2990
bsalomon6df86402015-06-01 10:41:49 -07002991 return false;
2992}
2993
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002994bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002995 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002996
2997 int progIdx = TextureToCopyProgramIdx(srcTex);
2998 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002999 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003000
3001 if (!fCopyProgramArrayBuffer) {
3002 static const GrGLfloat vdata[] = {
3003 0, 0,
3004 0, 1,
3005 1, 0,
3006 1, 1
3007 };
3008 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3009 kStatic_GrAccessPattern, vdata);
3010 }
3011 if (!fCopyProgramArrayBuffer) {
3012 return false;
3013 }
3014
3015 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3016 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3017 if (!fCopyPrograms[progIdx].fProgram) {
3018 return false;
3019 }
3020
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003021 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003022 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003023 GrShaderVar::TypeModifier::Uniform);
3024 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3025 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3026 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3027 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003028
Greg Daniel9e3169b2019-06-06 14:38:17 -04003029 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003030 if (shaderCaps->noperspectiveInterpolationSupport()) {
3031 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3032 vshaderTxt.appendf("#extension %s : require\n", extension);
3033 }
3034 vTexCoord.addModifier("noperspective");
3035 }
3036
3037 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3038 vshaderTxt.append(";");
3039 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3040 vshaderTxt.append(";");
3041 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3042 vshaderTxt.append(";");
3043 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3044 vshaderTxt.append(";");
3045
3046 vshaderTxt.append(
3047 "// Copy Program VS\n"
3048 "void main() {"
3049 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3050 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3051 " sk_Position.zw = half2(0, 1);"
3052 "}"
3053 );
3054
Greg Daniel9e3169b2019-06-06 14:38:17 -04003055 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003056 if (shaderCaps->noperspectiveInterpolationSupport()) {
3057 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3058 fshaderTxt.appendf("#extension %s : require\n", extension);
3059 }
3060 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003061 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003062 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3063 fshaderTxt.append(";");
3064 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3065 fshaderTxt.append(";");
3066 fshaderTxt.appendf(
3067 "// Copy Program FS\n"
3068 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003069 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003070 "}"
3071 );
3072
3073 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3074 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3075 SkSL::Program::Settings settings;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003076 SkSL::String glsl;
John Stilesdbd4e6f2021-02-16 13:29:15 -05003077 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::ProgramKind::kVertex,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003078 sksl, settings, &glsl, errorHandler);
3079 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Robert Phillipsae67c522021-03-03 11:03:38 -05003080 GR_GL_VERTEX_SHADER, glsl, fProgramCache->stats(),
3081 errorHandler);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003082 SkASSERT(program->fInputs.isEmpty());
3083
3084 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
John Stilesdbd4e6f2021-02-16 13:29:15 -05003085 program = GrSkSLtoGLSL(this, SkSL::ProgramKind::kFragment, sksl, settings, &glsl,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003086 errorHandler);
3087 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
Robert Phillipsae67c522021-03-03 11:03:38 -05003088 GR_GL_FRAGMENT_SHADER, glsl,
3089 fProgramCache->stats(), errorHandler);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003090 SkASSERT(program->fInputs.isEmpty());
3091
3092 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3093
3094 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3095 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3096 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3097 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3098 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3099 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3100
3101 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3102
3103 GL_CALL(DeleteShader(vshader));
3104 GL_CALL(DeleteShader(fshader));
3105
3106 return true;
3107}
3108
brianosman33f6b3f2016-06-02 05:49:21 -07003109bool GrGLGpu::createMipmapProgram(int progIdx) {
3110 const bool oddWidth = SkToBool(progIdx & 0x2);
3111 const bool oddHeight = SkToBool(progIdx & 0x1);
3112 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3113
Brian Salomon1edc5b92016-11-29 13:43:46 -05003114 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003115
3116 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3117 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3118 if (!fMipmapPrograms[progIdx].fProgram) {
3119 return false;
3120 }
3121
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003122 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003123 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003124 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003125 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003126 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003127 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003128 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003129 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3130 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3131 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3132 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003133 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003134 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003135
Ethan Nicholasfc994162019-06-06 10:04:27 -04003136 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003137 if (shaderCaps->noperspectiveInterpolationSupport()) {
3138 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003139 vshaderTxt.appendf("#extension %s : require\n", extension);
3140 }
3141 vTexCoords[0].addModifier("noperspective");
3142 vTexCoords[1].addModifier("noperspective");
3143 vTexCoords[2].addModifier("noperspective");
3144 vTexCoords[3].addModifier("noperspective");
3145 }
3146
Brian Salomon1edc5b92016-11-29 13:43:46 -05003147 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003148 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003149 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003150 vshaderTxt.append(";");
3151 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003152 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003153 vshaderTxt.append(";");
3154 }
3155
3156 vshaderTxt.append(
3157 "// Mipmap Program VS\n"
3158 "void main() {"
John Stiles15b48922020-11-25 10:34:59 -05003159 " sk_Position.xy = a_vertex * half2(2) - half2(1);"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003160 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003161 );
3162
3163 // Insert texture coordinate computation:
3164 if (oddWidth && oddHeight) {
3165 vshaderTxt.append(
3166 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003167 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3168 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003169 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3170 );
3171 } else if (oddWidth) {
3172 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003173 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3174 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003175 );
3176 } else if (oddHeight) {
3177 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003178 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3179 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003180 );
3181 } else {
3182 vshaderTxt.append(
3183 " v_texCoord0 = a_vertex.xy;"
3184 );
3185 }
3186
3187 vshaderTxt.append("}");
3188
Ethan Nicholasfc994162019-06-06 10:04:27 -04003189 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003190 if (shaderCaps->noperspectiveInterpolationSupport()) {
3191 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003192 fshaderTxt.appendf("#extension %s : require\n", extension);
3193 }
3194 }
brianosman33f6b3f2016-06-02 05:49:21 -07003195 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003196 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003197 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003198 fshaderTxt.append(";");
3199 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003200 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003201 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003202 fshaderTxt.append(
3203 "// Mipmap Program FS\n"
3204 "void main() {"
3205 );
3206
3207 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003208 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003209 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3210 " sample(u_texture, v_texCoord1) + "
3211 " sample(u_texture, v_texCoord2) + "
3212 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003213 );
3214 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003215 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003216 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3217 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003218 );
3219 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003220 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003221 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003222 );
3223 }
3224
3225 fshaderTxt.append("}");
3226
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003227 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003228 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003229 SkSL::Program::Settings settings;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003230 SkSL::String glsl;
John Stilesdbd4e6f2021-02-16 13:29:15 -05003231 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::ProgramKind::kVertex,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003232 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003233 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Robert Phillipsae67c522021-03-03 11:03:38 -05003234 GR_GL_VERTEX_SHADER, glsl,
3235 fProgramCache->stats(), errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003236 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003237
Brian Osman6c431d52019-04-15 16:31:54 -04003238 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
John Stilesdbd4e6f2021-02-16 13:29:15 -05003239 program = GrSkSLtoGLSL(this, SkSL::ProgramKind::kFragment, sksl, settings, &glsl,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003240 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003241 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Robert Phillipsae67c522021-03-03 11:03:38 -05003242 GR_GL_FRAGMENT_SHADER, glsl,
3243 fProgramCache->stats(), errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003244 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003245
3246 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3247
3248 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3249 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3250 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3251 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3252
3253 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3254
3255 GL_CALL(DeleteShader(vshader));
3256 GL_CALL(DeleteShader(fshader));
3257
3258 return true;
3259}
3260
Greg Daniel46cfbc62019-06-07 11:43:30 -04003261bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003262 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003263 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3264 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3265 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3266 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 return false;
3268 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003269 int progIdx = TextureToCopyProgramIdx(srcTex);
3270 if (!dstRT) {
3271 SkASSERT(dstTex);
3272 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3273 return false;
3274 }
3275 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 if (!fCopyPrograms[progIdx].fProgram) {
3277 if (!this->createCopyProgram(srcTex)) {
3278 SkDebugf("Failed to create copy program.\n");
3279 return false;
3280 }
3281 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003282 int w = srcRect.width();
3283 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003284 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003285 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003286 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05003287 this->flushViewport(SkIRect::MakeSize(dst->dimensions()),
3288 dst->height(),
3289 kTopLeft_GrSurfaceOrigin); // the origin is irrelevant in this case
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003290 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003291 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003292 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003293 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3295 attribs->enableVertexArrays(this, 1);
3296 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3297 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003298 // dst rect edges in NDC (-1 to 1)
3299 int dw = dst->width();
3300 int dh = dst->height();
3301 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3302 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3303 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3304 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003305 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3306 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3307 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3308 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3309 int sw = src->width();
3310 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003311 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003312 // src rect edges in normalized texture space (0 to 1)
3313 sx0 /= sw;
3314 sx1 /= sw;
3315 sy0 /= sh;
3316 sy1 /= sh;
3317 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003318 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3319 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3320 sx1 - sx0, sy1 - sy0, sx0, sy0));
3321 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003322 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003323 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003324 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003325 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003326 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003327 this->disableWindowRectangles();
3328 this->disableStencil();
3329 if (this->glCaps().srgbWriteControl()) {
3330 this->flushFramebufferSRGB(true);
3331 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003332 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003333 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003334 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3335 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003336 return true;
3337}
3338
Greg Daniel46cfbc62019-06-07 11:43:30 -04003339void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003340 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003341 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003342 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003343 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003344 SkASSERT(dstTex);
3345 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003346 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003347
Brian Salomond978b902019-02-07 15:09:18 -05003348 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003349 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003350 dstPoint.fX, dstPoint.fY,
3351 srcRect.fLeft, srcRect.fTop,
3352 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003353 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003354 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3355 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003356 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3357 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003358}
3359
Greg Daniel46cfbc62019-06-07 11:43:30 -04003360bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003361 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003362 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003363 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3364 srcRect.width(), srcRect.height());
3365 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003366 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003367 return false;
mtklein404b3b22015-05-18 09:29:10 -07003368 }
bsalomon5df6fee2015-05-18 06:26:15 -07003369 }
bsalomon6df86402015-06-01 10:41:49 -07003370
Brian Salomond2a8ae22019-09-10 16:03:59 -04003371 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3372 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003373 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003374 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003375
3376 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003377 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003378 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003379
Greg Daniel46cfbc62019-06-07 11:43:30 -04003380 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3381 srcRect.fTop,
3382 srcRect.fRight,
3383 srcRect.fBottom,
3384 dstRect.fLeft,
3385 dstRect.fTop,
3386 dstRect.fRight,
3387 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003388 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003389 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3390 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003391
3392 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3393 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003394 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003395}
3396
Brian Salomon930f9392018-06-20 16:25:26 -04003397bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3398 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003399 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003400 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003401 return false;
3402 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003403 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003404 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3405 // Uses draw calls to do a series of downsample operations to successive mips.
3406
3407 // The manual approach requires the ability to limit which level we're sampling and that the
3408 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003409 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003410 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003411 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003412 GL_CALL(GenerateMipmap(glTex->target()));
3413 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003414 }
3415
brianosman33f6b3f2016-06-02 05:49:21 -07003416 int width = texture->width();
3417 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003418 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003419 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003420
3421 // Create (if necessary), then bind temporary FBO:
3422 if (0 == fTempDstFBOID) {
3423 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3424 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003425 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003426 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003427
3428 // Bind the texture, to get things configured for filtering.
3429 // We'll be changing our base level further below:
3430 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003431 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003432 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003433
3434 // Vertex data:
3435 if (!fMipmapProgramArrayBuffer) {
3436 static const GrGLfloat vdata[] = {
3437 0, 0,
3438 0, 1,
3439 1, 0,
3440 1, 1
3441 };
Brian Salomonae64c192019-02-05 09:41:37 -05003442 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003443 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003444 }
3445 if (!fMipmapProgramArrayBuffer) {
3446 return false;
3447 }
3448
3449 fHWVertexArrayState.setVertexArrayID(this, 0);
3450
3451 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003452 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003453 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003454 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003455
3456 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003457 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003458 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003459 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003460 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003461 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003462
3463 // Do all the blits:
3464 width = texture->width();
3465 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003466
brianosman33f6b3f2016-06-02 05:49:21 -07003467 for (GrGLint level = 1; level < levelCount; ++level) {
3468 // Get and bind the program for this particular downsample (filter shape can vary):
3469 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3470 if (!fMipmapPrograms[progIdx].fProgram) {
3471 if (!this->createMipmapProgram(progIdx)) {
3472 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003473 // Invalidate all params to cover base level change in a previous iteration.
3474 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003475 return false;
3476 }
3477 }
Brian Salomon802cb312018-06-08 18:05:20 -04003478 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003479
3480 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3481 const float invWidth = 1.0f / width;
3482 const float invHeight = 1.0f / height;
3483 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3484 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3485 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3486
3487 // Only sample from previous mip
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003488 SkASSERT(this->glCaps().mipmapLevelControlSupport());
brianosman33f6b3f2016-06-02 05:49:21 -07003489 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3490
Brian Salomon930f9392018-06-20 16:25:26 -04003491 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3492 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003493
Brian Osman788b9162020-02-07 10:36:46 -05003494 width = std::max(1, width / 2);
3495 height = std::max(1, height / 2);
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05003496 this->flushViewport(SkIRect::MakeWH(width, height), height, kTopLeft_GrSurfaceOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07003497
3498 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3499 }
3500
3501 // Unbind:
3502 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3503 GR_GL_TEXTURE_2D, 0, 0));
3504
Brian Salomon930f9392018-06-20 16:25:26 -04003505 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003506 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3507 // We drew the 2nd to last level into the last level.
3508 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3509 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003510
brianosman33f6b3f2016-06-02 05:49:21 -07003511 return true;
3512}
3513
Chris Daltond7291ba2019-03-07 14:17:03 -07003514void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003515 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3516 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003517
3518 int effectiveSampleCnt;
3519 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003520 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003521
3522 sampleLocations->reset(effectiveSampleCnt);
3523 for (int i = 0; i < effectiveSampleCnt; ++i) {
3524 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3525 }
3526}
3527
cdalton231c5fd2015-05-13 12:35:36 -07003528void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003529 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003530 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003531 case kTexture_GrXferBarrierType: {
3532 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003533 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003534 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3535 // The render target uses separate storage so no need for glTextureBarrier.
3536 // FIXME: The render target will resolve automatically when its texture is bound,
3537 // but we could resolve only the bounds that will be read if we do it here instead.
3538 return;
3539 }
cdalton9954bc32015-04-29 14:17:00 -07003540 SkASSERT(this->caps()->textureBarrierSupport());
3541 GL_CALL(TextureBarrier());
3542 return;
cdalton231c5fd2015-05-13 12:35:36 -07003543 }
cdalton8917d622015-05-06 13:40:21 -07003544 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003545 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003546 this->caps()->blendEquationSupport());
3547 GL_CALL(BlendBarrier());
3548 return;
bsalomoncb02b382015-08-12 11:14:50 -07003549 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003550 }
3551}
3552
Chris Daltone1196c52019-12-28 14:31:09 -07003553void GrGLGpu::insertManualFramebufferBarrier() {
3554 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3555 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3556}
3557
Brian Salomon85c3d682019-11-04 15:04:54 -05003558GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003559 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003560 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003561 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003562 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003563 // We don't support protected textures in GL.
3564 if (isProtected == GrProtected::kYes) {
3565 return {};
3566 }
3567
Brian Salomon8a375832018-03-14 10:21:40 -04003568 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003569
Brian Salomond4764a12019-08-08 12:08:24 -04003570 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003571 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003572 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003573 }
3574
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003575 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003576 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003577 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003578 }
3579
Robert Phillipsd34691b2019-09-24 13:38:43 -04003580 // Compressed formats go through onCreateCompressedBackendTexture
3581 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3582
Greg Daniel15500642019-06-27 03:05:55 +00003583 GrGLTextureInfo info;
3584 GrGLTextureParameters::SamplerOverriddenState initialState;
3585
Greg Danield51fa2f2020-01-22 16:53:38 -05003586 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003587 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003588 }
Brian Salomon0f396992020-06-19 19:51:21 -04003589 switch (format.textureType()) {
3590 case GrTextureType::kNone:
3591 case GrTextureType::kExternal:
3592 return {};
3593 case GrTextureType::k2D:
3594 info.fTarget = GR_GL_TEXTURE_2D;
3595 break;
3596 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003597 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003598 return {};
3599 }
3600 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3601 break;
3602 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003603 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003604 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3605 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003606 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003607 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003608 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003609
Brian Salomon85c3d682019-11-04 15:04:54 -05003610 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003611 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003612
Brian Salomone2826ab2019-06-04 15:58:31 -04003613 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003614 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003615 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3616 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003617
Brian Salomon85c3d682019-11-04 15:04:54 -05003618 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3619 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003620}
3621
Greg Daniel16032b32020-05-06 15:31:10 -04003622bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3623 sk_sp<GrRefCntedCallback> finishedCallback,
3624 const BackendTextureData* data) {
Robert Phillips489e5b92021-01-26 09:42:03 -05003625 this->handleDirtyContext();
3626
Greg Daniel16032b32020-05-06 15:31:10 -04003627 GrGLTextureInfo info;
3628 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3629
3630 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003631 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003632 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003633 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003634 }
3635
3636 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3637
Brian Salomon22558932020-05-15 14:46:34 -04003638 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003639
Brian Salomonc3f49432020-06-03 10:21:50 -04003640 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
Greg Danielb2365d82020-05-13 15:32:04 -04003641 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003642 if (numMipLevels && this->glCaps().mipmapLevelControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003643 auto params = backendTexture.getGLTextureParams();
3644 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3645 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003646 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003647 nonsamplerState.fBaseMipMapLevel = 0;
3648 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003649 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003650 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003651 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3652 }
3653 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3654 }
3655
Greg Daniel16032b32020-05-06 15:31:10 -04003656 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003657 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003658 if (data->type() == BackendTextureData::Type::kPixmaps) {
3659 SkTDArray<GrMipLevel> texels;
Brian Salomon05487ab2020-12-23 20:32:22 -05003660 GrColorType colorType = data->pixmap(0).colorType();
Greg Daniel16032b32020-05-06 15:31:10 -04003661 texels.append(numMipLevels);
3662 for (int i = 0; i < numMipLevels; ++i) {
3663 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3664 }
Brian Salomon22558932020-05-15 14:46:34 -04003665 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3666 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3667 info.fTarget, dstRect, colorType, texels.begin(),
3668 texels.count());
3669 } else if (data->type() == BackendTextureData::Type::kColor) {
3670 uint32_t levelMask = (1 << numMipLevels) - 1;
3671 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3672 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003673 }
3674
3675 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003676 this->bindTextureToScratchUnit(info.fTarget, 0);
3677 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003678}
3679
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003680void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003681 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3682
3683 GrGLTextureInfo info;
3684 if (tex.getGLTextureInfo(&info)) {
3685 GL_CALL(DeleteTextures(1, &info.fID));
3686 }
3687}
3688
Robert Phillips3bce1f72020-05-12 12:41:58 -04003689bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3690 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3691
Robert Phillipsae67c522021-03-03 11:03:38 -05003692 GrThreadSafePipelineBuilder::Stats::ProgramCacheResult stat;
Robert Phillips3bce1f72020-05-12 12:41:58 -04003693
3694 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3695 if (!tmp) {
3696 return false;
3697 }
3698
Robert Phillipsae67c522021-03-03 11:03:38 -05003699 return stat != GrThreadSafePipelineBuilder::Stats::ProgramCacheResult::kHit;
Robert Phillips3bce1f72020-05-12 12:41:58 -04003700}
3701
Robert Phillipsf0ced622019-05-16 09:06:25 -04003702#if GR_TEST_UTILS
3703
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003704bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003705 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003706
Greg Daniel52e16d92018-04-10 09:34:07 -04003707 GrGLTextureInfo info;
3708 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003709 return false;
3710 }
3711
3712 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003713 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003714
3715 return (GR_GL_TRUE == result);
3716}
3717
Brian Salomon72c7b982020-10-06 10:07:38 -04003718GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
3719 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04003720 int sampleCnt,
3721 GrProtected isProtected) {
Brian Salomon72c7b982020-10-06 10:07:38 -04003722 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
3723 dimensions.height() > this->caps()->maxRenderTargetSize()) {
3724 return {};
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003725 }
Brian Salomonf9b00422020-10-08 16:00:14 -04003726 if (isProtected == GrProtected::kYes) {
3727 return {};
3728 }
3729
Brian Salomon8a375832018-03-14 10:21:40 -04003730 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003731 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -04003732 sampleCnt = this->glCaps().getRenderTargetSampleCount(sampleCnt, format);
3733 if (!sampleCnt) {
Brian Salomonf865b052018-03-09 09:01:53 -05003734 return {};
3735 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003736 // We make a texture instead of a render target if we're using a
3737 // "multisampled_render_to_texture" style extension or have a BGRA format that
3738 // is allowed for textures but not render buffer internal formats.
3739 bool useTexture = false;
3740 if (sampleCnt > 1 && !this->glCaps().usesMSAARenderBuffers()) {
3741 useTexture = true;
3742 } else if (format == GrGLFormat::kBGRA8 &&
3743 this->glCaps().getRenderbufferInternalFormat(GrGLFormat::kBGRA8) != GR_GL_BGRA8) {
3744 // We have a BGRA extension that doesn't support BGRA render buffers. We can use a texture
3745 // unless we've been asked for MSAA. Note we already checked above for render-to-
3746 // multisampled-texture style extensions.
3747 if (sampleCnt > 1) {
3748 return {};
3749 }
3750 useTexture = true;
3751 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003752 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003753 if (sFormatIdx < 0) {
3754 return {};
3755 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003756 GrGLuint colorID = 0;
3757 GrGLuint stencilID = 0;
Brian Salomon72c7b982020-10-06 10:07:38 -04003758 GrGLFramebufferInfo info;
3759 info.fFBOID = 0;
3760 info.fFormat = GrGLFormatToEnum(format);
3761
3762 auto deleteIDs = [&](bool saveFBO = false) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003763 if (colorID) {
3764 if (useTexture) {
3765 GL_CALL(DeleteTextures(1, &colorID));
3766 } else {
3767 GL_CALL(DeleteRenderbuffers(1, &colorID));
3768 }
Brian Salomonf865b052018-03-09 09:01:53 -05003769 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003770 if (stencilID) {
3771 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003772 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003773 if (!saveFBO && info.fFBOID) {
3774 this->deleteFramebuffer(info.fFBOID);
3775 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003776 };
3777
3778 if (useTexture) {
3779 GL_CALL(GenTextures(1, &colorID));
3780 } else {
3781 GL_CALL(GenRenderbuffers(1, &colorID));
3782 }
3783 GL_CALL(GenRenderbuffers(1, &stencilID));
3784 if (!stencilID || !colorID) {
3785 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003786 return {};
3787 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003788
Brian Salomonf865b052018-03-09 09:01:53 -05003789 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3790 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003791 deleteIDs();
3792 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003793 }
3794
3795 this->invalidateBoundRenderTarget();
3796
Adrienne Walker4ee88512018-05-17 11:37:14 -07003797 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003798 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003799 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon72c7b982020-10-06 10:07:38 -04003800 colorID = this->createTexture(dimensions, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
Brian Salomon0f396992020-06-19 19:51:21 -04003801 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003802 if (!colorID) {
3803 deleteIDs();
3804 return {};
3805 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003806 if (sampleCnt == 1) {
3807 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3808 GR_GL_TEXTURE_2D, colorID, 0));
3809 } else {
3810 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3811 GR_GL_TEXTURE_2D, colorID, 0, sampleCnt));
3812 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003813 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003814 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003815 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomon72c7b982020-10-06 10:07:38 -04003816 if (sampleCnt == 1) {
3817 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, dimensions.width(),
3818 dimensions.height()));
3819 } else {
3820 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt, renderBufferFormat,
3821 dimensions.width(), dimensions.height())) {
3822 deleteIDs();
3823 return {};
3824 }
3825 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003826 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3827 GR_GL_RENDERBUFFER, colorID));
3828 }
3829 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003830 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx];
Brian Salomon72c7b982020-10-06 10:07:38 -04003831 if (sampleCnt == 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003832 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GrGLFormatToEnum(stencilBufferFormat),
3833 dimensions.width(), dimensions.height()));
Brian Salomon72c7b982020-10-06 10:07:38 -04003834 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003835 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt,
3836 GrGLFormatToEnum(stencilBufferFormat),
Brian Salomon72c7b982020-10-06 10:07:38 -04003837 dimensions.width(), dimensions.height())) {
3838 deleteIDs();
3839 return {};
3840 }
3841 }
Brian Salomonf865b052018-03-09 09:01:53 -05003842 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003843 stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003844 if (GrGLFormatIsPackedDepthStencil(this->glCaps().stencilFormats()[sFormatIdx])) {
Brian Salomonf865b052018-03-09 09:01:53 -05003845 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003846 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003847 }
3848
Brian Salomon93348dd2018-08-29 12:56:23 -04003849 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3850 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3851 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3852 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003853 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon72c7b982020-10-06 10:07:38 -04003854 deleteIDs(/* saveFBO = */ true);
Brian Salomonf865b052018-03-09 09:01:53 -05003855
Adrienne Walker4ee88512018-05-17 11:37:14 -07003856 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003857 GrGLenum status;
3858 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3859 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003860 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003861 return {};
3862 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003863
Greg Daniel8ade5e82020-10-07 13:09:48 -04003864 auto stencilBits = SkToInt(GrGLFormatStencilBits(this->glCaps().stencilFormats()[sFormatIdx]));
Robert Phillips62221e72019-07-24 15:07:38 -04003865
Brian Salomon72c7b982020-10-06 10:07:38 -04003866 GrBackendRenderTarget beRT = GrBackendRenderTarget(dimensions.width(), dimensions.height(),
3867 sampleCnt, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003868 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003869 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003870}
3871
3872void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003873 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003874 GrGLFramebufferInfo info;
3875 if (backendRT.getGLFramebufferInfo(&info)) {
3876 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003877 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003878 }
3879 }
joshualitt8fd844f2015-12-02 13:36:47 -08003880}
Brian Salomonf865b052018-03-09 09:01:53 -05003881#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003882
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003883///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003884
cdaltone2e71c22016-04-07 18:13:29 -07003885GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003886 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003887 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003888 GrGLAttribArrayState* attribState;
3889
cdaltone2e71c22016-04-07 18:13:29 -07003890 if (gpu->glCaps().isCoreProfile()) {
3891 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003892 GrGLuint arrayID;
3893 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3894 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003895 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003896 }
cdaltone2e71c22016-04-07 18:13:29 -07003897 if (ibuf) {
3898 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003899 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003900 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003901 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003902 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003903 if (ibuf) {
3904 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003905 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003906 } else {
3907 this->setVertexArrayID(gpu, 0);
3908 }
3909 int attrCount = gpu->glCaps().maxVertexAttributes();
3910 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3911 fDefaultVertexArrayAttribState.resize(attrCount);
3912 }
3913 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003914 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003915 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003916}
bsalomone179a912016-01-20 06:18:10 -08003917
Greg Danielfe159622020-04-10 17:43:51 +00003918void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3919 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003920 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003921}
3922
Greg Daniel78be37f2020-04-14 16:40:35 -04003923void GrGLGpu::flush(FlushType flushType) {
3924 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3925 GL_CALL(Flush());
3926 fNeedsGLFlush = false;
3927 }
3928}
3929
Greg Danielfe159622020-04-10 17:43:51 +00003930bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3931 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Daniela89b4302021-01-29 10:48:40 -05003932 this->finishOutstandingGpuWork();
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003933 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003934 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003935 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003936 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003937 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003938 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003939 if (!this->glCaps().skipErrorChecks()) {
3940 this->clearErrorsAndCheckForOOM();
3941 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003942 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003943}
3944
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003945void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3946 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3947 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3948 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003949}
3950
Greg Daniel6be35232017-03-01 17:01:09 -05003951GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003952 if (!this->caps()->fenceSyncSupport()) {
3953 return 0;
3954 }
Greg Daniel6be35232017-03-01 17:01:09 -05003955 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003956 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3957 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3958 GrGLuint fence = 0;
3959 GL_CALL(GenFences(1, &fence));
3960 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3961 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3962 } else {
3963 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3964 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003965 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003966 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003967 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003968}
3969
Brian Salomonb0d8b762019-05-06 16:58:22 -04003970bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003971 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3972 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3973 if (!timeout) {
3974 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003975 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003976 }
3977 GrGLboolean result;
3978 GL_CALL_RET(result, TestFence(nvFence));
3979 return result == GR_GL_TRUE;
3980 }
3981 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3982 // If this really becomes necessary we could poll TestFence().
3983 // FinishFence always flushes so no need to check flush param.
3984 GL_CALL(FinishFence(nvFence));
3985 return true;
3986 } else {
3987 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3988 GrGLenum result;
3989 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3990 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3991 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003992}
3993
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003994bool GrGLGpu::waitFence(GrFence fence) {
3995 if (!this->caps()->fenceSyncSupport()) {
3996 return true;
3997 }
3998 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003999}
4000
4001void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004002 if (this->caps()->fenceSyncSupport()) {
4003 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
4004 }
Greg Daniel6be35232017-03-01 17:01:09 -05004005}
4006
Greg Daniel301015c2019-11-18 14:06:46 -05004007std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004008 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004009 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05004010}
4011
Greg Daniel301015c2019-11-18 14:06:46 -05004012std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
4013 const GrBackendSemaphore& semaphore,
4014 GrResourceProvider::SemaphoreWrapType wrapType,
4015 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004016 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004017 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4018}
4019
Greg Daniel301015c2019-11-18 14:06:46 -05004020void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004021 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004022 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004023
Greg Daniel48661b82018-01-22 16:11:35 -05004024 GrGLsync sync;
4025 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4026 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04004027 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05004028}
4029
Greg Daniel301015c2019-11-18 14:06:46 -05004030void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004031 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004032 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004033
4034 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4035}
4036
Brian Salomonb0d8b762019-05-06 16:58:22 -04004037void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004038 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004039}
4040
Greg Daniela89b4302021-01-29 10:48:40 -05004041void GrGLGpu::finishOutstandingGpuWork() {
4042 GL_CALL(Finish());
4043}
4044
Brian Salomon24069eb2020-06-24 10:19:52 -04004045void GrGLGpu::clearErrorsAndCheckForOOM() {
4046 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4047}
4048
4049GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4050#if GR_GL_CHECK_ERROR
4051 if (this->glInterface()->checkAndResetOOMed()) {
4052 this->setOOMed();
4053 }
4054#endif
4055 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4056 if (error == GR_GL_OUT_OF_MEMORY) {
4057 this->setOOMed();
4058 }
4059 return error;
4060}
4061
Greg Daniel6be35232017-03-01 17:01:09 -05004062void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004063 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4064 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4065 GL_CALL(DeleteFences(1, &nvFence));
4066 } else {
4067 GL_CALL(DeleteSync(sync));
4068 }
jvanverth84741b32016-09-30 08:39:02 -07004069}
Brian Osman13dddce2017-05-09 13:19:50 -04004070
Greg Daniel301015c2019-11-18 14:06:46 -05004071std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004072 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004073 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004074 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004075 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004076 // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu.
Greg Daniel78be37f2020-04-14 16:40:35 -04004077 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004078
4079 return semaphore;
4080}
Robert Phillips646e4292017-06-13 12:44:56 -04004081
4082int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004083 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004084 case kTexture2DSampler_GrSLType:
4085 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004086 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004087 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004088 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004089 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004090 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004091 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004092 }
4093}
Brian Osman71a18892017-08-10 10:23:25 -04004094
Kevin Lubickf4def342018-10-04 12:52:50 -04004095#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004096#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004097void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4098 // We are called by the base class, which has already called beginObject(). We choose to nest
4099 // all of our caps information in a named sub-object.
4100 writer->beginObject("GL GPU");
4101
4102 const GrGLubyte* str;
4103 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4104 writer->appendString("GL_VERSION", (const char*)(str));
4105 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4106 writer->appendString("GL_RENDERER", (const char*)(str));
4107 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4108 writer->appendString("GL_VENDOR", (const char*)(str));
4109 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4110 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4111
4112 writer->appendName("extensions");
4113 glInterface()->fExtensions.dumpJSON(writer);
4114
4115 writer->endObject();
4116}
Kevin Lubickf4def342018-10-04 12:52:50 -04004117#endif