blob: 76f69754c65a1910d2e9739607ff2bfa0565a19a [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();
kkinnunen702501d2016-01-13 23:36:45 -0800390
Chris Dalton20e7a532020-02-28 15:37:53 +0000391 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400392 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000393 // detach the current program so there is no confusion on OpenGL's part
394 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000395 GL_CALL(UseProgram(0));
396 }
397
Brian Salomon43f8bf02017-10-18 08:33:29 -0400398 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700399 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800400 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400401 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700402 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800403 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400404 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700405 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800406 }
egdaniel0f5f9672015-02-03 11:10:51 -0800407
bsalomon7ea33f52015-11-22 14:51:00 -0800408 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
409 if (0 != fCopyPrograms[i].fProgram) {
410 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
411 }
bsalomon6df86402015-06-01 10:41:49 -0700412 }
bsalomon6dea83f2015-12-03 12:58:06 -0800413
brianosman33f6b3f2016-06-02 05:49:21 -0700414 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
415 if (0 != fMipmapPrograms[i].fProgram) {
416 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
417 }
418 }
419
Brian Salomondc829942018-10-23 16:07:24 -0400420 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500421
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400422 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700423}
424
bsalomon6e2aad42016-04-01 11:54:31 -0700425void GrGLGpu::disconnect(DisconnectType type) {
426 INHERITED::disconnect(type);
427 if (DisconnectType::kCleanup == type) {
428 if (fHWProgramID) {
429 GL_CALL(UseProgram(0));
430 }
431 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700432 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700433 }
434 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700435 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700436 }
437 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700438 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700439 }
bsalomon6e2aad42016-04-01 11:54:31 -0700440 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
441 if (fCopyPrograms[i].fProgram) {
442 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
443 }
444 }
brianosman33f6b3f2016-06-02 05:49:21 -0700445 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
446 if (fMipmapPrograms[i].fProgram) {
447 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
448 }
449 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400450
Brian Salomondc829942018-10-23 16:07:24 -0400451 if (fSamplerObjectCache) {
452 fSamplerObjectCache->release();
453 }
bsalomon6e2aad42016-04-01 11:54:31 -0700454 } else {
455 if (fProgramCache) {
456 fProgramCache->abandon();
457 }
Brian Salomondc829942018-10-23 16:07:24 -0400458 if (fSamplerObjectCache) {
459 fSamplerObjectCache->abandon();
460 }
bsalomon6e2aad42016-04-01 11:54:31 -0700461 }
462
Chris Dalton20e7a532020-02-28 15:37:53 +0000463 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500464 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700465
bsalomonc8dc1f72014-08-21 13:02:13 -0700466 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700467 fTempSrcFBOID = 0;
468 fTempDstFBOID = 0;
469 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700470 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800471 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
472 fCopyPrograms[i].fProgram = 0;
473 }
brianosman33f6b3f2016-06-02 05:49:21 -0700474 fMipmapProgramArrayBuffer.reset();
475 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
476 fMipmapPrograms[i].fProgram = 0;
477 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400478
jvanverthe9c0fc62015-04-29 11:18:05 -0700479 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700480 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700481 }
Adlai Holler61a591c2020-10-12 12:38:33 -0400482 fFinishCallbacks.callAll(/* doDelete */ DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483}
484
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000485///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000486
bsalomon861e1032014-12-16 07:33:49 -0800487void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000488 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400489 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000490 GL_CALL(Disable(GR_GL_DEPTH_TEST));
491 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000492
Brian Salomonf0861672017-05-08 11:10:10 -0400493 // We don't use face culling.
494 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600495 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
496 // just set this to the default for self-consistency.
497 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400498
Brian Salomonae64c192019-02-05 09:41:37 -0500499 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
500 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700501
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400502 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500503#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000504 // Desktop-only state that we never change
505 if (!this->glCaps().isCoreProfile()) {
506 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
507 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
508 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
509 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
510 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
511 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
512 }
513 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
514 // core profile. This seems like a bug since the core spec removes any mention of
515 // GL_ARB_imaging.
516 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
517 GL_CALL(Disable(GR_GL_COLOR_TABLE));
518 }
519 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400520
Chris Dalton1215cda2019-12-17 21:44:04 -0700521 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500522#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 // Since ES doesn't support glPointSize at all we always use the VS to
524 // set the point size
525 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
526
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000527 }
joshualitt58162332014-08-01 06:44:53 -0700528
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400529 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500530 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700531 // The arm extension requires specifically enabling MSAA fetching per sample.
532 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500533 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700534 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 fHWWriteToColor = kUnknown_TriState;
536 // we only ever use lines in hairline mode
537 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700538 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500539
540 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000541 }
edisonn@google.comba669992013-06-28 16:03:21 +0000542
egdanielb414f252014-07-29 13:15:47 -0700543 if (resetBits & kMSAAEnable_GrGLBackendState) {
544 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700545
Chris Dalton6ce447a2019-06-23 18:07:38 -0600546 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800547 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
548 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700549 GL_CALL(CoverageModulation(GR_GL_RGBA));
550 }
Chris Daltonce425af2019-12-16 10:39:03 -0700551
552 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000553 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000555 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400556 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000557
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500559 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500560 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000561 }
Brian Salomondc829942018-10-23 16:07:24 -0400562 if (fSamplerObjectCache) {
563 fSamplerObjectCache->invalidateBindings();
564 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000565 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000566
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000567 if (resetBits & kBlend_GrGLBackendState) {
568 fHWBlendState.invalidate();
569 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000570
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 if (resetBits & kView_GrGLBackendState) {
572 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700573 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000574 fHWViewport.invalidate();
575 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000576
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000577 if (resetBits & kStencil_GrGLBackendState) {
578 fHWStencilSettings.invalidate();
579 fHWStencilTestEnabled = kUnknown_TriState;
580 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000581
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 // Vertex
583 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700584 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500585 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
586 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600587 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700588 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500592 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700593 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500594 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000596
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000597 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700598 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700599 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000601 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000602
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000603 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400605 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
607 }
Brian Salomon1047a492019-07-02 12:25:21 -0400608 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
610 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000611 if (this->glCaps().packFlipYSupport()) {
612 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
613 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000614 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000615
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000616 if (resetBits & kProgram_GrGLBackendState) {
617 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000618 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000619 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400620 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000621}
622
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400623static bool check_backend_texture(const GrBackendTexture& backendTex,
624 const GrGLCaps& caps,
625 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400626 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400627 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400628 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400629 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800630 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000631
Brian Salomonea4ad302019-08-07 13:04:55 -0400632 desc->fSize = {backendTex.width(), backendTex.height()};
633 desc->fTarget = info.fTarget;
634 desc->fID = info.fID;
635 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800636
Brian Salomonea4ad302019-08-07 13:04:55 -0400637 if (desc->fFormat == GrGLFormat::kUnknown) {
638 return false;
639 }
640 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400641 if (!caps.shaderCaps()->externalTextureSupport()) {
642 return false;
643 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400644 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
645 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400646 return false;
647 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400648 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400649 return false;
650 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400651 if (backendTex.isProtected()) {
652 // Not supported in GL backend at this time.
653 return false;
654 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400655
Brian Salomond17f6582017-07-19 18:28:58 -0400656 return true;
657}
658
659sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400660 GrWrapOwnership ownership,
661 GrWrapCacheable cacheable,
662 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400663 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400664 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400665 return nullptr;
666 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400667
Brian Salomond17f6582017-07-19 18:28:58 -0400668 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400669 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400670 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400671 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400672 }
bsalomone5286e02016-01-14 09:24:09 -0800673
Brian Salomon40a40622020-07-21 10:32:07 -0400674 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400675 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400676
Brian Salomona6db5102020-07-21 09:56:23 -0400677 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400678 backendTex.getGLTextureParams(), cacheable, ioType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600679 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400680}
681
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500682static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
683 const GrGLCaps& caps, GrGLTexture::Desc* desc,
684 bool skipRectTexSupportCheck = false) {
685 GrGLTextureInfo info;
686 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
687 return false;
688 }
689
690 desc->fSize = {backendTex.width(), backendTex.height()};
691 desc->fTarget = info.fTarget;
692 desc->fID = info.fID;
693 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
694
695 if (desc->fFormat == GrGLFormat::kUnknown) {
696 return false;
697 }
698
699 if (GR_GL_TEXTURE_2D != desc->fTarget) {
700 return false;
701 }
702 if (backendTex.isProtected()) {
703 // Not supported in GL backend at this time.
704 return false;
705 }
706
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500707 return true;
708}
709
Robert Phillipsb915c942019-12-17 14:44:37 -0500710sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
711 GrWrapOwnership ownership,
712 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500713 GrGLTexture::Desc desc;
714 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
715 return nullptr;
716 }
717
718 if (kBorrow_GrWrapOwnership == ownership) {
719 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
720 } else {
721 desc.fOwnership = GrBackendObjectOwnership::kOwned;
722 }
723
Brian Salomon40a40622020-07-21 10:32:07 -0400724 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400725 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500726
Brian Salomona6db5102020-07-21 09:56:23 -0400727 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500728 backendTex.getGLTextureParams(), cacheable,
729 kRead_GrIOType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600730 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500731}
732
Brian Salomond17f6582017-07-19 18:28:58 -0400733sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400734 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500735 GrWrapOwnership ownership,
736 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400737 const GrGLCaps& caps = this->glCaps();
738
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400740 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800741 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800742 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400743 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
744 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800745
Brian Salomond17f6582017-07-19 18:28:58 -0400746 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400747 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800748 return nullptr;
749 }
bsalomon10528f12015-10-14 12:54:52 -0700750
Brian Osman766fcbb2017-03-13 09:33:09 -0400751 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400752 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400753 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400754 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800755 }
bsalomonb15b4c12014-10-29 12:41:57 -0700756
Robert Phillips0902c982019-07-16 07:47:56 -0400757
Greg Daniel6fa62e22019-08-07 15:52:37 -0400758 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
759 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000760
Brian Salomonea4ad302019-08-07 13:04:55 -0400761 GrGLRenderTarget::IDs rtIDs;
762 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400763 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000764 }
Greg Daniel177e6952017-10-12 12:27:11 -0400765
Brian Salomon40a40622020-07-21 10:32:07 -0400766 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400767 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400768
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500769 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400770 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400771 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400772 texRT->baseLevelWasBoundToFBO();
Mike Kleina9609ea2020-02-18 13:33:23 -0600773 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000774}
775
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400776sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400777 GrGLFramebufferInfo info;
778 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000779 return nullptr;
780 }
781
Brian Salomone8a766b2019-07-19 14:24:36 -0400782 if (backendRT.isProtected()) {
783 // Not supported in GL at this time.
784 return nullptr;
785 }
786
Brian Salomond4764a12019-08-08 12:08:24 -0400787 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400788 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
789 return nullptr;
790 }
791
Brian Salomonea4ad302019-08-07 13:04:55 -0400792 GrGLRenderTarget::IDs rtIDs;
793 rtIDs.fRTFBOID = info.fFBOID;
794 rtIDs.fMSColorRenderbufferID = 0;
795 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
796 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000797
Greg Daniel6fa62e22019-08-07 15:52:37 -0400798 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700799
Brian Salomona56a7462020-02-07 14:17:25 -0500800 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400801 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000802}
803
Brian Salomonc320b152018-02-20 14:05:36 -0500804static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700805 if (!glTex) {
806 return false;
807 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000808
bsalomone5286e02016-01-14 09:24:09 -0800809 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
810 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800811 return false;
812 }
813
jvanverth17aa0472016-01-05 10:41:27 -0800814 return true;
815}
816
Brian Salomona9b04b92018-06-01 15:04:28 -0400817bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400818 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400819 const GrMipLevel texels[], int mipLevelCount,
820 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500821 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800822
Brian Salomonc320b152018-02-20 14:05:36 -0500823 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800824 return false;
825 }
826
Brian Salomond978b902019-02-07 15:09:18 -0500827 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000828
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400829 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400830 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
831 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
832 glTex->target(), dstRect, srcColorType, texels,
833 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000834}
835
Brian Salomone05ba5a2019-04-08 11:59:07 -0400836bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400837 GrColorType textureColorType, GrColorType bufferColorType,
838 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400839 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400840
Jim Van Verth1676cb92019-01-15 13:24:45 -0500841 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400842 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500843
Brian Salomonc320b152018-02-20 14:05:36 -0500844 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400845 return false;
846 }
847
Hal Canaryc36f7e72018-06-18 15:50:09 -0400848 static_assert(sizeof(int) == sizeof(int32_t), "");
849 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400850 return false;
851 }
852
Brian Salomond978b902019-02-07 15:09:18 -0500853 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854
855 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500856 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400857 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500858 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859
Greg Daniel660cc992017-06-26 14:55:05 -0400860 SkDEBUGCODE(
861 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
862 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
863 SkASSERT(bounds.contains(subRect));
864 )
865
Brian Salomonb28cb682019-07-26 12:48:47 -0400866 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400867 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400868 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700869 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400870 return false;
871 }
872
873 bool restoreGLRowLength = false;
874 if (trimRowBytes != rowBytes) {
875 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400876 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400877 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
878 restoreGLRowLength = true;
879 }
880
Greg Danielba88ab62019-07-26 09:14:01 -0400881 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400882 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400883 GrGLenum externalFormat = 0;
884 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400885 this->glCaps().getTexSubImageExternalFormatAndType(
886 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400887 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400888 return false;
889 }
890
Brian Salomon8cbf6622019-07-30 11:03:14 -0400891 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400892 GL_CALL(TexSubImage2D(glTex->target(),
893 0,
894 left, top,
895 width,
896 height,
897 externalFormat, externalType,
898 pixels));
899
900 if (restoreGLRowLength) {
901 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
902 }
903
904 return true;
905}
906
Brian Salomon26de56e2019-04-10 12:14:26 -0400907bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400908 GrColorType surfaceColorType, GrColorType dstColorType,
909 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400910 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
911 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400912 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400913 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
914 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400915}
916
Brian Osman9b560242017-09-05 15:34:52 -0400917void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500918 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
919 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
920 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
921 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400922 }
Brian Osman9b560242017-09-05 15:34:52 -0400923}
924
Brian Salomon22558932020-05-15 14:46:34 -0400925bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
926 GrColorType textureColorType,
927 SkISize texDims,
928 GrGLenum target,
929 SkIRect dstRect,
930 GrColorType srcColorType,
931 const GrMipLevel texels[],
932 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500933 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400934 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500935
Greg Daniel7bfc9132019-08-14 14:23:53 -0400936 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800937
Brian Salomonf77c1462019-08-01 15:19:29 -0400938 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800939
bsalomon5b30c6f2015-12-17 14:17:34 -0800940 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800941 GrGLenum externalFormat;
942 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400943 this->glCaps().getTexSubImageExternalFormatAndType(
944 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400945 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800946 return false;
947 }
Brian Salomon22558932020-05-15 14:46:34 -0400948 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
949 mipLevelCount);
950 return true;
951}
Brian Salomonf77c1462019-08-01 15:19:29 -0400952
Brian Salomon22558932020-05-15 14:46:34 -0400953bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
954 SkISize texDims,
955 GrGLenum target,
956 SkColor4f color,
957 uint32_t levelMask) {
958 GrColorType colorType;
959 GrGLenum externalFormat, externalType;
960 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
961 &externalType, &colorType);
962 if (colorType == GrColorType::kUnknown) {
963 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000964 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400965
Brian Salomon22558932020-05-15 14:46:34 -0400966 std::unique_ptr<char[]> pixelStorage;
967 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400968 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400969 SkSTArray<16, GrMipLevel> levels;
970 levels.resize(numLevels);
971 SkISize levelDims = texDims;
972 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
973 std::max(levelDims.height() >> 1, 1)}) {
974 if (levelMask & (1 << i)) {
975 if (!pixelStorage) {
976 // Make one tight image at the first size and reuse it for smaller levels.
977 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
978 size_t rb = ii.minRowBytes();
979 pixelStorage.reset(new char[rb * levelDims.height()]);
980 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
981 return false;
982 }
983 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000984 }
Brian Salomon22558932020-05-15 14:46:34 -0400985 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
986 }
987 }
988 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
989 bpp, levels.begin(), levels.count());
990 return true;
991}
992
993void GrGLGpu::uploadTexData(SkISize texDims,
994 GrGLenum target,
995 SkIRect dstRect,
996 GrGLenum externalFormat,
997 GrGLenum externalType,
998 size_t bpp,
999 const GrMipLevel texels[],
1000 int mipLevelCount) {
1001 SkASSERT(!texDims.isEmpty());
1002 SkASSERT(!dstRect.isEmpty());
1003 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001004 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001005 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1006
1007 const GrGLCaps& caps = this->glCaps();
1008
1009 bool restoreGLRowLength = false;
1010
1011 this->unbindCpuToGpuXferBuffer();
1012 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1013
1014 SkISize dims = dstRect.size();
1015 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1016 std::max(dims.height() >> 1, 1)}) {
1017 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001018 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001019 }
Brian Salomon22558932020-05-15 14:46:34 -04001020 const size_t trimRowBytes = dims.width() * bpp;
1021 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001022
1023 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1024 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001025 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001026 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001027 } else {
1028 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001029 }
1030
Brian Salomon22558932020-05-15 14:46:34 -04001031 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1032 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001033 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001034 if (restoreGLRowLength) {
1035 SkASSERT(caps.writePixelsRowBytesSupport());
1036 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1037 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001038}
1039
Greg Daniel01f278c2020-06-12 16:58:17 -04001040bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1041 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001042 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001043 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001044 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001045 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001046 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001047 const GrGLCaps& caps = this->glCaps();
1048
1049 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001050 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001051 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001052 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001053 }
1054
Robert Phillips9f744f72019-12-19 19:14:33 -05001055 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1056
Greg Daniel7bfc9132019-08-14 14:23:53 -04001057 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001058
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001059 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001060 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001061 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001062 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001063
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001064 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001065 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001066
1067 if (useTexStorage) {
1068 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001069 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1070 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001071 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073 }
Robert Phillips42716d42019-12-16 12:19:54 -05001074
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001075 size_t offset = 0;
1076 for (int level = 0; level < numMipLevels; ++level) {
1077
Robert Phillips99dead92020-01-27 16:11:57 -05001078 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1079 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001080
Brian Salomond8575452020-02-25 12:13:29 -05001081 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1082 level,
1083 0, // left
1084 0, // top
1085 dimensions.width(),
1086 dimensions.height(),
1087 internalFormat,
1088 SkToInt(levelDataSize),
1089 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001090
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001091 if (error != GR_GL_NO_ERROR) {
1092 return false;
1093 }
1094
Robert Phillips9f744f72019-12-19 19:14:33 -05001095 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001096 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001097 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001098 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001100
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001101 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001102 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1103 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001104
1105 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001106 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1107 level,
1108 internalFormat,
1109 dimensions.width(),
1110 dimensions.height(),
1111 0, // border
1112 SkToInt(levelDataSize),
1113 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001114
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001115 if (error != GR_GL_NO_ERROR) {
1116 return false;
1117 }
1118
Robert Phillips9f744f72019-12-19 19:14:33 -05001119 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001120 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001121 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001122 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001123 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001124}
1125
Brian Salomond8575452020-02-25 12:13:29 -05001126bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001127 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001128 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001129 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001130 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001131 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001132 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1133 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001134 break;
1135 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001136 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1137 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001138 break;
1139 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1140 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001141 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1142 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001143 break;
1144 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001145 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001146 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001147 }
Brian Salomond8575452020-02-25 12:13:29 -05001148 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001149}
1150
Brian Salomonea4ad302019-08-07 13:04:55 -04001151bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001152 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001153 GrGLRenderTarget::IDs* rtIDs) {
1154 rtIDs->fMSColorRenderbufferID = 0;
1155 rtIDs->fRTFBOID = 0;
1156 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1157 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001158
bsalomona11e5fc2015-12-18 07:59:41 -08001159 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001160
Brian Salomonea4ad302019-08-07 13:04:55 -04001161 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001162 goto FAILED;
1163 }
1164
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001165 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001166 goto FAILED;
1167 }
1168
Brian Salomonea4ad302019-08-07 13:04:55 -04001169 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1170 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001171 goto FAILED;
1172 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001173
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001174 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1175 // the texture bound to the other. The exception is the IMG multisample extension. With this
1176 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1177 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001178 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001179 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1180 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1181 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001182 goto FAILED;
1183 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001184 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001186 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001187 }
1188
egdanield803f272015-03-18 13:01:52 -07001189 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001190 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001191 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001192 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001194 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1195 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 goto FAILED;
1197 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001199 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001200 GR_GL_COLOR_ATTACHMENT0,
1201 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001202 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001204 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001205
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001206 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1208 GR_GL_COLOR_ATTACHMENT0,
1209 desc.fTarget,
1210 desc.fID,
1211 0,
1212 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001213 } else {
egdanield803f272015-03-18 13:01:52 -07001214 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001215 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001216 desc.fTarget,
1217 desc.fID,
1218 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001219 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220
1221 return true;
1222
1223FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001224 if (rtIDs->fMSColorRenderbufferID) {
1225 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001227 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1228 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001229 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 if (rtIDs->fTexFBOID) {
1231 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001232 }
1233 return false;
1234}
1235
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001236// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001237static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001238// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001239 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001240}
1241
Brian Salomone2826ab2019-06-04 15:58:31 -04001242static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001243 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001244 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1245 // drivers have a bug where an FBO won't be complete if it includes a
1246 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001247 GrGLTextureParameters::SamplerOverriddenState state;
1248 state.fMinFilter = GR_GL_NEAREST;
1249 state.fMagFilter = GR_GL_NEAREST;
1250 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1251 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001252 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1253 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1254 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1255 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001256 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001257}
1258
Brian Salomona56a7462020-02-07 14:17:25 -05001259sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001260 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001261 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001262 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001263 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001264 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001265 int mipLevelCount,
1266 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001267 // We don't support protected textures in GL.
1268 if (isProtected == GrProtected::kYes) {
1269 return nullptr;
1270 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001271 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001272
Brian Salomond2a8ae22019-09-10 16:03:59 -04001273 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001274 GrMipmapStatus mipmapStatus =
1275 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001276 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001277 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001278 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001279 switch (format.textureType()) {
1280 case GrTextureType::kExternal:
1281 case GrTextureType::kNone:
1282 return nullptr;
1283 case GrTextureType::k2D:
1284 texDesc.fTarget = GR_GL_TEXTURE_2D;
1285 break;
1286 case GrTextureType::kRectangle:
1287 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1288 return nullptr;
1289 }
1290 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1291 break;
1292 }
Brian Salomon81536f22019-08-08 16:30:49 -04001293 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001294 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001295 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1296 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001297
Brian Salomon0f396992020-06-19 19:51:21 -04001298 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1299 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001300
1301 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001302 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001303 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001304
Robert Phillips67d52cf2017-06-05 13:38:13 -04001305 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001306 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001307 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001308 GL_CALL(BindTexture(texDesc.fTarget, 0));
1309 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001310
Brian Salomonea4ad302019-08-07 13:04:55 -04001311 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1312 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 return return_null_texture();
1314 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001315 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001316 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001317 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001318 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001319 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001321 // The non-sampler params are still at their default values.
1322 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1323 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001324 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001325 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001326 GrGLenum externalFormat, externalType;
1327 GrColorType colorType;
1328 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1329 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001330 for (int i = 0; i < mipLevelCount; ++i) {
1331 if (levelClearMask & (1U << i)) {
1332 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1333 nullptr));
1334 }
1335 }
1336 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1337 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001338 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001339 this->disableWindowRectangles();
1340 this->flushColorWrite(true);
Brian Salomon07bc9a22020-12-02 13:37:16 -05001341 this->flushClearColor({0, 0, 0, 0});
Brian Salomond2a8ae22019-09-10 16:03:59 -04001342 for (int i = 0; i < mipLevelCount; ++i) {
1343 if (levelClearMask & (1U << i)) {
1344 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1345 kDst_TempFBOTarget);
1346 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1347 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1348 }
1349 }
Brian Salomonc2249852019-09-16 11:08:50 -04001350 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001351 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001352 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001353 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001354 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001355 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001356 }
1357 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001358 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001359}
1360
Robert Phillips9f744f72019-12-19 19:14:33 -05001361sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001362 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001363 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001364 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001365 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001366 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001367 // We don't support protected textures in GL.
1368 if (isProtected == GrProtected::kYes) {
1369 return nullptr;
1370 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001371 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1372
Brian Salomonbb8dde82019-06-27 10:52:13 -04001373 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001374 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001375 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001376 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001377 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001378 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001379 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001380 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001381 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001382 return nullptr;
1383 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001384
Greg Danielaaf738c2020-07-10 09:30:33 -04001385 if (data) {
1386 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1387 GR_GL_TEXTURE_2D, data, dataSize)) {
1388 GL_CALL(DeleteTextures(1, &desc.fID));
1389 return nullptr;
1390 }
1391 }
1392
Robert Phillipsee946932019-12-18 11:16:17 -05001393 // Unbind this texture from the scratch texture unit.
1394 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1395
Brian Salomona6db5102020-07-21 09:56:23 -04001396 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1397 ? GrMipmapStatus::kValid
1398 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001399
Brian Salomona6db5102020-07-21 09:56:23 -04001400 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001401 // The non-sampler params are still at their default values.
1402 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1403 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001404 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001405}
1406
Greg Danielc1ad77c2020-05-06 11:40:03 -04001407GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001408 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001409 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001410 // We don't support protected textures in GL.
1411 if (isProtected == GrProtected::kYes) {
1412 return {};
1413 }
1414
1415 this->handleDirtyContext();
1416
1417 GrGLFormat glFormat = format.asGLFormat();
1418 if (glFormat == GrGLFormat::kUnknown) {
1419 return {};
1420 }
1421
Greg Daniel01f278c2020-06-12 16:58:17 -04001422 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1423
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001424 GrGLTextureInfo info;
1425 GrGLTextureParameters::SamplerOverriddenState initialState;
1426
1427 info.fTarget = GR_GL_TEXTURE_2D;
1428 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001429 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001430 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001431 if (!info.fID) {
1432 return {};
1433 }
1434
1435 // Unbind this texture from the scratch texture unit.
1436 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1437
1438 auto parameters = sk_make_sp<GrGLTextureParameters>();
1439 // The non-sampler params are still at their default values.
1440 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1441 fResetTimestampForTextureParameters);
1442
1443 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1444 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001445}
1446
Greg Danielaaf738c2020-07-10 09:30:33 -04001447bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1448 sk_sp<GrRefCntedCallback> finishedCallback,
1449 const BackendTextureData* data) {
1450 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1451
1452 GrGLTextureInfo info;
1453 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1454
1455 GrBackendFormat format = backendTexture.getBackendFormat();
1456 GrGLFormat glFormat = format.asGLFormat();
1457 if (glFormat == GrGLFormat::kUnknown) {
1458 return false;
1459 }
1460 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1461
Brian Salomon40a40622020-07-21 10:32:07 -04001462 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001463
1464 const char* rawData = nullptr;
1465 size_t rawDataSize = 0;
1466 SkAutoMalloc am;
1467 if (data->type() == BackendTextureData::Type::kCompressed) {
1468 rawData = (const char*)data->compressedData();
1469 rawDataSize = data->compressedSize();
1470 } else {
1471 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1472 SkASSERT(compression != SkImage::CompressionType::kNone);
1473
1474 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001475 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001476
1477 am.reset(rawDataSize);
1478
1479 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1480 data->color());
1481
1482 rawData = (const char*)am.get();
1483 }
1484
1485 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001486
1487 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1488 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04001489 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelControlSupport()) {
Greg Daniel95afafb2020-07-22 12:09:26 -04001490 auto params = backendTexture.getGLTextureParams();
1491 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1492 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1493 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1494 nonsamplerState.fBaseMipMapLevel = 0;
1495 }
1496 int numMipLevels =
1497 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1498 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1499 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1500 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1501 }
1502 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1503 }
1504
Greg Danielaaf738c2020-07-10 09:30:33 -04001505 bool result = this->uploadCompressedTexData(
1506 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1507 rawData, rawDataSize);
1508
1509 // Unbind this texture from the scratch texture unit.
1510 this->bindTextureToScratchUnit(info.fTarget, 0);
1511
1512 return result;
1513}
1514
Brian Salomon5043f1f2019-07-11 21:27:54 -04001515int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001516 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001517 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1518
1519 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001520 // Default to unsupported, set this if we find a stencil format that works.
1521 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001522
Brian Salomon0f396992020-06-19 19:51:21 -04001523 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1524 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001525 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001526 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001527 }
egdanielff1d5472015-09-10 08:37:20 -07001528 // unbind the texture from the texture unit before binding it to the frame buffer
1529 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1530
1531 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001532 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001533 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001534 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001535 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001536 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1537 GR_GL_COLOR_ATTACHMENT0,
1538 GR_GL_TEXTURE_2D,
1539 colorID,
1540 0));
bsalomon30447372015-12-21 09:03:05 -08001541 GrGLuint sbRBID = 0;
1542 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001543
1544 // look over formats till I find a compatible one
1545 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001546 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001547 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001548 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001549 GrGLFormat sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001550 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
Greg Daniel8ade5e82020-10-07 13:09:48 -04001551 GR_GL_RENDERBUFFER, GrGLFormatToEnum(sFmt), kSize, kSize));
Brian Salomond8575452020-02-25 12:13:29 -05001552 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001553 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001554 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001555 GR_GL_RENDERBUFFER, sbRBID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001556 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
bsalomon30447372015-12-21 09:03:05 -08001557 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1558 GR_GL_DEPTH_ATTACHMENT,
1559 GR_GL_RENDERBUFFER, sbRBID));
1560 } else {
1561 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1562 GR_GL_DEPTH_ATTACHMENT,
1563 GR_GL_RENDERBUFFER, 0));
1564 }
1565 GrGLenum status;
1566 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1567 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1568 firstWorkingStencilFormatIndex = i;
1569 break;
1570 }
egdanielff1d5472015-09-10 08:37:20 -07001571 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1572 GR_GL_STENCIL_ATTACHMENT,
1573 GR_GL_RENDERBUFFER, 0));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001574 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
egdanielff1d5472015-09-10 08:37:20 -07001575 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1576 GR_GL_DEPTH_ATTACHMENT,
1577 GR_GL_RENDERBUFFER, 0));
1578 }
egdanielff1d5472015-09-10 08:37:20 -07001579 }
1580 }
bsalomon30447372015-12-21 09:03:05 -08001581 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001582 }
1583 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001584 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1585 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001586 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001587 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001588 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001589}
1590
Brian Salomonea4ad302019-08-07 13:04:55 -04001591GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001592 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001593 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001594 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001595 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001596 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001597 if (format == GrGLFormat::kUnknown) {
1598 return 0;
1599 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001600 GrGLuint id = 0;
1601 GL_CALL(GenTextures(1, &id));
1602 if (!id) {
1603 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001604 }
1605
Brian Salomonea4ad302019-08-07 13:04:55 -04001606 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001607
Brian Salomonea4ad302019-08-07 13:04:55 -04001608 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1609
Brian Salomonea4ad302019-08-07 13:04:55 -04001610 return id;
1611}
1612
Brian Salomon0f396992020-06-19 19:51:21 -04001613GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1614 GrGLFormat format,
1615 GrGLenum target,
1616 GrRenderable renderable,
1617 GrGLTextureParameters::SamplerOverriddenState* initialState,
1618 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001619 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001620 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001621
Brian Salomonea4ad302019-08-07 13:04:55 -04001622 GrGLuint id = 0;
1623 GL_CALL(GenTextures(1, &id));
1624
1625 if (!id) {
1626 return 0;
1627 }
1628
Brian Salomon0f396992020-06-19 19:51:21 -04001629 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001630
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001631 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001632 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001633 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001634 }
1635
Brian Salomond2a8ae22019-09-10 16:03:59 -04001636 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001637 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001638 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001639 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001640 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001641
1642 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1643
1644 bool success = false;
1645 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001646 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001647 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001648 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1649 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001650 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001651 } else {
1652 GrGLenum externalFormat, externalType;
1653 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1654 GrGLenum error = GR_GL_NO_ERROR;
1655 if (externalFormat && externalType) {
1656 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1657 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001658 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1659 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001660 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1661 currentHeight, 0, externalFormat, externalType,
1662 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001663 }
Brian Salomond8575452020-02-25 12:13:29 -05001664 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001665 }
1666 }
1667 }
1668 if (success) {
1669 return id;
1670 }
1671 GL_CALL(DeleteTextures(1, &id));
1672 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001673}
1674
Greg Danielc0d69152020-10-08 14:59:00 -04001675sk_sp<GrAttachment> GrGLGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1676 SkISize dimensions,
1677 int numStencilSamples) {
Greg Daniele77162e2020-09-21 15:32:11 -04001678 SkASSERT(dimensions.width() >= rt->width());
1679 SkASSERT(dimensions.height() >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001680
Greg Danielc0d69152020-10-08 14:59:00 -04001681 GrGLAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001682
Brian Salomond4764a12019-08-08 12:08:24 -04001683 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001684 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001685 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001686 }
egdanielff1d5472015-09-10 08:37:20 -07001687
1688 if (!sbDesc.fRenderbufferID) {
1689 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1690 }
1691 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001692 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001693 }
1694 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001695 GrGLFormat sFmt = this->glCaps().stencilFormats()[sIdx];
1696 GrGLenum glFmt = GrGLFormatToEnum(sFmt);
egdanielff1d5472015-09-10 08:37:20 -07001697 // we do this "if" so that we don't call the multisample
1698 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001699 if (numStencilSamples > 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001700 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001701 dimensions.width(), dimensions.height())) {
Brian Salomond8575452020-02-25 12:13:29 -05001702 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1703 return nullptr;
1704 }
egdanielff1d5472015-09-10 08:37:20 -07001705 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001706 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001707 dimensions.width(),
1708 dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001709 if (error != GR_GL_NO_ERROR) {
1710 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1711 return nullptr;
1712 }
egdanielff1d5472015-09-10 08:37:20 -07001713 }
1714 fStats.incStencilAttachmentCreates();
Greg Daniel17b0c752020-10-01 16:06:37 -04001715
Greg Danielc0d69152020-10-08 14:59:00 -04001716 return sk_sp<GrAttachment>(new GrGLAttachment(
Greg Daniel25ebd952020-11-03 11:17:05 -05001717 this, sbDesc, dimensions, GrAttachment::UsageFlags::kStencilAttachment,
1718 numStencilSamples, sFmt));
reed@google.comac10a2d2010-12-22 21:39:39 +00001719}
1720
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001721////////////////////////////////////////////////////////////////////////////////
1722
Brian Salomondbf70722019-02-07 11:31:24 -05001723sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1724 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001725 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001726}
1727
Chris Dalton2e7ed262020-02-21 15:17:59 -07001728void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1729 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001730 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1731 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1732 fHWScissorSettings.fEnabled = kYes_TriState;
1733 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001734 } else {
1735 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1736 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1737 fHWScissorSettings.fEnabled = kNo_TriState;
1738 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001740}
Brian Salomond818ebf2018-07-02 14:08:49 +00001741
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001742void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtHeight, GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001743 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001744 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1745 if (fHWScissorSettings.fRect != nativeScissor) {
1746 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1747 nativeScissor.fHeight));
1748 fHWScissorSettings.fRect = nativeScissor;
1749 }
joshualitt77b13072014-10-27 14:51:01 -07001750}
1751
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001752void GrGLGpu::flushViewport(const SkIRect& viewport, int rtHeight, GrSurfaceOrigin rtOrigin) {
1753 auto nativeViewport = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, viewport);
1754 if (fHWViewport != nativeViewport) {
1755 GL_CALL(Viewport(nativeViewport.fX, nativeViewport.fY,
1756 nativeViewport.fWidth, nativeViewport.fHeight));
1757 fHWViewport = nativeViewport;
1758 }
1759}
1760
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001761void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001762 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001763#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001764 typedef GrWindowRectsState::Mode Mode;
1765 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1766 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001767
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001768 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001769 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001770 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001771 }
1772
csmartdalton7535f412016-08-23 06:51:00 -07001773 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1774 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001775 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001776 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001777
Chris Daltond6cda8d2019-09-05 02:30:04 -06001778 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001780 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001781 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001782 }
1783
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001784 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001785 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001786
Greg Danielacd66b42019-05-22 16:29:12 -04001787 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001788#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001789}
1790
1791void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001792#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001793 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001794 return;
1795 }
1796 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001798#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001799}
1800
Robert Phillipsdf546db2020-05-29 09:42:54 -04001801bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001802 this->handleDirtyContext();
1803
Chris Daltond42d2002020-02-28 12:05:04 -07001804 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001805 if (!program) {
1806 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1807 return false;
1808 }
1809
1810 this->flushProgram(std::move(program));
1811
Chris Daltond42d2002020-02-28 12:05:04 -07001812 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1813 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1814 }
1815
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001816 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001817 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001818 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001819
Chris Dalton20e7a532020-02-28 15:37:53 +00001820 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001821
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001822 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001823 GrStencilSettings stencil;
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001824 if (programInfo.isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001825 SkASSERT(glRT->getStencilAttachment());
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001826 stencil.reset(*programInfo.userStencilSettings(),
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001827 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001828 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001829 }
Robert Phillips901aff02019-10-08 12:32:56 -04001830 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001831 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001832 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1833 glRT, programInfo.origin());
1834 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001835 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001836 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001837
1838 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001839 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001840 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001841
Chris Dalton20e7a532020-02-28 15:37:53 +00001842 return true;
1843}
1844
1845void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1846 if (!program) {
1847 fHWProgram.reset();
1848 fHWProgramID = 0;
1849 return;
1850 }
1851 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1852 if (program == fHWProgram) {
1853 return;
1854 }
1855 auto id = program->programID();
1856 SkASSERT(id);
1857 GL_CALL(UseProgram(id));
1858 fHWProgram = std::move(program);
1859 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001860}
1861
1862void GrGLGpu::flushProgram(GrGLuint id) {
1863 SkASSERT(id);
1864 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001865 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001866 return;
1867 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001868 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001869 GL_CALL(UseProgram(id));
1870 fHWProgramID = id;
1871}
1872
Brian Salomonae64c192019-02-05 09:41:37 -05001873GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001874 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001875
cdaltone2e71c22016-04-07 18:13:29 -07001876 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001877 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001878 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001879 }
cdaltone2e71c22016-04-07 18:13:29 -07001880
Brian Salomonae64c192019-02-05 09:41:37 -05001881 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001882 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001883 if (!bufferState->fBufferZeroKnownBound) {
1884 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1885 bufferState->fBufferZeroKnownBound = true;
1886 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001887 }
Brian Salomondbf70722019-02-07 11:31:24 -05001888 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1889 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001890 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1891 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1892 bufferState->fBufferZeroKnownBound = false;
1893 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001894 }
1895
Brian Salomonae64c192019-02-05 09:41:37 -05001896 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001897}
Brian Salomond818ebf2018-07-02 14:08:49 +00001898
Brian Salomon07bc9a22020-12-02 13:37:16 -05001899void GrGLGpu::clear(const GrScissorState& scissor,
1900 std::array<float, 4> color,
1901 GrRenderTarget* target,
1902 GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001903 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001904 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001905 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001906 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001907
Brian Salomon43f8bf02017-10-18 08:33:29 -04001908 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001909
Brian Salomon43f8bf02017-10-18 08:33:29 -04001910 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1911
Michael Ludwig1e632792020-05-21 12:45:31 -04001912 if (scissor.enabled()) {
1913 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001914 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001915 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001916 }
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05001917 this->flushScissor(scissor, glRT->height(), origin);
Michael Ludwig1e632792020-05-21 12:45:31 -04001918 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001919 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001920 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001921 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001922}
1923
Chris Daltonb50cc812019-10-14 16:29:21 -06001924static bool use_tiled_rendering(const GrGLCaps& glCaps,
1925 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1926 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1927 // Otherwise it's faster to just not use it.
1928 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1929 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1930}
1931
1932void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001933 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1934 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1935 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1936
1937 this->handleDirtyContext();
1938
1939 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1940 this->flushRenderTarget(glRT);
1941 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1942
Chris Daltonb50cc812019-10-14 16:29:21 -06001943 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1944 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1945 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1946 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1947 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1948 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1949 nativeBounds.fHeight, preserveMask));
1950 }
1951
Chris Dalton674f77a2019-09-30 20:49:39 -06001952 GrGLbitfield clearMask = 0;
1953 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1954 SkASSERT(!this->caps()->performColorClearsAsDraws());
1955 this->flushClearColor(colorLoadStore.fClearColor);
1956 this->flushColorWrite(true);
1957 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001958 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001959 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1960 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1961 GL_CALL(StencilMask(0xffffffff));
1962 GL_CALL(ClearStencil(0));
1963 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1964 }
1965 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001966 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001967 this->disableWindowRectangles();
1968 GL_CALL(Clear(clearMask));
1969 }
1970}
1971
1972void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1973 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1974 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1975 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1976
1977 this->handleDirtyContext();
1978
1979 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1980 // The framebuffer binding changed in the middle of a command buffer. We should have already
1981 // printed a warning during onFBOChanged.
1982 return;
1983 }
1984
1985 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1986 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1987
1988 SkSTArray<2, GrGLenum> discardAttachments;
1989 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1990 discardAttachments.push_back(
1991 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1992 }
1993 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1994 discardAttachments.push_back(
1995 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1996 }
1997
1998 if (!discardAttachments.empty()) {
1999 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2000 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2001 discardAttachments.begin()));
2002 } else {
2003 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2004 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2005 discardAttachments.begin()));
2006 }
2007 }
2008 }
2009
Chris Daltonb50cc812019-10-14 16:29:21 -06002010 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2011 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2012 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2013 // Handled by use_tiled_rendering().
2014 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2015 GL_CALL(EndTiling(preserveMask));
2016 }
2017
Chris Dalton674f77a2019-09-30 20:49:39 -06002018 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002019}
2020
Michael Ludwig1e632792020-05-21 12:45:31 -04002021void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002022 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002023 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002024 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002025 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002026 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002027
Greg Danielc0d69152020-10-08 14:59:00 -04002028 GrAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002029 if (!sb) {
2030 // We should only get here if we marked a proxy as requiring a SB. However,
2031 // the SB creation could later fail. Likely clipping is going to go awry now.
2032 return;
2033 }
2034
Greg Daniel8ade5e82020-10-07 13:09:48 -04002035 GrGLint stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat());
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002036#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002037 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002038 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002039#else
2040 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002041 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002042 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002043 // changing the clip between stencil passes may or may not
2044 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002045 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002046#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002047 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002048 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002049 value = (1 << (stencilBitCount - 1));
2050 } else {
2051 value = 0;
2052 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002053 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002054 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002055
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002056 this->flushScissor(scissor, glRT->height(), origin);
Michael Ludwig1e632792020-05-21 12:45:31 -04002057 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002058
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002059 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002060 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002061 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002062 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002063}
2064
Brian Salomone05ba5a2019-04-08 11:59:07 -04002065bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002066 GrColorType surfaceColorType, GrColorType dstColorType,
2067 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002068 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002069
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002070 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002071 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002072 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002073 return false;
2074 }
Greg Danielba88ab62019-07-26 09:14:01 -04002075 GrGLenum externalFormat = 0;
2076 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002077 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2078 surfaceColorType,
2079 dstColorType,
2080 &externalFormat,
2081 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002082 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002083 return false;
2084 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002085
Brian Salomon71d9d842016-11-03 13:42:00 -04002086 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002087 if (renderTarget->numSamples() <= 1 ||
2088 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2089 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2090 this->flushRenderTargetNoColorWrites(renderTarget);
2091 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2092 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2093 return false;
2094 } else {
2095 SkASSERT(renderTarget->requiresManualMSAAResolve());
2096 // we don't track the state of the READ FBO ID.
2097 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002098 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002099 } else {
2100 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002101 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002102 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002103 }
2104
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002105 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002106 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002107
Brian Salomona6948702018-06-01 15:33:20 -04002108 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002109 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002110 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002111 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002112 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002113 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002114
Brian Osman1348ed02018-09-12 09:08:39 -04002115 bool reattachStencil = false;
2116 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2117 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002118 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002119 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002120 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2121 reattachStencil = true;
2122 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2123 GR_GL_RENDERBUFFER, 0));
2124 }
2125
Chris Dalton76500e52019-09-05 02:13:05 -06002126 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002127 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002128
2129 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002130 auto* stencilAttachment =
Greg Danielc0d69152020-10-08 14:59:00 -04002131 static_cast<GrGLAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002132 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2133 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2134 }
2135
Brian Salomon26de56e2019-04-10 12:14:26 -04002136 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002137 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002138 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2139 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002140
Brian Salomone05ba5a2019-04-08 11:59:07 -04002141 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002142 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002143 }
2144 return true;
2145}
2146
2147bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002148 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2149 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002150 SkASSERT(surface);
2151
Brian Salomonb28cb682019-07-26 12:48:47 -04002152 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002153
Brian Salomon26de56e2019-04-10 12:14:26 -04002154 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2155 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002156
Brian Salomon1047a492019-07-02 12:25:21 -04002157 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002158 rowPixelWidth = width;
2159 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002160 SkASSERT(!(rowBytes % bytesPerPixel));
2161 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002162 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002163 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2164 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002165}
2166
Greg Daniel65476e02020-10-27 09:20:20 -04002167GrOpsRenderPass* GrGLGpu::onGetOpsRenderPass(
Greg Danielc0d69152020-10-08 14:59:00 -04002168 GrRenderTarget* rt,
2169 GrAttachment*,
2170 GrSurfaceOrigin origin,
2171 const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002172 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002173 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Greg Daniel9a18b082020-08-14 14:03:50 -04002174 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -04002175 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002176 if (!fCachedOpsRenderPass) {
John Stilesfbd050b2020-08-03 13:21:46 -04002177 fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002178 }
2179
Chris Daltonb50cc812019-10-14 16:29:21 -06002180 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002181 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002182}
2183
Brian Salomon1fabd512018-02-09 09:54:25 -05002184void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002185 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002186 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002187 this->didWriteToSurface(target, origin, &bounds);
2188}
bsalomon6ba6fa12015-03-04 11:57:37 -08002189
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002190void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2191 this->flushRenderTargetNoColorWrites(target);
2192 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002193}
2194
Brian Osman9aa30c62018-07-02 15:21:46 -04002195void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002196 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002197 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002198 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002199 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002200#ifdef SK_DEBUG
2201 // don't do this check in Chromium -- this is causing
2202 // lots of repeated command buffer flushes when the compositor is
2203 // rendering with Ganesh, which is really slow; even too slow for
2204 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002205 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002206 GrGLenum status;
2207 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2208 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002209 SkDebugf("GrGLGpu::flushRenderTargetNoColorWrites glCheckFramebufferStatus %x\n",
2210 status);
egdanield803f272015-03-18 13:01:52 -07002211 }
bsalomon160f24c2015-03-17 15:55:42 -07002212 }
egdanield803f272015-03-18 13:01:52 -07002213#endif
2214 fHWBoundRenderTargetUniqueID = rtID;
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002215 this->flushViewport(SkIRect::MakeSize(target->dimensions()),
2216 target->height(),
2217 kTopLeft_GrSurfaceOrigin); // the origin is irrelevant in this case
brianosman64d094d2016-03-25 06:01:59 -07002218 }
Brian Salomona2984312020-12-15 11:49:19 -05002219 if (this->caps()->workarounds().force_update_scissor_state_when_binding_fbo0) {
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002220 // The driver forgets the correct scissor state when using FBO 0.
2221 if (!fHWScissorSettings.fRect.isInvalid()) {
2222 const GrNativeRect& r = fHWScissorSettings.fRect;
2223 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
2224 }
2225 if (fHWScissorSettings.fEnabled == kYes_TriState) {
2226 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2227 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2228 } else if (fHWScissorSettings.fEnabled == kNo_TriState) {
2229 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2230 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2231 }
2232 }
brianosman64d094d2016-03-25 06:01:59 -07002233
brianosman35b784d2016-05-05 11:52:53 -07002234 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002235 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002236 }
Brian Salomon9b553272020-01-24 14:38:37 -05002237
2238 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2239 GrGLint format;
2240 GrGLint type;
2241 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2242 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2243 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2244 }
bsalomon083617b2016-02-12 12:10:14 -08002245}
bsalomona9909122016-01-23 10:41:40 -08002246
brianosman33f6b3f2016-06-02 05:49:21 -07002247void GrGLGpu::flushFramebufferSRGB(bool enable) {
2248 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2249 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2250 fHWSRGBFramebuffer = kYes_TriState;
2251 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2252 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2253 fHWSRGBFramebuffer = kNo_TriState;
2254 }
2255}
2256
Chris Dalton33db9fc2020-02-25 18:52:12 -07002257GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002258 fStats.incNumDraws();
2259
Chris Dalton2e7ed262020-02-21 15:17:59 -07002260 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2261 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2262 GL_CALL(Enable(GR_GL_CULL_FACE));
2263 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002264 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002265 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002266
Chris Dalton3809bab2017-06-13 10:55:06 -06002267 switch (primitiveType) {
2268 case GrPrimitiveType::kTriangles:
2269 return GR_GL_TRIANGLES;
2270 case GrPrimitiveType::kTriangleStrip:
2271 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002272 case GrPrimitiveType::kPoints:
2273 return GR_GL_POINTS;
2274 case GrPrimitiveType::kLines:
2275 return GR_GL_LINES;
2276 case GrPrimitiveType::kLineStrip:
2277 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002278 case GrPrimitiveType::kPatches:
2279 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002280 case GrPrimitiveType::kPath:
2281 SK_ABORT("non-mesh-based GrPrimitiveType");
2282 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002283 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002284 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002285}
2286
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002287void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002288 // Some extensions automatically resolves the texture when it is read.
2289 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2290
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002291 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002292 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2293 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2294 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2295 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002296
Chris Daltonc139d082019-09-26 14:04:24 -06002297 // make sure we go through flushRenderTarget() since we've modified
2298 // the bound DRAW FBO ID.
2299 fHWBoundRenderTargetUniqueID.makeInvalid();
2300 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2301 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002302 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2303 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002304 GrScissorState scissor(rt->dimensions());
2305 SkAssertResult(scissor.set(resolveRect));
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05002306 this->flushScissor(scissor, rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002307 this->disableWindowRectangles();
2308 GL_CALL(ResolveMultisampleFramebuffer());
2309 } else {
2310 int l, b, r, t;
2311 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2312 this->glCaps().blitFramebufferSupportFlags()) {
2313 l = 0;
2314 b = 0;
2315 r = target->width();
2316 t = target->height();
2317 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002318 l = resolveRect.x();
2319 b = resolveRect.y();
2320 r = resolveRect.x() + resolveRect.width();
2321 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002322 }
Chris Daltonc139d082019-09-26 14:04:24 -06002323
2324 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002325 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002326 this->disableWindowRectangles();
2327 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 +00002328 }
2329}
2330
bsalomon@google.com411dad02012-06-05 20:24:20 +00002331namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002332
bsalomon@google.com411dad02012-06-05 20:24:20 +00002333
2334GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002335 static const GrGLenum gTable[kGrStencilOpCount] = {
2336 GR_GL_KEEP, // kKeep
2337 GR_GL_ZERO, // kZero
2338 GR_GL_REPLACE, // kReplace
2339 GR_GL_INVERT, // kInvert
2340 GR_GL_INCR_WRAP, // kIncWrap
2341 GR_GL_DECR_WRAP, // kDecWrap
2342 GR_GL_INCR, // kIncClamp
2343 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002344 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002345 static_assert(0 == (int)GrStencilOp::kKeep);
2346 static_assert(1 == (int)GrStencilOp::kZero);
2347 static_assert(2 == (int)GrStencilOp::kReplace);
2348 static_assert(3 == (int)GrStencilOp::kInvert);
2349 static_assert(4 == (int)GrStencilOp::kIncWrap);
2350 static_assert(5 == (int)GrStencilOp::kDecWrap);
2351 static_assert(6 == (int)GrStencilOp::kIncClamp);
2352 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002353 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2354 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002355}
2356
2357void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002358 const GrStencilSettings::Face& face,
2359 GrGLenum glFace) {
2360 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2361 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2362 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002363
cdalton93a379b2016-05-11 13:58:08 -07002364 GrGLint ref = face.fRef;
2365 GrGLint mask = face.fTestMask;
2366 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002367
2368 if (GR_GL_FRONT_AND_BACK == glFace) {
2369 // we call the combined func just in case separate stencil is not
2370 // supported.
2371 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2372 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002373 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002374 } else {
2375 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2376 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002377 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002378 }
2379}
John Stilesa6841be2020-08-06 14:11:56 -04002380} // namespace
bsalomon@google.comd302f142011-03-03 13:54:13 +00002381
Chris Dalton71713f62019-04-18 12:41:03 -06002382void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002383 if (stencilSettings.isDisabled()) {
2384 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002385 } else if (fHWStencilSettings != stencilSettings ||
2386 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002387 if (kYes_TriState != fHWStencilTestEnabled) {
2388 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002389
csmartdaltonc7d85332016-10-26 10:13:46 -07002390 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002391 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002392 if (!stencilSettings.isTwoSided()) {
2393 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2394 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002395 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002396 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2397 GR_GL_FRONT);
2398 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2399 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002400 }
joshualitta58fe352014-10-27 08:39:00 -07002401 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002402 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002403 }
2404}
2405
csmartdaltonc7d85332016-10-26 10:13:46 -07002406void GrGLGpu::disableStencil() {
2407 if (kNo_TriState != fHWStencilTestEnabled) {
2408 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002409
csmartdaltonc7d85332016-10-26 10:13:46 -07002410 fHWStencilTestEnabled = kNo_TriState;
2411 fHWStencilSettings.invalidate();
2412 }
2413}
2414
Chris Dalton4c56b032019-03-04 12:28:42 -07002415void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002416 // rt is only optional if useHWAA is false.
2417 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002418#ifdef SK_DEBUG
2419 if (useHWAA && rt->numSamples() <= 1) {
2420 SkASSERT(this->caps()->mixedSamplesSupport());
2421 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002422 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002423 }
2424#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002425
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002426 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002427 if (useHWAA) {
2428 if (kYes_TriState != fMSAAEnabled) {
2429 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2430 fMSAAEnabled = kYes_TriState;
2431 }
2432 } else {
2433 if (kNo_TriState != fMSAAEnabled) {
2434 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2435 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002436 }
2437 }
2438 }
2439}
2440
Chris Daltonce425af2019-12-16 10:39:03 -07002441void GrGLGpu::flushConservativeRasterState(bool enabled) {
2442 if (this->caps()->conservativeRasterSupport()) {
2443 if (enabled) {
2444 if (kYes_TriState != fHWConservativeRasterEnabled) {
2445 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2446 fHWConservativeRasterEnabled = kYes_TriState;
2447 }
2448 } else {
2449 if (kNo_TriState != fHWConservativeRasterEnabled) {
2450 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2451 fHWConservativeRasterEnabled = kNo_TriState;
2452 }
2453 }
2454 }
2455}
2456
Chris Dalton1215cda2019-12-17 21:44:04 -07002457void GrGLGpu::flushWireframeState(bool enabled) {
2458 if (this->caps()->wireframeSupport()) {
2459 if (this->caps()->wireframeMode() || enabled) {
2460 if (kYes_TriState != fHWWireframeEnabled) {
2461 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2462 fHWWireframeEnabled = kYes_TriState;
2463 }
2464 } else {
2465 if (kNo_TriState != fHWWireframeEnabled) {
2466 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2467 fHWWireframeEnabled = kNo_TriState;
2468 }
2469 }
2470 }
2471}
2472
Chris Daltonbbb3f642019-07-24 12:25:08 -04002473void GrGLGpu::flushBlendAndColorWrite(
2474 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2475 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2476 // We need to work around a driver bug by using a blend state that preserves the dst color,
2477 // rather than disabling color writes.
2478 GrXferProcessor::BlendInfo preserveDstBlend;
2479 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2480 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2481 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2482 return;
2483 }
bsalomonf7cc8772015-05-11 11:21:14 -07002484
cdalton8917d622015-05-06 13:40:21 -07002485 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002486 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2487 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002488
2489 // Any optimization to disable blending should have already been applied and
2490 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002491 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2492 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002493
egdanielb414f252014-07-29 13:15:47 -07002494 if (blendOff) {
2495 if (kNo_TriState != fHWBlendState.fEnabled) {
2496 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002497
Jim Van Verth1bef9792020-07-09 08:09:13 -04002498 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002499 // https://code.google.com/p/skia/issues/detail?id=3943
2500 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2501 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2502 SkASSERT(this->caps()->advancedBlendEquationSupport());
2503 // Set to any basic blending equation.
2504 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2505 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2506 fHWBlendState.fEquation = blend_equation;
2507 }
2508
egdanielb414f252014-07-29 13:15:47 -07002509 fHWBlendState.fEnabled = kNo_TriState;
2510 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002511 } else {
2512 if (kYes_TriState != fHWBlendState.fEnabled) {
2513 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002514
Chris Daltonbbb3f642019-07-24 12:25:08 -04002515 fHWBlendState.fEnabled = kYes_TriState;
2516 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002517
Chris Daltonbbb3f642019-07-24 12:25:08 -04002518 if (fHWBlendState.fEquation != equation) {
2519 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2520 fHWBlendState.fEquation = equation;
2521 }
cdalton8917d622015-05-06 13:40:21 -07002522
Chris Daltonbbb3f642019-07-24 12:25:08 -04002523 if (GrBlendEquationIsAdvanced(equation)) {
2524 SkASSERT(this->caps()->advancedBlendEquationSupport());
2525 // Advanced equations have no other blend state.
2526 return;
2527 }
cdalton8917d622015-05-06 13:40:21 -07002528
Chris Daltonbbb3f642019-07-24 12:25:08 -04002529 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2530 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2531 gXfermodeCoeff2Blend[dstCoeff]));
2532 fHWBlendState.fSrcCoeff = srcCoeff;
2533 fHWBlendState.fDstCoeff = dstCoeff;
2534 }
cdalton8917d622015-05-06 13:40:21 -07002535
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002536 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002537 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2538 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2539 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2540 fHWBlendState.fConstColor = blendConst;
2541 fHWBlendState.fConstColorValid = true;
2542 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002543 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002544 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002545
2546 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002547}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002548
Greg Daniel2c3398d2019-06-19 11:58:01 -04002549void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2550 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002551 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002552
reed856e9d92015-09-30 12:21:45 -07002553#ifdef SK_DEBUG
2554 if (!this->caps()->npotTextureTileSupport()) {
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002555 if (samplerState.isRepeatedX()) {
reed856e9d92015-09-30 12:21:45 -07002556 const int w = texture->width();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002557 SkASSERT(SkIsPow2(w));
2558 }
2559 if (samplerState.isRepeatedY()) {
reed856e9d92015-09-30 12:21:45 -07002560 const int h = texture->height();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002561 SkASSERT(SkIsPow2(h));
reed856e9d92015-09-30 12:21:45 -07002562 }
2563 }
2564#endif
2565
Robert Phillips294870f2016-11-11 12:38:40 -05002566 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002567 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002568 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002569 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002570 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002571 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002572 }
2573
Brian Salomone69b9ef2020-07-22 11:18:06 -04002574 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002575 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002576 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2577 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002578 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002579 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002580 }
bsalomonefd7d452014-10-23 14:17:46 -07002581
Brian Salomone2826ab2019-06-04 15:58:31 -04002582 auto timestamp = texture->parameters()->resetTimestamp();
2583 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002584 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2585 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomona0d913b2020-09-01 12:42:26 -04002586 if (this->glCaps().useSamplerObjects()) {
Brian Salomondc829942018-10-23 16:07:24 -04002587 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002588 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002589 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2590 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2591 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002592 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2593 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002594 this->setTextureUnit(unitIdx);
2595 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2596 newSamplerState = oldSamplerState;
2597 newSamplerState.fMinFilter = minFilter;
2598 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002599 }
2600 }
Brian Salomondc829942018-10-23 16:07:24 -04002601 } else {
Brian Salomona0d913b2020-09-01 12:42:26 -04002602 if (fSamplerObjectCache) {
2603 fSamplerObjectCache->unbindSampler(unitIdx);
2604 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002605 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2606 texture->parameters()->samplerOverriddenState();
2607 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002608
Brian Salomone69b9ef2020-07-22 11:18:06 -04002609 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2610 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002611 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002612
Brian Salomone2826ab2019-06-04 15:58:31 -04002613 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2614 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002615
2616 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002617 newSamplerState.fMinLOD = -1000.f;
2618 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002619
Brian Salomone2826ab2019-06-04 15:58:31 -04002620 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002621 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002622 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002623 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002624 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002625 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002626 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002627 }
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002628 if (this->glCaps().mipmapLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002629 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002630 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002631 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002632 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002633 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002634 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002635 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002636 }
2637 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002638 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002639 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002640 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002641 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002642 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002643 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002644 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002645 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002646 if (this->glCaps().clampToBorderSupport()) {
2647 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002649 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002650 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2651 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002652 }
2653 }
Brian Salomondc829942018-10-23 16:07:24 -04002654 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002655 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2656 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002657 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002658 newNonsamplerState.fSwizzleIsRGBA = true;
Brian Salomondc829942018-10-23 16:07:24 -04002659
Brian Salomone2826ab2019-06-04 15:58:31 -04002660 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2661 texture->parameters()->nonsamplerState();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002662 if (this->glCaps().textureSwizzleSupport()) {
2663 if (setAll || !oldNonsamplerState.fSwizzleIsRGBA) {
2664 static constexpr GrGLenum kRGBA[4] {
2665 GR_GL_RED,
2666 GR_GL_GREEN,
2667 GR_GL_BLUE,
2668 GR_GL_ALPHA
2669 };
Brian Salomondc829942018-10-23 16:07:24 -04002670 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002671 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon280fa3d2020-08-27 17:16:49 -04002672 static_assert(sizeof(kRGBA[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002673 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
Brian Salomon280fa3d2020-08-27 17:16:49 -04002674 reinterpret_cast<const GrGLint*>(kRGBA)));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002675 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002676 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
Brian Salomon280fa3d2020-08-27 17:16:49 -04002677 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, kRGBA[0]));
2678 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, kRGBA[1]));
2679 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, kRGBA[2]));
2680 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, kRGBA[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002681 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002682 }
2683 }
Brian Salomondc829942018-10-23 16:07:24 -04002684 // These are not supported in ES2 contexts
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002685 if (this->glCaps().mipmapLevelControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002686 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002687 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002688 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002689 this->setTextureUnit(unitIdx);
2690 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002691 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002692 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002693 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002694 this->setTextureUnit(unitIdx);
2695 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002696 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002697 }
Brian Salomon327955b2018-10-22 15:39:22 +00002698 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002699 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2700 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002701}
2702
Brian Salomon1f05d452019-02-08 12:33:08 -05002703void GrGLGpu::onResetTextureBindings() {
2704 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2705 GR_GL_TEXTURE_EXTERNAL};
2706 for (int i = 0; i < this->numTextureUnits(); ++i) {
2707 this->setTextureUnit(i);
2708 for (auto target : kTargets) {
2709 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2710 GL_CALL(BindTexture(target, 0));
2711 }
2712 }
2713 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2714 }
2715}
2716
Chris Dalton5a2f9622019-12-27 14:56:38 -07002717void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2718 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2719 if (fHWPatchVertexCount != count) {
2720 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2721 fHWPatchVertexCount = count;
2722 }
2723}
2724
egdaniel080e6732014-12-22 07:35:52 -08002725void GrGLGpu::flushColorWrite(bool writeColor) {
2726 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002727 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002728 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2729 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002730 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002731 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002732 } else {
2733 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002734 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002735 fHWWriteToColor = kYes_TriState;
2736 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002737 }
bsalomon3e791242014-12-17 13:43:13 -08002738}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002739
Brian Salomon07bc9a22020-12-02 13:37:16 -05002740void GrGLGpu::flushClearColor(std::array<float, 4> color) {
2741 GrGLfloat r = color[0], g = color[1], b = color[2], a = color[3];
Chris Dalton674f77a2019-09-30 20:49:39 -06002742 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2743 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2744 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2745 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2746 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2747 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002748 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2749 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2750 GL_CALL(ClearColor(r, g, b, a));
2751 fHWClearColor[0] = r;
2752 fHWClearColor[1] = g;
2753 fHWClearColor[2] = b;
2754 fHWClearColor[3] = a;
2755 }
2756}
2757
bsalomon861e1032014-12-16 07:33:49 -08002758void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002759 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002760 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002761 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002762 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002763 }
2764}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002765
Brian Salomond978b902019-02-07 15:09:18 -05002766void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002767 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002768 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002769 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2770 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2771 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002772 }
Brian Salomond978b902019-02-07 15:09:18 -05002773 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2774 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002775 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002776 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002777}
2778
Brian Salomone5e7eb12016-10-14 16:18:33 -04002779// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002780static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2781 const GrSurface* src,
2782 const SkIRect& srcRect,
2783 const SkIPoint& dstPoint,
2784 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002785 int dstSampleCnt = 0;
2786 int srcSampleCnt = 0;
2787 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002788 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002789 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002790 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002791 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002792 }
2793 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2794 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2795
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002796 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2797 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2798
Brian Salomone5e7eb12016-10-14 16:18:33 -04002799 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002800 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002801
Greg Daniel46cfbc62019-06-07 11:43:30 -04002802 GrTextureType dstTexType;
2803 GrTextureType* dstTexTypePtr = nullptr;
2804 GrTextureType srcTexType;
2805 GrTextureType* srcTexTypePtr = nullptr;
2806 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002807 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002808 dstTexTypePtr = &dstTexType;
2809 }
2810 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002811 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002812 srcTexTypePtr = &srcTexType;
2813 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002814
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002815 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2816 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002817 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002818}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002819
Brian Osmana9c8a052018-01-19 10:31:56 -05002820static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2821 // A RT has a separate MSAA renderbuffer if:
2822 // 1) It's multisampled
2823 // 2) We're using an extension with separate MSAA renderbuffers
2824 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002825 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002826}
2827
Greg Daniel46cfbc62019-06-07 11:43:30 -04002828static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002829 const GrGLCaps& caps) {
2830
bsalomon@google.comeb851172013-04-15 13:51:00 +00002831 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002832 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002833 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002834 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002835
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002836 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2837 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2838
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002839 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2840 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2841
Greg Daniel46cfbc62019-06-07 11:43:30 -04002842 GrTextureType dstTexType;
2843 GrTextureType* dstTexTypePtr = nullptr;
2844 GrTextureType srcTexType;
2845 GrTextureType* srcTexTypePtr = nullptr;
2846 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002847 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002848 dstTexTypePtr = &dstTexType;
2849 }
2850 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002851 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002852 srcTexTypePtr = &srcTexType;
2853 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002854
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002855 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2856 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002857}
2858
Greg Danielacd66b42019-05-22 16:29:12 -04002859// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002860void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002861 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002862 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002863 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002864 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002865 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2866 GrGLuint texID = texture->textureID();
2867 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002868 GrGLuint* tempFBOID;
2869 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002870
egdanield803f272015-03-18 13:01:52 -07002871 if (0 == *tempFBOID) {
2872 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002873 }
2874
Adrienne Walker4ee88512018-05-17 11:37:14 -07002875 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002876 GR_GL_CALL(
2877 this->glInterface(),
2878 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2879 if (mipLevel == 0) {
2880 texture->baseLevelWasBoundToFBO();
2881 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002882 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002883 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002884 }
egdaniel0f5f9672015-02-03 11:10:51 -08002885}
2886
Brian Salomond2a8ae22019-09-10 16:03:59 -04002887void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002888 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002889 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002890 SkASSERT(surface->asTexture());
2891 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2892 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2893 GR_GL_COLOR_ATTACHMENT0,
2894 textureTarget,
2895 0,
2896 0));
2897 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002898}
2899
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002900void GrGLGpu::onFBOChanged() {
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002901 if (this->caps()->workarounds().flush_on_framebuffer_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002902 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002903 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002904#ifdef SK_DEBUG
2905 if (fIsExecutingCommandBuffer_DebugOnly) {
2906 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2907 "This will severely hurt performance.\n");
2908 }
2909#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002910}
2911
Adrienne Walker4ee88512018-05-17 11:37:14 -07002912void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2913 fStats.incRenderTargetBinds();
2914 GL_CALL(BindFramebuffer(target, fboid));
2915 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2916 fBoundDrawFramebuffer = fboid;
2917 }
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002918 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002919}
2920
Adrienne Walker4ee88512018-05-17 11:37:14 -07002921void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002922 // We're relying on the GL state shadowing being correct in the workaround code below so we
2923 // need to handle a dirty context.
2924 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002925 if (fboid == fBoundDrawFramebuffer &&
2926 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2927 // This workaround only applies to deleting currently bound framebuffers
2928 // on Adreno 420. Because this is a somewhat rare case, instead of
2929 // tracking all the attachments of every framebuffer instead just always
2930 // unbind all attachments.
2931 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2932 GR_GL_RENDERBUFFER, 0));
2933 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2934 GR_GL_RENDERBUFFER, 0));
2935 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2936 GR_GL_RENDERBUFFER, 0));
2937 }
2938
2939 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002940
2941 // Deleting the currently bound framebuffer rebinds to 0.
2942 if (fboid == fBoundDrawFramebuffer) {
2943 this->onFBOChanged();
2944 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002945}
2946
Greg Daniel46cfbc62019-06-07 11:43:30 -04002947bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002948 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002949 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2950 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2951 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002952 auto dstFormat = dst->backendFormat().asGLFormat();
2953 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002954 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002955 return true;
2956 }
2957 }
cblume61214052016-01-26 09:10:48 -08002958
Greg Daniel46cfbc62019-06-07 11:43:30 -04002959 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2960 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002961 return true;
2962 }
2963
Greg Daniel46cfbc62019-06-07 11:43:30 -04002964 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2965 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002966 }
2967
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002968 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002969 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002970 return true;
2971 }
bsalomon083617b2016-02-12 12:10:14 -08002972 }
2973
bsalomon6df86402015-06-01 10:41:49 -07002974 return false;
2975}
2976
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002977bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002978 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002979
2980 int progIdx = TextureToCopyProgramIdx(srcTex);
2981 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002982 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002983
2984 if (!fCopyProgramArrayBuffer) {
2985 static const GrGLfloat vdata[] = {
2986 0, 0,
2987 0, 1,
2988 1, 0,
2989 1, 1
2990 };
2991 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2992 kStatic_GrAccessPattern, vdata);
2993 }
2994 if (!fCopyProgramArrayBuffer) {
2995 return false;
2996 }
2997
2998 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2999 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3000 if (!fCopyPrograms[progIdx].fProgram) {
3001 return false;
3002 }
3003
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003004 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003005 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003006 GrShaderVar::TypeModifier::Uniform);
3007 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3008 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3009 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3010 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003011
Greg Daniel9e3169b2019-06-06 14:38:17 -04003012 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003013 if (shaderCaps->noperspectiveInterpolationSupport()) {
3014 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3015 vshaderTxt.appendf("#extension %s : require\n", extension);
3016 }
3017 vTexCoord.addModifier("noperspective");
3018 }
3019
3020 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3021 vshaderTxt.append(";");
3022 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3023 vshaderTxt.append(";");
3024 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3025 vshaderTxt.append(";");
3026 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3027 vshaderTxt.append(";");
3028
3029 vshaderTxt.append(
3030 "// Copy Program VS\n"
3031 "void main() {"
3032 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3033 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3034 " sk_Position.zw = half2(0, 1);"
3035 "}"
3036 );
3037
Greg Daniel9e3169b2019-06-06 14:38:17 -04003038 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003039 if (shaderCaps->noperspectiveInterpolationSupport()) {
3040 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3041 fshaderTxt.appendf("#extension %s : require\n", extension);
3042 }
3043 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003044 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003045 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3046 fshaderTxt.append(";");
3047 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3048 fshaderTxt.append(";");
3049 fshaderTxt.appendf(
3050 "// Copy Program FS\n"
3051 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003052 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003053 "}"
3054 );
3055
3056 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3057 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3058 SkSL::Program::Settings settings;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003059 SkSL::String glsl;
Brian Osman79719262020-11-23 14:54:33 -05003060 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::Program::kVertex_Kind,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003061 sksl, settings, &glsl, errorHandler);
3062 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3063 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3064 SkASSERT(program->fInputs.isEmpty());
3065
3066 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman79719262020-11-23 14:54:33 -05003067 program = GrSkSLtoGLSL(this, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003068 errorHandler);
3069 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3070 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3071 errorHandler);
3072 SkASSERT(program->fInputs.isEmpty());
3073
3074 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3075
3076 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3077 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3078 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3079 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3080 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3081 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3082
3083 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3084
3085 GL_CALL(DeleteShader(vshader));
3086 GL_CALL(DeleteShader(fshader));
3087
3088 return true;
3089}
3090
brianosman33f6b3f2016-06-02 05:49:21 -07003091bool GrGLGpu::createMipmapProgram(int progIdx) {
3092 const bool oddWidth = SkToBool(progIdx & 0x2);
3093 const bool oddHeight = SkToBool(progIdx & 0x1);
3094 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3095
Brian Salomon1edc5b92016-11-29 13:43:46 -05003096 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003097
3098 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3099 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3100 if (!fMipmapPrograms[progIdx].fProgram) {
3101 return false;
3102 }
3103
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003104 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003105 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003106 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003107 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003108 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003109 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003110 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003111 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3112 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3113 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3114 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003115 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003116 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003117
Ethan Nicholasfc994162019-06-06 10:04:27 -04003118 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003119 if (shaderCaps->noperspectiveInterpolationSupport()) {
3120 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003121 vshaderTxt.appendf("#extension %s : require\n", extension);
3122 }
3123 vTexCoords[0].addModifier("noperspective");
3124 vTexCoords[1].addModifier("noperspective");
3125 vTexCoords[2].addModifier("noperspective");
3126 vTexCoords[3].addModifier("noperspective");
3127 }
3128
Brian Salomon1edc5b92016-11-29 13:43:46 -05003129 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003130 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003131 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003132 vshaderTxt.append(";");
3133 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003134 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003135 vshaderTxt.append(";");
3136 }
3137
3138 vshaderTxt.append(
3139 "// Mipmap Program VS\n"
3140 "void main() {"
John Stiles15b48922020-11-25 10:34:59 -05003141 " sk_Position.xy = a_vertex * half2(2) - half2(1);"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003142 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003143 );
3144
3145 // Insert texture coordinate computation:
3146 if (oddWidth && oddHeight) {
3147 vshaderTxt.append(
3148 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003149 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3150 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003151 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3152 );
3153 } else if (oddWidth) {
3154 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003155 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3156 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003157 );
3158 } else if (oddHeight) {
3159 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003160 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3161 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003162 );
3163 } else {
3164 vshaderTxt.append(
3165 " v_texCoord0 = a_vertex.xy;"
3166 );
3167 }
3168
3169 vshaderTxt.append("}");
3170
Ethan Nicholasfc994162019-06-06 10:04:27 -04003171 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003172 if (shaderCaps->noperspectiveInterpolationSupport()) {
3173 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003174 fshaderTxt.appendf("#extension %s : require\n", extension);
3175 }
3176 }
brianosman33f6b3f2016-06-02 05:49:21 -07003177 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003178 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003179 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003180 fshaderTxt.append(";");
3181 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003182 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003183 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003184 fshaderTxt.append(
3185 "// Mipmap Program FS\n"
3186 "void main() {"
3187 );
3188
3189 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003190 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003191 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3192 " sample(u_texture, v_texCoord1) + "
3193 " sample(u_texture, v_texCoord2) + "
3194 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003195 );
3196 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003197 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003198 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3199 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003200 );
3201 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003202 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003203 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003204 );
3205 }
3206
3207 fshaderTxt.append("}");
3208
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003209 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003210 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003211 SkSL::Program::Settings settings;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003212 SkSL::String glsl;
Brian Osman79719262020-11-23 14:54:33 -05003213 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003214 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003215 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003216 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003217 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003218
Brian Osman6c431d52019-04-15 16:31:54 -04003219 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman79719262020-11-23 14:54:33 -05003220 program = GrSkSLtoGLSL(this, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003221 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003222 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003223 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003224 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003225 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003226
3227 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3228
3229 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3230 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3231 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3232 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3233
3234 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3235
3236 GL_CALL(DeleteShader(vshader));
3237 GL_CALL(DeleteShader(fshader));
3238
3239 return true;
3240}
3241
Greg Daniel46cfbc62019-06-07 11:43:30 -04003242bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003243 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003244 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3245 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3246 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3247 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003248 return false;
3249 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003250 int progIdx = TextureToCopyProgramIdx(srcTex);
3251 if (!dstRT) {
3252 SkASSERT(dstTex);
3253 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3254 return false;
3255 }
3256 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003257 if (!fCopyPrograms[progIdx].fProgram) {
3258 if (!this->createCopyProgram(srcTex)) {
3259 SkDebugf("Failed to create copy program.\n");
3260 return false;
3261 }
3262 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003263 int w = srcRect.width();
3264 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003265 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003266 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003267 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05003268 this->flushViewport(SkIRect::MakeSize(dst->dimensions()),
3269 dst->height(),
3270 kTopLeft_GrSurfaceOrigin); // the origin is irrelevant in this case
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003271 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003272 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003273 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003274 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003275 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3276 attribs->enableVertexArrays(this, 1);
3277 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3278 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003279 // dst rect edges in NDC (-1 to 1)
3280 int dw = dst->width();
3281 int dh = dst->height();
3282 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3283 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3284 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3285 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003286 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3287 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3288 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3289 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3290 int sw = src->width();
3291 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003292 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003293 // src rect edges in normalized texture space (0 to 1)
3294 sx0 /= sw;
3295 sx1 /= sw;
3296 sy0 /= sh;
3297 sy1 /= sh;
3298 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003299 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3300 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3301 sx1 - sx0, sy1 - sy0, sx0, sy0));
3302 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003303 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003304 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003305 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003306 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003307 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003308 this->disableWindowRectangles();
3309 this->disableStencil();
3310 if (this->glCaps().srgbWriteControl()) {
3311 this->flushFramebufferSRGB(true);
3312 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003313 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003314 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003315 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3316 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003317 return true;
3318}
3319
Greg Daniel46cfbc62019-06-07 11:43:30 -04003320void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003321 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003322 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003323 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003324 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003325 SkASSERT(dstTex);
3326 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003327 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003328
Brian Salomond978b902019-02-07 15:09:18 -05003329 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003330 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003331 dstPoint.fX, dstPoint.fY,
3332 srcRect.fLeft, srcRect.fTop,
3333 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003334 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003335 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3336 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003337 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3338 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003339}
3340
Greg Daniel46cfbc62019-06-07 11:43:30 -04003341bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003342 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003343 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003344 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3345 srcRect.width(), srcRect.height());
3346 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003347 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003348 return false;
mtklein404b3b22015-05-18 09:29:10 -07003349 }
bsalomon5df6fee2015-05-18 06:26:15 -07003350 }
bsalomon6df86402015-06-01 10:41:49 -07003351
Brian Salomond2a8ae22019-09-10 16:03:59 -04003352 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3353 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003354 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003355 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003356
3357 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003358 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003359 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003360
Greg Daniel46cfbc62019-06-07 11:43:30 -04003361 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3362 srcRect.fTop,
3363 srcRect.fRight,
3364 srcRect.fBottom,
3365 dstRect.fLeft,
3366 dstRect.fTop,
3367 dstRect.fRight,
3368 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003369 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003370 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3371 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003372
3373 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3374 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003375 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003376}
3377
Brian Salomon930f9392018-06-20 16:25:26 -04003378bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3379 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003380 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003381 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003382 return false;
3383 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003384 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003385 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3386 // Uses draw calls to do a series of downsample operations to successive mips.
3387
3388 // The manual approach requires the ability to limit which level we're sampling and that the
3389 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003390 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003391 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003392 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003393 GL_CALL(GenerateMipmap(glTex->target()));
3394 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003395 }
3396
brianosman33f6b3f2016-06-02 05:49:21 -07003397 int width = texture->width();
3398 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003399 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003400 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003401
3402 // Create (if necessary), then bind temporary FBO:
3403 if (0 == fTempDstFBOID) {
3404 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3405 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003406 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003407 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003408
3409 // Bind the texture, to get things configured for filtering.
3410 // We'll be changing our base level further below:
3411 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003412 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003413 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003414
3415 // Vertex data:
3416 if (!fMipmapProgramArrayBuffer) {
3417 static const GrGLfloat vdata[] = {
3418 0, 0,
3419 0, 1,
3420 1, 0,
3421 1, 1
3422 };
Brian Salomonae64c192019-02-05 09:41:37 -05003423 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003424 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003425 }
3426 if (!fMipmapProgramArrayBuffer) {
3427 return false;
3428 }
3429
3430 fHWVertexArrayState.setVertexArrayID(this, 0);
3431
3432 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003433 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003434 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003435 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003436
3437 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003438 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003439 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003440 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003441 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003442 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003443
3444 // Do all the blits:
3445 width = texture->width();
3446 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003447
brianosman33f6b3f2016-06-02 05:49:21 -07003448 for (GrGLint level = 1; level < levelCount; ++level) {
3449 // Get and bind the program for this particular downsample (filter shape can vary):
3450 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3451 if (!fMipmapPrograms[progIdx].fProgram) {
3452 if (!this->createMipmapProgram(progIdx)) {
3453 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003454 // Invalidate all params to cover base level change in a previous iteration.
3455 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003456 return false;
3457 }
3458 }
Brian Salomon802cb312018-06-08 18:05:20 -04003459 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003460
3461 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3462 const float invWidth = 1.0f / width;
3463 const float invHeight = 1.0f / height;
3464 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3465 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3466 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3467
3468 // Only sample from previous mip
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003469 SkASSERT(this->glCaps().mipmapLevelControlSupport());
brianosman33f6b3f2016-06-02 05:49:21 -07003470 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3471
Brian Salomon930f9392018-06-20 16:25:26 -04003472 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3473 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003474
Brian Osman788b9162020-02-07 10:36:46 -05003475 width = std::max(1, width / 2);
3476 height = std::max(1, height / 2);
Robert Phillipsedc5b4d2021-01-06 13:06:23 -05003477 this->flushViewport(SkIRect::MakeWH(width, height), height, kTopLeft_GrSurfaceOrigin);
brianosman33f6b3f2016-06-02 05:49:21 -07003478
3479 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3480 }
3481
3482 // Unbind:
3483 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3484 GR_GL_TEXTURE_2D, 0, 0));
3485
Brian Salomon930f9392018-06-20 16:25:26 -04003486 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003487 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3488 // We drew the 2nd to last level into the last level.
3489 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3490 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003491
brianosman33f6b3f2016-06-02 05:49:21 -07003492 return true;
3493}
3494
Chris Daltond7291ba2019-03-07 14:17:03 -07003495void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003496 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3497 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003498
3499 int effectiveSampleCnt;
3500 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003501 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003502
3503 sampleLocations->reset(effectiveSampleCnt);
3504 for (int i = 0; i < effectiveSampleCnt; ++i) {
3505 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3506 }
3507}
3508
cdalton231c5fd2015-05-13 12:35:36 -07003509void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003510 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003511 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003512 case kTexture_GrXferBarrierType: {
3513 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003514 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003515 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3516 // The render target uses separate storage so no need for glTextureBarrier.
3517 // FIXME: The render target will resolve automatically when its texture is bound,
3518 // but we could resolve only the bounds that will be read if we do it here instead.
3519 return;
3520 }
cdalton9954bc32015-04-29 14:17:00 -07003521 SkASSERT(this->caps()->textureBarrierSupport());
3522 GL_CALL(TextureBarrier());
3523 return;
cdalton231c5fd2015-05-13 12:35:36 -07003524 }
cdalton8917d622015-05-06 13:40:21 -07003525 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003526 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003527 this->caps()->blendEquationSupport());
3528 GL_CALL(BlendBarrier());
3529 return;
bsalomoncb02b382015-08-12 11:14:50 -07003530 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003531 }
3532}
3533
Chris Daltone1196c52019-12-28 14:31:09 -07003534void GrGLGpu::insertManualFramebufferBarrier() {
3535 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3536 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3537}
3538
Brian Salomon85c3d682019-11-04 15:04:54 -05003539GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003540 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003541 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003542 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003543 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003544 // We don't support protected textures in GL.
3545 if (isProtected == GrProtected::kYes) {
3546 return {};
3547 }
3548
Brian Salomon8a375832018-03-14 10:21:40 -04003549 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003550
Brian Salomond4764a12019-08-08 12:08:24 -04003551 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003552 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003553 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003554 }
3555
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003556 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003557 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003558 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003559 }
3560
Robert Phillipsd34691b2019-09-24 13:38:43 -04003561 // Compressed formats go through onCreateCompressedBackendTexture
3562 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3563
Greg Daniel15500642019-06-27 03:05:55 +00003564 GrGLTextureInfo info;
3565 GrGLTextureParameters::SamplerOverriddenState initialState;
3566
Greg Danield51fa2f2020-01-22 16:53:38 -05003567 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003568 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003569 }
Brian Salomon0f396992020-06-19 19:51:21 -04003570 switch (format.textureType()) {
3571 case GrTextureType::kNone:
3572 case GrTextureType::kExternal:
3573 return {};
3574 case GrTextureType::k2D:
3575 info.fTarget = GR_GL_TEXTURE_2D;
3576 break;
3577 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003578 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003579 return {};
3580 }
3581 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3582 break;
3583 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003584 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003585 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3586 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003587 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003588 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003589 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003590
Brian Salomon85c3d682019-11-04 15:04:54 -05003591 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003592 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003593
Brian Salomone2826ab2019-06-04 15:58:31 -04003594 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003595 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003596 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3597 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003598
Brian Salomon85c3d682019-11-04 15:04:54 -05003599 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3600 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003601}
3602
Greg Daniel16032b32020-05-06 15:31:10 -04003603bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3604 sk_sp<GrRefCntedCallback> finishedCallback,
3605 const BackendTextureData* data) {
3606 GrGLTextureInfo info;
3607 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3608
3609 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003610 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003611 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003612 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003613 }
3614
3615 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3616
Brian Salomon22558932020-05-15 14:46:34 -04003617 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003618
Brian Salomonc3f49432020-06-03 10:21:50 -04003619 // 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 -04003620 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003621 if (numMipLevels && this->glCaps().mipmapLevelControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003622 auto params = backendTexture.getGLTextureParams();
3623 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3624 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003625 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003626 nonsamplerState.fBaseMipMapLevel = 0;
3627 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003628 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003629 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003630 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3631 }
3632 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3633 }
3634
Greg Daniel16032b32020-05-06 15:31:10 -04003635 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003636 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003637 if (data->type() == BackendTextureData::Type::kPixmaps) {
3638 SkTDArray<GrMipLevel> texels;
Brian Salomon05487ab2020-12-23 20:32:22 -05003639 GrColorType colorType = data->pixmap(0).colorType();
Greg Daniel16032b32020-05-06 15:31:10 -04003640 texels.append(numMipLevels);
3641 for (int i = 0; i < numMipLevels; ++i) {
3642 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3643 }
Brian Salomon22558932020-05-15 14:46:34 -04003644 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3645 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3646 info.fTarget, dstRect, colorType, texels.begin(),
3647 texels.count());
3648 } else if (data->type() == BackendTextureData::Type::kColor) {
3649 uint32_t levelMask = (1 << numMipLevels) - 1;
3650 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3651 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003652 }
3653
3654 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003655 this->bindTextureToScratchUnit(info.fTarget, 0);
3656 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003657}
3658
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003659void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003660 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3661
3662 GrGLTextureInfo info;
3663 if (tex.getGLTextureInfo(&info)) {
3664 GL_CALL(DeleteTextures(1, &info.fID));
3665 }
3666}
3667
Robert Phillips3bce1f72020-05-12 12:41:58 -04003668bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3669 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3670
3671 Stats::ProgramCacheResult stat;
3672
3673 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3674 if (!tmp) {
3675 return false;
3676 }
3677
3678 return stat != Stats::ProgramCacheResult::kHit;
3679}
3680
Robert Phillipsf0ced622019-05-16 09:06:25 -04003681#if GR_TEST_UTILS
3682
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003683bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003684 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003685
Greg Daniel52e16d92018-04-10 09:34:07 -04003686 GrGLTextureInfo info;
3687 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003688 return false;
3689 }
3690
3691 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003692 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003693
3694 return (GR_GL_TRUE == result);
3695}
3696
Brian Salomon72c7b982020-10-06 10:07:38 -04003697GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
3698 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04003699 int sampleCnt,
3700 GrProtected isProtected) {
Brian Salomon72c7b982020-10-06 10:07:38 -04003701 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
3702 dimensions.height() > this->caps()->maxRenderTargetSize()) {
3703 return {};
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003704 }
Brian Salomonf9b00422020-10-08 16:00:14 -04003705 if (isProtected == GrProtected::kYes) {
3706 return {};
3707 }
3708
Brian Salomon8a375832018-03-14 10:21:40 -04003709 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003710 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -04003711 sampleCnt = this->glCaps().getRenderTargetSampleCount(sampleCnt, format);
3712 if (!sampleCnt) {
Brian Salomonf865b052018-03-09 09:01:53 -05003713 return {};
3714 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003715 // We make a texture instead of a render target if we're using a
3716 // "multisampled_render_to_texture" style extension or have a BGRA format that
3717 // is allowed for textures but not render buffer internal formats.
3718 bool useTexture = false;
3719 if (sampleCnt > 1 && !this->glCaps().usesMSAARenderBuffers()) {
3720 useTexture = true;
3721 } else if (format == GrGLFormat::kBGRA8 &&
3722 this->glCaps().getRenderbufferInternalFormat(GrGLFormat::kBGRA8) != GR_GL_BGRA8) {
3723 // We have a BGRA extension that doesn't support BGRA render buffers. We can use a texture
3724 // unless we've been asked for MSAA. Note we already checked above for render-to-
3725 // multisampled-texture style extensions.
3726 if (sampleCnt > 1) {
3727 return {};
3728 }
3729 useTexture = true;
3730 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003731 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003732 if (sFormatIdx < 0) {
3733 return {};
3734 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003735 GrGLuint colorID = 0;
3736 GrGLuint stencilID = 0;
Brian Salomon72c7b982020-10-06 10:07:38 -04003737 GrGLFramebufferInfo info;
3738 info.fFBOID = 0;
3739 info.fFormat = GrGLFormatToEnum(format);
3740
3741 auto deleteIDs = [&](bool saveFBO = false) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003742 if (colorID) {
3743 if (useTexture) {
3744 GL_CALL(DeleteTextures(1, &colorID));
3745 } else {
3746 GL_CALL(DeleteRenderbuffers(1, &colorID));
3747 }
Brian Salomonf865b052018-03-09 09:01:53 -05003748 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003749 if (stencilID) {
3750 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003751 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003752 if (!saveFBO && info.fFBOID) {
3753 this->deleteFramebuffer(info.fFBOID);
3754 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003755 };
3756
3757 if (useTexture) {
3758 GL_CALL(GenTextures(1, &colorID));
3759 } else {
3760 GL_CALL(GenRenderbuffers(1, &colorID));
3761 }
3762 GL_CALL(GenRenderbuffers(1, &stencilID));
3763 if (!stencilID || !colorID) {
3764 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003765 return {};
3766 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003767
Brian Salomonf865b052018-03-09 09:01:53 -05003768 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3769 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003770 deleteIDs();
3771 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003772 }
3773
3774 this->invalidateBoundRenderTarget();
3775
Adrienne Walker4ee88512018-05-17 11:37:14 -07003776 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003777 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003778 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon72c7b982020-10-06 10:07:38 -04003779 colorID = this->createTexture(dimensions, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
Brian Salomon0f396992020-06-19 19:51:21 -04003780 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003781 if (!colorID) {
3782 deleteIDs();
3783 return {};
3784 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003785 if (sampleCnt == 1) {
3786 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3787 GR_GL_TEXTURE_2D, colorID, 0));
3788 } else {
3789 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3790 GR_GL_TEXTURE_2D, colorID, 0, sampleCnt));
3791 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003792 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003793 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003794 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomon72c7b982020-10-06 10:07:38 -04003795 if (sampleCnt == 1) {
3796 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, dimensions.width(),
3797 dimensions.height()));
3798 } else {
3799 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt, renderBufferFormat,
3800 dimensions.width(), dimensions.height())) {
3801 deleteIDs();
3802 return {};
3803 }
3804 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003805 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3806 GR_GL_RENDERBUFFER, colorID));
3807 }
3808 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003809 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx];
Brian Salomon72c7b982020-10-06 10:07:38 -04003810 if (sampleCnt == 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003811 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GrGLFormatToEnum(stencilBufferFormat),
3812 dimensions.width(), dimensions.height()));
Brian Salomon72c7b982020-10-06 10:07:38 -04003813 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003814 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt,
3815 GrGLFormatToEnum(stencilBufferFormat),
Brian Salomon72c7b982020-10-06 10:07:38 -04003816 dimensions.width(), dimensions.height())) {
3817 deleteIDs();
3818 return {};
3819 }
3820 }
Brian Salomonf865b052018-03-09 09:01:53 -05003821 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003822 stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003823 if (GrGLFormatIsPackedDepthStencil(this->glCaps().stencilFormats()[sFormatIdx])) {
Brian Salomonf865b052018-03-09 09:01:53 -05003824 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003825 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003826 }
3827
Brian Salomon93348dd2018-08-29 12:56:23 -04003828 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3829 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3830 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3831 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003832 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon72c7b982020-10-06 10:07:38 -04003833 deleteIDs(/* saveFBO = */ true);
Brian Salomonf865b052018-03-09 09:01:53 -05003834
Adrienne Walker4ee88512018-05-17 11:37:14 -07003835 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003836 GrGLenum status;
3837 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3838 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003839 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003840 return {};
3841 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003842
Greg Daniel8ade5e82020-10-07 13:09:48 -04003843 auto stencilBits = SkToInt(GrGLFormatStencilBits(this->glCaps().stencilFormats()[sFormatIdx]));
Robert Phillips62221e72019-07-24 15:07:38 -04003844
Brian Salomon72c7b982020-10-06 10:07:38 -04003845 GrBackendRenderTarget beRT = GrBackendRenderTarget(dimensions.width(), dimensions.height(),
3846 sampleCnt, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003847 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003848 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003849}
3850
3851void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003852 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003853 GrGLFramebufferInfo info;
3854 if (backendRT.getGLFramebufferInfo(&info)) {
3855 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003856 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003857 }
3858 }
joshualitt8fd844f2015-12-02 13:36:47 -08003859}
3860
Greg Daniel26b50a42018-03-08 09:49:58 -05003861void GrGLGpu::testingOnly_flushGpuAndSync() {
3862 GL_CALL(Finish());
3863}
Brian Salomonf865b052018-03-09 09:01:53 -05003864#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003865
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003866///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003867
cdaltone2e71c22016-04-07 18:13:29 -07003868GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003869 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003870 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003871 GrGLAttribArrayState* attribState;
3872
cdaltone2e71c22016-04-07 18:13:29 -07003873 if (gpu->glCaps().isCoreProfile()) {
3874 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003875 GrGLuint arrayID;
3876 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3877 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003878 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003879 }
cdaltone2e71c22016-04-07 18:13:29 -07003880 if (ibuf) {
3881 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003882 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003883 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003884 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003885 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003886 if (ibuf) {
3887 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003888 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003889 } else {
3890 this->setVertexArrayID(gpu, 0);
3891 }
3892 int attrCount = gpu->glCaps().maxVertexAttributes();
3893 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3894 fDefaultVertexArrayAttribState.resize(attrCount);
3895 }
3896 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003897 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003898 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003899}
bsalomone179a912016-01-20 06:18:10 -08003900
Greg Danielfe159622020-04-10 17:43:51 +00003901void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3902 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003903 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003904}
3905
Greg Daniel78be37f2020-04-14 16:40:35 -04003906void GrGLGpu::flush(FlushType flushType) {
3907 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3908 GL_CALL(Flush());
3909 fNeedsGLFlush = false;
3910 }
3911}
3912
Greg Danielfe159622020-04-10 17:43:51 +00003913bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3914 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003915 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003916 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003917 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003918 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003919 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003920 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003921 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003922 if (!this->glCaps().skipErrorChecks()) {
3923 this->clearErrorsAndCheckForOOM();
3924 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003925 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003926}
3927
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003928void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3929 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3930 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3931 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003932}
3933
Greg Daniel6be35232017-03-01 17:01:09 -05003934GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003935 if (!this->caps()->fenceSyncSupport()) {
3936 return 0;
3937 }
Greg Daniel6be35232017-03-01 17:01:09 -05003938 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003939 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3940 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3941 GrGLuint fence = 0;
3942 GL_CALL(GenFences(1, &fence));
3943 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3944 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3945 } else {
3946 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3947 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003948 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003949 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003950 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003951}
3952
Brian Salomonb0d8b762019-05-06 16:58:22 -04003953bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003954 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3955 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3956 if (!timeout) {
3957 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003958 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003959 }
3960 GrGLboolean result;
3961 GL_CALL_RET(result, TestFence(nvFence));
3962 return result == GR_GL_TRUE;
3963 }
3964 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3965 // If this really becomes necessary we could poll TestFence().
3966 // FinishFence always flushes so no need to check flush param.
3967 GL_CALL(FinishFence(nvFence));
3968 return true;
3969 } else {
3970 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3971 GrGLenum result;
3972 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3973 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3974 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003975}
3976
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003977bool GrGLGpu::waitFence(GrFence fence) {
3978 if (!this->caps()->fenceSyncSupport()) {
3979 return true;
3980 }
3981 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003982}
3983
3984void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003985 if (this->caps()->fenceSyncSupport()) {
3986 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3987 }
Greg Daniel6be35232017-03-01 17:01:09 -05003988}
3989
Greg Daniel301015c2019-11-18 14:06:46 -05003990std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003991 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003992 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003993}
3994
Greg Daniel301015c2019-11-18 14:06:46 -05003995std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3996 const GrBackendSemaphore& semaphore,
3997 GrResourceProvider::SemaphoreWrapType wrapType,
3998 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003999 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004000 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4001}
4002
Greg Daniel301015c2019-11-18 14:06:46 -05004003void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004004 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004005 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004006
Greg Daniel48661b82018-01-22 16:11:35 -05004007 GrGLsync sync;
4008 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4009 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04004010 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05004011}
4012
Greg Daniel301015c2019-11-18 14:06:46 -05004013void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004014 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004015 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004016
4017 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4018}
4019
Brian Salomonb0d8b762019-05-06 16:58:22 -04004020void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004021 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004022}
4023
Brian Salomon24069eb2020-06-24 10:19:52 -04004024void GrGLGpu::clearErrorsAndCheckForOOM() {
4025 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4026}
4027
4028GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4029#if GR_GL_CHECK_ERROR
4030 if (this->glInterface()->checkAndResetOOMed()) {
4031 this->setOOMed();
4032 }
4033#endif
4034 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4035 if (error == GR_GL_OUT_OF_MEMORY) {
4036 this->setOOMed();
4037 }
4038 return error;
4039}
4040
Greg Daniel6be35232017-03-01 17:01:09 -05004041void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004042 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4043 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4044 GL_CALL(DeleteFences(1, &nvFence));
4045 } else {
4046 GL_CALL(DeleteSync(sync));
4047 }
jvanverth84741b32016-09-30 08:39:02 -07004048}
Brian Osman13dddce2017-05-09 13:19:50 -04004049
Greg Daniel301015c2019-11-18 14:06:46 -05004050std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004051 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004052 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004053 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004054 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004055 // 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 -04004056 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004057
4058 return semaphore;
4059}
Robert Phillips646e4292017-06-13 12:44:56 -04004060
4061int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004062 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004063 case kTexture2DSampler_GrSLType:
4064 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004065 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004066 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004067 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004068 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004069 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004070 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004071 }
4072}
Brian Osman71a18892017-08-10 10:23:25 -04004073
Kevin Lubickf4def342018-10-04 12:52:50 -04004074#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004075#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004076void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4077 // We are called by the base class, which has already called beginObject(). We choose to nest
4078 // all of our caps information in a named sub-object.
4079 writer->beginObject("GL GPU");
4080
4081 const GrGLubyte* str;
4082 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4083 writer->appendString("GL_VERSION", (const char*)(str));
4084 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4085 writer->appendString("GL_RENDERER", (const char*)(str));
4086 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4087 writer->appendString("GL_VENDOR", (const char*)(str));
4088 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4089 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4090
4091 writer->appendName("extensions");
4092 glInterface()->fExtensions.dumpJSON(writer);
4093
4094 writer->endObject();
4095}
Kevin Lubickf4def342018-10-04 12:52:50 -04004096#endif