blob: 8d0ec2b17c0ef292abb160bc2e25cd5587ce24a0 [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;
180 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
181 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR;
182 }
183 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400184}
185
186static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) {
187 switch (filter) {
188 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
189 case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR;
190 case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR;
191 }
192 SK_ABORT("Unknown filter");
Brian Salomondc829942018-10-23 16:07:24 -0400193}
194
Michael Ludwigf23a1522018-12-10 11:36:13 -0500195static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
196 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400197 switch (wrapMode) {
198 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
199 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
200 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500201 case GrSamplerState::WrapMode::kClampToBorder:
202 // May not be supported but should have been caught earlier
203 SkASSERT(caps.clampToBorderSupport());
204 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500205 }
Brian Salomondc829942018-10-23 16:07:24 -0400206 SK_ABORT("Unknown wrap mode");
Brian Salomondc829942018-10-23 16:07:24 -0400207}
208
209///////////////////////////////////////////////////////////////////////////////
210
211class GrGLGpu::SamplerObjectCache {
212public:
213 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
214 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
215 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
216 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
217 std::fill_n(fSamplers, kNumSamplers, 0);
218 }
219
220 ~SamplerObjectCache() {
221 if (!fNumTextureUnits) {
222 // We've already been abandoned.
223 return;
224 }
Chris Dalton703fe682019-05-15 12:58:12 -0600225 for (GrGLuint sampler : fSamplers) {
226 // The spec states that "zero" values should be silently ignored, however they still
227 // trigger GL errors on some NVIDIA platforms.
228 if (sampler) {
229 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
230 }
231 }
Brian Salomondc829942018-10-23 16:07:24 -0400232 }
233
Brian Salomonccb61422020-01-09 10:46:36 -0500234 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400235 int index = StateToIndex(state);
236 if (!fSamplers[index]) {
237 GrGLuint s;
238 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
239 if (!s) {
240 return;
241 }
242 fSamplers[index] = s;
243 auto minFilter = filter_to_gl_min_filter(state.filter());
244 auto magFilter = filter_to_gl_mag_filter(state.filter());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500245 auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
246 auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400247 GR_GL_CALL(fGpu->glInterface(),
248 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
249 GR_GL_CALL(fGpu->glInterface(),
250 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
251 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
252 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
253 }
254 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
255 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
256 fHWBoundSamplers[unitIdx] = fSamplers[index];
257 }
258 }
259
260 void invalidateBindings() {
261 // When we have sampler support we always use samplers. So setting these to zero will cause
262 // a rebind on next usage.
263 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
264 }
265
266 void abandon() {
267 fHWBoundSamplers.reset();
268 fNumTextureUnits = 0;
269 }
270
271 void release() {
272 if (!fNumTextureUnits) {
273 // We've already been abandoned.
274 return;
275 }
276 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
277 std::fill_n(fSamplers, kNumSamplers, 0);
278 // Deleting a bound sampler implicitly binds sampler 0.
279 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
280 }
281
282private:
Brian Salomonccb61422020-01-09 10:46:36 -0500283 static int StateToIndex(GrSamplerState state) {
Brian Salomondc829942018-10-23 16:07:24 -0400284 int filter = static_cast<int>(state.filter());
285 SkASSERT(filter >= 0 && filter < 3);
286 int wrapX = static_cast<int>(state.wrapModeX());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500287 SkASSERT(wrapX >= 0 && wrapX < 4);
Brian Salomondc829942018-10-23 16:07:24 -0400288 int wrapY = static_cast<int>(state.wrapModeY());
Michael Ludwigf23a1522018-12-10 11:36:13 -0500289 SkASSERT(wrapY >= 0 && wrapY < 4);
290 int idx = 16 * filter + 4 * wrapX + wrapY;
Brian Salomondc829942018-10-23 16:07:24 -0400291 SkASSERT(idx < kNumSamplers);
292 return idx;
293 }
294
295 GrGLGpu* fGpu;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500296 static constexpr int kNumSamplers = 48;
Brian Salomondc829942018-10-23 16:07:24 -0400297 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
298 GrGLuint fSamplers[kNumSamplers];
299 int fNumTextureUnits;
300};
301
reed@google.comac10a2d2010-12-22 21:39:39 +0000302///////////////////////////////////////////////////////////////////////////////
303
Brian Salomon384fab42017-12-07 12:33:05 -0500304sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
Adlai Holler3d0359a2020-07-09 15:35:55 -0400305 GrDirectContext* direct) {
Brian Salomon384fab42017-12-07 12:33:05 -0500306 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500307 interface = GrGLMakeNativeInterface();
308 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
309 // to GrGLMakeNativeInterface.
310 if (!interface) {
311 interface = sk_ref_sp(GrGLCreateNativeInterface());
312 }
Brian Salomon384fab42017-12-07 12:33:05 -0500313 if (!interface) {
314 return nullptr;
315 }
bsalomon424cc262015-05-22 10:37:30 -0700316 }
Jim Van Verth76334772017-05-05 16:46:05 -0400317#ifdef USE_NSIGHT
318 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
319#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500320 auto glContext = GrGLContext::Make(std::move(interface), options);
321 if (!glContext) {
322 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700323 }
Adlai Holler3d0359a2020-07-09 15:35:55 -0400324 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
bsalomon424cc262015-05-22 10:37:30 -0700325}
326
Adlai Holler3d0359a2020-07-09 15:35:55 -0400327GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* direct)
328 : GrGpu(direct)
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500329 , fGLContext(std::move(ctx))
330 , fProgramCache(new ProgramCache(this))
331 , fHWProgramID(0)
332 , fTempSrcFBOID(0)
333 , fTempDstFBOID(0)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400334 , fStencilClearFBOID(0)
335 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500336 SkASSERT(fGLContext);
Brian Salomon24069eb2020-06-24 10:19:52 -0400337 // Clear errors so we don't get confused whether we caused an error.
338 this->clearErrorsAndCheckForOOM();
339 // Toss out any pre-existing OOM that was hanging around before we got started.
340 this->checkAndResetOOMed();
341
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500342 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000343
Brian Salomond978b902019-02-07 15:09:18 -0500344 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000345
Brian Salomonae64c192019-02-05 09:41:37 -0500346 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
347 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600348 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500349 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500350 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
351 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
352 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
353 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700354 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500355 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
356 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700357 }
Brian Salomonae64c192019-02-05 09:41:37 -0500358 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400359 fHWBufferState[i].invalidate();
360 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600361 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700362
363 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
364 fPathRendering.reset(new GrGLPathRendering(this));
365 }
366
Brian Salomondc829942018-10-23 16:07:24 -0400367 if (this->glCaps().samplerObjectSupport()) {
368 fSamplerObjectCache.reset(new SamplerObjectCache(this));
369 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000370}
371
bsalomon861e1032014-12-16 07:33:49 -0800372GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700373 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
374 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800375 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700376 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700377 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800378
Chris Dalton20e7a532020-02-28 15:37:53 +0000379 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400380 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000381 // detach the current program so there is no confusion on OpenGL's part
382 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000383 GL_CALL(UseProgram(0));
384 }
385
Brian Salomon43f8bf02017-10-18 08:33:29 -0400386 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700387 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800388 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400389 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700390 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800391 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400392 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700393 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800394 }
egdaniel0f5f9672015-02-03 11:10:51 -0800395
bsalomon7ea33f52015-11-22 14:51:00 -0800396 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
397 if (0 != fCopyPrograms[i].fProgram) {
398 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
399 }
bsalomon6df86402015-06-01 10:41:49 -0700400 }
bsalomon6dea83f2015-12-03 12:58:06 -0800401
brianosman33f6b3f2016-06-02 05:49:21 -0700402 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
403 if (0 != fMipmapPrograms[i].fProgram) {
404 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
405 }
406 }
407
Brian Salomondc829942018-10-23 16:07:24 -0400408 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500409
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400410 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700411}
412
bsalomon6e2aad42016-04-01 11:54:31 -0700413void GrGLGpu::disconnect(DisconnectType type) {
414 INHERITED::disconnect(type);
415 if (DisconnectType::kCleanup == type) {
416 if (fHWProgramID) {
417 GL_CALL(UseProgram(0));
418 }
419 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700420 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700421 }
422 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700423 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700424 }
425 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700426 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700427 }
bsalomon6e2aad42016-04-01 11:54:31 -0700428 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
429 if (fCopyPrograms[i].fProgram) {
430 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
431 }
432 }
brianosman33f6b3f2016-06-02 05:49:21 -0700433 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
434 if (fMipmapPrograms[i].fProgram) {
435 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
436 }
437 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400438
Brian Salomondc829942018-10-23 16:07:24 -0400439 if (fSamplerObjectCache) {
440 fSamplerObjectCache->release();
441 }
bsalomon6e2aad42016-04-01 11:54:31 -0700442 } else {
443 if (fProgramCache) {
444 fProgramCache->abandon();
445 }
Brian Salomondc829942018-10-23 16:07:24 -0400446 if (fSamplerObjectCache) {
447 fSamplerObjectCache->abandon();
448 }
bsalomon6e2aad42016-04-01 11:54:31 -0700449 }
450
Chris Dalton20e7a532020-02-28 15:37:53 +0000451 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500452 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700453
bsalomonc8dc1f72014-08-21 13:02:13 -0700454 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700455 fTempSrcFBOID = 0;
456 fTempDstFBOID = 0;
457 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700458 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800459 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
460 fCopyPrograms[i].fProgram = 0;
461 }
brianosman33f6b3f2016-06-02 05:49:21 -0700462 fMipmapProgramArrayBuffer.reset();
463 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
464 fMipmapPrograms[i].fProgram = 0;
465 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400466
jvanverthe9c0fc62015-04-29 11:18:05 -0700467 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700468 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700469 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400470 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471}
472
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000473///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000474
bsalomon861e1032014-12-16 07:33:49 -0800475void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000476 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400477 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000478 GL_CALL(Disable(GR_GL_DEPTH_TEST));
479 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000480
Brian Salomonf0861672017-05-08 11:10:10 -0400481 // We don't use face culling.
482 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600483 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
484 // just set this to the default for self-consistency.
485 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400486
Brian Salomonae64c192019-02-05 09:41:37 -0500487 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
488 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700489
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400490 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500491#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 // Desktop-only state that we never change
493 if (!this->glCaps().isCoreProfile()) {
494 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
495 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
496 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
497 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
498 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
499 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
500 }
501 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
502 // core profile. This seems like a bug since the core spec removes any mention of
503 // GL_ARB_imaging.
504 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
505 GL_CALL(Disable(GR_GL_COLOR_TABLE));
506 }
507 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400508
Chris Dalton1215cda2019-12-17 21:44:04 -0700509 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500510#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000511 // Since ES doesn't support glPointSize at all we always use the VS to
512 // set the point size
513 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
514
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000515 }
joshualitt58162332014-08-01 06:44:53 -0700516
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400517 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500518 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700519 // The arm extension requires specifically enabling MSAA fetching per sample.
520 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500521 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700522 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000523 fHWWriteToColor = kUnknown_TriState;
524 // we only ever use lines in hairline mode
525 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700526 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500527
528 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000529 }
edisonn@google.comba669992013-06-28 16:03:21 +0000530
egdanielb414f252014-07-29 13:15:47 -0700531 if (resetBits & kMSAAEnable_GrGLBackendState) {
532 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700533
Chris Dalton6ce447a2019-06-23 18:07:38 -0600534 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800535 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
536 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700537 GL_CALL(CoverageModulation(GR_GL_RGBA));
538 }
Chris Daltonce425af2019-12-16 10:39:03 -0700539
540 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000541 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000542
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000543 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400544 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000545
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000546 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500547 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500548 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000549 }
Brian Salomondc829942018-10-23 16:07:24 -0400550 if (fSamplerObjectCache) {
551 fSamplerObjectCache->invalidateBindings();
552 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000553 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000554
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000555 if (resetBits & kBlend_GrGLBackendState) {
556 fHWBlendState.invalidate();
557 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000558
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000559 if (resetBits & kView_GrGLBackendState) {
560 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700561 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000562 fHWViewport.invalidate();
563 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000565 if (resetBits & kStencil_GrGLBackendState) {
566 fHWStencilSettings.invalidate();
567 fHWStencilTestEnabled = kUnknown_TriState;
568 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000569
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000570 // Vertex
571 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700572 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500573 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
574 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600575 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700576 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000577 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000579 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500580 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700581 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500582 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000583 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000584
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000585 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700586 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700587 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000588 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000589 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000590
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000591 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000592 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400593 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000594 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
595 }
Brian Salomon1047a492019-07-02 12:25:21 -0400596 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000597 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
598 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 if (this->glCaps().packFlipYSupport()) {
600 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
601 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000602 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000603
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000604 if (resetBits & kProgram_GrGLBackendState) {
605 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000606 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000607 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400608 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000609}
610
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400611static bool check_backend_texture(const GrBackendTexture& backendTex,
612 const GrGLCaps& caps,
613 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400614 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400615 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400616 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400617 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800618 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000619
Brian Salomonea4ad302019-08-07 13:04:55 -0400620 desc->fSize = {backendTex.width(), backendTex.height()};
621 desc->fTarget = info.fTarget;
622 desc->fID = info.fID;
623 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800624
Brian Salomonea4ad302019-08-07 13:04:55 -0400625 if (desc->fFormat == GrGLFormat::kUnknown) {
626 return false;
627 }
628 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400629 if (!caps.shaderCaps()->externalTextureSupport()) {
630 return false;
631 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400632 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
633 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400634 return false;
635 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400636 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400637 return false;
638 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400639 if (backendTex.isProtected()) {
640 // Not supported in GL backend at this time.
641 return false;
642 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400643
Brian Salomond17f6582017-07-19 18:28:58 -0400644 return true;
645}
646
647sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400648 GrWrapOwnership ownership,
649 GrWrapCacheable cacheable,
650 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400651 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400652 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400653 return nullptr;
654 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400655
Brian Salomond17f6582017-07-19 18:28:58 -0400656 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400657 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400658 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400659 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400660 }
bsalomone5286e02016-01-14 09:24:09 -0800661
Greg Daniel177e6952017-10-12 12:27:11 -0400662 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
663 : GrMipMapsStatus::kNotAllocated;
664
Brian Salomonea4ad302019-08-07 13:04:55 -0400665 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400666 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400667 // We don't know what parameters are already set on wrapped textures.
668 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600669 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400670}
671
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500672static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
673 const GrGLCaps& caps, GrGLTexture::Desc* desc,
674 bool skipRectTexSupportCheck = false) {
675 GrGLTextureInfo info;
676 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
677 return false;
678 }
679
680 desc->fSize = {backendTex.width(), backendTex.height()};
681 desc->fTarget = info.fTarget;
682 desc->fID = info.fID;
683 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
684
685 if (desc->fFormat == GrGLFormat::kUnknown) {
686 return false;
687 }
688
689 if (GR_GL_TEXTURE_2D != desc->fTarget) {
690 return false;
691 }
692 if (backendTex.isProtected()) {
693 // Not supported in GL backend at this time.
694 return false;
695 }
696
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500697 return true;
698}
699
Robert Phillipsb915c942019-12-17 14:44:37 -0500700sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
701 GrWrapOwnership ownership,
702 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500703 GrGLTexture::Desc desc;
704 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
705 return nullptr;
706 }
707
708 if (kBorrow_GrWrapOwnership == ownership) {
709 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
710 } else {
711 desc.fOwnership = GrBackendObjectOwnership::kOwned;
712 }
713
714 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid
715 : GrMipMapsStatus::kNotAllocated;
716
717 auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc,
718 backendTex.getGLTextureParams(), cacheable,
719 kRead_GrIOType);
720 // We don't know what parameters are already set on wrapped textures.
721 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600722 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500723}
724
Brian Salomond17f6582017-07-19 18:28:58 -0400725sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400726 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500727 GrWrapOwnership ownership,
728 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400729 const GrGLCaps& caps = this->glCaps();
730
Brian Salomonea4ad302019-08-07 13:04:55 -0400731 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400732 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800733 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800734 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400735 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
736 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800737
Brian Salomond17f6582017-07-19 18:28:58 -0400738 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400739 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800740 return nullptr;
741 }
bsalomon10528f12015-10-14 12:54:52 -0700742
Brian Osman766fcbb2017-03-13 09:33:09 -0400743 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400744 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400745 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400746 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800747 }
bsalomonb15b4c12014-10-29 12:41:57 -0700748
Robert Phillips0902c982019-07-16 07:47:56 -0400749
Greg Daniel6fa62e22019-08-07 15:52:37 -0400750 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
751 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000752
Brian Salomonea4ad302019-08-07 13:04:55 -0400753 GrGLRenderTarget::IDs rtIDs;
754 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400755 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000756 }
Greg Daniel177e6952017-10-12 12:27:11 -0400757
758 GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty
759 : GrMipMapsStatus::kNotAllocated;
760
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500761 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400762 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomone2826ab2019-06-04 15:58:31 -0400763 mipMapsStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400764 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400765 // We don't know what parameters are already set on wrapped textures.
766 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600767 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000768}
769
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400770sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400771 GrGLFramebufferInfo info;
772 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000773 return nullptr;
774 }
775
Brian Salomone8a766b2019-07-19 14:24:36 -0400776 if (backendRT.isProtected()) {
777 // Not supported in GL at this time.
778 return nullptr;
779 }
780
Brian Salomond4764a12019-08-08 12:08:24 -0400781 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400782 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
783 return nullptr;
784 }
785
Brian Salomonea4ad302019-08-07 13:04:55 -0400786 GrGLRenderTarget::IDs rtIDs;
787 rtIDs.fRTFBOID = info.fFBOID;
788 rtIDs.fMSColorRenderbufferID = 0;
789 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
790 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000791
Greg Daniel6fa62e22019-08-07 15:52:37 -0400792 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700793
Brian Salomona56a7462020-02-07 14:17:25 -0500794 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400795 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000796}
797
Greg Daniel7ef28f32017-04-20 16:41:55 +0000798sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400799 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400800 GrGLTexture::Desc desc;
801 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
802 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400803 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800804 return nullptr;
805 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400806
807 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
808 return nullptr;
809 }
810
811 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400812 GrGLRenderTarget::IDs rtIDs;
813 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800814 return nullptr;
815 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500816 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800817}
818
Brian Salomonc320b152018-02-20 14:05:36 -0500819static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700820 if (!glTex) {
821 return false;
822 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000823
bsalomone5286e02016-01-14 09:24:09 -0800824 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
825 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800826 return false;
827 }
828
jvanverth17aa0472016-01-05 10:41:27 -0800829 return true;
830}
831
Brian Salomona9b04b92018-06-01 15:04:28 -0400832bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400833 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400834 const GrMipLevel texels[], int mipLevelCount,
835 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500836 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800837
Brian Salomonc320b152018-02-20 14:05:36 -0500838 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800839 return false;
840 }
841
Brian Salomond978b902019-02-07 15:09:18 -0500842 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000843
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400844 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400845 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
846 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
847 glTex->target(), dstRect, srcColorType, texels,
848 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000849}
850
Brian Salomone05ba5a2019-04-08 11:59:07 -0400851bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400852 GrColorType textureColorType, GrColorType bufferColorType,
853 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400855
Jim Van Verth1676cb92019-01-15 13:24:45 -0500856 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400857 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500858
Brian Salomonc320b152018-02-20 14:05:36 -0500859 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400860 return false;
861 }
862
Hal Canaryc36f7e72018-06-18 15:50:09 -0400863 static_assert(sizeof(int) == sizeof(int32_t), "");
864 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400865 return false;
866 }
867
Brian Salomond978b902019-02-07 15:09:18 -0500868 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400869
870 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500871 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400872 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500873 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400874
Greg Daniel660cc992017-06-26 14:55:05 -0400875 SkDEBUGCODE(
876 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
877 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
878 SkASSERT(bounds.contains(subRect));
879 )
880
Brian Salomonb28cb682019-07-26 12:48:47 -0400881 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400882 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400883 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700884 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400885 return false;
886 }
887
888 bool restoreGLRowLength = false;
889 if (trimRowBytes != rowBytes) {
890 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400891 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400892 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
893 restoreGLRowLength = true;
894 }
895
Greg Danielba88ab62019-07-26 09:14:01 -0400896 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400897 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400898 GrGLenum externalFormat = 0;
899 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400900 this->glCaps().getTexSubImageExternalFormatAndType(
901 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400902 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400903 return false;
904 }
905
Brian Salomon8cbf6622019-07-30 11:03:14 -0400906 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400907 GL_CALL(TexSubImage2D(glTex->target(),
908 0,
909 left, top,
910 width,
911 height,
912 externalFormat, externalType,
913 pixels));
914
915 if (restoreGLRowLength) {
916 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
917 }
918
919 return true;
920}
921
Brian Salomon26de56e2019-04-10 12:14:26 -0400922bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400923 GrColorType surfaceColorType, GrColorType dstColorType,
924 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400925 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
926 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400927 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400928 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
929 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400930}
931
Brian Osman9b560242017-09-05 15:34:52 -0400932void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500933 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
934 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
935 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
936 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400937 }
Brian Osman9b560242017-09-05 15:34:52 -0400938}
939
Brian Salomon22558932020-05-15 14:46:34 -0400940bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
941 GrColorType textureColorType,
942 SkISize texDims,
943 GrGLenum target,
944 SkIRect dstRect,
945 GrColorType srcColorType,
946 const GrMipLevel texels[],
947 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500948 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400949 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500950
Greg Daniel7bfc9132019-08-14 14:23:53 -0400951 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800952
Brian Salomonf77c1462019-08-01 15:19:29 -0400953 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800954
bsalomon5b30c6f2015-12-17 14:17:34 -0800955 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800956 GrGLenum externalFormat;
957 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400958 this->glCaps().getTexSubImageExternalFormatAndType(
959 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400960 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800961 return false;
962 }
Brian Salomon22558932020-05-15 14:46:34 -0400963 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
964 mipLevelCount);
965 return true;
966}
Brian Salomonf77c1462019-08-01 15:19:29 -0400967
Brian Salomon22558932020-05-15 14:46:34 -0400968bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
969 SkISize texDims,
970 GrGLenum target,
971 SkColor4f color,
972 uint32_t levelMask) {
973 GrColorType colorType;
974 GrGLenum externalFormat, externalType;
975 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
976 &externalType, &colorType);
977 if (colorType == GrColorType::kUnknown) {
978 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000979 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400980
Brian Salomon22558932020-05-15 14:46:34 -0400981 std::unique_ptr<char[]> pixelStorage;
982 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400983 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400984 SkSTArray<16, GrMipLevel> levels;
985 levels.resize(numLevels);
986 SkISize levelDims = texDims;
987 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
988 std::max(levelDims.height() >> 1, 1)}) {
989 if (levelMask & (1 << i)) {
990 if (!pixelStorage) {
991 // Make one tight image at the first size and reuse it for smaller levels.
992 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
993 size_t rb = ii.minRowBytes();
994 pixelStorage.reset(new char[rb * levelDims.height()]);
995 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
996 return false;
997 }
998 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000999 }
Brian Salomon22558932020-05-15 14:46:34 -04001000 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
1001 }
1002 }
1003 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
1004 bpp, levels.begin(), levels.count());
1005 return true;
1006}
1007
1008void GrGLGpu::uploadTexData(SkISize texDims,
1009 GrGLenum target,
1010 SkIRect dstRect,
1011 GrGLenum externalFormat,
1012 GrGLenum externalType,
1013 size_t bpp,
1014 const GrMipLevel texels[],
1015 int mipLevelCount) {
1016 SkASSERT(!texDims.isEmpty());
1017 SkASSERT(!dstRect.isEmpty());
1018 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001019 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001020 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1021
1022 const GrGLCaps& caps = this->glCaps();
1023
1024 bool restoreGLRowLength = false;
1025
1026 this->unbindCpuToGpuXferBuffer();
1027 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1028
1029 SkISize dims = dstRect.size();
1030 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1031 std::max(dims.height() >> 1, 1)}) {
1032 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001034 }
Brian Salomon22558932020-05-15 14:46:34 -04001035 const size_t trimRowBytes = dims.width() * bpp;
1036 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001037
1038 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1039 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001040 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001041 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001042 } else {
1043 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001044 }
1045
Brian Salomon22558932020-05-15 14:46:34 -04001046 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1047 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001048 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001049 if (restoreGLRowLength) {
1050 SkASSERT(caps.writePixelsRowBytesSupport());
1051 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1052 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001053}
1054
Greg Daniel01f278c2020-06-12 16:58:17 -04001055bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1056 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001057 SkISize dimensions,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001058 GrMipMapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001059 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001060 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001061 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001062 const GrGLCaps& caps = this->glCaps();
1063
1064 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001065 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001066 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001067 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001068 }
1069
Robert Phillips9f744f72019-12-19 19:14:33 -05001070 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1071
Greg Daniel7bfc9132019-08-14 14:23:53 -04001072 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001074 int numMipLevels = 1;
1075 if (mipMapped == GrMipMapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001076 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001077 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001078
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001079 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001081
1082 if (useTexStorage) {
1083 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001084 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1085 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001086 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001087 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001088 }
Robert Phillips42716d42019-12-16 12:19:54 -05001089
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001090 size_t offset = 0;
1091 for (int level = 0; level < numMipLevels; ++level) {
1092
Robert Phillips99dead92020-01-27 16:11:57 -05001093 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1094 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001095
Brian Salomond8575452020-02-25 12:13:29 -05001096 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1097 level,
1098 0, // left
1099 0, // top
1100 dimensions.width(),
1101 dimensions.height(),
1102 internalFormat,
1103 SkToInt(levelDataSize),
1104 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001105
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001106 if (error != GR_GL_NO_ERROR) {
1107 return false;
1108 }
1109
Robert Phillips9f744f72019-12-19 19:14:33 -05001110 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001111 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001112 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001113 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001114 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001115
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001116 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001117 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1118 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001119
1120 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001121 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1122 level,
1123 internalFormat,
1124 dimensions.width(),
1125 dimensions.height(),
1126 0, // border
1127 SkToInt(levelDataSize),
1128 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001130 if (error != GR_GL_NO_ERROR) {
1131 return false;
1132 }
1133
Robert Phillips9f744f72019-12-19 19:14:33 -05001134 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001135 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001136 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001137 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001138 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001139}
1140
Brian Salomond8575452020-02-25 12:13:29 -05001141bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001142 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001143 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001144 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001145 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001146 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001147 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1148 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001149 break;
1150 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001151 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1152 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001153 break;
1154 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1155 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001156 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1157 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001158 break;
1159 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001160 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001161 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001162 }
Brian Salomond8575452020-02-25 12:13:29 -05001163 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001164}
1165
Brian Salomonea4ad302019-08-07 13:04:55 -04001166bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001167 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001168 GrGLRenderTarget::IDs* rtIDs) {
1169 rtIDs->fMSColorRenderbufferID = 0;
1170 rtIDs->fRTFBOID = 0;
1171 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1172 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001173
bsalomona11e5fc2015-12-18 07:59:41 -08001174 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001175
Brian Salomonea4ad302019-08-07 13:04:55 -04001176 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001177 goto FAILED;
1178 }
1179
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001180 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001181 goto FAILED;
1182 }
1183
Brian Salomonea4ad302019-08-07 13:04:55 -04001184 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1185 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001186 goto FAILED;
1187 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001188
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001189 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1190 // the texture bound to the other. The exception is the IMG multisample extension. With this
1191 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1192 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001193 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001194 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1195 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1196 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001197 goto FAILED;
1198 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001199 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001200 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001201 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001202 }
1203
egdanield803f272015-03-18 13:01:52 -07001204 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001205 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001206 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001207 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001208 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001209 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1210 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001211 goto FAILED;
1212 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001213 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001214 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001215 GR_GL_COLOR_ATTACHMENT0,
1216 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001217 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001218 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001219 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001220
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001221 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001222 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1223 GR_GL_COLOR_ATTACHMENT0,
1224 desc.fTarget,
1225 desc.fID,
1226 0,
1227 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001228 } else {
egdanield803f272015-03-18 13:01:52 -07001229 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001230 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001231 desc.fTarget,
1232 desc.fID,
1233 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001234 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001235
1236 return true;
1237
1238FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001239 if (rtIDs->fMSColorRenderbufferID) {
1240 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001241 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001242 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1243 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001244 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001245 if (rtIDs->fTexFBOID) {
1246 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001247 }
1248 return false;
1249}
1250
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001251// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001252static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001253// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001254 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001255}
1256
Brian Salomone2826ab2019-06-04 15:58:31 -04001257static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001258 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001259 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1260 // drivers have a bug where an FBO won't be complete if it includes a
1261 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001262 GrGLTextureParameters::SamplerOverriddenState state;
1263 state.fMinFilter = GR_GL_NEAREST;
1264 state.fMagFilter = GR_GL_NEAREST;
1265 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1266 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001267 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1268 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1269 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1270 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001271 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001272}
1273
Brian Salomona56a7462020-02-07 14:17:25 -05001274sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001275 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001276 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001277 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001278 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001279 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001280 int mipLevelCount,
1281 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001282 // We don't support protected textures in GL.
1283 if (isProtected == GrProtected::kYes) {
1284 return nullptr;
1285 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001286 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001287
Brian Salomond2a8ae22019-09-10 16:03:59 -04001288 SkASSERT(mipLevelCount > 0);
1289 GrMipMapsStatus mipMapsStatus =
1290 mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001291 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001292 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001293 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001294 switch (format.textureType()) {
1295 case GrTextureType::kExternal:
1296 case GrTextureType::kNone:
1297 return nullptr;
1298 case GrTextureType::k2D:
1299 texDesc.fTarget = GR_GL_TEXTURE_2D;
1300 break;
1301 case GrTextureType::kRectangle:
1302 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1303 return nullptr;
1304 }
1305 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1306 break;
1307 }
Brian Salomon81536f22019-08-08 16:30:49 -04001308 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001309 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001310 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1311 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001312
Brian Salomon0f396992020-06-19 19:51:21 -04001313 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1314 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001315
1316 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001317 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001318 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001319
Robert Phillips67d52cf2017-06-05 13:38:13 -04001320 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001321 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001322 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001323 GL_CALL(BindTexture(texDesc.fTarget, 0));
1324 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001325
Brian Salomonea4ad302019-08-07 13:04:55 -04001326 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1327 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 return return_null_texture();
1329 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001330 tex = sk_make_sp<GrGLTextureRenderTarget>(
1331 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001332 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001333 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001334 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001336 // The non-sampler params are still at their default values.
1337 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1338 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001339 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001340 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001341 GrGLenum externalFormat, externalType;
1342 GrColorType colorType;
1343 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1344 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001345 for (int i = 0; i < mipLevelCount; ++i) {
1346 if (levelClearMask & (1U << i)) {
1347 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1348 nullptr));
1349 }
1350 }
1351 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1352 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001353 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001354 this->disableWindowRectangles();
1355 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001356 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001357 for (int i = 0; i < mipLevelCount; ++i) {
1358 if (levelClearMask & (1U << i)) {
1359 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1360 kDst_TempFBOTarget);
1361 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1362 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1363 }
1364 }
Brian Salomonc2249852019-09-16 11:08:50 -04001365 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001366 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001367 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001368 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001369 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001370 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001371 }
1372 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001373 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001374}
1375
Robert Phillips9f744f72019-12-19 19:14:33 -05001376sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001377 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001378 SkBudgeted budgeted,
1379 GrMipMapped mipMapped,
1380 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001381 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001382 // We don't support protected textures in GL.
1383 if (isProtected == GrProtected::kYes) {
1384 return nullptr;
1385 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001386 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1387
Brian Salomonbb8dde82019-06-27 10:52:13 -04001388 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001389 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001390 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001391 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001392 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001393 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001394 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001395 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001396 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001397 return nullptr;
1398 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001399
Greg Danielaaf738c2020-07-10 09:30:33 -04001400 if (data) {
1401 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1402 GR_GL_TEXTURE_2D, data, dataSize)) {
1403 GL_CALL(DeleteTextures(1, &desc.fID));
1404 return nullptr;
1405 }
1406 }
1407
Robert Phillipsee946932019-12-18 11:16:17 -05001408 // Unbind this texture from the scratch texture unit.
1409 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1410
Robert Phillipse4720c62020-01-14 14:33:24 -05001411 GrMipMapsStatus mipMapsStatus = mipMapped == GrMipMapped::kYes
1412 ? GrMipMapsStatus::kValid
1413 : GrMipMapsStatus::kNotAllocated;
1414
1415 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipMapsStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001416 // The non-sampler params are still at their default values.
1417 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1418 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001419 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001420}
1421
Greg Danielc1ad77c2020-05-06 11:40:03 -04001422GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
1423 SkISize dimensions, const GrBackendFormat& format, GrMipMapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001424 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001425 // We don't support protected textures in GL.
1426 if (isProtected == GrProtected::kYes) {
1427 return {};
1428 }
1429
1430 this->handleDirtyContext();
1431
1432 GrGLFormat glFormat = format.asGLFormat();
1433 if (glFormat == GrGLFormat::kUnknown) {
1434 return {};
1435 }
1436
Greg Daniel01f278c2020-06-12 16:58:17 -04001437 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1438
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001439 GrGLTextureInfo info;
1440 GrGLTextureParameters::SamplerOverriddenState initialState;
1441
1442 info.fTarget = GR_GL_TEXTURE_2D;
1443 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001444 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001445 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001446 if (!info.fID) {
1447 return {};
1448 }
1449
1450 // Unbind this texture from the scratch texture unit.
1451 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1452
1453 auto parameters = sk_make_sp<GrGLTextureParameters>();
1454 // The non-sampler params are still at their default values.
1455 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1456 fResetTimestampForTextureParameters);
1457
1458 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1459 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001460}
1461
Greg Danielaaf738c2020-07-10 09:30:33 -04001462bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1463 sk_sp<GrRefCntedCallback> finishedCallback,
1464 const BackendTextureData* data) {
1465 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1466
1467 GrGLTextureInfo info;
1468 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1469
1470 GrBackendFormat format = backendTexture.getBackendFormat();
1471 GrGLFormat glFormat = format.asGLFormat();
1472 if (glFormat == GrGLFormat::kUnknown) {
1473 return false;
1474 }
1475 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1476
1477 GrMipMapped mipMapped = backendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
1478
1479 const char* rawData = nullptr;
1480 size_t rawDataSize = 0;
1481 SkAutoMalloc am;
1482 if (data->type() == BackendTextureData::Type::kCompressed) {
1483 rawData = (const char*)data->compressedData();
1484 rawDataSize = data->compressedSize();
1485 } else {
1486 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1487 SkASSERT(compression != SkImage::CompressionType::kNone);
1488
1489 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
1490 backendTexture.hasMipMaps());
1491
1492 am.reset(rawDataSize);
1493
1494 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1495 data->color());
1496
1497 rawData = (const char*)am.get();
1498 }
1499
1500 this->bindTextureToScratchUnit(info.fTarget, info.fID);
1501 bool result = this->uploadCompressedTexData(
1502 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1503 rawData, rawDataSize);
1504
1505 // Unbind this texture from the scratch texture unit.
1506 this->bindTextureToScratchUnit(info.fTarget, 0);
1507
1508 return result;
1509}
1510
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001511namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001512
egdaniel8dc7c3a2015-04-16 11:22:42 -07001513const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001514
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001515void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001516 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001517
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001518 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001519 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001520 (kUnknownBitCount == format->fTotalBits));
1521 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001522 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001523 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1524 (GrGLint*)&format->fStencilBits);
1525 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001527 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1528 (GrGLint*)&format->fTotalBits);
1529 format->fTotalBits += format->fStencilBits;
1530 } else {
1531 format->fTotalBits = format->fStencilBits;
1532 }
1533 }
1534}
1535}
1536
Brian Salomon5043f1f2019-07-11 21:27:54 -04001537int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001538 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001539 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1540
1541 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001542 // Default to unsupported, set this if we find a stencil format that works.
1543 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001544
Brian Salomon0f396992020-06-19 19:51:21 -04001545 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1546 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001547 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001548 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001549 }
egdanielff1d5472015-09-10 08:37:20 -07001550 // unbind the texture from the texture unit before binding it to the frame buffer
1551 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1552
1553 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001554 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001555 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001556 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001557 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001558 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1559 GR_GL_COLOR_ATTACHMENT0,
1560 GR_GL_TEXTURE_2D,
1561 colorID,
1562 0));
bsalomon30447372015-12-21 09:03:05 -08001563 GrGLuint sbRBID = 0;
1564 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001565
1566 // look over formats till I find a compatible one
1567 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001568 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001570 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1571 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001572 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1573 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1574 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001575 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001576 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001577 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001578 if (sFmt.fPacked) {
1579 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1580 GR_GL_DEPTH_ATTACHMENT,
1581 GR_GL_RENDERBUFFER, sbRBID));
1582 } else {
1583 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1584 GR_GL_DEPTH_ATTACHMENT,
1585 GR_GL_RENDERBUFFER, 0));
1586 }
1587 GrGLenum status;
1588 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1589 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1590 firstWorkingStencilFormatIndex = i;
1591 break;
1592 }
egdanielff1d5472015-09-10 08:37:20 -07001593 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1594 GR_GL_STENCIL_ATTACHMENT,
1595 GR_GL_RENDERBUFFER, 0));
1596 if (sFmt.fPacked) {
1597 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1598 GR_GL_DEPTH_ATTACHMENT,
1599 GR_GL_RENDERBUFFER, 0));
1600 }
egdanielff1d5472015-09-10 08:37:20 -07001601 }
1602 }
bsalomon30447372015-12-21 09:03:05 -08001603 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001604 }
1605 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001606 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1607 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001608 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001609 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001610 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001611}
1612
Brian Salomonea4ad302019-08-07 13:04:55 -04001613GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001614 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001615 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001616 GrGLFormat format,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001617 GrMipMapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001618 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001619 if (format == GrGLFormat::kUnknown) {
1620 return 0;
1621 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001622 GrGLuint id = 0;
1623 GL_CALL(GenTextures(1, &id));
1624 if (!id) {
1625 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001626 }
1627
Brian Salomonea4ad302019-08-07 13:04:55 -04001628 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001629
Brian Salomonea4ad302019-08-07 13:04:55 -04001630 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1631
Brian Salomonea4ad302019-08-07 13:04:55 -04001632 return id;
1633}
1634
Brian Salomon0f396992020-06-19 19:51:21 -04001635GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1636 GrGLFormat format,
1637 GrGLenum target,
1638 GrRenderable renderable,
1639 GrGLTextureParameters::SamplerOverriddenState* initialState,
1640 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001641 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001642 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001643
Brian Salomonea4ad302019-08-07 13:04:55 -04001644 GrGLuint id = 0;
1645 GL_CALL(GenTextures(1, &id));
1646
1647 if (!id) {
1648 return 0;
1649 }
1650
Brian Salomon0f396992020-06-19 19:51:21 -04001651 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001652
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001653 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001654 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001655 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001656 }
1657
Brian Salomond2a8ae22019-09-10 16:03:59 -04001658 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001659 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001660 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001661 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001662 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001663
1664 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1665
1666 bool success = false;
1667 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001668 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001669 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001670 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1671 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001672 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001673 } else {
1674 GrGLenum externalFormat, externalType;
1675 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1676 GrGLenum error = GR_GL_NO_ERROR;
1677 if (externalFormat && externalType) {
1678 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1679 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001680 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1681 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001682 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1683 currentHeight, 0, externalFormat, externalType,
1684 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001685 }
Brian Salomond8575452020-02-25 12:13:29 -05001686 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001687 }
1688 }
1689 }
1690 if (success) {
1691 return id;
1692 }
1693 GL_CALL(DeleteTextures(1, &id));
1694 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001695}
1696
Chris Daltoneffee202019-07-01 22:28:03 -06001697GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1698 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001699 SkASSERT(width >= rt->width());
1700 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001701
egdaniel8dc7c3a2015-04-16 11:22:42 -07001702 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001703
Brian Salomond4764a12019-08-08 12:08:24 -04001704 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001705 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001706 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001707 }
egdanielff1d5472015-09-10 08:37:20 -07001708
1709 if (!sbDesc.fRenderbufferID) {
1710 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1711 }
1712 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001713 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001714 }
1715 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1716 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001717 // we do this "if" so that we don't call the multisample
1718 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001719 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001720 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1721 width, height)) {
1722 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1723 return nullptr;
1724 }
egdanielff1d5472015-09-10 08:37:20 -07001725 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001726 GrGLenum error = GL_ALLOC_CALL(
1727 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1728 if (error != GR_GL_NO_ERROR) {
1729 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1730 return nullptr;
1731 }
egdanielff1d5472015-09-10 08:37:20 -07001732 }
1733 fStats.incStencilAttachmentCreates();
1734 // After sized formats we attempt an unsized format and take
1735 // whatever sizes GL gives us. In that case we query for the size.
1736 GrGLStencilAttachment::Format format = sFmt;
1737 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001738 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1739 sbDesc,
1740 width,
1741 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001742 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001743 format);
1744 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001745}
1746
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001747////////////////////////////////////////////////////////////////////////////////
1748
Brian Salomondbf70722019-02-07 11:31:24 -05001749sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1750 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001751 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001752}
1753
Chris Dalton2e7ed262020-02-21 15:17:59 -07001754void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1755 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001756 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1757 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1758 fHWScissorSettings.fEnabled = kYes_TriState;
1759 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001760 } else {
1761 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1762 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1763 fHWScissorSettings.fEnabled = kNo_TriState;
1764 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001766}
Brian Salomond818ebf2018-07-02 14:08:49 +00001767
Chris Dalton2e7ed262020-02-21 15:17:59 -07001768void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1769 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001770 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001771 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1772 if (fHWScissorSettings.fRect != nativeScissor) {
1773 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1774 nativeScissor.fHeight));
1775 fHWScissorSettings.fRect = nativeScissor;
1776 }
joshualitt77b13072014-10-27 14:51:01 -07001777}
1778
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001779void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001780 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001781#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001782 typedef GrWindowRectsState::Mode Mode;
1783 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1784 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001785
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001786 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001787 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001788 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001789 }
1790
csmartdalton7535f412016-08-23 06:51:00 -07001791 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1792 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001793 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001794 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001795
Chris Daltond6cda8d2019-09-05 02:30:04 -06001796 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001798 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001799 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001800 }
1801
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001802 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001803 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001804
Greg Danielacd66b42019-05-22 16:29:12 -04001805 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001806#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001807}
1808
1809void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001810#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001811 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001812 return;
1813 }
1814 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001815 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001816#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001817}
1818
Robert Phillipsdf546db2020-05-29 09:42:54 -04001819bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001820 this->handleDirtyContext();
1821
Chris Daltond42d2002020-02-28 12:05:04 -07001822 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001823 if (!program) {
1824 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1825 return false;
1826 }
1827
1828 this->flushProgram(std::move(program));
1829
Chris Daltond42d2002020-02-28 12:05:04 -07001830 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1831 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1832 }
1833
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001834 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001835 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001836 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001837
Chris Dalton20e7a532020-02-28 15:37:53 +00001838 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001839
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001840 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001841 GrStencilSettings stencil;
1842 if (programInfo.pipeline().isStencilEnabled()) {
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001843 SkASSERT(glRT->renderTargetPriv().getStencilAttachment());
1844 stencil.reset(*programInfo.pipeline().getUserStencil(),
1845 programInfo.pipeline().hasStencilClip(),
1846 glRT->renderTargetPriv().numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001847 }
Robert Phillips901aff02019-10-08 12:32:56 -04001848 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001849 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001850 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1851 glRT, programInfo.origin());
1852 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001853 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001854 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001855
1856 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001857 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001858 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001859
Chris Dalton20e7a532020-02-28 15:37:53 +00001860 return true;
1861}
1862
1863void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1864 if (!program) {
1865 fHWProgram.reset();
1866 fHWProgramID = 0;
1867 return;
1868 }
1869 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1870 if (program == fHWProgram) {
1871 return;
1872 }
1873 auto id = program->programID();
1874 SkASSERT(id);
1875 GL_CALL(UseProgram(id));
1876 fHWProgram = std::move(program);
1877 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001878}
1879
1880void GrGLGpu::flushProgram(GrGLuint id) {
1881 SkASSERT(id);
1882 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001883 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001884 return;
1885 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001886 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001887 GL_CALL(UseProgram(id));
1888 fHWProgramID = id;
1889}
1890
Brian Salomonae64c192019-02-05 09:41:37 -05001891GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001892 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001893
cdaltone2e71c22016-04-07 18:13:29 -07001894 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001895 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001896 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001897 }
cdaltone2e71c22016-04-07 18:13:29 -07001898
Brian Salomonae64c192019-02-05 09:41:37 -05001899 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001900 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001901 if (!bufferState->fBufferZeroKnownBound) {
1902 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1903 bufferState->fBufferZeroKnownBound = true;
1904 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001905 }
Brian Salomondbf70722019-02-07 11:31:24 -05001906 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1907 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001908 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1909 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1910 bufferState->fBufferZeroKnownBound = false;
1911 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001912 }
1913
Brian Salomonae64c192019-02-05 09:41:37 -05001914 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001915}
Brian Salomond818ebf2018-07-02 14:08:49 +00001916
Michael Ludwig1e632792020-05-21 12:45:31 -04001917void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001918 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001919 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001920 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001921 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001922 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001923
Brian Salomon43f8bf02017-10-18 08:33:29 -04001924 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001925
Brian Salomon43f8bf02017-10-18 08:33:29 -04001926 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1927
Michael Ludwig1e632792020-05-21 12:45:31 -04001928 if (scissor.enabled()) {
1929 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001930 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001931 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001932 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001933 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1934 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001935 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001936 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001937 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001938}
1939
Chris Daltonb50cc812019-10-14 16:29:21 -06001940static bool use_tiled_rendering(const GrGLCaps& glCaps,
1941 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1942 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1943 // Otherwise it's faster to just not use it.
1944 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1945 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1946}
1947
1948void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001949 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1950 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1951 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1952
1953 this->handleDirtyContext();
1954
1955 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1956 this->flushRenderTarget(glRT);
1957 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1958
Chris Daltonb50cc812019-10-14 16:29:21 -06001959 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1960 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1961 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1962 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1963 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1964 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1965 nativeBounds.fHeight, preserveMask));
1966 }
1967
Chris Dalton674f77a2019-09-30 20:49:39 -06001968 GrGLbitfield clearMask = 0;
1969 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1970 SkASSERT(!this->caps()->performColorClearsAsDraws());
1971 this->flushClearColor(colorLoadStore.fClearColor);
1972 this->flushColorWrite(true);
1973 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001974 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001975 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1976 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1977 GL_CALL(StencilMask(0xffffffff));
1978 GL_CALL(ClearStencil(0));
1979 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1980 }
1981 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001982 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001983 this->disableWindowRectangles();
1984 GL_CALL(Clear(clearMask));
1985 }
1986}
1987
1988void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
1989 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1990 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1991 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
1992
1993 this->handleDirtyContext();
1994
1995 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
1996 // The framebuffer binding changed in the middle of a command buffer. We should have already
1997 // printed a warning during onFBOChanged.
1998 return;
1999 }
2000
2001 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2002 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2003
2004 SkSTArray<2, GrGLenum> discardAttachments;
2005 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2006 discardAttachments.push_back(
2007 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2008 }
2009 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2010 discardAttachments.push_back(
2011 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2012 }
2013
2014 if (!discardAttachments.empty()) {
2015 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2016 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2017 discardAttachments.begin()));
2018 } else {
2019 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2020 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2021 discardAttachments.begin()));
2022 }
2023 }
2024 }
2025
Chris Daltonb50cc812019-10-14 16:29:21 -06002026 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2027 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2028 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2029 // Handled by use_tiled_rendering().
2030 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2031 GL_CALL(EndTiling(preserveMask));
2032 }
2033
Chris Dalton674f77a2019-09-30 20:49:39 -06002034 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002035}
2036
Michael Ludwig1e632792020-05-21 12:45:31 -04002037void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002038 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002039 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002040 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002041 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002042 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002043
egdaniel8dc7c3a2015-04-16 11:22:42 -07002044 GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002045 if (!sb) {
2046 // We should only get here if we marked a proxy as requiring a SB. However,
2047 // the SB creation could later fail. Likely clipping is going to go awry now.
2048 return;
2049 }
2050
bsalomon6bc1b5f2015-02-23 09:06:38 -08002051 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002052#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002053 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002054 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002055#else
2056 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002057 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002058 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002059 // changing the clip between stencil passes may or may not
2060 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002061 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002062#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002063 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002064 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002065 value = (1 << (stencilBitCount - 1));
2066 } else {
2067 value = 0;
2068 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002069 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002070 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002071
Michael Ludwig1e632792020-05-21 12:45:31 -04002072 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2073 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002074
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002075 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002076 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002077 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002078 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002079}
2080
Brian Salomone05ba5a2019-04-08 11:59:07 -04002081bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002082 GrColorType surfaceColorType, GrColorType dstColorType,
2083 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002084 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002085
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002086 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002087 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002088 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002089 return false;
2090 }
Greg Danielba88ab62019-07-26 09:14:01 -04002091 GrGLenum externalFormat = 0;
2092 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002093 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2094 surfaceColorType,
2095 dstColorType,
2096 &externalFormat,
2097 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002098 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002099 return false;
2100 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002101
Brian Salomon71d9d842016-11-03 13:42:00 -04002102 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002103 if (renderTarget->numSamples() <= 1 ||
2104 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2105 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2106 this->flushRenderTargetNoColorWrites(renderTarget);
2107 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2108 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2109 return false;
2110 } else {
2111 SkASSERT(renderTarget->requiresManualMSAAResolve());
2112 // we don't track the state of the READ FBO ID.
2113 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002114 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002115 } else {
2116 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002117 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002118 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002119 }
2120
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002121 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002122 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002123
Brian Salomona6948702018-06-01 15:33:20 -04002124 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002125 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002126 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002127 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002128 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002129 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002130
Brian Osman1348ed02018-09-12 09:08:39 -04002131 bool reattachStencil = false;
2132 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2133 renderTarget &&
2134 renderTarget->renderTargetPriv().getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002135 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002136 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2137 reattachStencil = true;
2138 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2139 GR_GL_RENDERBUFFER, 0));
2140 }
2141
Chris Dalton76500e52019-09-05 02:13:05 -06002142 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002143 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002144
2145 if (reattachStencil) {
2146 GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>(
2147 renderTarget->renderTargetPriv().getStencilAttachment());
2148 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2149 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2150 }
2151
Brian Salomon26de56e2019-04-10 12:14:26 -04002152 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002153 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002154 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2155 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002156
Brian Salomone05ba5a2019-04-08 11:59:07 -04002157 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002158 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002159 }
2160 return true;
2161}
2162
2163bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002164 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2165 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002166 SkASSERT(surface);
2167
Brian Salomonb28cb682019-07-26 12:48:47 -04002168 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002169
Brian Salomon26de56e2019-04-10 12:14:26 -04002170 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2171 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002172
Brian Salomon1047a492019-07-02 12:25:21 -04002173 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002174 rowPixelWidth = width;
2175 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002176 SkASSERT(!(rowBytes % bytesPerPixel));
2177 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002178 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002179 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2180 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002181}
2182
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002183GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002184 GrRenderTarget* rt, GrStencilAttachment*,
2185 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002186 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002187 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002188 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002189 if (!fCachedOpsRenderPass) {
2190 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002191 }
2192
Chris Daltonb50cc812019-10-14 16:29:21 -06002193 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002194 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002195}
2196
Brian Salomon1fabd512018-02-09 09:54:25 -05002197void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002198 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002199 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002200 this->didWriteToSurface(target, origin, &bounds);
2201}
bsalomon6ba6fa12015-03-04 11:57:37 -08002202
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002203void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2204 this->flushRenderTargetNoColorWrites(target);
2205 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002206}
2207
Brian Osman9aa30c62018-07-02 15:21:46 -04002208void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002209 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002210 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002211 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002212 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002213#ifdef SK_DEBUG
2214 // don't do this check in Chromium -- this is causing
2215 // lots of repeated command buffer flushes when the compositor is
2216 // rendering with Ganesh, which is really slow; even too slow for
2217 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002218 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002219 GrGLenum status;
2220 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2221 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2222 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2223 }
bsalomon160f24c2015-03-17 15:55:42 -07002224 }
egdanield803f272015-03-18 13:01:52 -07002225#endif
2226 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002227 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002228 }
2229
brianosman35b784d2016-05-05 11:52:53 -07002230 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002231 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002232 }
Brian Salomon9b553272020-01-24 14:38:37 -05002233
2234 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2235 GrGLint format;
2236 GrGLint type;
2237 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2238 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2239 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2240 }
bsalomon083617b2016-02-12 12:10:14 -08002241}
bsalomona9909122016-01-23 10:41:40 -08002242
brianosman33f6b3f2016-06-02 05:49:21 -07002243void GrGLGpu::flushFramebufferSRGB(bool enable) {
2244 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2245 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2246 fHWSRGBFramebuffer = kYes_TriState;
2247 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2248 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2249 fHWSRGBFramebuffer = kNo_TriState;
2250 }
2251}
2252
Greg Danielacd66b42019-05-22 16:29:12 -04002253void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002254 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002255 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002256 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002257 fHWViewport = viewport;
2258 }
2259}
2260
Chris Dalton33db9fc2020-02-25 18:52:12 -07002261GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002262 fStats.incNumDraws();
2263
Chris Dalton2e7ed262020-02-21 15:17:59 -07002264 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2265 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2266 GL_CALL(Enable(GR_GL_CULL_FACE));
2267 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002268 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002269 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002270
Chris Dalton3809bab2017-06-13 10:55:06 -06002271 switch (primitiveType) {
2272 case GrPrimitiveType::kTriangles:
2273 return GR_GL_TRIANGLES;
2274 case GrPrimitiveType::kTriangleStrip:
2275 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002276 case GrPrimitiveType::kPoints:
2277 return GR_GL_POINTS;
2278 case GrPrimitiveType::kLines:
2279 return GR_GL_LINES;
2280 case GrPrimitiveType::kLineStrip:
2281 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002282 case GrPrimitiveType::kPatches:
2283 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002284 case GrPrimitiveType::kPath:
2285 SK_ABORT("non-mesh-based GrPrimitiveType");
2286 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002287 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002288 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002289}
2290
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002291void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002292 // Some extensions automatically resolves the texture when it is read.
2293 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2294
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002295 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002296 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2297 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2298 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2299 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002300
Chris Daltonc139d082019-09-26 14:04:24 -06002301 // make sure we go through flushRenderTarget() since we've modified
2302 // the bound DRAW FBO ID.
2303 fHWBoundRenderTargetUniqueID.makeInvalid();
2304 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2305 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002306 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2307 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002308 GrScissorState scissor(rt->dimensions());
2309 SkAssertResult(scissor.set(resolveRect));
2310 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002311 this->disableWindowRectangles();
2312 GL_CALL(ResolveMultisampleFramebuffer());
2313 } else {
2314 int l, b, r, t;
2315 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2316 this->glCaps().blitFramebufferSupportFlags()) {
2317 l = 0;
2318 b = 0;
2319 r = target->width();
2320 t = target->height();
2321 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002322 l = resolveRect.x();
2323 b = resolveRect.y();
2324 r = resolveRect.x() + resolveRect.width();
2325 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002326 }
Chris Daltonc139d082019-09-26 14:04:24 -06002327
2328 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002329 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002330 this->disableWindowRectangles();
2331 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 +00002332 }
2333}
2334
bsalomon@google.com411dad02012-06-05 20:24:20 +00002335namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002336
bsalomon@google.com411dad02012-06-05 20:24:20 +00002337
2338GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002339 static const GrGLenum gTable[kGrStencilOpCount] = {
2340 GR_GL_KEEP, // kKeep
2341 GR_GL_ZERO, // kZero
2342 GR_GL_REPLACE, // kReplace
2343 GR_GL_INVERT, // kInvert
2344 GR_GL_INCR_WRAP, // kIncWrap
2345 GR_GL_DECR_WRAP, // kDecWrap
2346 GR_GL_INCR, // kIncClamp
2347 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002348 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002349 static_assert(0 == (int)GrStencilOp::kKeep);
2350 static_assert(1 == (int)GrStencilOp::kZero);
2351 static_assert(2 == (int)GrStencilOp::kReplace);
2352 static_assert(3 == (int)GrStencilOp::kInvert);
2353 static_assert(4 == (int)GrStencilOp::kIncWrap);
2354 static_assert(5 == (int)GrStencilOp::kDecWrap);
2355 static_assert(6 == (int)GrStencilOp::kIncClamp);
2356 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002357 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2358 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002359}
2360
2361void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002362 const GrStencilSettings::Face& face,
2363 GrGLenum glFace) {
2364 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2365 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2366 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002367
cdalton93a379b2016-05-11 13:58:08 -07002368 GrGLint ref = face.fRef;
2369 GrGLint mask = face.fTestMask;
2370 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002371
2372 if (GR_GL_FRONT_AND_BACK == glFace) {
2373 // we call the combined func just in case separate stencil is not
2374 // supported.
2375 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2376 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002377 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002378 } else {
2379 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2380 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002381 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002382 }
2383}
2384}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002385
Chris Dalton71713f62019-04-18 12:41:03 -06002386void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002387 if (stencilSettings.isDisabled()) {
2388 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002389 } else if (fHWStencilSettings != stencilSettings ||
2390 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002391 if (kYes_TriState != fHWStencilTestEnabled) {
2392 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002393
csmartdaltonc7d85332016-10-26 10:13:46 -07002394 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002395 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002396 if (!stencilSettings.isTwoSided()) {
2397 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2398 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002399 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002400 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2401 GR_GL_FRONT);
2402 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2403 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002404 }
joshualitta58fe352014-10-27 08:39:00 -07002405 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002406 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002407 }
2408}
2409
csmartdaltonc7d85332016-10-26 10:13:46 -07002410void GrGLGpu::disableStencil() {
2411 if (kNo_TriState != fHWStencilTestEnabled) {
2412 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002413
csmartdaltonc7d85332016-10-26 10:13:46 -07002414 fHWStencilTestEnabled = kNo_TriState;
2415 fHWStencilSettings.invalidate();
2416 }
2417}
2418
Chris Dalton4c56b032019-03-04 12:28:42 -07002419void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002420 // rt is only optional if useHWAA is false.
2421 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002422#ifdef SK_DEBUG
2423 if (useHWAA && rt->numSamples() <= 1) {
2424 SkASSERT(this->caps()->mixedSamplesSupport());
2425 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
2426 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
2427 }
2428#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002429
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002430 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002431 if (useHWAA) {
2432 if (kYes_TriState != fMSAAEnabled) {
2433 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2434 fMSAAEnabled = kYes_TriState;
2435 }
2436 } else {
2437 if (kNo_TriState != fMSAAEnabled) {
2438 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2439 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002440 }
2441 }
2442 }
2443}
2444
Chris Daltonce425af2019-12-16 10:39:03 -07002445void GrGLGpu::flushConservativeRasterState(bool enabled) {
2446 if (this->caps()->conservativeRasterSupport()) {
2447 if (enabled) {
2448 if (kYes_TriState != fHWConservativeRasterEnabled) {
2449 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2450 fHWConservativeRasterEnabled = kYes_TriState;
2451 }
2452 } else {
2453 if (kNo_TriState != fHWConservativeRasterEnabled) {
2454 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2455 fHWConservativeRasterEnabled = kNo_TriState;
2456 }
2457 }
2458 }
2459}
2460
Chris Dalton1215cda2019-12-17 21:44:04 -07002461void GrGLGpu::flushWireframeState(bool enabled) {
2462 if (this->caps()->wireframeSupport()) {
2463 if (this->caps()->wireframeMode() || enabled) {
2464 if (kYes_TriState != fHWWireframeEnabled) {
2465 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2466 fHWWireframeEnabled = kYes_TriState;
2467 }
2468 } else {
2469 if (kNo_TriState != fHWWireframeEnabled) {
2470 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2471 fHWWireframeEnabled = kNo_TriState;
2472 }
2473 }
2474 }
2475}
2476
Chris Daltonbbb3f642019-07-24 12:25:08 -04002477void GrGLGpu::flushBlendAndColorWrite(
2478 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2479 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2480 // We need to work around a driver bug by using a blend state that preserves the dst color,
2481 // rather than disabling color writes.
2482 GrXferProcessor::BlendInfo preserveDstBlend;
2483 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2484 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2485 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2486 return;
2487 }
bsalomonf7cc8772015-05-11 11:21:14 -07002488
cdalton8917d622015-05-06 13:40:21 -07002489 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002490 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2491 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002492
2493 // Any optimization to disable blending should have already been applied and
2494 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002495 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2496 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002497
egdanielb414f252014-07-29 13:15:47 -07002498 if (blendOff) {
2499 if (kNo_TriState != fHWBlendState.fEnabled) {
2500 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002501
Jim Van Verth1bef9792020-07-09 08:09:13 -04002502 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002503 // https://code.google.com/p/skia/issues/detail?id=3943
2504 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2505 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2506 SkASSERT(this->caps()->advancedBlendEquationSupport());
2507 // Set to any basic blending equation.
2508 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2509 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2510 fHWBlendState.fEquation = blend_equation;
2511 }
2512
egdanielb414f252014-07-29 13:15:47 -07002513 fHWBlendState.fEnabled = kNo_TriState;
2514 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002515 } else {
2516 if (kYes_TriState != fHWBlendState.fEnabled) {
2517 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002518
Chris Daltonbbb3f642019-07-24 12:25:08 -04002519 fHWBlendState.fEnabled = kYes_TriState;
2520 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002521
Chris Daltonbbb3f642019-07-24 12:25:08 -04002522 if (fHWBlendState.fEquation != equation) {
2523 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2524 fHWBlendState.fEquation = equation;
2525 }
cdalton8917d622015-05-06 13:40:21 -07002526
Chris Daltonbbb3f642019-07-24 12:25:08 -04002527 if (GrBlendEquationIsAdvanced(equation)) {
2528 SkASSERT(this->caps()->advancedBlendEquationSupport());
2529 // Advanced equations have no other blend state.
2530 return;
2531 }
cdalton8917d622015-05-06 13:40:21 -07002532
Chris Daltonbbb3f642019-07-24 12:25:08 -04002533 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2534 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2535 gXfermodeCoeff2Blend[dstCoeff]));
2536 fHWBlendState.fSrcCoeff = srcCoeff;
2537 fHWBlendState.fDstCoeff = dstCoeff;
2538 }
cdalton8917d622015-05-06 13:40:21 -07002539
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002540 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002541 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2542 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2543 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2544 fHWBlendState.fConstColor = blendConst;
2545 fHWBlendState.fConstColorValid = true;
2546 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002547 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002548 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002549
2550 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002551}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002552
Brian Salomondc829942018-10-23 16:07:24 -04002553static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002554 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002555 switch (swizzle[i]) {
2556 case 'r': glValues[i] = GR_GL_RED; break;
2557 case 'g': glValues[i] = GR_GL_GREEN; break;
2558 case 'b': glValues[i] = GR_GL_BLUE; break;
2559 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002560 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002561 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002562 default: SK_ABORT("Unsupported component");
2563 }
Brian Salomon327955b2018-10-22 15:39:22 +00002564 }
2565}
2566
Greg Daniel2c3398d2019-06-19 11:58:01 -04002567void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2568 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002569 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002570
reed856e9d92015-09-30 12:21:45 -07002571#ifdef SK_DEBUG
2572 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002573 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002574 const int w = texture->width();
2575 const int h = texture->height();
2576 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2577 }
2578 }
2579#endif
2580
Robert Phillips294870f2016-11-11 12:38:40 -05002581 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002582 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002583 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002584 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002585 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002586 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002587 }
2588
Brian Salomondc829942018-10-23 16:07:24 -04002589 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -04002590 if (!this->caps()->mipMapSupport() ||
2591 texture->texturePriv().mipMapped() == GrMipMapped::kNo) {
Brian Salomondc829942018-10-23 16:07:24 -04002592 samplerState.setFilterMode(GrSamplerState::Filter::kBilerp);
bsalomonefd7d452014-10-23 14:17:46 -07002593 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002594 }
bsalomonefd7d452014-10-23 14:17:46 -07002595
brianosman33f6b3f2016-06-02 05:49:21 -07002596#ifdef SK_DEBUG
Brian Osman2b23c4b2018-06-01 12:25:08 -04002597 // We were supposed to ensure MipMaps were up-to-date before getting here.
Brian Salomondc829942018-10-23 16:07:24 -04002598 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
brianosman33f6b3f2016-06-02 05:49:21 -07002599 SkASSERT(!texture->texturePriv().mipMapsAreDirty());
brianosman33f6b3f2016-06-02 05:49:21 -07002600 }
2601#endif
2602
Brian Salomone2826ab2019-06-04 15:58:31 -04002603 auto timestamp = texture->parameters()->resetTimestamp();
2604 bool setAll = timestamp < fResetTimestampForTextureParameters;
cblume55f2d2d2016-02-26 13:20:48 -08002605
Brian Salomone2826ab2019-06-04 15:58:31 -04002606 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2607 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002608 if (fSamplerObjectCache) {
2609 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Brian Salomon1908bb82020-05-13 12:22:54 -04002610 if (this->glCaps().mustSetTexParameterMinFilterToEnableMipMapping()) {
2611 if (samplerState.filter() == GrSamplerState::Filter::kMipMap) {
2612 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2613 texture->parameters()->samplerOverriddenState();
2614 if (setAll || oldSamplerState.fMinFilter != GR_GL_LINEAR_MIPMAP_LINEAR) {
2615 this->setTextureUnit(unitIdx);
2616 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER,
2617 GR_GL_LINEAR_MIPMAP_LINEAR));
2618 newSamplerState = oldSamplerState;
2619 newSamplerState.fMinFilter = GR_GL_LINEAR_MIPMAP_LINEAR;
2620 samplerStateToRecord = &newSamplerState;
2621 }
2622 }
2623 }
Brian Salomondc829942018-10-23 16:07:24 -04002624 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002625 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2626 texture->parameters()->samplerOverriddenState();
2627 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002628
Brian Salomone2826ab2019-06-04 15:58:31 -04002629 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter());
2630 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002631
Brian Salomone2826ab2019-06-04 15:58:31 -04002632 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2633 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002634
2635 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002636 newSamplerState.fMinLOD = -1000.f;
2637 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002638
Brian Salomone2826ab2019-06-04 15:58:31 -04002639 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002640 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002642 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002644 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002645 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002646 }
Mike Klein1bccae52018-10-19 11:38:44 +00002647 if (this->glCaps().mipMapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002648 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002649 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002651 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002653 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002654 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002655 }
2656 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002657 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002658 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002659 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002660 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002662 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002663 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002664 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002665 if (this->glCaps().clampToBorderSupport()) {
2666 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002667 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002668 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002669 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2670 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002671 }
2672 }
Brian Salomondc829942018-10-23 16:07:24 -04002673 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002674 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2675 newNonsamplerState.fBaseMipMapLevel = 0;
2676 newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002677
Brian Salomone2826ab2019-06-04 15:58:31 -04002678 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2679 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002680 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002681 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2682 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002683 GrGLenum glValues[4];
2684 get_gl_swizzle_values(swizzle, glValues);
2685 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002686 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002687 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002688 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2689 reinterpret_cast<const GrGLint*>(glValues)));
2690 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002691 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2692 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2693 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2694 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2695 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002696 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002697 }
2698 }
Brian Salomondc829942018-10-23 16:07:24 -04002699 // These are not supported in ES2 contexts
Brian Salomonf3841932018-11-29 09:13:37 -05002700 if (this->glCaps().mipMapLevelAndLodControlSupport() &&
2701 (texture->texturePriv().textureType() != GrTextureType::kExternal ||
2702 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002703 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002704 this->setTextureUnit(unitIdx);
2705 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002706 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002707 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002708 if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002709 this->setTextureUnit(unitIdx);
2710 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002711 newNonsamplerState.fMaxMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002712 }
Brian Salomon327955b2018-10-22 15:39:22 +00002713 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002714 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2715 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002716}
2717
Brian Salomon1f05d452019-02-08 12:33:08 -05002718void GrGLGpu::onResetTextureBindings() {
2719 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2720 GR_GL_TEXTURE_EXTERNAL};
2721 for (int i = 0; i < this->numTextureUnits(); ++i) {
2722 this->setTextureUnit(i);
2723 for (auto target : kTargets) {
2724 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2725 GL_CALL(BindTexture(target, 0));
2726 }
2727 }
2728 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2729 }
2730}
2731
Chris Dalton5a2f9622019-12-27 14:56:38 -07002732void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2733 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2734 if (fHWPatchVertexCount != count) {
2735 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2736 fHWPatchVertexCount = count;
2737 }
2738}
2739
egdaniel080e6732014-12-22 07:35:52 -08002740void GrGLGpu::flushColorWrite(bool writeColor) {
2741 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002742 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002743 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2744 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002745 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002746 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002747 } else {
2748 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002749 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002750 fHWWriteToColor = kYes_TriState;
2751 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002752 }
bsalomon3e791242014-12-17 13:43:13 -08002753}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002754
Chris Dalton674f77a2019-09-30 20:49:39 -06002755void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2756 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2757 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2758 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2759 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2760 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2761 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2762 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002763 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2764 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2765 GL_CALL(ClearColor(r, g, b, a));
2766 fHWClearColor[0] = r;
2767 fHWClearColor[1] = g;
2768 fHWClearColor[2] = b;
2769 fHWClearColor[3] = a;
2770 }
2771}
2772
bsalomon861e1032014-12-16 07:33:49 -08002773void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002774 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002775 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002776 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002777 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002778 }
2779}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002780
Brian Salomond978b902019-02-07 15:09:18 -05002781void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002782 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002783 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002784 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2785 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2786 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002787 }
Brian Salomond978b902019-02-07 15:09:18 -05002788 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2789 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002790 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002791 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002792}
2793
Brian Salomone5e7eb12016-10-14 16:18:33 -04002794// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002795static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2796 const GrSurface* src,
2797 const SkIRect& srcRect,
2798 const SkIPoint& dstPoint,
2799 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002800 int dstSampleCnt = 0;
2801 int srcSampleCnt = 0;
2802 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002803 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002804 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002805 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002806 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002807 }
2808 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2809 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2810
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002811 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2812 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2813
Brian Salomone5e7eb12016-10-14 16:18:33 -04002814 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002815 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002816
Greg Daniel46cfbc62019-06-07 11:43:30 -04002817 GrTextureType dstTexType;
2818 GrTextureType* dstTexTypePtr = nullptr;
2819 GrTextureType srcTexType;
2820 GrTextureType* srcTexTypePtr = nullptr;
2821 if (dstTex) {
2822 dstTexType = dstTex->texturePriv().textureType();
2823 dstTexTypePtr = &dstTexType;
2824 }
2825 if (srcTex) {
2826 srcTexType = srcTex->texturePriv().textureType();
2827 srcTexTypePtr = &srcTexType;
2828 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002829
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002830 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2831 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002832 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002833}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002834
Brian Osmana9c8a052018-01-19 10:31:56 -05002835static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2836 // A RT has a separate MSAA renderbuffer if:
2837 // 1) It's multisampled
2838 // 2) We're using an extension with separate MSAA renderbuffers
2839 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002840 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002841}
2842
Greg Daniel46cfbc62019-06-07 11:43:30 -04002843static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002844 const GrGLCaps& caps) {
2845
bsalomon@google.comeb851172013-04-15 13:51:00 +00002846 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002847 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002848 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002849 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002850
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002851 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2852 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2853
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002854 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2855 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2856
Greg Daniel46cfbc62019-06-07 11:43:30 -04002857 GrTextureType dstTexType;
2858 GrTextureType* dstTexTypePtr = nullptr;
2859 GrTextureType srcTexType;
2860 GrTextureType* srcTexTypePtr = nullptr;
2861 if (dstTex) {
2862 dstTexType = dstTex->texturePriv().textureType();
2863 dstTexTypePtr = &dstTexType;
2864 }
2865 if (srcTex) {
2866 srcTexType = srcTex->texturePriv().textureType();
2867 srcTexTypePtr = &srcTexType;
2868 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002869
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002870 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2871 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002872}
2873
Greg Danielacd66b42019-05-22 16:29:12 -04002874// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002875void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002876 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002877 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002878 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002879 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002880 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2881 GrGLuint texID = texture->textureID();
2882 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002883 GrGLuint* tempFBOID;
2884 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002885
egdanield803f272015-03-18 13:01:52 -07002886 if (0 == *tempFBOID) {
2887 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002888 }
2889
Adrienne Walker4ee88512018-05-17 11:37:14 -07002890 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002891 GR_GL_CALL(
2892 this->glInterface(),
2893 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2894 if (mipLevel == 0) {
2895 texture->baseLevelWasBoundToFBO();
2896 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002897 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002898 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002899 }
egdaniel0f5f9672015-02-03 11:10:51 -08002900}
2901
Brian Salomond2a8ae22019-09-10 16:03:59 -04002902void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002903 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002904 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002905 SkASSERT(surface->asTexture());
2906 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2907 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2908 GR_GL_COLOR_ATTACHMENT0,
2909 textureTarget,
2910 0,
2911 0));
2912 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002913}
2914
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002915void GrGLGpu::onFBOChanged() {
2916 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2917 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002918 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002919 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002920#ifdef SK_DEBUG
2921 if (fIsExecutingCommandBuffer_DebugOnly) {
2922 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2923 "This will severely hurt performance.\n");
2924 }
2925#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002926}
2927
Adrienne Walker4ee88512018-05-17 11:37:14 -07002928void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2929 fStats.incRenderTargetBinds();
2930 GL_CALL(BindFramebuffer(target, fboid));
2931 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2932 fBoundDrawFramebuffer = fboid;
2933 }
2934
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002935 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2936 // The driver forgets the correct scissor when modifying the FBO binding.
2937 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002938 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002939 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002940 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002941 }
2942
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002943 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002944}
2945
Adrienne Walker4ee88512018-05-17 11:37:14 -07002946void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002947 // We're relying on the GL state shadowing being correct in the workaround code below so we
2948 // need to handle a dirty context.
2949 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002950 if (fboid == fBoundDrawFramebuffer &&
2951 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2952 // This workaround only applies to deleting currently bound framebuffers
2953 // on Adreno 420. Because this is a somewhat rare case, instead of
2954 // tracking all the attachments of every framebuffer instead just always
2955 // unbind all attachments.
2956 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2957 GR_GL_RENDERBUFFER, 0));
2958 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2959 GR_GL_RENDERBUFFER, 0));
2960 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2961 GR_GL_RENDERBUFFER, 0));
2962 }
2963
2964 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002965
2966 // Deleting the currently bound framebuffer rebinds to 0.
2967 if (fboid == fBoundDrawFramebuffer) {
2968 this->onFBOChanged();
2969 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002970}
2971
Greg Daniel46cfbc62019-06-07 11:43:30 -04002972bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002973 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002974 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2975 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2976 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002977 auto dstFormat = dst->backendFormat().asGLFormat();
2978 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002979 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002980 return true;
2981 }
2982 }
cblume61214052016-01-26 09:10:48 -08002983
Greg Daniel46cfbc62019-06-07 11:43:30 -04002984 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2985 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002986 return true;
2987 }
2988
Greg Daniel46cfbc62019-06-07 11:43:30 -04002989 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2990 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991 }
2992
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002993 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002994 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002995 return true;
2996 }
bsalomon083617b2016-02-12 12:10:14 -08002997 }
2998
bsalomon6df86402015-06-01 10:41:49 -07002999 return false;
3000}
3001
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003002bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003003 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003004
3005 int progIdx = TextureToCopyProgramIdx(srcTex);
3006 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
3007 GrSLType samplerType =
3008 GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType());
3009
3010 if (!fCopyProgramArrayBuffer) {
3011 static const GrGLfloat vdata[] = {
3012 0, 0,
3013 0, 1,
3014 1, 0,
3015 1, 1
3016 };
3017 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3018 kStatic_GrAccessPattern, vdata);
3019 }
3020 if (!fCopyProgramArrayBuffer) {
3021 return false;
3022 }
3023
3024 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3025 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3026 if (!fCopyPrograms[progIdx].fProgram) {
3027 return false;
3028 }
3029
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003030 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003031 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003032 GrShaderVar::TypeModifier::Uniform);
3033 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3034 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3035 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3036 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003037
Greg Daniel9e3169b2019-06-06 14:38:17 -04003038 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003039 if (shaderCaps->noperspectiveInterpolationSupport()) {
3040 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3041 vshaderTxt.appendf("#extension %s : require\n", extension);
3042 }
3043 vTexCoord.addModifier("noperspective");
3044 }
3045
3046 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3047 vshaderTxt.append(";");
3048 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3049 vshaderTxt.append(";");
3050 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3051 vshaderTxt.append(";");
3052 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3053 vshaderTxt.append(";");
3054
3055 vshaderTxt.append(
3056 "// Copy Program VS\n"
3057 "void main() {"
3058 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3059 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3060 " sk_Position.zw = half2(0, 1);"
3061 "}"
3062 );
3063
Greg Daniel9e3169b2019-06-06 14:38:17 -04003064 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003065 if (shaderCaps->noperspectiveInterpolationSupport()) {
3066 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3067 fshaderTxt.appendf("#extension %s : require\n", extension);
3068 }
3069 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003070 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003071 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3072 fshaderTxt.append(";");
3073 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3074 fshaderTxt.append(";");
3075 fshaderTxt.appendf(
3076 "// Copy Program FS\n"
3077 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003078 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003079 "}"
3080 );
3081
3082 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3083 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3084 SkSL::Program::Settings settings;
3085 settings.fCaps = shaderCaps;
3086 SkSL::String glsl;
3087 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3088 sksl, settings, &glsl, errorHandler);
3089 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3090 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3091 SkASSERT(program->fInputs.isEmpty());
3092
3093 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3094 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3095 errorHandler);
3096 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3097 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3098 errorHandler);
3099 SkASSERT(program->fInputs.isEmpty());
3100
3101 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3102
3103 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3104 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3105 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3106 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3107 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3108 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3109
3110 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3111
3112 GL_CALL(DeleteShader(vshader));
3113 GL_CALL(DeleteShader(fshader));
3114
3115 return true;
3116}
3117
brianosman33f6b3f2016-06-02 05:49:21 -07003118bool GrGLGpu::createMipmapProgram(int progIdx) {
3119 const bool oddWidth = SkToBool(progIdx & 0x2);
3120 const bool oddHeight = SkToBool(progIdx & 0x1);
3121 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3122
Brian Salomon1edc5b92016-11-29 13:43:46 -05003123 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003124
3125 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3126 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3127 if (!fMipmapPrograms[progIdx].fProgram) {
3128 return false;
3129 }
3130
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003131 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003132 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003133 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003134 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003135 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003136 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003137 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003138 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3139 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3140 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3141 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003142 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003143 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003144
Ethan Nicholasfc994162019-06-06 10:04:27 -04003145 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003146 if (shaderCaps->noperspectiveInterpolationSupport()) {
3147 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003148 vshaderTxt.appendf("#extension %s : require\n", extension);
3149 }
3150 vTexCoords[0].addModifier("noperspective");
3151 vTexCoords[1].addModifier("noperspective");
3152 vTexCoords[2].addModifier("noperspective");
3153 vTexCoords[3].addModifier("noperspective");
3154 }
3155
Brian Salomon1edc5b92016-11-29 13:43:46 -05003156 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003157 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003158 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003159 vshaderTxt.append(";");
3160 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003161 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003162 vshaderTxt.append(";");
3163 }
3164
3165 vshaderTxt.append(
3166 "// Mipmap Program VS\n"
3167 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003168 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3169 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003170 );
3171
3172 // Insert texture coordinate computation:
3173 if (oddWidth && oddHeight) {
3174 vshaderTxt.append(
3175 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003176 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3177 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003178 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3179 );
3180 } else if (oddWidth) {
3181 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003182 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3183 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003184 );
3185 } else if (oddHeight) {
3186 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003187 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3188 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003189 );
3190 } else {
3191 vshaderTxt.append(
3192 " v_texCoord0 = a_vertex.xy;"
3193 );
3194 }
3195
3196 vshaderTxt.append("}");
3197
Ethan Nicholasfc994162019-06-06 10:04:27 -04003198 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003199 if (shaderCaps->noperspectiveInterpolationSupport()) {
3200 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003201 fshaderTxt.appendf("#extension %s : require\n", extension);
3202 }
3203 }
brianosman33f6b3f2016-06-02 05:49:21 -07003204 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003205 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003206 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003207 fshaderTxt.append(";");
3208 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003209 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003210 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003211 fshaderTxt.append(
3212 "// Mipmap Program FS\n"
3213 "void main() {"
3214 );
3215
3216 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003217 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003218 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3219 " sample(u_texture, v_texCoord1) + "
3220 " sample(u_texture, v_texCoord2) + "
3221 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003222 );
3223 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003224 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003225 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3226 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003227 );
3228 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003229 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003230 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003231 );
3232 }
3233
3234 fshaderTxt.append("}");
3235
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003236 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003237 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003238 SkSL::Program::Settings settings;
3239 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003240 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003241 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003242 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003243 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003244 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003245 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003246
Brian Osman6c431d52019-04-15 16:31:54 -04003247 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003248 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3249 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003250 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003251 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003252 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003253 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003254
3255 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3256
3257 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3258 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3259 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3260 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3261
3262 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3263
3264 GL_CALL(DeleteShader(vshader));
3265 GL_CALL(DeleteShader(fshader));
3266
3267 return true;
3268}
3269
Greg Daniel46cfbc62019-06-07 11:43:30 -04003270bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003271 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003272 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3273 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3274 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3275 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003276 return false;
3277 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003278 int progIdx = TextureToCopyProgramIdx(srcTex);
3279 if (!dstRT) {
3280 SkASSERT(dstTex);
3281 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3282 return false;
3283 }
3284 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003285 if (!fCopyPrograms[progIdx].fProgram) {
3286 if (!this->createCopyProgram(srcTex)) {
3287 SkDebugf("Failed to create copy program.\n");
3288 return false;
3289 }
3290 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003291 int w = srcRect.width();
3292 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003293 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003294 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003295 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003296 this->flushViewport(dst->width(), dst->height());
3297 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003298 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003299 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003300 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3302 attribs->enableVertexArrays(this, 1);
3303 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3304 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003305 // dst rect edges in NDC (-1 to 1)
3306 int dw = dst->width();
3307 int dh = dst->height();
3308 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3309 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3310 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3311 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003312 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3313 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3314 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3315 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3316 int sw = src->width();
3317 int sh = src->height();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003318 if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) {
3319 // src rect edges in normalized texture space (0 to 1)
3320 sx0 /= sw;
3321 sx1 /= sw;
3322 sy0 /= sh;
3323 sy1 /= sh;
3324 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003325 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3326 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3327 sx1 - sx0, sy1 - sy0, sx0, sy0));
3328 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003329 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003330 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003331 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003332 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003333 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003334 this->disableWindowRectangles();
3335 this->disableStencil();
3336 if (this->glCaps().srgbWriteControl()) {
3337 this->flushFramebufferSRGB(true);
3338 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003339 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003340 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003341 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3342 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003343 return true;
3344}
3345
Greg Daniel46cfbc62019-06-07 11:43:30 -04003346void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003347 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003348 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003349 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003350 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003351 SkASSERT(dstTex);
3352 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003353 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003354
Brian Salomond978b902019-02-07 15:09:18 -05003355 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003356 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003357 dstPoint.fX, dstPoint.fY,
3358 srcRect.fLeft, srcRect.fTop,
3359 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003360 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003361 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3362 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003363 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3364 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003365}
3366
Greg Daniel46cfbc62019-06-07 11:43:30 -04003367bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003368 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003369 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003370 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3371 srcRect.width(), srcRect.height());
3372 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003373 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003374 return false;
mtklein404b3b22015-05-18 09:29:10 -07003375 }
bsalomon5df6fee2015-05-18 06:26:15 -07003376 }
bsalomon6df86402015-06-01 10:41:49 -07003377
Brian Salomond2a8ae22019-09-10 16:03:59 -04003378 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3379 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003380 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003381 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003382
3383 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003384 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003385 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003386
Greg Daniel46cfbc62019-06-07 11:43:30 -04003387 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3388 srcRect.fTop,
3389 srcRect.fRight,
3390 srcRect.fBottom,
3391 dstRect.fLeft,
3392 dstRect.fTop,
3393 dstRect.fRight,
3394 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003395 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003396 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3397 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003398
3399 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3400 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003401 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003402}
3403
Brian Salomon930f9392018-06-20 16:25:26 -04003404bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3405 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003406 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003407 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003408 return false;
3409 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003410 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003411 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3412 // Uses draw calls to do a series of downsample operations to successive mips.
3413
3414 // The manual approach requires the ability to limit which level we're sampling and that the
3415 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003416 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003417 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003418 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003419 GL_CALL(GenerateMipmap(glTex->target()));
3420 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003421 }
3422
brianosman33f6b3f2016-06-02 05:49:21 -07003423 int width = texture->width();
3424 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003425 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Greg Danielda86e282018-06-13 09:41:19 -04003426 SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003427
3428 // Create (if necessary), then bind temporary FBO:
3429 if (0 == fTempDstFBOID) {
3430 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3431 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003432 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003433 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003434
3435 // Bind the texture, to get things configured for filtering.
3436 // We'll be changing our base level further below:
3437 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003438 // The mipmap program does not do any swizzling.
Brian Salomonccb61422020-01-09 10:46:36 -05003439 this->bindTexture(0, GrSamplerState::Filter::kBilerp, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003440
3441 // Vertex data:
3442 if (!fMipmapProgramArrayBuffer) {
3443 static const GrGLfloat vdata[] = {
3444 0, 0,
3445 0, 1,
3446 1, 0,
3447 1, 1
3448 };
Brian Salomonae64c192019-02-05 09:41:37 -05003449 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003450 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003451 }
3452 if (!fMipmapProgramArrayBuffer) {
3453 return false;
3454 }
3455
3456 fHWVertexArrayState.setVertexArrayID(this, 0);
3457
3458 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003459 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003460 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003461 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003462
3463 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003464 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003465 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003466 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003467 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003468 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003469
3470 // Do all the blits:
3471 width = texture->width();
3472 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003473
brianosman33f6b3f2016-06-02 05:49:21 -07003474 for (GrGLint level = 1; level < levelCount; ++level) {
3475 // Get and bind the program for this particular downsample (filter shape can vary):
3476 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3477 if (!fMipmapPrograms[progIdx].fProgram) {
3478 if (!this->createMipmapProgram(progIdx)) {
3479 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003480 // Invalidate all params to cover base level change in a previous iteration.
3481 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003482 return false;
3483 }
3484 }
Brian Salomon802cb312018-06-08 18:05:20 -04003485 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003486
3487 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3488 const float invWidth = 1.0f / width;
3489 const float invHeight = 1.0f / height;
3490 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3491 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3492 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3493
3494 // Only sample from previous mip
3495 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3496
Brian Salomon930f9392018-06-20 16:25:26 -04003497 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3498 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003499
Brian Osman788b9162020-02-07 10:36:46 -05003500 width = std::max(1, width / 2);
3501 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003502 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003503
3504 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3505 }
3506
3507 // Unbind:
3508 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3509 GR_GL_TEXTURE_2D, 0, 0));
3510
Brian Salomon930f9392018-06-20 16:25:26 -04003511 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003512 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3513 // We drew the 2nd to last level into the last level.
3514 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3515 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003516
brianosman33f6b3f2016-06-02 05:49:21 -07003517 return true;
3518}
3519
Chris Daltond7291ba2019-03-07 14:17:03 -07003520void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003521 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3522 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003523
3524 int effectiveSampleCnt;
3525 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003526 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003527
3528 sampleLocations->reset(effectiveSampleCnt);
3529 for (int i = 0; i < effectiveSampleCnt; ++i) {
3530 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3531 }
3532}
3533
cdalton231c5fd2015-05-13 12:35:36 -07003534void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003535 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003536 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003537 case kTexture_GrXferBarrierType: {
3538 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003539 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003540 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3541 // The render target uses separate storage so no need for glTextureBarrier.
3542 // FIXME: The render target will resolve automatically when its texture is bound,
3543 // but we could resolve only the bounds that will be read if we do it here instead.
3544 return;
3545 }
cdalton9954bc32015-04-29 14:17:00 -07003546 SkASSERT(this->caps()->textureBarrierSupport());
3547 GL_CALL(TextureBarrier());
3548 return;
cdalton231c5fd2015-05-13 12:35:36 -07003549 }
cdalton8917d622015-05-06 13:40:21 -07003550 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003551 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003552 this->caps()->blendEquationSupport());
3553 GL_CALL(BlendBarrier());
3554 return;
bsalomoncb02b382015-08-12 11:14:50 -07003555 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003556 }
3557}
3558
Chris Daltone1196c52019-12-28 14:31:09 -07003559void GrGLGpu::insertManualFramebufferBarrier() {
3560 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3561 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3562}
3563
Brian Salomon85c3d682019-11-04 15:04:54 -05003564GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003565 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003566 GrRenderable renderable,
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003567 GrMipMapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003568 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003569 // We don't support protected textures in GL.
3570 if (isProtected == GrProtected::kYes) {
3571 return {};
3572 }
3573
Brian Salomon8a375832018-03-14 10:21:40 -04003574 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003575
Brian Salomond4764a12019-08-08 12:08:24 -04003576 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003577 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003578 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003579 }
3580
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003581 int numMipLevels = 1;
3582 if (mipMapped == GrMipMapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003583 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003584 }
3585
Robert Phillipsd34691b2019-09-24 13:38:43 -04003586 // Compressed formats go through onCreateCompressedBackendTexture
3587 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3588
Greg Daniel15500642019-06-27 03:05:55 +00003589 GrGLTextureInfo info;
3590 GrGLTextureParameters::SamplerOverriddenState initialState;
3591
Greg Danield51fa2f2020-01-22 16:53:38 -05003592 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003593 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003594 }
Brian Salomon0f396992020-06-19 19:51:21 -04003595 switch (format.textureType()) {
3596 case GrTextureType::kNone:
3597 case GrTextureType::kExternal:
3598 return {};
3599 case GrTextureType::k2D:
3600 info.fTarget = GR_GL_TEXTURE_2D;
3601 break;
3602 case GrTextureType::kRectangle:
3603 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipMapped::kYes) {
3604 return {};
3605 }
3606 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3607 break;
3608 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003609 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003610 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3611 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003612 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003613 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003614 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003615
Brian Salomon85c3d682019-11-04 15:04:54 -05003616 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003617 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003618
Brian Salomone2826ab2019-06-04 15:58:31 -04003619 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003620 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003621 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3622 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003623
Brian Salomon85c3d682019-11-04 15:04:54 -05003624 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3625 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003626}
3627
Greg Daniel16032b32020-05-06 15:31:10 -04003628bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3629 sk_sp<GrRefCntedCallback> finishedCallback,
3630 const BackendTextureData* data) {
3631 GrGLTextureInfo info;
3632 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3633
3634 int numMipLevels = 1;
3635 if (backendTexture.hasMipMaps()) {
3636 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003637 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003638 }
3639
3640 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3641
Brian Salomon22558932020-05-15 14:46:34 -04003642 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003643
Brian Salomonc3f49432020-06-03 10:21:50 -04003644 // 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 -04003645 // so that the uploads go to the right levels.
Brian Salomonc3f49432020-06-03 10:21:50 -04003646 if (numMipLevels && this->glCaps().mipMapLevelAndLodControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003647 auto params = backendTexture.getGLTextureParams();
3648 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3649 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003650 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003651 nonsamplerState.fBaseMipMapLevel = 0;
3652 }
3653 if (params->nonsamplerState().fMaxMipMapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003654 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003655 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3656 }
3657 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3658 }
3659
Greg Daniel16032b32020-05-06 15:31:10 -04003660 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003661 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003662 if (data->type() == BackendTextureData::Type::kPixmaps) {
3663 SkTDArray<GrMipLevel> texels;
3664 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3665 texels.append(numMipLevels);
3666 for (int i = 0; i < numMipLevels; ++i) {
3667 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3668 }
Brian Salomon22558932020-05-15 14:46:34 -04003669 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3670 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3671 info.fTarget, dstRect, colorType, texels.begin(),
3672 texels.count());
3673 } else if (data->type() == BackendTextureData::Type::kColor) {
3674 uint32_t levelMask = (1 << numMipLevels) - 1;
3675 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3676 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003677 }
3678
3679 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003680 this->bindTextureToScratchUnit(info.fTarget, 0);
3681 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003682}
3683
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003684void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003685 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3686
3687 GrGLTextureInfo info;
3688 if (tex.getGLTextureInfo(&info)) {
3689 GL_CALL(DeleteTextures(1, &info.fID));
3690 }
3691}
3692
Robert Phillips3bce1f72020-05-12 12:41:58 -04003693bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3694 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3695
3696 Stats::ProgramCacheResult stat;
3697
3698 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3699 if (!tmp) {
3700 return false;
3701 }
3702
3703 return stat != Stats::ProgramCacheResult::kHit;
3704}
3705
Robert Phillipsf0ced622019-05-16 09:06:25 -04003706#if GR_TEST_UTILS
3707
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003708bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003709 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003710
Greg Daniel52e16d92018-04-10 09:34:07 -04003711 GrGLTextureInfo info;
3712 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003713 return false;
3714 }
3715
3716 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003717 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003718
3719 return (GR_GL_TRUE == result);
3720}
3721
Brian Salomonf865b052018-03-09 09:01:53 -05003722GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003723 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003724 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3725 return GrBackendRenderTarget(); // invalid
3726 }
Brian Salomon8a375832018-03-14 10:21:40 -04003727 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003728 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003729 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003730 return {};
3731 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003732 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003733 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003734 if (sFormatIdx < 0) {
3735 return {};
3736 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003737 GrGLuint colorID = 0;
3738 GrGLuint stencilID = 0;
3739 auto deleteIDs = [&] {
3740 if (colorID) {
3741 if (useTexture) {
3742 GL_CALL(DeleteTextures(1, &colorID));
3743 } else {
3744 GL_CALL(DeleteRenderbuffers(1, &colorID));
3745 }
Brian Salomonf865b052018-03-09 09:01:53 -05003746 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003747 if (stencilID) {
3748 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003749 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003750 };
3751
3752 if (useTexture) {
3753 GL_CALL(GenTextures(1, &colorID));
3754 } else {
3755 GL_CALL(GenRenderbuffers(1, &colorID));
3756 }
3757 GL_CALL(GenRenderbuffers(1, &stencilID));
3758 if (!stencilID || !colorID) {
3759 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003760 return {};
3761 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003762
Brian Salomonf865b052018-03-09 09:01:53 -05003763 GrGLFramebufferInfo info;
3764 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003765 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003766 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3767 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003768 deleteIDs();
3769 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003770 }
3771
3772 this->invalidateBoundRenderTarget();
3773
Adrienne Walker4ee88512018-05-17 11:37:14 -07003774 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003775 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003776 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon0f396992020-06-19 19:51:21 -04003777 colorID = this->createTexture({w, h}, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
3778 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003779 if (!colorID) {
3780 deleteIDs();
3781 return {};
3782 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003783 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3784 colorID, 0));
3785 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003786 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003787 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003788 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003789 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3790 GR_GL_RENDERBUFFER, colorID));
3791 }
3792 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003793 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003794 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003795 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003796 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003797 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3798 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003799 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003800 }
3801
Brian Salomon93348dd2018-08-29 12:56:23 -04003802 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3803 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3804 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3805 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003806 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003807 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003808
Adrienne Walker4ee88512018-05-17 11:37:14 -07003809 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003810 GrGLenum status;
3811 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3812 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003813 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003814 return {};
3815 }
3816 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003817
Greg Daniel108bb232018-07-03 16:18:29 -04003818 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003819 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003820 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003821}
3822
3823void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003824 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003825 GrGLFramebufferInfo info;
3826 if (backendRT.getGLFramebufferInfo(&info)) {
3827 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003828 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003829 }
3830 }
joshualitt8fd844f2015-12-02 13:36:47 -08003831}
3832
Greg Daniel26b50a42018-03-08 09:49:58 -05003833void GrGLGpu::testingOnly_flushGpuAndSync() {
3834 GL_CALL(Finish());
3835}
Brian Salomonf865b052018-03-09 09:01:53 -05003836#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003837
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003838///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003839
cdaltone2e71c22016-04-07 18:13:29 -07003840GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003841 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003842 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003843 GrGLAttribArrayState* attribState;
3844
cdaltone2e71c22016-04-07 18:13:29 -07003845 if (gpu->glCaps().isCoreProfile()) {
3846 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003847 GrGLuint arrayID;
3848 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3849 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003850 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003851 }
cdaltone2e71c22016-04-07 18:13:29 -07003852 if (ibuf) {
3853 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003854 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003855 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003856 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003857 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003858 if (ibuf) {
3859 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003860 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003861 } else {
3862 this->setVertexArrayID(gpu, 0);
3863 }
3864 int attrCount = gpu->glCaps().maxVertexAttributes();
3865 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3866 fDefaultVertexArrayAttribState.resize(attrCount);
3867 }
3868 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003869 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003870 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003871}
bsalomone179a912016-01-20 06:18:10 -08003872
Greg Danielfe159622020-04-10 17:43:51 +00003873void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3874 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003875 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003876}
3877
Greg Daniel78be37f2020-04-14 16:40:35 -04003878void GrGLGpu::flush(FlushType flushType) {
3879 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3880 GL_CALL(Flush());
3881 fNeedsGLFlush = false;
3882 }
3883}
3884
Greg Danielfe159622020-04-10 17:43:51 +00003885bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3886 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003887 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003888 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003889 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003890 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003891 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003892 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003893 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003894 if (!this->glCaps().skipErrorChecks()) {
3895 this->clearErrorsAndCheckForOOM();
3896 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003897 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003898}
3899
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003900void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3901 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3902 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3903 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003904}
3905
Greg Daniel6be35232017-03-01 17:01:09 -05003906GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003907 if (!this->caps()->fenceSyncSupport()) {
3908 return 0;
3909 }
Greg Daniel6be35232017-03-01 17:01:09 -05003910 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003911 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3912 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3913 GrGLuint fence = 0;
3914 GL_CALL(GenFences(1, &fence));
3915 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3916 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3917 } else {
3918 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3919 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003920 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003921 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003922 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003923}
3924
Brian Salomonb0d8b762019-05-06 16:58:22 -04003925bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003926 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3927 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3928 if (!timeout) {
3929 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003930 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003931 }
3932 GrGLboolean result;
3933 GL_CALL_RET(result, TestFence(nvFence));
3934 return result == GR_GL_TRUE;
3935 }
3936 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3937 // If this really becomes necessary we could poll TestFence().
3938 // FinishFence always flushes so no need to check flush param.
3939 GL_CALL(FinishFence(nvFence));
3940 return true;
3941 } else {
3942 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3943 GrGLenum result;
3944 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3945 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3946 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003947}
3948
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003949bool GrGLGpu::waitFence(GrFence fence) {
3950 if (!this->caps()->fenceSyncSupport()) {
3951 return true;
3952 }
3953 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003954}
3955
3956void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003957 if (this->caps()->fenceSyncSupport()) {
3958 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3959 }
Greg Daniel6be35232017-03-01 17:01:09 -05003960}
3961
Greg Daniel301015c2019-11-18 14:06:46 -05003962std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003963 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003964 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003965}
3966
Greg Daniel301015c2019-11-18 14:06:46 -05003967std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3968 const GrBackendSemaphore& semaphore,
3969 GrResourceProvider::SemaphoreWrapType wrapType,
3970 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003971 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003972 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3973}
3974
Greg Daniel301015c2019-11-18 14:06:46 -05003975void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003976 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003977 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003978
Greg Daniel48661b82018-01-22 16:11:35 -05003979 GrGLsync sync;
3980 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3981 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003982 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003983}
3984
Greg Daniel301015c2019-11-18 14:06:46 -05003985void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003986 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003987 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003988
3989 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3990}
3991
Brian Salomonb0d8b762019-05-06 16:58:22 -04003992void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003993 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003994}
3995
Brian Salomon24069eb2020-06-24 10:19:52 -04003996void GrGLGpu::clearErrorsAndCheckForOOM() {
3997 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
3998}
3999
4000GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4001#if GR_GL_CHECK_ERROR
4002 if (this->glInterface()->checkAndResetOOMed()) {
4003 this->setOOMed();
4004 }
4005#endif
4006 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4007 if (error == GR_GL_OUT_OF_MEMORY) {
4008 this->setOOMed();
4009 }
4010 return error;
4011}
4012
Greg Daniel6be35232017-03-01 17:01:09 -05004013void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004014 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4015 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4016 GL_CALL(DeleteFences(1, &nvFence));
4017 } else {
4018 GL_CALL(DeleteSync(sync));
4019 }
jvanverth84741b32016-09-30 08:39:02 -07004020}
Brian Osman13dddce2017-05-09 13:19:50 -04004021
Greg Daniel301015c2019-11-18 14:06:46 -05004022std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004023 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004024 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004025 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004026 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004027 // 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 -04004028 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004029
4030 return semaphore;
4031}
Robert Phillips646e4292017-06-13 12:44:56 -04004032
4033int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon60dd8c72018-07-30 10:24:13 -04004034 switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004035 case kTexture2DSampler_GrSLType:
4036 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004037 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004038 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004039 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004040 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004041 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004042 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004043 }
4044}
Brian Osman71a18892017-08-10 10:23:25 -04004045
Kevin Lubickf4def342018-10-04 12:52:50 -04004046#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004047#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004048void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4049 // We are called by the base class, which has already called beginObject(). We choose to nest
4050 // all of our caps information in a named sub-object.
4051 writer->beginObject("GL GPU");
4052
4053 const GrGLubyte* str;
4054 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4055 writer->appendString("GL_VERSION", (const char*)(str));
4056 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4057 writer->appendString("GL_RENDERER", (const char*)(str));
4058 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4059 writer->appendString("GL_VENDOR", (const char*)(str));
4060 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4061 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4062
4063 writer->appendName("extensions");
4064 glInterface()->fExtensions.dumpJSON(writer);
4065
4066 writer->endObject();
4067}
Kevin Lubickf4def342018-10-04 12:52:50 -04004068#endif