blob: f6798069c36c6b8a3cc9e2b5688aa17382406fdf [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 Salomon8ab1cc42017-12-07 12:40:00 -0500354 fCaps = sk_ref_sp(fGLContext->caps());
Brian Osman79719262020-11-23 14:54:33 -0500355 fCompiler = std::make_unique<SkSL::Compiler>(fCaps->shaderCaps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000356
Brian Salomond978b902019-02-07 15:09:18 -0500357 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000358
Brian Salomonae64c192019-02-05 09:41:37 -0500359 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
360 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600361 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500362 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500363 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
364 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
365 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
366 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700367 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500368 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
369 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700370 }
Brian Salomonae64c192019-02-05 09:41:37 -0500371 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400372 fHWBufferState[i].invalidate();
373 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600374 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700375
376 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400377 fPathRendering = std::make_unique<GrGLPathRendering>(this);
cdaltone2e71c22016-04-07 18:13:29 -0700378 }
379
Brian Salomona0d913b2020-09-01 12:42:26 -0400380 if (this->glCaps().useSamplerObjects()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400381 fSamplerObjectCache = std::make_unique<SamplerObjectCache>(this);
Brian Salomondc829942018-10-23 16:07:24 -0400382 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000383}
384
bsalomon861e1032014-12-16 07:33:49 -0800385GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700386 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
387 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800388 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700389 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700390 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800391
Chris Dalton20e7a532020-02-28 15:37:53 +0000392 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400393 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000394 // detach the current program so there is no confusion on OpenGL's part
395 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000396 GL_CALL(UseProgram(0));
397 }
398
Brian Salomon43f8bf02017-10-18 08:33:29 -0400399 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700400 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800401 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400402 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700403 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800404 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400405 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700406 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800407 }
egdaniel0f5f9672015-02-03 11:10:51 -0800408
bsalomon7ea33f52015-11-22 14:51:00 -0800409 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
410 if (0 != fCopyPrograms[i].fProgram) {
411 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
412 }
bsalomon6df86402015-06-01 10:41:49 -0700413 }
bsalomon6dea83f2015-12-03 12:58:06 -0800414
brianosman33f6b3f2016-06-02 05:49:21 -0700415 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
416 if (0 != fMipmapPrograms[i].fProgram) {
417 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
418 }
419 }
420
Brian Salomondc829942018-10-23 16:07:24 -0400421 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500422
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400423 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700424}
425
bsalomon6e2aad42016-04-01 11:54:31 -0700426void GrGLGpu::disconnect(DisconnectType type) {
427 INHERITED::disconnect(type);
428 if (DisconnectType::kCleanup == type) {
429 if (fHWProgramID) {
430 GL_CALL(UseProgram(0));
431 }
432 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700433 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700434 }
435 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700436 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700437 }
438 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700439 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700440 }
bsalomon6e2aad42016-04-01 11:54:31 -0700441 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
442 if (fCopyPrograms[i].fProgram) {
443 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
444 }
445 }
brianosman33f6b3f2016-06-02 05:49:21 -0700446 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
447 if (fMipmapPrograms[i].fProgram) {
448 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
449 }
450 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400451
Brian Salomondc829942018-10-23 16:07:24 -0400452 if (fSamplerObjectCache) {
453 fSamplerObjectCache->release();
454 }
bsalomon6e2aad42016-04-01 11:54:31 -0700455 } else {
456 if (fProgramCache) {
457 fProgramCache->abandon();
458 }
Brian Salomondc829942018-10-23 16:07:24 -0400459 if (fSamplerObjectCache) {
460 fSamplerObjectCache->abandon();
461 }
bsalomon6e2aad42016-04-01 11:54:31 -0700462 }
463
Chris Dalton20e7a532020-02-28 15:37:53 +0000464 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500465 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700466
bsalomonc8dc1f72014-08-21 13:02:13 -0700467 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700468 fTempSrcFBOID = 0;
469 fTempDstFBOID = 0;
470 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700471 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800472 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
473 fCopyPrograms[i].fProgram = 0;
474 }
brianosman33f6b3f2016-06-02 05:49:21 -0700475 fMipmapProgramArrayBuffer.reset();
476 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
477 fMipmapPrograms[i].fProgram = 0;
478 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400479
jvanverthe9c0fc62015-04-29 11:18:05 -0700480 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700481 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700482 }
Adlai Holler61a591c2020-10-12 12:38:33 -0400483 fFinishCallbacks.callAll(/* doDelete */ DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000484}
485
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000486///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000487
bsalomon861e1032014-12-16 07:33:49 -0800488void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000489 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400490 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000491 GL_CALL(Disable(GR_GL_DEPTH_TEST));
492 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000493
Brian Salomonf0861672017-05-08 11:10:10 -0400494 // We don't use face culling.
495 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600496 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
497 // just set this to the default for self-consistency.
498 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400499
Brian Salomonae64c192019-02-05 09:41:37 -0500500 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
501 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700502
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400503 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500504#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000505 // Desktop-only state that we never change
506 if (!this->glCaps().isCoreProfile()) {
507 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
508 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
509 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
510 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
511 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
512 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
513 }
514 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
515 // core profile. This seems like a bug since the core spec removes any mention of
516 // GL_ARB_imaging.
517 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
518 GL_CALL(Disable(GR_GL_COLOR_TABLE));
519 }
520 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400521
Chris Dalton1215cda2019-12-17 21:44:04 -0700522 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500523#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000524 // Since ES doesn't support glPointSize at all we always use the VS to
525 // set the point size
526 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
527
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000528 }
joshualitt58162332014-08-01 06:44:53 -0700529
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400530 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500531 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700532 // The arm extension requires specifically enabling MSAA fetching per sample.
533 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500534 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700535 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000536 fHWWriteToColor = kUnknown_TriState;
537 // we only ever use lines in hairline mode
538 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700539 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500540
541 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000542 }
edisonn@google.comba669992013-06-28 16:03:21 +0000543
egdanielb414f252014-07-29 13:15:47 -0700544 if (resetBits & kMSAAEnable_GrGLBackendState) {
545 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700546
Chris Dalton6ce447a2019-06-23 18:07:38 -0600547 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800548 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
549 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700550 GL_CALL(CoverageModulation(GR_GL_RGBA));
551 }
Chris Daltonce425af2019-12-16 10:39:03 -0700552
553 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000556 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400557 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000558
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000559 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500560 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500561 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000562 }
Brian Salomondc829942018-10-23 16:07:24 -0400563 if (fSamplerObjectCache) {
564 fSamplerObjectCache->invalidateBindings();
565 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000566 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000567
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000568 if (resetBits & kBlend_GrGLBackendState) {
569 fHWBlendState.invalidate();
570 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000571
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000572 if (resetBits & kView_GrGLBackendState) {
573 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700574 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000575 fHWViewport.invalidate();
576 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000578 if (resetBits & kStencil_GrGLBackendState) {
579 fHWStencilSettings.invalidate();
580 fHWStencilTestEnabled = kUnknown_TriState;
581 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000582
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 // Vertex
584 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700585 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500586 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
587 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600588 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700589 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000590 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000591
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500593 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700594 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500595 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000597
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000598 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700599 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700600 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000601 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000602 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000603
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000604 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000605 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400606 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
608 }
Brian Salomon1047a492019-07-02 12:25:21 -0400609 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000610 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
611 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000612 if (this->glCaps().packFlipYSupport()) {
613 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
614 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000615 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000616
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000617 if (resetBits & kProgram_GrGLBackendState) {
618 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000619 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000620 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400621 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000622}
623
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400624static bool check_backend_texture(const GrBackendTexture& backendTex,
625 const GrGLCaps& caps,
626 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400627 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400628 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400629 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400630 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800631 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000632
Brian Salomonea4ad302019-08-07 13:04:55 -0400633 desc->fSize = {backendTex.width(), backendTex.height()};
634 desc->fTarget = info.fTarget;
635 desc->fID = info.fID;
636 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800637
Brian Salomonea4ad302019-08-07 13:04:55 -0400638 if (desc->fFormat == GrGLFormat::kUnknown) {
639 return false;
640 }
641 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400642 if (!caps.shaderCaps()->externalTextureSupport()) {
643 return false;
644 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400645 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
646 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400647 return false;
648 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400649 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400650 return false;
651 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400652 if (backendTex.isProtected()) {
653 // Not supported in GL backend at this time.
654 return false;
655 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400656
Brian Salomond17f6582017-07-19 18:28:58 -0400657 return true;
658}
659
660sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400661 GrWrapOwnership ownership,
662 GrWrapCacheable cacheable,
663 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400664 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400665 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400666 return nullptr;
667 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400668
Brian Salomond17f6582017-07-19 18:28:58 -0400669 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400670 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400671 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400672 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400673 }
bsalomone5286e02016-01-14 09:24:09 -0800674
Brian Salomon40a40622020-07-21 10:32:07 -0400675 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400676 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400677
Brian Salomona6db5102020-07-21 09:56:23 -0400678 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400679 backendTex.getGLTextureParams(), cacheable, ioType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600680 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400681}
682
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500683static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
684 const GrGLCaps& caps, GrGLTexture::Desc* desc,
685 bool skipRectTexSupportCheck = false) {
686 GrGLTextureInfo info;
687 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
688 return false;
689 }
690
691 desc->fSize = {backendTex.width(), backendTex.height()};
692 desc->fTarget = info.fTarget;
693 desc->fID = info.fID;
694 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
695
696 if (desc->fFormat == GrGLFormat::kUnknown) {
697 return false;
698 }
699
700 if (GR_GL_TEXTURE_2D != desc->fTarget) {
701 return false;
702 }
703 if (backendTex.isProtected()) {
704 // Not supported in GL backend at this time.
705 return false;
706 }
707
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500708 return true;
709}
710
Robert Phillipsb915c942019-12-17 14:44:37 -0500711sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
712 GrWrapOwnership ownership,
713 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500714 GrGLTexture::Desc desc;
715 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
716 return nullptr;
717 }
718
719 if (kBorrow_GrWrapOwnership == ownership) {
720 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
721 } else {
722 desc.fOwnership = GrBackendObjectOwnership::kOwned;
723 }
724
Brian Salomon40a40622020-07-21 10:32:07 -0400725 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400726 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500727
Brian Salomona6db5102020-07-21 09:56:23 -0400728 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500729 backendTex.getGLTextureParams(), cacheable,
730 kRead_GrIOType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600731 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500732}
733
Brian Salomond17f6582017-07-19 18:28:58 -0400734sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400735 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500736 GrWrapOwnership ownership,
737 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400738 const GrGLCaps& caps = this->glCaps();
739
Brian Salomonea4ad302019-08-07 13:04:55 -0400740 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400741 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800742 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800743 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400744 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
745 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800746
Brian Salomond17f6582017-07-19 18:28:58 -0400747 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400748 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800749 return nullptr;
750 }
bsalomon10528f12015-10-14 12:54:52 -0700751
Brian Osman766fcbb2017-03-13 09:33:09 -0400752 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400753 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400754 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400755 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800756 }
bsalomonb15b4c12014-10-29 12:41:57 -0700757
Robert Phillips0902c982019-07-16 07:47:56 -0400758
Greg Daniel6fa62e22019-08-07 15:52:37 -0400759 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
760 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000761
Brian Salomonea4ad302019-08-07 13:04:55 -0400762 GrGLRenderTarget::IDs rtIDs;
763 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400764 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000765 }
Greg Daniel177e6952017-10-12 12:27:11 -0400766
Brian Salomon40a40622020-07-21 10:32:07 -0400767 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400768 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400769
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500770 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400771 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400772 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400773 texRT->baseLevelWasBoundToFBO();
Mike Kleina9609ea2020-02-18 13:33:23 -0600774 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000775}
776
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400777sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400778 GrGLFramebufferInfo info;
779 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000780 return nullptr;
781 }
782
Brian Salomone8a766b2019-07-19 14:24:36 -0400783 if (backendRT.isProtected()) {
784 // Not supported in GL at this time.
785 return nullptr;
786 }
787
Brian Salomond4764a12019-08-08 12:08:24 -0400788 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400789 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
790 return nullptr;
791 }
792
Brian Salomonea4ad302019-08-07 13:04:55 -0400793 GrGLRenderTarget::IDs rtIDs;
794 rtIDs.fRTFBOID = info.fFBOID;
795 rtIDs.fMSColorRenderbufferID = 0;
796 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
797 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000798
Greg Daniel6fa62e22019-08-07 15:52:37 -0400799 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700800
Brian Salomona56a7462020-02-07 14:17:25 -0500801 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400802 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000803}
804
Brian Salomonc320b152018-02-20 14:05:36 -0500805static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700806 if (!glTex) {
807 return false;
808 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000809
bsalomone5286e02016-01-14 09:24:09 -0800810 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
811 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800812 return false;
813 }
814
jvanverth17aa0472016-01-05 10:41:27 -0800815 return true;
816}
817
Brian Salomona9b04b92018-06-01 15:04:28 -0400818bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400819 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400820 const GrMipLevel texels[], int mipLevelCount,
821 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500822 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800823
Brian Salomonc320b152018-02-20 14:05:36 -0500824 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800825 return false;
826 }
827
Brian Salomond978b902019-02-07 15:09:18 -0500828 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000829
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400830 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400831 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
832 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
833 glTex->target(), dstRect, srcColorType, texels,
834 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000835}
836
Brian Salomone05ba5a2019-04-08 11:59:07 -0400837bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400838 GrColorType textureColorType, GrColorType bufferColorType,
839 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400840 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400841
Jim Van Verth1676cb92019-01-15 13:24:45 -0500842 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400843 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500844
Brian Salomonc320b152018-02-20 14:05:36 -0500845 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400846 return false;
847 }
848
Hal Canaryc36f7e72018-06-18 15:50:09 -0400849 static_assert(sizeof(int) == sizeof(int32_t), "");
850 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400851 return false;
852 }
853
Brian Salomond978b902019-02-07 15:09:18 -0500854 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400855
856 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500857 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500859 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400860
Greg Daniel660cc992017-06-26 14:55:05 -0400861 SkDEBUGCODE(
862 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
863 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
864 SkASSERT(bounds.contains(subRect));
865 )
866
Brian Salomonb28cb682019-07-26 12:48:47 -0400867 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400868 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400869 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700870 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400871 return false;
872 }
873
874 bool restoreGLRowLength = false;
875 if (trimRowBytes != rowBytes) {
876 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400877 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400878 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
879 restoreGLRowLength = true;
880 }
881
Greg Danielba88ab62019-07-26 09:14:01 -0400882 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400883 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400884 GrGLenum externalFormat = 0;
885 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400886 this->glCaps().getTexSubImageExternalFormatAndType(
887 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400888 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400889 return false;
890 }
891
Brian Salomon8cbf6622019-07-30 11:03:14 -0400892 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400893 GL_CALL(TexSubImage2D(glTex->target(),
894 0,
895 left, top,
896 width,
897 height,
898 externalFormat, externalType,
899 pixels));
900
901 if (restoreGLRowLength) {
902 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
903 }
904
905 return true;
906}
907
Brian Salomon26de56e2019-04-10 12:14:26 -0400908bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400909 GrColorType surfaceColorType, GrColorType dstColorType,
910 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400911 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
912 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400913 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400914 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
915 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400916}
917
Brian Osman9b560242017-09-05 15:34:52 -0400918void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500919 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
920 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
921 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
922 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400923 }
Brian Osman9b560242017-09-05 15:34:52 -0400924}
925
Brian Salomon22558932020-05-15 14:46:34 -0400926bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
927 GrColorType textureColorType,
928 SkISize texDims,
929 GrGLenum target,
930 SkIRect dstRect,
931 GrColorType srcColorType,
932 const GrMipLevel texels[],
933 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500934 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400935 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500936
Greg Daniel7bfc9132019-08-14 14:23:53 -0400937 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800938
Brian Salomonf77c1462019-08-01 15:19:29 -0400939 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800940
bsalomon5b30c6f2015-12-17 14:17:34 -0800941 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800942 GrGLenum externalFormat;
943 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400944 this->glCaps().getTexSubImageExternalFormatAndType(
945 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400946 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800947 return false;
948 }
Brian Salomon22558932020-05-15 14:46:34 -0400949 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
950 mipLevelCount);
951 return true;
952}
Brian Salomonf77c1462019-08-01 15:19:29 -0400953
Brian Salomon22558932020-05-15 14:46:34 -0400954bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
955 SkISize texDims,
956 GrGLenum target,
957 SkColor4f color,
958 uint32_t levelMask) {
959 GrColorType colorType;
960 GrGLenum externalFormat, externalType;
961 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
962 &externalType, &colorType);
963 if (colorType == GrColorType::kUnknown) {
964 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000965 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400966
Brian Salomon22558932020-05-15 14:46:34 -0400967 std::unique_ptr<char[]> pixelStorage;
968 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400969 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400970 SkSTArray<16, GrMipLevel> levels;
971 levels.resize(numLevels);
972 SkISize levelDims = texDims;
973 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
974 std::max(levelDims.height() >> 1, 1)}) {
975 if (levelMask & (1 << i)) {
976 if (!pixelStorage) {
977 // Make one tight image at the first size and reuse it for smaller levels.
978 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
979 size_t rb = ii.minRowBytes();
980 pixelStorage.reset(new char[rb * levelDims.height()]);
981 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
982 return false;
983 }
984 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000985 }
Brian Salomon22558932020-05-15 14:46:34 -0400986 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
987 }
988 }
989 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
990 bpp, levels.begin(), levels.count());
991 return true;
992}
993
994void GrGLGpu::uploadTexData(SkISize texDims,
995 GrGLenum target,
996 SkIRect dstRect,
997 GrGLenum externalFormat,
998 GrGLenum externalType,
999 size_t bpp,
1000 const GrMipLevel texels[],
1001 int mipLevelCount) {
1002 SkASSERT(!texDims.isEmpty());
1003 SkASSERT(!dstRect.isEmpty());
1004 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001005 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001006 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1007
1008 const GrGLCaps& caps = this->glCaps();
1009
1010 bool restoreGLRowLength = false;
1011
1012 this->unbindCpuToGpuXferBuffer();
1013 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1014
1015 SkISize dims = dstRect.size();
1016 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1017 std::max(dims.height() >> 1, 1)}) {
1018 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001019 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001020 }
Brian Salomon22558932020-05-15 14:46:34 -04001021 const size_t trimRowBytes = dims.width() * bpp;
1022 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001023
1024 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1025 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001026 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001027 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001028 } else {
1029 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001030 }
1031
Brian Salomon22558932020-05-15 14:46:34 -04001032 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1033 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001034 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001035 if (restoreGLRowLength) {
1036 SkASSERT(caps.writePixelsRowBytesSupport());
1037 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1038 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001039}
1040
Greg Daniel01f278c2020-06-12 16:58:17 -04001041bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1042 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001043 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001044 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001045 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001046 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001047 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001048 const GrGLCaps& caps = this->glCaps();
1049
1050 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001051 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001052 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001053 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001054 }
1055
Robert Phillips9f744f72019-12-19 19:14:33 -05001056 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1057
Greg Daniel7bfc9132019-08-14 14:23:53 -04001058 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001059
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001060 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001061 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001062 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001063 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001064
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001065 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001066 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001067
1068 if (useTexStorage) {
1069 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001070 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1071 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001072 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001073 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001074 }
Robert Phillips42716d42019-12-16 12:19:54 -05001075
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001076 size_t offset = 0;
1077 for (int level = 0; level < numMipLevels; ++level) {
1078
Robert Phillips99dead92020-01-27 16:11:57 -05001079 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1080 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001081
Brian Salomond8575452020-02-25 12:13:29 -05001082 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1083 level,
1084 0, // left
1085 0, // top
1086 dimensions.width(),
1087 dimensions.height(),
1088 internalFormat,
1089 SkToInt(levelDataSize),
1090 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001091
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001092 if (error != GR_GL_NO_ERROR) {
1093 return false;
1094 }
1095
Robert Phillips9f744f72019-12-19 19:14:33 -05001096 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001097 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001098 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001099 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001100 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001101
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001102 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001103 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1104 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001105
1106 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001107 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1108 level,
1109 internalFormat,
1110 dimensions.width(),
1111 dimensions.height(),
1112 0, // border
1113 SkToInt(levelDataSize),
1114 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001115
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001116 if (error != GR_GL_NO_ERROR) {
1117 return false;
1118 }
1119
Robert Phillips9f744f72019-12-19 19:14:33 -05001120 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001121 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001122 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001123 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001124 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001125}
1126
Brian Salomond8575452020-02-25 12:13:29 -05001127bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001128 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001129 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001130 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001131 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001132 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001133 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1134 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001135 break;
1136 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001137 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1138 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001139 break;
1140 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1141 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001142 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1143 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001144 break;
1145 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001146 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001147 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001148 }
Brian Salomond8575452020-02-25 12:13:29 -05001149 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001150}
1151
Brian Salomonea4ad302019-08-07 13:04:55 -04001152bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001153 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001154 GrGLRenderTarget::IDs* rtIDs) {
1155 rtIDs->fMSColorRenderbufferID = 0;
1156 rtIDs->fRTFBOID = 0;
1157 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1158 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001159
bsalomona11e5fc2015-12-18 07:59:41 -08001160 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001161
Brian Salomonea4ad302019-08-07 13:04:55 -04001162 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001163 goto FAILED;
1164 }
1165
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001166 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001167 goto FAILED;
1168 }
1169
Brian Salomonea4ad302019-08-07 13:04:55 -04001170 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1171 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172 goto FAILED;
1173 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001174
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001175 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1176 // the texture bound to the other. The exception is the IMG multisample extension. With this
1177 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1178 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001179 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001180 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1181 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1182 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001183 goto FAILED;
1184 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001185 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001187 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001188 }
1189
egdanield803f272015-03-18 13:01:52 -07001190 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001191 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001193 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001194 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001195 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1196 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 goto FAILED;
1198 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001200 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001201 GR_GL_COLOR_ATTACHMENT0,
1202 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001203 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001207 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1209 GR_GL_COLOR_ATTACHMENT0,
1210 desc.fTarget,
1211 desc.fID,
1212 0,
1213 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001214 } else {
egdanield803f272015-03-18 13:01:52 -07001215 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001216 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 desc.fTarget,
1218 desc.fID,
1219 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001220 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001221
1222 return true;
1223
1224FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001225 if (rtIDs->fMSColorRenderbufferID) {
1226 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1229 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001230 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001231 if (rtIDs->fTexFBOID) {
1232 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 }
1234 return false;
1235}
1236
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001237// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001238static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001239// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001240 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001241}
1242
Brian Salomone2826ab2019-06-04 15:58:31 -04001243static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001245 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1246 // drivers have a bug where an FBO won't be complete if it includes a
1247 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001248 GrGLTextureParameters::SamplerOverriddenState state;
1249 state.fMinFilter = GR_GL_NEAREST;
1250 state.fMagFilter = GR_GL_NEAREST;
1251 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1252 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001253 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1254 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1255 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1256 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001257 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001258}
1259
Brian Salomona56a7462020-02-07 14:17:25 -05001260sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001261 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001262 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001263 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001264 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001265 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001266 int mipLevelCount,
1267 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001268 // We don't support protected textures in GL.
1269 if (isProtected == GrProtected::kYes) {
1270 return nullptr;
1271 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001272 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001273
Brian Salomond2a8ae22019-09-10 16:03:59 -04001274 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001275 GrMipmapStatus mipmapStatus =
1276 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001277 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001278 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001279 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001280 switch (format.textureType()) {
1281 case GrTextureType::kExternal:
1282 case GrTextureType::kNone:
1283 return nullptr;
1284 case GrTextureType::k2D:
1285 texDesc.fTarget = GR_GL_TEXTURE_2D;
1286 break;
1287 case GrTextureType::kRectangle:
1288 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1289 return nullptr;
1290 }
1291 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1292 break;
1293 }
Brian Salomon81536f22019-08-08 16:30:49 -04001294 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001295 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001296 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1297 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001298
Brian Salomon0f396992020-06-19 19:51:21 -04001299 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1300 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001301
1302 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001303 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001304 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001305
Robert Phillips67d52cf2017-06-05 13:38:13 -04001306 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001307 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001308 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001309 GL_CALL(BindTexture(texDesc.fTarget, 0));
1310 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001311
Brian Salomonea4ad302019-08-07 13:04:55 -04001312 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1313 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 return return_null_texture();
1315 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001317 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001318 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001319 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001320 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001322 // The non-sampler params are still at their default values.
1323 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1324 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001325 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001326 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001327 GrGLenum externalFormat, externalType;
1328 GrColorType colorType;
1329 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1330 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001331 for (int i = 0; i < mipLevelCount; ++i) {
1332 if (levelClearMask & (1U << i)) {
1333 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1334 nullptr));
1335 }
1336 }
1337 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1338 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001339 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001340 this->disableWindowRectangles();
1341 this->flushColorWrite(true);
Brian Salomon07bc9a22020-12-02 13:37:16 -05001342 this->flushClearColor({0, 0, 0, 0});
Brian Salomond2a8ae22019-09-10 16:03:59 -04001343 for (int i = 0; i < mipLevelCount; ++i) {
1344 if (levelClearMask & (1U << i)) {
1345 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1346 kDst_TempFBOTarget);
1347 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1348 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1349 }
1350 }
Brian Salomonc2249852019-09-16 11:08:50 -04001351 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001352 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001353 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001354 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001355 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001356 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001357 }
1358 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001359 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001360}
1361
Robert Phillips9f744f72019-12-19 19:14:33 -05001362sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001363 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001364 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001365 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001366 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001367 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001368 // We don't support protected textures in GL.
1369 if (isProtected == GrProtected::kYes) {
1370 return nullptr;
1371 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001372 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1373
Brian Salomonbb8dde82019-06-27 10:52:13 -04001374 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001375 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001376 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001377 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001378 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001379 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001380 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001381 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001382 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001383 return nullptr;
1384 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001385
Greg Danielaaf738c2020-07-10 09:30:33 -04001386 if (data) {
1387 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1388 GR_GL_TEXTURE_2D, data, dataSize)) {
1389 GL_CALL(DeleteTextures(1, &desc.fID));
1390 return nullptr;
1391 }
1392 }
1393
Robert Phillipsee946932019-12-18 11:16:17 -05001394 // Unbind this texture from the scratch texture unit.
1395 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1396
Brian Salomona6db5102020-07-21 09:56:23 -04001397 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1398 ? GrMipmapStatus::kValid
1399 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001400
Brian Salomona6db5102020-07-21 09:56:23 -04001401 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001402 // The non-sampler params are still at their default values.
1403 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1404 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001405 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001406}
1407
Greg Danielc1ad77c2020-05-06 11:40:03 -04001408GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001409 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001410 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001411 // We don't support protected textures in GL.
1412 if (isProtected == GrProtected::kYes) {
1413 return {};
1414 }
1415
1416 this->handleDirtyContext();
1417
1418 GrGLFormat glFormat = format.asGLFormat();
1419 if (glFormat == GrGLFormat::kUnknown) {
1420 return {};
1421 }
1422
Greg Daniel01f278c2020-06-12 16:58:17 -04001423 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1424
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001425 GrGLTextureInfo info;
1426 GrGLTextureParameters::SamplerOverriddenState initialState;
1427
1428 info.fTarget = GR_GL_TEXTURE_2D;
1429 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001430 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001431 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001432 if (!info.fID) {
1433 return {};
1434 }
1435
1436 // Unbind this texture from the scratch texture unit.
1437 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1438
1439 auto parameters = sk_make_sp<GrGLTextureParameters>();
1440 // The non-sampler params are still at their default values.
1441 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1442 fResetTimestampForTextureParameters);
1443
1444 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1445 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001446}
1447
Greg Danielaaf738c2020-07-10 09:30:33 -04001448bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1449 sk_sp<GrRefCntedCallback> finishedCallback,
1450 const BackendTextureData* data) {
1451 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1452
1453 GrGLTextureInfo info;
1454 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1455
1456 GrBackendFormat format = backendTexture.getBackendFormat();
1457 GrGLFormat glFormat = format.asGLFormat();
1458 if (glFormat == GrGLFormat::kUnknown) {
1459 return false;
1460 }
1461 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1462
Brian Salomon40a40622020-07-21 10:32:07 -04001463 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001464
1465 const char* rawData = nullptr;
1466 size_t rawDataSize = 0;
1467 SkAutoMalloc am;
1468 if (data->type() == BackendTextureData::Type::kCompressed) {
1469 rawData = (const char*)data->compressedData();
1470 rawDataSize = data->compressedSize();
1471 } else {
1472 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1473 SkASSERT(compression != SkImage::CompressionType::kNone);
1474
1475 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001476 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001477
1478 am.reset(rawDataSize);
1479
1480 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1481 data->color());
1482
1483 rawData = (const char*)am.get();
1484 }
1485
1486 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001487
1488 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1489 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04001490 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelControlSupport()) {
Greg Daniel95afafb2020-07-22 12:09:26 -04001491 auto params = backendTexture.getGLTextureParams();
1492 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1493 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1494 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1495 nonsamplerState.fBaseMipMapLevel = 0;
1496 }
1497 int numMipLevels =
1498 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1499 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1500 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1501 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1502 }
1503 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1504 }
1505
Greg Danielaaf738c2020-07-10 09:30:33 -04001506 bool result = this->uploadCompressedTexData(
1507 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1508 rawData, rawDataSize);
1509
1510 // Unbind this texture from the scratch texture unit.
1511 this->bindTextureToScratchUnit(info.fTarget, 0);
1512
1513 return result;
1514}
1515
Brian Salomon5043f1f2019-07-11 21:27:54 -04001516int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001517 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001518 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1519
1520 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001521 // Default to unsupported, set this if we find a stencil format that works.
1522 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001523
Brian Salomon0f396992020-06-19 19:51:21 -04001524 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1525 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001526 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001527 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001528 }
egdanielff1d5472015-09-10 08:37:20 -07001529 // unbind the texture from the texture unit before binding it to the frame buffer
1530 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1531
1532 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001533 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001534 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001535 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001536 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001537 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1538 GR_GL_COLOR_ATTACHMENT0,
1539 GR_GL_TEXTURE_2D,
1540 colorID,
1541 0));
bsalomon30447372015-12-21 09:03:05 -08001542 GrGLuint sbRBID = 0;
1543 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001544
1545 // look over formats till I find a compatible one
1546 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001547 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001548 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001549 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001550 GrGLFormat sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001551 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
Greg Daniel8ade5e82020-10-07 13:09:48 -04001552 GR_GL_RENDERBUFFER, GrGLFormatToEnum(sFmt), kSize, kSize));
Brian Salomond8575452020-02-25 12:13:29 -05001553 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001554 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001555 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001556 GR_GL_RENDERBUFFER, sbRBID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001557 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
bsalomon30447372015-12-21 09:03:05 -08001558 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1559 GR_GL_DEPTH_ATTACHMENT,
1560 GR_GL_RENDERBUFFER, sbRBID));
1561 } else {
1562 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1563 GR_GL_DEPTH_ATTACHMENT,
1564 GR_GL_RENDERBUFFER, 0));
1565 }
1566 GrGLenum status;
1567 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1568 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1569 firstWorkingStencilFormatIndex = i;
1570 break;
1571 }
egdanielff1d5472015-09-10 08:37:20 -07001572 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1573 GR_GL_STENCIL_ATTACHMENT,
1574 GR_GL_RENDERBUFFER, 0));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001575 if (GrGLFormatIsPackedDepthStencil(sFmt)) {
egdanielff1d5472015-09-10 08:37:20 -07001576 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1577 GR_GL_DEPTH_ATTACHMENT,
1578 GR_GL_RENDERBUFFER, 0));
1579 }
egdanielff1d5472015-09-10 08:37:20 -07001580 }
1581 }
bsalomon30447372015-12-21 09:03:05 -08001582 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001583 }
1584 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001585 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1586 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001587 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001588 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001589 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001590}
1591
Brian Salomonea4ad302019-08-07 13:04:55 -04001592GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001593 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001594 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001595 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001596 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001597 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001598 if (format == GrGLFormat::kUnknown) {
1599 return 0;
1600 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001601 GrGLuint id = 0;
1602 GL_CALL(GenTextures(1, &id));
1603 if (!id) {
1604 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001605 }
1606
Brian Salomonea4ad302019-08-07 13:04:55 -04001607 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001608
Brian Salomonea4ad302019-08-07 13:04:55 -04001609 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1610
Brian Salomonea4ad302019-08-07 13:04:55 -04001611 return id;
1612}
1613
Brian Salomon0f396992020-06-19 19:51:21 -04001614GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1615 GrGLFormat format,
1616 GrGLenum target,
1617 GrRenderable renderable,
1618 GrGLTextureParameters::SamplerOverriddenState* initialState,
1619 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001620 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001622
Brian Salomonea4ad302019-08-07 13:04:55 -04001623 GrGLuint id = 0;
1624 GL_CALL(GenTextures(1, &id));
1625
1626 if (!id) {
1627 return 0;
1628 }
1629
Brian Salomon0f396992020-06-19 19:51:21 -04001630 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001631
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001632 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001633 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001634 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001635 }
1636
Brian Salomond2a8ae22019-09-10 16:03:59 -04001637 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001638 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001639 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001640 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001641 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001642
1643 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1644
1645 bool success = false;
1646 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001647 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001648 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001649 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1650 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001651 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001652 } else {
1653 GrGLenum externalFormat, externalType;
1654 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1655 GrGLenum error = GR_GL_NO_ERROR;
1656 if (externalFormat && externalType) {
1657 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1658 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001659 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1660 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001661 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1662 currentHeight, 0, externalFormat, externalType,
1663 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001664 }
Brian Salomond8575452020-02-25 12:13:29 -05001665 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001666 }
1667 }
1668 }
1669 if (success) {
1670 return id;
1671 }
1672 GL_CALL(DeleteTextures(1, &id));
1673 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001674}
1675
Greg Danielc0d69152020-10-08 14:59:00 -04001676sk_sp<GrAttachment> GrGLGpu::makeStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1677 SkISize dimensions,
1678 int numStencilSamples) {
Greg Daniele77162e2020-09-21 15:32:11 -04001679 SkASSERT(dimensions.width() >= rt->width());
1680 SkASSERT(dimensions.height() >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001681
Greg Danielc0d69152020-10-08 14:59:00 -04001682 GrGLAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001683
Brian Salomond4764a12019-08-08 12:08:24 -04001684 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001685 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001686 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001687 }
egdanielff1d5472015-09-10 08:37:20 -07001688
1689 if (!sbDesc.fRenderbufferID) {
1690 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1691 }
1692 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001693 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001694 }
1695 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04001696 GrGLFormat sFmt = this->glCaps().stencilFormats()[sIdx];
1697 GrGLenum glFmt = GrGLFormatToEnum(sFmt);
egdanielff1d5472015-09-10 08:37:20 -07001698 // we do this "if" so that we don't call the multisample
1699 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001700 if (numStencilSamples > 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001701 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001702 dimensions.width(), dimensions.height())) {
Brian Salomond8575452020-02-25 12:13:29 -05001703 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1704 return nullptr;
1705 }
egdanielff1d5472015-09-10 08:37:20 -07001706 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04001707 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, glFmt,
Greg Daniele77162e2020-09-21 15:32:11 -04001708 dimensions.width(),
1709 dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001710 if (error != GR_GL_NO_ERROR) {
1711 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1712 return nullptr;
1713 }
egdanielff1d5472015-09-10 08:37:20 -07001714 }
1715 fStats.incStencilAttachmentCreates();
Greg Daniel17b0c752020-10-01 16:06:37 -04001716
Greg Danielc0d69152020-10-08 14:59:00 -04001717 return sk_sp<GrAttachment>(new GrGLAttachment(
Greg Daniel25ebd952020-11-03 11:17:05 -05001718 this, sbDesc, dimensions, GrAttachment::UsageFlags::kStencilAttachment,
1719 numStencilSamples, sFmt));
reed@google.comac10a2d2010-12-22 21:39:39 +00001720}
1721
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001722////////////////////////////////////////////////////////////////////////////////
1723
Brian Salomondbf70722019-02-07 11:31:24 -05001724sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1725 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001726 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001727}
1728
Chris Dalton2e7ed262020-02-21 15:17:59 -07001729void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1730 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001731 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1732 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1733 fHWScissorSettings.fEnabled = kYes_TriState;
1734 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001735 } else {
1736 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1737 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1738 fHWScissorSettings.fEnabled = kNo_TriState;
1739 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001741}
Brian Salomond818ebf2018-07-02 14:08:49 +00001742
Chris Dalton2e7ed262020-02-21 15:17:59 -07001743void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1744 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001745 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001746 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1747 if (fHWScissorSettings.fRect != nativeScissor) {
1748 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1749 nativeScissor.fHeight));
1750 fHWScissorSettings.fRect = nativeScissor;
1751 }
joshualitt77b13072014-10-27 14:51:01 -07001752}
1753
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001754void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001755 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001756#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001757 typedef GrWindowRectsState::Mode Mode;
1758 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1759 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001760
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001761 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001762 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001763 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001764 }
1765
csmartdalton7535f412016-08-23 06:51:00 -07001766 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1767 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001768 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001769 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001770
Chris Daltond6cda8d2019-09-05 02:30:04 -06001771 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001772 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001773 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001774 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001775 }
1776
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001777 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001778 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001779
Greg Danielacd66b42019-05-22 16:29:12 -04001780 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001781#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001782}
1783
1784void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001785#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001786 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001787 return;
1788 }
1789 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001791#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001792}
1793
Robert Phillipsdf546db2020-05-29 09:42:54 -04001794bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001795 this->handleDirtyContext();
1796
Chris Daltond42d2002020-02-28 12:05:04 -07001797 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001798 if (!program) {
1799 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1800 return false;
1801 }
1802
1803 this->flushProgram(std::move(program));
1804
Chris Daltond42d2002020-02-28 12:05:04 -07001805 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1806 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1807 }
1808
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001809 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001810 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001811 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001812
Chris Dalton20e7a532020-02-28 15:37:53 +00001813 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001814
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001815 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001816 GrStencilSettings stencil;
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001817 if (programInfo.isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001818 SkASSERT(glRT->getStencilAttachment());
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001819 stencil.reset(*programInfo.userStencilSettings(),
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001820 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001821 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001822 }
Robert Phillips901aff02019-10-08 12:32:56 -04001823 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001824 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001825 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1826 glRT, programInfo.origin());
1827 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001828 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001829 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001830
1831 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001832 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001833 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001834
Chris Dalton20e7a532020-02-28 15:37:53 +00001835 return true;
1836}
1837
1838void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1839 if (!program) {
1840 fHWProgram.reset();
1841 fHWProgramID = 0;
1842 return;
1843 }
1844 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1845 if (program == fHWProgram) {
1846 return;
1847 }
1848 auto id = program->programID();
1849 SkASSERT(id);
1850 GL_CALL(UseProgram(id));
1851 fHWProgram = std::move(program);
1852 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001853}
1854
1855void GrGLGpu::flushProgram(GrGLuint id) {
1856 SkASSERT(id);
1857 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001858 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001859 return;
1860 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001861 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001862 GL_CALL(UseProgram(id));
1863 fHWProgramID = id;
1864}
1865
Brian Salomonae64c192019-02-05 09:41:37 -05001866GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001867 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001868
cdaltone2e71c22016-04-07 18:13:29 -07001869 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001870 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001871 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001872 }
cdaltone2e71c22016-04-07 18:13:29 -07001873
Brian Salomonae64c192019-02-05 09:41:37 -05001874 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001875 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001876 if (!bufferState->fBufferZeroKnownBound) {
1877 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1878 bufferState->fBufferZeroKnownBound = true;
1879 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001880 }
Brian Salomondbf70722019-02-07 11:31:24 -05001881 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1882 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001883 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1884 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1885 bufferState->fBufferZeroKnownBound = false;
1886 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001887 }
1888
Brian Salomonae64c192019-02-05 09:41:37 -05001889 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001890}
Brian Salomond818ebf2018-07-02 14:08:49 +00001891
Brian Salomon07bc9a22020-12-02 13:37:16 -05001892void GrGLGpu::clear(const GrScissorState& scissor,
1893 std::array<float, 4> color,
1894 GrRenderTarget* target,
1895 GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001896 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001897 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001898 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001899 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001900
Brian Salomon43f8bf02017-10-18 08:33:29 -04001901 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001902
Brian Salomon43f8bf02017-10-18 08:33:29 -04001903 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1904
Michael Ludwig1e632792020-05-21 12:45:31 -04001905 if (scissor.enabled()) {
1906 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001907 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001908 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001909 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001910 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1911 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001912 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001913 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001914 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001915}
1916
Chris Daltonb50cc812019-10-14 16:29:21 -06001917static bool use_tiled_rendering(const GrGLCaps& glCaps,
1918 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1919 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1920 // Otherwise it's faster to just not use it.
1921 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1922 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1923}
1924
1925void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001926 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1927 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1928 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1929
1930 this->handleDirtyContext();
1931
1932 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1933 this->flushRenderTarget(glRT);
1934 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1935
Chris Daltonb50cc812019-10-14 16:29:21 -06001936 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1937 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1938 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1939 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1940 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1941 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1942 nativeBounds.fHeight, preserveMask));
1943 }
1944
Chris Dalton674f77a2019-09-30 20:49:39 -06001945 GrGLbitfield clearMask = 0;
1946 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1947 SkASSERT(!this->caps()->performColorClearsAsDraws());
1948 this->flushClearColor(colorLoadStore.fClearColor);
1949 this->flushColorWrite(true);
1950 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001951 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001952 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1953 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1954 GL_CALL(StencilMask(0xffffffff));
1955 GL_CALL(ClearStencil(0));
1956 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1957 }
1958 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001959 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001960 this->disableWindowRectangles();
1961 GL_CALL(Clear(clearMask));
1962 }
1963}
1964
1965void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1966 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1967 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1968 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1969
1970 this->handleDirtyContext();
1971
1972 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1973 // The framebuffer binding changed in the middle of a command buffer. We should have already
1974 // printed a warning during onFBOChanged.
1975 return;
1976 }
1977
1978 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1979 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1980
1981 SkSTArray<2, GrGLenum> discardAttachments;
1982 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1983 discardAttachments.push_back(
1984 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
1985 }
1986 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
1987 discardAttachments.push_back(
1988 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
1989 }
1990
1991 if (!discardAttachments.empty()) {
1992 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
1993 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1994 discardAttachments.begin()));
1995 } else {
1996 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
1997 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
1998 discardAttachments.begin()));
1999 }
2000 }
2001 }
2002
Chris Daltonb50cc812019-10-14 16:29:21 -06002003 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2004 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2005 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2006 // Handled by use_tiled_rendering().
2007 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2008 GL_CALL(EndTiling(preserveMask));
2009 }
2010
Chris Dalton674f77a2019-09-30 20:49:39 -06002011 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002012}
2013
Michael Ludwig1e632792020-05-21 12:45:31 -04002014void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002015 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002016 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002017 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002018 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002019 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002020
Greg Danielc0d69152020-10-08 14:59:00 -04002021 GrAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002022 if (!sb) {
2023 // We should only get here if we marked a proxy as requiring a SB. However,
2024 // the SB creation could later fail. Likely clipping is going to go awry now.
2025 return;
2026 }
2027
Greg Daniel8ade5e82020-10-07 13:09:48 -04002028 GrGLint stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat());
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002029#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002030 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002031 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002032#else
2033 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002034 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002035 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002036 // changing the clip between stencil passes may or may not
2037 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002038 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002039#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002040 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002041 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002042 value = (1 << (stencilBitCount - 1));
2043 } else {
2044 value = 0;
2045 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002046 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002047 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002048
Michael Ludwig1e632792020-05-21 12:45:31 -04002049 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2050 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002051
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002052 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002053 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002054 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002055 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002056}
2057
Brian Salomone05ba5a2019-04-08 11:59:07 -04002058bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002059 GrColorType surfaceColorType, GrColorType dstColorType,
2060 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002061 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002062
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002063 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002064 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002065 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002066 return false;
2067 }
Greg Danielba88ab62019-07-26 09:14:01 -04002068 GrGLenum externalFormat = 0;
2069 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002070 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2071 surfaceColorType,
2072 dstColorType,
2073 &externalFormat,
2074 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002075 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002076 return false;
2077 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002078
Brian Salomon71d9d842016-11-03 13:42:00 -04002079 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002080 if (renderTarget->numSamples() <= 1 ||
2081 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2082 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2083 this->flushRenderTargetNoColorWrites(renderTarget);
2084 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2085 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2086 return false;
2087 } else {
2088 SkASSERT(renderTarget->requiresManualMSAAResolve());
2089 // we don't track the state of the READ FBO ID.
2090 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002091 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002092 } else {
2093 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002094 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002095 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002096 }
2097
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002098 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002099 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002100
Brian Salomona6948702018-06-01 15:33:20 -04002101 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002102 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002103 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002104 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002105 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002106 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002107
Brian Osman1348ed02018-09-12 09:08:39 -04002108 bool reattachStencil = false;
2109 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2110 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002111 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002112 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002113 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2114 reattachStencil = true;
2115 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2116 GR_GL_RENDERBUFFER, 0));
2117 }
2118
Chris Dalton76500e52019-09-05 02:13:05 -06002119 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002120 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002121
2122 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002123 auto* stencilAttachment =
Greg Danielc0d69152020-10-08 14:59:00 -04002124 static_cast<GrGLAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002125 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2126 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2127 }
2128
Brian Salomon26de56e2019-04-10 12:14:26 -04002129 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002130 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002131 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2132 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002133
Brian Salomone05ba5a2019-04-08 11:59:07 -04002134 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002135 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002136 }
2137 return true;
2138}
2139
2140bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002141 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2142 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002143 SkASSERT(surface);
2144
Brian Salomonb28cb682019-07-26 12:48:47 -04002145 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002146
Brian Salomon26de56e2019-04-10 12:14:26 -04002147 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2148 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002149
Brian Salomon1047a492019-07-02 12:25:21 -04002150 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002151 rowPixelWidth = width;
2152 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002153 SkASSERT(!(rowBytes % bytesPerPixel));
2154 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002155 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002156 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2157 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002158}
2159
Greg Daniel65476e02020-10-27 09:20:20 -04002160GrOpsRenderPass* GrGLGpu::onGetOpsRenderPass(
Greg Danielc0d69152020-10-08 14:59:00 -04002161 GrRenderTarget* rt,
2162 GrAttachment*,
2163 GrSurfaceOrigin origin,
2164 const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002165 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002166 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Greg Daniel9a18b082020-08-14 14:03:50 -04002167 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -04002168 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002169 if (!fCachedOpsRenderPass) {
John Stilesfbd050b2020-08-03 13:21:46 -04002170 fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002171 }
2172
Chris Daltonb50cc812019-10-14 16:29:21 -06002173 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002174 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002175}
2176
Brian Salomon1fabd512018-02-09 09:54:25 -05002177void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002178 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002179 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002180 this->didWriteToSurface(target, origin, &bounds);
2181}
bsalomon6ba6fa12015-03-04 11:57:37 -08002182
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002183void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2184 this->flushRenderTargetNoColorWrites(target);
2185 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002186}
2187
Brian Osman9aa30c62018-07-02 15:21:46 -04002188void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002189 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002190 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002191 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002192 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002193#ifdef SK_DEBUG
2194 // don't do this check in Chromium -- this is causing
2195 // lots of repeated command buffer flushes when the compositor is
2196 // rendering with Ganesh, which is really slow; even too slow for
2197 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002198 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002199 GrGLenum status;
2200 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2201 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2202 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2203 }
bsalomon160f24c2015-03-17 15:55:42 -07002204 }
egdanield803f272015-03-18 13:01:52 -07002205#endif
2206 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002207 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002208 }
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002209 if (this->caps()->workarounds().force_update_scissor_state_when_binding_fbo0 &&
2210 fBoundDrawFramebuffer == 0) {
2211 // The driver forgets the correct scissor state when using FBO 0.
2212 if (!fHWScissorSettings.fRect.isInvalid()) {
2213 const GrNativeRect& r = fHWScissorSettings.fRect;
2214 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
2215 }
2216 if (fHWScissorSettings.fEnabled == kYes_TriState) {
2217 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2218 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2219 } else if (fHWScissorSettings.fEnabled == kNo_TriState) {
2220 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
2221 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
2222 }
2223 }
brianosman64d094d2016-03-25 06:01:59 -07002224
brianosman35b784d2016-05-05 11:52:53 -07002225 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002226 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002227 }
Brian Salomon9b553272020-01-24 14:38:37 -05002228
2229 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2230 GrGLint format;
2231 GrGLint type;
2232 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2233 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2234 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2235 }
bsalomon083617b2016-02-12 12:10:14 -08002236}
bsalomona9909122016-01-23 10:41:40 -08002237
brianosman33f6b3f2016-06-02 05:49:21 -07002238void GrGLGpu::flushFramebufferSRGB(bool enable) {
2239 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2240 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2241 fHWSRGBFramebuffer = kYes_TriState;
2242 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2243 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2244 fHWSRGBFramebuffer = kNo_TriState;
2245 }
2246}
2247
Greg Danielacd66b42019-05-22 16:29:12 -04002248void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002249 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002250 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002251 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002252 fHWViewport = viewport;
2253 }
2254}
2255
Chris Dalton33db9fc2020-02-25 18:52:12 -07002256GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002257 fStats.incNumDraws();
2258
Chris Dalton2e7ed262020-02-21 15:17:59 -07002259 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2260 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2261 GL_CALL(Enable(GR_GL_CULL_FACE));
2262 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002263 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002264 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002265
Chris Dalton3809bab2017-06-13 10:55:06 -06002266 switch (primitiveType) {
2267 case GrPrimitiveType::kTriangles:
2268 return GR_GL_TRIANGLES;
2269 case GrPrimitiveType::kTriangleStrip:
2270 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002271 case GrPrimitiveType::kPoints:
2272 return GR_GL_POINTS;
2273 case GrPrimitiveType::kLines:
2274 return GR_GL_LINES;
2275 case GrPrimitiveType::kLineStrip:
2276 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002277 case GrPrimitiveType::kPatches:
2278 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002279 case GrPrimitiveType::kPath:
2280 SK_ABORT("non-mesh-based GrPrimitiveType");
2281 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002282 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002283 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002284}
2285
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002286void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002287 // Some extensions automatically resolves the texture when it is read.
2288 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2289
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002290 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002291 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2292 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2293 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2294 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002295
Chris Daltonc139d082019-09-26 14:04:24 -06002296 // make sure we go through flushRenderTarget() since we've modified
2297 // the bound DRAW FBO ID.
2298 fHWBoundRenderTargetUniqueID.makeInvalid();
2299 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2300 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002301 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2302 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002303 GrScissorState scissor(rt->dimensions());
2304 SkAssertResult(scissor.set(resolveRect));
2305 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002306 this->disableWindowRectangles();
2307 GL_CALL(ResolveMultisampleFramebuffer());
2308 } else {
2309 int l, b, r, t;
2310 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2311 this->glCaps().blitFramebufferSupportFlags()) {
2312 l = 0;
2313 b = 0;
2314 r = target->width();
2315 t = target->height();
2316 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002317 l = resolveRect.x();
2318 b = resolveRect.y();
2319 r = resolveRect.x() + resolveRect.width();
2320 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002321 }
Chris Daltonc139d082019-09-26 14:04:24 -06002322
2323 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002324 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002325 this->disableWindowRectangles();
2326 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 +00002327 }
2328}
2329
bsalomon@google.com411dad02012-06-05 20:24:20 +00002330namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002331
bsalomon@google.com411dad02012-06-05 20:24:20 +00002332
2333GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002334 static const GrGLenum gTable[kGrStencilOpCount] = {
2335 GR_GL_KEEP, // kKeep
2336 GR_GL_ZERO, // kZero
2337 GR_GL_REPLACE, // kReplace
2338 GR_GL_INVERT, // kInvert
2339 GR_GL_INCR_WRAP, // kIncWrap
2340 GR_GL_DECR_WRAP, // kDecWrap
2341 GR_GL_INCR, // kIncClamp
2342 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002343 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002344 static_assert(0 == (int)GrStencilOp::kKeep);
2345 static_assert(1 == (int)GrStencilOp::kZero);
2346 static_assert(2 == (int)GrStencilOp::kReplace);
2347 static_assert(3 == (int)GrStencilOp::kInvert);
2348 static_assert(4 == (int)GrStencilOp::kIncWrap);
2349 static_assert(5 == (int)GrStencilOp::kDecWrap);
2350 static_assert(6 == (int)GrStencilOp::kIncClamp);
2351 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002352 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2353 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002354}
2355
2356void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002357 const GrStencilSettings::Face& face,
2358 GrGLenum glFace) {
2359 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2360 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2361 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002362
cdalton93a379b2016-05-11 13:58:08 -07002363 GrGLint ref = face.fRef;
2364 GrGLint mask = face.fTestMask;
2365 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002366
2367 if (GR_GL_FRONT_AND_BACK == glFace) {
2368 // we call the combined func just in case separate stencil is not
2369 // supported.
2370 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2371 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002372 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002373 } else {
2374 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2375 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002376 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002377 }
2378}
John Stilesa6841be2020-08-06 14:11:56 -04002379} // namespace
bsalomon@google.comd302f142011-03-03 13:54:13 +00002380
Chris Dalton71713f62019-04-18 12:41:03 -06002381void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002382 if (stencilSettings.isDisabled()) {
2383 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002384 } else if (fHWStencilSettings != stencilSettings ||
2385 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002386 if (kYes_TriState != fHWStencilTestEnabled) {
2387 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002388
csmartdaltonc7d85332016-10-26 10:13:46 -07002389 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002390 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002391 if (!stencilSettings.isTwoSided()) {
2392 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2393 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002394 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002395 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2396 GR_GL_FRONT);
2397 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2398 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002399 }
joshualitta58fe352014-10-27 08:39:00 -07002400 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002401 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002402 }
2403}
2404
csmartdaltonc7d85332016-10-26 10:13:46 -07002405void GrGLGpu::disableStencil() {
2406 if (kNo_TriState != fHWStencilTestEnabled) {
2407 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002408
csmartdaltonc7d85332016-10-26 10:13:46 -07002409 fHWStencilTestEnabled = kNo_TriState;
2410 fHWStencilSettings.invalidate();
2411 }
2412}
2413
Chris Dalton4c56b032019-03-04 12:28:42 -07002414void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002415 // rt is only optional if useHWAA is false.
2416 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002417#ifdef SK_DEBUG
2418 if (useHWAA && rt->numSamples() <= 1) {
2419 SkASSERT(this->caps()->mixedSamplesSupport());
2420 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002421 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002422 }
2423#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002424
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002425 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002426 if (useHWAA) {
2427 if (kYes_TriState != fMSAAEnabled) {
2428 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2429 fMSAAEnabled = kYes_TriState;
2430 }
2431 } else {
2432 if (kNo_TriState != fMSAAEnabled) {
2433 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2434 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002435 }
2436 }
2437 }
2438}
2439
Chris Daltonce425af2019-12-16 10:39:03 -07002440void GrGLGpu::flushConservativeRasterState(bool enabled) {
2441 if (this->caps()->conservativeRasterSupport()) {
2442 if (enabled) {
2443 if (kYes_TriState != fHWConservativeRasterEnabled) {
2444 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2445 fHWConservativeRasterEnabled = kYes_TriState;
2446 }
2447 } else {
2448 if (kNo_TriState != fHWConservativeRasterEnabled) {
2449 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2450 fHWConservativeRasterEnabled = kNo_TriState;
2451 }
2452 }
2453 }
2454}
2455
Chris Dalton1215cda2019-12-17 21:44:04 -07002456void GrGLGpu::flushWireframeState(bool enabled) {
2457 if (this->caps()->wireframeSupport()) {
2458 if (this->caps()->wireframeMode() || enabled) {
2459 if (kYes_TriState != fHWWireframeEnabled) {
2460 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2461 fHWWireframeEnabled = kYes_TriState;
2462 }
2463 } else {
2464 if (kNo_TriState != fHWWireframeEnabled) {
2465 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2466 fHWWireframeEnabled = kNo_TriState;
2467 }
2468 }
2469 }
2470}
2471
Chris Daltonbbb3f642019-07-24 12:25:08 -04002472void GrGLGpu::flushBlendAndColorWrite(
2473 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2474 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2475 // We need to work around a driver bug by using a blend state that preserves the dst color,
2476 // rather than disabling color writes.
2477 GrXferProcessor::BlendInfo preserveDstBlend;
2478 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2479 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2480 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2481 return;
2482 }
bsalomonf7cc8772015-05-11 11:21:14 -07002483
cdalton8917d622015-05-06 13:40:21 -07002484 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002485 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2486 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002487
2488 // Any optimization to disable blending should have already been applied and
2489 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002490 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2491 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002492
egdanielb414f252014-07-29 13:15:47 -07002493 if (blendOff) {
2494 if (kNo_TriState != fHWBlendState.fEnabled) {
2495 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002496
Jim Van Verth1bef9792020-07-09 08:09:13 -04002497 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002498 // https://code.google.com/p/skia/issues/detail?id=3943
2499 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2500 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2501 SkASSERT(this->caps()->advancedBlendEquationSupport());
2502 // Set to any basic blending equation.
2503 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2504 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2505 fHWBlendState.fEquation = blend_equation;
2506 }
2507
egdanielb414f252014-07-29 13:15:47 -07002508 fHWBlendState.fEnabled = kNo_TriState;
2509 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002510 } else {
2511 if (kYes_TriState != fHWBlendState.fEnabled) {
2512 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002513
Chris Daltonbbb3f642019-07-24 12:25:08 -04002514 fHWBlendState.fEnabled = kYes_TriState;
2515 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002516
Chris Daltonbbb3f642019-07-24 12:25:08 -04002517 if (fHWBlendState.fEquation != equation) {
2518 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2519 fHWBlendState.fEquation = equation;
2520 }
cdalton8917d622015-05-06 13:40:21 -07002521
Chris Daltonbbb3f642019-07-24 12:25:08 -04002522 if (GrBlendEquationIsAdvanced(equation)) {
2523 SkASSERT(this->caps()->advancedBlendEquationSupport());
2524 // Advanced equations have no other blend state.
2525 return;
2526 }
cdalton8917d622015-05-06 13:40:21 -07002527
Chris Daltonbbb3f642019-07-24 12:25:08 -04002528 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2529 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2530 gXfermodeCoeff2Blend[dstCoeff]));
2531 fHWBlendState.fSrcCoeff = srcCoeff;
2532 fHWBlendState.fDstCoeff = dstCoeff;
2533 }
cdalton8917d622015-05-06 13:40:21 -07002534
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002535 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002536 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2537 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2538 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2539 fHWBlendState.fConstColor = blendConst;
2540 fHWBlendState.fConstColorValid = true;
2541 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002542 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002543 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002544
2545 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002546}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002547
Greg Daniel2c3398d2019-06-19 11:58:01 -04002548void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2549 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002550 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002551
reed856e9d92015-09-30 12:21:45 -07002552#ifdef SK_DEBUG
2553 if (!this->caps()->npotTextureTileSupport()) {
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002554 if (samplerState.isRepeatedX()) {
reed856e9d92015-09-30 12:21:45 -07002555 const int w = texture->width();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002556 SkASSERT(SkIsPow2(w));
2557 }
2558 if (samplerState.isRepeatedY()) {
reed856e9d92015-09-30 12:21:45 -07002559 const int h = texture->height();
Greg Daniel4fd41ff2020-10-22 11:07:28 -04002560 SkASSERT(SkIsPow2(h));
reed856e9d92015-09-30 12:21:45 -07002561 }
2562 }
2563#endif
2564
Robert Phillips294870f2016-11-11 12:38:40 -05002565 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002566 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002567 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002568 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002569 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002570 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002571 }
2572
Brian Salomone69b9ef2020-07-22 11:18:06 -04002573 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002574 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002575 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2576 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002577 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002578 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002579 }
bsalomonefd7d452014-10-23 14:17:46 -07002580
Brian Salomone2826ab2019-06-04 15:58:31 -04002581 auto timestamp = texture->parameters()->resetTimestamp();
2582 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002583 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2584 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomona0d913b2020-09-01 12:42:26 -04002585 if (this->glCaps().useSamplerObjects()) {
Brian Salomondc829942018-10-23 16:07:24 -04002586 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002587 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002588 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2589 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2590 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002591 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2592 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002593 this->setTextureUnit(unitIdx);
2594 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2595 newSamplerState = oldSamplerState;
2596 newSamplerState.fMinFilter = minFilter;
2597 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002598 }
2599 }
Brian Salomondc829942018-10-23 16:07:24 -04002600 } else {
Brian Salomona0d913b2020-09-01 12:42:26 -04002601 if (fSamplerObjectCache) {
2602 fSamplerObjectCache->unbindSampler(unitIdx);
2603 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002604 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2605 texture->parameters()->samplerOverriddenState();
2606 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002607
Brian Salomone69b9ef2020-07-22 11:18:06 -04002608 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2609 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002610 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002611
Brian Salomone2826ab2019-06-04 15:58:31 -04002612 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2613 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002614
2615 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002616 newSamplerState.fMinLOD = -1000.f;
2617 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002618
Brian Salomone2826ab2019-06-04 15:58:31 -04002619 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002620 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002621 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002622 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002623 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002624 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002625 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002626 }
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002627 if (this->glCaps().mipmapLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002629 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002631 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002632 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002633 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002634 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002635 }
2636 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002637 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002638 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002639 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002640 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002642 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002644 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002645 if (this->glCaps().clampToBorderSupport()) {
2646 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002647 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002648 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002649 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2650 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002651 }
2652 }
Brian Salomondc829942018-10-23 16:07:24 -04002653 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002654 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2655 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002656 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002657 newNonsamplerState.fSwizzleIsRGBA = true;
Brian Salomondc829942018-10-23 16:07:24 -04002658
Brian Salomone2826ab2019-06-04 15:58:31 -04002659 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2660 texture->parameters()->nonsamplerState();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002661 if (this->glCaps().textureSwizzleSupport()) {
2662 if (setAll || !oldNonsamplerState.fSwizzleIsRGBA) {
2663 static constexpr GrGLenum kRGBA[4] {
2664 GR_GL_RED,
2665 GR_GL_GREEN,
2666 GR_GL_BLUE,
2667 GR_GL_ALPHA
2668 };
Brian Salomondc829942018-10-23 16:07:24 -04002669 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002670 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon280fa3d2020-08-27 17:16:49 -04002671 static_assert(sizeof(kRGBA[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002672 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
Brian Salomon280fa3d2020-08-27 17:16:49 -04002673 reinterpret_cast<const GrGLint*>(kRGBA)));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002674 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002675 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
Brian Salomon280fa3d2020-08-27 17:16:49 -04002676 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, kRGBA[0]));
2677 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, kRGBA[1]));
2678 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, kRGBA[2]));
2679 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, kRGBA[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002680 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002681 }
2682 }
Brian Salomondc829942018-10-23 16:07:24 -04002683 // These are not supported in ES2 contexts
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002684 if (this->glCaps().mipmapLevelControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002685 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002686 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002687 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002688 this->setTextureUnit(unitIdx);
2689 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002690 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002691 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002692 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002693 this->setTextureUnit(unitIdx);
2694 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002695 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002696 }
Brian Salomon327955b2018-10-22 15:39:22 +00002697 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002698 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2699 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002700}
2701
Brian Salomon1f05d452019-02-08 12:33:08 -05002702void GrGLGpu::onResetTextureBindings() {
2703 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2704 GR_GL_TEXTURE_EXTERNAL};
2705 for (int i = 0; i < this->numTextureUnits(); ++i) {
2706 this->setTextureUnit(i);
2707 for (auto target : kTargets) {
2708 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2709 GL_CALL(BindTexture(target, 0));
2710 }
2711 }
2712 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2713 }
2714}
2715
Chris Dalton5a2f9622019-12-27 14:56:38 -07002716void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2717 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2718 if (fHWPatchVertexCount != count) {
2719 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2720 fHWPatchVertexCount = count;
2721 }
2722}
2723
egdaniel080e6732014-12-22 07:35:52 -08002724void GrGLGpu::flushColorWrite(bool writeColor) {
2725 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002726 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002727 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2728 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002729 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002730 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002731 } else {
2732 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002733 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002734 fHWWriteToColor = kYes_TriState;
2735 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002736 }
bsalomon3e791242014-12-17 13:43:13 -08002737}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002738
Brian Salomon07bc9a22020-12-02 13:37:16 -05002739void GrGLGpu::flushClearColor(std::array<float, 4> color) {
2740 GrGLfloat r = color[0], g = color[1], b = color[2], a = color[3];
Chris Dalton674f77a2019-09-30 20:49:39 -06002741 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2742 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2743 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2744 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2745 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2746 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002747 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2748 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2749 GL_CALL(ClearColor(r, g, b, a));
2750 fHWClearColor[0] = r;
2751 fHWClearColor[1] = g;
2752 fHWClearColor[2] = b;
2753 fHWClearColor[3] = a;
2754 }
2755}
2756
bsalomon861e1032014-12-16 07:33:49 -08002757void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002758 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002759 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002760 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002761 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002762 }
2763}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002764
Brian Salomond978b902019-02-07 15:09:18 -05002765void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002766 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002767 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002768 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2769 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2770 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002771 }
Brian Salomond978b902019-02-07 15:09:18 -05002772 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2773 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002774 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002775 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002776}
2777
Brian Salomone5e7eb12016-10-14 16:18:33 -04002778// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002779static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2780 const GrSurface* src,
2781 const SkIRect& srcRect,
2782 const SkIPoint& dstPoint,
2783 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002784 int dstSampleCnt = 0;
2785 int srcSampleCnt = 0;
2786 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002787 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002788 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002789 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002790 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002791 }
2792 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2793 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2794
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002795 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2796 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2797
Brian Salomone5e7eb12016-10-14 16:18:33 -04002798 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002799 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002800
Greg Daniel46cfbc62019-06-07 11:43:30 -04002801 GrTextureType dstTexType;
2802 GrTextureType* dstTexTypePtr = nullptr;
2803 GrTextureType srcTexType;
2804 GrTextureType* srcTexTypePtr = nullptr;
2805 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002806 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002807 dstTexTypePtr = &dstTexType;
2808 }
2809 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002810 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002811 srcTexTypePtr = &srcTexType;
2812 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002813
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002814 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2815 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002816 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002817}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002818
Brian Osmana9c8a052018-01-19 10:31:56 -05002819static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2820 // A RT has a separate MSAA renderbuffer if:
2821 // 1) It's multisampled
2822 // 2) We're using an extension with separate MSAA renderbuffers
2823 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002824 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002825}
2826
Greg Daniel46cfbc62019-06-07 11:43:30 -04002827static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002828 const GrGLCaps& caps) {
2829
bsalomon@google.comeb851172013-04-15 13:51:00 +00002830 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002831 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002832 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002833 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002834
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002835 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2836 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2837
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002838 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2839 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2840
Greg Daniel46cfbc62019-06-07 11:43:30 -04002841 GrTextureType dstTexType;
2842 GrTextureType* dstTexTypePtr = nullptr;
2843 GrTextureType srcTexType;
2844 GrTextureType* srcTexTypePtr = nullptr;
2845 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002846 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002847 dstTexTypePtr = &dstTexType;
2848 }
2849 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002850 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002851 srcTexTypePtr = &srcTexType;
2852 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002853
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002854 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2855 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002856}
2857
Greg Danielacd66b42019-05-22 16:29:12 -04002858// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002859void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002860 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002861 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002862 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002863 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002864 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2865 GrGLuint texID = texture->textureID();
2866 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002867 GrGLuint* tempFBOID;
2868 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002869
egdanield803f272015-03-18 13:01:52 -07002870 if (0 == *tempFBOID) {
2871 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002872 }
2873
Adrienne Walker4ee88512018-05-17 11:37:14 -07002874 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002875 GR_GL_CALL(
2876 this->glInterface(),
2877 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2878 if (mipLevel == 0) {
2879 texture->baseLevelWasBoundToFBO();
2880 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002881 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002882 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002883 }
egdaniel0f5f9672015-02-03 11:10:51 -08002884}
2885
Brian Salomond2a8ae22019-09-10 16:03:59 -04002886void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002887 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002888 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002889 SkASSERT(surface->asTexture());
2890 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2891 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2892 GR_GL_COLOR_ATTACHMENT0,
2893 textureTarget,
2894 0,
2895 0));
2896 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002897}
2898
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002899void GrGLGpu::onFBOChanged() {
Brian Salomonc1e5fb62020-12-08 16:11:46 -05002900 if (this->caps()->workarounds().flush_on_framebuffer_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002901 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002902 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002903#ifdef SK_DEBUG
2904 if (fIsExecutingCommandBuffer_DebugOnly) {
2905 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2906 "This will severely hurt performance.\n");
2907 }
2908#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002909}
2910
Adrienne Walker4ee88512018-05-17 11:37:14 -07002911void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2912 fStats.incRenderTargetBinds();
2913 GL_CALL(BindFramebuffer(target, fboid));
2914 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2915 fBoundDrawFramebuffer = fboid;
2916 }
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002917 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002918}
2919
Adrienne Walker4ee88512018-05-17 11:37:14 -07002920void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002921 // We're relying on the GL state shadowing being correct in the workaround code below so we
2922 // need to handle a dirty context.
2923 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002924 if (fboid == fBoundDrawFramebuffer &&
2925 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2926 // This workaround only applies to deleting currently bound framebuffers
2927 // on Adreno 420. Because this is a somewhat rare case, instead of
2928 // tracking all the attachments of every framebuffer instead just always
2929 // unbind all attachments.
2930 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2931 GR_GL_RENDERBUFFER, 0));
2932 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2933 GR_GL_RENDERBUFFER, 0));
2934 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2935 GR_GL_RENDERBUFFER, 0));
2936 }
2937
2938 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002939
2940 // Deleting the currently bound framebuffer rebinds to 0.
2941 if (fboid == fBoundDrawFramebuffer) {
2942 this->onFBOChanged();
2943 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002944}
2945
Greg Daniel46cfbc62019-06-07 11:43:30 -04002946bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002947 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002948 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2949 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2950 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002951 auto dstFormat = dst->backendFormat().asGLFormat();
2952 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002953 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002954 return true;
2955 }
2956 }
cblume61214052016-01-26 09:10:48 -08002957
Greg Daniel46cfbc62019-06-07 11:43:30 -04002958 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2959 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002960 return true;
2961 }
2962
Greg Daniel46cfbc62019-06-07 11:43:30 -04002963 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2964 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002965 }
2966
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002967 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002968 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002969 return true;
2970 }
bsalomon083617b2016-02-12 12:10:14 -08002971 }
2972
bsalomon6df86402015-06-01 10:41:49 -07002973 return false;
2974}
2975
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002976bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002977 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002978
2979 int progIdx = TextureToCopyProgramIdx(srcTex);
2980 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002981 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002982
2983 if (!fCopyProgramArrayBuffer) {
2984 static const GrGLfloat vdata[] = {
2985 0, 0,
2986 0, 1,
2987 1, 0,
2988 1, 1
2989 };
2990 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
2991 kStatic_GrAccessPattern, vdata);
2992 }
2993 if (!fCopyProgramArrayBuffer) {
2994 return false;
2995 }
2996
2997 SkASSERT(!fCopyPrograms[progIdx].fProgram);
2998 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
2999 if (!fCopyPrograms[progIdx].fProgram) {
3000 return false;
3001 }
3002
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003003 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003004 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003005 GrShaderVar::TypeModifier::Uniform);
3006 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3007 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3008 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3009 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003010
Greg Daniel9e3169b2019-06-06 14:38:17 -04003011 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003012 if (shaderCaps->noperspectiveInterpolationSupport()) {
3013 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3014 vshaderTxt.appendf("#extension %s : require\n", extension);
3015 }
3016 vTexCoord.addModifier("noperspective");
3017 }
3018
3019 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3020 vshaderTxt.append(";");
3021 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3022 vshaderTxt.append(";");
3023 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3024 vshaderTxt.append(";");
3025 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3026 vshaderTxt.append(";");
3027
3028 vshaderTxt.append(
3029 "// Copy Program VS\n"
3030 "void main() {"
3031 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3032 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3033 " sk_Position.zw = half2(0, 1);"
3034 "}"
3035 );
3036
Greg Daniel9e3169b2019-06-06 14:38:17 -04003037 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003038 if (shaderCaps->noperspectiveInterpolationSupport()) {
3039 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3040 fshaderTxt.appendf("#extension %s : require\n", extension);
3041 }
3042 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003043 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003044 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3045 fshaderTxt.append(";");
3046 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3047 fshaderTxt.append(";");
3048 fshaderTxt.appendf(
3049 "// Copy Program FS\n"
3050 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003051 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003052 "}"
3053 );
3054
3055 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3056 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3057 SkSL::Program::Settings settings;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003058 SkSL::String glsl;
Brian Osman79719262020-11-23 14:54:33 -05003059 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::Program::kVertex_Kind,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003060 sksl, settings, &glsl, errorHandler);
3061 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3062 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3063 SkASSERT(program->fInputs.isEmpty());
3064
3065 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman79719262020-11-23 14:54:33 -05003066 program = GrSkSLtoGLSL(this, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003067 errorHandler);
3068 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3069 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3070 errorHandler);
3071 SkASSERT(program->fInputs.isEmpty());
3072
3073 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3074
3075 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3076 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3077 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3078 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3079 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3080 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3081
3082 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3083
3084 GL_CALL(DeleteShader(vshader));
3085 GL_CALL(DeleteShader(fshader));
3086
3087 return true;
3088}
3089
brianosman33f6b3f2016-06-02 05:49:21 -07003090bool GrGLGpu::createMipmapProgram(int progIdx) {
3091 const bool oddWidth = SkToBool(progIdx & 0x2);
3092 const bool oddHeight = SkToBool(progIdx & 0x1);
3093 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3094
Brian Salomon1edc5b92016-11-29 13:43:46 -05003095 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003096
3097 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3098 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3099 if (!fMipmapPrograms[progIdx].fProgram) {
3100 return false;
3101 }
3102
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003103 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003104 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003105 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003106 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003107 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003108 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003109 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003110 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3111 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3112 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3113 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003114 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003115 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003116
Ethan Nicholasfc994162019-06-06 10:04:27 -04003117 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003118 if (shaderCaps->noperspectiveInterpolationSupport()) {
3119 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003120 vshaderTxt.appendf("#extension %s : require\n", extension);
3121 }
3122 vTexCoords[0].addModifier("noperspective");
3123 vTexCoords[1].addModifier("noperspective");
3124 vTexCoords[2].addModifier("noperspective");
3125 vTexCoords[3].addModifier("noperspective");
3126 }
3127
Brian Salomon1edc5b92016-11-29 13:43:46 -05003128 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003129 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003130 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003131 vshaderTxt.append(";");
3132 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003133 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003134 vshaderTxt.append(";");
3135 }
3136
3137 vshaderTxt.append(
3138 "// Mipmap Program VS\n"
3139 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003140 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3141 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003142 );
3143
3144 // Insert texture coordinate computation:
3145 if (oddWidth && oddHeight) {
3146 vshaderTxt.append(
3147 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003148 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3149 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003150 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3151 );
3152 } else if (oddWidth) {
3153 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003154 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3155 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003156 );
3157 } else if (oddHeight) {
3158 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003159 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3160 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003161 );
3162 } else {
3163 vshaderTxt.append(
3164 " v_texCoord0 = a_vertex.xy;"
3165 );
3166 }
3167
3168 vshaderTxt.append("}");
3169
Ethan Nicholasfc994162019-06-06 10:04:27 -04003170 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003171 if (shaderCaps->noperspectiveInterpolationSupport()) {
3172 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003173 fshaderTxt.appendf("#extension %s : require\n", extension);
3174 }
3175 }
brianosman33f6b3f2016-06-02 05:49:21 -07003176 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003177 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003178 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003179 fshaderTxt.append(";");
3180 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003181 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003182 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003183 fshaderTxt.append(
3184 "// Mipmap Program FS\n"
3185 "void main() {"
3186 );
3187
3188 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003189 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003190 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3191 " sample(u_texture, v_texCoord1) + "
3192 " sample(u_texture, v_texCoord2) + "
3193 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003194 );
3195 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003196 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003197 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3198 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003199 );
3200 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003201 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003202 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003203 );
3204 }
3205
3206 fshaderTxt.append("}");
3207
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003208 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003209 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003210 SkSL::Program::Settings settings;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003211 SkSL::String glsl;
Brian Osman79719262020-11-23 14:54:33 -05003212 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(this, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003213 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003214 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003215 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003216 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003217
Brian Osman6c431d52019-04-15 16:31:54 -04003218 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman79719262020-11-23 14:54:33 -05003219 program = GrSkSLtoGLSL(this, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003220 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003221 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003222 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003223 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003224 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003225
3226 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3227
3228 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3229 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3230 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3231 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3232
3233 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3234
3235 GL_CALL(DeleteShader(vshader));
3236 GL_CALL(DeleteShader(fshader));
3237
3238 return true;
3239}
3240
Greg Daniel46cfbc62019-06-07 11:43:30 -04003241bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003242 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003243 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3244 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3245 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3246 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003247 return false;
3248 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003249 int progIdx = TextureToCopyProgramIdx(srcTex);
3250 if (!dstRT) {
3251 SkASSERT(dstTex);
3252 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3253 return false;
3254 }
3255 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003256 if (!fCopyPrograms[progIdx].fProgram) {
3257 if (!this->createCopyProgram(srcTex)) {
3258 SkDebugf("Failed to create copy program.\n");
3259 return false;
3260 }
3261 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003262 int w = srcRect.width();
3263 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003264 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003265 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003266 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 this->flushViewport(dst->width(), dst->height());
3268 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003269 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003270 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003271 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003272 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3273 attribs->enableVertexArrays(this, 1);
3274 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3275 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 // dst rect edges in NDC (-1 to 1)
3277 int dw = dst->width();
3278 int dh = dst->height();
3279 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3280 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3281 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3282 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003283 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3284 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3285 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3286 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3287 int sw = src->width();
3288 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003289 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003290 // src rect edges in normalized texture space (0 to 1)
3291 sx0 /= sw;
3292 sx1 /= sw;
3293 sy0 /= sh;
3294 sy1 /= sh;
3295 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003296 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3297 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3298 sx1 - sx0, sy1 - sy0, sx0, sy0));
3299 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003300 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003302 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003303 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003304 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003305 this->disableWindowRectangles();
3306 this->disableStencil();
3307 if (this->glCaps().srgbWriteControl()) {
3308 this->flushFramebufferSRGB(true);
3309 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003310 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003311 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003312 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3313 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003314 return true;
3315}
3316
Greg Daniel46cfbc62019-06-07 11:43:30 -04003317void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003318 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003319 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003320 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003321 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003322 SkASSERT(dstTex);
3323 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003324 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003325
Brian Salomond978b902019-02-07 15:09:18 -05003326 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003327 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003328 dstPoint.fX, dstPoint.fY,
3329 srcRect.fLeft, srcRect.fTop,
3330 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003331 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003332 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3333 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003334 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3335 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003336}
3337
Greg Daniel46cfbc62019-06-07 11:43:30 -04003338bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003339 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003340 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003341 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3342 srcRect.width(), srcRect.height());
3343 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003344 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003345 return false;
mtklein404b3b22015-05-18 09:29:10 -07003346 }
bsalomon5df6fee2015-05-18 06:26:15 -07003347 }
bsalomon6df86402015-06-01 10:41:49 -07003348
Brian Salomond2a8ae22019-09-10 16:03:59 -04003349 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3350 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003351 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003352 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003353
3354 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003355 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003356 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003357
Greg Daniel46cfbc62019-06-07 11:43:30 -04003358 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3359 srcRect.fTop,
3360 srcRect.fRight,
3361 srcRect.fBottom,
3362 dstRect.fLeft,
3363 dstRect.fTop,
3364 dstRect.fRight,
3365 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003366 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003367 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3368 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003369
3370 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3371 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003372 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003373}
3374
Brian Salomon930f9392018-06-20 16:25:26 -04003375bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3376 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003377 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003378 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003379 return false;
3380 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003381 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003382 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3383 // Uses draw calls to do a series of downsample operations to successive mips.
3384
3385 // The manual approach requires the ability to limit which level we're sampling and that the
3386 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003387 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003388 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003389 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003390 GL_CALL(GenerateMipmap(glTex->target()));
3391 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003392 }
3393
brianosman33f6b3f2016-06-02 05:49:21 -07003394 int width = texture->width();
3395 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003396 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003397 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003398
3399 // Create (if necessary), then bind temporary FBO:
3400 if (0 == fTempDstFBOID) {
3401 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3402 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003403 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003404 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003405
3406 // Bind the texture, to get things configured for filtering.
3407 // We'll be changing our base level further below:
3408 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003409 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003410 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003411
3412 // Vertex data:
3413 if (!fMipmapProgramArrayBuffer) {
3414 static const GrGLfloat vdata[] = {
3415 0, 0,
3416 0, 1,
3417 1, 0,
3418 1, 1
3419 };
Brian Salomonae64c192019-02-05 09:41:37 -05003420 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003421 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003422 }
3423 if (!fMipmapProgramArrayBuffer) {
3424 return false;
3425 }
3426
3427 fHWVertexArrayState.setVertexArrayID(this, 0);
3428
3429 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003430 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003431 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003432 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003433
3434 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003435 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003436 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003437 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003438 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003439 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003440
3441 // Do all the blits:
3442 width = texture->width();
3443 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003444
brianosman33f6b3f2016-06-02 05:49:21 -07003445 for (GrGLint level = 1; level < levelCount; ++level) {
3446 // Get and bind the program for this particular downsample (filter shape can vary):
3447 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3448 if (!fMipmapPrograms[progIdx].fProgram) {
3449 if (!this->createMipmapProgram(progIdx)) {
3450 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003451 // Invalidate all params to cover base level change in a previous iteration.
3452 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003453 return false;
3454 }
3455 }
Brian Salomon802cb312018-06-08 18:05:20 -04003456 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003457
3458 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3459 const float invWidth = 1.0f / width;
3460 const float invHeight = 1.0f / height;
3461 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3462 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3463 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3464
3465 // Only sample from previous mip
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003466 SkASSERT(this->glCaps().mipmapLevelControlSupport());
brianosman33f6b3f2016-06-02 05:49:21 -07003467 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3468
Brian Salomon930f9392018-06-20 16:25:26 -04003469 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3470 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003471
Brian Osman788b9162020-02-07 10:36:46 -05003472 width = std::max(1, width / 2);
3473 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003474 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003475
3476 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3477 }
3478
3479 // Unbind:
3480 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3481 GR_GL_TEXTURE_2D, 0, 0));
3482
Brian Salomon930f9392018-06-20 16:25:26 -04003483 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003484 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3485 // We drew the 2nd to last level into the last level.
3486 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3487 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003488
brianosman33f6b3f2016-06-02 05:49:21 -07003489 return true;
3490}
3491
Chris Daltond7291ba2019-03-07 14:17:03 -07003492void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003493 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3494 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003495
3496 int effectiveSampleCnt;
3497 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003498 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003499
3500 sampleLocations->reset(effectiveSampleCnt);
3501 for (int i = 0; i < effectiveSampleCnt; ++i) {
3502 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3503 }
3504}
3505
cdalton231c5fd2015-05-13 12:35:36 -07003506void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003507 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003508 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003509 case kTexture_GrXferBarrierType: {
3510 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003511 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003512 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3513 // The render target uses separate storage so no need for glTextureBarrier.
3514 // FIXME: The render target will resolve automatically when its texture is bound,
3515 // but we could resolve only the bounds that will be read if we do it here instead.
3516 return;
3517 }
cdalton9954bc32015-04-29 14:17:00 -07003518 SkASSERT(this->caps()->textureBarrierSupport());
3519 GL_CALL(TextureBarrier());
3520 return;
cdalton231c5fd2015-05-13 12:35:36 -07003521 }
cdalton8917d622015-05-06 13:40:21 -07003522 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003523 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003524 this->caps()->blendEquationSupport());
3525 GL_CALL(BlendBarrier());
3526 return;
bsalomoncb02b382015-08-12 11:14:50 -07003527 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003528 }
3529}
3530
Chris Daltone1196c52019-12-28 14:31:09 -07003531void GrGLGpu::insertManualFramebufferBarrier() {
3532 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3533 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3534}
3535
Brian Salomon85c3d682019-11-04 15:04:54 -05003536GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003537 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003538 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003539 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003540 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003541 // We don't support protected textures in GL.
3542 if (isProtected == GrProtected::kYes) {
3543 return {};
3544 }
3545
Brian Salomon8a375832018-03-14 10:21:40 -04003546 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003547
Brian Salomond4764a12019-08-08 12:08:24 -04003548 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003549 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003550 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003551 }
3552
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003553 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003554 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003555 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003556 }
3557
Robert Phillipsd34691b2019-09-24 13:38:43 -04003558 // Compressed formats go through onCreateCompressedBackendTexture
3559 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3560
Greg Daniel15500642019-06-27 03:05:55 +00003561 GrGLTextureInfo info;
3562 GrGLTextureParameters::SamplerOverriddenState initialState;
3563
Greg Danield51fa2f2020-01-22 16:53:38 -05003564 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003565 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003566 }
Brian Salomon0f396992020-06-19 19:51:21 -04003567 switch (format.textureType()) {
3568 case GrTextureType::kNone:
3569 case GrTextureType::kExternal:
3570 return {};
3571 case GrTextureType::k2D:
3572 info.fTarget = GR_GL_TEXTURE_2D;
3573 break;
3574 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003575 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003576 return {};
3577 }
3578 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3579 break;
3580 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003581 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003582 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3583 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003584 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003585 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003586 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003587
Brian Salomon85c3d682019-11-04 15:04:54 -05003588 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003589 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003590
Brian Salomone2826ab2019-06-04 15:58:31 -04003591 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003592 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003593 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3594 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003595
Brian Salomon85c3d682019-11-04 15:04:54 -05003596 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3597 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003598}
3599
Greg Daniel16032b32020-05-06 15:31:10 -04003600bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3601 sk_sp<GrRefCntedCallback> finishedCallback,
3602 const BackendTextureData* data) {
3603 GrGLTextureInfo info;
3604 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3605
3606 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003607 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003608 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003609 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003610 }
3611
3612 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3613
Brian Salomon22558932020-05-15 14:46:34 -04003614 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003615
Brian Salomonc3f49432020-06-03 10:21:50 -04003616 // 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 -04003617 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003618 if (numMipLevels && this->glCaps().mipmapLevelControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003619 auto params = backendTexture.getGLTextureParams();
3620 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3621 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003622 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003623 nonsamplerState.fBaseMipMapLevel = 0;
3624 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003625 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003626 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003627 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3628 }
3629 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3630 }
3631
Greg Daniel16032b32020-05-06 15:31:10 -04003632 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003633 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003634 if (data->type() == BackendTextureData::Type::kPixmaps) {
3635 SkTDArray<GrMipLevel> texels;
3636 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3637 texels.append(numMipLevels);
3638 for (int i = 0; i < numMipLevels; ++i) {
3639 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3640 }
Brian Salomon22558932020-05-15 14:46:34 -04003641 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3642 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3643 info.fTarget, dstRect, colorType, texels.begin(),
3644 texels.count());
3645 } else if (data->type() == BackendTextureData::Type::kColor) {
3646 uint32_t levelMask = (1 << numMipLevels) - 1;
3647 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3648 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003649 }
3650
3651 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003652 this->bindTextureToScratchUnit(info.fTarget, 0);
3653 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003654}
3655
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003656void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003657 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3658
3659 GrGLTextureInfo info;
3660 if (tex.getGLTextureInfo(&info)) {
3661 GL_CALL(DeleteTextures(1, &info.fID));
3662 }
3663}
3664
Robert Phillips3bce1f72020-05-12 12:41:58 -04003665bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3666 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3667
3668 Stats::ProgramCacheResult stat;
3669
3670 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3671 if (!tmp) {
3672 return false;
3673 }
3674
3675 return stat != Stats::ProgramCacheResult::kHit;
3676}
3677
Robert Phillipsf0ced622019-05-16 09:06:25 -04003678#if GR_TEST_UTILS
3679
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003680bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003681 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003682
Greg Daniel52e16d92018-04-10 09:34:07 -04003683 GrGLTextureInfo info;
3684 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003685 return false;
3686 }
3687
3688 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003689 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003690
3691 return (GR_GL_TRUE == result);
3692}
3693
Brian Salomon72c7b982020-10-06 10:07:38 -04003694GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
3695 GrColorType colorType,
Brian Salomonf9b00422020-10-08 16:00:14 -04003696 int sampleCnt,
3697 GrProtected isProtected) {
Brian Salomon72c7b982020-10-06 10:07:38 -04003698 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
3699 dimensions.height() > this->caps()->maxRenderTargetSize()) {
3700 return {};
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003701 }
Brian Salomonf9b00422020-10-08 16:00:14 -04003702 if (isProtected == GrProtected::kYes) {
3703 return {};
3704 }
3705
Brian Salomon8a375832018-03-14 10:21:40 -04003706 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003707 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -04003708 sampleCnt = this->glCaps().getRenderTargetSampleCount(sampleCnt, format);
3709 if (!sampleCnt) {
Brian Salomonf865b052018-03-09 09:01:53 -05003710 return {};
3711 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003712 // We make a texture instead of a render target if we're using a
3713 // "multisampled_render_to_texture" style extension or have a BGRA format that
3714 // is allowed for textures but not render buffer internal formats.
3715 bool useTexture = false;
3716 if (sampleCnt > 1 && !this->glCaps().usesMSAARenderBuffers()) {
3717 useTexture = true;
3718 } else if (format == GrGLFormat::kBGRA8 &&
3719 this->glCaps().getRenderbufferInternalFormat(GrGLFormat::kBGRA8) != GR_GL_BGRA8) {
3720 // We have a BGRA extension that doesn't support BGRA render buffers. We can use a texture
3721 // unless we've been asked for MSAA. Note we already checked above for render-to-
3722 // multisampled-texture style extensions.
3723 if (sampleCnt > 1) {
3724 return {};
3725 }
3726 useTexture = true;
3727 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003728 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003729 if (sFormatIdx < 0) {
3730 return {};
3731 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003732 GrGLuint colorID = 0;
3733 GrGLuint stencilID = 0;
Brian Salomon72c7b982020-10-06 10:07:38 -04003734 GrGLFramebufferInfo info;
3735 info.fFBOID = 0;
3736 info.fFormat = GrGLFormatToEnum(format);
3737
3738 auto deleteIDs = [&](bool saveFBO = false) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003739 if (colorID) {
3740 if (useTexture) {
3741 GL_CALL(DeleteTextures(1, &colorID));
3742 } else {
3743 GL_CALL(DeleteRenderbuffers(1, &colorID));
3744 }
Brian Salomonf865b052018-03-09 09:01:53 -05003745 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003746 if (stencilID) {
3747 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003748 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003749 if (!saveFBO && info.fFBOID) {
3750 this->deleteFramebuffer(info.fFBOID);
3751 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003752 };
3753
3754 if (useTexture) {
3755 GL_CALL(GenTextures(1, &colorID));
3756 } else {
3757 GL_CALL(GenRenderbuffers(1, &colorID));
3758 }
3759 GL_CALL(GenRenderbuffers(1, &stencilID));
3760 if (!stencilID || !colorID) {
3761 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003762 return {};
3763 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003764
Brian Salomonf865b052018-03-09 09:01:53 -05003765 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3766 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003767 deleteIDs();
3768 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003769 }
3770
3771 this->invalidateBoundRenderTarget();
3772
Adrienne Walker4ee88512018-05-17 11:37:14 -07003773 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003774 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003775 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon72c7b982020-10-06 10:07:38 -04003776 colorID = this->createTexture(dimensions, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
Brian Salomon0f396992020-06-19 19:51:21 -04003777 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003778 if (!colorID) {
3779 deleteIDs();
3780 return {};
3781 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003782 if (sampleCnt == 1) {
3783 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3784 GR_GL_TEXTURE_2D, colorID, 0));
3785 } else {
3786 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3787 GR_GL_TEXTURE_2D, colorID, 0, sampleCnt));
3788 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003789 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003790 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003791 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomon72c7b982020-10-06 10:07:38 -04003792 if (sampleCnt == 1) {
3793 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, dimensions.width(),
3794 dimensions.height()));
3795 } else {
3796 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt, renderBufferFormat,
3797 dimensions.width(), dimensions.height())) {
3798 deleteIDs();
3799 return {};
3800 }
3801 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003802 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3803 GR_GL_RENDERBUFFER, colorID));
3804 }
3805 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003806 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx];
Brian Salomon72c7b982020-10-06 10:07:38 -04003807 if (sampleCnt == 1) {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003808 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, GrGLFormatToEnum(stencilBufferFormat),
3809 dimensions.width(), dimensions.height()));
Brian Salomon72c7b982020-10-06 10:07:38 -04003810 } else {
Greg Daniel8ade5e82020-10-07 13:09:48 -04003811 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt,
3812 GrGLFormatToEnum(stencilBufferFormat),
Brian Salomon72c7b982020-10-06 10:07:38 -04003813 dimensions.width(), dimensions.height())) {
3814 deleteIDs();
3815 return {};
3816 }
3817 }
Brian Salomonf865b052018-03-09 09:01:53 -05003818 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003819 stencilID));
Greg Daniel8ade5e82020-10-07 13:09:48 -04003820 if (GrGLFormatIsPackedDepthStencil(this->glCaps().stencilFormats()[sFormatIdx])) {
Brian Salomonf865b052018-03-09 09:01:53 -05003821 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003822 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003823 }
3824
Brian Salomon93348dd2018-08-29 12:56:23 -04003825 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3826 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3827 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3828 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003829 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon72c7b982020-10-06 10:07:38 -04003830 deleteIDs(/* saveFBO = */ true);
Brian Salomonf865b052018-03-09 09:01:53 -05003831
Adrienne Walker4ee88512018-05-17 11:37:14 -07003832 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003833 GrGLenum status;
3834 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3835 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003836 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003837 return {};
3838 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003839
Greg Daniel8ade5e82020-10-07 13:09:48 -04003840 auto stencilBits = SkToInt(GrGLFormatStencilBits(this->glCaps().stencilFormats()[sFormatIdx]));
Robert Phillips62221e72019-07-24 15:07:38 -04003841
Brian Salomon72c7b982020-10-06 10:07:38 -04003842 GrBackendRenderTarget beRT = GrBackendRenderTarget(dimensions.width(), dimensions.height(),
3843 sampleCnt, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003844 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003845 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003846}
3847
3848void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003849 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003850 GrGLFramebufferInfo info;
3851 if (backendRT.getGLFramebufferInfo(&info)) {
3852 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003853 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003854 }
3855 }
joshualitt8fd844f2015-12-02 13:36:47 -08003856}
3857
Greg Daniel26b50a42018-03-08 09:49:58 -05003858void GrGLGpu::testingOnly_flushGpuAndSync() {
3859 GL_CALL(Finish());
3860}
Brian Salomonf865b052018-03-09 09:01:53 -05003861#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003862
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003863///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003864
cdaltone2e71c22016-04-07 18:13:29 -07003865GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003866 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003867 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003868 GrGLAttribArrayState* attribState;
3869
cdaltone2e71c22016-04-07 18:13:29 -07003870 if (gpu->glCaps().isCoreProfile()) {
3871 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003872 GrGLuint arrayID;
3873 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3874 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003875 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003876 }
cdaltone2e71c22016-04-07 18:13:29 -07003877 if (ibuf) {
3878 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003879 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003880 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003881 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003882 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003883 if (ibuf) {
3884 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003885 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003886 } else {
3887 this->setVertexArrayID(gpu, 0);
3888 }
3889 int attrCount = gpu->glCaps().maxVertexAttributes();
3890 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3891 fDefaultVertexArrayAttribState.resize(attrCount);
3892 }
3893 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003894 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003895 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003896}
bsalomone179a912016-01-20 06:18:10 -08003897
Greg Danielfe159622020-04-10 17:43:51 +00003898void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3899 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003900 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003901}
3902
Greg Daniel78be37f2020-04-14 16:40:35 -04003903void GrGLGpu::flush(FlushType flushType) {
3904 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3905 GL_CALL(Flush());
3906 fNeedsGLFlush = false;
3907 }
3908}
3909
Greg Danielfe159622020-04-10 17:43:51 +00003910bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3911 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003912 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003913 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003914 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003915 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003916 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003917 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003918 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003919 if (!this->glCaps().skipErrorChecks()) {
3920 this->clearErrorsAndCheckForOOM();
3921 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003922 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003923}
3924
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003925void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3926 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3927 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3928 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003929}
3930
Greg Daniel6be35232017-03-01 17:01:09 -05003931GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003932 if (!this->caps()->fenceSyncSupport()) {
3933 return 0;
3934 }
Greg Daniel6be35232017-03-01 17:01:09 -05003935 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003936 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3937 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3938 GrGLuint fence = 0;
3939 GL_CALL(GenFences(1, &fence));
3940 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3941 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3942 } else {
3943 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3944 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003945 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003946 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003947 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003948}
3949
Brian Salomonb0d8b762019-05-06 16:58:22 -04003950bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003951 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3952 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3953 if (!timeout) {
3954 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003955 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003956 }
3957 GrGLboolean result;
3958 GL_CALL_RET(result, TestFence(nvFence));
3959 return result == GR_GL_TRUE;
3960 }
3961 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3962 // If this really becomes necessary we could poll TestFence().
3963 // FinishFence always flushes so no need to check flush param.
3964 GL_CALL(FinishFence(nvFence));
3965 return true;
3966 } else {
3967 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3968 GrGLenum result;
3969 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3970 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3971 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003972}
3973
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003974bool GrGLGpu::waitFence(GrFence fence) {
3975 if (!this->caps()->fenceSyncSupport()) {
3976 return true;
3977 }
3978 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003979}
3980
3981void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003982 if (this->caps()->fenceSyncSupport()) {
3983 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3984 }
Greg Daniel6be35232017-03-01 17:01:09 -05003985}
3986
Greg Daniel301015c2019-11-18 14:06:46 -05003987std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003988 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003989 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003990}
3991
Greg Daniel301015c2019-11-18 14:06:46 -05003992std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3993 const GrBackendSemaphore& semaphore,
3994 GrResourceProvider::SemaphoreWrapType wrapType,
3995 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003996 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003997 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3998}
3999
Greg Daniel301015c2019-11-18 14:06:46 -05004000void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004001 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004002 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004003
Greg Daniel48661b82018-01-22 16:11:35 -05004004 GrGLsync sync;
4005 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4006 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04004007 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05004008}
4009
Greg Daniel301015c2019-11-18 14:06:46 -05004010void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004011 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004012 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004013
4014 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4015}
4016
Brian Salomonb0d8b762019-05-06 16:58:22 -04004017void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004018 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004019}
4020
Brian Salomon24069eb2020-06-24 10:19:52 -04004021void GrGLGpu::clearErrorsAndCheckForOOM() {
4022 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4023}
4024
4025GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4026#if GR_GL_CHECK_ERROR
4027 if (this->glInterface()->checkAndResetOOMed()) {
4028 this->setOOMed();
4029 }
4030#endif
4031 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4032 if (error == GR_GL_OUT_OF_MEMORY) {
4033 this->setOOMed();
4034 }
4035 return error;
4036}
4037
Greg Daniel6be35232017-03-01 17:01:09 -05004038void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004039 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4040 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4041 GL_CALL(DeleteFences(1, &nvFence));
4042 } else {
4043 GL_CALL(DeleteSync(sync));
4044 }
jvanverth84741b32016-09-30 08:39:02 -07004045}
Brian Osman13dddce2017-05-09 13:19:50 -04004046
Greg Daniel301015c2019-11-18 14:06:46 -05004047std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004048 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004049 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004050 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004051 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004052 // 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 -04004053 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004054
4055 return semaphore;
4056}
Robert Phillips646e4292017-06-13 12:44:56 -04004057
4058int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004059 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004060 case kTexture2DSampler_GrSLType:
4061 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004062 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004063 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004064 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004065 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004066 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004067 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004068 }
4069}
Brian Osman71a18892017-08-10 10:23:25 -04004070
Kevin Lubickf4def342018-10-04 12:52:50 -04004071#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004072#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004073void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4074 // We are called by the base class, which has already called beginObject(). We choose to nest
4075 // all of our caps information in a named sub-object.
4076 writer->beginObject("GL GPU");
4077
4078 const GrGLubyte* str;
4079 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4080 writer->appendString("GL_VERSION", (const char*)(str));
4081 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4082 writer->appendString("GL_RENDERER", (const char*)(str));
4083 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4084 writer->appendString("GL_VENDOR", (const char*)(str));
4085 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4086 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4087
4088 writer->appendName("extensions");
4089 glInterface()->fExtensions.dumpJSON(writer);
4090
4091 writer->endObject();
4092}
Kevin Lubickf4def342018-10-04 12:52:50 -04004093#endif