blob: a766cac71feb1d2e819f870e7e9822974373c8c9 [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"
Brian Osman8518f2e2019-05-01 14:13:41 -040024#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040026#include "src/gpu/GrDataUtils.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/gl/GrGLBuffer.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
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());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000355
Brian Salomond978b902019-02-07 15:09:18 -0500356 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000357
Brian Salomonae64c192019-02-05 09:41:37 -0500358 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
359 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600360 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500361 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500362 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
363 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
364 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
365 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700366 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500367 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
368 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700369 }
Brian Salomonae64c192019-02-05 09:41:37 -0500370 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400371 fHWBufferState[i].invalidate();
372 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600373 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700374
375 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400376 fPathRendering = std::make_unique<GrGLPathRendering>(this);
cdaltone2e71c22016-04-07 18:13:29 -0700377 }
378
Brian Salomona0d913b2020-09-01 12:42:26 -0400379 if (this->glCaps().useSamplerObjects()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400380 fSamplerObjectCache = std::make_unique<SamplerObjectCache>(this);
Brian Salomondc829942018-10-23 16:07:24 -0400381 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000382}
383
bsalomon861e1032014-12-16 07:33:49 -0800384GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700385 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
386 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800387 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700388 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700389 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800390
Chris Dalton20e7a532020-02-28 15:37:53 +0000391 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400392 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000393 // detach the current program so there is no confusion on OpenGL's part
394 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000395 GL_CALL(UseProgram(0));
396 }
397
Brian Salomon43f8bf02017-10-18 08:33:29 -0400398 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700399 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800400 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400401 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700402 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800403 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400404 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700405 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800406 }
egdaniel0f5f9672015-02-03 11:10:51 -0800407
bsalomon7ea33f52015-11-22 14:51:00 -0800408 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
409 if (0 != fCopyPrograms[i].fProgram) {
410 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
411 }
bsalomon6df86402015-06-01 10:41:49 -0700412 }
bsalomon6dea83f2015-12-03 12:58:06 -0800413
brianosman33f6b3f2016-06-02 05:49:21 -0700414 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
415 if (0 != fMipmapPrograms[i].fProgram) {
416 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
417 }
418 }
419
Brian Salomondc829942018-10-23 16:07:24 -0400420 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500421
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400422 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700423}
424
bsalomon6e2aad42016-04-01 11:54:31 -0700425void GrGLGpu::disconnect(DisconnectType type) {
426 INHERITED::disconnect(type);
427 if (DisconnectType::kCleanup == type) {
428 if (fHWProgramID) {
429 GL_CALL(UseProgram(0));
430 }
431 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700432 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700433 }
434 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700435 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700436 }
437 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700438 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700439 }
bsalomon6e2aad42016-04-01 11:54:31 -0700440 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
441 if (fCopyPrograms[i].fProgram) {
442 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
443 }
444 }
brianosman33f6b3f2016-06-02 05:49:21 -0700445 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
446 if (fMipmapPrograms[i].fProgram) {
447 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
448 }
449 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400450
Brian Salomondc829942018-10-23 16:07:24 -0400451 if (fSamplerObjectCache) {
452 fSamplerObjectCache->release();
453 }
bsalomon6e2aad42016-04-01 11:54:31 -0700454 } else {
455 if (fProgramCache) {
456 fProgramCache->abandon();
457 }
Brian Salomondc829942018-10-23 16:07:24 -0400458 if (fSamplerObjectCache) {
459 fSamplerObjectCache->abandon();
460 }
bsalomon6e2aad42016-04-01 11:54:31 -0700461 }
462
Chris Dalton20e7a532020-02-28 15:37:53 +0000463 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500464 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700465
bsalomonc8dc1f72014-08-21 13:02:13 -0700466 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700467 fTempSrcFBOID = 0;
468 fTempDstFBOID = 0;
469 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700470 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800471 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
472 fCopyPrograms[i].fProgram = 0;
473 }
brianosman33f6b3f2016-06-02 05:49:21 -0700474 fMipmapProgramArrayBuffer.reset();
475 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
476 fMipmapPrograms[i].fProgram = 0;
477 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400478
jvanverthe9c0fc62015-04-29 11:18:05 -0700479 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700480 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700481 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400482 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483}
484
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000485///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000486
bsalomon861e1032014-12-16 07:33:49 -0800487void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000488 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400489 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000490 GL_CALL(Disable(GR_GL_DEPTH_TEST));
491 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000492
Brian Salomonf0861672017-05-08 11:10:10 -0400493 // We don't use face culling.
494 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600495 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
496 // just set this to the default for self-consistency.
497 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400498
Brian Salomonae64c192019-02-05 09:41:37 -0500499 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
500 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700501
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400502 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500503#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000504 // Desktop-only state that we never change
505 if (!this->glCaps().isCoreProfile()) {
506 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
507 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
508 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
509 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
510 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
511 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
512 }
513 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
514 // core profile. This seems like a bug since the core spec removes any mention of
515 // GL_ARB_imaging.
516 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
517 GL_CALL(Disable(GR_GL_COLOR_TABLE));
518 }
519 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400520
Chris Dalton1215cda2019-12-17 21:44:04 -0700521 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500522#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 // Since ES doesn't support glPointSize at all we always use the VS to
524 // set the point size
525 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
526
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000527 }
joshualitt58162332014-08-01 06:44:53 -0700528
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400529 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500530 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700531 // The arm extension requires specifically enabling MSAA fetching per sample.
532 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500533 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700534 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000535 fHWWriteToColor = kUnknown_TriState;
536 // we only ever use lines in hairline mode
537 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700538 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500539
540 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000541 }
edisonn@google.comba669992013-06-28 16:03:21 +0000542
egdanielb414f252014-07-29 13:15:47 -0700543 if (resetBits & kMSAAEnable_GrGLBackendState) {
544 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700545
Chris Dalton6ce447a2019-06-23 18:07:38 -0600546 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800547 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
548 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700549 GL_CALL(CoverageModulation(GR_GL_RGBA));
550 }
Chris Daltonce425af2019-12-16 10:39:03 -0700551
552 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000553 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000555 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400556 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000557
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500559 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500560 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000561 }
Brian Salomondc829942018-10-23 16:07:24 -0400562 if (fSamplerObjectCache) {
563 fSamplerObjectCache->invalidateBindings();
564 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000565 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000566
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000567 if (resetBits & kBlend_GrGLBackendState) {
568 fHWBlendState.invalidate();
569 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000570
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 if (resetBits & kView_GrGLBackendState) {
572 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700573 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000574 fHWViewport.invalidate();
575 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000576
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000577 if (resetBits & kStencil_GrGLBackendState) {
578 fHWStencilSettings.invalidate();
579 fHWStencilTestEnabled = kUnknown_TriState;
580 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000581
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 // Vertex
583 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700584 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500585 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
586 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600587 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700588 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000590
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500592 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700593 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500594 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000595 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000596
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000597 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700598 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700599 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000601 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000602
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000603 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400605 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
607 }
Brian Salomon1047a492019-07-02 12:25:21 -0400608 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000609 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
610 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000611 if (this->glCaps().packFlipYSupport()) {
612 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
613 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000614 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000615
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000616 if (resetBits & kProgram_GrGLBackendState) {
617 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000618 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000619 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400620 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000621}
622
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400623static bool check_backend_texture(const GrBackendTexture& backendTex,
624 const GrGLCaps& caps,
625 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400626 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400627 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400628 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400629 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800630 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000631
Brian Salomonea4ad302019-08-07 13:04:55 -0400632 desc->fSize = {backendTex.width(), backendTex.height()};
633 desc->fTarget = info.fTarget;
634 desc->fID = info.fID;
635 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800636
Brian Salomonea4ad302019-08-07 13:04:55 -0400637 if (desc->fFormat == GrGLFormat::kUnknown) {
638 return false;
639 }
640 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400641 if (!caps.shaderCaps()->externalTextureSupport()) {
642 return false;
643 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400644 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
645 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400646 return false;
647 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400648 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400649 return false;
650 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400651 if (backendTex.isProtected()) {
652 // Not supported in GL backend at this time.
653 return false;
654 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400655
Brian Salomond17f6582017-07-19 18:28:58 -0400656 return true;
657}
658
659sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400660 GrWrapOwnership ownership,
661 GrWrapCacheable cacheable,
662 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400663 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400664 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400665 return nullptr;
666 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400667
Brian Salomond17f6582017-07-19 18:28:58 -0400668 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400669 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400670 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400671 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400672 }
bsalomone5286e02016-01-14 09:24:09 -0800673
Brian Salomon40a40622020-07-21 10:32:07 -0400674 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400675 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400676
Brian Salomona6db5102020-07-21 09:56:23 -0400677 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400678 backendTex.getGLTextureParams(), cacheable, ioType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600679 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400680}
681
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500682static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
683 const GrGLCaps& caps, GrGLTexture::Desc* desc,
684 bool skipRectTexSupportCheck = false) {
685 GrGLTextureInfo info;
686 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
687 return false;
688 }
689
690 desc->fSize = {backendTex.width(), backendTex.height()};
691 desc->fTarget = info.fTarget;
692 desc->fID = info.fID;
693 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
694
695 if (desc->fFormat == GrGLFormat::kUnknown) {
696 return false;
697 }
698
699 if (GR_GL_TEXTURE_2D != desc->fTarget) {
700 return false;
701 }
702 if (backendTex.isProtected()) {
703 // Not supported in GL backend at this time.
704 return false;
705 }
706
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500707 return true;
708}
709
Robert Phillipsb915c942019-12-17 14:44:37 -0500710sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
711 GrWrapOwnership ownership,
712 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500713 GrGLTexture::Desc desc;
714 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
715 return nullptr;
716 }
717
718 if (kBorrow_GrWrapOwnership == ownership) {
719 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
720 } else {
721 desc.fOwnership = GrBackendObjectOwnership::kOwned;
722 }
723
Brian Salomon40a40622020-07-21 10:32:07 -0400724 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400725 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500726
Brian Salomona6db5102020-07-21 09:56:23 -0400727 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500728 backendTex.getGLTextureParams(), cacheable,
729 kRead_GrIOType);
Mike Kleina9609ea2020-02-18 13:33:23 -0600730 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500731}
732
Brian Salomond17f6582017-07-19 18:28:58 -0400733sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400734 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500735 GrWrapOwnership ownership,
736 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400737 const GrGLCaps& caps = this->glCaps();
738
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400740 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800741 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800742 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400743 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
744 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800745
Brian Salomond17f6582017-07-19 18:28:58 -0400746 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400747 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800748 return nullptr;
749 }
bsalomon10528f12015-10-14 12:54:52 -0700750
Brian Osman766fcbb2017-03-13 09:33:09 -0400751 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400752 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400753 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400754 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800755 }
bsalomonb15b4c12014-10-29 12:41:57 -0700756
Robert Phillips0902c982019-07-16 07:47:56 -0400757
Greg Daniel6fa62e22019-08-07 15:52:37 -0400758 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
759 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000760
Brian Salomonea4ad302019-08-07 13:04:55 -0400761 GrGLRenderTarget::IDs rtIDs;
762 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400763 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000764 }
Greg Daniel177e6952017-10-12 12:27:11 -0400765
Brian Salomon40a40622020-07-21 10:32:07 -0400766 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400767 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400768
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500769 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400770 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400771 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400772 texRT->baseLevelWasBoundToFBO();
Mike Kleina9609ea2020-02-18 13:33:23 -0600773 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000774}
775
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400776sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400777 GrGLFramebufferInfo info;
778 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000779 return nullptr;
780 }
781
Brian Salomone8a766b2019-07-19 14:24:36 -0400782 if (backendRT.isProtected()) {
783 // Not supported in GL at this time.
784 return nullptr;
785 }
786
Brian Salomond4764a12019-08-08 12:08:24 -0400787 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400788 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
789 return nullptr;
790 }
791
Brian Salomonea4ad302019-08-07 13:04:55 -0400792 GrGLRenderTarget::IDs rtIDs;
793 rtIDs.fRTFBOID = info.fFBOID;
794 rtIDs.fMSColorRenderbufferID = 0;
795 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
796 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000797
Greg Daniel6fa62e22019-08-07 15:52:37 -0400798 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700799
Brian Salomona56a7462020-02-07 14:17:25 -0500800 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400801 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000802}
803
Greg Daniel7ef28f32017-04-20 16:41:55 +0000804sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400805 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400806 GrGLTexture::Desc desc;
807 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
808 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400809 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800810 return nullptr;
811 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400812
813 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
814 return nullptr;
815 }
816
817 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400818 GrGLRenderTarget::IDs rtIDs;
819 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800820 return nullptr;
821 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500822 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800823}
824
Brian Salomonc320b152018-02-20 14:05:36 -0500825static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700826 if (!glTex) {
827 return false;
828 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000829
bsalomone5286e02016-01-14 09:24:09 -0800830 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
831 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800832 return false;
833 }
834
jvanverth17aa0472016-01-05 10:41:27 -0800835 return true;
836}
837
Brian Salomona9b04b92018-06-01 15:04:28 -0400838bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400839 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400840 const GrMipLevel texels[], int mipLevelCount,
841 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500842 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800843
Brian Salomonc320b152018-02-20 14:05:36 -0500844 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800845 return false;
846 }
847
Brian Salomond978b902019-02-07 15:09:18 -0500848 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000849
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400850 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400851 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
852 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
853 glTex->target(), dstRect, srcColorType, texels,
854 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000855}
856
Brian Salomone05ba5a2019-04-08 11:59:07 -0400857bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400858 GrColorType textureColorType, GrColorType bufferColorType,
859 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400860 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400861
Jim Van Verth1676cb92019-01-15 13:24:45 -0500862 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400863 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500864
Brian Salomonc320b152018-02-20 14:05:36 -0500865 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400866 return false;
867 }
868
Hal Canaryc36f7e72018-06-18 15:50:09 -0400869 static_assert(sizeof(int) == sizeof(int32_t), "");
870 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400871 return false;
872 }
873
Brian Salomond978b902019-02-07 15:09:18 -0500874 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400875
876 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500877 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400878 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500879 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400880
Greg Daniel660cc992017-06-26 14:55:05 -0400881 SkDEBUGCODE(
882 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
883 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
884 SkASSERT(bounds.contains(subRect));
885 )
886
Brian Salomonb28cb682019-07-26 12:48:47 -0400887 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400888 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400889 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700890 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400891 return false;
892 }
893
894 bool restoreGLRowLength = false;
895 if (trimRowBytes != rowBytes) {
896 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400897 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400898 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
899 restoreGLRowLength = true;
900 }
901
Greg Danielba88ab62019-07-26 09:14:01 -0400902 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400903 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400904 GrGLenum externalFormat = 0;
905 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400906 this->glCaps().getTexSubImageExternalFormatAndType(
907 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400908 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400909 return false;
910 }
911
Brian Salomon8cbf6622019-07-30 11:03:14 -0400912 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400913 GL_CALL(TexSubImage2D(glTex->target(),
914 0,
915 left, top,
916 width,
917 height,
918 externalFormat, externalType,
919 pixels));
920
921 if (restoreGLRowLength) {
922 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
923 }
924
925 return true;
926}
927
Brian Salomon26de56e2019-04-10 12:14:26 -0400928bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400929 GrColorType surfaceColorType, GrColorType dstColorType,
930 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400931 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
932 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400933 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400934 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
935 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400936}
937
Brian Osman9b560242017-09-05 15:34:52 -0400938void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500939 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
940 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
941 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
942 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400943 }
Brian Osman9b560242017-09-05 15:34:52 -0400944}
945
Brian Salomon22558932020-05-15 14:46:34 -0400946bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
947 GrColorType textureColorType,
948 SkISize texDims,
949 GrGLenum target,
950 SkIRect dstRect,
951 GrColorType srcColorType,
952 const GrMipLevel texels[],
953 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500954 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400955 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500956
Greg Daniel7bfc9132019-08-14 14:23:53 -0400957 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800958
Brian Salomonf77c1462019-08-01 15:19:29 -0400959 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800960
bsalomon5b30c6f2015-12-17 14:17:34 -0800961 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800962 GrGLenum externalFormat;
963 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400964 this->glCaps().getTexSubImageExternalFormatAndType(
965 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400966 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800967 return false;
968 }
Brian Salomon22558932020-05-15 14:46:34 -0400969 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
970 mipLevelCount);
971 return true;
972}
Brian Salomonf77c1462019-08-01 15:19:29 -0400973
Brian Salomon22558932020-05-15 14:46:34 -0400974bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
975 SkISize texDims,
976 GrGLenum target,
977 SkColor4f color,
978 uint32_t levelMask) {
979 GrColorType colorType;
980 GrGLenum externalFormat, externalType;
981 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
982 &externalType, &colorType);
983 if (colorType == GrColorType::kUnknown) {
984 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000985 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400986
Brian Salomon22558932020-05-15 14:46:34 -0400987 std::unique_ptr<char[]> pixelStorage;
988 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400989 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400990 SkSTArray<16, GrMipLevel> levels;
991 levels.resize(numLevels);
992 SkISize levelDims = texDims;
993 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
994 std::max(levelDims.height() >> 1, 1)}) {
995 if (levelMask & (1 << i)) {
996 if (!pixelStorage) {
997 // Make one tight image at the first size and reuse it for smaller levels.
998 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
999 size_t rb = ii.minRowBytes();
1000 pixelStorage.reset(new char[rb * levelDims.height()]);
1001 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
1002 return false;
1003 }
1004 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +00001005 }
Brian Salomon22558932020-05-15 14:46:34 -04001006 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
1007 }
1008 }
1009 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
1010 bpp, levels.begin(), levels.count());
1011 return true;
1012}
1013
1014void GrGLGpu::uploadTexData(SkISize texDims,
1015 GrGLenum target,
1016 SkIRect dstRect,
1017 GrGLenum externalFormat,
1018 GrGLenum externalType,
1019 size_t bpp,
1020 const GrMipLevel texels[],
1021 int mipLevelCount) {
1022 SkASSERT(!texDims.isEmpty());
1023 SkASSERT(!dstRect.isEmpty());
1024 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001025 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001026 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1027
1028 const GrGLCaps& caps = this->glCaps();
1029
1030 bool restoreGLRowLength = false;
1031
1032 this->unbindCpuToGpuXferBuffer();
1033 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1034
1035 SkISize dims = dstRect.size();
1036 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1037 std::max(dims.height() >> 1, 1)}) {
1038 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001039 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001040 }
Brian Salomon22558932020-05-15 14:46:34 -04001041 const size_t trimRowBytes = dims.width() * bpp;
1042 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043
1044 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1045 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001046 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001047 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001048 } else {
1049 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001050 }
1051
Brian Salomon22558932020-05-15 14:46:34 -04001052 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1053 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001054 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001055 if (restoreGLRowLength) {
1056 SkASSERT(caps.writePixelsRowBytesSupport());
1057 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1058 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001059}
1060
Greg Daniel01f278c2020-06-12 16:58:17 -04001061bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1062 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001063 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001064 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001066 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001067 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001068 const GrGLCaps& caps = this->glCaps();
1069
1070 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001071 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001073 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001074 }
1075
Robert Phillips9f744f72019-12-19 19:14:33 -05001076 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1077
Greg Daniel7bfc9132019-08-14 14:23:53 -04001078 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001079
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001080 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001081 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001082 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001083 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001084
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001085 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001086 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087
1088 if (useTexStorage) {
1089 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001090 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1091 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001092 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001093 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001094 }
Robert Phillips42716d42019-12-16 12:19:54 -05001095
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001096 size_t offset = 0;
1097 for (int level = 0; level < numMipLevels; ++level) {
1098
Robert Phillips99dead92020-01-27 16:11:57 -05001099 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1100 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001101
Brian Salomond8575452020-02-25 12:13:29 -05001102 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1103 level,
1104 0, // left
1105 0, // top
1106 dimensions.width(),
1107 dimensions.height(),
1108 internalFormat,
1109 SkToInt(levelDataSize),
1110 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001111
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001112 if (error != GR_GL_NO_ERROR) {
1113 return false;
1114 }
1115
Robert Phillips9f744f72019-12-19 19:14:33 -05001116 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001117 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001118 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001119 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001120 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001121
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001122 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001123 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1124 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001125
1126 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001127 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1128 level,
1129 internalFormat,
1130 dimensions.width(),
1131 dimensions.height(),
1132 0, // border
1133 SkToInt(levelDataSize),
1134 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001135
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001136 if (error != GR_GL_NO_ERROR) {
1137 return false;
1138 }
1139
Robert Phillips9f744f72019-12-19 19:14:33 -05001140 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001141 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001142 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001143 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001144 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001145}
1146
Brian Salomond8575452020-02-25 12:13:29 -05001147bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001148 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001149 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001150 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001151 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001152 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001153 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1154 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001155 break;
1156 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001157 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1158 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001159 break;
1160 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1161 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001162 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1163 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001164 break;
1165 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001166 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001167 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001168 }
Brian Salomond8575452020-02-25 12:13:29 -05001169 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001170}
1171
Brian Salomonea4ad302019-08-07 13:04:55 -04001172bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001173 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001174 GrGLRenderTarget::IDs* rtIDs) {
1175 rtIDs->fMSColorRenderbufferID = 0;
1176 rtIDs->fRTFBOID = 0;
1177 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1178 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179
bsalomona11e5fc2015-12-18 07:59:41 -08001180 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001181
Brian Salomonea4ad302019-08-07 13:04:55 -04001182 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001183 goto FAILED;
1184 }
1185
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001186 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001187 goto FAILED;
1188 }
1189
Brian Salomonea4ad302019-08-07 13:04:55 -04001190 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1191 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 goto FAILED;
1193 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001194
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001195 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1196 // the texture bound to the other. The exception is the IMG multisample extension. With this
1197 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1198 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001199 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1201 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1202 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 goto FAILED;
1204 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001206 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001208 }
1209
egdanield803f272015-03-18 13:01:52 -07001210 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001211 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001212 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001213 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001214 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001215 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1216 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 goto FAILED;
1218 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001219 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001220 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001221 GR_GL_COLOR_ATTACHMENT0,
1222 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001224 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001225 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001226
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001227 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001228 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1229 GR_GL_COLOR_ATTACHMENT0,
1230 desc.fTarget,
1231 desc.fID,
1232 0,
1233 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001234 } else {
egdanield803f272015-03-18 13:01:52 -07001235 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001236 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001237 desc.fTarget,
1238 desc.fID,
1239 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001240 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241
1242 return true;
1243
1244FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001245 if (rtIDs->fMSColorRenderbufferID) {
1246 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001248 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1249 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001250 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001251 if (rtIDs->fTexFBOID) {
1252 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001253 }
1254 return false;
1255}
1256
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001257// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001258static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001259// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001260 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001261}
1262
Brian Salomone2826ab2019-06-04 15:58:31 -04001263static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001264 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001265 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1266 // drivers have a bug where an FBO won't be complete if it includes a
1267 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001268 GrGLTextureParameters::SamplerOverriddenState state;
1269 state.fMinFilter = GR_GL_NEAREST;
1270 state.fMagFilter = GR_GL_NEAREST;
1271 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1272 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001273 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1274 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1275 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1276 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001277 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001278}
1279
Brian Salomona56a7462020-02-07 14:17:25 -05001280sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001281 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001282 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001283 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001284 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001285 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001286 int mipLevelCount,
1287 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001288 // We don't support protected textures in GL.
1289 if (isProtected == GrProtected::kYes) {
1290 return nullptr;
1291 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001292 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001293
Brian Salomond2a8ae22019-09-10 16:03:59 -04001294 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001295 GrMipmapStatus mipmapStatus =
1296 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001297 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001298 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001299 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001300 switch (format.textureType()) {
1301 case GrTextureType::kExternal:
1302 case GrTextureType::kNone:
1303 return nullptr;
1304 case GrTextureType::k2D:
1305 texDesc.fTarget = GR_GL_TEXTURE_2D;
1306 break;
1307 case GrTextureType::kRectangle:
1308 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1309 return nullptr;
1310 }
1311 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1312 break;
1313 }
Brian Salomon81536f22019-08-08 16:30:49 -04001314 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001315 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001316 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1317 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001318
Brian Salomon0f396992020-06-19 19:51:21 -04001319 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1320 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001321
1322 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001323 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001324 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001325
Robert Phillips67d52cf2017-06-05 13:38:13 -04001326 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001327 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001328 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001329 GL_CALL(BindTexture(texDesc.fTarget, 0));
1330 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001331
Brian Salomonea4ad302019-08-07 13:04:55 -04001332 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1333 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 return return_null_texture();
1335 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001336 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001337 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001338 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001339 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001340 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001342 // The non-sampler params are still at their default values.
1343 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1344 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001345 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001346 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001347 GrGLenum externalFormat, externalType;
1348 GrColorType colorType;
1349 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1350 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001351 for (int i = 0; i < mipLevelCount; ++i) {
1352 if (levelClearMask & (1U << i)) {
1353 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1354 nullptr));
1355 }
1356 }
1357 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1358 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001359 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001360 this->disableWindowRectangles();
1361 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001362 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001363 for (int i = 0; i < mipLevelCount; ++i) {
1364 if (levelClearMask & (1U << i)) {
1365 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1366 kDst_TempFBOTarget);
1367 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1368 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1369 }
1370 }
Brian Salomonc2249852019-09-16 11:08:50 -04001371 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001372 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001373 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001374 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001375 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001376 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001377 }
1378 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001379 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001380}
1381
Robert Phillips9f744f72019-12-19 19:14:33 -05001382sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001383 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001384 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001385 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001386 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001387 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001388 // We don't support protected textures in GL.
1389 if (isProtected == GrProtected::kYes) {
1390 return nullptr;
1391 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001392 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1393
Brian Salomonbb8dde82019-06-27 10:52:13 -04001394 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001395 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001396 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001397 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001398 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001399 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001400 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001401 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001402 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001403 return nullptr;
1404 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001405
Greg Danielaaf738c2020-07-10 09:30:33 -04001406 if (data) {
1407 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1408 GR_GL_TEXTURE_2D, data, dataSize)) {
1409 GL_CALL(DeleteTextures(1, &desc.fID));
1410 return nullptr;
1411 }
1412 }
1413
Robert Phillipsee946932019-12-18 11:16:17 -05001414 // Unbind this texture from the scratch texture unit.
1415 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1416
Brian Salomona6db5102020-07-21 09:56:23 -04001417 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1418 ? GrMipmapStatus::kValid
1419 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001420
Brian Salomona6db5102020-07-21 09:56:23 -04001421 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001422 // The non-sampler params are still at their default values.
1423 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1424 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001425 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001426}
1427
Greg Danielc1ad77c2020-05-06 11:40:03 -04001428GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001429 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001430 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001431 // We don't support protected textures in GL.
1432 if (isProtected == GrProtected::kYes) {
1433 return {};
1434 }
1435
1436 this->handleDirtyContext();
1437
1438 GrGLFormat glFormat = format.asGLFormat();
1439 if (glFormat == GrGLFormat::kUnknown) {
1440 return {};
1441 }
1442
Greg Daniel01f278c2020-06-12 16:58:17 -04001443 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1444
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001445 GrGLTextureInfo info;
1446 GrGLTextureParameters::SamplerOverriddenState initialState;
1447
1448 info.fTarget = GR_GL_TEXTURE_2D;
1449 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001450 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001451 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001452 if (!info.fID) {
1453 return {};
1454 }
1455
1456 // Unbind this texture from the scratch texture unit.
1457 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1458
1459 auto parameters = sk_make_sp<GrGLTextureParameters>();
1460 // The non-sampler params are still at their default values.
1461 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1462 fResetTimestampForTextureParameters);
1463
1464 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1465 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001466}
1467
Greg Danielaaf738c2020-07-10 09:30:33 -04001468bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1469 sk_sp<GrRefCntedCallback> finishedCallback,
1470 const BackendTextureData* data) {
1471 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1472
1473 GrGLTextureInfo info;
1474 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1475
1476 GrBackendFormat format = backendTexture.getBackendFormat();
1477 GrGLFormat glFormat = format.asGLFormat();
1478 if (glFormat == GrGLFormat::kUnknown) {
1479 return false;
1480 }
1481 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1482
Brian Salomon40a40622020-07-21 10:32:07 -04001483 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001484
1485 const char* rawData = nullptr;
1486 size_t rawDataSize = 0;
1487 SkAutoMalloc am;
1488 if (data->type() == BackendTextureData::Type::kCompressed) {
1489 rawData = (const char*)data->compressedData();
1490 rawDataSize = data->compressedSize();
1491 } else {
1492 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1493 SkASSERT(compression != SkImage::CompressionType::kNone);
1494
1495 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001496 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001497
1498 am.reset(rawDataSize);
1499
1500 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1501 data->color());
1502
1503 rawData = (const char*)am.get();
1504 }
1505
1506 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001507
1508 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1509 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04001510 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelControlSupport()) {
Greg Daniel95afafb2020-07-22 12:09:26 -04001511 auto params = backendTexture.getGLTextureParams();
1512 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1513 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1514 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1515 nonsamplerState.fBaseMipMapLevel = 0;
1516 }
1517 int numMipLevels =
1518 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1519 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1520 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1521 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1522 }
1523 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1524 }
1525
Greg Danielaaf738c2020-07-10 09:30:33 -04001526 bool result = this->uploadCompressedTexData(
1527 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1528 rawData, rawDataSize);
1529
1530 // Unbind this texture from the scratch texture unit.
1531 this->bindTextureToScratchUnit(info.fTarget, 0);
1532
1533 return result;
1534}
1535
Brian Salomon5043f1f2019-07-11 21:27:54 -04001536int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001537 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001538 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1539
1540 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001541 // Default to unsupported, set this if we find a stencil format that works.
1542 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001543
Brian Salomon0f396992020-06-19 19:51:21 -04001544 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1545 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001546 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001547 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001548 }
egdanielff1d5472015-09-10 08:37:20 -07001549 // unbind the texture from the texture unit before binding it to the frame buffer
1550 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1551
1552 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001553 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001554 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001555 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001556 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001557 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1558 GR_GL_COLOR_ATTACHMENT0,
1559 GR_GL_TEXTURE_2D,
1560 colorID,
1561 0));
bsalomon30447372015-12-21 09:03:05 -08001562 GrGLuint sbRBID = 0;
1563 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001564
1565 // look over formats till I find a compatible one
1566 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001567 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001568 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001569 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1570 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001571 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1572 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1573 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001574 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001575 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001576 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001577 if (sFmt.fPacked) {
1578 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1579 GR_GL_DEPTH_ATTACHMENT,
1580 GR_GL_RENDERBUFFER, sbRBID));
1581 } else {
1582 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1583 GR_GL_DEPTH_ATTACHMENT,
1584 GR_GL_RENDERBUFFER, 0));
1585 }
1586 GrGLenum status;
1587 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1588 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1589 firstWorkingStencilFormatIndex = i;
1590 break;
1591 }
egdanielff1d5472015-09-10 08:37:20 -07001592 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1593 GR_GL_STENCIL_ATTACHMENT,
1594 GR_GL_RENDERBUFFER, 0));
1595 if (sFmt.fPacked) {
1596 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1597 GR_GL_DEPTH_ATTACHMENT,
1598 GR_GL_RENDERBUFFER, 0));
1599 }
egdanielff1d5472015-09-10 08:37:20 -07001600 }
1601 }
bsalomon30447372015-12-21 09:03:05 -08001602 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001603 }
1604 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001605 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1606 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001607 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001608 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001609 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001610}
1611
Brian Salomonea4ad302019-08-07 13:04:55 -04001612GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001613 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001614 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001615 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001616 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001617 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001618 if (format == GrGLFormat::kUnknown) {
1619 return 0;
1620 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 GrGLuint id = 0;
1622 GL_CALL(GenTextures(1, &id));
1623 if (!id) {
1624 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001625 }
1626
Brian Salomonea4ad302019-08-07 13:04:55 -04001627 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001628
Brian Salomonea4ad302019-08-07 13:04:55 -04001629 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1630
Brian Salomonea4ad302019-08-07 13:04:55 -04001631 return id;
1632}
1633
Brian Salomon0f396992020-06-19 19:51:21 -04001634GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1635 GrGLFormat format,
1636 GrGLenum target,
1637 GrRenderable renderable,
1638 GrGLTextureParameters::SamplerOverriddenState* initialState,
1639 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001640 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001641 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001642
Brian Salomonea4ad302019-08-07 13:04:55 -04001643 GrGLuint id = 0;
1644 GL_CALL(GenTextures(1, &id));
1645
1646 if (!id) {
1647 return 0;
1648 }
1649
Brian Salomon0f396992020-06-19 19:51:21 -04001650 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001651
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001652 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001653 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001654 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001655 }
1656
Brian Salomond2a8ae22019-09-10 16:03:59 -04001657 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001658 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001659 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001660 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001661 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001662
1663 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1664
1665 bool success = false;
1666 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001667 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001668 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001669 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1670 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001671 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001672 } else {
1673 GrGLenum externalFormat, externalType;
1674 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1675 GrGLenum error = GR_GL_NO_ERROR;
1676 if (externalFormat && externalType) {
1677 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1678 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001679 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1680 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001681 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1682 currentHeight, 0, externalFormat, externalType,
1683 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001684 }
Brian Salomond8575452020-02-25 12:13:29 -05001685 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001686 }
1687 }
1688 }
1689 if (success) {
1690 return id;
1691 }
1692 GL_CALL(DeleteTextures(1, &id));
1693 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001694}
1695
Chris Daltoneffee202019-07-01 22:28:03 -06001696GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
Greg Daniele77162e2020-09-21 15:32:11 -04001697 const GrRenderTarget* rt, SkISize dimensions, int numStencilSamples) {
1698 SkASSERT(dimensions.width() >= rt->width());
1699 SkASSERT(dimensions.height() >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001700
egdaniel8dc7c3a2015-04-16 11:22:42 -07001701 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001702
Brian Salomond4764a12019-08-08 12:08:24 -04001703 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001704 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001705 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001706 }
egdanielff1d5472015-09-10 08:37:20 -07001707
1708 if (!sbDesc.fRenderbufferID) {
1709 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1710 }
1711 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001712 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001713 }
1714 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1715 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001716 // we do this "if" so that we don't call the multisample
1717 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001718 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001719 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
Greg Daniele77162e2020-09-21 15:32:11 -04001720 dimensions.width(), dimensions.height())) {
Brian Salomond8575452020-02-25 12:13:29 -05001721 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1722 return nullptr;
1723 }
egdanielff1d5472015-09-10 08:37:20 -07001724 } else {
Greg Daniele77162e2020-09-21 15:32:11 -04001725 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat,
1726 dimensions.width(),
1727 dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001728 if (error != GR_GL_NO_ERROR) {
1729 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1730 return nullptr;
1731 }
egdanielff1d5472015-09-10 08:37:20 -07001732 }
1733 fStats.incStencilAttachmentCreates();
Greg Daniel17b0c752020-10-01 16:06:37 -04001734
egdanielec00d942015-09-14 12:56:10 -07001735 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1736 sbDesc,
Greg Daniele77162e2020-09-21 15:32:11 -04001737 dimensions,
Chris Daltoneffee202019-07-01 22:28:03 -06001738 numStencilSamples,
Greg Daniel17b0c752020-10-01 16:06:37 -04001739 sFmt);
egdanielec00d942015-09-14 12:56:10 -07001740 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001741}
1742
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001743////////////////////////////////////////////////////////////////////////////////
1744
Brian Salomondbf70722019-02-07 11:31:24 -05001745sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1746 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001747 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001748}
1749
Chris Dalton2e7ed262020-02-21 15:17:59 -07001750void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1751 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001752 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1753 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1754 fHWScissorSettings.fEnabled = kYes_TriState;
1755 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001756 } else {
1757 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1758 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1759 fHWScissorSettings.fEnabled = kNo_TriState;
1760 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001762}
Brian Salomond818ebf2018-07-02 14:08:49 +00001763
Chris Dalton2e7ed262020-02-21 15:17:59 -07001764void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1765 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001766 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001767 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1768 if (fHWScissorSettings.fRect != nativeScissor) {
1769 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1770 nativeScissor.fHeight));
1771 fHWScissorSettings.fRect = nativeScissor;
1772 }
joshualitt77b13072014-10-27 14:51:01 -07001773}
1774
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001775void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001776 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001777#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001778 typedef GrWindowRectsState::Mode Mode;
1779 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1780 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001781
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001782 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001783 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001784 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001785 }
1786
csmartdalton7535f412016-08-23 06:51:00 -07001787 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1788 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001789 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001791
Chris Daltond6cda8d2019-09-05 02:30:04 -06001792 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001794 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001795 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001796 }
1797
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001798 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001799 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001800
Greg Danielacd66b42019-05-22 16:29:12 -04001801 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001802#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001803}
1804
1805void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001806#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001807 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001808 return;
1809 }
1810 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001811 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001812#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001813}
1814
Robert Phillipsdf546db2020-05-29 09:42:54 -04001815bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001816 this->handleDirtyContext();
1817
Chris Daltond42d2002020-02-28 12:05:04 -07001818 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001819 if (!program) {
1820 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1821 return false;
1822 }
1823
1824 this->flushProgram(std::move(program));
1825
Chris Daltond42d2002020-02-28 12:05:04 -07001826 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1827 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1828 }
1829
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001830 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001831 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001832 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001833
Chris Dalton20e7a532020-02-28 15:37:53 +00001834 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001835
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001836 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001837 GrStencilSettings stencil;
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001838 if (programInfo.isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001839 SkASSERT(glRT->getStencilAttachment());
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001840 stencil.reset(*programInfo.userStencilSettings(),
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001841 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001842 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001843 }
Robert Phillips901aff02019-10-08 12:32:56 -04001844 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001845 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001846 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1847 glRT, programInfo.origin());
1848 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001849 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001850 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001851
1852 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001853 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001854 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001855
Chris Dalton20e7a532020-02-28 15:37:53 +00001856 return true;
1857}
1858
1859void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1860 if (!program) {
1861 fHWProgram.reset();
1862 fHWProgramID = 0;
1863 return;
1864 }
1865 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1866 if (program == fHWProgram) {
1867 return;
1868 }
1869 auto id = program->programID();
1870 SkASSERT(id);
1871 GL_CALL(UseProgram(id));
1872 fHWProgram = std::move(program);
1873 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001874}
1875
1876void GrGLGpu::flushProgram(GrGLuint id) {
1877 SkASSERT(id);
1878 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001879 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001880 return;
1881 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001882 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001883 GL_CALL(UseProgram(id));
1884 fHWProgramID = id;
1885}
1886
Brian Salomonae64c192019-02-05 09:41:37 -05001887GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001888 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001889
cdaltone2e71c22016-04-07 18:13:29 -07001890 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001891 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001892 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001893 }
cdaltone2e71c22016-04-07 18:13:29 -07001894
Brian Salomonae64c192019-02-05 09:41:37 -05001895 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001896 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001897 if (!bufferState->fBufferZeroKnownBound) {
1898 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1899 bufferState->fBufferZeroKnownBound = true;
1900 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001901 }
Brian Salomondbf70722019-02-07 11:31:24 -05001902 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1903 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001904 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1905 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1906 bufferState->fBufferZeroKnownBound = false;
1907 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001908 }
1909
Brian Salomonae64c192019-02-05 09:41:37 -05001910 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001911}
Brian Salomond818ebf2018-07-02 14:08:49 +00001912
Michael Ludwig1e632792020-05-21 12:45:31 -04001913void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001914 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001915 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001916 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001917 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001918 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001919
Brian Salomon43f8bf02017-10-18 08:33:29 -04001920 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001921
Brian Salomon43f8bf02017-10-18 08:33:29 -04001922 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1923
Michael Ludwig1e632792020-05-21 12:45:31 -04001924 if (scissor.enabled()) {
1925 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001926 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001927 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001928 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001929 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1930 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001931 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001932 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001933 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001934}
1935
Chris Daltonb50cc812019-10-14 16:29:21 -06001936static bool use_tiled_rendering(const GrGLCaps& glCaps,
1937 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1938 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1939 // Otherwise it's faster to just not use it.
1940 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1941 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1942}
1943
1944void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001945 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1946 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1947 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1948
1949 this->handleDirtyContext();
1950
1951 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1952 this->flushRenderTarget(glRT);
1953 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1954
Chris Daltonb50cc812019-10-14 16:29:21 -06001955 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1956 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1957 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1958 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1959 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1960 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1961 nativeBounds.fHeight, preserveMask));
1962 }
1963
Chris Dalton674f77a2019-09-30 20:49:39 -06001964 GrGLbitfield clearMask = 0;
1965 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1966 SkASSERT(!this->caps()->performColorClearsAsDraws());
1967 this->flushClearColor(colorLoadStore.fClearColor);
1968 this->flushColorWrite(true);
1969 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001970 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001971 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1972 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1973 GL_CALL(StencilMask(0xffffffff));
1974 GL_CALL(ClearStencil(0));
1975 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1976 }
1977 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001978 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001979 this->disableWindowRectangles();
1980 GL_CALL(Clear(clearMask));
1981 }
1982}
1983
1984void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1985 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1986 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1987 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1988
1989 this->handleDirtyContext();
1990
1991 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1992 // The framebuffer binding changed in the middle of a command buffer. We should have already
1993 // printed a warning during onFBOChanged.
1994 return;
1995 }
1996
1997 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1998 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1999
2000 SkSTArray<2, GrGLenum> discardAttachments;
2001 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2002 discardAttachments.push_back(
2003 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2004 }
2005 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2006 discardAttachments.push_back(
2007 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2008 }
2009
2010 if (!discardAttachments.empty()) {
2011 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2012 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2013 discardAttachments.begin()));
2014 } else {
2015 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2016 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2017 discardAttachments.begin()));
2018 }
2019 }
2020 }
2021
Chris Daltonb50cc812019-10-14 16:29:21 -06002022 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2023 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2024 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2025 // Handled by use_tiled_rendering().
2026 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2027 GL_CALL(EndTiling(preserveMask));
2028 }
2029
Chris Dalton674f77a2019-09-30 20:49:39 -06002030 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002031}
2032
Michael Ludwig1e632792020-05-21 12:45:31 -04002033void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002034 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002035 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002036 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002037 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002038 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002039
Brian Salomonf7f54332020-07-28 09:23:35 -04002040 GrStencilAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002041 if (!sb) {
2042 // We should only get here if we marked a proxy as requiring a SB. However,
2043 // the SB creation could later fail. Likely clipping is going to go awry now.
2044 return;
2045 }
2046
bsalomon6bc1b5f2015-02-23 09:06:38 -08002047 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002048#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002049 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002050 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002051#else
2052 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002053 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002054 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002055 // changing the clip between stencil passes may or may not
2056 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002057 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002058#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002059 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002060 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002061 value = (1 << (stencilBitCount - 1));
2062 } else {
2063 value = 0;
2064 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002065 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002066 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002067
Michael Ludwig1e632792020-05-21 12:45:31 -04002068 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2069 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002070
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002071 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002072 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002073 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002074 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002075}
2076
Brian Salomone05ba5a2019-04-08 11:59:07 -04002077bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002078 GrColorType surfaceColorType, GrColorType dstColorType,
2079 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002080 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002081
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002082 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002083 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002084 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002085 return false;
2086 }
Greg Danielba88ab62019-07-26 09:14:01 -04002087 GrGLenum externalFormat = 0;
2088 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002089 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2090 surfaceColorType,
2091 dstColorType,
2092 &externalFormat,
2093 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002094 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002095 return false;
2096 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002097
Brian Salomon71d9d842016-11-03 13:42:00 -04002098 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002099 if (renderTarget->numSamples() <= 1 ||
2100 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2101 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2102 this->flushRenderTargetNoColorWrites(renderTarget);
2103 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2104 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2105 return false;
2106 } else {
2107 SkASSERT(renderTarget->requiresManualMSAAResolve());
2108 // we don't track the state of the READ FBO ID.
2109 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002110 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002111 } else {
2112 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002113 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002114 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002115 }
2116
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002117 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002118 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002119
Brian Salomona6948702018-06-01 15:33:20 -04002120 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002121 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002122 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002123 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002124 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002125 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002126
Brian Osman1348ed02018-09-12 09:08:39 -04002127 bool reattachStencil = false;
2128 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2129 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002130 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002131 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002132 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2133 reattachStencil = true;
2134 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2135 GR_GL_RENDERBUFFER, 0));
2136 }
2137
Chris Dalton76500e52019-09-05 02:13:05 -06002138 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002139 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002140
2141 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002142 auto* stencilAttachment =
2143 static_cast<GrGLStencilAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002144 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2145 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2146 }
2147
Brian Salomon26de56e2019-04-10 12:14:26 -04002148 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002149 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002150 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2151 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002152
Brian Salomone05ba5a2019-04-08 11:59:07 -04002153 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002154 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002155 }
2156 return true;
2157}
2158
2159bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002160 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2161 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002162 SkASSERT(surface);
2163
Brian Salomonb28cb682019-07-26 12:48:47 -04002164 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002165
Brian Salomon26de56e2019-04-10 12:14:26 -04002166 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2167 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002168
Brian Salomon1047a492019-07-02 12:25:21 -04002169 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002170 rowPixelWidth = width;
2171 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002172 SkASSERT(!(rowBytes % bytesPerPixel));
2173 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002174 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002175 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2176 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002177}
2178
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002179GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002180 GrRenderTarget* rt, GrStencilAttachment*,
2181 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002182 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002183 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Greg Daniel9a18b082020-08-14 14:03:50 -04002184 const SkTArray<GrSurfaceProxy*, true>& sampledProxies,
Greg Daniel21774362020-09-14 10:36:43 -04002185 GrXferBarrierFlags renderPassXferBarriers) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002186 if (!fCachedOpsRenderPass) {
John Stilesfbd050b2020-08-03 13:21:46 -04002187 fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002188 }
2189
Chris Daltonb50cc812019-10-14 16:29:21 -06002190 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002191 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002192}
2193
Brian Salomon1fabd512018-02-09 09:54:25 -05002194void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002195 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002196 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002197 this->didWriteToSurface(target, origin, &bounds);
2198}
bsalomon6ba6fa12015-03-04 11:57:37 -08002199
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002200void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2201 this->flushRenderTargetNoColorWrites(target);
2202 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002203}
2204
Brian Osman9aa30c62018-07-02 15:21:46 -04002205void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002206 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002207 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002208 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002209 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002210#ifdef SK_DEBUG
2211 // don't do this check in Chromium -- this is causing
2212 // lots of repeated command buffer flushes when the compositor is
2213 // rendering with Ganesh, which is really slow; even too slow for
2214 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002215 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002216 GrGLenum status;
2217 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2218 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2219 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2220 }
bsalomon160f24c2015-03-17 15:55:42 -07002221 }
egdanield803f272015-03-18 13:01:52 -07002222#endif
2223 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002224 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002225 }
2226
brianosman35b784d2016-05-05 11:52:53 -07002227 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002228 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002229 }
Brian Salomon9b553272020-01-24 14:38:37 -05002230
2231 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2232 GrGLint format;
2233 GrGLint type;
2234 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2235 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2236 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2237 }
bsalomon083617b2016-02-12 12:10:14 -08002238}
bsalomona9909122016-01-23 10:41:40 -08002239
brianosman33f6b3f2016-06-02 05:49:21 -07002240void GrGLGpu::flushFramebufferSRGB(bool enable) {
2241 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2242 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2243 fHWSRGBFramebuffer = kYes_TriState;
2244 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2245 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2246 fHWSRGBFramebuffer = kNo_TriState;
2247 }
2248}
2249
Greg Danielacd66b42019-05-22 16:29:12 -04002250void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002251 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002252 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002253 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002254 fHWViewport = viewport;
2255 }
2256}
2257
Chris Dalton33db9fc2020-02-25 18:52:12 -07002258GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002259 fStats.incNumDraws();
2260
Chris Dalton2e7ed262020-02-21 15:17:59 -07002261 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2262 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2263 GL_CALL(Enable(GR_GL_CULL_FACE));
2264 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002265 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002266 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002267
Chris Dalton3809bab2017-06-13 10:55:06 -06002268 switch (primitiveType) {
2269 case GrPrimitiveType::kTriangles:
2270 return GR_GL_TRIANGLES;
2271 case GrPrimitiveType::kTriangleStrip:
2272 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002273 case GrPrimitiveType::kPoints:
2274 return GR_GL_POINTS;
2275 case GrPrimitiveType::kLines:
2276 return GR_GL_LINES;
2277 case GrPrimitiveType::kLineStrip:
2278 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002279 case GrPrimitiveType::kPatches:
2280 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002281 case GrPrimitiveType::kPath:
2282 SK_ABORT("non-mesh-based GrPrimitiveType");
2283 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002284 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002285 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002286}
2287
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002288void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002289 // Some extensions automatically resolves the texture when it is read.
2290 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2291
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002292 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002293 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2294 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2295 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2296 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002297
Chris Daltonc139d082019-09-26 14:04:24 -06002298 // make sure we go through flushRenderTarget() since we've modified
2299 // the bound DRAW FBO ID.
2300 fHWBoundRenderTargetUniqueID.makeInvalid();
2301 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2302 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002303 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2304 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002305 GrScissorState scissor(rt->dimensions());
2306 SkAssertResult(scissor.set(resolveRect));
2307 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002308 this->disableWindowRectangles();
2309 GL_CALL(ResolveMultisampleFramebuffer());
2310 } else {
2311 int l, b, r, t;
2312 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2313 this->glCaps().blitFramebufferSupportFlags()) {
2314 l = 0;
2315 b = 0;
2316 r = target->width();
2317 t = target->height();
2318 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002319 l = resolveRect.x();
2320 b = resolveRect.y();
2321 r = resolveRect.x() + resolveRect.width();
2322 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002323 }
Chris Daltonc139d082019-09-26 14:04:24 -06002324
2325 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002326 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002327 this->disableWindowRectangles();
2328 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 +00002329 }
2330}
2331
bsalomon@google.com411dad02012-06-05 20:24:20 +00002332namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002333
bsalomon@google.com411dad02012-06-05 20:24:20 +00002334
2335GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002336 static const GrGLenum gTable[kGrStencilOpCount] = {
2337 GR_GL_KEEP, // kKeep
2338 GR_GL_ZERO, // kZero
2339 GR_GL_REPLACE, // kReplace
2340 GR_GL_INVERT, // kInvert
2341 GR_GL_INCR_WRAP, // kIncWrap
2342 GR_GL_DECR_WRAP, // kDecWrap
2343 GR_GL_INCR, // kIncClamp
2344 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002345 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002346 static_assert(0 == (int)GrStencilOp::kKeep);
2347 static_assert(1 == (int)GrStencilOp::kZero);
2348 static_assert(2 == (int)GrStencilOp::kReplace);
2349 static_assert(3 == (int)GrStencilOp::kInvert);
2350 static_assert(4 == (int)GrStencilOp::kIncWrap);
2351 static_assert(5 == (int)GrStencilOp::kDecWrap);
2352 static_assert(6 == (int)GrStencilOp::kIncClamp);
2353 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002354 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2355 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002356}
2357
2358void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002359 const GrStencilSettings::Face& face,
2360 GrGLenum glFace) {
2361 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2362 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2363 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002364
cdalton93a379b2016-05-11 13:58:08 -07002365 GrGLint ref = face.fRef;
2366 GrGLint mask = face.fTestMask;
2367 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002368
2369 if (GR_GL_FRONT_AND_BACK == glFace) {
2370 // we call the combined func just in case separate stencil is not
2371 // supported.
2372 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2373 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002374 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002375 } else {
2376 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2377 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002378 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002379 }
2380}
John Stilesa6841be2020-08-06 14:11:56 -04002381} // namespace
bsalomon@google.comd302f142011-03-03 13:54:13 +00002382
Chris Dalton71713f62019-04-18 12:41:03 -06002383void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002384 if (stencilSettings.isDisabled()) {
2385 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002386 } else if (fHWStencilSettings != stencilSettings ||
2387 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002388 if (kYes_TriState != fHWStencilTestEnabled) {
2389 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002390
csmartdaltonc7d85332016-10-26 10:13:46 -07002391 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002392 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002393 if (!stencilSettings.isTwoSided()) {
2394 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2395 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002396 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002397 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2398 GR_GL_FRONT);
2399 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2400 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002401 }
joshualitta58fe352014-10-27 08:39:00 -07002402 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002403 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002404 }
2405}
2406
csmartdaltonc7d85332016-10-26 10:13:46 -07002407void GrGLGpu::disableStencil() {
2408 if (kNo_TriState != fHWStencilTestEnabled) {
2409 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002410
csmartdaltonc7d85332016-10-26 10:13:46 -07002411 fHWStencilTestEnabled = kNo_TriState;
2412 fHWStencilSettings.invalidate();
2413 }
2414}
2415
Chris Dalton4c56b032019-03-04 12:28:42 -07002416void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002417 // rt is only optional if useHWAA is false.
2418 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002419#ifdef SK_DEBUG
2420 if (useHWAA && rt->numSamples() <= 1) {
2421 SkASSERT(this->caps()->mixedSamplesSupport());
2422 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002423 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002424 }
2425#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002426
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002427 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002428 if (useHWAA) {
2429 if (kYes_TriState != fMSAAEnabled) {
2430 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2431 fMSAAEnabled = kYes_TriState;
2432 }
2433 } else {
2434 if (kNo_TriState != fMSAAEnabled) {
2435 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2436 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002437 }
2438 }
2439 }
2440}
2441
Chris Daltonce425af2019-12-16 10:39:03 -07002442void GrGLGpu::flushConservativeRasterState(bool enabled) {
2443 if (this->caps()->conservativeRasterSupport()) {
2444 if (enabled) {
2445 if (kYes_TriState != fHWConservativeRasterEnabled) {
2446 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2447 fHWConservativeRasterEnabled = kYes_TriState;
2448 }
2449 } else {
2450 if (kNo_TriState != fHWConservativeRasterEnabled) {
2451 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2452 fHWConservativeRasterEnabled = kNo_TriState;
2453 }
2454 }
2455 }
2456}
2457
Chris Dalton1215cda2019-12-17 21:44:04 -07002458void GrGLGpu::flushWireframeState(bool enabled) {
2459 if (this->caps()->wireframeSupport()) {
2460 if (this->caps()->wireframeMode() || enabled) {
2461 if (kYes_TriState != fHWWireframeEnabled) {
2462 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2463 fHWWireframeEnabled = kYes_TriState;
2464 }
2465 } else {
2466 if (kNo_TriState != fHWWireframeEnabled) {
2467 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2468 fHWWireframeEnabled = kNo_TriState;
2469 }
2470 }
2471 }
2472}
2473
Chris Daltonbbb3f642019-07-24 12:25:08 -04002474void GrGLGpu::flushBlendAndColorWrite(
2475 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2476 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2477 // We need to work around a driver bug by using a blend state that preserves the dst color,
2478 // rather than disabling color writes.
2479 GrXferProcessor::BlendInfo preserveDstBlend;
2480 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2481 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2482 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2483 return;
2484 }
bsalomonf7cc8772015-05-11 11:21:14 -07002485
cdalton8917d622015-05-06 13:40:21 -07002486 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002487 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2488 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002489
2490 // Any optimization to disable blending should have already been applied and
2491 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002492 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2493 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002494
egdanielb414f252014-07-29 13:15:47 -07002495 if (blendOff) {
2496 if (kNo_TriState != fHWBlendState.fEnabled) {
2497 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002498
Jim Van Verth1bef9792020-07-09 08:09:13 -04002499 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002500 // https://code.google.com/p/skia/issues/detail?id=3943
2501 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2502 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2503 SkASSERT(this->caps()->advancedBlendEquationSupport());
2504 // Set to any basic blending equation.
2505 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2506 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2507 fHWBlendState.fEquation = blend_equation;
2508 }
2509
egdanielb414f252014-07-29 13:15:47 -07002510 fHWBlendState.fEnabled = kNo_TriState;
2511 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002512 } else {
2513 if (kYes_TriState != fHWBlendState.fEnabled) {
2514 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002515
Chris Daltonbbb3f642019-07-24 12:25:08 -04002516 fHWBlendState.fEnabled = kYes_TriState;
2517 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002518
Chris Daltonbbb3f642019-07-24 12:25:08 -04002519 if (fHWBlendState.fEquation != equation) {
2520 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2521 fHWBlendState.fEquation = equation;
2522 }
cdalton8917d622015-05-06 13:40:21 -07002523
Chris Daltonbbb3f642019-07-24 12:25:08 -04002524 if (GrBlendEquationIsAdvanced(equation)) {
2525 SkASSERT(this->caps()->advancedBlendEquationSupport());
2526 // Advanced equations have no other blend state.
2527 return;
2528 }
cdalton8917d622015-05-06 13:40:21 -07002529
Chris Daltonbbb3f642019-07-24 12:25:08 -04002530 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2531 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2532 gXfermodeCoeff2Blend[dstCoeff]));
2533 fHWBlendState.fSrcCoeff = srcCoeff;
2534 fHWBlendState.fDstCoeff = dstCoeff;
2535 }
cdalton8917d622015-05-06 13:40:21 -07002536
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002537 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002538 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2539 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2540 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2541 fHWBlendState.fConstColor = blendConst;
2542 fHWBlendState.fConstColorValid = true;
2543 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002544 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002545 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002546
2547 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002548}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002549
Greg Daniel2c3398d2019-06-19 11:58:01 -04002550void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2551 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002552 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002553
reed856e9d92015-09-30 12:21:45 -07002554#ifdef SK_DEBUG
2555 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002556 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002557 const int w = texture->width();
2558 const int h = texture->height();
2559 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2560 }
2561 }
2562#endif
2563
Robert Phillips294870f2016-11-11 12:38:40 -05002564 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002565 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002566 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002567 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002568 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002569 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002570 }
2571
Brian Salomone69b9ef2020-07-22 11:18:06 -04002572 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002573 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002574 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2575 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002576 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002577 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002578 }
bsalomonefd7d452014-10-23 14:17:46 -07002579
Brian Salomone2826ab2019-06-04 15:58:31 -04002580 auto timestamp = texture->parameters()->resetTimestamp();
2581 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002582 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2583 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomona0d913b2020-09-01 12:42:26 -04002584 if (this->glCaps().useSamplerObjects()) {
Brian Salomondc829942018-10-23 16:07:24 -04002585 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002586 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002587 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2588 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2589 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002590 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2591 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002592 this->setTextureUnit(unitIdx);
2593 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2594 newSamplerState = oldSamplerState;
2595 newSamplerState.fMinFilter = minFilter;
2596 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002597 }
2598 }
Brian Salomondc829942018-10-23 16:07:24 -04002599 } else {
Brian Salomona0d913b2020-09-01 12:42:26 -04002600 if (fSamplerObjectCache) {
2601 fSamplerObjectCache->unbindSampler(unitIdx);
2602 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002603 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2604 texture->parameters()->samplerOverriddenState();
2605 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002606
Brian Salomone69b9ef2020-07-22 11:18:06 -04002607 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2608 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002609 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002610
Brian Salomone2826ab2019-06-04 15:58:31 -04002611 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2612 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002613
2614 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002615 newSamplerState.fMinLOD = -1000.f;
2616 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002617
Brian Salomone2826ab2019-06-04 15:58:31 -04002618 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002619 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002620 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002621 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002622 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002623 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002624 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002625 }
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002626 if (this->glCaps().mipmapLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002627 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002628 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002629 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002630 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002631 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002632 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002633 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002634 }
2635 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002636 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002637 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002638 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002639 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002640 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002641 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002642 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002643 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002644 if (this->glCaps().clampToBorderSupport()) {
2645 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002646 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002647 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002648 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2649 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002650 }
2651 }
Brian Salomondc829942018-10-23 16:07:24 -04002652 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002653 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2654 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002655 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002656 newNonsamplerState.fSwizzleIsRGBA = true;
Brian Salomondc829942018-10-23 16:07:24 -04002657
Brian Salomone2826ab2019-06-04 15:58:31 -04002658 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2659 texture->parameters()->nonsamplerState();
Brian Salomon280fa3d2020-08-27 17:16:49 -04002660 if (this->glCaps().textureSwizzleSupport()) {
2661 if (setAll || !oldNonsamplerState.fSwizzleIsRGBA) {
2662 static constexpr GrGLenum kRGBA[4] {
2663 GR_GL_RED,
2664 GR_GL_GREEN,
2665 GR_GL_BLUE,
2666 GR_GL_ALPHA
2667 };
Brian Salomondc829942018-10-23 16:07:24 -04002668 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002669 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon280fa3d2020-08-27 17:16:49 -04002670 static_assert(sizeof(kRGBA[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002671 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
Brian Salomon280fa3d2020-08-27 17:16:49 -04002672 reinterpret_cast<const GrGLint*>(kRGBA)));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002673 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002674 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
Brian Salomon280fa3d2020-08-27 17:16:49 -04002675 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, kRGBA[0]));
2676 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, kRGBA[1]));
2677 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, kRGBA[2]));
2678 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, kRGBA[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002679 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002680 }
2681 }
Brian Salomondc829942018-10-23 16:07:24 -04002682 // These are not supported in ES2 contexts
Brian Salomon9e5a60c2020-09-08 11:48:08 -04002683 if (this->glCaps().mipmapLevelControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002684 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002685 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002686 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002687 this->setTextureUnit(unitIdx);
2688 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002689 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002690 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002691 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002692 this->setTextureUnit(unitIdx);
2693 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002694 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002695 }
Brian Salomon327955b2018-10-22 15:39:22 +00002696 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002697 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2698 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002699}
2700
Brian Salomon1f05d452019-02-08 12:33:08 -05002701void GrGLGpu::onResetTextureBindings() {
2702 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2703 GR_GL_TEXTURE_EXTERNAL};
2704 for (int i = 0; i < this->numTextureUnits(); ++i) {
2705 this->setTextureUnit(i);
2706 for (auto target : kTargets) {
2707 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2708 GL_CALL(BindTexture(target, 0));
2709 }
2710 }
2711 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2712 }
2713}
2714
Chris Dalton5a2f9622019-12-27 14:56:38 -07002715void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2716 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2717 if (fHWPatchVertexCount != count) {
2718 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2719 fHWPatchVertexCount = count;
2720 }
2721}
2722
egdaniel080e6732014-12-22 07:35:52 -08002723void GrGLGpu::flushColorWrite(bool writeColor) {
2724 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002725 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002726 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2727 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002728 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002729 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002730 } else {
2731 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002732 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002733 fHWWriteToColor = kYes_TriState;
2734 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002735 }
bsalomon3e791242014-12-17 13:43:13 -08002736}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002737
Chris Dalton674f77a2019-09-30 20:49:39 -06002738void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2739 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2740 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2741 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2742 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2743 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2744 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2745 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002746 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2747 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2748 GL_CALL(ClearColor(r, g, b, a));
2749 fHWClearColor[0] = r;
2750 fHWClearColor[1] = g;
2751 fHWClearColor[2] = b;
2752 fHWClearColor[3] = a;
2753 }
2754}
2755
bsalomon861e1032014-12-16 07:33:49 -08002756void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002757 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002758 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002759 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002760 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002761 }
2762}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002763
Brian Salomond978b902019-02-07 15:09:18 -05002764void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002765 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002766 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002767 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2768 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2769 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002770 }
Brian Salomond978b902019-02-07 15:09:18 -05002771 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2772 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002773 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002774 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002775}
2776
Brian Salomone5e7eb12016-10-14 16:18:33 -04002777// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002778static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2779 const GrSurface* src,
2780 const SkIRect& srcRect,
2781 const SkIPoint& dstPoint,
2782 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002783 int dstSampleCnt = 0;
2784 int srcSampleCnt = 0;
2785 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002786 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002787 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002788 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002789 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002790 }
2791 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2792 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2793
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002794 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2795 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2796
Brian Salomone5e7eb12016-10-14 16:18:33 -04002797 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002798 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002799
Greg Daniel46cfbc62019-06-07 11:43:30 -04002800 GrTextureType dstTexType;
2801 GrTextureType* dstTexTypePtr = nullptr;
2802 GrTextureType srcTexType;
2803 GrTextureType* srcTexTypePtr = nullptr;
2804 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002805 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002806 dstTexTypePtr = &dstTexType;
2807 }
2808 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002809 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002810 srcTexTypePtr = &srcTexType;
2811 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002812
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002813 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2814 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002815 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002816}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002817
Brian Osmana9c8a052018-01-19 10:31:56 -05002818static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2819 // A RT has a separate MSAA renderbuffer if:
2820 // 1) It's multisampled
2821 // 2) We're using an extension with separate MSAA renderbuffers
2822 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002823 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002824}
2825
Greg Daniel46cfbc62019-06-07 11:43:30 -04002826static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002827 const GrGLCaps& caps) {
2828
bsalomon@google.comeb851172013-04-15 13:51:00 +00002829 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002830 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002831 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002832 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002833
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002834 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2835 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2836
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002837 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2838 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2839
Greg Daniel46cfbc62019-06-07 11:43:30 -04002840 GrTextureType dstTexType;
2841 GrTextureType* dstTexTypePtr = nullptr;
2842 GrTextureType srcTexType;
2843 GrTextureType* srcTexTypePtr = nullptr;
2844 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002845 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002846 dstTexTypePtr = &dstTexType;
2847 }
2848 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002849 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002850 srcTexTypePtr = &srcTexType;
2851 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002852
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002853 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2854 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002855}
2856
Greg Danielacd66b42019-05-22 16:29:12 -04002857// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002858void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002859 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002860 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002861 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002862 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002863 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2864 GrGLuint texID = texture->textureID();
2865 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002866 GrGLuint* tempFBOID;
2867 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002868
egdanield803f272015-03-18 13:01:52 -07002869 if (0 == *tempFBOID) {
2870 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002871 }
2872
Adrienne Walker4ee88512018-05-17 11:37:14 -07002873 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002874 GR_GL_CALL(
2875 this->glInterface(),
2876 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2877 if (mipLevel == 0) {
2878 texture->baseLevelWasBoundToFBO();
2879 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002880 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002881 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002882 }
egdaniel0f5f9672015-02-03 11:10:51 -08002883}
2884
Brian Salomond2a8ae22019-09-10 16:03:59 -04002885void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002886 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002887 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002888 SkASSERT(surface->asTexture());
2889 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2890 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2891 GR_GL_COLOR_ATTACHMENT0,
2892 textureTarget,
2893 0,
2894 0));
2895 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002896}
2897
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002898void GrGLGpu::onFBOChanged() {
2899 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2900 this->caps()->workarounds().restore_scissor_on_fbo_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 }
2917
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002918 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2919 // The driver forgets the correct scissor when modifying the FBO binding.
2920 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002921 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002922 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002923 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002924 }
2925
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002926 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002927}
2928
Adrienne Walker4ee88512018-05-17 11:37:14 -07002929void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002930 // We're relying on the GL state shadowing being correct in the workaround code below so we
2931 // need to handle a dirty context.
2932 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002933 if (fboid == fBoundDrawFramebuffer &&
2934 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2935 // This workaround only applies to deleting currently bound framebuffers
2936 // on Adreno 420. Because this is a somewhat rare case, instead of
2937 // tracking all the attachments of every framebuffer instead just always
2938 // unbind all attachments.
2939 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2940 GR_GL_RENDERBUFFER, 0));
2941 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2942 GR_GL_RENDERBUFFER, 0));
2943 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2944 GR_GL_RENDERBUFFER, 0));
2945 }
2946
2947 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002948
2949 // Deleting the currently bound framebuffer rebinds to 0.
2950 if (fboid == fBoundDrawFramebuffer) {
2951 this->onFBOChanged();
2952 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002953}
2954
Greg Daniel46cfbc62019-06-07 11:43:30 -04002955bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002956 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002957 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2958 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2959 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002960 auto dstFormat = dst->backendFormat().asGLFormat();
2961 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002962 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002963 return true;
2964 }
2965 }
cblume61214052016-01-26 09:10:48 -08002966
Greg Daniel46cfbc62019-06-07 11:43:30 -04002967 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2968 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002969 return true;
2970 }
2971
Greg Daniel46cfbc62019-06-07 11:43:30 -04002972 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2973 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002974 }
2975
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002976 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002977 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002978 return true;
2979 }
bsalomon083617b2016-02-12 12:10:14 -08002980 }
2981
bsalomon6df86402015-06-01 10:41:49 -07002982 return false;
2983}
2984
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002985bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002986 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002987
2988 int progIdx = TextureToCopyProgramIdx(srcTex);
2989 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002990 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991
2992 if (!fCopyProgramArrayBuffer) {
2993 static const GrGLfloat vdata[] = {
2994 0, 0,
2995 0, 1,
2996 1, 0,
2997 1, 1
2998 };
2999 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3000 kStatic_GrAccessPattern, vdata);
3001 }
3002 if (!fCopyProgramArrayBuffer) {
3003 return false;
3004 }
3005
3006 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3007 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3008 if (!fCopyPrograms[progIdx].fProgram) {
3009 return false;
3010 }
3011
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003012 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003013 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003014 GrShaderVar::TypeModifier::Uniform);
3015 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3016 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3017 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3018 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003019
Greg Daniel9e3169b2019-06-06 14:38:17 -04003020 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003021 if (shaderCaps->noperspectiveInterpolationSupport()) {
3022 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3023 vshaderTxt.appendf("#extension %s : require\n", extension);
3024 }
3025 vTexCoord.addModifier("noperspective");
3026 }
3027
3028 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3029 vshaderTxt.append(";");
3030 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3031 vshaderTxt.append(";");
3032 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3033 vshaderTxt.append(";");
3034 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3035 vshaderTxt.append(";");
3036
3037 vshaderTxt.append(
3038 "// Copy Program VS\n"
3039 "void main() {"
3040 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3041 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3042 " sk_Position.zw = half2(0, 1);"
3043 "}"
3044 );
3045
Greg Daniel9e3169b2019-06-06 14:38:17 -04003046 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003047 if (shaderCaps->noperspectiveInterpolationSupport()) {
3048 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3049 fshaderTxt.appendf("#extension %s : require\n", extension);
3050 }
3051 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003052 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003053 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3054 fshaderTxt.append(";");
3055 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3056 fshaderTxt.append(";");
3057 fshaderTxt.appendf(
3058 "// Copy Program FS\n"
3059 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003060 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003061 "}"
3062 );
3063
3064 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3065 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3066 SkSL::Program::Settings settings;
3067 settings.fCaps = shaderCaps;
3068 SkSL::String glsl;
3069 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3070 sksl, settings, &glsl, errorHandler);
3071 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3072 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3073 SkASSERT(program->fInputs.isEmpty());
3074
3075 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3076 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3077 errorHandler);
3078 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3079 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3080 errorHandler);
3081 SkASSERT(program->fInputs.isEmpty());
3082
3083 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3084
3085 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3086 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3087 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3088 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3089 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3090 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3091
3092 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3093
3094 GL_CALL(DeleteShader(vshader));
3095 GL_CALL(DeleteShader(fshader));
3096
3097 return true;
3098}
3099
brianosman33f6b3f2016-06-02 05:49:21 -07003100bool GrGLGpu::createMipmapProgram(int progIdx) {
3101 const bool oddWidth = SkToBool(progIdx & 0x2);
3102 const bool oddHeight = SkToBool(progIdx & 0x1);
3103 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3104
Brian Salomon1edc5b92016-11-29 13:43:46 -05003105 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003106
3107 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3108 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3109 if (!fMipmapPrograms[progIdx].fProgram) {
3110 return false;
3111 }
3112
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003113 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003114 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003115 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003116 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003117 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003118 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003119 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003120 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3121 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3122 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3123 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003124 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003125 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003126
Ethan Nicholasfc994162019-06-06 10:04:27 -04003127 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003128 if (shaderCaps->noperspectiveInterpolationSupport()) {
3129 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003130 vshaderTxt.appendf("#extension %s : require\n", extension);
3131 }
3132 vTexCoords[0].addModifier("noperspective");
3133 vTexCoords[1].addModifier("noperspective");
3134 vTexCoords[2].addModifier("noperspective");
3135 vTexCoords[3].addModifier("noperspective");
3136 }
3137
Brian Salomon1edc5b92016-11-29 13:43:46 -05003138 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003139 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003140 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003141 vshaderTxt.append(";");
3142 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003143 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003144 vshaderTxt.append(";");
3145 }
3146
3147 vshaderTxt.append(
3148 "// Mipmap Program VS\n"
3149 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003150 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3151 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003152 );
3153
3154 // Insert texture coordinate computation:
3155 if (oddWidth && oddHeight) {
3156 vshaderTxt.append(
3157 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003158 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3159 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003160 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3161 );
3162 } else if (oddWidth) {
3163 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003164 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3165 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003166 );
3167 } else if (oddHeight) {
3168 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003169 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3170 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003171 );
3172 } else {
3173 vshaderTxt.append(
3174 " v_texCoord0 = a_vertex.xy;"
3175 );
3176 }
3177
3178 vshaderTxt.append("}");
3179
Ethan Nicholasfc994162019-06-06 10:04:27 -04003180 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003181 if (shaderCaps->noperspectiveInterpolationSupport()) {
3182 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003183 fshaderTxt.appendf("#extension %s : require\n", extension);
3184 }
3185 }
brianosman33f6b3f2016-06-02 05:49:21 -07003186 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003187 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003188 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003189 fshaderTxt.append(";");
3190 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003191 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003192 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003193 fshaderTxt.append(
3194 "// Mipmap Program FS\n"
3195 "void main() {"
3196 );
3197
3198 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003199 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003200 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3201 " sample(u_texture, v_texCoord1) + "
3202 " sample(u_texture, v_texCoord2) + "
3203 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003204 );
3205 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003206 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003207 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3208 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003209 );
3210 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003211 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003212 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003213 );
3214 }
3215
3216 fshaderTxt.append("}");
3217
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003218 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003219 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003220 SkSL::Program::Settings settings;
3221 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003222 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003223 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003224 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003225 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003226 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003227 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003228
Brian Osman6c431d52019-04-15 16:31:54 -04003229 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003230 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3231 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003232 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003233 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003234 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003235 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003236
3237 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3238
3239 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3240 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3241 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3242 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3243
3244 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3245
3246 GL_CALL(DeleteShader(vshader));
3247 GL_CALL(DeleteShader(fshader));
3248
3249 return true;
3250}
3251
Greg Daniel46cfbc62019-06-07 11:43:30 -04003252bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003253 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003254 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3255 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3256 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3257 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003258 return false;
3259 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003260 int progIdx = TextureToCopyProgramIdx(srcTex);
3261 if (!dstRT) {
3262 SkASSERT(dstTex);
3263 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3264 return false;
3265 }
3266 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003267 if (!fCopyPrograms[progIdx].fProgram) {
3268 if (!this->createCopyProgram(srcTex)) {
3269 SkDebugf("Failed to create copy program.\n");
3270 return false;
3271 }
3272 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003273 int w = srcRect.width();
3274 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003275 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003276 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003277 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003278 this->flushViewport(dst->width(), dst->height());
3279 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003280 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003281 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003282 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003283 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3284 attribs->enableVertexArrays(this, 1);
3285 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3286 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003287 // dst rect edges in NDC (-1 to 1)
3288 int dw = dst->width();
3289 int dh = dst->height();
3290 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3291 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3292 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3293 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3295 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3296 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3297 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3298 int sw = src->width();
3299 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003300 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 // src rect edges in normalized texture space (0 to 1)
3302 sx0 /= sw;
3303 sx1 /= sw;
3304 sy0 /= sh;
3305 sy1 /= sh;
3306 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003307 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3308 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3309 sx1 - sx0, sy1 - sy0, sx0, sy0));
3310 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003311 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003312 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003313 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003314 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003315 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003316 this->disableWindowRectangles();
3317 this->disableStencil();
3318 if (this->glCaps().srgbWriteControl()) {
3319 this->flushFramebufferSRGB(true);
3320 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003321 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003322 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003323 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3324 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003325 return true;
3326}
3327
Greg Daniel46cfbc62019-06-07 11:43:30 -04003328void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003329 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003330 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003331 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003332 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003333 SkASSERT(dstTex);
3334 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003335 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003336
Brian Salomond978b902019-02-07 15:09:18 -05003337 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003338 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003339 dstPoint.fX, dstPoint.fY,
3340 srcRect.fLeft, srcRect.fTop,
3341 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003342 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003343 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3344 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003345 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3346 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003347}
3348
Greg Daniel46cfbc62019-06-07 11:43:30 -04003349bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003350 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003351 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003352 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3353 srcRect.width(), srcRect.height());
3354 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003355 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003356 return false;
mtklein404b3b22015-05-18 09:29:10 -07003357 }
bsalomon5df6fee2015-05-18 06:26:15 -07003358 }
bsalomon6df86402015-06-01 10:41:49 -07003359
Brian Salomond2a8ae22019-09-10 16:03:59 -04003360 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3361 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003362 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003363 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003364
3365 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003366 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003367 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003368
Greg Daniel46cfbc62019-06-07 11:43:30 -04003369 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3370 srcRect.fTop,
3371 srcRect.fRight,
3372 srcRect.fBottom,
3373 dstRect.fLeft,
3374 dstRect.fTop,
3375 dstRect.fRight,
3376 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003377 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003378 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3379 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003380
3381 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3382 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003383 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003384}
3385
Brian Salomon930f9392018-06-20 16:25:26 -04003386bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3387 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003388 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003389 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003390 return false;
3391 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003392 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003393 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3394 // Uses draw calls to do a series of downsample operations to successive mips.
3395
3396 // The manual approach requires the ability to limit which level we're sampling and that the
3397 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003398 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003399 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003400 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003401 GL_CALL(GenerateMipmap(glTex->target()));
3402 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003403 }
3404
brianosman33f6b3f2016-06-02 05:49:21 -07003405 int width = texture->width();
3406 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003407 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003408 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003409
3410 // Create (if necessary), then bind temporary FBO:
3411 if (0 == fTempDstFBOID) {
3412 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3413 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003414 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003415 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003416
3417 // Bind the texture, to get things configured for filtering.
3418 // We'll be changing our base level further below:
3419 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003420 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003421 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003422
3423 // Vertex data:
3424 if (!fMipmapProgramArrayBuffer) {
3425 static const GrGLfloat vdata[] = {
3426 0, 0,
3427 0, 1,
3428 1, 0,
3429 1, 1
3430 };
Brian Salomonae64c192019-02-05 09:41:37 -05003431 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003432 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003433 }
3434 if (!fMipmapProgramArrayBuffer) {
3435 return false;
3436 }
3437
3438 fHWVertexArrayState.setVertexArrayID(this, 0);
3439
3440 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003441 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003442 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003443 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003444
3445 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003446 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003447 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003448 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003449 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003450 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003451
3452 // Do all the blits:
3453 width = texture->width();
3454 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003455
brianosman33f6b3f2016-06-02 05:49:21 -07003456 for (GrGLint level = 1; level < levelCount; ++level) {
3457 // Get and bind the program for this particular downsample (filter shape can vary):
3458 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3459 if (!fMipmapPrograms[progIdx].fProgram) {
3460 if (!this->createMipmapProgram(progIdx)) {
3461 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003462 // Invalidate all params to cover base level change in a previous iteration.
3463 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003464 return false;
3465 }
3466 }
Brian Salomon802cb312018-06-08 18:05:20 -04003467 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003468
3469 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3470 const float invWidth = 1.0f / width;
3471 const float invHeight = 1.0f / height;
3472 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3473 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3474 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3475
3476 // Only sample from previous mip
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003477 SkASSERT(this->glCaps().mipmapLevelControlSupport());
brianosman33f6b3f2016-06-02 05:49:21 -07003478 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3479
Brian Salomon930f9392018-06-20 16:25:26 -04003480 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3481 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003482
Brian Osman788b9162020-02-07 10:36:46 -05003483 width = std::max(1, width / 2);
3484 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003485 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003486
3487 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3488 }
3489
3490 // Unbind:
3491 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3492 GR_GL_TEXTURE_2D, 0, 0));
3493
Brian Salomon930f9392018-06-20 16:25:26 -04003494 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003495 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3496 // We drew the 2nd to last level into the last level.
3497 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3498 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003499
brianosman33f6b3f2016-06-02 05:49:21 -07003500 return true;
3501}
3502
Chris Daltond7291ba2019-03-07 14:17:03 -07003503void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003504 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3505 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003506
3507 int effectiveSampleCnt;
3508 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003509 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003510
3511 sampleLocations->reset(effectiveSampleCnt);
3512 for (int i = 0; i < effectiveSampleCnt; ++i) {
3513 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3514 }
3515}
3516
cdalton231c5fd2015-05-13 12:35:36 -07003517void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003518 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003519 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003520 case kTexture_GrXferBarrierType: {
3521 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003522 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003523 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3524 // The render target uses separate storage so no need for glTextureBarrier.
3525 // FIXME: The render target will resolve automatically when its texture is bound,
3526 // but we could resolve only the bounds that will be read if we do it here instead.
3527 return;
3528 }
cdalton9954bc32015-04-29 14:17:00 -07003529 SkASSERT(this->caps()->textureBarrierSupport());
3530 GL_CALL(TextureBarrier());
3531 return;
cdalton231c5fd2015-05-13 12:35:36 -07003532 }
cdalton8917d622015-05-06 13:40:21 -07003533 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003534 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003535 this->caps()->blendEquationSupport());
3536 GL_CALL(BlendBarrier());
3537 return;
bsalomoncb02b382015-08-12 11:14:50 -07003538 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003539 }
3540}
3541
Chris Daltone1196c52019-12-28 14:31:09 -07003542void GrGLGpu::insertManualFramebufferBarrier() {
3543 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3544 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3545}
3546
Brian Salomon85c3d682019-11-04 15:04:54 -05003547GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003548 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003549 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003550 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003551 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003552 // We don't support protected textures in GL.
3553 if (isProtected == GrProtected::kYes) {
3554 return {};
3555 }
3556
Brian Salomon8a375832018-03-14 10:21:40 -04003557 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003558
Brian Salomond4764a12019-08-08 12:08:24 -04003559 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003560 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003561 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003562 }
3563
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003564 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003565 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003566 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003567 }
3568
Robert Phillipsd34691b2019-09-24 13:38:43 -04003569 // Compressed formats go through onCreateCompressedBackendTexture
3570 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3571
Greg Daniel15500642019-06-27 03:05:55 +00003572 GrGLTextureInfo info;
3573 GrGLTextureParameters::SamplerOverriddenState initialState;
3574
Greg Danield51fa2f2020-01-22 16:53:38 -05003575 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003576 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003577 }
Brian Salomon0f396992020-06-19 19:51:21 -04003578 switch (format.textureType()) {
3579 case GrTextureType::kNone:
3580 case GrTextureType::kExternal:
3581 return {};
3582 case GrTextureType::k2D:
3583 info.fTarget = GR_GL_TEXTURE_2D;
3584 break;
3585 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003586 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003587 return {};
3588 }
3589 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3590 break;
3591 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003592 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003593 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3594 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003595 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003596 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003597 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003598
Brian Salomon85c3d682019-11-04 15:04:54 -05003599 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003600 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003601
Brian Salomone2826ab2019-06-04 15:58:31 -04003602 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003603 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003604 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3605 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003606
Brian Salomon85c3d682019-11-04 15:04:54 -05003607 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3608 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003609}
3610
Greg Daniel16032b32020-05-06 15:31:10 -04003611bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3612 sk_sp<GrRefCntedCallback> finishedCallback,
3613 const BackendTextureData* data) {
3614 GrGLTextureInfo info;
3615 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3616
3617 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003618 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003619 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003620 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003621 }
3622
3623 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3624
Brian Salomon22558932020-05-15 14:46:34 -04003625 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003626
Brian Salomonc3f49432020-06-03 10:21:50 -04003627 // 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 -04003628 // so that the uploads go to the right levels.
Brian Salomon9e5a60c2020-09-08 11:48:08 -04003629 if (numMipLevels && this->glCaps().mipmapLevelControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003630 auto params = backendTexture.getGLTextureParams();
3631 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3632 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003633 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003634 nonsamplerState.fBaseMipMapLevel = 0;
3635 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003636 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003637 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003638 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3639 }
3640 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3641 }
3642
Greg Daniel16032b32020-05-06 15:31:10 -04003643 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003644 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003645 if (data->type() == BackendTextureData::Type::kPixmaps) {
3646 SkTDArray<GrMipLevel> texels;
3647 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3648 texels.append(numMipLevels);
3649 for (int i = 0; i < numMipLevels; ++i) {
3650 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3651 }
Brian Salomon22558932020-05-15 14:46:34 -04003652 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3653 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3654 info.fTarget, dstRect, colorType, texels.begin(),
3655 texels.count());
3656 } else if (data->type() == BackendTextureData::Type::kColor) {
3657 uint32_t levelMask = (1 << numMipLevels) - 1;
3658 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3659 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003660 }
3661
3662 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003663 this->bindTextureToScratchUnit(info.fTarget, 0);
3664 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003665}
3666
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003667void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003668 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3669
3670 GrGLTextureInfo info;
3671 if (tex.getGLTextureInfo(&info)) {
3672 GL_CALL(DeleteTextures(1, &info.fID));
3673 }
3674}
3675
Robert Phillips3bce1f72020-05-12 12:41:58 -04003676bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3677 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3678
3679 Stats::ProgramCacheResult stat;
3680
3681 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3682 if (!tmp) {
3683 return false;
3684 }
3685
3686 return stat != Stats::ProgramCacheResult::kHit;
3687}
3688
Robert Phillipsf0ced622019-05-16 09:06:25 -04003689#if GR_TEST_UTILS
3690
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003691bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003692 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003693
Greg Daniel52e16d92018-04-10 09:34:07 -04003694 GrGLTextureInfo info;
3695 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003696 return false;
3697 }
3698
3699 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003700 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003701
3702 return (GR_GL_TRUE == result);
3703}
3704
Brian Salomon72c7b982020-10-06 10:07:38 -04003705GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(SkISize dimensions,
3706 GrColorType colorType,
3707 int sampleCnt) {
3708 if (dimensions.width() > this->caps()->maxRenderTargetSize() ||
3709 dimensions.height() > this->caps()->maxRenderTargetSize()) {
3710 return {};
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003711 }
Brian Salomon8a375832018-03-14 10:21:40 -04003712 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003713 auto format = this->glCaps().getFormatFromColorType(colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -04003714 sampleCnt = this->glCaps().getRenderTargetSampleCount(sampleCnt, format);
3715 if (!sampleCnt) {
Brian Salomonf865b052018-03-09 09:01:53 -05003716 return {};
3717 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003718 // We make a texture instead of a render target if we're using a
3719 // "multisampled_render_to_texture" style extension or have a BGRA format that
3720 // is allowed for textures but not render buffer internal formats.
3721 bool useTexture = false;
3722 if (sampleCnt > 1 && !this->glCaps().usesMSAARenderBuffers()) {
3723 useTexture = true;
3724 } else if (format == GrGLFormat::kBGRA8 &&
3725 this->glCaps().getRenderbufferInternalFormat(GrGLFormat::kBGRA8) != GR_GL_BGRA8) {
3726 // We have a BGRA extension that doesn't support BGRA render buffers. We can use a texture
3727 // unless we've been asked for MSAA. Note we already checked above for render-to-
3728 // multisampled-texture style extensions.
3729 if (sampleCnt > 1) {
3730 return {};
3731 }
3732 useTexture = true;
3733 }
Brian Salomonf6da1462019-07-11 14:21:59 -04003734 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003735 if (sFormatIdx < 0) {
3736 return {};
3737 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003738 GrGLuint colorID = 0;
3739 GrGLuint stencilID = 0;
Brian Salomon72c7b982020-10-06 10:07:38 -04003740 GrGLFramebufferInfo info;
3741 info.fFBOID = 0;
3742 info.fFormat = GrGLFormatToEnum(format);
3743
3744 auto deleteIDs = [&](bool saveFBO = false) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003745 if (colorID) {
3746 if (useTexture) {
3747 GL_CALL(DeleteTextures(1, &colorID));
3748 } else {
3749 GL_CALL(DeleteRenderbuffers(1, &colorID));
3750 }
Brian Salomonf865b052018-03-09 09:01:53 -05003751 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003752 if (stencilID) {
3753 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003754 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003755 if (!saveFBO && info.fFBOID) {
3756 this->deleteFramebuffer(info.fFBOID);
3757 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003758 };
3759
3760 if (useTexture) {
3761 GL_CALL(GenTextures(1, &colorID));
3762 } else {
3763 GL_CALL(GenRenderbuffers(1, &colorID));
3764 }
3765 GL_CALL(GenRenderbuffers(1, &stencilID));
3766 if (!stencilID || !colorID) {
3767 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003768 return {};
3769 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003770
Brian Salomonf865b052018-03-09 09:01:53 -05003771 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3772 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003773 deleteIDs();
3774 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003775 }
3776
3777 this->invalidateBoundRenderTarget();
3778
Adrienne Walker4ee88512018-05-17 11:37:14 -07003779 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003780 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003781 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon72c7b982020-10-06 10:07:38 -04003782 colorID = this->createTexture(dimensions, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
Brian Salomon0f396992020-06-19 19:51:21 -04003783 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003784 if (!colorID) {
3785 deleteIDs();
3786 return {};
3787 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003788 if (sampleCnt == 1) {
3789 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3790 GR_GL_TEXTURE_2D, colorID, 0));
3791 } else {
3792 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3793 GR_GL_TEXTURE_2D, colorID, 0, sampleCnt));
3794 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003795 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003796 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003797 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomon72c7b982020-10-06 10:07:38 -04003798 if (sampleCnt == 1) {
3799 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, dimensions.width(),
3800 dimensions.height()));
3801 } else {
3802 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt, renderBufferFormat,
3803 dimensions.width(), dimensions.height())) {
3804 deleteIDs();
3805 return {};
3806 }
3807 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003808 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3809 GR_GL_RENDERBUFFER, colorID));
3810 }
3811 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003812 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomon72c7b982020-10-06 10:07:38 -04003813 if (sampleCnt == 1) {
3814 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, dimensions.width(),
3815 dimensions.height()));
3816 } else {
3817 if (!this->renderbufferStorageMSAA(this->glContext(), sampleCnt, stencilBufferFormat,
3818 dimensions.width(), dimensions.height())) {
3819 deleteIDs();
3820 return {};
3821 }
3822 }
Brian Salomonf865b052018-03-09 09:01:53 -05003823 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003824 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003825 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3826 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003827 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003828 }
3829
Brian Salomon93348dd2018-08-29 12:56:23 -04003830 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3831 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3832 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3833 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003834 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon72c7b982020-10-06 10:07:38 -04003835 deleteIDs(/* saveFBO = */ true);
Brian Salomonf865b052018-03-09 09:01:53 -05003836
Adrienne Walker4ee88512018-05-17 11:37:14 -07003837 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003838 GrGLenum status;
3839 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3840 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003841 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003842 return {};
3843 }
Brian Salomon72c7b982020-10-06 10:07:38 -04003844
Brian Salomonf865b052018-03-09 09:01:53 -05003845 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003846
Brian Salomon72c7b982020-10-06 10:07:38 -04003847 GrBackendRenderTarget beRT = GrBackendRenderTarget(dimensions.width(), dimensions.height(),
3848 sampleCnt, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003849 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003850 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003851}
3852
3853void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003854 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003855 GrGLFramebufferInfo info;
3856 if (backendRT.getGLFramebufferInfo(&info)) {
3857 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003858 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003859 }
3860 }
joshualitt8fd844f2015-12-02 13:36:47 -08003861}
3862
Greg Daniel26b50a42018-03-08 09:49:58 -05003863void GrGLGpu::testingOnly_flushGpuAndSync() {
3864 GL_CALL(Finish());
3865}
Brian Salomonf865b052018-03-09 09:01:53 -05003866#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003867
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003868///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003869
cdaltone2e71c22016-04-07 18:13:29 -07003870GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003871 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003872 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003873 GrGLAttribArrayState* attribState;
3874
cdaltone2e71c22016-04-07 18:13:29 -07003875 if (gpu->glCaps().isCoreProfile()) {
3876 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003877 GrGLuint arrayID;
3878 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3879 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003880 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003881 }
cdaltone2e71c22016-04-07 18:13:29 -07003882 if (ibuf) {
3883 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003884 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003885 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003886 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003887 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003888 if (ibuf) {
3889 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003890 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003891 } else {
3892 this->setVertexArrayID(gpu, 0);
3893 }
3894 int attrCount = gpu->glCaps().maxVertexAttributes();
3895 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3896 fDefaultVertexArrayAttribState.resize(attrCount);
3897 }
3898 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003899 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003900 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003901}
bsalomone179a912016-01-20 06:18:10 -08003902
Greg Danielfe159622020-04-10 17:43:51 +00003903void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3904 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003905 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003906}
3907
Greg Daniel78be37f2020-04-14 16:40:35 -04003908void GrGLGpu::flush(FlushType flushType) {
3909 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3910 GL_CALL(Flush());
3911 fNeedsGLFlush = false;
3912 }
3913}
3914
Greg Danielfe159622020-04-10 17:43:51 +00003915bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3916 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003917 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003918 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003919 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003920 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003921 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003922 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003923 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003924 if (!this->glCaps().skipErrorChecks()) {
3925 this->clearErrorsAndCheckForOOM();
3926 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003927 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003928}
3929
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003930void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3931 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3932 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3933 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003934}
3935
Greg Daniel6be35232017-03-01 17:01:09 -05003936GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003937 if (!this->caps()->fenceSyncSupport()) {
3938 return 0;
3939 }
Greg Daniel6be35232017-03-01 17:01:09 -05003940 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003941 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3942 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3943 GrGLuint fence = 0;
3944 GL_CALL(GenFences(1, &fence));
3945 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3946 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3947 } else {
3948 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3949 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003950 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003951 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003952 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003953}
3954
Brian Salomonb0d8b762019-05-06 16:58:22 -04003955bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003956 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3957 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3958 if (!timeout) {
3959 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003960 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003961 }
3962 GrGLboolean result;
3963 GL_CALL_RET(result, TestFence(nvFence));
3964 return result == GR_GL_TRUE;
3965 }
3966 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3967 // If this really becomes necessary we could poll TestFence().
3968 // FinishFence always flushes so no need to check flush param.
3969 GL_CALL(FinishFence(nvFence));
3970 return true;
3971 } else {
3972 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3973 GrGLenum result;
3974 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3975 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3976 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003977}
3978
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003979bool GrGLGpu::waitFence(GrFence fence) {
3980 if (!this->caps()->fenceSyncSupport()) {
3981 return true;
3982 }
3983 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003984}
3985
3986void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003987 if (this->caps()->fenceSyncSupport()) {
3988 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3989 }
Greg Daniel6be35232017-03-01 17:01:09 -05003990}
3991
Greg Daniel301015c2019-11-18 14:06:46 -05003992std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003993 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003994 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003995}
3996
Greg Daniel301015c2019-11-18 14:06:46 -05003997std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3998 const GrBackendSemaphore& semaphore,
3999 GrResourceProvider::SemaphoreWrapType wrapType,
4000 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04004001 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04004002 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
4003}
4004
Greg Daniel301015c2019-11-18 14:06:46 -05004005void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004006 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004007 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004008
Greg Daniel48661b82018-01-22 16:11:35 -05004009 GrGLsync sync;
4010 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
4011 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04004012 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05004013}
4014
Greg Daniel301015c2019-11-18 14:06:46 -05004015void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04004016 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004017 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05004018
4019 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4020}
4021
Brian Salomonb0d8b762019-05-06 16:58:22 -04004022void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004023 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004024}
4025
Brian Salomon24069eb2020-06-24 10:19:52 -04004026void GrGLGpu::clearErrorsAndCheckForOOM() {
4027 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4028}
4029
4030GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4031#if GR_GL_CHECK_ERROR
4032 if (this->glInterface()->checkAndResetOOMed()) {
4033 this->setOOMed();
4034 }
4035#endif
4036 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4037 if (error == GR_GL_OUT_OF_MEMORY) {
4038 this->setOOMed();
4039 }
4040 return error;
4041}
4042
Greg Daniel6be35232017-03-01 17:01:09 -05004043void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004044 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4045 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4046 GL_CALL(DeleteFences(1, &nvFence));
4047 } else {
4048 GL_CALL(DeleteSync(sync));
4049 }
jvanverth84741b32016-09-30 08:39:02 -07004050}
Brian Osman13dddce2017-05-09 13:19:50 -04004051
Greg Daniel301015c2019-11-18 14:06:46 -05004052std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004053 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004054 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004055 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004056 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004057 // 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 -04004058 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004059
4060 return semaphore;
4061}
Robert Phillips646e4292017-06-13 12:44:56 -04004062
4063int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004064 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004065 case kTexture2DSampler_GrSLType:
4066 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004067 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004068 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004069 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004070 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004071 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004072 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004073 }
4074}
Brian Osman71a18892017-08-10 10:23:25 -04004075
Kevin Lubickf4def342018-10-04 12:52:50 -04004076#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004077#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004078void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4079 // We are called by the base class, which has already called beginObject(). We choose to nest
4080 // all of our caps information in a named sub-object.
4081 writer->beginObject("GL GPU");
4082
4083 const GrGLubyte* str;
4084 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4085 writer->appendString("GL_VERSION", (const char*)(str));
4086 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4087 writer->appendString("GL_RENDERER", (const char*)(str));
4088 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4089 writer->appendString("GL_VENDOR", (const char*)(str));
4090 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4091 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4092
4093 writer->appendName("extensions");
4094 glInterface()->fExtensions.dumpJSON(writer);
4095
4096 writer->endObject();
4097}
Kevin Lubickf4def342018-10-04 12:52:50 -04004098#endif