blob: 117a78a806a1dce52f370a9ebb64b5904d514c5c [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPixmap.h"
9#include "include/core/SkStrokeRec.h"
10#include "include/core/SkTypes.h"
11#include "include/gpu/GrBackendSemaphore.h"
12#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040013#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/gpu/GrTypes.h"
15#include "include/private/SkHalf.h"
16#include "include/private/SkTemplates.h"
17#include "include/private/SkTo.h"
18#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050019#include "src/core/SkCompressedDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkConvertPixels.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrRenderTargetPriv.h"
31#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
33#include "src/gpu/GrTexturePriv.h"
34#include "src/gpu/gl/GrGLBuffer.h"
35#include "src/gpu/gl/GrGLGpu.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040036#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/gl/GrGLSemaphore.h"
38#include "src/gpu/gl/GrGLStencilAttachment.h"
39#include "src/gpu/gl/GrGLTextureRenderTarget.h"
40#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
41#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000042
Hal Canaryc640d0d2018-06-13 09:59:02 -040043#include <cmath>
44
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);
190 case GrSamplerState::MipmapMode::kLinear:
191 switch (filter) {
192 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_LINEAR;
193 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_LINEAR;
194 }
195 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400196 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400197 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400198}
199
Michael Ludwigf23a1522018-12-10 11:36:13 -0500200static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
201 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400202 switch (wrapMode) {
203 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
204 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
205 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500206 case GrSamplerState::WrapMode::kClampToBorder:
207 // May not be supported but should have been caught earlier
208 SkASSERT(caps.clampToBorderSupport());
209 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500210 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400211 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400212}
213
214///////////////////////////////////////////////////////////////////////////////
215
216class GrGLGpu::SamplerObjectCache {
217public:
218 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
219 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
220 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
221 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
222 std::fill_n(fSamplers, kNumSamplers, 0);
223 }
224
225 ~SamplerObjectCache() {
226 if (!fNumTextureUnits) {
227 // We've already been abandoned.
228 return;
229 }
Chris Dalton703fe682019-05-15 12:58:12 -0600230 for (GrGLuint sampler : fSamplers) {
231 // The spec states that "zero" values should be silently ignored, however they still
232 // trigger GL errors on some NVIDIA platforms.
233 if (sampler) {
234 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
235 }
236 }
Brian Salomondc829942018-10-23 16:07:24 -0400237 }
238
Brian Salomonccb61422020-01-09 10:46:36 -0500239 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomona7d9f302020-07-15 13:25:30 -0400240 int index = state.asIndex();
Brian Salomondc829942018-10-23 16:07:24 -0400241 if (!fSamplers[index]) {
242 GrGLuint s;
243 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
244 if (!s) {
245 return;
246 }
247 fSamplers[index] = s;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400248 GrGLenum minFilter = filter_to_gl_min_filter(state.filter(), state.mipmapMode());
249 GrGLenum magFilter = filter_to_gl_mag_filter(state.filter());
250 GrGLenum wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
251 GrGLenum wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400252 GR_GL_CALL(fGpu->glInterface(),
253 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
254 GR_GL_CALL(fGpu->glInterface(),
255 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
256 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
257 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
258 }
259 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
260 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
261 fHWBoundSamplers[unitIdx] = fSamplers[index];
262 }
263 }
264
265 void invalidateBindings() {
266 // When we have sampler support we always use samplers. So setting these to zero will cause
267 // a rebind on next usage.
268 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
269 }
270
271 void abandon() {
272 fHWBoundSamplers.reset();
273 fNumTextureUnits = 0;
274 }
275
276 void release() {
277 if (!fNumTextureUnits) {
278 // We've already been abandoned.
279 return;
280 }
281 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
282 std::fill_n(fSamplers, kNumSamplers, 0);
283 // Deleting a bound sampler implicitly binds sampler 0.
284 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
285 }
286
287private:
Brian Salomona7d9f302020-07-15 13:25:30 -0400288 static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
Brian Salomondc829942018-10-23 16:07:24 -0400289 GrGLGpu* fGpu;
Brian Salomondc829942018-10-23 16:07:24 -0400290 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
291 GrGLuint fSamplers[kNumSamplers];
292 int fNumTextureUnits;
293};
294
reed@google.comac10a2d2010-12-22 21:39:39 +0000295///////////////////////////////////////////////////////////////////////////////
296
Brian Salomon384fab42017-12-07 12:33:05 -0500297sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
Adlai Holler3d0359a2020-07-09 15:35:55 -0400298 GrDirectContext* direct) {
Brian Salomon384fab42017-12-07 12:33:05 -0500299 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500300 interface = GrGLMakeNativeInterface();
301 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
302 // to GrGLMakeNativeInterface.
303 if (!interface) {
304 interface = sk_ref_sp(GrGLCreateNativeInterface());
305 }
Brian Salomon384fab42017-12-07 12:33:05 -0500306 if (!interface) {
307 return nullptr;
308 }
bsalomon424cc262015-05-22 10:37:30 -0700309 }
Jim Van Verth76334772017-05-05 16:46:05 -0400310#ifdef USE_NSIGHT
311 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
312#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500313 auto glContext = GrGLContext::Make(std::move(interface), options);
314 if (!glContext) {
315 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700316 }
Adlai Holler3d0359a2020-07-09 15:35:55 -0400317 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
bsalomon424cc262015-05-22 10:37:30 -0700318}
319
Adlai Holler3d0359a2020-07-09 15:35:55 -0400320GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* direct)
321 : GrGpu(direct)
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500322 , fGLContext(std::move(ctx))
323 , fProgramCache(new ProgramCache(this))
324 , fHWProgramID(0)
325 , fTempSrcFBOID(0)
326 , fTempDstFBOID(0)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400327 , fStencilClearFBOID(0)
328 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500329 SkASSERT(fGLContext);
Brian Salomon24069eb2020-06-24 10:19:52 -0400330 // Clear errors so we don't get confused whether we caused an error.
331 this->clearErrorsAndCheckForOOM();
332 // Toss out any pre-existing OOM that was hanging around before we got started.
333 this->checkAndResetOOMed();
334
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500335 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000336
Brian Salomond978b902019-02-07 15:09:18 -0500337 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000338
Brian Salomonae64c192019-02-05 09:41:37 -0500339 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
340 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600341 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500342 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500343 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
344 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
345 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
346 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700347 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500348 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
349 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700350 }
Brian Salomonae64c192019-02-05 09:41:37 -0500351 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400352 fHWBufferState[i].invalidate();
353 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600354 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700355
356 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
357 fPathRendering.reset(new GrGLPathRendering(this));
358 }
359
Brian Salomondc829942018-10-23 16:07:24 -0400360 if (this->glCaps().samplerObjectSupport()) {
361 fSamplerObjectCache.reset(new SamplerObjectCache(this));
362 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000363}
364
bsalomon861e1032014-12-16 07:33:49 -0800365GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700366 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
367 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800368 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700369 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700370 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800371
Chris Dalton20e7a532020-02-28 15:37:53 +0000372 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400373 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000374 // detach the current program so there is no confusion on OpenGL's part
375 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000376 GL_CALL(UseProgram(0));
377 }
378
Brian Salomon43f8bf02017-10-18 08:33:29 -0400379 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700380 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800381 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400382 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700383 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800384 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400385 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700386 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800387 }
egdaniel0f5f9672015-02-03 11:10:51 -0800388
bsalomon7ea33f52015-11-22 14:51:00 -0800389 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
390 if (0 != fCopyPrograms[i].fProgram) {
391 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
392 }
bsalomon6df86402015-06-01 10:41:49 -0700393 }
bsalomon6dea83f2015-12-03 12:58:06 -0800394
brianosman33f6b3f2016-06-02 05:49:21 -0700395 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
396 if (0 != fMipmapPrograms[i].fProgram) {
397 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
398 }
399 }
400
Brian Salomondc829942018-10-23 16:07:24 -0400401 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500402
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400403 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700404}
405
bsalomon6e2aad42016-04-01 11:54:31 -0700406void GrGLGpu::disconnect(DisconnectType type) {
407 INHERITED::disconnect(type);
408 if (DisconnectType::kCleanup == type) {
409 if (fHWProgramID) {
410 GL_CALL(UseProgram(0));
411 }
412 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700413 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700414 }
415 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700416 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700417 }
418 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700419 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700420 }
bsalomon6e2aad42016-04-01 11:54:31 -0700421 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
422 if (fCopyPrograms[i].fProgram) {
423 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
424 }
425 }
brianosman33f6b3f2016-06-02 05:49:21 -0700426 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
427 if (fMipmapPrograms[i].fProgram) {
428 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
429 }
430 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400431
Brian Salomondc829942018-10-23 16:07:24 -0400432 if (fSamplerObjectCache) {
433 fSamplerObjectCache->release();
434 }
bsalomon6e2aad42016-04-01 11:54:31 -0700435 } else {
436 if (fProgramCache) {
437 fProgramCache->abandon();
438 }
Brian Salomondc829942018-10-23 16:07:24 -0400439 if (fSamplerObjectCache) {
440 fSamplerObjectCache->abandon();
441 }
bsalomon6e2aad42016-04-01 11:54:31 -0700442 }
443
Chris Dalton20e7a532020-02-28 15:37:53 +0000444 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500445 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700446
bsalomonc8dc1f72014-08-21 13:02:13 -0700447 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700448 fTempSrcFBOID = 0;
449 fTempDstFBOID = 0;
450 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700451 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800452 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
453 fCopyPrograms[i].fProgram = 0;
454 }
brianosman33f6b3f2016-06-02 05:49:21 -0700455 fMipmapProgramArrayBuffer.reset();
456 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
457 fMipmapPrograms[i].fProgram = 0;
458 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400459
jvanverthe9c0fc62015-04-29 11:18:05 -0700460 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700461 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700462 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400463 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464}
465
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000466///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000467
bsalomon861e1032014-12-16 07:33:49 -0800468void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000469 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400470 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000471 GL_CALL(Disable(GR_GL_DEPTH_TEST));
472 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000473
Brian Salomonf0861672017-05-08 11:10:10 -0400474 // We don't use face culling.
475 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600476 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
477 // just set this to the default for self-consistency.
478 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400479
Brian Salomonae64c192019-02-05 09:41:37 -0500480 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
481 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700482
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400483 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500484#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000485 // Desktop-only state that we never change
486 if (!this->glCaps().isCoreProfile()) {
487 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
488 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
489 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
490 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
491 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
492 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
493 }
494 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
495 // core profile. This seems like a bug since the core spec removes any mention of
496 // GL_ARB_imaging.
497 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
498 GL_CALL(Disable(GR_GL_COLOR_TABLE));
499 }
500 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400501
Chris Dalton1215cda2019-12-17 21:44:04 -0700502 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500503#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000504 // Since ES doesn't support glPointSize at all we always use the VS to
505 // set the point size
506 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
507
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000508 }
joshualitt58162332014-08-01 06:44:53 -0700509
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400510 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500511 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700512 // The arm extension requires specifically enabling MSAA fetching per sample.
513 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500514 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700515 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000516 fHWWriteToColor = kUnknown_TriState;
517 // we only ever use lines in hairline mode
518 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700519 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500520
521 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000522 }
edisonn@google.comba669992013-06-28 16:03:21 +0000523
egdanielb414f252014-07-29 13:15:47 -0700524 if (resetBits & kMSAAEnable_GrGLBackendState) {
525 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700526
Chris Dalton6ce447a2019-06-23 18:07:38 -0600527 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800528 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
529 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700530 GL_CALL(CoverageModulation(GR_GL_RGBA));
531 }
Chris Daltonce425af2019-12-16 10:39:03 -0700532
533 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000534 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000536 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400537 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000538
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000539 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500540 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500541 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000542 }
Brian Salomondc829942018-10-23 16:07:24 -0400543 if (fSamplerObjectCache) {
544 fSamplerObjectCache->invalidateBindings();
545 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000546 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000547
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000548 if (resetBits & kBlend_GrGLBackendState) {
549 fHWBlendState.invalidate();
550 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000551
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000552 if (resetBits & kView_GrGLBackendState) {
553 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700554 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 fHWViewport.invalidate();
556 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000557
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 if (resetBits & kStencil_GrGLBackendState) {
559 fHWStencilSettings.invalidate();
560 fHWStencilTestEnabled = kUnknown_TriState;
561 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000562
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000563 // Vertex
564 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700565 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500566 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
567 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600568 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700569 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000572 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500573 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700574 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500575 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000576 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000577
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000578 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700579 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700580 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000581 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000582 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000583
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000584 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000585 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400586 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
588 }
Brian Salomon1047a492019-07-02 12:25:21 -0400589 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000590 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
591 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 if (this->glCaps().packFlipYSupport()) {
593 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
594 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000595 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000596
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 if (resetBits & kProgram_GrGLBackendState) {
598 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000599 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000600 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400601 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000602}
603
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400604static bool check_backend_texture(const GrBackendTexture& backendTex,
605 const GrGLCaps& caps,
606 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400607 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400608 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400609 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400610 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800611 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000612
Brian Salomonea4ad302019-08-07 13:04:55 -0400613 desc->fSize = {backendTex.width(), backendTex.height()};
614 desc->fTarget = info.fTarget;
615 desc->fID = info.fID;
616 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800617
Brian Salomonea4ad302019-08-07 13:04:55 -0400618 if (desc->fFormat == GrGLFormat::kUnknown) {
619 return false;
620 }
621 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400622 if (!caps.shaderCaps()->externalTextureSupport()) {
623 return false;
624 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400625 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
626 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400627 return false;
628 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400629 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400630 return false;
631 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400632 if (backendTex.isProtected()) {
633 // Not supported in GL backend at this time.
634 return false;
635 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400636
Brian Salomond17f6582017-07-19 18:28:58 -0400637 return true;
638}
639
640sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400641 GrWrapOwnership ownership,
642 GrWrapCacheable cacheable,
643 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400644 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400645 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400646 return nullptr;
647 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400648
Brian Salomond17f6582017-07-19 18:28:58 -0400649 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400650 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400651 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400652 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400653 }
bsalomone5286e02016-01-14 09:24:09 -0800654
Brian Salomon40a40622020-07-21 10:32:07 -0400655 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400656 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400657
Brian Salomona6db5102020-07-21 09:56:23 -0400658 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400659 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400660 // We don't know what parameters are already set on wrapped textures.
661 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600662 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400663}
664
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500665static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
666 const GrGLCaps& caps, GrGLTexture::Desc* desc,
667 bool skipRectTexSupportCheck = false) {
668 GrGLTextureInfo info;
669 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
670 return false;
671 }
672
673 desc->fSize = {backendTex.width(), backendTex.height()};
674 desc->fTarget = info.fTarget;
675 desc->fID = info.fID;
676 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
677
678 if (desc->fFormat == GrGLFormat::kUnknown) {
679 return false;
680 }
681
682 if (GR_GL_TEXTURE_2D != desc->fTarget) {
683 return false;
684 }
685 if (backendTex.isProtected()) {
686 // Not supported in GL backend at this time.
687 return false;
688 }
689
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500690 return true;
691}
692
Robert Phillipsb915c942019-12-17 14:44:37 -0500693sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
694 GrWrapOwnership ownership,
695 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500696 GrGLTexture::Desc desc;
697 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
698 return nullptr;
699 }
700
701 if (kBorrow_GrWrapOwnership == ownership) {
702 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
703 } else {
704 desc.fOwnership = GrBackendObjectOwnership::kOwned;
705 }
706
Brian Salomon40a40622020-07-21 10:32:07 -0400707 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400708 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500709
Brian Salomona6db5102020-07-21 09:56:23 -0400710 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500711 backendTex.getGLTextureParams(), cacheable,
712 kRead_GrIOType);
713 // We don't know what parameters are already set on wrapped textures.
714 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600715 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500716}
717
Brian Salomond17f6582017-07-19 18:28:58 -0400718sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400719 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500720 GrWrapOwnership ownership,
721 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400722 const GrGLCaps& caps = this->glCaps();
723
Brian Salomonea4ad302019-08-07 13:04:55 -0400724 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400725 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800726 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800727 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400728 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
729 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800730
Brian Salomond17f6582017-07-19 18:28:58 -0400731 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400732 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800733 return nullptr;
734 }
bsalomon10528f12015-10-14 12:54:52 -0700735
Brian Osman766fcbb2017-03-13 09:33:09 -0400736 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400737 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400738 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800740 }
bsalomonb15b4c12014-10-29 12:41:57 -0700741
Robert Phillips0902c982019-07-16 07:47:56 -0400742
Greg Daniel6fa62e22019-08-07 15:52:37 -0400743 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
744 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000745
Brian Salomonea4ad302019-08-07 13:04:55 -0400746 GrGLRenderTarget::IDs rtIDs;
747 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400748 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000749 }
Greg Daniel177e6952017-10-12 12:27:11 -0400750
Brian Salomon40a40622020-07-21 10:32:07 -0400751 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400752 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400753
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500754 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400755 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400756 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400757 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400758 // We don't know what parameters are already set on wrapped textures.
759 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600760 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000761}
762
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400763sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400764 GrGLFramebufferInfo info;
765 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000766 return nullptr;
767 }
768
Brian Salomone8a766b2019-07-19 14:24:36 -0400769 if (backendRT.isProtected()) {
770 // Not supported in GL at this time.
771 return nullptr;
772 }
773
Brian Salomond4764a12019-08-08 12:08:24 -0400774 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400775 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
776 return nullptr;
777 }
778
Brian Salomonea4ad302019-08-07 13:04:55 -0400779 GrGLRenderTarget::IDs rtIDs;
780 rtIDs.fRTFBOID = info.fFBOID;
781 rtIDs.fMSColorRenderbufferID = 0;
782 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
783 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000784
Greg Daniel6fa62e22019-08-07 15:52:37 -0400785 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700786
Brian Salomona56a7462020-02-07 14:17:25 -0500787 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400788 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000789}
790
Greg Daniel7ef28f32017-04-20 16:41:55 +0000791sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400792 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400793 GrGLTexture::Desc desc;
794 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
795 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400796 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800797 return nullptr;
798 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400799
800 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
801 return nullptr;
802 }
803
804 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400805 GrGLRenderTarget::IDs rtIDs;
806 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800807 return nullptr;
808 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500809 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800810}
811
Brian Salomonc320b152018-02-20 14:05:36 -0500812static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700813 if (!glTex) {
814 return false;
815 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000816
bsalomone5286e02016-01-14 09:24:09 -0800817 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
818 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800819 return false;
820 }
821
jvanverth17aa0472016-01-05 10:41:27 -0800822 return true;
823}
824
Brian Salomona9b04b92018-06-01 15:04:28 -0400825bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400826 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400827 const GrMipLevel texels[], int mipLevelCount,
828 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500829 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800830
Brian Salomonc320b152018-02-20 14:05:36 -0500831 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800832 return false;
833 }
834
Brian Salomond978b902019-02-07 15:09:18 -0500835 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000836
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400837 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400838 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
839 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
840 glTex->target(), dstRect, srcColorType, texels,
841 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000842}
843
Brian Salomone05ba5a2019-04-08 11:59:07 -0400844bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400845 GrColorType textureColorType, GrColorType bufferColorType,
846 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400847 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400848
Jim Van Verth1676cb92019-01-15 13:24:45 -0500849 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400850 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500851
Brian Salomonc320b152018-02-20 14:05:36 -0500852 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400853 return false;
854 }
855
Hal Canaryc36f7e72018-06-18 15:50:09 -0400856 static_assert(sizeof(int) == sizeof(int32_t), "");
857 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400858 return false;
859 }
860
Brian Salomond978b902019-02-07 15:09:18 -0500861 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400862
863 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500864 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400865 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500866 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400867
Greg Daniel660cc992017-06-26 14:55:05 -0400868 SkDEBUGCODE(
869 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
870 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
871 SkASSERT(bounds.contains(subRect));
872 )
873
Brian Salomonb28cb682019-07-26 12:48:47 -0400874 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400875 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400876 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700877 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400878 return false;
879 }
880
881 bool restoreGLRowLength = false;
882 if (trimRowBytes != rowBytes) {
883 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400884 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400885 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
886 restoreGLRowLength = true;
887 }
888
Greg Danielba88ab62019-07-26 09:14:01 -0400889 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400890 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400891 GrGLenum externalFormat = 0;
892 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400893 this->glCaps().getTexSubImageExternalFormatAndType(
894 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400895 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 return false;
897 }
898
Brian Salomon8cbf6622019-07-30 11:03:14 -0400899 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400900 GL_CALL(TexSubImage2D(glTex->target(),
901 0,
902 left, top,
903 width,
904 height,
905 externalFormat, externalType,
906 pixels));
907
908 if (restoreGLRowLength) {
909 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
910 }
911
912 return true;
913}
914
Brian Salomon26de56e2019-04-10 12:14:26 -0400915bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400916 GrColorType surfaceColorType, GrColorType dstColorType,
917 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400918 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
919 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400920 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400921 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
922 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400923}
924
Brian Osman9b560242017-09-05 15:34:52 -0400925void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500926 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
927 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
928 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
929 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400930 }
Brian Osman9b560242017-09-05 15:34:52 -0400931}
932
Brian Salomon22558932020-05-15 14:46:34 -0400933bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
934 GrColorType textureColorType,
935 SkISize texDims,
936 GrGLenum target,
937 SkIRect dstRect,
938 GrColorType srcColorType,
939 const GrMipLevel texels[],
940 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500941 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400942 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500943
Greg Daniel7bfc9132019-08-14 14:23:53 -0400944 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800945
Brian Salomonf77c1462019-08-01 15:19:29 -0400946 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800947
bsalomon5b30c6f2015-12-17 14:17:34 -0800948 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800949 GrGLenum externalFormat;
950 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400951 this->glCaps().getTexSubImageExternalFormatAndType(
952 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400953 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800954 return false;
955 }
Brian Salomon22558932020-05-15 14:46:34 -0400956 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
957 mipLevelCount);
958 return true;
959}
Brian Salomonf77c1462019-08-01 15:19:29 -0400960
Brian Salomon22558932020-05-15 14:46:34 -0400961bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
962 SkISize texDims,
963 GrGLenum target,
964 SkColor4f color,
965 uint32_t levelMask) {
966 GrColorType colorType;
967 GrGLenum externalFormat, externalType;
968 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
969 &externalType, &colorType);
970 if (colorType == GrColorType::kUnknown) {
971 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000972 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400973
Brian Salomon22558932020-05-15 14:46:34 -0400974 std::unique_ptr<char[]> pixelStorage;
975 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400976 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400977 SkSTArray<16, GrMipLevel> levels;
978 levels.resize(numLevels);
979 SkISize levelDims = texDims;
980 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
981 std::max(levelDims.height() >> 1, 1)}) {
982 if (levelMask & (1 << i)) {
983 if (!pixelStorage) {
984 // Make one tight image at the first size and reuse it for smaller levels.
985 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
986 size_t rb = ii.minRowBytes();
987 pixelStorage.reset(new char[rb * levelDims.height()]);
988 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
989 return false;
990 }
991 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000992 }
Brian Salomon22558932020-05-15 14:46:34 -0400993 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
994 }
995 }
996 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
997 bpp, levels.begin(), levels.count());
998 return true;
999}
1000
1001void GrGLGpu::uploadTexData(SkISize texDims,
1002 GrGLenum target,
1003 SkIRect dstRect,
1004 GrGLenum externalFormat,
1005 GrGLenum externalType,
1006 size_t bpp,
1007 const GrMipLevel texels[],
1008 int mipLevelCount) {
1009 SkASSERT(!texDims.isEmpty());
1010 SkASSERT(!dstRect.isEmpty());
1011 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001012 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001013 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1014
1015 const GrGLCaps& caps = this->glCaps();
1016
1017 bool restoreGLRowLength = false;
1018
1019 this->unbindCpuToGpuXferBuffer();
1020 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1021
1022 SkISize dims = dstRect.size();
1023 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1024 std::max(dims.height() >> 1, 1)}) {
1025 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001026 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001027 }
Brian Salomon22558932020-05-15 14:46:34 -04001028 const size_t trimRowBytes = dims.width() * bpp;
1029 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001030
1031 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1032 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001033 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001034 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001035 } else {
1036 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001037 }
1038
Brian Salomon22558932020-05-15 14:46:34 -04001039 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1040 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001041 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001042 if (restoreGLRowLength) {
1043 SkASSERT(caps.writePixelsRowBytesSupport());
1044 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1045 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001046}
1047
Greg Daniel01f278c2020-06-12 16:58:17 -04001048bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1049 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001050 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001051 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001052 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001053 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001054 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001055 const GrGLCaps& caps = this->glCaps();
1056
1057 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001058 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001059 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001060 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001061 }
1062
Robert Phillips9f744f72019-12-19 19:14:33 -05001063 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1064
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001066
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001067 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001068 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001069 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001070 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001071
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001072 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001074
1075 if (useTexStorage) {
1076 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001077 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1078 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001079 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001080 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001081 }
Robert Phillips42716d42019-12-16 12:19:54 -05001082
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001083 size_t offset = 0;
1084 for (int level = 0; level < numMipLevels; ++level) {
1085
Robert Phillips99dead92020-01-27 16:11:57 -05001086 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1087 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001088
Brian Salomond8575452020-02-25 12:13:29 -05001089 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1090 level,
1091 0, // left
1092 0, // top
1093 dimensions.width(),
1094 dimensions.height(),
1095 internalFormat,
1096 SkToInt(levelDataSize),
1097 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001098
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001099 if (error != GR_GL_NO_ERROR) {
1100 return false;
1101 }
1102
Robert Phillips9f744f72019-12-19 19:14:33 -05001103 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001104 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001105 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001106 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001107 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001108
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001109 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001110 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1111 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001112
1113 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001114 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1115 level,
1116 internalFormat,
1117 dimensions.width(),
1118 dimensions.height(),
1119 0, // border
1120 SkToInt(levelDataSize),
1121 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001122
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001123 if (error != GR_GL_NO_ERROR) {
1124 return false;
1125 }
1126
Robert Phillips9f744f72019-12-19 19:14:33 -05001127 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001128 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001129 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001130 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001131 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001132}
1133
Brian Salomond8575452020-02-25 12:13:29 -05001134bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001135 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001136 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001137 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001138 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001139 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001140 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1141 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001142 break;
1143 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001144 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1145 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001146 break;
1147 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1148 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001149 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1150 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001151 break;
1152 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001153 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001154 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001155 }
Brian Salomond8575452020-02-25 12:13:29 -05001156 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001157}
1158
Brian Salomonea4ad302019-08-07 13:04:55 -04001159bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001160 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001161 GrGLRenderTarget::IDs* rtIDs) {
1162 rtIDs->fMSColorRenderbufferID = 0;
1163 rtIDs->fRTFBOID = 0;
1164 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1165 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001166
bsalomona11e5fc2015-12-18 07:59:41 -08001167 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001168
Brian Salomonea4ad302019-08-07 13:04:55 -04001169 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001170 goto FAILED;
1171 }
1172
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001173 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001174 goto FAILED;
1175 }
1176
Brian Salomonea4ad302019-08-07 13:04:55 -04001177 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1178 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001179 goto FAILED;
1180 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001181
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001182 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1183 // the texture bound to the other. The exception is the IMG multisample extension. With this
1184 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1185 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001186 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001187 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1188 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1189 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001190 goto FAILED;
1191 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001192 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001193 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001194 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001195 }
1196
egdanield803f272015-03-18 13:01:52 -07001197 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001198 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001200 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001201 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001202 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1203 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001204 goto FAILED;
1205 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001207 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001208 GR_GL_COLOR_ATTACHMENT0,
1209 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001210 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001212 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001213
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001214 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001215 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1216 GR_GL_COLOR_ATTACHMENT0,
1217 desc.fTarget,
1218 desc.fID,
1219 0,
1220 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001221 } else {
egdanield803f272015-03-18 13:01:52 -07001222 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001223 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001224 desc.fTarget,
1225 desc.fID,
1226 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001227 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001228
1229 return true;
1230
1231FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001232 if (rtIDs->fMSColorRenderbufferID) {
1233 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001235 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1236 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001237 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001238 if (rtIDs->fTexFBOID) {
1239 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
1241 return false;
1242}
1243
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001244// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001245static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001246// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001247 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001248}
1249
Brian Salomone2826ab2019-06-04 15:58:31 -04001250static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001251 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001252 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1253 // drivers have a bug where an FBO won't be complete if it includes a
1254 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001255 GrGLTextureParameters::SamplerOverriddenState state;
1256 state.fMinFilter = GR_GL_NEAREST;
1257 state.fMagFilter = GR_GL_NEAREST;
1258 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1259 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001260 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1261 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1262 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1263 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001264 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001265}
1266
Brian Salomona56a7462020-02-07 14:17:25 -05001267sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001268 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001269 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001270 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001271 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001272 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001273 int mipLevelCount,
1274 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001275 // We don't support protected textures in GL.
1276 if (isProtected == GrProtected::kYes) {
1277 return nullptr;
1278 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001279 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001280
Brian Salomond2a8ae22019-09-10 16:03:59 -04001281 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001282 GrMipmapStatus mipmapStatus =
1283 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001284 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001285 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001286 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001287 switch (format.textureType()) {
1288 case GrTextureType::kExternal:
1289 case GrTextureType::kNone:
1290 return nullptr;
1291 case GrTextureType::k2D:
1292 texDesc.fTarget = GR_GL_TEXTURE_2D;
1293 break;
1294 case GrTextureType::kRectangle:
1295 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1296 return nullptr;
1297 }
1298 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1299 break;
1300 }
Brian Salomon81536f22019-08-08 16:30:49 -04001301 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001302 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001303 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1304 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001305
Brian Salomon0f396992020-06-19 19:51:21 -04001306 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1307 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001308
1309 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001310 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001311 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001312
Robert Phillips67d52cf2017-06-05 13:38:13 -04001313 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001314 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001315 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001316 GL_CALL(BindTexture(texDesc.fTarget, 0));
1317 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001318
Brian Salomonea4ad302019-08-07 13:04:55 -04001319 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1320 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 return return_null_texture();
1322 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001323 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001324 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001325 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001326 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001327 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001329 // The non-sampler params are still at their default values.
1330 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1331 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001332 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001333 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001334 GrGLenum externalFormat, externalType;
1335 GrColorType colorType;
1336 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1337 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001338 for (int i = 0; i < mipLevelCount; ++i) {
1339 if (levelClearMask & (1U << i)) {
1340 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1341 nullptr));
1342 }
1343 }
1344 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1345 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001346 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001347 this->disableWindowRectangles();
1348 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001349 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001350 for (int i = 0; i < mipLevelCount; ++i) {
1351 if (levelClearMask & (1U << i)) {
1352 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1353 kDst_TempFBOTarget);
1354 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1355 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1356 }
1357 }
Brian Salomonc2249852019-09-16 11:08:50 -04001358 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001359 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001360 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001361 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001362 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001363 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001364 }
1365 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001366 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001367}
1368
Robert Phillips9f744f72019-12-19 19:14:33 -05001369sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001370 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001371 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001372 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001373 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001374 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001375 // We don't support protected textures in GL.
1376 if (isProtected == GrProtected::kYes) {
1377 return nullptr;
1378 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001379 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1380
Brian Salomonbb8dde82019-06-27 10:52:13 -04001381 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001382 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001383 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001384 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001385 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001386 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001387 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001388 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001389 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001390 return nullptr;
1391 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001392
Greg Danielaaf738c2020-07-10 09:30:33 -04001393 if (data) {
1394 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1395 GR_GL_TEXTURE_2D, data, dataSize)) {
1396 GL_CALL(DeleteTextures(1, &desc.fID));
1397 return nullptr;
1398 }
1399 }
1400
Robert Phillipsee946932019-12-18 11:16:17 -05001401 // Unbind this texture from the scratch texture unit.
1402 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1403
Brian Salomona6db5102020-07-21 09:56:23 -04001404 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1405 ? GrMipmapStatus::kValid
1406 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001407
Brian Salomona6db5102020-07-21 09:56:23 -04001408 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001409 // The non-sampler params are still at their default values.
1410 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1411 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001412 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001413}
1414
Greg Danielc1ad77c2020-05-06 11:40:03 -04001415GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001416 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001417 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001418 // We don't support protected textures in GL.
1419 if (isProtected == GrProtected::kYes) {
1420 return {};
1421 }
1422
1423 this->handleDirtyContext();
1424
1425 GrGLFormat glFormat = format.asGLFormat();
1426 if (glFormat == GrGLFormat::kUnknown) {
1427 return {};
1428 }
1429
Greg Daniel01f278c2020-06-12 16:58:17 -04001430 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1431
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001432 GrGLTextureInfo info;
1433 GrGLTextureParameters::SamplerOverriddenState initialState;
1434
1435 info.fTarget = GR_GL_TEXTURE_2D;
1436 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001437 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001438 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001439 if (!info.fID) {
1440 return {};
1441 }
1442
1443 // Unbind this texture from the scratch texture unit.
1444 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1445
1446 auto parameters = sk_make_sp<GrGLTextureParameters>();
1447 // The non-sampler params are still at their default values.
1448 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1449 fResetTimestampForTextureParameters);
1450
1451 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1452 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001453}
1454
Greg Danielaaf738c2020-07-10 09:30:33 -04001455bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1456 sk_sp<GrRefCntedCallback> finishedCallback,
1457 const BackendTextureData* data) {
1458 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1459
1460 GrGLTextureInfo info;
1461 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1462
1463 GrBackendFormat format = backendTexture.getBackendFormat();
1464 GrGLFormat glFormat = format.asGLFormat();
1465 if (glFormat == GrGLFormat::kUnknown) {
1466 return false;
1467 }
1468 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1469
Brian Salomon40a40622020-07-21 10:32:07 -04001470 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001471
1472 const char* rawData = nullptr;
1473 size_t rawDataSize = 0;
1474 SkAutoMalloc am;
1475 if (data->type() == BackendTextureData::Type::kCompressed) {
1476 rawData = (const char*)data->compressedData();
1477 rawDataSize = data->compressedSize();
1478 } else {
1479 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1480 SkASSERT(compression != SkImage::CompressionType::kNone);
1481
1482 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001483 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001484
1485 am.reset(rawDataSize);
1486
1487 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1488 data->color());
1489
1490 rawData = (const char*)am.get();
1491 }
1492
1493 this->bindTextureToScratchUnit(info.fTarget, info.fID);
1494 bool result = this->uploadCompressedTexData(
1495 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1496 rawData, rawDataSize);
1497
1498 // Unbind this texture from the scratch texture unit.
1499 this->bindTextureToScratchUnit(info.fTarget, 0);
1500
1501 return result;
1502}
1503
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001504namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001505
egdaniel8dc7c3a2015-04-16 11:22:42 -07001506const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001507
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001508void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001509 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001510
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001512 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001513 (kUnknownBitCount == format->fTotalBits));
1514 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001515 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001516 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1517 (GrGLint*)&format->fStencilBits);
1518 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001519 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001520 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1521 (GrGLint*)&format->fTotalBits);
1522 format->fTotalBits += format->fStencilBits;
1523 } else {
1524 format->fTotalBits = format->fStencilBits;
1525 }
1526 }
1527}
1528}
1529
Brian Salomon5043f1f2019-07-11 21:27:54 -04001530int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001531 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001532 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1533
1534 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001535 // Default to unsupported, set this if we find a stencil format that works.
1536 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001537
Brian Salomon0f396992020-06-19 19:51:21 -04001538 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1539 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001540 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001541 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001542 }
egdanielff1d5472015-09-10 08:37:20 -07001543 // unbind the texture from the texture unit before binding it to the frame buffer
1544 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1545
1546 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001547 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001548 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001549 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001550 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001551 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1552 GR_GL_COLOR_ATTACHMENT0,
1553 GR_GL_TEXTURE_2D,
1554 colorID,
1555 0));
bsalomon30447372015-12-21 09:03:05 -08001556 GrGLuint sbRBID = 0;
1557 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001558
1559 // look over formats till I find a compatible one
1560 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001561 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001562 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001563 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1564 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001565 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1566 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1567 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001568 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001569 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001570 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001571 if (sFmt.fPacked) {
1572 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1573 GR_GL_DEPTH_ATTACHMENT,
1574 GR_GL_RENDERBUFFER, sbRBID));
1575 } else {
1576 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1577 GR_GL_DEPTH_ATTACHMENT,
1578 GR_GL_RENDERBUFFER, 0));
1579 }
1580 GrGLenum status;
1581 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1582 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1583 firstWorkingStencilFormatIndex = i;
1584 break;
1585 }
egdanielff1d5472015-09-10 08:37:20 -07001586 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1587 GR_GL_STENCIL_ATTACHMENT,
1588 GR_GL_RENDERBUFFER, 0));
1589 if (sFmt.fPacked) {
1590 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1591 GR_GL_DEPTH_ATTACHMENT,
1592 GR_GL_RENDERBUFFER, 0));
1593 }
egdanielff1d5472015-09-10 08:37:20 -07001594 }
1595 }
bsalomon30447372015-12-21 09:03:05 -08001596 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001597 }
1598 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001599 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1600 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001601 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001602 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001603 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001604}
1605
Brian Salomonea4ad302019-08-07 13:04:55 -04001606GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001607 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001608 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001609 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001610 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001611 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001612 if (format == GrGLFormat::kUnknown) {
1613 return 0;
1614 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001615 GrGLuint id = 0;
1616 GL_CALL(GenTextures(1, &id));
1617 if (!id) {
1618 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001619 }
1620
Brian Salomonea4ad302019-08-07 13:04:55 -04001621 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001622
Brian Salomonea4ad302019-08-07 13:04:55 -04001623 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1624
Brian Salomonea4ad302019-08-07 13:04:55 -04001625 return id;
1626}
1627
Brian Salomon0f396992020-06-19 19:51:21 -04001628GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1629 GrGLFormat format,
1630 GrGLenum target,
1631 GrRenderable renderable,
1632 GrGLTextureParameters::SamplerOverriddenState* initialState,
1633 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001634 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001635 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001636
Brian Salomonea4ad302019-08-07 13:04:55 -04001637 GrGLuint id = 0;
1638 GL_CALL(GenTextures(1, &id));
1639
1640 if (!id) {
1641 return 0;
1642 }
1643
Brian Salomon0f396992020-06-19 19:51:21 -04001644 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001645
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001646 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001647 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001648 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001649 }
1650
Brian Salomond2a8ae22019-09-10 16:03:59 -04001651 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001652 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001653 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001654 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001655 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001656
1657 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1658
1659 bool success = false;
1660 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001661 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001662 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001663 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1664 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001665 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001666 } else {
1667 GrGLenum externalFormat, externalType;
1668 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1669 GrGLenum error = GR_GL_NO_ERROR;
1670 if (externalFormat && externalType) {
1671 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1672 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001673 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1674 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001675 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1676 currentHeight, 0, externalFormat, externalType,
1677 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001678 }
Brian Salomond8575452020-02-25 12:13:29 -05001679 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001680 }
1681 }
1682 }
1683 if (success) {
1684 return id;
1685 }
1686 GL_CALL(DeleteTextures(1, &id));
1687 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001688}
1689
Chris Daltoneffee202019-07-01 22:28:03 -06001690GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1691 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001692 SkASSERT(width >= rt->width());
1693 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001694
egdaniel8dc7c3a2015-04-16 11:22:42 -07001695 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001696
Brian Salomond4764a12019-08-08 12:08:24 -04001697 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001698 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001699 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001700 }
egdanielff1d5472015-09-10 08:37:20 -07001701
1702 if (!sbDesc.fRenderbufferID) {
1703 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1704 }
1705 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001706 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001707 }
1708 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1709 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001710 // we do this "if" so that we don't call the multisample
1711 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001712 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001713 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1714 width, height)) {
1715 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1716 return nullptr;
1717 }
egdanielff1d5472015-09-10 08:37:20 -07001718 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001719 GrGLenum error = GL_ALLOC_CALL(
1720 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1721 if (error != GR_GL_NO_ERROR) {
1722 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1723 return nullptr;
1724 }
egdanielff1d5472015-09-10 08:37:20 -07001725 }
1726 fStats.incStencilAttachmentCreates();
1727 // After sized formats we attempt an unsized format and take
1728 // whatever sizes GL gives us. In that case we query for the size.
1729 GrGLStencilAttachment::Format format = sFmt;
1730 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001731 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1732 sbDesc,
1733 width,
1734 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001735 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001736 format);
1737 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001738}
1739
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001740////////////////////////////////////////////////////////////////////////////////
1741
Brian Salomondbf70722019-02-07 11:31:24 -05001742sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1743 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001744 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001745}
1746
Chris Dalton2e7ed262020-02-21 15:17:59 -07001747void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1748 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001749 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1750 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1751 fHWScissorSettings.fEnabled = kYes_TriState;
1752 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001753 } else {
1754 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1755 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1756 fHWScissorSettings.fEnabled = kNo_TriState;
1757 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001759}
Brian Salomond818ebf2018-07-02 14:08:49 +00001760
Chris Dalton2e7ed262020-02-21 15:17:59 -07001761void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1762 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001763 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001764 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1765 if (fHWScissorSettings.fRect != nativeScissor) {
1766 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1767 nativeScissor.fHeight));
1768 fHWScissorSettings.fRect = nativeScissor;
1769 }
joshualitt77b13072014-10-27 14:51:01 -07001770}
1771
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001772void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001773 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001774#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001775 typedef GrWindowRectsState::Mode Mode;
1776 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1777 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001778
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001779 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001780 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001781 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001782 }
1783
csmartdalton7535f412016-08-23 06:51:00 -07001784 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1785 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001786 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001787 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001788
Chris Daltond6cda8d2019-09-05 02:30:04 -06001789 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001791 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001792 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001793 }
1794
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001795 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001796 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001797
Greg Danielacd66b42019-05-22 16:29:12 -04001798 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001799#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001800}
1801
1802void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001803#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001804 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001805 return;
1806 }
1807 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001808 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001809#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001810}
1811
Robert Phillipsdf546db2020-05-29 09:42:54 -04001812bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001813 this->handleDirtyContext();
1814
Chris Daltond42d2002020-02-28 12:05:04 -07001815 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001816 if (!program) {
1817 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1818 return false;
1819 }
1820
1821 this->flushProgram(std::move(program));
1822
Chris Daltond42d2002020-02-28 12:05:04 -07001823 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1824 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1825 }
1826
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001827 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001828 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001829 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001830
Chris Dalton20e7a532020-02-28 15:37:53 +00001831 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001832
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001833 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001834 GrStencilSettings stencil;
1835 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001836 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1837 stencil.reset(*programInfo.pipeline().getUserStencil(),
1838 programInfo.pipeline().hasStencilClip(),
1839 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001840 }
Robert Phillips901aff02019-10-08 12:32:56 -04001841 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001842 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001843 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1844 glRT, programInfo.origin());
1845 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001846 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001847 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001848
1849 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001850 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001851 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001852
Chris Dalton20e7a532020-02-28 15:37:53 +00001853 return true;
1854}
1855
1856void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1857 if (!program) {
1858 fHWProgram.reset();
1859 fHWProgramID = 0;
1860 return;
1861 }
1862 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1863 if (program == fHWProgram) {
1864 return;
1865 }
1866 auto id = program->programID();
1867 SkASSERT(id);
1868 GL_CALL(UseProgram(id));
1869 fHWProgram = std::move(program);
1870 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001871}
1872
1873void GrGLGpu::flushProgram(GrGLuint id) {
1874 SkASSERT(id);
1875 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001876 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001877 return;
1878 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001879 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001880 GL_CALL(UseProgram(id));
1881 fHWProgramID = id;
1882}
1883
Brian Salomonae64c192019-02-05 09:41:37 -05001884GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001885 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001886
cdaltone2e71c22016-04-07 18:13:29 -07001887 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001888 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001889 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001890 }
cdaltone2e71c22016-04-07 18:13:29 -07001891
Brian Salomonae64c192019-02-05 09:41:37 -05001892 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001893 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001894 if (!bufferState->fBufferZeroKnownBound) {
1895 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1896 bufferState->fBufferZeroKnownBound = true;
1897 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001898 }
Brian Salomondbf70722019-02-07 11:31:24 -05001899 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1900 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001901 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1902 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1903 bufferState->fBufferZeroKnownBound = false;
1904 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001905 }
1906
Brian Salomonae64c192019-02-05 09:41:37 -05001907 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001908}
Brian Salomond818ebf2018-07-02 14:08:49 +00001909
Michael Ludwig1e632792020-05-21 12:45:31 -04001910void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001911 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001912 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001913 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001914 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001915 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001916
Brian Salomon43f8bf02017-10-18 08:33:29 -04001917 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001918
Brian Salomon43f8bf02017-10-18 08:33:29 -04001919 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1920
Michael Ludwig1e632792020-05-21 12:45:31 -04001921 if (scissor.enabled()) {
1922 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001923 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001924 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001925 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001926 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1927 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001928 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001929 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001930 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001931}
1932
Chris Daltonb50cc812019-10-14 16:29:21 -06001933static bool use_tiled_rendering(const GrGLCaps& glCaps,
1934 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1935 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1936 // Otherwise it's faster to just not use it.
1937 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1938 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1939}
1940
1941void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001942 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1943 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1944 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1945
1946 this->handleDirtyContext();
1947
1948 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1949 this->flushRenderTarget(glRT);
1950 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1951
Chris Daltonb50cc812019-10-14 16:29:21 -06001952 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1953 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1954 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1955 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1956 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1957 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1958 nativeBounds.fHeight, preserveMask));
1959 }
1960
Chris Dalton674f77a2019-09-30 20:49:39 -06001961 GrGLbitfield clearMask = 0;
1962 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1963 SkASSERT(!this->caps()->performColorClearsAsDraws());
1964 this->flushClearColor(colorLoadStore.fClearColor);
1965 this->flushColorWrite(true);
1966 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001967 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001968 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1969 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1970 GL_CALL(StencilMask(0xffffffff));
1971 GL_CALL(ClearStencil(0));
1972 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1973 }
1974 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001975 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001976 this->disableWindowRectangles();
1977 GL_CALL(Clear(clearMask));
1978 }
1979}
1980
1981void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1982 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1983 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1984 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1985
1986 this->handleDirtyContext();
1987
1988 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1989 // The framebuffer binding changed in the middle of a command buffer. We should have already
1990 // printed a warning during onFBOChanged.
1991 return;
1992 }
1993
1994 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
1995 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1996
1997 SkSTArray<2, GrGLenum> discardAttachments;
1998 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
1999 discardAttachments.push_back(
2000 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2001 }
2002 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2003 discardAttachments.push_back(
2004 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2005 }
2006
2007 if (!discardAttachments.empty()) {
2008 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2009 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2010 discardAttachments.begin()));
2011 } else {
2012 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2013 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2014 discardAttachments.begin()));
2015 }
2016 }
2017 }
2018
Chris Daltonb50cc812019-10-14 16:29:21 -06002019 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2020 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2021 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2022 // Handled by use_tiled_rendering().
2023 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2024 GL_CALL(EndTiling(preserveMask));
2025 }
2026
Chris Dalton674f77a2019-09-30 20:49:39 -06002027 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002028}
2029
Michael Ludwig1e632792020-05-21 12:45:31 -04002030void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002031 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002032 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002033 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002034 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002035 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002036
egdaniel8dc7c3a2015-04-16 11:22:42 -07002037 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002038 if (!sb) {
2039 // We should only get here if we marked a proxy as requiring a SB. However,
2040 // the SB creation could later fail. Likely clipping is going to go awry now.
2041 return;
2042 }
2043
bsalomon6bc1b5f2015-02-23 09:06:38 -08002044 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002045#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002046 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002047 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002048#else
2049 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002050 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002051 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002052 // changing the clip between stencil passes may or may not
2053 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002054 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002055#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002056 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002057 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002058 value = (1 << (stencilBitCount - 1));
2059 } else {
2060 value = 0;
2061 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002062 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002063 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002064
Michael Ludwig1e632792020-05-21 12:45:31 -04002065 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2066 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002067
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002068 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002069 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002070 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002071 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002072}
2073
Brian Salomone05ba5a2019-04-08 11:59:07 -04002074bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002075 GrColorType surfaceColorType, GrColorType dstColorType,
2076 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002077 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002078
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002079 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002080 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002081 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002082 return false;
2083 }
Greg Danielba88ab62019-07-26 09:14:01 -04002084 GrGLenum externalFormat = 0;
2085 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002086 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2087 surfaceColorType,
2088 dstColorType,
2089 &externalFormat,
2090 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002091 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002092 return false;
2093 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002094
Brian Salomon71d9d842016-11-03 13:42:00 -04002095 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002096 if (renderTarget->numSamples() <= 1 ||
2097 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2098 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2099 this->flushRenderTargetNoColorWrites(renderTarget);
2100 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2101 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2102 return false;
2103 } else {
2104 SkASSERT(renderTarget->requiresManualMSAAResolve());
2105 // we don't track the state of the READ FBO ID.
2106 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002107 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002108 } else {
2109 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002110 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002111 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002112 }
2113
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002114 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002115 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002116
Brian Salomona6948702018-06-01 15:33:20 -04002117 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002118 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002119 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002120 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002121 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002122 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002123
Brian Osman1348ed02018-09-12 09:08:39 -04002124 bool reattachStencil = false;
2125 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2126 renderTarget &&
2127 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002128 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002129 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2130 reattachStencil = true;
2131 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2132 GR_GL_RENDERBUFFER, 0));
2133 }
2134
Chris Dalton76500e52019-09-05 02:13:05 -06002135 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002136 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002137
2138 if (reattachStencil) {
2139 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2140 renderTarget->renderTargetPriv().getStencilAttachment());
2141 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2142 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2143 }
2144
Brian Salomon26de56e2019-04-10 12:14:26 -04002145 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002146 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002147 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2148 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002149
Brian Salomone05ba5a2019-04-08 11:59:07 -04002150 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002151 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002152 }
2153 return true;
2154}
2155
2156bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002157 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2158 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002159 SkASSERT(surface);
2160
Brian Salomonb28cb682019-07-26 12:48:47 -04002161 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002162
Brian Salomon26de56e2019-04-10 12:14:26 -04002163 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2164 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002165
Brian Salomon1047a492019-07-02 12:25:21 -04002166 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002167 rowPixelWidth = width;
2168 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002169 SkASSERT(!(rowBytes % bytesPerPixel));
2170 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002171 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002172 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2173 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002174}
2175
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002176GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002177 GrRenderTarget* rt, GrStencilAttachment*,
2178 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002179 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002180 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002181 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002182 if (!fCachedOpsRenderPass) {
2183 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002184 }
2185
Chris Daltonb50cc812019-10-14 16:29:21 -06002186 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002187 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002188}
2189
Brian Salomon1fabd512018-02-09 09:54:25 -05002190void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002191 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002192 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002193 this->didWriteToSurface(target, origin, &bounds);
2194}
bsalomon6ba6fa12015-03-04 11:57:37 -08002195
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002196void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2197 this->flushRenderTargetNoColorWrites(target);
2198 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002199}
2200
Brian Osman9aa30c62018-07-02 15:21:46 -04002201void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002202 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002203 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002204 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002205 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002206#ifdef SK_DEBUG
2207 // don't do this check in Chromium -- this is causing
2208 // lots of repeated command buffer flushes when the compositor is
2209 // rendering with Ganesh, which is really slow; even too slow for
2210 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002211 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002212 GrGLenum status;
2213 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2214 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2215 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2216 }
bsalomon160f24c2015-03-17 15:55:42 -07002217 }
egdanield803f272015-03-18 13:01:52 -07002218#endif
2219 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002220 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002221 }
2222
brianosman35b784d2016-05-05 11:52:53 -07002223 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002224 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002225 }
Brian Salomon9b553272020-01-24 14:38:37 -05002226
2227 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2228 GrGLint format;
2229 GrGLint type;
2230 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2231 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2232 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2233 }
bsalomon083617b2016-02-12 12:10:14 -08002234}
bsalomona9909122016-01-23 10:41:40 -08002235
brianosman33f6b3f2016-06-02 05:49:21 -07002236void GrGLGpu::flushFramebufferSRGB(bool enable) {
2237 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2238 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2239 fHWSRGBFramebuffer = kYes_TriState;
2240 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2241 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2242 fHWSRGBFramebuffer = kNo_TriState;
2243 }
2244}
2245
Greg Danielacd66b42019-05-22 16:29:12 -04002246void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002247 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002248 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002249 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002250 fHWViewport = viewport;
2251 }
2252}
2253
Chris Dalton33db9fc2020-02-25 18:52:12 -07002254GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002255 fStats.incNumDraws();
2256
Chris Dalton2e7ed262020-02-21 15:17:59 -07002257 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2258 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2259 GL_CALL(Enable(GR_GL_CULL_FACE));
2260 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002261 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002262 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002263
Chris Dalton3809bab2017-06-13 10:55:06 -06002264 switch (primitiveType) {
2265 case GrPrimitiveType::kTriangles:
2266 return GR_GL_TRIANGLES;
2267 case GrPrimitiveType::kTriangleStrip:
2268 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002269 case GrPrimitiveType::kPoints:
2270 return GR_GL_POINTS;
2271 case GrPrimitiveType::kLines:
2272 return GR_GL_LINES;
2273 case GrPrimitiveType::kLineStrip:
2274 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002275 case GrPrimitiveType::kPatches:
2276 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002277 case GrPrimitiveType::kPath:
2278 SK_ABORT("non-mesh-based GrPrimitiveType");
2279 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002280 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002281 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002282}
2283
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002284void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002285 // Some extensions automatically resolves the texture when it is read.
2286 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2287
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002288 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002289 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2290 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2291 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2292 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002293
Chris Daltonc139d082019-09-26 14:04:24 -06002294 // make sure we go through flushRenderTarget() since we've modified
2295 // the bound DRAW FBO ID.
2296 fHWBoundRenderTargetUniqueID.makeInvalid();
2297 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2298 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002299 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2300 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002301 GrScissorState scissor(rt->dimensions());
2302 SkAssertResult(scissor.set(resolveRect));
2303 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002304 this->disableWindowRectangles();
2305 GL_CALL(ResolveMultisampleFramebuffer());
2306 } else {
2307 int l, b, r, t;
2308 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2309 this->glCaps().blitFramebufferSupportFlags()) {
2310 l = 0;
2311 b = 0;
2312 r = target->width();
2313 t = target->height();
2314 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002315 l = resolveRect.x();
2316 b = resolveRect.y();
2317 r = resolveRect.x() + resolveRect.width();
2318 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002319 }
Chris Daltonc139d082019-09-26 14:04:24 -06002320
2321 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002322 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002323 this->disableWindowRectangles();
2324 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 +00002325 }
2326}
2327
bsalomon@google.com411dad02012-06-05 20:24:20 +00002328namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002329
bsalomon@google.com411dad02012-06-05 20:24:20 +00002330
2331GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002332 static const GrGLenum gTable[kGrStencilOpCount] = {
2333 GR_GL_KEEP, // kKeep
2334 GR_GL_ZERO, // kZero
2335 GR_GL_REPLACE, // kReplace
2336 GR_GL_INVERT, // kInvert
2337 GR_GL_INCR_WRAP, // kIncWrap
2338 GR_GL_DECR_WRAP, // kDecWrap
2339 GR_GL_INCR, // kIncClamp
2340 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002341 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002342 static_assert(0 == (int)GrStencilOp::kKeep);
2343 static_assert(1 == (int)GrStencilOp::kZero);
2344 static_assert(2 == (int)GrStencilOp::kReplace);
2345 static_assert(3 == (int)GrStencilOp::kInvert);
2346 static_assert(4 == (int)GrStencilOp::kIncWrap);
2347 static_assert(5 == (int)GrStencilOp::kDecWrap);
2348 static_assert(6 == (int)GrStencilOp::kIncClamp);
2349 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002350 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2351 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002352}
2353
2354void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002355 const GrStencilSettings::Face& face,
2356 GrGLenum glFace) {
2357 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2358 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2359 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002360
cdalton93a379b2016-05-11 13:58:08 -07002361 GrGLint ref = face.fRef;
2362 GrGLint mask = face.fTestMask;
2363 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002364
2365 if (GR_GL_FRONT_AND_BACK == glFace) {
2366 // we call the combined func just in case separate stencil is not
2367 // supported.
2368 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2369 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002370 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002371 } else {
2372 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2373 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002374 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002375 }
2376}
2377}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002378
Chris Dalton71713f62019-04-18 12:41:03 -06002379void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002380 if (stencilSettings.isDisabled()) {
2381 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002382 } else if (fHWStencilSettings != stencilSettings ||
2383 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002384 if (kYes_TriState != fHWStencilTestEnabled) {
2385 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002386
csmartdaltonc7d85332016-10-26 10:13:46 -07002387 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002388 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002389 if (!stencilSettings.isTwoSided()) {
2390 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2391 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002392 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002393 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2394 GR_GL_FRONT);
2395 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2396 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002397 }
joshualitta58fe352014-10-27 08:39:00 -07002398 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002399 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002400 }
2401}
2402
csmartdaltonc7d85332016-10-26 10:13:46 -07002403void GrGLGpu::disableStencil() {
2404 if (kNo_TriState != fHWStencilTestEnabled) {
2405 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002406
csmartdaltonc7d85332016-10-26 10:13:46 -07002407 fHWStencilTestEnabled = kNo_TriState;
2408 fHWStencilSettings.invalidate();
2409 }
2410}
2411
Chris Dalton4c56b032019-03-04 12:28:42 -07002412void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002413 // rt is only optional if useHWAA is false.
2414 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002415#ifdef SK_DEBUG
2416 if (useHWAA && rt->numSamples() <= 1) {
2417 SkASSERT(this->caps()->mixedSamplesSupport());
2418 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2419 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2420 }
2421#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002422
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002423 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002424 if (useHWAA) {
2425 if (kYes_TriState != fMSAAEnabled) {
2426 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2427 fMSAAEnabled = kYes_TriState;
2428 }
2429 } else {
2430 if (kNo_TriState != fMSAAEnabled) {
2431 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2432 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002433 }
2434 }
2435 }
2436}
2437
Chris Daltonce425af2019-12-16 10:39:03 -07002438void GrGLGpu::flushConservativeRasterState(bool enabled) {
2439 if (this->caps()->conservativeRasterSupport()) {
2440 if (enabled) {
2441 if (kYes_TriState != fHWConservativeRasterEnabled) {
2442 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2443 fHWConservativeRasterEnabled = kYes_TriState;
2444 }
2445 } else {
2446 if (kNo_TriState != fHWConservativeRasterEnabled) {
2447 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2448 fHWConservativeRasterEnabled = kNo_TriState;
2449 }
2450 }
2451 }
2452}
2453
Chris Dalton1215cda2019-12-17 21:44:04 -07002454void GrGLGpu::flushWireframeState(bool enabled) {
2455 if (this->caps()->wireframeSupport()) {
2456 if (this->caps()->wireframeMode() || enabled) {
2457 if (kYes_TriState != fHWWireframeEnabled) {
2458 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2459 fHWWireframeEnabled = kYes_TriState;
2460 }
2461 } else {
2462 if (kNo_TriState != fHWWireframeEnabled) {
2463 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2464 fHWWireframeEnabled = kNo_TriState;
2465 }
2466 }
2467 }
2468}
2469
Chris Daltonbbb3f642019-07-24 12:25:08 -04002470void GrGLGpu::flushBlendAndColorWrite(
2471 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2472 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2473 // We need to work around a driver bug by using a blend state that preserves the dst color,
2474 // rather than disabling color writes.
2475 GrXferProcessor::BlendInfo preserveDstBlend;
2476 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2477 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2478 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2479 return;
2480 }
bsalomonf7cc8772015-05-11 11:21:14 -07002481
cdalton8917d622015-05-06 13:40:21 -07002482 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002483 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2484 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002485
2486 // Any optimization to disable blending should have already been applied and
2487 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002488 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2489 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002490
egdanielb414f252014-07-29 13:15:47 -07002491 if (blendOff) {
2492 if (kNo_TriState != fHWBlendState.fEnabled) {
2493 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002494
Jim Van Verth1bef9792020-07-09 08:09:13 -04002495 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002496 // https://code.google.com/p/skia/issues/detail?id=3943
2497 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2498 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2499 SkASSERT(this->caps()->advancedBlendEquationSupport());
2500 // Set to any basic blending equation.
2501 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2502 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2503 fHWBlendState.fEquation = blend_equation;
2504 }
2505
egdanielb414f252014-07-29 13:15:47 -07002506 fHWBlendState.fEnabled = kNo_TriState;
2507 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002508 } else {
2509 if (kYes_TriState != fHWBlendState.fEnabled) {
2510 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002511
Chris Daltonbbb3f642019-07-24 12:25:08 -04002512 fHWBlendState.fEnabled = kYes_TriState;
2513 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002514
Chris Daltonbbb3f642019-07-24 12:25:08 -04002515 if (fHWBlendState.fEquation != equation) {
2516 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2517 fHWBlendState.fEquation = equation;
2518 }
cdalton8917d622015-05-06 13:40:21 -07002519
Chris Daltonbbb3f642019-07-24 12:25:08 -04002520 if (GrBlendEquationIsAdvanced(equation)) {
2521 SkASSERT(this->caps()->advancedBlendEquationSupport());
2522 // Advanced equations have no other blend state.
2523 return;
2524 }
cdalton8917d622015-05-06 13:40:21 -07002525
Chris Daltonbbb3f642019-07-24 12:25:08 -04002526 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2527 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2528 gXfermodeCoeff2Blend[dstCoeff]));
2529 fHWBlendState.fSrcCoeff = srcCoeff;
2530 fHWBlendState.fDstCoeff = dstCoeff;
2531 }
cdalton8917d622015-05-06 13:40:21 -07002532
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002533 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002534 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2535 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2536 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2537 fHWBlendState.fConstColor = blendConst;
2538 fHWBlendState.fConstColorValid = true;
2539 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002540 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002541 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002542
2543 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002544}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002545
Brian Salomondc829942018-10-23 16:07:24 -04002546static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002547 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002548 switch (swizzle[i]) {
2549 case 'r': glValues[i] = GR_GL_RED; break;
2550 case 'g': glValues[i] = GR_GL_GREEN; break;
2551 case 'b': glValues[i] = GR_GL_BLUE; break;
2552 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002553 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002554 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002555 default: SK_ABORT("Unsupported component");
2556 }
Brian Salomon327955b2018-10-22 15:39:22 +00002557 }
2558}
2559
Greg Daniel2c3398d2019-06-19 11:58:01 -04002560void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2561 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002562 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002563
reed856e9d92015-09-30 12:21:45 -07002564#ifdef SK_DEBUG
2565 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002566 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002567 const int w = texture->width();
2568 const int h = texture->height();
2569 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2570 }
2571 }
2572#endif
2573
Robert Phillips294870f2016-11-11 12:38:40 -05002574 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002575 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002576 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002577 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002578 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002579 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002580 }
2581
Brian Salomone69b9ef2020-07-22 11:18:06 -04002582 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon69100f02020-07-21 10:49:25 -04002583 if (!this->caps()->mipmapSupport() ||
Brian Salomon8c82a872020-07-21 12:09:58 -04002584 texture->texturePriv().mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002585 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2586 } else {
2587 SkASSERT(!texture->texturePriv().mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002588 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002589 }
bsalomonefd7d452014-10-23 14:17:46 -07002590
Brian Salomone2826ab2019-06-04 15:58:31 -04002591 auto timestamp = texture->parameters()->resetTimestamp();
2592 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002593 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2594 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002595 if (fSamplerObjectCache) {
2596 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Brian Salomon69100f02020-07-21 10:49:25 -04002597 if (this->glCaps().mustSetTexParameterMinFilterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002598 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2599 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2600 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002601 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2602 texture->parameters()->samplerOverriddenState();
Brian Salomone69b9ef2020-07-22 11:18:06 -04002603 if (setAll || oldSamplerState.fMinFilter != minFilter) {
Brian Salomon1908bb82020-05-13 12:22:54 -04002604 this->setTextureUnit(unitIdx);
Brian Salomone69b9ef2020-07-22 11:18:06 -04002605 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
Brian Salomon1908bb82020-05-13 12:22:54 -04002606 newSamplerState = oldSamplerState;
Brian Salomone69b9ef2020-07-22 11:18:06 -04002607 newSamplerState.fMinFilter = minFilter;
Brian Salomon1908bb82020-05-13 12:22:54 -04002608 samplerStateToRecord = &newSamplerState;
2609 }
2610 }
2611 }
Brian Salomondc829942018-10-23 16:07:24 -04002612 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002613 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2614 texture->parameters()->samplerOverriddenState();
2615 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002616
Brian Salomone69b9ef2020-07-22 11:18:06 -04002617 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2618 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002619 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002620
Brian Salomone2826ab2019-06-04 15:58:31 -04002621 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2622 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002623
2624 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002625 newSamplerState.fMinLOD = -1000.f;
2626 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002627
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002629 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002630 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002631 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002632 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002633 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002634 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002635 }
Brian Salomon69100f02020-07-21 10:49:25 -04002636 if (this->glCaps().mipmapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002637 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002638 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002639 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002640 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002642 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002644 }
2645 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002646 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002647 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002649 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002651 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002653 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002654 if (this->glCaps().clampToBorderSupport()) {
2655 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002656 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002657 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002658 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2659 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002660 }
2661 }
Brian Salomondc829942018-10-23 16:07:24 -04002662 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002663 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2664 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon8c82a872020-07-21 12:09:58 -04002665 newNonsamplerState.fMaxMipmapLevel = texture->texturePriv().maxMipmapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002666
Brian Salomone2826ab2019-06-04 15:58:31 -04002667 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2668 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002669 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002670 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2671 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002672 GrGLenum glValues[4];
2673 get_gl_swizzle_values(swizzle, glValues);
2674 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002675 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002676 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002677 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2678 reinterpret_cast<const GrGLint*>(glValues)));
2679 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002680 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2681 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2682 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2683 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2684 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002685 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002686 }
2687 }
Brian Salomondc829942018-10-23 16:07:24 -04002688 // These are not supported in ES2 contexts
Brian Salomon69100f02020-07-21 10:49:25 -04002689 if (this->glCaps().mipmapLevelAndLodControlSupport() &&
Brian Salomonf3841932018-11-29 09:13:37 -05002690 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2691 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002692 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002693 this->setTextureUnit(unitIdx);
2694 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002695 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002696 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002697 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002698 this->setTextureUnit(unitIdx);
2699 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002700 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002701 }
Brian Salomon327955b2018-10-22 15:39:22 +00002702 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002703 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2704 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002705}
2706
Brian Salomon1f05d452019-02-08 12:33:08 -05002707void GrGLGpu::onResetTextureBindings() {
2708 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2709 GR_GL_TEXTURE_EXTERNAL};
2710 for (int i = 0; i < this->numTextureUnits(); ++i) {
2711 this->setTextureUnit(i);
2712 for (auto target : kTargets) {
2713 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2714 GL_CALL(BindTexture(target, 0));
2715 }
2716 }
2717 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2718 }
2719}
2720
Chris Dalton5a2f9622019-12-27 14:56:38 -07002721void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2722 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2723 if (fHWPatchVertexCount != count) {
2724 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2725 fHWPatchVertexCount = count;
2726 }
2727}
2728
egdaniel080e6732014-12-22 07:35:52 -08002729void GrGLGpu::flushColorWrite(bool writeColor) {
2730 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002731 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002732 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2733 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002734 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002735 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002736 } else {
2737 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002738 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002739 fHWWriteToColor = kYes_TriState;
2740 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002741 }
bsalomon3e791242014-12-17 13:43:13 -08002742}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002743
Chris Dalton674f77a2019-09-30 20:49:39 -06002744void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2745 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2746 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2747 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2748 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2749 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2750 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2751 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002752 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2753 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2754 GL_CALL(ClearColor(r, g, b, a));
2755 fHWClearColor[0] = r;
2756 fHWClearColor[1] = g;
2757 fHWClearColor[2] = b;
2758 fHWClearColor[3] = a;
2759 }
2760}
2761
bsalomon861e1032014-12-16 07:33:49 -08002762void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002763 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002764 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002765 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002766 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002767 }
2768}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002769
Brian Salomond978b902019-02-07 15:09:18 -05002770void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002771 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002772 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002773 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2774 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2775 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002776 }
Brian Salomond978b902019-02-07 15:09:18 -05002777 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2778 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002779 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002780 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002781}
2782
Brian Salomone5e7eb12016-10-14 16:18:33 -04002783// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002784static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2785 const GrSurface* src,
2786 const SkIRect& srcRect,
2787 const SkIPoint& dstPoint,
2788 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002789 int dstSampleCnt = 0;
2790 int srcSampleCnt = 0;
2791 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002792 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002793 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002794 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002795 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002796 }
2797 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2798 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2799
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002800 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2801 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2802
Brian Salomone5e7eb12016-10-14 16:18:33 -04002803 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002804 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002805
Greg Daniel46cfbc62019-06-07 11:43:30 -04002806 GrTextureType dstTexType;
2807 GrTextureType* dstTexTypePtr = nullptr;
2808 GrTextureType srcTexType;
2809 GrTextureType* srcTexTypePtr = nullptr;
2810 if (dstTex) {
2811 dstTexType = dstTex->texturePriv().textureType();
2812 dstTexTypePtr = &dstTexType;
2813 }
2814 if (srcTex) {
2815 srcTexType = srcTex->texturePriv().textureType();
2816 srcTexTypePtr = &srcTexType;
2817 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002818
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002819 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2820 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002821 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002822}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002823
Brian Osmana9c8a052018-01-19 10:31:56 -05002824static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2825 // A RT has a separate MSAA renderbuffer if:
2826 // 1) It's multisampled
2827 // 2) We're using an extension with separate MSAA renderbuffers
2828 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002829 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002830}
2831
Greg Daniel46cfbc62019-06-07 11:43:30 -04002832static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002833 const GrGLCaps& caps) {
2834
bsalomon@google.comeb851172013-04-15 13:51:00 +00002835 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002836 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002837 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002838 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002839
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002840 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2841 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2842
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002843 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2844 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2845
Greg Daniel46cfbc62019-06-07 11:43:30 -04002846 GrTextureType dstTexType;
2847 GrTextureType* dstTexTypePtr = nullptr;
2848 GrTextureType srcTexType;
2849 GrTextureType* srcTexTypePtr = nullptr;
2850 if (dstTex) {
2851 dstTexType = dstTex->texturePriv().textureType();
2852 dstTexTypePtr = &dstTexType;
2853 }
2854 if (srcTex) {
2855 srcTexType = srcTex->texturePriv().textureType();
2856 srcTexTypePtr = &srcTexType;
2857 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002858
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002859 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2860 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002861}
2862
Greg Danielacd66b42019-05-22 16:29:12 -04002863// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002864void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002865 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002866 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002867 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002868 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002869 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2870 GrGLuint texID = texture->textureID();
2871 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002872 GrGLuint* tempFBOID;
2873 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002874
egdanield803f272015-03-18 13:01:52 -07002875 if (0 == *tempFBOID) {
2876 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002877 }
2878
Adrienne Walker4ee88512018-05-17 11:37:14 -07002879 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002880 GR_GL_CALL(
2881 this->glInterface(),
2882 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2883 if (mipLevel == 0) {
2884 texture->baseLevelWasBoundToFBO();
2885 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002886 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002887 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002888 }
egdaniel0f5f9672015-02-03 11:10:51 -08002889}
2890
Brian Salomond2a8ae22019-09-10 16:03:59 -04002891void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002892 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002893 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002894 SkASSERT(surface->asTexture());
2895 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2896 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2897 GR_GL_COLOR_ATTACHMENT0,
2898 textureTarget,
2899 0,
2900 0));
2901 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002902}
2903
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002904void GrGLGpu::onFBOChanged() {
2905 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2906 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002907 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002908 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002909#ifdef SK_DEBUG
2910 if (fIsExecutingCommandBuffer_DebugOnly) {
2911 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2912 "This will severely hurt performance.\n");
2913 }
2914#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002915}
2916
Adrienne Walker4ee88512018-05-17 11:37:14 -07002917void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2918 fStats.incRenderTargetBinds();
2919 GL_CALL(BindFramebuffer(target, fboid));
2920 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2921 fBoundDrawFramebuffer = fboid;
2922 }
2923
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002924 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2925 // The driver forgets the correct scissor when modifying the FBO binding.
2926 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002927 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002928 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002929 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002930 }
2931
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002932 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002933}
2934
Adrienne Walker4ee88512018-05-17 11:37:14 -07002935void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002936 // We're relying on the GL state shadowing being correct in the workaround code below so we
2937 // need to handle a dirty context.
2938 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002939 if (fboid == fBoundDrawFramebuffer &&
2940 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2941 // This workaround only applies to deleting currently bound framebuffers
2942 // on Adreno 420. Because this is a somewhat rare case, instead of
2943 // tracking all the attachments of every framebuffer instead just always
2944 // unbind all attachments.
2945 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2946 GR_GL_RENDERBUFFER, 0));
2947 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2948 GR_GL_RENDERBUFFER, 0));
2949 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2950 GR_GL_RENDERBUFFER, 0));
2951 }
2952
2953 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002954
2955 // Deleting the currently bound framebuffer rebinds to 0.
2956 if (fboid == fBoundDrawFramebuffer) {
2957 this->onFBOChanged();
2958 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002959}
2960
Greg Daniel46cfbc62019-06-07 11:43:30 -04002961bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002962 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002963 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2964 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2965 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002966 auto dstFormat = dst->backendFormat().asGLFormat();
2967 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002968 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002969 return true;
2970 }
2971 }
cblume61214052016-01-26 09:10:48 -08002972
Greg Daniel46cfbc62019-06-07 11:43:30 -04002973 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2974 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002975 return true;
2976 }
2977
Greg Daniel46cfbc62019-06-07 11:43:30 -04002978 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2979 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002980 }
2981
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002982 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002983 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002984 return true;
2985 }
bsalomon083617b2016-02-12 12:10:14 -08002986 }
2987
bsalomon6df86402015-06-01 10:41:49 -07002988 return false;
2989}
2990
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04002992 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002993
2994 int progIdx = TextureToCopyProgramIdx(srcTex);
2995 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
2996 GrSLType samplerType =
2997 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
2998
2999 if (!fCopyProgramArrayBuffer) {
3000 static const GrGLfloat vdata[] = {
3001 0, 0,
3002 0, 1,
3003 1, 0,
3004 1, 1
3005 };
3006 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3007 kStatic_GrAccessPattern, vdata);
3008 }
3009 if (!fCopyProgramArrayBuffer) {
3010 return false;
3011 }
3012
3013 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3014 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3015 if (!fCopyPrograms[progIdx].fProgram) {
3016 return false;
3017 }
3018
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003019 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003020 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003021 GrShaderVar::TypeModifier::Uniform);
3022 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3023 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3024 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3025 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003026
Greg Daniel9e3169b2019-06-06 14:38:17 -04003027 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003028 if (shaderCaps->noperspectiveInterpolationSupport()) {
3029 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3030 vshaderTxt.appendf("#extension %s : require\n", extension);
3031 }
3032 vTexCoord.addModifier("noperspective");
3033 }
3034
3035 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3036 vshaderTxt.append(";");
3037 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3038 vshaderTxt.append(";");
3039 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3040 vshaderTxt.append(";");
3041 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3042 vshaderTxt.append(";");
3043
3044 vshaderTxt.append(
3045 "// Copy Program VS\n"
3046 "void main() {"
3047 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3048 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3049 " sk_Position.zw = half2(0, 1);"
3050 "}"
3051 );
3052
Greg Daniel9e3169b2019-06-06 14:38:17 -04003053 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003054 if (shaderCaps->noperspectiveInterpolationSupport()) {
3055 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3056 fshaderTxt.appendf("#extension %s : require\n", extension);
3057 }
3058 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003059 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003060 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3061 fshaderTxt.append(";");
3062 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3063 fshaderTxt.append(";");
3064 fshaderTxt.appendf(
3065 "// Copy Program FS\n"
3066 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003067 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003068 "}"
3069 );
3070
3071 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3072 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3073 SkSL::Program::Settings settings;
3074 settings.fCaps = shaderCaps;
3075 SkSL::String glsl;
3076 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3077 sksl, settings, &glsl, errorHandler);
3078 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3079 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3080 SkASSERT(program->fInputs.isEmpty());
3081
3082 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3083 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3084 errorHandler);
3085 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3086 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3087 errorHandler);
3088 SkASSERT(program->fInputs.isEmpty());
3089
3090 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3091
3092 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3093 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3094 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3095 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3096 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3097 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3098
3099 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3100
3101 GL_CALL(DeleteShader(vshader));
3102 GL_CALL(DeleteShader(fshader));
3103
3104 return true;
3105}
3106
brianosman33f6b3f2016-06-02 05:49:21 -07003107bool GrGLGpu::createMipmapProgram(int progIdx) {
3108 const bool oddWidth = SkToBool(progIdx & 0x2);
3109 const bool oddHeight = SkToBool(progIdx & 0x1);
3110 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3111
Brian Salomon1edc5b92016-11-29 13:43:46 -05003112 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003113
3114 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3115 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3116 if (!fMipmapPrograms[progIdx].fProgram) {
3117 return false;
3118 }
3119
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003120 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003121 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003122 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003123 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003124 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003125 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003126 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003127 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3128 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3129 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3130 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003131 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003132 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003133
Ethan Nicholasfc994162019-06-06 10:04:27 -04003134 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003135 if (shaderCaps->noperspectiveInterpolationSupport()) {
3136 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003137 vshaderTxt.appendf("#extension %s : require\n", extension);
3138 }
3139 vTexCoords[0].addModifier("noperspective");
3140 vTexCoords[1].addModifier("noperspective");
3141 vTexCoords[2].addModifier("noperspective");
3142 vTexCoords[3].addModifier("noperspective");
3143 }
3144
Brian Salomon1edc5b92016-11-29 13:43:46 -05003145 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003146 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003147 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003148 vshaderTxt.append(";");
3149 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003150 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003151 vshaderTxt.append(";");
3152 }
3153
3154 vshaderTxt.append(
3155 "// Mipmap Program VS\n"
3156 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003157 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3158 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003159 );
3160
3161 // Insert texture coordinate computation:
3162 if (oddWidth && oddHeight) {
3163 vshaderTxt.append(
3164 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003165 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3166 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003167 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3168 );
3169 } else if (oddWidth) {
3170 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003171 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3172 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003173 );
3174 } else if (oddHeight) {
3175 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003176 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3177 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003178 );
3179 } else {
3180 vshaderTxt.append(
3181 " v_texCoord0 = a_vertex.xy;"
3182 );
3183 }
3184
3185 vshaderTxt.append("}");
3186
Ethan Nicholasfc994162019-06-06 10:04:27 -04003187 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003188 if (shaderCaps->noperspectiveInterpolationSupport()) {
3189 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003190 fshaderTxt.appendf("#extension %s : require\n", extension);
3191 }
3192 }
brianosman33f6b3f2016-06-02 05:49:21 -07003193 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003194 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003195 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003196 fshaderTxt.append(";");
3197 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003198 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003199 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003200 fshaderTxt.append(
3201 "// Mipmap Program FS\n"
3202 "void main() {"
3203 );
3204
3205 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) + "
3209 " sample(u_texture, v_texCoord2) + "
3210 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003211 );
3212 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003213 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003214 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3215 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003216 );
3217 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003218 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003219 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003220 );
3221 }
3222
3223 fshaderTxt.append("}");
3224
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003225 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003226 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003227 SkSL::Program::Settings settings;
3228 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003229 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003230 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003231 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003232 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003233 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003234 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003235
Brian Osman6c431d52019-04-15 16:31:54 -04003236 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003237 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3238 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003239 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003240 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003241 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003242 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003243
3244 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3245
3246 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3247 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3248 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3249 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3250
3251 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3252
3253 GL_CALL(DeleteShader(vshader));
3254 GL_CALL(DeleteShader(fshader));
3255
3256 return true;
3257}
3258
Greg Daniel46cfbc62019-06-07 11:43:30 -04003259bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003260 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003261 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3262 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3263 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3264 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003265 return false;
3266 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003267 int progIdx = TextureToCopyProgramIdx(srcTex);
3268 if (!dstRT) {
3269 SkASSERT(dstTex);
3270 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3271 return false;
3272 }
3273 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003274 if (!fCopyPrograms[progIdx].fProgram) {
3275 if (!this->createCopyProgram(srcTex)) {
3276 SkDebugf("Failed to create copy program.\n");
3277 return false;
3278 }
3279 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003280 int w = srcRect.width();
3281 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003282 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003283 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003284 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003285 this->flushViewport(dst->width(), dst->height());
3286 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003287 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003288 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003289 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003290 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3291 attribs->enableVertexArrays(this, 1);
3292 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3293 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 // dst rect edges in NDC (-1 to 1)
3295 int dw = dst->width();
3296 int dh = dst->height();
3297 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3298 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3299 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3300 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3302 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3303 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3304 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3305 int sw = src->width();
3306 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003307 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3308 // src rect edges in normalized texture space (0 to 1)
3309 sx0 /= sw;
3310 sx1 /= sw;
3311 sy0 /= sh;
3312 sy1 /= sh;
3313 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003314 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3315 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3316 sx1 - sx0, sy1 - sy0, sx0, sy0));
3317 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003318 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003319 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003320 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003321 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003322 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003323 this->disableWindowRectangles();
3324 this->disableStencil();
3325 if (this->glCaps().srgbWriteControl()) {
3326 this->flushFramebufferSRGB(true);
3327 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003328 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003329 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003330 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3331 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003332 return true;
3333}
3334
Greg Daniel46cfbc62019-06-07 11:43:30 -04003335void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003336 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003337 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003338 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003339 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003340 SkASSERT(dstTex);
3341 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003342 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003343
Brian Salomond978b902019-02-07 15:09:18 -05003344 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003345 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003346 dstPoint.fX, dstPoint.fY,
3347 srcRect.fLeft, srcRect.fTop,
3348 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003349 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003350 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3351 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003352 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3353 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003354}
3355
Greg Daniel46cfbc62019-06-07 11:43:30 -04003356bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003357 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003358 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003359 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3360 srcRect.width(), srcRect.height());
3361 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003362 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003363 return false;
mtklein404b3b22015-05-18 09:29:10 -07003364 }
bsalomon5df6fee2015-05-18 06:26:15 -07003365 }
bsalomon6df86402015-06-01 10:41:49 -07003366
Brian Salomond2a8ae22019-09-10 16:03:59 -04003367 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3368 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003369 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003370 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003371
3372 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003373 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003374 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003375
Greg Daniel46cfbc62019-06-07 11:43:30 -04003376 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3377 srcRect.fTop,
3378 srcRect.fRight,
3379 srcRect.fBottom,
3380 dstRect.fLeft,
3381 dstRect.fTop,
3382 dstRect.fRight,
3383 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003384 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003385 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3386 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003387
3388 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3389 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003390 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003391}
3392
Brian Salomon930f9392018-06-20 16:25:26 -04003393bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3394 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003395 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003396 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003397 return false;
3398 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003399 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003400 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3401 // Uses draw calls to do a series of downsample operations to successive mips.
3402
3403 // The manual approach requires the ability to limit which level we're sampling and that the
3404 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003405 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003406 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003407 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003408 GL_CALL(GenerateMipmap(glTex->target()));
3409 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003410 }
3411
brianosman33f6b3f2016-06-02 05:49:21 -07003412 int width = texture->width();
3413 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003414 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon8c82a872020-07-21 12:09:58 -04003415 SkASSERT(levelCount == texture->texturePriv().maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003416
3417 // Create (if necessary), then bind temporary FBO:
3418 if (0 == fTempDstFBOID) {
3419 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3420 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003421 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003422 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003423
3424 // Bind the texture, to get things configured for filtering.
3425 // We'll be changing our base level further below:
3426 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003427 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003428 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003429
3430 // Vertex data:
3431 if (!fMipmapProgramArrayBuffer) {
3432 static const GrGLfloat vdata[] = {
3433 0, 0,
3434 0, 1,
3435 1, 0,
3436 1, 1
3437 };
Brian Salomonae64c192019-02-05 09:41:37 -05003438 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003439 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003440 }
3441 if (!fMipmapProgramArrayBuffer) {
3442 return false;
3443 }
3444
3445 fHWVertexArrayState.setVertexArrayID(this, 0);
3446
3447 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003448 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003449 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003450 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003451
3452 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003453 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003454 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003455 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003456 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003457 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003458
3459 // Do all the blits:
3460 width = texture->width();
3461 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003462
brianosman33f6b3f2016-06-02 05:49:21 -07003463 for (GrGLint level = 1; level < levelCount; ++level) {
3464 // Get and bind the program for this particular downsample (filter shape can vary):
3465 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3466 if (!fMipmapPrograms[progIdx].fProgram) {
3467 if (!this->createMipmapProgram(progIdx)) {
3468 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003469 // Invalidate all params to cover base level change in a previous iteration.
3470 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003471 return false;
3472 }
3473 }
Brian Salomon802cb312018-06-08 18:05:20 -04003474 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003475
3476 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3477 const float invWidth = 1.0f / width;
3478 const float invHeight = 1.0f / height;
3479 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3480 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3481 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3482
3483 // Only sample from previous mip
3484 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3485
Brian Salomon930f9392018-06-20 16:25:26 -04003486 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3487 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003488
Brian Osman788b9162020-02-07 10:36:46 -05003489 width = std::max(1, width / 2);
3490 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003491 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003492
3493 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3494 }
3495
3496 // Unbind:
3497 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3498 GR_GL_TEXTURE_2D, 0, 0));
3499
Brian Salomon930f9392018-06-20 16:25:26 -04003500 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003501 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3502 // We drew the 2nd to last level into the last level.
3503 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3504 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003505
brianosman33f6b3f2016-06-02 05:49:21 -07003506 return true;
3507}
3508
Chris Daltond7291ba2019-03-07 14:17:03 -07003509void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003510 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3511 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003512
3513 int effectiveSampleCnt;
3514 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003515 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003516
3517 sampleLocations->reset(effectiveSampleCnt);
3518 for (int i = 0; i < effectiveSampleCnt; ++i) {
3519 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3520 }
3521}
3522
cdalton231c5fd2015-05-13 12:35:36 -07003523void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003524 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003525 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003526 case kTexture_GrXferBarrierType: {
3527 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003528 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003529 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3530 // The render target uses separate storage so no need for glTextureBarrier.
3531 // FIXME: The render target will resolve automatically when its texture is bound,
3532 // but we could resolve only the bounds that will be read if we do it here instead.
3533 return;
3534 }
cdalton9954bc32015-04-29 14:17:00 -07003535 SkASSERT(this->caps()->textureBarrierSupport());
3536 GL_CALL(TextureBarrier());
3537 return;
cdalton231c5fd2015-05-13 12:35:36 -07003538 }
cdalton8917d622015-05-06 13:40:21 -07003539 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003540 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003541 this->caps()->blendEquationSupport());
3542 GL_CALL(BlendBarrier());
3543 return;
bsalomoncb02b382015-08-12 11:14:50 -07003544 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003545 }
3546}
3547
Chris Daltone1196c52019-12-28 14:31:09 -07003548void GrGLGpu::insertManualFramebufferBarrier() {
3549 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3550 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3551}
3552
Brian Salomon85c3d682019-11-04 15:04:54 -05003553GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003554 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003555 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003556 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003557 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003558 // We don't support protected textures in GL.
3559 if (isProtected == GrProtected::kYes) {
3560 return {};
3561 }
3562
Brian Salomon8a375832018-03-14 10:21:40 -04003563 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003564
Brian Salomond4764a12019-08-08 12:08:24 -04003565 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003566 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003567 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003568 }
3569
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003570 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003571 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003572 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003573 }
3574
Robert Phillipsd34691b2019-09-24 13:38:43 -04003575 // Compressed formats go through onCreateCompressedBackendTexture
3576 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3577
Greg Daniel15500642019-06-27 03:05:55 +00003578 GrGLTextureInfo info;
3579 GrGLTextureParameters::SamplerOverriddenState initialState;
3580
Greg Danield51fa2f2020-01-22 16:53:38 -05003581 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003582 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003583 }
Brian Salomon0f396992020-06-19 19:51:21 -04003584 switch (format.textureType()) {
3585 case GrTextureType::kNone:
3586 case GrTextureType::kExternal:
3587 return {};
3588 case GrTextureType::k2D:
3589 info.fTarget = GR_GL_TEXTURE_2D;
3590 break;
3591 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003592 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003593 return {};
3594 }
3595 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3596 break;
3597 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003598 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003599 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3600 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003601 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003602 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003603 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003604
Brian Salomon85c3d682019-11-04 15:04:54 -05003605 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003606 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003607
Brian Salomone2826ab2019-06-04 15:58:31 -04003608 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003609 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003610 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3611 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003612
Brian Salomon85c3d682019-11-04 15:04:54 -05003613 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3614 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003615}
3616
Greg Daniel16032b32020-05-06 15:31:10 -04003617bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3618 sk_sp<GrRefCntedCallback> finishedCallback,
3619 const BackendTextureData* data) {
3620 GrGLTextureInfo info;
3621 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3622
3623 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003624 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003625 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003626 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003627 }
3628
3629 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3630
Brian Salomon22558932020-05-15 14:46:34 -04003631 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003632
Brian Salomonc3f49432020-06-03 10:21:50 -04003633 // 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 -04003634 // so that the uploads go to the right levels.
Brian Salomon69100f02020-07-21 10:49:25 -04003635 if (numMipLevels && this->glCaps().mipmapLevelAndLodControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003636 auto params = backendTexture.getGLTextureParams();
3637 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3638 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003639 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003640 nonsamplerState.fBaseMipMapLevel = 0;
3641 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003642 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003643 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003644 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3645 }
3646 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3647 }
3648
Greg Daniel16032b32020-05-06 15:31:10 -04003649 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003650 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003651 if (data->type() == BackendTextureData::Type::kPixmaps) {
3652 SkTDArray<GrMipLevel> texels;
3653 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3654 texels.append(numMipLevels);
3655 for (int i = 0; i < numMipLevels; ++i) {
3656 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3657 }
Brian Salomon22558932020-05-15 14:46:34 -04003658 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3659 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3660 info.fTarget, dstRect, colorType, texels.begin(),
3661 texels.count());
3662 } else if (data->type() == BackendTextureData::Type::kColor) {
3663 uint32_t levelMask = (1 << numMipLevels) - 1;
3664 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3665 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003666 }
3667
3668 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003669 this->bindTextureToScratchUnit(info.fTarget, 0);
3670 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003671}
3672
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003673void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003674 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3675
3676 GrGLTextureInfo info;
3677 if (tex.getGLTextureInfo(&info)) {
3678 GL_CALL(DeleteTextures(1, &info.fID));
3679 }
3680}
3681
Robert Phillips3bce1f72020-05-12 12:41:58 -04003682bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3683 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3684
3685 Stats::ProgramCacheResult stat;
3686
3687 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3688 if (!tmp) {
3689 return false;
3690 }
3691
3692 return stat != Stats::ProgramCacheResult::kHit;
3693}
3694
Robert Phillipsf0ced622019-05-16 09:06:25 -04003695#if GR_TEST_UTILS
3696
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003697bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003698 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003699
Greg Daniel52e16d92018-04-10 09:34:07 -04003700 GrGLTextureInfo info;
3701 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003702 return false;
3703 }
3704
3705 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003706 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003707
3708 return (GR_GL_TRUE == result);
3709}
3710
Brian Salomonf865b052018-03-09 09:01:53 -05003711GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003712 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003713 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3714 return GrBackendRenderTarget(); // invalid
3715 }
Brian Salomon8a375832018-03-14 10:21:40 -04003716 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003717 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003718 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003719 return {};
3720 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003721 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003722 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003723 if (sFormatIdx < 0) {
3724 return {};
3725 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003726 GrGLuint colorID = 0;
3727 GrGLuint stencilID = 0;
3728 auto deleteIDs = [&] {
3729 if (colorID) {
3730 if (useTexture) {
3731 GL_CALL(DeleteTextures(1, &colorID));
3732 } else {
3733 GL_CALL(DeleteRenderbuffers(1, &colorID));
3734 }
Brian Salomonf865b052018-03-09 09:01:53 -05003735 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003736 if (stencilID) {
3737 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003738 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003739 };
3740
3741 if (useTexture) {
3742 GL_CALL(GenTextures(1, &colorID));
3743 } else {
3744 GL_CALL(GenRenderbuffers(1, &colorID));
3745 }
3746 GL_CALL(GenRenderbuffers(1, &stencilID));
3747 if (!stencilID || !colorID) {
3748 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003749 return {};
3750 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003751
Brian Salomonf865b052018-03-09 09:01:53 -05003752 GrGLFramebufferInfo info;
3753 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003754 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003755 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3756 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003757 deleteIDs();
3758 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003759 }
3760
3761 this->invalidateBoundRenderTarget();
3762
Adrienne Walker4ee88512018-05-17 11:37:14 -07003763 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003764 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003765 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon0f396992020-06-19 19:51:21 -04003766 colorID = this->createTexture({w, h}, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
3767 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003768 if (!colorID) {
3769 deleteIDs();
3770 return {};
3771 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003772 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3773 colorID, 0));
3774 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003775 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003776 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003777 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003778 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3779 GR_GL_RENDERBUFFER, colorID));
3780 }
3781 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003782 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003783 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003784 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003785 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003786 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3787 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003788 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003789 }
3790
Brian Salomon93348dd2018-08-29 12:56:23 -04003791 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3792 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3793 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3794 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003795 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003796 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003797
Adrienne Walker4ee88512018-05-17 11:37:14 -07003798 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003799 GrGLenum status;
3800 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3801 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003802 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003803 return {};
3804 }
3805 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003806
Greg Daniel108bb232018-07-03 16:18:29 -04003807 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003808 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003809 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003810}
3811
3812void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003813 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003814 GrGLFramebufferInfo info;
3815 if (backendRT.getGLFramebufferInfo(&info)) {
3816 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003817 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003818 }
3819 }
joshualitt8fd844f2015-12-02 13:36:47 -08003820}
3821
Greg Daniel26b50a42018-03-08 09:49:58 -05003822void GrGLGpu::testingOnly_flushGpuAndSync() {
3823 GL_CALL(Finish());
3824}
Brian Salomonf865b052018-03-09 09:01:53 -05003825#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003826
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003827///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003828
cdaltone2e71c22016-04-07 18:13:29 -07003829GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003830 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003831 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003832 GrGLAttribArrayState* attribState;
3833
cdaltone2e71c22016-04-07 18:13:29 -07003834 if (gpu->glCaps().isCoreProfile()) {
3835 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003836 GrGLuint arrayID;
3837 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3838 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003839 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003840 }
cdaltone2e71c22016-04-07 18:13:29 -07003841 if (ibuf) {
3842 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003843 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003844 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003845 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003846 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003847 if (ibuf) {
3848 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003849 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003850 } else {
3851 this->setVertexArrayID(gpu, 0);
3852 }
3853 int attrCount = gpu->glCaps().maxVertexAttributes();
3854 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3855 fDefaultVertexArrayAttribState.resize(attrCount);
3856 }
3857 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003858 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003859 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003860}
bsalomone179a912016-01-20 06:18:10 -08003861
Greg Danielfe159622020-04-10 17:43:51 +00003862void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3863 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003864 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003865}
3866
Greg Daniel78be37f2020-04-14 16:40:35 -04003867void GrGLGpu::flush(FlushType flushType) {
3868 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3869 GL_CALL(Flush());
3870 fNeedsGLFlush = false;
3871 }
3872}
3873
Greg Danielfe159622020-04-10 17:43:51 +00003874bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3875 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003876 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003877 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003878 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003879 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003880 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003881 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003882 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003883 if (!this->glCaps().skipErrorChecks()) {
3884 this->clearErrorsAndCheckForOOM();
3885 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003886 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003887}
3888
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003889void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3890 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3891 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3892 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003893}
3894
Greg Daniel6be35232017-03-01 17:01:09 -05003895GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003896 if (!this->caps()->fenceSyncSupport()) {
3897 return 0;
3898 }
Greg Daniel6be35232017-03-01 17:01:09 -05003899 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003900 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3901 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3902 GrGLuint fence = 0;
3903 GL_CALL(GenFences(1, &fence));
3904 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3905 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3906 } else {
3907 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3908 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003909 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003910 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003911 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003912}
3913
Brian Salomonb0d8b762019-05-06 16:58:22 -04003914bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003915 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3916 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3917 if (!timeout) {
3918 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003919 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003920 }
3921 GrGLboolean result;
3922 GL_CALL_RET(result, TestFence(nvFence));
3923 return result == GR_GL_TRUE;
3924 }
3925 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3926 // If this really becomes necessary we could poll TestFence().
3927 // FinishFence always flushes so no need to check flush param.
3928 GL_CALL(FinishFence(nvFence));
3929 return true;
3930 } else {
3931 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3932 GrGLenum result;
3933 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3934 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3935 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003936}
3937
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003938bool GrGLGpu::waitFence(GrFence fence) {
3939 if (!this->caps()->fenceSyncSupport()) {
3940 return true;
3941 }
3942 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003943}
3944
3945void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003946 if (this->caps()->fenceSyncSupport()) {
3947 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3948 }
Greg Daniel6be35232017-03-01 17:01:09 -05003949}
3950
Greg Daniel301015c2019-11-18 14:06:46 -05003951std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003952 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003953 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003954}
3955
Greg Daniel301015c2019-11-18 14:06:46 -05003956std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3957 const GrBackendSemaphore& semaphore,
3958 GrResourceProvider::SemaphoreWrapType wrapType,
3959 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003960 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003961 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3962}
3963
Greg Daniel301015c2019-11-18 14:06:46 -05003964void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003965 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003966 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003967
Greg Daniel48661b82018-01-22 16:11:35 -05003968 GrGLsync sync;
3969 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3970 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003971 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003972}
3973
Greg Daniel301015c2019-11-18 14:06:46 -05003974void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003975 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003976 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003977
3978 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3979}
3980
Brian Salomonb0d8b762019-05-06 16:58:22 -04003981void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003982 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003983}
3984
Brian Salomon24069eb2020-06-24 10:19:52 -04003985void GrGLGpu::clearErrorsAndCheckForOOM() {
3986 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
3987}
3988
3989GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
3990#if GR_GL_CHECK_ERROR
3991 if (this->glInterface()->checkAndResetOOMed()) {
3992 this->setOOMed();
3993 }
3994#endif
3995 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
3996 if (error == GR_GL_OUT_OF_MEMORY) {
3997 this->setOOMed();
3998 }
3999 return error;
4000}
4001
Greg Daniel6be35232017-03-01 17:01:09 -05004002void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004003 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4004 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4005 GL_CALL(DeleteFences(1, &nvFence));
4006 } else {
4007 GL_CALL(DeleteSync(sync));
4008 }
jvanverth84741b32016-09-30 08:39:02 -07004009}
Brian Osman13dddce2017-05-09 13:19:50 -04004010
Greg Daniel301015c2019-11-18 14:06:46 -05004011std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004012 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004013 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004014 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004015 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004016 // 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 -04004017 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004018
4019 return semaphore;
4020}
Robert Phillips646e4292017-06-13 12:44:56 -04004021
4022int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004023 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004024 case kTexture2DSampler_GrSLType:
4025 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004026 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004027 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004028 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004029 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004030 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004031 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004032 }
4033}
Brian Osman71a18892017-08-10 10:23:25 -04004034
Kevin Lubickf4def342018-10-04 12:52:50 -04004035#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004036#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004037void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4038 // We are called by the base class, which has already called beginObject(). We choose to nest
4039 // all of our caps information in a named sub-object.
4040 writer->beginObject("GL GPU");
4041
4042 const GrGLubyte* str;
4043 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4044 writer->appendString("GL_VERSION", (const char*)(str));
4045 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4046 writer->appendString("GL_RENDERER", (const char*)(str));
4047 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4048 writer->appendString("GL_VENDOR", (const char*)(str));
4049 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4050 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4051
4052 writer->appendName("extensions");
4053 glInterface()->fExtensions.dumpJSON(writer);
4054
4055 writer->endObject();
4056}
Kevin Lubickf4def342018-10-04 12:52:50 -04004057#endif