blob: d5793daa70c7a1083d492f2b9a6b2a4df5cf39c6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Brian Salomon4cfae3b2020-07-23 10:33:24 -04008#include "src/gpu/gl/GrGLGpu.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPixmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/gpu/GrBackendSemaphore.h"
13#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrTypes.h"
16#include "include/private/SkHalf.h"
17#include "include/private/SkTemplates.h"
18#include "include/private/SkTo.h"
19#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050020#include "src/core/SkCompressedDataUtils.h"
Mike Reed13711eb2020-07-14 17:16:32 -040021#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkTraceEvent.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040023#include "src/gpu/GrBackendUtils.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040024#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040026#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040029#include "src/gpu/GrProgramInfo.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040030#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040033#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/gl/GrGLBuffer.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000045#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000046
Brian Salomond8575452020-02-25 12:13:29 -050047#define GL_ALLOC_CALL(call) \
48 [&] { \
49 if (this->glCaps().skipErrorChecks()) { \
50 GR_GL_CALL(this->glInterface(), call); \
51 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
52 } else { \
Brian Salomon24069eb2020-06-24 10:19:52 -040053 this->clearErrorsAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050054 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
Brian Salomon24069eb2020-06-24 10:19:52 -040055 return this->getErrorAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050056 } \
57 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000058
Jim Van Verth32ac83e2016-11-28 15:23:57 -050059//#define USE_NSIGHT
60
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000061///////////////////////////////////////////////////////////////////////////////
62
cdalton8917d622015-05-06 13:40:21 -070063static const GrGLenum gXfermodeEquation2Blend[] = {
64 // Basic OpenGL blend equations.
65 GR_GL_FUNC_ADD,
66 GR_GL_FUNC_SUBTRACT,
67 GR_GL_FUNC_REVERSE_SUBTRACT,
68
69 // GL_KHR_blend_equation_advanced.
70 GR_GL_SCREEN,
71 GR_GL_OVERLAY,
72 GR_GL_DARKEN,
73 GR_GL_LIGHTEN,
74 GR_GL_COLORDODGE,
75 GR_GL_COLORBURN,
76 GR_GL_HARDLIGHT,
77 GR_GL_SOFTLIGHT,
78 GR_GL_DIFFERENCE,
79 GR_GL_EXCLUSION,
80 GR_GL_MULTIPLY,
81 GR_GL_HSL_HUE,
82 GR_GL_HSL_SATURATION,
83 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050084 GR_GL_HSL_LUMINOSITY,
85
86 // Illegal... needs to map to something.
87 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070088};
Brian Salomon4dea72a2019-12-18 10:43:10 -050089static_assert(0 == kAdd_GrBlendEquation);
90static_assert(1 == kSubtract_GrBlendEquation);
91static_assert(2 == kReverseSubtract_GrBlendEquation);
92static_assert(3 == kScreen_GrBlendEquation);
93static_assert(4 == kOverlay_GrBlendEquation);
94static_assert(5 == kDarken_GrBlendEquation);
95static_assert(6 == kLighten_GrBlendEquation);
96static_assert(7 == kColorDodge_GrBlendEquation);
97static_assert(8 == kColorBurn_GrBlendEquation);
98static_assert(9 == kHardLight_GrBlendEquation);
99static_assert(10 == kSoftLight_GrBlendEquation);
100static_assert(11 == kDifference_GrBlendEquation);
101static_assert(12 == kExclusion_GrBlendEquation);
102static_assert(13 == kMultiply_GrBlendEquation);
103static_assert(14 == kHSLHue_GrBlendEquation);
104static_assert(15 == kHSLSaturation_GrBlendEquation);
105static_assert(16 == kHSLColor_GrBlendEquation);
106static_assert(17 == kHSLLuminosity_GrBlendEquation);
107static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700108
twiz@google.com0f31ca72011-03-18 17:38:11 +0000109static const GrGLenum gXfermodeCoeff2Blend[] = {
110 GR_GL_ZERO,
111 GR_GL_ONE,
112 GR_GL_SRC_COLOR,
113 GR_GL_ONE_MINUS_SRC_COLOR,
114 GR_GL_DST_COLOR,
115 GR_GL_ONE_MINUS_DST_COLOR,
116 GR_GL_SRC_ALPHA,
117 GR_GL_ONE_MINUS_SRC_ALPHA,
118 GR_GL_DST_ALPHA,
119 GR_GL_ONE_MINUS_DST_ALPHA,
120 GR_GL_CONSTANT_COLOR,
121 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000122
123 // extended blend coeffs
124 GR_GL_SRC1_COLOR,
125 GR_GL_ONE_MINUS_SRC1_COLOR,
126 GR_GL_SRC1_ALPHA,
127 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500128
129 // Illegal... needs to map to something.
130 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000131};
132
Brian Salomond978b902019-02-07 15:09:18 -0500133//////////////////////////////////////////////////////////////////////////////
134
135static int gl_target_to_binding_index(GrGLenum target) {
136 switch (target) {
137 case GR_GL_TEXTURE_2D:
138 return 0;
139 case GR_GL_TEXTURE_RECTANGLE:
140 return 1;
141 case GR_GL_TEXTURE_EXTERNAL:
142 return 2;
143 }
144 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500145}
146
147GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500148 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
149}
150
151bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
152 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500153}
154
155void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500156 int targetIndex = gl_target_to_binding_index(target);
157 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
158 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500159}
160
Brian Salomon1f05d452019-02-08 12:33:08 -0500161void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
162 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500163}
164
Brian Salomon1f05d452019-02-08 12:33:08 -0500165void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
166 for (auto& targetBinding : fTargetBindings) {
167 targetBinding.fBoundResourceID.makeInvalid();
168 if (markUnmodified) {
169 targetBinding.fHasBeenModified = false;
170 }
Brian Salomond978b902019-02-07 15:09:18 -0500171 }
172}
173
174//////////////////////////////////////////////////////////////////////////////
175
Brian Salomondc829942018-10-23 16:07:24 -0400176static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
177 switch (filter) {
178 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
Brian Salomona3b02f52020-07-15 16:02:01 -0400179 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR;
Brian Salomondc829942018-10-23 16:07:24 -0400180 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400181 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400182}
183
Brian Salomone69b9ef2020-07-22 11:18:06 -0400184static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter,
185 GrSamplerState::MipmapMode mm) {
186 switch (mm) {
187 case GrSamplerState::MipmapMode::kNone:
188 return filter_to_gl_mag_filter(filter);
189 case GrSamplerState::MipmapMode::kLinear:
190 switch (filter) {
191 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_LINEAR;
192 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_LINEAR;
193 }
194 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400195 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400196 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400197}
198
Michael Ludwigf23a1522018-12-10 11:36:13 -0500199static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
200 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400201 switch (wrapMode) {
202 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
203 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
204 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500205 case GrSamplerState::WrapMode::kClampToBorder:
206 // May not be supported but should have been caught earlier
207 SkASSERT(caps.clampToBorderSupport());
208 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500209 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400210 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400211}
212
213///////////////////////////////////////////////////////////////////////////////
214
215class GrGLGpu::SamplerObjectCache {
216public:
217 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
218 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
219 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
220 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
221 std::fill_n(fSamplers, kNumSamplers, 0);
222 }
223
224 ~SamplerObjectCache() {
225 if (!fNumTextureUnits) {
226 // We've already been abandoned.
227 return;
228 }
Chris Dalton703fe682019-05-15 12:58:12 -0600229 for (GrGLuint sampler : fSamplers) {
230 // The spec states that "zero" values should be silently ignored, however they still
231 // trigger GL errors on some NVIDIA platforms.
232 if (sampler) {
233 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
234 }
235 }
Brian Salomondc829942018-10-23 16:07:24 -0400236 }
237
Brian Salomonccb61422020-01-09 10:46:36 -0500238 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomona7d9f302020-07-15 13:25:30 -0400239 int index = state.asIndex();
Brian Salomondc829942018-10-23 16:07:24 -0400240 if (!fSamplers[index]) {
241 GrGLuint s;
242 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
243 if (!s) {
244 return;
245 }
246 fSamplers[index] = s;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400247 GrGLenum minFilter = filter_to_gl_min_filter(state.filter(), state.mipmapMode());
248 GrGLenum magFilter = filter_to_gl_mag_filter(state.filter());
249 GrGLenum wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
250 GrGLenum wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400251 GR_GL_CALL(fGpu->glInterface(),
252 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
253 GR_GL_CALL(fGpu->glInterface(),
254 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
255 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
256 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
257 }
258 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
259 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
260 fHWBoundSamplers[unitIdx] = fSamplers[index];
261 }
262 }
263
264 void invalidateBindings() {
265 // When we have sampler support we always use samplers. So setting these to zero will cause
266 // a rebind on next usage.
267 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
268 }
269
270 void abandon() {
271 fHWBoundSamplers.reset();
272 fNumTextureUnits = 0;
273 }
274
275 void release() {
276 if (!fNumTextureUnits) {
277 // We've already been abandoned.
278 return;
279 }
280 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
281 std::fill_n(fSamplers, kNumSamplers, 0);
282 // Deleting a bound sampler implicitly binds sampler 0.
283 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
284 }
285
286private:
Brian Salomona7d9f302020-07-15 13:25:30 -0400287 static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
Brian Salomondc829942018-10-23 16:07:24 -0400288 GrGLGpu* fGpu;
Brian Salomondc829942018-10-23 16:07:24 -0400289 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
290 GrGLuint fSamplers[kNumSamplers];
291 int fNumTextureUnits;
292};
293
reed@google.comac10a2d2010-12-22 21:39:39 +0000294///////////////////////////////////////////////////////////////////////////////
295
Brian Salomon384fab42017-12-07 12:33:05 -0500296sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
Adlai Holler3d0359a2020-07-09 15:35:55 -0400297 GrDirectContext* direct) {
Brian Salomon384fab42017-12-07 12:33:05 -0500298 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500299 interface = GrGLMakeNativeInterface();
300 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
301 // to GrGLMakeNativeInterface.
302 if (!interface) {
303 interface = sk_ref_sp(GrGLCreateNativeInterface());
304 }
Brian Salomon384fab42017-12-07 12:33:05 -0500305 if (!interface) {
306 return nullptr;
307 }
bsalomon424cc262015-05-22 10:37:30 -0700308 }
Jim Van Verth76334772017-05-05 16:46:05 -0400309#ifdef USE_NSIGHT
310 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
311#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500312 auto glContext = GrGLContext::Make(std::move(interface), options);
313 if (!glContext) {
314 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700315 }
Adlai Holler3d0359a2020-07-09 15:35:55 -0400316 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
bsalomon424cc262015-05-22 10:37:30 -0700317}
318
Adlai Holler3d0359a2020-07-09 15:35:55 -0400319GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* direct)
320 : GrGpu(direct)
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500321 , fGLContext(std::move(ctx))
322 , fProgramCache(new ProgramCache(this))
323 , fHWProgramID(0)
324 , fTempSrcFBOID(0)
325 , fTempDstFBOID(0)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400326 , fStencilClearFBOID(0)
327 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500328 SkASSERT(fGLContext);
Brian Salomon24069eb2020-06-24 10:19:52 -0400329 // Clear errors so we don't get confused whether we caused an error.
330 this->clearErrorsAndCheckForOOM();
331 // Toss out any pre-existing OOM that was hanging around before we got started.
332 this->checkAndResetOOMed();
333
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500334 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000335
Brian Salomond978b902019-02-07 15:09:18 -0500336 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000337
Brian Salomonae64c192019-02-05 09:41:37 -0500338 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
339 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600340 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500341 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500342 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
343 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
344 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
345 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700346 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500347 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
348 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700349 }
Brian Salomonae64c192019-02-05 09:41:37 -0500350 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400351 fHWBufferState[i].invalidate();
352 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600353 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700354
355 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
356 fPathRendering.reset(new GrGLPathRendering(this));
357 }
358
Brian Salomondc829942018-10-23 16:07:24 -0400359 if (this->glCaps().samplerObjectSupport()) {
360 fSamplerObjectCache.reset(new SamplerObjectCache(this));
361 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000362}
363
bsalomon861e1032014-12-16 07:33:49 -0800364GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700365 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
366 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800367 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700368 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700369 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800370
Chris Dalton20e7a532020-02-28 15:37:53 +0000371 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400372 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000373 // detach the current program so there is no confusion on OpenGL's part
374 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000375 GL_CALL(UseProgram(0));
376 }
377
Brian Salomon43f8bf02017-10-18 08:33:29 -0400378 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700379 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800380 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400381 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700382 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800383 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400384 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700385 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800386 }
egdaniel0f5f9672015-02-03 11:10:51 -0800387
bsalomon7ea33f52015-11-22 14:51:00 -0800388 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
389 if (0 != fCopyPrograms[i].fProgram) {
390 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
391 }
bsalomon6df86402015-06-01 10:41:49 -0700392 }
bsalomon6dea83f2015-12-03 12:58:06 -0800393
brianosman33f6b3f2016-06-02 05:49:21 -0700394 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
395 if (0 != fMipmapPrograms[i].fProgram) {
396 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
397 }
398 }
399
Brian Salomondc829942018-10-23 16:07:24 -0400400 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500401
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400402 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700403}
404
bsalomon6e2aad42016-04-01 11:54:31 -0700405void GrGLGpu::disconnect(DisconnectType type) {
406 INHERITED::disconnect(type);
407 if (DisconnectType::kCleanup == type) {
408 if (fHWProgramID) {
409 GL_CALL(UseProgram(0));
410 }
411 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700412 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700413 }
414 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700415 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700416 }
417 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700418 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700419 }
bsalomon6e2aad42016-04-01 11:54:31 -0700420 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
421 if (fCopyPrograms[i].fProgram) {
422 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
423 }
424 }
brianosman33f6b3f2016-06-02 05:49:21 -0700425 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
426 if (fMipmapPrograms[i].fProgram) {
427 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
428 }
429 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400430
Brian Salomondc829942018-10-23 16:07:24 -0400431 if (fSamplerObjectCache) {
432 fSamplerObjectCache->release();
433 }
bsalomon6e2aad42016-04-01 11:54:31 -0700434 } else {
435 if (fProgramCache) {
436 fProgramCache->abandon();
437 }
Brian Salomondc829942018-10-23 16:07:24 -0400438 if (fSamplerObjectCache) {
439 fSamplerObjectCache->abandon();
440 }
bsalomon6e2aad42016-04-01 11:54:31 -0700441 }
442
Chris Dalton20e7a532020-02-28 15:37:53 +0000443 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500444 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700445
bsalomonc8dc1f72014-08-21 13:02:13 -0700446 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700447 fTempSrcFBOID = 0;
448 fTempDstFBOID = 0;
449 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700450 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800451 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
452 fCopyPrograms[i].fProgram = 0;
453 }
brianosman33f6b3f2016-06-02 05:49:21 -0700454 fMipmapProgramArrayBuffer.reset();
455 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
456 fMipmapPrograms[i].fProgram = 0;
457 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400458
jvanverthe9c0fc62015-04-29 11:18:05 -0700459 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700460 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700461 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400462 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000463}
464
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000465///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000466
bsalomon861e1032014-12-16 07:33:49 -0800467void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000468 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400469 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000470 GL_CALL(Disable(GR_GL_DEPTH_TEST));
471 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000472
Brian Salomonf0861672017-05-08 11:10:10 -0400473 // We don't use face culling.
474 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600475 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
476 // just set this to the default for self-consistency.
477 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400478
Brian Salomonae64c192019-02-05 09:41:37 -0500479 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
480 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700481
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400482 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500483#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000484 // Desktop-only state that we never change
485 if (!this->glCaps().isCoreProfile()) {
486 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
487 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
488 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
489 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
490 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
491 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
492 }
493 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
494 // core profile. This seems like a bug since the core spec removes any mention of
495 // GL_ARB_imaging.
496 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
497 GL_CALL(Disable(GR_GL_COLOR_TABLE));
498 }
499 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400500
Chris Dalton1215cda2019-12-17 21:44:04 -0700501 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500502#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000503 // Since ES doesn't support glPointSize at all we always use the VS to
504 // set the point size
505 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
506
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000507 }
joshualitt58162332014-08-01 06:44:53 -0700508
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400509 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500510 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700511 // The arm extension requires specifically enabling MSAA fetching per sample.
512 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500513 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700514 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000515 fHWWriteToColor = kUnknown_TriState;
516 // we only ever use lines in hairline mode
517 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700518 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500519
520 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000521 }
edisonn@google.comba669992013-06-28 16:03:21 +0000522
egdanielb414f252014-07-29 13:15:47 -0700523 if (resetBits & kMSAAEnable_GrGLBackendState) {
524 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700525
Chris Dalton6ce447a2019-06-23 18:07:38 -0600526 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800527 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
528 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700529 GL_CALL(CoverageModulation(GR_GL_RGBA));
530 }
Chris Daltonce425af2019-12-16 10:39:03 -0700531
532 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000533 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000535 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400536 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000537
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000538 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500539 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500540 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000541 }
Brian Salomondc829942018-10-23 16:07:24 -0400542 if (fSamplerObjectCache) {
543 fSamplerObjectCache->invalidateBindings();
544 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000545 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000546
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000547 if (resetBits & kBlend_GrGLBackendState) {
548 fHWBlendState.invalidate();
549 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000550
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000551 if (resetBits & kView_GrGLBackendState) {
552 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700553 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 fHWViewport.invalidate();
555 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000556
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000557 if (resetBits & kStencil_GrGLBackendState) {
558 fHWStencilSettings.invalidate();
559 fHWStencilTestEnabled = kUnknown_TriState;
560 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000561
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000562 // Vertex
563 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700564 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500565 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
566 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600567 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700568 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000571 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500572 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700573 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500574 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000575 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000576
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000577 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700578 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700579 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000580 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000581 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000582
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000583 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000584 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400585 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000586 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
587 }
Brian Salomon1047a492019-07-02 12:25:21 -0400588 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000589 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
590 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (this->glCaps().packFlipYSupport()) {
592 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
593 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000594 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000595
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 if (resetBits & kProgram_GrGLBackendState) {
597 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000598 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000599 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400600 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000601}
602
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400603static bool check_backend_texture(const GrBackendTexture& backendTex,
604 const GrGLCaps& caps,
605 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400606 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400607 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400608 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400609 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800610 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000611
Brian Salomonea4ad302019-08-07 13:04:55 -0400612 desc->fSize = {backendTex.width(), backendTex.height()};
613 desc->fTarget = info.fTarget;
614 desc->fID = info.fID;
615 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800616
Brian Salomonea4ad302019-08-07 13:04:55 -0400617 if (desc->fFormat == GrGLFormat::kUnknown) {
618 return false;
619 }
620 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400621 if (!caps.shaderCaps()->externalTextureSupport()) {
622 return false;
623 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400624 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
625 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400626 return false;
627 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400628 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400629 return false;
630 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400631 if (backendTex.isProtected()) {
632 // Not supported in GL backend at this time.
633 return false;
634 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400635
Brian Salomond17f6582017-07-19 18:28:58 -0400636 return true;
637}
638
639sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400640 GrWrapOwnership ownership,
641 GrWrapCacheable cacheable,
642 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400643 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400644 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400645 return nullptr;
646 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400647
Brian Salomond17f6582017-07-19 18:28:58 -0400648 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400649 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400650 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400651 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400652 }
bsalomone5286e02016-01-14 09:24:09 -0800653
Brian Salomon40a40622020-07-21 10:32:07 -0400654 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400655 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400656
Brian Salomona6db5102020-07-21 09:56:23 -0400657 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400658 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400659 // We don't know what parameters are already set on wrapped textures.
660 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600661 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400662}
663
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500664static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
665 const GrGLCaps& caps, GrGLTexture::Desc* desc,
666 bool skipRectTexSupportCheck = false) {
667 GrGLTextureInfo info;
668 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
669 return false;
670 }
671
672 desc->fSize = {backendTex.width(), backendTex.height()};
673 desc->fTarget = info.fTarget;
674 desc->fID = info.fID;
675 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
676
677 if (desc->fFormat == GrGLFormat::kUnknown) {
678 return false;
679 }
680
681 if (GR_GL_TEXTURE_2D != desc->fTarget) {
682 return false;
683 }
684 if (backendTex.isProtected()) {
685 // Not supported in GL backend at this time.
686 return false;
687 }
688
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500689 return true;
690}
691
Robert Phillipsb915c942019-12-17 14:44:37 -0500692sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
693 GrWrapOwnership ownership,
694 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500695 GrGLTexture::Desc desc;
696 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
697 return nullptr;
698 }
699
700 if (kBorrow_GrWrapOwnership == ownership) {
701 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
702 } else {
703 desc.fOwnership = GrBackendObjectOwnership::kOwned;
704 }
705
Brian Salomon40a40622020-07-21 10:32:07 -0400706 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400707 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500708
Brian Salomona6db5102020-07-21 09:56:23 -0400709 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500710 backendTex.getGLTextureParams(), cacheable,
711 kRead_GrIOType);
712 // We don't know what parameters are already set on wrapped textures.
713 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600714 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500715}
716
Brian Salomond17f6582017-07-19 18:28:58 -0400717sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400718 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500719 GrWrapOwnership ownership,
720 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400721 const GrGLCaps& caps = this->glCaps();
722
Brian Salomonea4ad302019-08-07 13:04:55 -0400723 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400724 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800725 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800726 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400727 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
728 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800729
Brian Salomond17f6582017-07-19 18:28:58 -0400730 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400731 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800732 return nullptr;
733 }
bsalomon10528f12015-10-14 12:54:52 -0700734
Brian Osman766fcbb2017-03-13 09:33:09 -0400735 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400736 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400737 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400738 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800739 }
bsalomonb15b4c12014-10-29 12:41:57 -0700740
Robert Phillips0902c982019-07-16 07:47:56 -0400741
Greg Daniel6fa62e22019-08-07 15:52:37 -0400742 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
743 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000744
Brian Salomonea4ad302019-08-07 13:04:55 -0400745 GrGLRenderTarget::IDs rtIDs;
746 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400747 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000748 }
Greg Daniel177e6952017-10-12 12:27:11 -0400749
Brian Salomon40a40622020-07-21 10:32:07 -0400750 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400751 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400752
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500753 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400754 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400755 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400756 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400757 // We don't know what parameters are already set on wrapped textures.
758 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600759 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000760}
761
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400762sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400763 GrGLFramebufferInfo info;
764 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000765 return nullptr;
766 }
767
Brian Salomone8a766b2019-07-19 14:24:36 -0400768 if (backendRT.isProtected()) {
769 // Not supported in GL at this time.
770 return nullptr;
771 }
772
Brian Salomond4764a12019-08-08 12:08:24 -0400773 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400774 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
775 return nullptr;
776 }
777
Brian Salomonea4ad302019-08-07 13:04:55 -0400778 GrGLRenderTarget::IDs rtIDs;
779 rtIDs.fRTFBOID = info.fFBOID;
780 rtIDs.fMSColorRenderbufferID = 0;
781 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
782 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000783
Greg Daniel6fa62e22019-08-07 15:52:37 -0400784 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700785
Brian Salomona56a7462020-02-07 14:17:25 -0500786 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400787 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000788}
789
Greg Daniel7ef28f32017-04-20 16:41:55 +0000790sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400791 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400792 GrGLTexture::Desc desc;
793 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
794 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400795 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800796 return nullptr;
797 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400798
799 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
800 return nullptr;
801 }
802
803 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400804 GrGLRenderTarget::IDs rtIDs;
805 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800806 return nullptr;
807 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500808 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800809}
810
Brian Salomonc320b152018-02-20 14:05:36 -0500811static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700812 if (!glTex) {
813 return false;
814 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000815
bsalomone5286e02016-01-14 09:24:09 -0800816 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
817 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800818 return false;
819 }
820
jvanverth17aa0472016-01-05 10:41:27 -0800821 return true;
822}
823
Brian Salomona9b04b92018-06-01 15:04:28 -0400824bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400825 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400826 const GrMipLevel texels[], int mipLevelCount,
827 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500828 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800829
Brian Salomonc320b152018-02-20 14:05:36 -0500830 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800831 return false;
832 }
833
Brian Salomond978b902019-02-07 15:09:18 -0500834 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000835
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400836 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400837 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
838 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
839 glTex->target(), dstRect, srcColorType, texels,
840 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000841}
842
Brian Salomone05ba5a2019-04-08 11:59:07 -0400843bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400844 GrColorType textureColorType, GrColorType bufferColorType,
845 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400846 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400847
Jim Van Verth1676cb92019-01-15 13:24:45 -0500848 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400849 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500850
Brian Salomonc320b152018-02-20 14:05:36 -0500851 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400852 return false;
853 }
854
Hal Canaryc36f7e72018-06-18 15:50:09 -0400855 static_assert(sizeof(int) == sizeof(int32_t), "");
856 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400857 return false;
858 }
859
Brian Salomond978b902019-02-07 15:09:18 -0500860 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400861
862 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500863 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400864 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500865 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400866
Greg Daniel660cc992017-06-26 14:55:05 -0400867 SkDEBUGCODE(
868 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
869 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
870 SkASSERT(bounds.contains(subRect));
871 )
872
Brian Salomonb28cb682019-07-26 12:48:47 -0400873 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400874 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400875 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700876 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400877 return false;
878 }
879
880 bool restoreGLRowLength = false;
881 if (trimRowBytes != rowBytes) {
882 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400883 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400884 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
885 restoreGLRowLength = true;
886 }
887
Greg Danielba88ab62019-07-26 09:14:01 -0400888 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400889 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400890 GrGLenum externalFormat = 0;
891 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400892 this->glCaps().getTexSubImageExternalFormatAndType(
893 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400894 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400895 return false;
896 }
897
Brian Salomon8cbf6622019-07-30 11:03:14 -0400898 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400899 GL_CALL(TexSubImage2D(glTex->target(),
900 0,
901 left, top,
902 width,
903 height,
904 externalFormat, externalType,
905 pixels));
906
907 if (restoreGLRowLength) {
908 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
909 }
910
911 return true;
912}
913
Brian Salomon26de56e2019-04-10 12:14:26 -0400914bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400915 GrColorType surfaceColorType, GrColorType dstColorType,
916 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400917 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
918 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400919 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400920 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
921 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400922}
923
Brian Osman9b560242017-09-05 15:34:52 -0400924void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500925 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
926 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
927 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
928 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400929 }
Brian Osman9b560242017-09-05 15:34:52 -0400930}
931
Brian Salomon22558932020-05-15 14:46:34 -0400932bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
933 GrColorType textureColorType,
934 SkISize texDims,
935 GrGLenum target,
936 SkIRect dstRect,
937 GrColorType srcColorType,
938 const GrMipLevel texels[],
939 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500940 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400941 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500942
Greg Daniel7bfc9132019-08-14 14:23:53 -0400943 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800944
Brian Salomonf77c1462019-08-01 15:19:29 -0400945 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800946
bsalomon5b30c6f2015-12-17 14:17:34 -0800947 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800948 GrGLenum externalFormat;
949 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400950 this->glCaps().getTexSubImageExternalFormatAndType(
951 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400952 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800953 return false;
954 }
Brian Salomon22558932020-05-15 14:46:34 -0400955 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
956 mipLevelCount);
957 return true;
958}
Brian Salomonf77c1462019-08-01 15:19:29 -0400959
Brian Salomon22558932020-05-15 14:46:34 -0400960bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
961 SkISize texDims,
962 GrGLenum target,
963 SkColor4f color,
964 uint32_t levelMask) {
965 GrColorType colorType;
966 GrGLenum externalFormat, externalType;
967 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
968 &externalType, &colorType);
969 if (colorType == GrColorType::kUnknown) {
970 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000971 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400972
Brian Salomon22558932020-05-15 14:46:34 -0400973 std::unique_ptr<char[]> pixelStorage;
974 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400975 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400976 SkSTArray<16, GrMipLevel> levels;
977 levels.resize(numLevels);
978 SkISize levelDims = texDims;
979 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
980 std::max(levelDims.height() >> 1, 1)}) {
981 if (levelMask & (1 << i)) {
982 if (!pixelStorage) {
983 // Make one tight image at the first size and reuse it for smaller levels.
984 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
985 size_t rb = ii.minRowBytes();
986 pixelStorage.reset(new char[rb * levelDims.height()]);
987 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
988 return false;
989 }
990 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000991 }
Brian Salomon22558932020-05-15 14:46:34 -0400992 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
993 }
994 }
995 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
996 bpp, levels.begin(), levels.count());
997 return true;
998}
999
1000void GrGLGpu::uploadTexData(SkISize texDims,
1001 GrGLenum target,
1002 SkIRect dstRect,
1003 GrGLenum externalFormat,
1004 GrGLenum externalType,
1005 size_t bpp,
1006 const GrMipLevel texels[],
1007 int mipLevelCount) {
1008 SkASSERT(!texDims.isEmpty());
1009 SkASSERT(!dstRect.isEmpty());
1010 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001011 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001012 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1013
1014 const GrGLCaps& caps = this->glCaps();
1015
1016 bool restoreGLRowLength = false;
1017
1018 this->unbindCpuToGpuXferBuffer();
1019 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1020
1021 SkISize dims = dstRect.size();
1022 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1023 std::max(dims.height() >> 1, 1)}) {
1024 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001025 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001026 }
Brian Salomon22558932020-05-15 14:46:34 -04001027 const size_t trimRowBytes = dims.width() * bpp;
1028 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001029
1030 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1031 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001032 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001033 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001034 } else {
1035 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001036 }
1037
Brian Salomon22558932020-05-15 14:46:34 -04001038 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1039 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001040 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001041 if (restoreGLRowLength) {
1042 SkASSERT(caps.writePixelsRowBytesSupport());
1043 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1044 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001045}
1046
Greg Daniel01f278c2020-06-12 16:58:17 -04001047bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1048 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001049 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001050 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001051 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001052 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001053 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001054 const GrGLCaps& caps = this->glCaps();
1055
1056 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001057 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001058 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001059 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001060 }
1061
Robert Phillips9f744f72019-12-19 19:14:33 -05001062 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1063
Greg Daniel7bfc9132019-08-14 14:23:53 -04001064 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001065
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001066 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001067 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001068 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001069 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001070
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001071 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001072 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001073
1074 if (useTexStorage) {
1075 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001076 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1077 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001078 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001079 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080 }
Robert Phillips42716d42019-12-16 12:19:54 -05001081
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001082 size_t offset = 0;
1083 for (int level = 0; level < numMipLevels; ++level) {
1084
Robert Phillips99dead92020-01-27 16:11:57 -05001085 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1086 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001087
Brian Salomond8575452020-02-25 12:13:29 -05001088 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1089 level,
1090 0, // left
1091 0, // top
1092 dimensions.width(),
1093 dimensions.height(),
1094 internalFormat,
1095 SkToInt(levelDataSize),
1096 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001097
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001098 if (error != GR_GL_NO_ERROR) {
1099 return false;
1100 }
1101
Robert Phillips9f744f72019-12-19 19:14:33 -05001102 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001103 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001104 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001105 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001106 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001107
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001108 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001109 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1110 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001111
1112 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001113 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1114 level,
1115 internalFormat,
1116 dimensions.width(),
1117 dimensions.height(),
1118 0, // border
1119 SkToInt(levelDataSize),
1120 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001121
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001122 if (error != GR_GL_NO_ERROR) {
1123 return false;
1124 }
1125
Robert Phillips9f744f72019-12-19 19:14:33 -05001126 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001127 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001128 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001129 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001130 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001131}
1132
Brian Salomond8575452020-02-25 12:13:29 -05001133bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001134 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001135 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001136 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001137 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001138 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001139 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1140 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001141 break;
1142 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001143 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1144 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001145 break;
1146 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1147 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001148 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1149 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001150 break;
1151 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001152 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001153 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001154 }
Brian Salomond8575452020-02-25 12:13:29 -05001155 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001156}
1157
Brian Salomonea4ad302019-08-07 13:04:55 -04001158bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001159 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001160 GrGLRenderTarget::IDs* rtIDs) {
1161 rtIDs->fMSColorRenderbufferID = 0;
1162 rtIDs->fRTFBOID = 0;
1163 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1164 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001165
bsalomona11e5fc2015-12-18 07:59:41 -08001166 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001167
Brian Salomonea4ad302019-08-07 13:04:55 -04001168 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001169 goto FAILED;
1170 }
1171
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001172 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001173 goto FAILED;
1174 }
1175
Brian Salomonea4ad302019-08-07 13:04:55 -04001176 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1177 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001178 goto FAILED;
1179 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001180
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001181 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1182 // the texture bound to the other. The exception is the IMG multisample extension. With this
1183 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1184 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001185 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001186 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1187 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1188 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001189 goto FAILED;
1190 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001191 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001192 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001194 }
1195
egdanield803f272015-03-18 13:01:52 -07001196 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001197 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001199 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001201 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1202 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001203 goto FAILED;
1204 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001206 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001207 GR_GL_COLOR_ATTACHMENT0,
1208 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001209 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001211 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001212
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001213 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001214 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1215 GR_GL_COLOR_ATTACHMENT0,
1216 desc.fTarget,
1217 desc.fID,
1218 0,
1219 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001220 } else {
egdanield803f272015-03-18 13:01:52 -07001221 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001222 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001223 desc.fTarget,
1224 desc.fID,
1225 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001226 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001227
1228 return true;
1229
1230FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001231 if (rtIDs->fMSColorRenderbufferID) {
1232 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001233 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001234 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1235 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001236 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001237 if (rtIDs->fTexFBOID) {
1238 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001239 }
1240 return false;
1241}
1242
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001243// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001244static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001245// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001246 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001247}
1248
Brian Salomone2826ab2019-06-04 15:58:31 -04001249static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001250 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001251 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1252 // drivers have a bug where an FBO won't be complete if it includes a
1253 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001254 GrGLTextureParameters::SamplerOverriddenState state;
1255 state.fMinFilter = GR_GL_NEAREST;
1256 state.fMagFilter = GR_GL_NEAREST;
1257 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1258 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001259 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1260 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1261 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1262 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001263 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001264}
1265
Brian Salomona56a7462020-02-07 14:17:25 -05001266sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001267 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001268 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001269 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001270 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001271 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001272 int mipLevelCount,
1273 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001274 // We don't support protected textures in GL.
1275 if (isProtected == GrProtected::kYes) {
1276 return nullptr;
1277 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001278 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001279
Brian Salomond2a8ae22019-09-10 16:03:59 -04001280 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001281 GrMipmapStatus mipmapStatus =
1282 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001283 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001284 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001285 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001286 switch (format.textureType()) {
1287 case GrTextureType::kExternal:
1288 case GrTextureType::kNone:
1289 return nullptr;
1290 case GrTextureType::k2D:
1291 texDesc.fTarget = GR_GL_TEXTURE_2D;
1292 break;
1293 case GrTextureType::kRectangle:
1294 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1295 return nullptr;
1296 }
1297 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1298 break;
1299 }
Brian Salomon81536f22019-08-08 16:30:49 -04001300 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001301 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001302 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1303 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001304
Brian Salomon0f396992020-06-19 19:51:21 -04001305 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1306 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001307
1308 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001309 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001310 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001311
Robert Phillips67d52cf2017-06-05 13:38:13 -04001312 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001313 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001314 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001315 GL_CALL(BindTexture(texDesc.fTarget, 0));
1316 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001317
Brian Salomonea4ad302019-08-07 13:04:55 -04001318 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1319 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 return return_null_texture();
1321 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001322 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001323 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001324 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001325 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001326 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001328 // The non-sampler params are still at their default values.
1329 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1330 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001331 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001332 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001333 GrGLenum externalFormat, externalType;
1334 GrColorType colorType;
1335 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1336 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001337 for (int i = 0; i < mipLevelCount; ++i) {
1338 if (levelClearMask & (1U << i)) {
1339 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1340 nullptr));
1341 }
1342 }
1343 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1344 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001345 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001346 this->disableWindowRectangles();
1347 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001348 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001349 for (int i = 0; i < mipLevelCount; ++i) {
1350 if (levelClearMask & (1U << i)) {
1351 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1352 kDst_TempFBOTarget);
1353 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1354 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1355 }
1356 }
Brian Salomonc2249852019-09-16 11:08:50 -04001357 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001358 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001359 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001360 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001361 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001362 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001363 }
1364 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001365 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001366}
1367
Robert Phillips9f744f72019-12-19 19:14:33 -05001368sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001369 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001370 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001371 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001372 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001373 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001374 // We don't support protected textures in GL.
1375 if (isProtected == GrProtected::kYes) {
1376 return nullptr;
1377 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001378 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1379
Brian Salomonbb8dde82019-06-27 10:52:13 -04001380 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001381 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001382 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001383 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001384 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001385 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001386 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001387 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001388 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001389 return nullptr;
1390 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001391
Greg Danielaaf738c2020-07-10 09:30:33 -04001392 if (data) {
1393 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1394 GR_GL_TEXTURE_2D, data, dataSize)) {
1395 GL_CALL(DeleteTextures(1, &desc.fID));
1396 return nullptr;
1397 }
1398 }
1399
Robert Phillipsee946932019-12-18 11:16:17 -05001400 // Unbind this texture from the scratch texture unit.
1401 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1402
Brian Salomona6db5102020-07-21 09:56:23 -04001403 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1404 ? GrMipmapStatus::kValid
1405 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001406
Brian Salomona6db5102020-07-21 09:56:23 -04001407 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001408 // The non-sampler params are still at their default values.
1409 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1410 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001411 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001412}
1413
Greg Danielc1ad77c2020-05-06 11:40:03 -04001414GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001415 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001416 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001417 // We don't support protected textures in GL.
1418 if (isProtected == GrProtected::kYes) {
1419 return {};
1420 }
1421
1422 this->handleDirtyContext();
1423
1424 GrGLFormat glFormat = format.asGLFormat();
1425 if (glFormat == GrGLFormat::kUnknown) {
1426 return {};
1427 }
1428
Greg Daniel01f278c2020-06-12 16:58:17 -04001429 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1430
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001431 GrGLTextureInfo info;
1432 GrGLTextureParameters::SamplerOverriddenState initialState;
1433
1434 info.fTarget = GR_GL_TEXTURE_2D;
1435 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001436 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001437 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001438 if (!info.fID) {
1439 return {};
1440 }
1441
1442 // Unbind this texture from the scratch texture unit.
1443 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1444
1445 auto parameters = sk_make_sp<GrGLTextureParameters>();
1446 // The non-sampler params are still at their default values.
1447 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1448 fResetTimestampForTextureParameters);
1449
1450 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1451 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001452}
1453
Greg Danielaaf738c2020-07-10 09:30:33 -04001454bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1455 sk_sp<GrRefCntedCallback> finishedCallback,
1456 const BackendTextureData* data) {
1457 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1458
1459 GrGLTextureInfo info;
1460 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1461
1462 GrBackendFormat format = backendTexture.getBackendFormat();
1463 GrGLFormat glFormat = format.asGLFormat();
1464 if (glFormat == GrGLFormat::kUnknown) {
1465 return false;
1466 }
1467 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1468
Brian Salomon40a40622020-07-21 10:32:07 -04001469 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001470
1471 const char* rawData = nullptr;
1472 size_t rawDataSize = 0;
1473 SkAutoMalloc am;
1474 if (data->type() == BackendTextureData::Type::kCompressed) {
1475 rawData = (const char*)data->compressedData();
1476 rawDataSize = data->compressedSize();
1477 } else {
1478 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1479 SkASSERT(compression != SkImage::CompressionType::kNone);
1480
1481 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001482 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001483
1484 am.reset(rawDataSize);
1485
1486 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1487 data->color());
1488
1489 rawData = (const char*)am.get();
1490 }
1491
1492 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001493
1494 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1495 // so that the uploads go to the right levels.
1496 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelAndLodControlSupport()) {
1497 auto params = backendTexture.getGLTextureParams();
1498 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1499 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1500 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1501 nonsamplerState.fBaseMipMapLevel = 0;
1502 }
1503 int numMipLevels =
1504 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1505 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1506 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1507 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1508 }
1509 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1510 }
1511
Greg Danielaaf738c2020-07-10 09:30:33 -04001512 bool result = this->uploadCompressedTexData(
1513 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1514 rawData, rawDataSize);
1515
1516 // Unbind this texture from the scratch texture unit.
1517 this->bindTextureToScratchUnit(info.fTarget, 0);
1518
1519 return result;
1520}
1521
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001522namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001523
egdaniel8dc7c3a2015-04-16 11:22:42 -07001524const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001525
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001526void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001527 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001528
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001529 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001530 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001531 (kUnknownBitCount == format->fTotalBits));
1532 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001533 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001534 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1535 (GrGLint*)&format->fStencilBits);
1536 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001537 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001538 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1539 (GrGLint*)&format->fTotalBits);
1540 format->fTotalBits += format->fStencilBits;
1541 } else {
1542 format->fTotalBits = format->fStencilBits;
1543 }
1544 }
1545}
1546}
1547
Brian Salomon5043f1f2019-07-11 21:27:54 -04001548int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001549 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001550 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1551
1552 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001553 // Default to unsupported, set this if we find a stencil format that works.
1554 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001555
Brian Salomon0f396992020-06-19 19:51:21 -04001556 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1557 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001558 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001559 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001560 }
egdanielff1d5472015-09-10 08:37:20 -07001561 // unbind the texture from the texture unit before binding it to the frame buffer
1562 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1563
1564 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001565 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001566 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001567 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001568 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001569 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1570 GR_GL_COLOR_ATTACHMENT0,
1571 GR_GL_TEXTURE_2D,
1572 colorID,
1573 0));
bsalomon30447372015-12-21 09:03:05 -08001574 GrGLuint sbRBID = 0;
1575 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001576
1577 // look over formats till I find a compatible one
1578 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001579 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001580 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001581 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1582 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001583 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1584 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1585 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001586 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001587 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001588 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001589 if (sFmt.fPacked) {
1590 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1591 GR_GL_DEPTH_ATTACHMENT,
1592 GR_GL_RENDERBUFFER, sbRBID));
1593 } else {
1594 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1595 GR_GL_DEPTH_ATTACHMENT,
1596 GR_GL_RENDERBUFFER, 0));
1597 }
1598 GrGLenum status;
1599 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1600 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1601 firstWorkingStencilFormatIndex = i;
1602 break;
1603 }
egdanielff1d5472015-09-10 08:37:20 -07001604 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1605 GR_GL_STENCIL_ATTACHMENT,
1606 GR_GL_RENDERBUFFER, 0));
1607 if (sFmt.fPacked) {
1608 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1609 GR_GL_DEPTH_ATTACHMENT,
1610 GR_GL_RENDERBUFFER, 0));
1611 }
egdanielff1d5472015-09-10 08:37:20 -07001612 }
1613 }
bsalomon30447372015-12-21 09:03:05 -08001614 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001615 }
1616 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001617 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1618 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001619 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001620 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001621 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001622}
1623
Brian Salomonea4ad302019-08-07 13:04:55 -04001624GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001625 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001626 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001627 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001628 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001629 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001630 if (format == GrGLFormat::kUnknown) {
1631 return 0;
1632 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001633 GrGLuint id = 0;
1634 GL_CALL(GenTextures(1, &id));
1635 if (!id) {
1636 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001637 }
1638
Brian Salomonea4ad302019-08-07 13:04:55 -04001639 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001640
Brian Salomonea4ad302019-08-07 13:04:55 -04001641 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1642
Brian Salomonea4ad302019-08-07 13:04:55 -04001643 return id;
1644}
1645
Brian Salomon0f396992020-06-19 19:51:21 -04001646GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1647 GrGLFormat format,
1648 GrGLenum target,
1649 GrRenderable renderable,
1650 GrGLTextureParameters::SamplerOverriddenState* initialState,
1651 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001652 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001653 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001654
Brian Salomonea4ad302019-08-07 13:04:55 -04001655 GrGLuint id = 0;
1656 GL_CALL(GenTextures(1, &id));
1657
1658 if (!id) {
1659 return 0;
1660 }
1661
Brian Salomon0f396992020-06-19 19:51:21 -04001662 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001663
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001664 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001665 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001666 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001667 }
1668
Brian Salomond2a8ae22019-09-10 16:03:59 -04001669 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001670 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001671 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001672 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001673 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001674
1675 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1676
1677 bool success = false;
1678 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001679 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001680 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001681 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1682 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001683 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001684 } else {
1685 GrGLenum externalFormat, externalType;
1686 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1687 GrGLenum error = GR_GL_NO_ERROR;
1688 if (externalFormat && externalType) {
1689 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1690 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001691 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1692 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001693 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1694 currentHeight, 0, externalFormat, externalType,
1695 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001696 }
Brian Salomond8575452020-02-25 12:13:29 -05001697 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001698 }
1699 }
1700 }
1701 if (success) {
1702 return id;
1703 }
1704 GL_CALL(DeleteTextures(1, &id));
1705 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001706}
1707
Chris Daltoneffee202019-07-01 22:28:03 -06001708GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1709 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001710 SkASSERT(width >= rt->width());
1711 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001712
egdaniel8dc7c3a2015-04-16 11:22:42 -07001713 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001714
Brian Salomond4764a12019-08-08 12:08:24 -04001715 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001716 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001717 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001718 }
egdanielff1d5472015-09-10 08:37:20 -07001719
1720 if (!sbDesc.fRenderbufferID) {
1721 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1722 }
1723 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001724 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001725 }
1726 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1727 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001728 // we do this "if" so that we don't call the multisample
1729 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001730 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001731 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1732 width, height)) {
1733 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1734 return nullptr;
1735 }
egdanielff1d5472015-09-10 08:37:20 -07001736 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001737 GrGLenum error = GL_ALLOC_CALL(
1738 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1739 if (error != GR_GL_NO_ERROR) {
1740 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1741 return nullptr;
1742 }
egdanielff1d5472015-09-10 08:37:20 -07001743 }
1744 fStats.incStencilAttachmentCreates();
1745 // After sized formats we attempt an unsized format and take
1746 // whatever sizes GL gives us. In that case we query for the size.
1747 GrGLStencilAttachment::Format format = sFmt;
1748 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001749 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1750 sbDesc,
1751 width,
1752 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001753 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001754 format);
1755 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001756}
1757
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001758////////////////////////////////////////////////////////////////////////////////
1759
Brian Salomondbf70722019-02-07 11:31:24 -05001760sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1761 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001762 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001763}
1764
Chris Dalton2e7ed262020-02-21 15:17:59 -07001765void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1766 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001767 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1768 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1769 fHWScissorSettings.fEnabled = kYes_TriState;
1770 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001771 } else {
1772 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1773 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1774 fHWScissorSettings.fEnabled = kNo_TriState;
1775 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001777}
Brian Salomond818ebf2018-07-02 14:08:49 +00001778
Chris Dalton2e7ed262020-02-21 15:17:59 -07001779void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1780 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001781 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001782 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1783 if (fHWScissorSettings.fRect != nativeScissor) {
1784 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1785 nativeScissor.fHeight));
1786 fHWScissorSettings.fRect = nativeScissor;
1787 }
joshualitt77b13072014-10-27 14:51:01 -07001788}
1789
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001790void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001791 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001792#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001793 typedef GrWindowRectsState::Mode Mode;
1794 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1795 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001796
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001797 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001798 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001799 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001800 }
1801
csmartdalton7535f412016-08-23 06:51:00 -07001802 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1803 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001804 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001805 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001806
Chris Daltond6cda8d2019-09-05 02:30:04 -06001807 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001808 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001809 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001810 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001811 }
1812
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001813 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001814 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001815
Greg Danielacd66b42019-05-22 16:29:12 -04001816 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001817#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001818}
1819
1820void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001821#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001822 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001823 return;
1824 }
1825 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001826 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001827#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001828}
1829
Robert Phillipsdf546db2020-05-29 09:42:54 -04001830bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001831 this->handleDirtyContext();
1832
Chris Daltond42d2002020-02-28 12:05:04 -07001833 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001834 if (!program) {
1835 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1836 return false;
1837 }
1838
1839 this->flushProgram(std::move(program));
1840
Chris Daltond42d2002020-02-28 12:05:04 -07001841 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1842 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1843 }
1844
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001845 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001846 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001847 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001848
Chris Dalton20e7a532020-02-28 15:37:53 +00001849 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001850
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001851 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001852 GrStencilSettings stencil;
1853 if (programInfo.pipeline().isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001854 SkASSERT(glRT->getStencilAttachment());
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001855 stencil.reset(*programInfo.pipeline().getUserStencil(),
1856 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001857 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001858 }
Robert Phillips901aff02019-10-08 12:32:56 -04001859 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001860 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001861 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1862 glRT, programInfo.origin());
1863 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001864 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001865 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001866
1867 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001868 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001869 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001870
Chris Dalton20e7a532020-02-28 15:37:53 +00001871 return true;
1872}
1873
1874void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1875 if (!program) {
1876 fHWProgram.reset();
1877 fHWProgramID = 0;
1878 return;
1879 }
1880 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1881 if (program == fHWProgram) {
1882 return;
1883 }
1884 auto id = program->programID();
1885 SkASSERT(id);
1886 GL_CALL(UseProgram(id));
1887 fHWProgram = std::move(program);
1888 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001889}
1890
1891void GrGLGpu::flushProgram(GrGLuint id) {
1892 SkASSERT(id);
1893 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001894 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001895 return;
1896 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001897 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001898 GL_CALL(UseProgram(id));
1899 fHWProgramID = id;
1900}
1901
Brian Salomonae64c192019-02-05 09:41:37 -05001902GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001903 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001904
cdaltone2e71c22016-04-07 18:13:29 -07001905 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001906 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001907 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001908 }
cdaltone2e71c22016-04-07 18:13:29 -07001909
Brian Salomonae64c192019-02-05 09:41:37 -05001910 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001911 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001912 if (!bufferState->fBufferZeroKnownBound) {
1913 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1914 bufferState->fBufferZeroKnownBound = true;
1915 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001916 }
Brian Salomondbf70722019-02-07 11:31:24 -05001917 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1918 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001919 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1920 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1921 bufferState->fBufferZeroKnownBound = false;
1922 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001923 }
1924
Brian Salomonae64c192019-02-05 09:41:37 -05001925 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001926}
Brian Salomond818ebf2018-07-02 14:08:49 +00001927
Michael Ludwig1e632792020-05-21 12:45:31 -04001928void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001929 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001930 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001931 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001932 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001933 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001934
Brian Salomon43f8bf02017-10-18 08:33:29 -04001935 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001936
Brian Salomon43f8bf02017-10-18 08:33:29 -04001937 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1938
Michael Ludwig1e632792020-05-21 12:45:31 -04001939 if (scissor.enabled()) {
1940 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001941 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001942 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001943 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001944 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1945 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001946 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001947 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001948 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001949}
1950
Chris Daltonb50cc812019-10-14 16:29:21 -06001951static bool use_tiled_rendering(const GrGLCaps& glCaps,
1952 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1953 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1954 // Otherwise it's faster to just not use it.
1955 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1956 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1957}
1958
1959void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001960 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1961 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1962 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1963
1964 this->handleDirtyContext();
1965
1966 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1967 this->flushRenderTarget(glRT);
1968 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1969
Chris Daltonb50cc812019-10-14 16:29:21 -06001970 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1971 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1972 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1973 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1974 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1975 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1976 nativeBounds.fHeight, preserveMask));
1977 }
1978
Chris Dalton674f77a2019-09-30 20:49:39 -06001979 GrGLbitfield clearMask = 0;
1980 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1981 SkASSERT(!this->caps()->performColorClearsAsDraws());
1982 this->flushClearColor(colorLoadStore.fClearColor);
1983 this->flushColorWrite(true);
1984 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001985 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001986 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1987 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1988 GL_CALL(StencilMask(0xffffffff));
1989 GL_CALL(ClearStencil(0));
1990 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1991 }
1992 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001993 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06001994 this->disableWindowRectangles();
1995 GL_CALL(Clear(clearMask));
1996 }
1997}
1998
1999void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2000 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2001 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2002 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2003
2004 this->handleDirtyContext();
2005
2006 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2007 // The framebuffer binding changed in the middle of a command buffer. We should have already
2008 // printed a warning during onFBOChanged.
2009 return;
2010 }
2011
2012 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2013 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2014
2015 SkSTArray<2, GrGLenum> discardAttachments;
2016 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2017 discardAttachments.push_back(
2018 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2019 }
2020 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2021 discardAttachments.push_back(
2022 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2023 }
2024
2025 if (!discardAttachments.empty()) {
2026 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2027 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2028 discardAttachments.begin()));
2029 } else {
2030 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2031 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2032 discardAttachments.begin()));
2033 }
2034 }
2035 }
2036
Chris Daltonb50cc812019-10-14 16:29:21 -06002037 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2038 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2039 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2040 // Handled by use_tiled_rendering().
2041 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2042 GL_CALL(EndTiling(preserveMask));
2043 }
2044
Chris Dalton674f77a2019-09-30 20:49:39 -06002045 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002046}
2047
Michael Ludwig1e632792020-05-21 12:45:31 -04002048void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002049 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002050 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002051 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002052 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002053 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002054
Brian Salomonf7f54332020-07-28 09:23:35 -04002055 GrStencilAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002056 if (!sb) {
2057 // We should only get here if we marked a proxy as requiring a SB. However,
2058 // the SB creation could later fail. Likely clipping is going to go awry now.
2059 return;
2060 }
2061
bsalomon6bc1b5f2015-02-23 09:06:38 -08002062 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002063#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002064 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002065 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002066#else
2067 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002068 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002069 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002070 // changing the clip between stencil passes may or may not
2071 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002072 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002073#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002074 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002075 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002076 value = (1 << (stencilBitCount - 1));
2077 } else {
2078 value = 0;
2079 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002080 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002081 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002082
Michael Ludwig1e632792020-05-21 12:45:31 -04002083 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2084 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002085
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002086 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002087 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002088 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002089 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002090}
2091
Brian Salomone05ba5a2019-04-08 11:59:07 -04002092bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002093 GrColorType surfaceColorType, GrColorType dstColorType,
2094 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002095 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002096
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002097 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002098 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002099 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002100 return false;
2101 }
Greg Danielba88ab62019-07-26 09:14:01 -04002102 GrGLenum externalFormat = 0;
2103 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002104 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2105 surfaceColorType,
2106 dstColorType,
2107 &externalFormat,
2108 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002109 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002110 return false;
2111 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002112
Brian Salomon71d9d842016-11-03 13:42:00 -04002113 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002114 if (renderTarget->numSamples() <= 1 ||
2115 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2116 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2117 this->flushRenderTargetNoColorWrites(renderTarget);
2118 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2119 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2120 return false;
2121 } else {
2122 SkASSERT(renderTarget->requiresManualMSAAResolve());
2123 // we don't track the state of the READ FBO ID.
2124 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002125 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002126 } else {
2127 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002128 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002129 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002130 }
2131
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002132 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002133 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002134
Brian Salomona6948702018-06-01 15:33:20 -04002135 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002136 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002137 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002138 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002139 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002140 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002141
Brian Osman1348ed02018-09-12 09:08:39 -04002142 bool reattachStencil = false;
2143 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2144 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002145 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002146 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002147 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2148 reattachStencil = true;
2149 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2150 GR_GL_RENDERBUFFER, 0));
2151 }
2152
Chris Dalton76500e52019-09-05 02:13:05 -06002153 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002154 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002155
2156 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002157 auto* stencilAttachment =
2158 static_cast<GrGLStencilAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002159 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2160 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2161 }
2162
Brian Salomon26de56e2019-04-10 12:14:26 -04002163 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002164 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002165 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2166 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002167
Brian Salomone05ba5a2019-04-08 11:59:07 -04002168 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002169 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002170 }
2171 return true;
2172}
2173
2174bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002175 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2176 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002177 SkASSERT(surface);
2178
Brian Salomonb28cb682019-07-26 12:48:47 -04002179 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002180
Brian Salomon26de56e2019-04-10 12:14:26 -04002181 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2182 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002183
Brian Salomon1047a492019-07-02 12:25:21 -04002184 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002185 rowPixelWidth = width;
2186 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002187 SkASSERT(!(rowBytes % bytesPerPixel));
2188 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002189 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002190 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2191 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002192}
2193
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002194GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002195 GrRenderTarget* rt, GrStencilAttachment*,
2196 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002197 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002198 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002199 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002200 if (!fCachedOpsRenderPass) {
2201 fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002202 }
2203
Chris Daltonb50cc812019-10-14 16:29:21 -06002204 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002205 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002206}
2207
Brian Salomon1fabd512018-02-09 09:54:25 -05002208void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002209 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002210 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002211 this->didWriteToSurface(target, origin, &bounds);
2212}
bsalomon6ba6fa12015-03-04 11:57:37 -08002213
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002214void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2215 this->flushRenderTargetNoColorWrites(target);
2216 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002217}
2218
Brian Osman9aa30c62018-07-02 15:21:46 -04002219void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002220 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002221 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002222 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002223 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002224#ifdef SK_DEBUG
2225 // don't do this check in Chromium -- this is causing
2226 // lots of repeated command buffer flushes when the compositor is
2227 // rendering with Ganesh, which is really slow; even too slow for
2228 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002229 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002230 GrGLenum status;
2231 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2232 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2233 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2234 }
bsalomon160f24c2015-03-17 15:55:42 -07002235 }
egdanield803f272015-03-18 13:01:52 -07002236#endif
2237 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002238 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002239 }
2240
brianosman35b784d2016-05-05 11:52:53 -07002241 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002242 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002243 }
Brian Salomon9b553272020-01-24 14:38:37 -05002244
2245 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2246 GrGLint format;
2247 GrGLint type;
2248 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2249 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2250 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2251 }
bsalomon083617b2016-02-12 12:10:14 -08002252}
bsalomona9909122016-01-23 10:41:40 -08002253
brianosman33f6b3f2016-06-02 05:49:21 -07002254void GrGLGpu::flushFramebufferSRGB(bool enable) {
2255 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2256 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2257 fHWSRGBFramebuffer = kYes_TriState;
2258 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2259 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2260 fHWSRGBFramebuffer = kNo_TriState;
2261 }
2262}
2263
Greg Danielacd66b42019-05-22 16:29:12 -04002264void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002265 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002266 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002267 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002268 fHWViewport = viewport;
2269 }
2270}
2271
Chris Dalton33db9fc2020-02-25 18:52:12 -07002272GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002273 fStats.incNumDraws();
2274
Chris Dalton2e7ed262020-02-21 15:17:59 -07002275 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2276 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2277 GL_CALL(Enable(GR_GL_CULL_FACE));
2278 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002279 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002280 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002281
Chris Dalton3809bab2017-06-13 10:55:06 -06002282 switch (primitiveType) {
2283 case GrPrimitiveType::kTriangles:
2284 return GR_GL_TRIANGLES;
2285 case GrPrimitiveType::kTriangleStrip:
2286 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002287 case GrPrimitiveType::kPoints:
2288 return GR_GL_POINTS;
2289 case GrPrimitiveType::kLines:
2290 return GR_GL_LINES;
2291 case GrPrimitiveType::kLineStrip:
2292 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002293 case GrPrimitiveType::kPatches:
2294 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002295 case GrPrimitiveType::kPath:
2296 SK_ABORT("non-mesh-based GrPrimitiveType");
2297 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002298 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002299 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002300}
2301
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002302void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002303 // Some extensions automatically resolves the texture when it is read.
2304 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2305
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002306 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002307 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2308 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2309 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2310 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002311
Chris Daltonc139d082019-09-26 14:04:24 -06002312 // make sure we go through flushRenderTarget() since we've modified
2313 // the bound DRAW FBO ID.
2314 fHWBoundRenderTargetUniqueID.makeInvalid();
2315 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2316 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002317 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2318 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002319 GrScissorState scissor(rt->dimensions());
2320 SkAssertResult(scissor.set(resolveRect));
2321 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002322 this->disableWindowRectangles();
2323 GL_CALL(ResolveMultisampleFramebuffer());
2324 } else {
2325 int l, b, r, t;
2326 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2327 this->glCaps().blitFramebufferSupportFlags()) {
2328 l = 0;
2329 b = 0;
2330 r = target->width();
2331 t = target->height();
2332 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002333 l = resolveRect.x();
2334 b = resolveRect.y();
2335 r = resolveRect.x() + resolveRect.width();
2336 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002337 }
Chris Daltonc139d082019-09-26 14:04:24 -06002338
2339 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002340 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002341 this->disableWindowRectangles();
2342 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 +00002343 }
2344}
2345
bsalomon@google.com411dad02012-06-05 20:24:20 +00002346namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002347
bsalomon@google.com411dad02012-06-05 20:24:20 +00002348
2349GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002350 static const GrGLenum gTable[kGrStencilOpCount] = {
2351 GR_GL_KEEP, // kKeep
2352 GR_GL_ZERO, // kZero
2353 GR_GL_REPLACE, // kReplace
2354 GR_GL_INVERT, // kInvert
2355 GR_GL_INCR_WRAP, // kIncWrap
2356 GR_GL_DECR_WRAP, // kDecWrap
2357 GR_GL_INCR, // kIncClamp
2358 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002359 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002360 static_assert(0 == (int)GrStencilOp::kKeep);
2361 static_assert(1 == (int)GrStencilOp::kZero);
2362 static_assert(2 == (int)GrStencilOp::kReplace);
2363 static_assert(3 == (int)GrStencilOp::kInvert);
2364 static_assert(4 == (int)GrStencilOp::kIncWrap);
2365 static_assert(5 == (int)GrStencilOp::kDecWrap);
2366 static_assert(6 == (int)GrStencilOp::kIncClamp);
2367 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002368 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2369 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002370}
2371
2372void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002373 const GrStencilSettings::Face& face,
2374 GrGLenum glFace) {
2375 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2376 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2377 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002378
cdalton93a379b2016-05-11 13:58:08 -07002379 GrGLint ref = face.fRef;
2380 GrGLint mask = face.fTestMask;
2381 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002382
2383 if (GR_GL_FRONT_AND_BACK == glFace) {
2384 // we call the combined func just in case separate stencil is not
2385 // supported.
2386 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2387 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002388 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002389 } else {
2390 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2391 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002392 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002393 }
2394}
2395}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002396
Chris Dalton71713f62019-04-18 12:41:03 -06002397void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002398 if (stencilSettings.isDisabled()) {
2399 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002400 } else if (fHWStencilSettings != stencilSettings ||
2401 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002402 if (kYes_TriState != fHWStencilTestEnabled) {
2403 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002404
csmartdaltonc7d85332016-10-26 10:13:46 -07002405 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002406 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002407 if (!stencilSettings.isTwoSided()) {
2408 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2409 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002410 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002411 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2412 GR_GL_FRONT);
2413 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2414 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002415 }
joshualitta58fe352014-10-27 08:39:00 -07002416 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002417 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002418 }
2419}
2420
csmartdaltonc7d85332016-10-26 10:13:46 -07002421void GrGLGpu::disableStencil() {
2422 if (kNo_TriState != fHWStencilTestEnabled) {
2423 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002424
csmartdaltonc7d85332016-10-26 10:13:46 -07002425 fHWStencilTestEnabled = kNo_TriState;
2426 fHWStencilSettings.invalidate();
2427 }
2428}
2429
Chris Dalton4c56b032019-03-04 12:28:42 -07002430void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002431 // rt is only optional if useHWAA is false.
2432 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002433#ifdef SK_DEBUG
2434 if (useHWAA && rt->numSamples() <= 1) {
2435 SkASSERT(this->caps()->mixedSamplesSupport());
2436 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002437 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002438 }
2439#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002440
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002441 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002442 if (useHWAA) {
2443 if (kYes_TriState != fMSAAEnabled) {
2444 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2445 fMSAAEnabled = kYes_TriState;
2446 }
2447 } else {
2448 if (kNo_TriState != fMSAAEnabled) {
2449 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2450 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002451 }
2452 }
2453 }
2454}
2455
Chris Daltonce425af2019-12-16 10:39:03 -07002456void GrGLGpu::flushConservativeRasterState(bool enabled) {
2457 if (this->caps()->conservativeRasterSupport()) {
2458 if (enabled) {
2459 if (kYes_TriState != fHWConservativeRasterEnabled) {
2460 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2461 fHWConservativeRasterEnabled = kYes_TriState;
2462 }
2463 } else {
2464 if (kNo_TriState != fHWConservativeRasterEnabled) {
2465 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2466 fHWConservativeRasterEnabled = kNo_TriState;
2467 }
2468 }
2469 }
2470}
2471
Chris Dalton1215cda2019-12-17 21:44:04 -07002472void GrGLGpu::flushWireframeState(bool enabled) {
2473 if (this->caps()->wireframeSupport()) {
2474 if (this->caps()->wireframeMode() || enabled) {
2475 if (kYes_TriState != fHWWireframeEnabled) {
2476 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2477 fHWWireframeEnabled = kYes_TriState;
2478 }
2479 } else {
2480 if (kNo_TriState != fHWWireframeEnabled) {
2481 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2482 fHWWireframeEnabled = kNo_TriState;
2483 }
2484 }
2485 }
2486}
2487
Chris Daltonbbb3f642019-07-24 12:25:08 -04002488void GrGLGpu::flushBlendAndColorWrite(
2489 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2490 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2491 // We need to work around a driver bug by using a blend state that preserves the dst color,
2492 // rather than disabling color writes.
2493 GrXferProcessor::BlendInfo preserveDstBlend;
2494 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2495 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2496 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2497 return;
2498 }
bsalomonf7cc8772015-05-11 11:21:14 -07002499
cdalton8917d622015-05-06 13:40:21 -07002500 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002501 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2502 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002503
2504 // Any optimization to disable blending should have already been applied and
2505 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002506 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2507 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002508
egdanielb414f252014-07-29 13:15:47 -07002509 if (blendOff) {
2510 if (kNo_TriState != fHWBlendState.fEnabled) {
2511 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002512
Jim Van Verth1bef9792020-07-09 08:09:13 -04002513 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002514 // https://code.google.com/p/skia/issues/detail?id=3943
2515 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2516 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2517 SkASSERT(this->caps()->advancedBlendEquationSupport());
2518 // Set to any basic blending equation.
2519 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2520 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2521 fHWBlendState.fEquation = blend_equation;
2522 }
2523
egdanielb414f252014-07-29 13:15:47 -07002524 fHWBlendState.fEnabled = kNo_TriState;
2525 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002526 } else {
2527 if (kYes_TriState != fHWBlendState.fEnabled) {
2528 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002529
Chris Daltonbbb3f642019-07-24 12:25:08 -04002530 fHWBlendState.fEnabled = kYes_TriState;
2531 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002532
Chris Daltonbbb3f642019-07-24 12:25:08 -04002533 if (fHWBlendState.fEquation != equation) {
2534 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2535 fHWBlendState.fEquation = equation;
2536 }
cdalton8917d622015-05-06 13:40:21 -07002537
Chris Daltonbbb3f642019-07-24 12:25:08 -04002538 if (GrBlendEquationIsAdvanced(equation)) {
2539 SkASSERT(this->caps()->advancedBlendEquationSupport());
2540 // Advanced equations have no other blend state.
2541 return;
2542 }
cdalton8917d622015-05-06 13:40:21 -07002543
Chris Daltonbbb3f642019-07-24 12:25:08 -04002544 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2545 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2546 gXfermodeCoeff2Blend[dstCoeff]));
2547 fHWBlendState.fSrcCoeff = srcCoeff;
2548 fHWBlendState.fDstCoeff = dstCoeff;
2549 }
cdalton8917d622015-05-06 13:40:21 -07002550
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002551 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002552 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2553 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2554 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2555 fHWBlendState.fConstColor = blendConst;
2556 fHWBlendState.fConstColorValid = true;
2557 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002558 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002559 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002560
2561 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002562}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002563
Brian Salomondc829942018-10-23 16:07:24 -04002564static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002565 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002566 switch (swizzle[i]) {
2567 case 'r': glValues[i] = GR_GL_RED; break;
2568 case 'g': glValues[i] = GR_GL_GREEN; break;
2569 case 'b': glValues[i] = GR_GL_BLUE; break;
2570 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002571 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002572 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002573 default: SK_ABORT("Unsupported component");
2574 }
Brian Salomon327955b2018-10-22 15:39:22 +00002575 }
2576}
2577
Greg Daniel2c3398d2019-06-19 11:58:01 -04002578void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2579 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002580 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002581
reed856e9d92015-09-30 12:21:45 -07002582#ifdef SK_DEBUG
2583 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002584 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002585 const int w = texture->width();
2586 const int h = texture->height();
2587 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2588 }
2589 }
2590#endif
2591
Robert Phillips294870f2016-11-11 12:38:40 -05002592 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002593 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002594 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002595 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002596 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002597 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002598 }
2599
Brian Salomone69b9ef2020-07-22 11:18:06 -04002600 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002601 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002602 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2603 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002604 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002605 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002606 }
bsalomonefd7d452014-10-23 14:17:46 -07002607
Brian Salomone2826ab2019-06-04 15:58:31 -04002608 auto timestamp = texture->parameters()->resetTimestamp();
2609 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002610 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2611 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002612 if (fSamplerObjectCache) {
2613 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002614 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002615 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2616 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2617 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002618 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2619 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002620 this->setTextureUnit(unitIdx);
2621 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2622 newSamplerState = oldSamplerState;
2623 newSamplerState.fMinFilter = minFilter;
2624 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002625 }
2626 }
Brian Salomondc829942018-10-23 16:07:24 -04002627 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002628 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2629 texture->parameters()->samplerOverriddenState();
2630 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002631
Brian Salomone69b9ef2020-07-22 11:18:06 -04002632 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2633 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002634 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002635
Brian Salomone2826ab2019-06-04 15:58:31 -04002636 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2637 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002638
2639 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002640 newSamplerState.fMinLOD = -1000.f;
2641 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002642
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
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_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002646 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002647 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002648 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002649 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002650 }
Brian Salomon69100f02020-07-21 10:49:25 -04002651 if (this->glCaps().mipmapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002653 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002654 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002655 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002656 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002657 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002658 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002659 }
2660 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
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_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002664 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002665 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002666 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002667 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002668 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002669 if (this->glCaps().clampToBorderSupport()) {
2670 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002671 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002672 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002673 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2674 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002675 }
2676 }
Brian Salomondc829942018-10-23 16:07:24 -04002677 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002678 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2679 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002680 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002681
Brian Salomone2826ab2019-06-04 15:58:31 -04002682 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2683 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002684 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002685 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2686 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002687 GrGLenum glValues[4];
2688 get_gl_swizzle_values(swizzle, glValues);
2689 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002690 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002691 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002692 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2693 reinterpret_cast<const GrGLint*>(glValues)));
2694 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002695 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2696 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2697 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2698 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2699 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002700 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002701 }
2702 }
Brian Salomondc829942018-10-23 16:07:24 -04002703 // These are not supported in ES2 contexts
Brian Salomon69100f02020-07-21 10:49:25 -04002704 if (this->glCaps().mipmapLevelAndLodControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002705 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002706 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002707 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002708 this->setTextureUnit(unitIdx);
2709 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002710 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002711 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002712 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002713 this->setTextureUnit(unitIdx);
2714 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002715 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002716 }
Brian Salomon327955b2018-10-22 15:39:22 +00002717 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002718 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2719 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002720}
2721
Brian Salomon1f05d452019-02-08 12:33:08 -05002722void GrGLGpu::onResetTextureBindings() {
2723 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2724 GR_GL_TEXTURE_EXTERNAL};
2725 for (int i = 0; i < this->numTextureUnits(); ++i) {
2726 this->setTextureUnit(i);
2727 for (auto target : kTargets) {
2728 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2729 GL_CALL(BindTexture(target, 0));
2730 }
2731 }
2732 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2733 }
2734}
2735
Chris Dalton5a2f9622019-12-27 14:56:38 -07002736void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2737 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2738 if (fHWPatchVertexCount != count) {
2739 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2740 fHWPatchVertexCount = count;
2741 }
2742}
2743
egdaniel080e6732014-12-22 07:35:52 -08002744void GrGLGpu::flushColorWrite(bool writeColor) {
2745 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002746 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002747 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2748 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002749 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002750 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002751 } else {
2752 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002753 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002754 fHWWriteToColor = kYes_TriState;
2755 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002756 }
bsalomon3e791242014-12-17 13:43:13 -08002757}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002758
Chris Dalton674f77a2019-09-30 20:49:39 -06002759void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2760 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2761 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2762 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2763 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2764 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2765 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2766 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002767 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2768 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2769 GL_CALL(ClearColor(r, g, b, a));
2770 fHWClearColor[0] = r;
2771 fHWClearColor[1] = g;
2772 fHWClearColor[2] = b;
2773 fHWClearColor[3] = a;
2774 }
2775}
2776
bsalomon861e1032014-12-16 07:33:49 -08002777void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002778 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002779 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002780 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002781 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002782 }
2783}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002784
Brian Salomond978b902019-02-07 15:09:18 -05002785void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002786 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002787 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002788 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2789 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2790 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002791 }
Brian Salomond978b902019-02-07 15:09:18 -05002792 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2793 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002794 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002795 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002796}
2797
Brian Salomone5e7eb12016-10-14 16:18:33 -04002798// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002799static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2800 const GrSurface* src,
2801 const SkIRect& srcRect,
2802 const SkIPoint& dstPoint,
2803 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002804 int dstSampleCnt = 0;
2805 int srcSampleCnt = 0;
2806 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002807 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002808 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002809 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002810 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002811 }
2812 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2813 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2814
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002815 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2816 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2817
Brian Salomone5e7eb12016-10-14 16:18:33 -04002818 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002819 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002820
Greg Daniel46cfbc62019-06-07 11:43:30 -04002821 GrTextureType dstTexType;
2822 GrTextureType* dstTexTypePtr = nullptr;
2823 GrTextureType srcTexType;
2824 GrTextureType* srcTexTypePtr = nullptr;
2825 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002826 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002827 dstTexTypePtr = &dstTexType;
2828 }
2829 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002830 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002831 srcTexTypePtr = &srcTexType;
2832 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002833
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002834 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2835 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002836 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002837}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002838
Brian Osmana9c8a052018-01-19 10:31:56 -05002839static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2840 // A RT has a separate MSAA renderbuffer if:
2841 // 1) It's multisampled
2842 // 2) We're using an extension with separate MSAA renderbuffers
2843 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002844 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002845}
2846
Greg Daniel46cfbc62019-06-07 11:43:30 -04002847static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002848 const GrGLCaps& caps) {
2849
bsalomon@google.comeb851172013-04-15 13:51:00 +00002850 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002851 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002852 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002853 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002854
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002855 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2856 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2857
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002858 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2859 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2860
Greg Daniel46cfbc62019-06-07 11:43:30 -04002861 GrTextureType dstTexType;
2862 GrTextureType* dstTexTypePtr = nullptr;
2863 GrTextureType srcTexType;
2864 GrTextureType* srcTexTypePtr = nullptr;
2865 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002866 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002867 dstTexTypePtr = &dstTexType;
2868 }
2869 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002870 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002871 srcTexTypePtr = &srcTexType;
2872 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002873
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002874 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2875 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002876}
2877
Greg Danielacd66b42019-05-22 16:29:12 -04002878// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002879void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002880 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002881 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002882 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002883 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002884 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2885 GrGLuint texID = texture->textureID();
2886 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002887 GrGLuint* tempFBOID;
2888 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002889
egdanield803f272015-03-18 13:01:52 -07002890 if (0 == *tempFBOID) {
2891 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002892 }
2893
Adrienne Walker4ee88512018-05-17 11:37:14 -07002894 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002895 GR_GL_CALL(
2896 this->glInterface(),
2897 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2898 if (mipLevel == 0) {
2899 texture->baseLevelWasBoundToFBO();
2900 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002901 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002902 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002903 }
egdaniel0f5f9672015-02-03 11:10:51 -08002904}
2905
Brian Salomond2a8ae22019-09-10 16:03:59 -04002906void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002907 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002908 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002909 SkASSERT(surface->asTexture());
2910 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2911 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2912 GR_GL_COLOR_ATTACHMENT0,
2913 textureTarget,
2914 0,
2915 0));
2916 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002917}
2918
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002919void GrGLGpu::onFBOChanged() {
2920 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2921 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002922 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002923 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002924#ifdef SK_DEBUG
2925 if (fIsExecutingCommandBuffer_DebugOnly) {
2926 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2927 "This will severely hurt performance.\n");
2928 }
2929#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002930}
2931
Adrienne Walker4ee88512018-05-17 11:37:14 -07002932void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2933 fStats.incRenderTargetBinds();
2934 GL_CALL(BindFramebuffer(target, fboid));
2935 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2936 fBoundDrawFramebuffer = fboid;
2937 }
2938
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002939 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2940 // The driver forgets the correct scissor when modifying the FBO binding.
2941 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002942 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002943 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002944 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002945 }
2946
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002947 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002948}
2949
Adrienne Walker4ee88512018-05-17 11:37:14 -07002950void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002951 // We're relying on the GL state shadowing being correct in the workaround code below so we
2952 // need to handle a dirty context.
2953 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002954 if (fboid == fBoundDrawFramebuffer &&
2955 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2956 // This workaround only applies to deleting currently bound framebuffers
2957 // on Adreno 420. Because this is a somewhat rare case, instead of
2958 // tracking all the attachments of every framebuffer instead just always
2959 // unbind all attachments.
2960 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2961 GR_GL_RENDERBUFFER, 0));
2962 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2963 GR_GL_RENDERBUFFER, 0));
2964 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2965 GR_GL_RENDERBUFFER, 0));
2966 }
2967
2968 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002969
2970 // Deleting the currently bound framebuffer rebinds to 0.
2971 if (fboid == fBoundDrawFramebuffer) {
2972 this->onFBOChanged();
2973 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002974}
2975
Greg Daniel46cfbc62019-06-07 11:43:30 -04002976bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002977 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002978 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2979 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2980 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002981 auto dstFormat = dst->backendFormat().asGLFormat();
2982 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002983 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002984 return true;
2985 }
2986 }
cblume61214052016-01-26 09:10:48 -08002987
Greg Daniel46cfbc62019-06-07 11:43:30 -04002988 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2989 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002990 return true;
2991 }
2992
Greg Daniel46cfbc62019-06-07 11:43:30 -04002993 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
2994 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002995 }
2996
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002997 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002998 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002999 return true;
3000 }
bsalomon083617b2016-02-12 12:10:14 -08003001 }
3002
bsalomon6df86402015-06-01 10:41:49 -07003003 return false;
3004}
3005
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003006bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003007 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003008
3009 int progIdx = TextureToCopyProgramIdx(srcTex);
3010 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003011 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003012
3013 if (!fCopyProgramArrayBuffer) {
3014 static const GrGLfloat vdata[] = {
3015 0, 0,
3016 0, 1,
3017 1, 0,
3018 1, 1
3019 };
3020 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3021 kStatic_GrAccessPattern, vdata);
3022 }
3023 if (!fCopyProgramArrayBuffer) {
3024 return false;
3025 }
3026
3027 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3028 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3029 if (!fCopyPrograms[progIdx].fProgram) {
3030 return false;
3031 }
3032
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003033 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003034 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003035 GrShaderVar::TypeModifier::Uniform);
3036 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3037 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3038 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3039 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003040
Greg Daniel9e3169b2019-06-06 14:38:17 -04003041 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003042 if (shaderCaps->noperspectiveInterpolationSupport()) {
3043 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3044 vshaderTxt.appendf("#extension %s : require\n", extension);
3045 }
3046 vTexCoord.addModifier("noperspective");
3047 }
3048
3049 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3050 vshaderTxt.append(";");
3051 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3052 vshaderTxt.append(";");
3053 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3054 vshaderTxt.append(";");
3055 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3056 vshaderTxt.append(";");
3057
3058 vshaderTxt.append(
3059 "// Copy Program VS\n"
3060 "void main() {"
3061 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3062 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3063 " sk_Position.zw = half2(0, 1);"
3064 "}"
3065 );
3066
Greg Daniel9e3169b2019-06-06 14:38:17 -04003067 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003068 if (shaderCaps->noperspectiveInterpolationSupport()) {
3069 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3070 fshaderTxt.appendf("#extension %s : require\n", extension);
3071 }
3072 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003073 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003074 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3075 fshaderTxt.append(";");
3076 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3077 fshaderTxt.append(";");
3078 fshaderTxt.appendf(
3079 "// Copy Program FS\n"
3080 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003081 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003082 "}"
3083 );
3084
3085 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3086 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3087 SkSL::Program::Settings settings;
3088 settings.fCaps = shaderCaps;
3089 SkSL::String glsl;
3090 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3091 sksl, settings, &glsl, errorHandler);
3092 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3093 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3094 SkASSERT(program->fInputs.isEmpty());
3095
3096 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3097 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3098 errorHandler);
3099 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3100 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3101 errorHandler);
3102 SkASSERT(program->fInputs.isEmpty());
3103
3104 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3105
3106 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3107 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3108 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3109 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3110 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3111 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3112
3113 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3114
3115 GL_CALL(DeleteShader(vshader));
3116 GL_CALL(DeleteShader(fshader));
3117
3118 return true;
3119}
3120
brianosman33f6b3f2016-06-02 05:49:21 -07003121bool GrGLGpu::createMipmapProgram(int progIdx) {
3122 const bool oddWidth = SkToBool(progIdx & 0x2);
3123 const bool oddHeight = SkToBool(progIdx & 0x1);
3124 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3125
Brian Salomon1edc5b92016-11-29 13:43:46 -05003126 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003127
3128 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3129 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3130 if (!fMipmapPrograms[progIdx].fProgram) {
3131 return false;
3132 }
3133
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003134 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003135 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003136 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003137 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003138 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003139 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003140 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003141 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3142 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3143 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3144 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003145 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003146 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003147
Ethan Nicholasfc994162019-06-06 10:04:27 -04003148 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003149 if (shaderCaps->noperspectiveInterpolationSupport()) {
3150 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003151 vshaderTxt.appendf("#extension %s : require\n", extension);
3152 }
3153 vTexCoords[0].addModifier("noperspective");
3154 vTexCoords[1].addModifier("noperspective");
3155 vTexCoords[2].addModifier("noperspective");
3156 vTexCoords[3].addModifier("noperspective");
3157 }
3158
Brian Salomon1edc5b92016-11-29 13:43:46 -05003159 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003160 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003161 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003162 vshaderTxt.append(";");
3163 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003164 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003165 vshaderTxt.append(";");
3166 }
3167
3168 vshaderTxt.append(
3169 "// Mipmap Program VS\n"
3170 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003171 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3172 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003173 );
3174
3175 // Insert texture coordinate computation:
3176 if (oddWidth && oddHeight) {
3177 vshaderTxt.append(
3178 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003179 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3180 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003181 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3182 );
3183 } else if (oddWidth) {
3184 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003185 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3186 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003187 );
3188 } else if (oddHeight) {
3189 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003190 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3191 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003192 );
3193 } else {
3194 vshaderTxt.append(
3195 " v_texCoord0 = a_vertex.xy;"
3196 );
3197 }
3198
3199 vshaderTxt.append("}");
3200
Ethan Nicholasfc994162019-06-06 10:04:27 -04003201 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003202 if (shaderCaps->noperspectiveInterpolationSupport()) {
3203 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003204 fshaderTxt.appendf("#extension %s : require\n", extension);
3205 }
3206 }
brianosman33f6b3f2016-06-02 05:49:21 -07003207 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003208 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003209 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003210 fshaderTxt.append(";");
3211 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003212 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003213 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003214 fshaderTxt.append(
3215 "// Mipmap Program FS\n"
3216 "void main() {"
3217 );
3218
3219 if (oddWidth && oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003220 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003221 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3222 " sample(u_texture, v_texCoord1) + "
3223 " sample(u_texture, v_texCoord2) + "
3224 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003225 );
3226 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003227 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003228 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3229 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003230 );
3231 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003232 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003233 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003234 );
3235 }
3236
3237 fshaderTxt.append("}");
3238
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003239 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003240 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003241 SkSL::Program::Settings settings;
3242 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003243 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003244 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003245 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003246 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003247 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003248 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003249
Brian Osman6c431d52019-04-15 16:31:54 -04003250 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003251 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3252 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003253 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003254 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003255 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003256 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003257
3258 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3259
3260 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3261 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3262 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3263 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3264
3265 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3266
3267 GL_CALL(DeleteShader(vshader));
3268 GL_CALL(DeleteShader(fshader));
3269
3270 return true;
3271}
3272
Greg Daniel46cfbc62019-06-07 11:43:30 -04003273bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003274 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003275 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3276 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3277 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3278 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003279 return false;
3280 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003281 int progIdx = TextureToCopyProgramIdx(srcTex);
3282 if (!dstRT) {
3283 SkASSERT(dstTex);
3284 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3285 return false;
3286 }
3287 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003288 if (!fCopyPrograms[progIdx].fProgram) {
3289 if (!this->createCopyProgram(srcTex)) {
3290 SkDebugf("Failed to create copy program.\n");
3291 return false;
3292 }
3293 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003294 int w = srcRect.width();
3295 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003296 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003297 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003298 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003299 this->flushViewport(dst->width(), dst->height());
3300 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003302 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003303 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003304 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3305 attribs->enableVertexArrays(this, 1);
3306 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3307 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003308 // dst rect edges in NDC (-1 to 1)
3309 int dw = dst->width();
3310 int dh = dst->height();
3311 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3312 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3313 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3314 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003315 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3316 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3317 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3318 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3319 int sw = src->width();
3320 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003321 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003322 // src rect edges in normalized texture space (0 to 1)
3323 sx0 /= sw;
3324 sx1 /= sw;
3325 sy0 /= sh;
3326 sy1 /= sh;
3327 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003328 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3329 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3330 sx1 - sx0, sy1 - sy0, sx0, sy0));
3331 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003332 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003333 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003334 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003335 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003336 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003337 this->disableWindowRectangles();
3338 this->disableStencil();
3339 if (this->glCaps().srgbWriteControl()) {
3340 this->flushFramebufferSRGB(true);
3341 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003342 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003343 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003344 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3345 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003346 return true;
3347}
3348
Greg Daniel46cfbc62019-06-07 11:43:30 -04003349void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003350 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003351 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003352 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003353 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003354 SkASSERT(dstTex);
3355 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003356 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003357
Brian Salomond978b902019-02-07 15:09:18 -05003358 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003359 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003360 dstPoint.fX, dstPoint.fY,
3361 srcRect.fLeft, srcRect.fTop,
3362 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003363 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003364 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3365 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003366 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3367 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003368}
3369
Greg Daniel46cfbc62019-06-07 11:43:30 -04003370bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003371 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003372 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003373 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3374 srcRect.width(), srcRect.height());
3375 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003376 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003377 return false;
mtklein404b3b22015-05-18 09:29:10 -07003378 }
bsalomon5df6fee2015-05-18 06:26:15 -07003379 }
bsalomon6df86402015-06-01 10:41:49 -07003380
Brian Salomond2a8ae22019-09-10 16:03:59 -04003381 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3382 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003383 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003384 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003385
3386 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003387 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003388 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003389
Greg Daniel46cfbc62019-06-07 11:43:30 -04003390 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3391 srcRect.fTop,
3392 srcRect.fRight,
3393 srcRect.fBottom,
3394 dstRect.fLeft,
3395 dstRect.fTop,
3396 dstRect.fRight,
3397 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003398 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003399 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3400 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003401
3402 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3403 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003404 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003405}
3406
Brian Salomon930f9392018-06-20 16:25:26 -04003407bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3408 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003409 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003410 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003411 return false;
3412 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003413 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003414 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3415 // Uses draw calls to do a series of downsample operations to successive mips.
3416
3417 // The manual approach requires the ability to limit which level we're sampling and that the
3418 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003419 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003420 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003421 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003422 GL_CALL(GenerateMipmap(glTex->target()));
3423 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003424 }
3425
brianosman33f6b3f2016-06-02 05:49:21 -07003426 int width = texture->width();
3427 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003428 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003429 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003430
3431 // Create (if necessary), then bind temporary FBO:
3432 if (0 == fTempDstFBOID) {
3433 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3434 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003435 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003436 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003437
3438 // Bind the texture, to get things configured for filtering.
3439 // We'll be changing our base level further below:
3440 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003441 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003442 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003443
3444 // Vertex data:
3445 if (!fMipmapProgramArrayBuffer) {
3446 static const GrGLfloat vdata[] = {
3447 0, 0,
3448 0, 1,
3449 1, 0,
3450 1, 1
3451 };
Brian Salomonae64c192019-02-05 09:41:37 -05003452 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003453 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003454 }
3455 if (!fMipmapProgramArrayBuffer) {
3456 return false;
3457 }
3458
3459 fHWVertexArrayState.setVertexArrayID(this, 0);
3460
3461 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003462 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003463 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003464 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003465
3466 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003467 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003468 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003469 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003470 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003471 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003472
3473 // Do all the blits:
3474 width = texture->width();
3475 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003476
brianosman33f6b3f2016-06-02 05:49:21 -07003477 for (GrGLint level = 1; level < levelCount; ++level) {
3478 // Get and bind the program for this particular downsample (filter shape can vary):
3479 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3480 if (!fMipmapPrograms[progIdx].fProgram) {
3481 if (!this->createMipmapProgram(progIdx)) {
3482 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003483 // Invalidate all params to cover base level change in a previous iteration.
3484 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003485 return false;
3486 }
3487 }
Brian Salomon802cb312018-06-08 18:05:20 -04003488 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003489
3490 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3491 const float invWidth = 1.0f / width;
3492 const float invHeight = 1.0f / height;
3493 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3494 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3495 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3496
3497 // Only sample from previous mip
3498 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3499
Brian Salomon930f9392018-06-20 16:25:26 -04003500 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3501 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003502
Brian Osman788b9162020-02-07 10:36:46 -05003503 width = std::max(1, width / 2);
3504 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003505 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003506
3507 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3508 }
3509
3510 // Unbind:
3511 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3512 GR_GL_TEXTURE_2D, 0, 0));
3513
Brian Salomon930f9392018-06-20 16:25:26 -04003514 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003515 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3516 // We drew the 2nd to last level into the last level.
3517 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3518 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003519
brianosman33f6b3f2016-06-02 05:49:21 -07003520 return true;
3521}
3522
Chris Daltond7291ba2019-03-07 14:17:03 -07003523void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003524 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3525 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003526
3527 int effectiveSampleCnt;
3528 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003529 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003530
3531 sampleLocations->reset(effectiveSampleCnt);
3532 for (int i = 0; i < effectiveSampleCnt; ++i) {
3533 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3534 }
3535}
3536
cdalton231c5fd2015-05-13 12:35:36 -07003537void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003538 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003539 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003540 case kTexture_GrXferBarrierType: {
3541 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003542 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003543 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3544 // The render target uses separate storage so no need for glTextureBarrier.
3545 // FIXME: The render target will resolve automatically when its texture is bound,
3546 // but we could resolve only the bounds that will be read if we do it here instead.
3547 return;
3548 }
cdalton9954bc32015-04-29 14:17:00 -07003549 SkASSERT(this->caps()->textureBarrierSupport());
3550 GL_CALL(TextureBarrier());
3551 return;
cdalton231c5fd2015-05-13 12:35:36 -07003552 }
cdalton8917d622015-05-06 13:40:21 -07003553 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003554 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003555 this->caps()->blendEquationSupport());
3556 GL_CALL(BlendBarrier());
3557 return;
bsalomoncb02b382015-08-12 11:14:50 -07003558 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003559 }
3560}
3561
Chris Daltone1196c52019-12-28 14:31:09 -07003562void GrGLGpu::insertManualFramebufferBarrier() {
3563 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3564 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3565}
3566
Brian Salomon85c3d682019-11-04 15:04:54 -05003567GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003568 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003569 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003570 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003571 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003572 // We don't support protected textures in GL.
3573 if (isProtected == GrProtected::kYes) {
3574 return {};
3575 }
3576
Brian Salomon8a375832018-03-14 10:21:40 -04003577 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003578
Brian Salomond4764a12019-08-08 12:08:24 -04003579 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003580 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003581 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003582 }
3583
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003584 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003585 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003586 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003587 }
3588
Robert Phillipsd34691b2019-09-24 13:38:43 -04003589 // Compressed formats go through onCreateCompressedBackendTexture
3590 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3591
Greg Daniel15500642019-06-27 03:05:55 +00003592 GrGLTextureInfo info;
3593 GrGLTextureParameters::SamplerOverriddenState initialState;
3594
Greg Danield51fa2f2020-01-22 16:53:38 -05003595 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003596 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003597 }
Brian Salomon0f396992020-06-19 19:51:21 -04003598 switch (format.textureType()) {
3599 case GrTextureType::kNone:
3600 case GrTextureType::kExternal:
3601 return {};
3602 case GrTextureType::k2D:
3603 info.fTarget = GR_GL_TEXTURE_2D;
3604 break;
3605 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003606 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003607 return {};
3608 }
3609 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3610 break;
3611 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003612 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003613 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3614 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003615 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003616 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003617 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003618
Brian Salomon85c3d682019-11-04 15:04:54 -05003619 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003620 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003621
Brian Salomone2826ab2019-06-04 15:58:31 -04003622 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003623 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003624 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3625 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003626
Brian Salomon85c3d682019-11-04 15:04:54 -05003627 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3628 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003629}
3630
Greg Daniel16032b32020-05-06 15:31:10 -04003631bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3632 sk_sp<GrRefCntedCallback> finishedCallback,
3633 const BackendTextureData* data) {
3634 GrGLTextureInfo info;
3635 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3636
3637 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003638 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003639 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003640 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003641 }
3642
3643 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3644
Brian Salomon22558932020-05-15 14:46:34 -04003645 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003646
Brian Salomonc3f49432020-06-03 10:21:50 -04003647 // 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 -04003648 // so that the uploads go to the right levels.
Brian Salomon69100f02020-07-21 10:49:25 -04003649 if (numMipLevels && this->glCaps().mipmapLevelAndLodControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003650 auto params = backendTexture.getGLTextureParams();
3651 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3652 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003653 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003654 nonsamplerState.fBaseMipMapLevel = 0;
3655 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003656 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003657 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003658 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3659 }
3660 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3661 }
3662
Greg Daniel16032b32020-05-06 15:31:10 -04003663 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003664 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003665 if (data->type() == BackendTextureData::Type::kPixmaps) {
3666 SkTDArray<GrMipLevel> texels;
3667 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3668 texels.append(numMipLevels);
3669 for (int i = 0; i < numMipLevels; ++i) {
3670 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3671 }
Brian Salomon22558932020-05-15 14:46:34 -04003672 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3673 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3674 info.fTarget, dstRect, colorType, texels.begin(),
3675 texels.count());
3676 } else if (data->type() == BackendTextureData::Type::kColor) {
3677 uint32_t levelMask = (1 << numMipLevels) - 1;
3678 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3679 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003680 }
3681
3682 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003683 this->bindTextureToScratchUnit(info.fTarget, 0);
3684 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003685}
3686
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003687void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003688 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3689
3690 GrGLTextureInfo info;
3691 if (tex.getGLTextureInfo(&info)) {
3692 GL_CALL(DeleteTextures(1, &info.fID));
3693 }
3694}
3695
Robert Phillips3bce1f72020-05-12 12:41:58 -04003696bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3697 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3698
3699 Stats::ProgramCacheResult stat;
3700
3701 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3702 if (!tmp) {
3703 return false;
3704 }
3705
3706 return stat != Stats::ProgramCacheResult::kHit;
3707}
3708
Robert Phillipsf0ced622019-05-16 09:06:25 -04003709#if GR_TEST_UTILS
3710
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003711bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003712 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003713
Greg Daniel52e16d92018-04-10 09:34:07 -04003714 GrGLTextureInfo info;
3715 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003716 return false;
3717 }
3718
3719 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003720 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003721
3722 return (GR_GL_TRUE == result);
3723}
3724
Brian Salomonf865b052018-03-09 09:01:53 -05003725GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003726 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003727 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3728 return GrBackendRenderTarget(); // invalid
3729 }
Brian Salomon8a375832018-03-14 10:21:40 -04003730 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003731 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003732 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003733 return {};
3734 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003735 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003736 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003737 if (sFormatIdx < 0) {
3738 return {};
3739 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003740 GrGLuint colorID = 0;
3741 GrGLuint stencilID = 0;
3742 auto deleteIDs = [&] {
3743 if (colorID) {
3744 if (useTexture) {
3745 GL_CALL(DeleteTextures(1, &colorID));
3746 } else {
3747 GL_CALL(DeleteRenderbuffers(1, &colorID));
3748 }
Brian Salomonf865b052018-03-09 09:01:53 -05003749 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003750 if (stencilID) {
3751 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003752 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003753 };
3754
3755 if (useTexture) {
3756 GL_CALL(GenTextures(1, &colorID));
3757 } else {
3758 GL_CALL(GenRenderbuffers(1, &colorID));
3759 }
3760 GL_CALL(GenRenderbuffers(1, &stencilID));
3761 if (!stencilID || !colorID) {
3762 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003763 return {};
3764 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003765
Brian Salomonf865b052018-03-09 09:01:53 -05003766 GrGLFramebufferInfo info;
3767 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003768 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003769 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3770 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003771 deleteIDs();
3772 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003773 }
3774
3775 this->invalidateBoundRenderTarget();
3776
Adrienne Walker4ee88512018-05-17 11:37:14 -07003777 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003778 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003779 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon0f396992020-06-19 19:51:21 -04003780 colorID = this->createTexture({w, h}, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
3781 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003782 if (!colorID) {
3783 deleteIDs();
3784 return {};
3785 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003786 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3787 colorID, 0));
3788 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003789 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003790 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003791 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003792 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3793 GR_GL_RENDERBUFFER, colorID));
3794 }
3795 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003796 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003797 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003798 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003799 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003800 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3801 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003802 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003803 }
3804
Brian Salomon93348dd2018-08-29 12:56:23 -04003805 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3806 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3807 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3808 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003809 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003810 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003811
Adrienne Walker4ee88512018-05-17 11:37:14 -07003812 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003813 GrGLenum status;
3814 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3815 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003816 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003817 return {};
3818 }
3819 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003820
Greg Daniel108bb232018-07-03 16:18:29 -04003821 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003822 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003823 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003824}
3825
3826void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003827 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003828 GrGLFramebufferInfo info;
3829 if (backendRT.getGLFramebufferInfo(&info)) {
3830 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003831 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003832 }
3833 }
joshualitt8fd844f2015-12-02 13:36:47 -08003834}
3835
Greg Daniel26b50a42018-03-08 09:49:58 -05003836void GrGLGpu::testingOnly_flushGpuAndSync() {
3837 GL_CALL(Finish());
3838}
Brian Salomonf865b052018-03-09 09:01:53 -05003839#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003840
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003841///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003842
cdaltone2e71c22016-04-07 18:13:29 -07003843GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003844 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003845 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003846 GrGLAttribArrayState* attribState;
3847
cdaltone2e71c22016-04-07 18:13:29 -07003848 if (gpu->glCaps().isCoreProfile()) {
3849 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003850 GrGLuint arrayID;
3851 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3852 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003853 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003854 }
cdaltone2e71c22016-04-07 18:13:29 -07003855 if (ibuf) {
3856 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003857 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003858 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003859 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003860 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003861 if (ibuf) {
3862 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003863 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003864 } else {
3865 this->setVertexArrayID(gpu, 0);
3866 }
3867 int attrCount = gpu->glCaps().maxVertexAttributes();
3868 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3869 fDefaultVertexArrayAttribState.resize(attrCount);
3870 }
3871 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003872 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003873 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003874}
bsalomone179a912016-01-20 06:18:10 -08003875
Greg Danielfe159622020-04-10 17:43:51 +00003876void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3877 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003878 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003879}
3880
Greg Daniel78be37f2020-04-14 16:40:35 -04003881void GrGLGpu::flush(FlushType flushType) {
3882 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3883 GL_CALL(Flush());
3884 fNeedsGLFlush = false;
3885 }
3886}
3887
Greg Danielfe159622020-04-10 17:43:51 +00003888bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3889 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003890 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003891 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003892 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003893 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003894 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003895 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003896 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003897 if (!this->glCaps().skipErrorChecks()) {
3898 this->clearErrorsAndCheckForOOM();
3899 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003900 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003901}
3902
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003903void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3904 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3905 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3906 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003907}
3908
Greg Daniel6be35232017-03-01 17:01:09 -05003909GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003910 if (!this->caps()->fenceSyncSupport()) {
3911 return 0;
3912 }
Greg Daniel6be35232017-03-01 17:01:09 -05003913 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003914 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3915 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3916 GrGLuint fence = 0;
3917 GL_CALL(GenFences(1, &fence));
3918 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3919 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3920 } else {
3921 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3922 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003923 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003924 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003925 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003926}
3927
Brian Salomonb0d8b762019-05-06 16:58:22 -04003928bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003929 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3930 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3931 if (!timeout) {
3932 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003933 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003934 }
3935 GrGLboolean result;
3936 GL_CALL_RET(result, TestFence(nvFence));
3937 return result == GR_GL_TRUE;
3938 }
3939 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3940 // If this really becomes necessary we could poll TestFence().
3941 // FinishFence always flushes so no need to check flush param.
3942 GL_CALL(FinishFence(nvFence));
3943 return true;
3944 } else {
3945 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3946 GrGLenum result;
3947 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3948 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3949 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003950}
3951
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003952bool GrGLGpu::waitFence(GrFence fence) {
3953 if (!this->caps()->fenceSyncSupport()) {
3954 return true;
3955 }
3956 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003957}
3958
3959void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003960 if (this->caps()->fenceSyncSupport()) {
3961 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3962 }
Greg Daniel6be35232017-03-01 17:01:09 -05003963}
3964
Greg Daniel301015c2019-11-18 14:06:46 -05003965std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003966 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003967 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003968}
3969
Greg Daniel301015c2019-11-18 14:06:46 -05003970std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3971 const GrBackendSemaphore& semaphore,
3972 GrResourceProvider::SemaphoreWrapType wrapType,
3973 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003974 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003975 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3976}
3977
Greg Daniel301015c2019-11-18 14:06:46 -05003978void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003979 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003980 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003981
Greg Daniel48661b82018-01-22 16:11:35 -05003982 GrGLsync sync;
3983 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3984 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003985 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003986}
3987
Greg Daniel301015c2019-11-18 14:06:46 -05003988void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003989 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003990 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003991
3992 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
3993}
3994
Brian Salomonb0d8b762019-05-06 16:58:22 -04003995void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003996 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003997}
3998
Brian Salomon24069eb2020-06-24 10:19:52 -04003999void GrGLGpu::clearErrorsAndCheckForOOM() {
4000 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4001}
4002
4003GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4004#if GR_GL_CHECK_ERROR
4005 if (this->glInterface()->checkAndResetOOMed()) {
4006 this->setOOMed();
4007 }
4008#endif
4009 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4010 if (error == GR_GL_OUT_OF_MEMORY) {
4011 this->setOOMed();
4012 }
4013 return error;
4014}
4015
Greg Daniel6be35232017-03-01 17:01:09 -05004016void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004017 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4018 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4019 GL_CALL(DeleteFences(1, &nvFence));
4020 } else {
4021 GL_CALL(DeleteSync(sync));
4022 }
jvanverth84741b32016-09-30 08:39:02 -07004023}
Brian Osman13dddce2017-05-09 13:19:50 -04004024
Greg Daniel301015c2019-11-18 14:06:46 -05004025std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004026 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004027 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004028 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004029 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004030 // 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 -04004031 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004032
4033 return semaphore;
4034}
Robert Phillips646e4292017-06-13 12:44:56 -04004035
4036int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004037 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004038 case kTexture2DSampler_GrSLType:
4039 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004040 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004041 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004042 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004043 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004044 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004045 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004046 }
4047}
Brian Osman71a18892017-08-10 10:23:25 -04004048
Kevin Lubickf4def342018-10-04 12:52:50 -04004049#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004050#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004051void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4052 // We are called by the base class, which has already called beginObject(). We choose to nest
4053 // all of our caps information in a named sub-object.
4054 writer->beginObject("GL GPU");
4055
4056 const GrGLubyte* str;
4057 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4058 writer->appendString("GL_VERSION", (const char*)(str));
4059 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4060 writer->appendString("GL_RENDERER", (const char*)(str));
4061 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4062 writer->appendString("GL_VENDOR", (const char*)(str));
4063 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4064 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4065
4066 writer->appendName("extensions");
4067 glInterface()->fExtensions.dumpJSON(writer);
4068
4069 writer->endObject();
4070}
Kevin Lubickf4def342018-10-04 12:52:50 -04004071#endif