blob: 768aeeff26c00ee7cf92589286acd6f8cd6add98 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
Brian Salomon4cfae3b2020-07-23 10:33:24 -04008#include "src/gpu/gl/GrGLGpu.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPixmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/gpu/GrBackendSemaphore.h"
13#include "include/gpu/GrBackendSurface.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrTypes.h"
16#include "include/private/SkHalf.h"
17#include "include/private/SkTemplates.h"
18#include "include/private/SkTo.h"
19#include "src/core/SkAutoMalloc.h"
Robert Phillips99dead92020-01-27 16:11:57 -050020#include "src/core/SkCompressedDataUtils.h"
Mike Reed13711eb2020-07-14 17:16:32 -040021#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkTraceEvent.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040023#include "src/gpu/GrBackendUtils.h"
Brian Osman8518f2e2019-05-01 14:13:41 -040024#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrCpuBuffer.h"
Robert Phillips459b2952019-05-23 09:38:27 -040026#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrPipeline.h"
Robert Phillips901aff02019-10-08 12:32:56 -040029#include "src/gpu/GrProgramInfo.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040030#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrShaderCaps.h"
32#include "src/gpu/GrSurfaceProxyPriv.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040033#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/gl/GrGLBuffer.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035#include "src/gpu/gl/GrGLOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/gl/GrGLSemaphore.h"
37#include "src/gpu/gl/GrGLStencilAttachment.h"
38#include "src/gpu/gl/GrGLTextureRenderTarget.h"
39#include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
40#include "src/sksl/SkSLCompiler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000041
Hal Canaryc640d0d2018-06-13 09:59:02 -040042#include <cmath>
John Stilesfbd050b2020-08-03 13:21:46 -040043#include <memory>
Hal Canaryc640d0d2018-06-13 09:59:02 -040044
bsalomon@google.com0b77d682011-08-19 13:28:54 +000045#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000046#define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000047
Brian Salomond8575452020-02-25 12:13:29 -050048#define GL_ALLOC_CALL(call) \
49 [&] { \
50 if (this->glCaps().skipErrorChecks()) { \
51 GR_GL_CALL(this->glInterface(), call); \
52 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
53 } else { \
Brian Salomon24069eb2020-06-24 10:19:52 -040054 this->clearErrorsAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050055 GR_GL_CALL_NOERRCHECK(this->glInterface(), call); \
Brian Salomon24069eb2020-06-24 10:19:52 -040056 return this->getErrorAndCheckForOOM(); \
Brian Salomond8575452020-02-25 12:13:29 -050057 } \
58 }()
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000059
Jim Van Verth32ac83e2016-11-28 15:23:57 -050060//#define USE_NSIGHT
61
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000062///////////////////////////////////////////////////////////////////////////////
63
cdalton8917d622015-05-06 13:40:21 -070064static const GrGLenum gXfermodeEquation2Blend[] = {
65 // Basic OpenGL blend equations.
66 GR_GL_FUNC_ADD,
67 GR_GL_FUNC_SUBTRACT,
68 GR_GL_FUNC_REVERSE_SUBTRACT,
69
70 // GL_KHR_blend_equation_advanced.
71 GR_GL_SCREEN,
72 GR_GL_OVERLAY,
73 GR_GL_DARKEN,
74 GR_GL_LIGHTEN,
75 GR_GL_COLORDODGE,
76 GR_GL_COLORBURN,
77 GR_GL_HARDLIGHT,
78 GR_GL_SOFTLIGHT,
79 GR_GL_DIFFERENCE,
80 GR_GL_EXCLUSION,
81 GR_GL_MULTIPLY,
82 GR_GL_HSL_HUE,
83 GR_GL_HSL_SATURATION,
84 GR_GL_HSL_COLOR,
Mike Klein36743362018-11-06 08:23:30 -050085 GR_GL_HSL_LUMINOSITY,
86
87 // Illegal... needs to map to something.
88 GR_GL_FUNC_ADD,
cdalton8917d622015-05-06 13:40:21 -070089};
Brian Salomon4dea72a2019-12-18 10:43:10 -050090static_assert(0 == kAdd_GrBlendEquation);
91static_assert(1 == kSubtract_GrBlendEquation);
92static_assert(2 == kReverseSubtract_GrBlendEquation);
93static_assert(3 == kScreen_GrBlendEquation);
94static_assert(4 == kOverlay_GrBlendEquation);
95static_assert(5 == kDarken_GrBlendEquation);
96static_assert(6 == kLighten_GrBlendEquation);
97static_assert(7 == kColorDodge_GrBlendEquation);
98static_assert(8 == kColorBurn_GrBlendEquation);
99static_assert(9 == kHardLight_GrBlendEquation);
100static_assert(10 == kSoftLight_GrBlendEquation);
101static_assert(11 == kDifference_GrBlendEquation);
102static_assert(12 == kExclusion_GrBlendEquation);
103static_assert(13 == kMultiply_GrBlendEquation);
104static_assert(14 == kHSLHue_GrBlendEquation);
105static_assert(15 == kHSLSaturation_GrBlendEquation);
106static_assert(16 == kHSLColor_GrBlendEquation);
107static_assert(17 == kHSLLuminosity_GrBlendEquation);
108static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt);
cdalton8917d622015-05-06 13:40:21 -0700109
twiz@google.com0f31ca72011-03-18 17:38:11 +0000110static const GrGLenum gXfermodeCoeff2Blend[] = {
111 GR_GL_ZERO,
112 GR_GL_ONE,
113 GR_GL_SRC_COLOR,
114 GR_GL_ONE_MINUS_SRC_COLOR,
115 GR_GL_DST_COLOR,
116 GR_GL_ONE_MINUS_DST_COLOR,
117 GR_GL_SRC_ALPHA,
118 GR_GL_ONE_MINUS_SRC_ALPHA,
119 GR_GL_DST_ALPHA,
120 GR_GL_ONE_MINUS_DST_ALPHA,
121 GR_GL_CONSTANT_COLOR,
122 GR_GL_ONE_MINUS_CONSTANT_COLOR,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000123
124 // extended blend coeffs
125 GR_GL_SRC1_COLOR,
126 GR_GL_ONE_MINUS_SRC1_COLOR,
127 GR_GL_SRC1_ALPHA,
128 GR_GL_ONE_MINUS_SRC1_ALPHA,
Mike Klein36743362018-11-06 08:23:30 -0500129
130 // Illegal... needs to map to something.
131 GR_GL_ZERO,
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
Brian Salomond978b902019-02-07 15:09:18 -0500134//////////////////////////////////////////////////////////////////////////////
135
136static int gl_target_to_binding_index(GrGLenum target) {
137 switch (target) {
138 case GR_GL_TEXTURE_2D:
139 return 0;
140 case GR_GL_TEXTURE_RECTANGLE:
141 return 1;
142 case GR_GL_TEXTURE_EXTERNAL:
143 return 2;
144 }
145 SK_ABORT("Unexpected GL texture target.");
Brian Salomond978b902019-02-07 15:09:18 -0500146}
147
148GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const {
Brian Salomon1f05d452019-02-08 12:33:08 -0500149 return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID;
150}
151
152bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const {
153 return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified;
Brian Salomond978b902019-02-07 15:09:18 -0500154}
155
156void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500157 int targetIndex = gl_target_to_binding_index(target);
158 fTargetBindings[targetIndex].fBoundResourceID = resourceID;
159 fTargetBindings[targetIndex].fHasBeenModified = true;
Brian Salomond978b902019-02-07 15:09:18 -0500160}
161
Brian Salomon1f05d452019-02-08 12:33:08 -0500162void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) {
163 this->setBoundID(target, GrGpuResource::UniqueID());
Brian Salomond978b902019-02-07 15:09:18 -0500164}
165
Brian Salomon1f05d452019-02-08 12:33:08 -0500166void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) {
167 for (auto& targetBinding : fTargetBindings) {
168 targetBinding.fBoundResourceID.makeInvalid();
169 if (markUnmodified) {
170 targetBinding.fHasBeenModified = false;
171 }
Brian Salomond978b902019-02-07 15:09:18 -0500172 }
173}
174
175//////////////////////////////////////////////////////////////////////////////
176
Brian Salomondc829942018-10-23 16:07:24 -0400177static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) {
178 switch (filter) {
179 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST;
Brian Salomona3b02f52020-07-15 16:02:01 -0400180 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR;
Brian Salomondc829942018-10-23 16:07:24 -0400181 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400182 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400183}
184
Brian Salomone69b9ef2020-07-22 11:18:06 -0400185static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter,
186 GrSamplerState::MipmapMode mm) {
187 switch (mm) {
188 case GrSamplerState::MipmapMode::kNone:
189 return filter_to_gl_mag_filter(filter);
Brian Salomonf7353512020-07-22 19:26:48 -0400190 case GrSamplerState::MipmapMode::kNearest:
191 switch (filter) {
192 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_NEAREST;
193 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_NEAREST;
194 }
195 SkUNREACHABLE;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400196 case GrSamplerState::MipmapMode::kLinear:
197 switch (filter) {
198 case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST_MIPMAP_LINEAR;
199 case GrSamplerState::Filter::kLinear: return GR_GL_LINEAR_MIPMAP_LINEAR;
200 }
201 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400202 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400203 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400204}
205
Michael Ludwigf23a1522018-12-10 11:36:13 -0500206static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode,
207 const GrCaps& caps) {
Brian Salomondc829942018-10-23 16:07:24 -0400208 switch (wrapMode) {
209 case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE;
210 case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT;
211 case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT;
Michael Ludwigf23a1522018-12-10 11:36:13 -0500212 case GrSamplerState::WrapMode::kClampToBorder:
213 // May not be supported but should have been caught earlier
214 SkASSERT(caps.clampToBorderSupport());
215 return GR_GL_CLAMP_TO_BORDER;
Brian Salomon23356442018-11-30 15:33:19 -0500216 }
Brian Salomone69b9ef2020-07-22 11:18:06 -0400217 SkUNREACHABLE;
Brian Salomondc829942018-10-23 16:07:24 -0400218}
219
220///////////////////////////////////////////////////////////////////////////////
221
222class GrGLGpu::SamplerObjectCache {
223public:
224 SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) {
225 fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers();
226 fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]);
227 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
228 std::fill_n(fSamplers, kNumSamplers, 0);
229 }
230
231 ~SamplerObjectCache() {
232 if (!fNumTextureUnits) {
233 // We've already been abandoned.
234 return;
235 }
Chris Dalton703fe682019-05-15 12:58:12 -0600236 for (GrGLuint sampler : fSamplers) {
237 // The spec states that "zero" values should be silently ignored, however they still
238 // trigger GL errors on some NVIDIA platforms.
239 if (sampler) {
240 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler));
241 }
242 }
Brian Salomondc829942018-10-23 16:07:24 -0400243 }
244
Brian Salomonccb61422020-01-09 10:46:36 -0500245 void bindSampler(int unitIdx, GrSamplerState state) {
Brian Salomona7d9f302020-07-15 13:25:30 -0400246 int index = state.asIndex();
Brian Salomondc829942018-10-23 16:07:24 -0400247 if (!fSamplers[index]) {
248 GrGLuint s;
249 GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s));
250 if (!s) {
251 return;
252 }
253 fSamplers[index] = s;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400254 GrGLenum minFilter = filter_to_gl_min_filter(state.filter(), state.mipmapMode());
255 GrGLenum magFilter = filter_to_gl_mag_filter(state.filter());
256 GrGLenum wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps());
257 GrGLenum wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -0400258 GR_GL_CALL(fGpu->glInterface(),
259 SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter));
260 GR_GL_CALL(fGpu->glInterface(),
261 SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter));
262 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX));
263 GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY));
264 }
265 if (fHWBoundSamplers[unitIdx] != fSamplers[index]) {
266 GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index]));
267 fHWBoundSamplers[unitIdx] = fSamplers[index];
268 }
269 }
270
271 void invalidateBindings() {
272 // When we have sampler support we always use samplers. So setting these to zero will cause
273 // a rebind on next usage.
274 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
275 }
276
277 void abandon() {
278 fHWBoundSamplers.reset();
279 fNumTextureUnits = 0;
280 }
281
282 void release() {
283 if (!fNumTextureUnits) {
284 // We've already been abandoned.
285 return;
286 }
287 GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers));
288 std::fill_n(fSamplers, kNumSamplers, 0);
289 // Deleting a bound sampler implicitly binds sampler 0.
290 std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0);
291 }
292
293private:
Brian Salomona7d9f302020-07-15 13:25:30 -0400294 static constexpr int kNumSamplers = GrSamplerState::kNumUniqueSamplers;
Brian Salomondc829942018-10-23 16:07:24 -0400295 GrGLGpu* fGpu;
Brian Salomondc829942018-10-23 16:07:24 -0400296 std::unique_ptr<GrGLuint[]> fHWBoundSamplers;
297 GrGLuint fSamplers[kNumSamplers];
298 int fNumTextureUnits;
299};
300
reed@google.comac10a2d2010-12-22 21:39:39 +0000301///////////////////////////////////////////////////////////////////////////////
302
Brian Salomon384fab42017-12-07 12:33:05 -0500303sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options,
Adlai Holler3d0359a2020-07-09 15:35:55 -0400304 GrDirectContext* direct) {
Brian Salomon384fab42017-12-07 12:33:05 -0500305 if (!interface) {
Brian Salomon3d6801e2017-12-11 10:06:31 -0500306 interface = GrGLMakeNativeInterface();
307 // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated
308 // to GrGLMakeNativeInterface.
309 if (!interface) {
310 interface = sk_ref_sp(GrGLCreateNativeInterface());
311 }
Brian Salomon384fab42017-12-07 12:33:05 -0500312 if (!interface) {
313 return nullptr;
314 }
bsalomon424cc262015-05-22 10:37:30 -0700315 }
Jim Van Verth76334772017-05-05 16:46:05 -0400316#ifdef USE_NSIGHT
317 const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
318#endif
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500319 auto glContext = GrGLContext::Make(std::move(interface), options);
320 if (!glContext) {
321 return nullptr;
bsalomon424cc262015-05-22 10:37:30 -0700322 }
Adlai Holler3d0359a2020-07-09 15:35:55 -0400323 return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), direct));
bsalomon424cc262015-05-22 10:37:30 -0700324}
325
Adlai Holler3d0359a2020-07-09 15:35:55 -0400326GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrDirectContext* direct)
327 : GrGpu(direct)
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500328 , fGLContext(std::move(ctx))
329 , fProgramCache(new ProgramCache(this))
330 , fHWProgramID(0)
331 , fTempSrcFBOID(0)
332 , fTempDstFBOID(0)
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400333 , fStencilClearFBOID(0)
334 , fFinishCallbacks(this) {
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500335 SkASSERT(fGLContext);
Brian Salomon24069eb2020-06-24 10:19:52 -0400336 // Clear errors so we don't get confused whether we caused an error.
337 this->clearErrorsAndCheckForOOM();
338 // Toss out any pre-existing OOM that was hanging around before we got started.
339 this->checkAndResetOOMed();
340
Brian Salomon8ab1cc42017-12-07 12:40:00 -0500341 fCaps = sk_ref_sp(fGLContext->caps());
bsalomon@google.combcce8922013-03-25 15:38:39 +0000342
Brian Salomond978b902019-02-07 15:09:18 -0500343 fHWTextureUnitBindings.reset(this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000344
Brian Salomonae64c192019-02-05 09:41:37 -0500345 this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER;
346 this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER;
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600347 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->fGLTarget = GR_GL_DRAW_INDIRECT_BUFFER;
Brian Salomon988f1092020-01-21 15:01:59 -0500348 if (GrGLCaps::TransferBufferType::kChromium == this->glCaps().transferBufferType()) {
Brian Salomonae64c192019-02-05 09:41:37 -0500349 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget =
350 GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM;
351 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget =
352 GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM;
cdaltone2e71c22016-04-07 18:13:29 -0700353 } else {
Brian Salomonae64c192019-02-05 09:41:37 -0500354 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER;
355 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER;
cdaltone2e71c22016-04-07 18:13:29 -0700356 }
Brian Salomonae64c192019-02-05 09:41:37 -0500357 for (int i = 0; i < kGrGpuBufferTypeCount; ++i) {
Rob Phillipsbc534f62017-09-05 19:56:19 -0400358 fHWBufferState[i].invalidate();
359 }
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600360 static_assert(kGrGpuBufferTypeCount == SK_ARRAY_COUNT(fHWBufferState));
cdaltone2e71c22016-04-07 18:13:29 -0700361
362 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400363 fPathRendering = std::make_unique<GrGLPathRendering>(this);
cdaltone2e71c22016-04-07 18:13:29 -0700364 }
365
Brian Salomondc829942018-10-23 16:07:24 -0400366 if (this->glCaps().samplerObjectSupport()) {
John Stilesfbd050b2020-08-03 13:21:46 -0400367 fSamplerObjectCache = std::make_unique<SamplerObjectCache>(this);
Brian Salomondc829942018-10-23 16:07:24 -0400368 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000369}
370
bsalomon861e1032014-12-16 07:33:49 -0800371GrGLGpu::~GrGLGpu() {
cdaltone2e71c22016-04-07 18:13:29 -0700372 // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu
373 // to release the resources held by the objects themselves.
kkinnunen702501d2016-01-13 23:36:45 -0800374 fPathRendering.reset();
cdaltone2e71c22016-04-07 18:13:29 -0700375 fCopyProgramArrayBuffer.reset();
brianosman33f6b3f2016-06-02 05:49:21 -0700376 fMipmapProgramArrayBuffer.reset();
kkinnunen702501d2016-01-13 23:36:45 -0800377
Chris Dalton20e7a532020-02-28 15:37:53 +0000378 fHWProgram.reset();
Brian Salomon43f8bf02017-10-18 08:33:29 -0400379 if (fHWProgramID) {
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000380 // detach the current program so there is no confusion on OpenGL's part
381 // that we want it to be deleted
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000382 GL_CALL(UseProgram(0));
383 }
384
Brian Salomon43f8bf02017-10-18 08:33:29 -0400385 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700386 this->deleteFramebuffer(fTempSrcFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800387 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400388 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700389 this->deleteFramebuffer(fTempDstFBOID);
egdaniel0f5f9672015-02-03 11:10:51 -0800390 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400391 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700392 this->deleteFramebuffer(fStencilClearFBOID);
bsalomondd3143b2015-02-23 09:27:45 -0800393 }
egdaniel0f5f9672015-02-03 11:10:51 -0800394
bsalomon7ea33f52015-11-22 14:51:00 -0800395 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
396 if (0 != fCopyPrograms[i].fProgram) {
397 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
398 }
bsalomon6df86402015-06-01 10:41:49 -0700399 }
bsalomon6dea83f2015-12-03 12:58:06 -0800400
brianosman33f6b3f2016-06-02 05:49:21 -0700401 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
402 if (0 != fMipmapPrograms[i].fProgram) {
403 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
404 }
405 }
406
Brian Salomondc829942018-10-23 16:07:24 -0400407 fSamplerObjectCache.reset();
Greg Daniel822f7b22020-02-21 14:41:36 -0500408
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400409 fFinishCallbacks.callAll(true);
bsalomonc8dc1f72014-08-21 13:02:13 -0700410}
411
bsalomon6e2aad42016-04-01 11:54:31 -0700412void GrGLGpu::disconnect(DisconnectType type) {
413 INHERITED::disconnect(type);
414 if (DisconnectType::kCleanup == type) {
415 if (fHWProgramID) {
416 GL_CALL(UseProgram(0));
417 }
418 if (fTempSrcFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700419 this->deleteFramebuffer(fTempSrcFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700420 }
421 if (fTempDstFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700422 this->deleteFramebuffer(fTempDstFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700423 }
424 if (fStencilClearFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700425 this->deleteFramebuffer(fStencilClearFBOID);
bsalomon6e2aad42016-04-01 11:54:31 -0700426 }
bsalomon6e2aad42016-04-01 11:54:31 -0700427 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
428 if (fCopyPrograms[i].fProgram) {
429 GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram));
430 }
431 }
brianosman33f6b3f2016-06-02 05:49:21 -0700432 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
433 if (fMipmapPrograms[i].fProgram) {
434 GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram));
435 }
436 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400437
Brian Salomondc829942018-10-23 16:07:24 -0400438 if (fSamplerObjectCache) {
439 fSamplerObjectCache->release();
440 }
bsalomon6e2aad42016-04-01 11:54:31 -0700441 } else {
442 if (fProgramCache) {
443 fProgramCache->abandon();
444 }
Brian Salomondc829942018-10-23 16:07:24 -0400445 if (fSamplerObjectCache) {
446 fSamplerObjectCache->abandon();
447 }
bsalomon6e2aad42016-04-01 11:54:31 -0700448 }
449
Chris Dalton20e7a532020-02-28 15:37:53 +0000450 fHWProgram.reset();
Robert Phillips1daa2392020-02-18 14:34:36 -0500451 fProgramCache.reset();
bsalomon6e2aad42016-04-01 11:54:31 -0700452
bsalomonc8dc1f72014-08-21 13:02:13 -0700453 fHWProgramID = 0;
egdanield803f272015-03-18 13:01:52 -0700454 fTempSrcFBOID = 0;
455 fTempDstFBOID = 0;
456 fStencilClearFBOID = 0;
cdaltone2e71c22016-04-07 18:13:29 -0700457 fCopyProgramArrayBuffer.reset();
bsalomon7ea33f52015-11-22 14:51:00 -0800458 for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) {
459 fCopyPrograms[i].fProgram = 0;
460 }
brianosman33f6b3f2016-06-02 05:49:21 -0700461 fMipmapProgramArrayBuffer.reset();
462 for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) {
463 fMipmapPrograms[i].fProgram = 0;
464 }
Brian Salomon43f8bf02017-10-18 08:33:29 -0400465
jvanverthe9c0fc62015-04-29 11:18:05 -0700466 if (this->glCaps().shaderCaps()->pathRenderingSupport()) {
bsalomon6e2aad42016-04-01 11:54:31 -0700467 this->glPathRendering()->disconnect(type);
bsalomonc8dc1f72014-08-21 13:02:13 -0700468 }
Stephen Whiteb353c9b2020-04-16 14:14:13 -0400469 fFinishCallbacks.callAll(DisconnectType::kCleanup == type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000470}
471
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000472///////////////////////////////////////////////////////////////////////////////
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000473
bsalomon861e1032014-12-16 07:33:49 -0800474void GrGLGpu::onResetContext(uint32_t resetBits) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000475 if (resetBits & kMisc_GrGLBackendState) {
Brian Salomonf0861672017-05-08 11:10:10 -0400476 // we don't use the zb at all
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000477 GL_CALL(Disable(GR_GL_DEPTH_TEST));
478 GL_CALL(DepthMask(GR_GL_FALSE));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000479
Brian Salomonf0861672017-05-08 11:10:10 -0400480 // We don't use face culling.
481 GL_CALL(Disable(GR_GL_CULL_FACE));
Chris Daltonc8ece3d2018-07-30 15:03:45 -0600482 // We do use separate stencil. Our algorithms don't care which face is front vs. back so
483 // just set this to the default for self-consistency.
484 GL_CALL(FrontFace(GR_GL_CCW));
Brian Salomonf0861672017-05-08 11:10:10 -0400485
Brian Salomonae64c192019-02-05 09:41:37 -0500486 this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate();
487 this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate();
cdaltonc1613102016-03-16 07:48:20 -0700488
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400489 if (GR_IS_GR_GL(this->glStandard())) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500490#ifndef USE_NSIGHT
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000491 // Desktop-only state that we never change
492 if (!this->glCaps().isCoreProfile()) {
493 GL_CALL(Disable(GR_GL_POINT_SMOOTH));
494 GL_CALL(Disable(GR_GL_LINE_SMOOTH));
495 GL_CALL(Disable(GR_GL_POLYGON_SMOOTH));
496 GL_CALL(Disable(GR_GL_POLYGON_STIPPLE));
497 GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP));
498 GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP));
499 }
500 // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a
501 // core profile. This seems like a bug since the core spec removes any mention of
502 // GL_ARB_imaging.
503 if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) {
504 GL_CALL(Disable(GR_GL_COLOR_TABLE));
505 }
506 GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL));
Jim Van Verth609e7cc2017-03-30 14:28:08 -0400507
Chris Dalton1215cda2019-12-17 21:44:04 -0700508 fHWWireframeEnabled = kUnknown_TriState;
Jim Van Verth32ac83e2016-11-28 15:23:57 -0500509#endif
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000510 // Since ES doesn't support glPointSize at all we always use the VS to
511 // set the point size
512 GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE));
513
bsalomon@google.com2b1b8c02013-02-28 22:06:02 +0000514 }
joshualitt58162332014-08-01 06:44:53 -0700515
Kevin Lubick8aa203c2019-03-19 13:23:10 -0400516 if (GR_IS_GR_GL_ES(this->glStandard()) &&
Brian Salomon8bc352c2019-01-28 09:05:22 -0500517 this->glCaps().fbFetchRequiresEnablePerSample()) {
joshualitt58162332014-08-01 06:44:53 -0700518 // The arm extension requires specifically enabling MSAA fetching per sample.
519 // On some devices this may have a perf hit. Also multiple render targets are disabled
Brian Salomon8bc352c2019-01-28 09:05:22 -0500520 GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE));
joshualitt58162332014-08-01 06:44:53 -0700521 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000522 fHWWriteToColor = kUnknown_TriState;
523 // we only ever use lines in hairline mode
524 GL_CALL(LineWidth(1));
bsalomonaca31fe2015-09-22 11:38:46 -0700525 GL_CALL(Disable(GR_GL_DITHER));
Brian Salomon805cc7a2019-01-28 09:52:34 -0500526
527 fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN;
bsalomon@google.comcad107b2013-06-28 14:32:08 +0000528 }
edisonn@google.comba669992013-06-28 16:03:21 +0000529
egdanielb414f252014-07-29 13:15:47 -0700530 if (resetBits & kMSAAEnable_GrGLBackendState) {
531 fMSAAEnabled = kUnknown_TriState;
vbuzinovdded6962015-06-12 08:59:45 -0700532
Chris Dalton6ce447a2019-06-23 18:07:38 -0600533 if (this->caps()->mixedSamplesSupport()) {
cdaltonaf8bc7d2016-02-05 09:35:20 -0800534 // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage
535 // modulation. This state has no effect when not rendering to a mixed sampled target.
vbuzinovdded6962015-06-12 08:59:45 -0700536 GL_CALL(CoverageModulation(GR_GL_RGBA));
537 }
Chris Daltonce425af2019-12-16 10:39:03 -0700538
539 fHWConservativeRasterEnabled = kUnknown_TriState;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000540 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000541
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000542 fHWActiveTextureUnitIdx = -1; // invalid
Brian Salomonaf971de2017-06-08 16:11:33 -0400543 fLastPrimitiveType = static_cast<GrPrimitiveType>(-1);
commit-bot@chromium.org46fbfe02013-08-30 15:52:12 +0000544
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000545 if (resetBits & kTextureBinding_GrGLBackendState) {
Brian Salomond978b902019-02-07 15:09:18 -0500546 for (int s = 0; s < this->numTextureUnits(); ++s) {
Brian Salomon1f05d452019-02-08 12:33:08 -0500547 fHWTextureUnitBindings[s].invalidateAllTargets(false);
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000548 }
Brian Salomondc829942018-10-23 16:07:24 -0400549 if (fSamplerObjectCache) {
550 fSamplerObjectCache->invalidateBindings();
551 }
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000552 }
bsalomon@google.com316f99232011-01-13 21:28:12 +0000553
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000554 if (resetBits & kBlend_GrGLBackendState) {
555 fHWBlendState.invalidate();
556 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000557
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000558 if (resetBits & kView_GrGLBackendState) {
559 fHWScissorSettings.invalidate();
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700560 fHWWindowRectsState.invalidate();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000561 fHWViewport.invalidate();
562 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000564 if (resetBits & kStencil_GrGLBackendState) {
565 fHWStencilSettings.invalidate();
566 fHWStencilTestEnabled = kUnknown_TriState;
567 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000568
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000569 // Vertex
570 if (resetBits & kVertex_GrGLBackendState) {
cdaltone2e71c22016-04-07 18:13:29 -0700571 fHWVertexArrayState.invalidate();
Brian Salomonae64c192019-02-05 09:41:37 -0500572 this->hwBufferState(GrGpuBufferType::kVertex)->invalidate();
573 this->hwBufferState(GrGpuBufferType::kIndex)->invalidate();
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600574 this->hwBufferState(GrGpuBufferType::kDrawIndirect)->invalidate();
Chris Dalton5a2f9622019-12-27 14:56:38 -0700575 fHWPatchVertexCount = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000576 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000577
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000578 if (resetBits & kRenderTarget_GrGLBackendState) {
Robert Phillips294870f2016-11-11 12:38:40 -0500579 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon16921ec2015-07-30 15:34:56 -0700580 fHWSRGBFramebuffer = kUnknown_TriState;
Brian Salomon3aef3012020-02-27 15:35:02 -0500581 fBoundDrawFramebuffer = 0;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000582 }
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000583
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000584 if (resetBits & kPathRendering_GrGLBackendState) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700585 if (this->caps()->shaderCaps()->pathRenderingSupport()) {
kkinnunenccdaa042014-08-20 01:36:23 -0700586 this->glPathRendering()->resetContext();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000587 }
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000588 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000589
bsalomon@google.com8ef3fd02011-11-21 15:53:13 +0000590 // we assume these values
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000591 if (resetBits & kPixelStore_GrGLBackendState) {
Brian Salomon1047a492019-07-02 12:25:21 -0400592 if (this->caps()->writePixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000593 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
594 }
Brian Salomon1047a492019-07-02 12:25:21 -0400595 if (this->glCaps().readPixelsRowBytesSupport()) {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000596 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
597 }
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000598 if (this->glCaps().packFlipYSupport()) {
599 GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE));
600 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000601 }
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000602
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000603 if (resetBits & kProgram_GrGLBackendState) {
604 fHWProgramID = 0;
Chris Dalton20e7a532020-02-28 15:37:53 +0000605 fHWProgram.reset();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000606 }
Brian Salomone2826ab2019-06-04 15:58:31 -0400607 ++fResetTimestampForTextureParameters;
reed@google.comac10a2d2010-12-22 21:39:39 +0000608}
609
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400610static bool check_backend_texture(const GrBackendTexture& backendTex,
611 const GrGLCaps& caps,
612 GrGLTexture::Desc* desc,
Brian Salomonea4ad302019-08-07 13:04:55 -0400613 bool skipRectTexSupportCheck = false) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400614 GrGLTextureInfo info;
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400615 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
Brian Salomond17f6582017-07-19 18:28:58 -0400616 return false;
bsalomon091f60c2015-11-10 11:54:56 -0800617 }
robertphillips@google.comb72e5d32012-10-30 15:18:10 +0000618
Brian Salomonea4ad302019-08-07 13:04:55 -0400619 desc->fSize = {backendTex.width(), backendTex.height()};
620 desc->fTarget = info.fTarget;
621 desc->fID = info.fID;
622 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
bsalomon7ea33f52015-11-22 14:51:00 -0800623
Brian Salomonea4ad302019-08-07 13:04:55 -0400624 if (desc->fFormat == GrGLFormat::kUnknown) {
625 return false;
626 }
627 if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400628 if (!caps.shaderCaps()->externalTextureSupport()) {
629 return false;
630 }
Brian Salomonf04fb442019-08-08 16:06:29 -0400631 } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) {
632 if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) {
Brian Salomond17f6582017-07-19 18:28:58 -0400633 return false;
634 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400635 } else if (GR_GL_TEXTURE_2D != desc->fTarget) {
Brian Salomond17f6582017-07-19 18:28:58 -0400636 return false;
637 }
Brian Salomone8a766b2019-07-19 14:24:36 -0400638 if (backendTex.isProtected()) {
639 // Not supported in GL backend at this time.
640 return false;
641 }
Brian Salomonea4ad302019-08-07 13:04:55 -0400642
Brian Salomond17f6582017-07-19 18:28:58 -0400643 return true;
644}
645
646sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400647 GrWrapOwnership ownership,
648 GrWrapCacheable cacheable,
649 GrIOType ioType) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400650 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400651 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400652 return nullptr;
653 }
Robert Phillips1cd1ed82019-07-23 13:21:01 -0400654
Brian Salomond17f6582017-07-19 18:28:58 -0400655 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400656 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Salomond17f6582017-07-19 18:28:58 -0400657 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400658 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomond17f6582017-07-19 18:28:58 -0400659 }
bsalomone5286e02016-01-14 09:24:09 -0800660
Brian Salomon40a40622020-07-21 10:32:07 -0400661 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400662 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400663
Brian Salomona6db5102020-07-21 09:56:23 -0400664 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Brian Salomone2826ab2019-06-04 15:58:31 -0400665 backendTex.getGLTextureParams(), cacheable, ioType);
Brian Salomondc829942018-10-23 16:07:24 -0400666 // We don't know what parameters are already set on wrapped textures.
667 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600668 return std::move(texture);
Brian Salomond17f6582017-07-19 18:28:58 -0400669}
670
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500671static bool check_compressed_backend_texture(const GrBackendTexture& backendTex,
672 const GrGLCaps& caps, GrGLTexture::Desc* desc,
673 bool skipRectTexSupportCheck = false) {
674 GrGLTextureInfo info;
675 if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) {
676 return false;
677 }
678
679 desc->fSize = {backendTex.width(), backendTex.height()};
680 desc->fTarget = info.fTarget;
681 desc->fID = info.fID;
682 desc->fFormat = GrGLFormatFromGLEnum(info.fFormat);
683
684 if (desc->fFormat == GrGLFormat::kUnknown) {
685 return false;
686 }
687
688 if (GR_GL_TEXTURE_2D != desc->fTarget) {
689 return false;
690 }
691 if (backendTex.isProtected()) {
692 // Not supported in GL backend at this time.
693 return false;
694 }
695
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500696 return true;
697}
698
Robert Phillipsb915c942019-12-17 14:44:37 -0500699sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex,
700 GrWrapOwnership ownership,
701 GrWrapCacheable cacheable) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500702 GrGLTexture::Desc desc;
703 if (!check_compressed_backend_texture(backendTex, this->glCaps(), &desc)) {
704 return nullptr;
705 }
706
707 if (kBorrow_GrWrapOwnership == ownership) {
708 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
709 } else {
710 desc.fOwnership = GrBackendObjectOwnership::kOwned;
711 }
712
Brian Salomon40a40622020-07-21 10:32:07 -0400713 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kValid
Brian Salomona6db5102020-07-21 09:56:23 -0400714 : GrMipmapStatus::kNotAllocated;
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500715
Brian Salomona6db5102020-07-21 09:56:23 -0400716 auto texture = GrGLTexture::MakeWrapped(this, mipmapStatus, desc,
Robert Phillipsd04ddcd2019-12-18 14:36:52 -0500717 backendTex.getGLTextureParams(), cacheable,
718 kRead_GrIOType);
719 // We don't know what parameters are already set on wrapped textures.
720 texture->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600721 return std::move(texture);
Robert Phillipsb915c942019-12-17 14:44:37 -0500722}
723
Brian Salomond17f6582017-07-19 18:28:58 -0400724sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400725 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500726 GrWrapOwnership ownership,
727 GrWrapCacheable cacheable) {
Brian Salomond2a8ae22019-09-10 16:03:59 -0400728 const GrGLCaps& caps = this->glCaps();
729
Brian Salomonea4ad302019-08-07 13:04:55 -0400730 GrGLTexture::Desc desc;
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400731 if (!check_backend_texture(backendTex, this->glCaps(), &desc)) {
bsalomone5286e02016-01-14 09:24:09 -0800732 return nullptr;
bsalomon7ea33f52015-11-22 14:51:00 -0800733 }
Brian Salomond2a8ae22019-09-10 16:03:59 -0400734 SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt));
735 SkASSERT(caps.isFormatTexturable(desc.fFormat));
bsalomone5286e02016-01-14 09:24:09 -0800736
Brian Salomond17f6582017-07-19 18:28:58 -0400737 // We don't support rendering to a EXTERNAL texture.
Brian Salomonea4ad302019-08-07 13:04:55 -0400738 if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) {
bsalomona98419b2015-11-23 07:09:50 -0800739 return nullptr;
740 }
bsalomon10528f12015-10-14 12:54:52 -0700741
Brian Osman766fcbb2017-03-13 09:33:09 -0400742 if (kBorrow_GrWrapOwnership == ownership) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400743 desc.fOwnership = GrBackendObjectOwnership::kBorrowed;
Brian Osman766fcbb2017-03-13 09:33:09 -0400744 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -0400745 desc.fOwnership = GrBackendObjectOwnership::kOwned;
bsalomone5286e02016-01-14 09:24:09 -0800746 }
bsalomonb15b4c12014-10-29 12:41:57 -0700747
Robert Phillips0902c982019-07-16 07:47:56 -0400748
Greg Daniel6fa62e22019-08-07 15:52:37 -0400749 sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat);
750 SkASSERT(sampleCnt);
bsalomon@google.come269f212011-11-07 13:29:52 +0000751
Brian Salomonea4ad302019-08-07 13:04:55 -0400752 GrGLRenderTarget::IDs rtIDs;
753 if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400754 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000755 }
Greg Daniel177e6952017-10-12 12:27:11 -0400756
Brian Salomon40a40622020-07-21 10:32:07 -0400757 GrMipmapStatus mipmapStatus = backendTex.hasMipmaps() ? GrMipmapStatus::kDirty
Brian Salomona6db5102020-07-21 09:56:23 -0400758 : GrMipmapStatus::kNotAllocated;
Greg Daniel177e6952017-10-12 12:27:11 -0400759
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500760 sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped(
Brian Salomonea4ad302019-08-07 13:04:55 -0400761 this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable,
Brian Salomona6db5102020-07-21 09:56:23 -0400762 mipmapStatus));
Brian Salomond17f6582017-07-19 18:28:58 -0400763 texRT->baseLevelWasBoundToFBO();
Brian Salomondc829942018-10-23 16:07:24 -0400764 // We don't know what parameters are already set on wrapped textures.
765 texRT->textureParamsModified();
Mike Kleina9609ea2020-02-18 13:33:23 -0600766 return std::move(texRT);
bsalomon@google.come269f212011-11-07 13:29:52 +0000767}
768
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400769sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400770 GrGLFramebufferInfo info;
771 if (!backendRT.getGLFramebufferInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000772 return nullptr;
773 }
774
Brian Salomone8a766b2019-07-19 14:24:36 -0400775 if (backendRT.isProtected()) {
776 // Not supported in GL at this time.
777 return nullptr;
778 }
779
Brian Salomond4764a12019-08-08 12:08:24 -0400780 const auto format = backendRT.getBackendFormat().asGLFormat();
Greg Daniel6fa62e22019-08-07 15:52:37 -0400781 if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) {
782 return nullptr;
783 }
784
Brian Salomonea4ad302019-08-07 13:04:55 -0400785 GrGLRenderTarget::IDs rtIDs;
786 rtIDs.fRTFBOID = info.fFBOID;
787 rtIDs.fMSColorRenderbufferID = 0;
788 rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID;
789 rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000790
Greg Daniel6fa62e22019-08-07 15:52:37 -0400791 int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format);
bsalomonb15b4c12014-10-29 12:41:57 -0700792
Brian Salomona56a7462020-02-07 14:17:25 -0500793 return GrGLRenderTarget::MakeWrapped(this, backendRT.dimensions(), format, sampleCount, rtIDs,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400794 backendRT.stencilBits());
bsalomon@google.come269f212011-11-07 13:29:52 +0000795}
796
Greg Daniel7ef28f32017-04-20 16:41:55 +0000797sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400798 int sampleCnt) {
Brian Salomonea4ad302019-08-07 13:04:55 -0400799 GrGLTexture::Desc desc;
800 // We do not check whether texture rectangle is supported by Skia - if the caller provided us
801 // with a texture rectangle,we assume the necessary support exists.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400802 if (!check_backend_texture(tex, this->glCaps(), &desc, true)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800803 return nullptr;
804 }
Greg Daniel6fa62e22019-08-07 15:52:37 -0400805
806 if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) {
807 return nullptr;
808 }
809
810 const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat);
Brian Salomonea4ad302019-08-07 13:04:55 -0400811 GrGLRenderTarget::IDs rtIDs;
812 if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800813 return nullptr;
814 }
Greg Danield51fa2f2020-01-22 16:53:38 -0500815 return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, sampleCount, rtIDs, 0);
ericrkf7b8b8a2016-02-24 14:49:51 -0800816}
817
Brian Salomonc320b152018-02-20 14:05:36 -0500818static bool check_write_and_transfer_input(GrGLTexture* glTex) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700819 if (!glTex) {
820 return false;
821 }
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000822
bsalomone5286e02016-01-14 09:24:09 -0800823 // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures
824 if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) {
bsalomon7ea33f52015-11-22 14:51:00 -0800825 return false;
826 }
827
jvanverth17aa0472016-01-05 10:41:27 -0800828 return true;
829}
830
Brian Salomona9b04b92018-06-01 15:04:28 -0400831bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400832 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400833 const GrMipLevel texels[], int mipLevelCount,
834 bool prepForTexSampling) {
Brian Salomonc320b152018-02-20 14:05:36 -0500835 auto glTex = static_cast<GrGLTexture*>(surface->asTexture());
jvanverth17aa0472016-01-05 10:41:27 -0800836
Brian Salomonc320b152018-02-20 14:05:36 -0500837 if (!check_write_and_transfer_input(glTex)) {
jvanverth17aa0472016-01-05 10:41:27 -0800838 return false;
839 }
840
Brian Salomond978b902019-02-07 15:09:18 -0500841 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
bsalomon@google.com9d6cfd82011-11-05 13:25:21 +0000842
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400843 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Brian Salomon22558932020-05-15 14:46:34 -0400844 SkIRect dstRect = SkIRect::MakeXYWH(left, top, width, height);
845 return this->uploadColorTypeTexData(glTex->format(), surfaceColorType, glTex->dimensions(),
846 glTex->target(), dstRect, srcColorType, texels,
847 mipLevelCount);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000848}
849
Brian Salomone05ba5a2019-04-08 11:59:07 -0400850bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400851 GrColorType textureColorType, GrColorType bufferColorType,
852 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400853 GrGLTexture* glTex = static_cast<GrGLTexture*>(texture);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400854
Jim Van Verth1676cb92019-01-15 13:24:45 -0500855 // Can't transfer compressed data
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400856 SkASSERT(!GrGLFormatIsCompressed(glTex->format()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500857
Brian Salomonc320b152018-02-20 14:05:36 -0500858 if (!check_write_and_transfer_input(glTex)) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400859 return false;
860 }
861
Hal Canaryc36f7e72018-06-18 15:50:09 -0400862 static_assert(sizeof(int) == sizeof(int32_t), "");
863 if (width <= 0 || height <= 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400864 return false;
865 }
866
Brian Salomond978b902019-02-07 15:09:18 -0500867 this->bindTextureToScratchUnit(glTex->target(), glTex->textureID());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400868
869 SkASSERT(!transferBuffer->isMapped());
Brian Salomondbf70722019-02-07 11:31:24 -0500870 SkASSERT(!transferBuffer->isCpuBuffer());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400871 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer);
Brian Salomonae64c192019-02-05 09:41:37 -0500872 this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400873
Greg Daniel660cc992017-06-26 14:55:05 -0400874 SkDEBUGCODE(
875 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
876 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
877 SkASSERT(bounds.contains(subRect));
878 )
879
Brian Salomonb28cb682019-07-26 12:48:47 -0400880 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400881 const size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400882 const void* pixels = (void*)offset;
Bruce Dawson71479b72017-07-05 14:30:20 -0700883 if (width < 0 || height < 0) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400884 return false;
885 }
886
887 bool restoreGLRowLength = false;
888 if (trimRowBytes != rowBytes) {
889 // we should have checked for this support already
Brian Salomon1047a492019-07-02 12:25:21 -0400890 SkASSERT(this->glCaps().writePixelsRowBytesSupport());
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400891 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp));
892 restoreGLRowLength = true;
893 }
894
Greg Danielba88ab62019-07-26 09:14:01 -0400895 GrGLFormat textureFormat = glTex->format();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400896 // External format and type come from the upload data.
Greg Danielba88ab62019-07-26 09:14:01 -0400897 GrGLenum externalFormat = 0;
898 GrGLenum externalType = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400899 this->glCaps().getTexSubImageExternalFormatAndType(
900 textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400901 if (!externalFormat || !externalType) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400902 return false;
903 }
904
Brian Salomon8cbf6622019-07-30 11:03:14 -0400905 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400906 GL_CALL(TexSubImage2D(glTex->target(),
907 0,
908 left, top,
909 width,
910 height,
911 externalFormat, externalType,
912 pixels));
913
914 if (restoreGLRowLength) {
915 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
916 }
917
918 return true;
919}
920
Brian Salomon26de56e2019-04-10 12:14:26 -0400921bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400922 GrColorType surfaceColorType, GrColorType dstColorType,
923 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomone05ba5a2019-04-08 11:59:07 -0400924 auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer);
925 this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400926 auto offsetAsPtr = reinterpret_cast<void*>(offset);
Brian Salomonf77c1462019-08-01 15:19:29 -0400927 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
928 dstColorType, offsetAsPtr, width);
Brian Salomone05ba5a2019-04-08 11:59:07 -0400929}
930
Brian Osman9b560242017-09-05 15:34:52 -0400931void GrGLGpu::unbindCpuToGpuXferBuffer() {
Brian Salomonae64c192019-02-05 09:41:37 -0500932 auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu);
933 if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) {
934 GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0));
935 xferBufferState->invalidate();
Brian Osman9b560242017-09-05 15:34:52 -0400936 }
Brian Osman9b560242017-09-05 15:34:52 -0400937}
938
Brian Salomon22558932020-05-15 14:46:34 -0400939bool GrGLGpu::uploadColorTypeTexData(GrGLFormat textureFormat,
940 GrColorType textureColorType,
941 SkISize texDims,
942 GrGLenum target,
943 SkIRect dstRect,
944 GrColorType srcColorType,
945 const GrMipLevel texels[],
946 int mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947 // If we're uploading compressed data then we should be using uploadCompressedTexData
Brian Salomonf77c1462019-08-01 15:19:29 -0400948 SkASSERT(!GrGLFormatIsCompressed(textureFormat));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500949
Greg Daniel7bfc9132019-08-14 14:23:53 -0400950 SkASSERT(this->glCaps().isFormatTexturable(textureFormat));
cblume55f2d2d2016-02-26 13:20:48 -0800951
Brian Salomonf77c1462019-08-01 15:19:29 -0400952 size_t bpp = GrColorTypeBytesPerPixel(srcColorType);
cblume55f2d2d2016-02-26 13:20:48 -0800953
bsalomon5b30c6f2015-12-17 14:17:34 -0800954 // External format and type come from the upload data.
bsalomon76148af2016-01-12 11:13:47 -0800955 GrGLenum externalFormat;
956 GrGLenum externalType;
Brian Salomond2a8ae22019-09-10 16:03:59 -0400957 this->glCaps().getTexSubImageExternalFormatAndType(
958 textureFormat, textureColorType, srcColorType, &externalFormat, &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -0400959 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -0800960 return false;
961 }
Brian Salomon22558932020-05-15 14:46:34 -0400962 this->uploadTexData(texDims, target, dstRect, externalFormat, externalType, bpp, texels,
963 mipLevelCount);
964 return true;
965}
Brian Salomonf77c1462019-08-01 15:19:29 -0400966
Brian Salomon22558932020-05-15 14:46:34 -0400967bool GrGLGpu::uploadColorToTex(GrGLFormat textureFormat,
968 SkISize texDims,
969 GrGLenum target,
970 SkColor4f color,
971 uint32_t levelMask) {
972 GrColorType colorType;
973 GrGLenum externalFormat, externalType;
974 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(textureFormat, &externalFormat,
975 &externalType, &colorType);
976 if (colorType == GrColorType::kUnknown) {
977 return false;
Robert Phillips71f06f62020-05-15 16:37:21 +0000978 }
Brian Salomon5c66aa72020-05-13 10:15:55 -0400979
Brian Salomon22558932020-05-15 14:46:34 -0400980 std::unique_ptr<char[]> pixelStorage;
981 size_t bpp = 0;
Mike Reed13711eb2020-07-14 17:16:32 -0400982 int numLevels = SkMipmap::ComputeLevelCount(texDims) + 1;
Brian Salomon22558932020-05-15 14:46:34 -0400983 SkSTArray<16, GrMipLevel> levels;
984 levels.resize(numLevels);
985 SkISize levelDims = texDims;
986 for (int i = 0; i < numLevels; ++i, levelDims = {std::max(levelDims.width() >> 1, 1),
987 std::max(levelDims.height() >> 1, 1)}) {
988 if (levelMask & (1 << i)) {
989 if (!pixelStorage) {
990 // Make one tight image at the first size and reuse it for smaller levels.
991 GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, levelDims);
992 size_t rb = ii.minRowBytes();
993 pixelStorage.reset(new char[rb * levelDims.height()]);
994 if (!GrClearImage(ii, pixelStorage.get(), ii.minRowBytes(), color)) {
995 return false;
996 }
997 bpp = ii.bpp();
Robert Phillips71f06f62020-05-15 16:37:21 +0000998 }
Brian Salomon22558932020-05-15 14:46:34 -0400999 levels[i] = {pixelStorage.get(), levelDims.width()*bpp};
1000 }
1001 }
1002 this->uploadTexData(texDims, target, SkIRect::MakeSize(texDims), externalFormat, externalType,
1003 bpp, levels.begin(), levels.count());
1004 return true;
1005}
1006
1007void GrGLGpu::uploadTexData(SkISize texDims,
1008 GrGLenum target,
1009 SkIRect dstRect,
1010 GrGLenum externalFormat,
1011 GrGLenum externalType,
1012 size_t bpp,
1013 const GrMipLevel texels[],
1014 int mipLevelCount) {
1015 SkASSERT(!texDims.isEmpty());
1016 SkASSERT(!dstRect.isEmpty());
1017 SkASSERT(SkIRect::MakeSize(texDims).contains(dstRect));
Mike Reed13711eb2020-07-14 17:16:32 -04001018 SkASSERT(mipLevelCount > 0 && mipLevelCount <= SkMipmap::ComputeLevelCount(texDims) + 1);
Brian Salomon22558932020-05-15 14:46:34 -04001019 SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(texDims));
1020
1021 const GrGLCaps& caps = this->glCaps();
1022
1023 bool restoreGLRowLength = false;
1024
1025 this->unbindCpuToGpuXferBuffer();
1026 GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1));
1027
1028 SkISize dims = dstRect.size();
1029 for (int level = 0; level < mipLevelCount; ++level, dims = {std::max(dims.width() >> 1, 1),
1030 std::max(dims.height() >> 1, 1)}) {
1031 if (!texels[level].fPixels) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001032 continue;
Brian Salomon8e63cab2019-09-10 19:02:29 +00001033 }
Brian Salomon22558932020-05-15 14:46:34 -04001034 const size_t trimRowBytes = dims.width() * bpp;
1035 const size_t rowBytes = texels[level].fRowBytes;
Brian Salomond2a8ae22019-09-10 16:03:59 -04001036
1037 if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) {
1038 GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp);
Brian Salomon22558932020-05-15 14:46:34 -04001039 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001040 restoreGLRowLength = true;
Brian Salomon22558932020-05-15 14:46:34 -04001041 } else {
1042 SkASSERT(rowBytes == trimRowBytes);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001043 }
1044
Brian Salomon22558932020-05-15 14:46:34 -04001045 GL_CALL(TexSubImage2D(target, level, dstRect.x(), dstRect.y(), dims.width(), dims.height(),
1046 externalFormat, externalType, texels[level].fPixels));
bsalomon@google.com6f379512011-11-16 20:36:03 +00001047 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001048 if (restoreGLRowLength) {
1049 SkASSERT(caps.writePixelsRowBytesSupport());
1050 GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0));
1051 }
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001052}
1053
Greg Daniel01f278c2020-06-12 16:58:17 -04001054bool GrGLGpu::uploadCompressedTexData(SkImage::CompressionType compressionType,
1055 GrGLFormat format,
Robert Phillipsb915c942019-12-17 14:44:37 -05001056 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001057 GrMipmapped mipMapped,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001058 GrGLenum target,
Robert Phillips9f744f72019-12-19 19:14:33 -05001059 const void* data, size_t dataSize) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001060 SkASSERT(format != GrGLFormat::kUnknown);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001061 const GrGLCaps& caps = this->glCaps();
1062
1063 // We only need the internal format for compressed 2D textures.
Brian Salomond2a8ae22019-09-10 16:03:59 -04001064 GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format);
Greg Daniel7bfc9132019-08-14 14:23:53 -04001065 if (!internalFormat) {
Robert Phillips9f744f72019-12-19 19:14:33 -05001066 return false;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001067 }
1068
Robert Phillips9f744f72019-12-19 19:14:33 -05001069 SkASSERT(compressionType != SkImage::CompressionType::kNone);
1070
Greg Daniel7bfc9132019-08-14 14:23:53 -04001071 bool useTexStorage = caps.formatSupportsTexStorage(format);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001072
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001073 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04001074 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04001075 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height())+1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05001076 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001077
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001078 // TODO: Make sure that the width and height that we pass to OpenGL
Brian Salomonbb8dde82019-06-27 10:52:13 -04001079 // is a multiple of the block size.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001080
1081 if (useTexStorage) {
1082 // We never resize or change formats of textures.
Brian Salomond8575452020-02-25 12:13:29 -05001083 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, numMipLevels, internalFormat,
1084 dimensions.width(), dimensions.height()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001085 if (error != GR_GL_NO_ERROR) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001086 return false;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001087 }
Robert Phillips42716d42019-12-16 12:19:54 -05001088
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001089 size_t offset = 0;
1090 for (int level = 0; level < numMipLevels; ++level) {
1091
Robert Phillips99dead92020-01-27 16:11:57 -05001092 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1093 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001094
Brian Salomond8575452020-02-25 12:13:29 -05001095 error = GL_ALLOC_CALL(CompressedTexSubImage2D(target,
1096 level,
1097 0, // left
1098 0, // top
1099 dimensions.width(),
1100 dimensions.height(),
1101 internalFormat,
1102 SkToInt(levelDataSize),
1103 &((char*)data)[offset]));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001104
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001105 if (error != GR_GL_NO_ERROR) {
1106 return false;
1107 }
1108
Robert Phillips9f744f72019-12-19 19:14:33 -05001109 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001110 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Robert Phillips42716d42019-12-16 12:19:54 -05001111 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001112 } else {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001113 size_t offset = 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001114
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001115 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillips99dead92020-01-27 16:11:57 -05001116 size_t levelDataSize = SkCompressedDataSize(compressionType, dimensions,
1117 nullptr, false);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001118
1119 const char* rawLevelData = &((char*)data)[offset];
Brian Salomond8575452020-02-25 12:13:29 -05001120 GrGLenum error = GL_ALLOC_CALL(CompressedTexImage2D(target,
1121 level,
1122 internalFormat,
1123 dimensions.width(),
1124 dimensions.height(),
1125 0, // border
1126 SkToInt(levelDataSize),
1127 rawLevelData));
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001128
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001129 if (error != GR_GL_NO_ERROR) {
1130 return false;
1131 }
1132
Robert Phillips9f744f72019-12-19 19:14:33 -05001133 offset += levelDataSize;
Brian Osman788b9162020-02-07 10:36:46 -05001134 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
Greg Kaiser73bfb892019-02-11 09:03:41 -08001135 }
Jim Van Verth1676cb92019-01-15 13:24:45 -05001136 }
Greg Daniel7bfc9132019-08-14 14:23:53 -04001137 return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001138}
1139
Brian Salomond8575452020-02-25 12:13:29 -05001140bool GrGLGpu::renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format,
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001141 int width, int height) {
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001142 SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType());
Brian Salomond8575452020-02-25 12:13:29 -05001143 GrGLenum error;
commit-bot@chromium.orgb1854a82014-01-16 18:39:04 +00001144 switch (ctx.caps()->msFBOType()) {
Brian Salomon00731b42016-10-14 11:30:51 -04001145 case GrGLCaps::kStandard_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001146 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, sampleCount,
1147 format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001148 break;
1149 case GrGLCaps::kES_Apple_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001150 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
1151 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001152 break;
1153 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
1154 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001155 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
1156 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001157 break;
1158 case GrGLCaps::kNone_MSFBOType:
Brian Salomond8575452020-02-25 12:13:29 -05001159 SkUNREACHABLE;
commit-bot@chromium.org040fd8f2013-09-06 20:00:41 +00001160 break;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001161 }
Brian Salomond8575452020-02-25 12:13:29 -05001162 return error == GR_GL_NO_ERROR;
bsalomon@google.comc9668ec2012-04-11 18:16:41 +00001163}
1164
Brian Salomonea4ad302019-08-07 13:04:55 -04001165bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001166 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -04001167 GrGLRenderTarget::IDs* rtIDs) {
1168 rtIDs->fMSColorRenderbufferID = 0;
1169 rtIDs->fRTFBOID = 0;
1170 rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned;
1171 rtIDs->fTexFBOID = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001172
bsalomona11e5fc2015-12-18 07:59:41 -08001173 GrGLenum colorRenderbufferFormat = 0; // suppress warning
bsalomon@google.comab15d612011-08-09 12:57:56 +00001174
Brian Salomonea4ad302019-08-07 13:04:55 -04001175 if (desc.fFormat == GrGLFormat::kUnknown) {
Greg Daniel9bb268b2019-07-17 10:40:13 -04001176 goto FAILED;
1177 }
1178
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001179 if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) {
bsalomon@google.com347c3822013-05-01 20:10:01 +00001180 goto FAILED;
1181 }
1182
Brian Salomonea4ad302019-08-07 13:04:55 -04001183 GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID));
1184 if (!rtIDs->fTexFBOID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001185 goto FAILED;
1186 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001187
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001188 // If we are using multisampling we will create two FBOS. We render to one and then resolve to
1189 // the texture bound to the other. The exception is the IMG multisample extension. With this
1190 // extension the texture is multisampled when rendered to and then auto-resolves it when it is
1191 // rendered from.
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001192 if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001193 GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID));
1194 GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
1195 if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001196 goto FAILED;
1197 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001198 colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001199 } else {
Brian Salomonea4ad302019-08-07 13:04:55 -04001200 rtIDs->fRTFBOID = rtIDs->fTexFBOID;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001201 }
1202
egdanield803f272015-03-18 13:01:52 -07001203 // below here we may bind the FBO
Robert Phillips294870f2016-11-11 12:38:40 -05001204 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomonea4ad302019-08-07 13:04:55 -04001205 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001206 SkASSERT(sampleCount > 1);
Brian Salomonea4ad302019-08-07 13:04:55 -04001207 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID));
Brian Salomond8575452020-02-25 12:13:29 -05001208 if (!this->renderbufferStorageMSAA(*fGLContext, sampleCount, colorRenderbufferFormat,
1209 desc.fSize.width(), desc.fSize.height())) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001210 goto FAILED;
1211 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001212 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID);
egdanield803f272015-03-18 13:01:52 -07001213 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon10528f12015-10-14 12:54:52 -07001214 GR_GL_COLOR_ATTACHMENT0,
1215 GR_GL_RENDERBUFFER,
Brian Salomonea4ad302019-08-07 13:04:55 -04001216 rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001217 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001218 this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001219
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001220 if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) {
Brian Salomonea4ad302019-08-07 13:04:55 -04001221 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER,
1222 GR_GL_COLOR_ATTACHMENT0,
1223 desc.fTarget,
1224 desc.fID,
1225 0,
1226 sampleCount));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001227 } else {
egdanield803f272015-03-18 13:01:52 -07001228 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001229 GR_GL_COLOR_ATTACHMENT0,
Brian Salomonea4ad302019-08-07 13:04:55 -04001230 desc.fTarget,
1231 desc.fID,
1232 0));
bsalomon@google.comf3a60c02013-03-19 19:06:09 +00001233 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001234
1235 return true;
1236
1237FAILED:
Brian Salomonea4ad302019-08-07 13:04:55 -04001238 if (rtIDs->fMSColorRenderbufferID) {
1239 GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID));
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001240 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001241 if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) {
1242 this->deleteFramebuffer(rtIDs->fRTFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001243 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001244 if (rtIDs->fTexFBOID) {
1245 this->deleteFramebuffer(rtIDs->fTexFBOID);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001246 }
1247 return false;
1248}
1249
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001250// good to set a break-point here to know when createTexture fails
Robert Phillips67d52cf2017-06-05 13:38:13 -04001251static sk_sp<GrTexture> return_null_texture() {
mtklein@google.com330313a2013-08-22 15:37:26 +00001252// SkDEBUGFAIL("null texture");
halcanary96fcdcc2015-08-27 07:41:13 -07001253 return nullptr;
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +00001254}
1255
Brian Salomone2826ab2019-06-04 15:58:31 -04001256static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params(
Brian Salomonea4ad302019-08-07 13:04:55 -04001257 const GrGLInterface* interface, GrGLenum target) {
cblume55f2d2d2016-02-26 13:20:48 -08001258 // Some drivers like to know filter/wrap before seeing glTexImage2D. Some
1259 // drivers have a bug where an FBO won't be complete if it includes a
1260 // texture that is not mipmap complete (considering the filter in use).
Brian Salomone2826ab2019-06-04 15:58:31 -04001261 GrGLTextureParameters::SamplerOverriddenState state;
1262 state.fMinFilter = GR_GL_NEAREST;
1263 state.fMagFilter = GR_GL_NEAREST;
1264 state.fWrapS = GR_GL_CLAMP_TO_EDGE;
1265 state.fWrapT = GR_GL_CLAMP_TO_EDGE;
Brian Salomonea4ad302019-08-07 13:04:55 -04001266 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter));
1267 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter));
1268 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS));
1269 GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT));
Brian Salomone2826ab2019-06-04 15:58:31 -04001270 return state;
cblume55f2d2d2016-02-26 13:20:48 -08001271}
1272
Brian Salomona56a7462020-02-07 14:17:25 -05001273sk_sp<GrTexture> GrGLGpu::onCreateTexture(SkISize dimensions,
Brian Salomon81536f22019-08-08 16:30:49 -04001274 const GrBackendFormat& format,
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001275 GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001276 int renderTargetSampleCnt,
Robert Phillips67d52cf2017-06-05 13:38:13 -04001277 SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -04001278 GrProtected isProtected,
Brian Salomond2a8ae22019-09-10 16:03:59 -04001279 int mipLevelCount,
1280 uint32_t levelClearMask) {
Brian Salomone8a766b2019-07-19 14:24:36 -04001281 // We don't support protected textures in GL.
1282 if (isProtected == GrProtected::kYes) {
1283 return nullptr;
1284 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001285 SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1);
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001286
Brian Salomond2a8ae22019-09-10 16:03:59 -04001287 SkASSERT(mipLevelCount > 0);
Brian Salomona6db5102020-07-21 09:56:23 -04001288 GrMipmapStatus mipmapStatus =
1289 mipLevelCount > 1 ? GrMipmapStatus::kDirty : GrMipmapStatus::kNotAllocated;
Brian Salomone2826ab2019-06-04 15:58:31 -04001290 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001291 GrGLTexture::Desc texDesc;
Brian Salomona56a7462020-02-07 14:17:25 -05001292 texDesc.fSize = dimensions;
Brian Salomon0f396992020-06-19 19:51:21 -04001293 switch (format.textureType()) {
1294 case GrTextureType::kExternal:
1295 case GrTextureType::kNone:
1296 return nullptr;
1297 case GrTextureType::k2D:
1298 texDesc.fTarget = GR_GL_TEXTURE_2D;
1299 break;
1300 case GrTextureType::kRectangle:
1301 if (mipLevelCount > 1 || !this->glCaps().rectangleTextureSupport()) {
1302 return nullptr;
1303 }
1304 texDesc.fTarget = GR_GL_TEXTURE_RECTANGLE;
1305 break;
1306 }
Brian Salomon81536f22019-08-08 16:30:49 -04001307 texDesc.fFormat = format.asGLFormat();
Brian Salomonea4ad302019-08-07 13:04:55 -04001308 texDesc.fOwnership = GrBackendObjectOwnership::kOwned;
Brian Salomon81536f22019-08-08 16:30:49 -04001309 SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown);
1310 SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat));
Brian Salomond4764a12019-08-08 12:08:24 -04001311
Brian Salomon0f396992020-06-19 19:51:21 -04001312 texDesc.fID = this->createTexture(dimensions, texDesc.fFormat, texDesc.fTarget, renderable,
1313 &initialState, mipLevelCount);
Brian Salomonea4ad302019-08-07 13:04:55 -04001314
1315 if (!texDesc.fID) {
bsalomon@google.comb1d14fd2011-12-09 18:41:34 +00001316 return return_null_texture();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001317 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
Robert Phillips67d52cf2017-06-05 13:38:13 -04001319 sk_sp<GrGLTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001320 if (renderable == GrRenderable::kYes) {
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001321 // unbind the texture from the texture unit before binding it to the frame buffer
Brian Salomonea4ad302019-08-07 13:04:55 -04001322 GL_CALL(BindTexture(texDesc.fTarget, 0));
1323 GrGLRenderTarget::IDs rtIDDesc;
robertphillips@google.comba0cc3e2012-03-26 17:58:35 +00001324
Brian Salomonea4ad302019-08-07 13:04:55 -04001325 if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) {
1326 GL_CALL(DeleteTextures(1, &texDesc.fID));
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 return return_null_texture();
1328 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001329 tex = sk_make_sp<GrGLTextureRenderTarget>(
Brian Salomona6db5102020-07-21 09:56:23 -04001330 this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipmapStatus);
Brian Salomon9bada542017-06-12 12:09:30 -04001331 tex->baseLevelWasBoundToFBO();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001332 } else {
Brian Salomona6db5102020-07-21 09:56:23 -04001333 tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipmapStatus);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
Brian Salomone2826ab2019-06-04 15:58:31 -04001335 // The non-sampler params are still at their default values.
1336 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1337 fResetTimestampForTextureParameters);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001338 if (levelClearMask) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001339 if (this->glCaps().clearTextureSupport()) {
Brian Salomon22558932020-05-15 14:46:34 -04001340 GrGLenum externalFormat, externalType;
1341 GrColorType colorType;
1342 this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(
1343 texDesc.fFormat, &externalFormat, &externalType, &colorType);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001344 for (int i = 0; i < mipLevelCount; ++i) {
1345 if (levelClearMask & (1U << i)) {
1346 GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType,
1347 nullptr));
1348 }
1349 }
1350 } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) &&
1351 !this->glCaps().performColorClearsAsDraws()) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07001352 this->flushScissorTest(GrScissorTest::kDisabled);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001353 this->disableWindowRectangles();
1354 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001355 this->flushClearColor(SK_PMColor4fTRANSPARENT);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001356 for (int i = 0; i < mipLevelCount; ++i) {
1357 if (levelClearMask & (1U << i)) {
1358 this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER,
1359 kDst_TempFBOTarget);
1360 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
1361 this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER);
1362 }
1363 }
Brian Salomonc2249852019-09-16 11:08:50 -04001364 fHWBoundRenderTargetUniqueID.makeInvalid();
Brian Salomond2a8ae22019-09-10 16:03:59 -04001365 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001366 this->bindTextureToScratchUnit(texDesc.fTarget, tex->textureID());
Brian Salomon22558932020-05-15 14:46:34 -04001367 static constexpr SkColor4f kZeroColor = {0, 0, 0, 0};
Brian Salomon0f396992020-06-19 19:51:21 -04001368 this->uploadColorToTex(texDesc.fFormat, texDesc.fSize, texDesc.fTarget, kZeroColor,
Brian Salomon22558932020-05-15 14:46:34 -04001369 levelClearMask);
Brian Salomond17b4a62017-05-23 16:53:47 -04001370 }
1371 }
Mike Kleina9609ea2020-02-18 13:33:23 -06001372 return std::move(tex);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001373}
1374
Robert Phillips9f744f72019-12-19 19:14:33 -05001375sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(SkISize dimensions,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001376 const GrBackendFormat& format,
Robert Phillips3a833922020-01-21 15:25:58 -05001377 SkBudgeted budgeted,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001378 GrMipmapped mipMapped,
Robert Phillips3a833922020-01-21 15:25:58 -05001379 GrProtected isProtected,
Robert Phillips9f744f72019-12-19 19:14:33 -05001380 const void* data, size_t dataSize) {
Robert Phillips3a833922020-01-21 15:25:58 -05001381 // We don't support protected textures in GL.
1382 if (isProtected == GrProtected::kYes) {
1383 return nullptr;
1384 }
Greg Daniel01f278c2020-06-12 16:58:17 -04001385 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1386
Brian Salomonbb8dde82019-06-27 10:52:13 -04001387 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomonea4ad302019-08-07 13:04:55 -04001388 GrGLTexture::Desc desc;
Robert Phillips9f744f72019-12-19 19:14:33 -05001389 desc.fSize = dimensions;
Brian Salomonea4ad302019-08-07 13:04:55 -04001390 desc.fTarget = GR_GL_TEXTURE_2D;
Brian Salomonea4ad302019-08-07 13:04:55 -04001391 desc.fOwnership = GrBackendObjectOwnership::kOwned;
Greg Daniel7bfc9132019-08-14 14:23:53 -04001392 desc.fFormat = format.asGLFormat();
Greg Daniel01f278c2020-06-12 16:58:17 -04001393 desc.fID = this->createCompressedTexture2D(desc.fSize, compression, desc.fFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001394 mipMapped, &initialState);
Brian Salomonea4ad302019-08-07 13:04:55 -04001395 if (!desc.fID) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001396 return nullptr;
1397 }
Robert Phillipsb915c942019-12-17 14:44:37 -05001398
Greg Danielaaf738c2020-07-10 09:30:33 -04001399 if (data) {
1400 if (!this->uploadCompressedTexData(compression, desc.fFormat, dimensions, mipMapped,
1401 GR_GL_TEXTURE_2D, data, dataSize)) {
1402 GL_CALL(DeleteTextures(1, &desc.fID));
1403 return nullptr;
1404 }
1405 }
1406
Robert Phillipsee946932019-12-18 11:16:17 -05001407 // Unbind this texture from the scratch texture unit.
1408 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1409
Brian Salomona6db5102020-07-21 09:56:23 -04001410 GrMipmapStatus mipmapStatus = mipMapped == GrMipmapped::kYes
1411 ? GrMipmapStatus::kValid
1412 : GrMipmapStatus::kNotAllocated;
Robert Phillipse4720c62020-01-14 14:33:24 -05001413
Brian Salomona6db5102020-07-21 09:56:23 -04001414 auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, mipmapStatus);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001415 // The non-sampler params are still at their default values.
1416 tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1417 fResetTimestampForTextureParameters);
Mike Kleina9609ea2020-02-18 13:33:23 -06001418 return std::move(tex);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001419}
1420
Greg Danielc1ad77c2020-05-06 11:40:03 -04001421GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -04001422 SkISize dimensions, const GrBackendFormat& format, GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001423 GrProtected isProtected) {
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001424 // We don't support protected textures in GL.
1425 if (isProtected == GrProtected::kYes) {
1426 return {};
1427 }
1428
1429 this->handleDirtyContext();
1430
1431 GrGLFormat glFormat = format.asGLFormat();
1432 if (glFormat == GrGLFormat::kUnknown) {
1433 return {};
1434 }
1435
Greg Daniel01f278c2020-06-12 16:58:17 -04001436 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1437
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001438 GrGLTextureInfo info;
1439 GrGLTextureParameters::SamplerOverriddenState initialState;
1440
1441 info.fTarget = GR_GL_TEXTURE_2D;
1442 info.fFormat = GrGLFormatToEnum(glFormat);
Greg Daniel01f278c2020-06-12 16:58:17 -04001443 info.fID = this->createCompressedTexture2D(dimensions, compression, glFormat,
Greg Danielaaf738c2020-07-10 09:30:33 -04001444 mipMapped, &initialState);
Robert Phillipsd04ddcd2019-12-18 14:36:52 -05001445 if (!info.fID) {
1446 return {};
1447 }
1448
1449 // Unbind this texture from the scratch texture unit.
1450 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0);
1451
1452 auto parameters = sk_make_sp<GrGLTextureParameters>();
1453 // The non-sampler params are still at their default values.
1454 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
1455 fResetTimestampForTextureParameters);
1456
1457 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
1458 std::move(parameters));
Robert Phillipsb915c942019-12-17 14:44:37 -05001459}
1460
Greg Danielaaf738c2020-07-10 09:30:33 -04001461bool GrGLGpu::onUpdateCompressedBackendTexture(const GrBackendTexture& backendTexture,
1462 sk_sp<GrRefCntedCallback> finishedCallback,
1463 const BackendTextureData* data) {
1464 SkASSERT(data && data->type() != BackendTextureData::Type::kPixmaps);
1465
1466 GrGLTextureInfo info;
1467 SkAssertResult(backendTexture.getGLTextureInfo(&info));
1468
1469 GrBackendFormat format = backendTexture.getBackendFormat();
1470 GrGLFormat glFormat = format.asGLFormat();
1471 if (glFormat == GrGLFormat::kUnknown) {
1472 return false;
1473 }
1474 SkImage::CompressionType compression = GrBackendFormatToCompressionType(format);
1475
Brian Salomon40a40622020-07-21 10:32:07 -04001476 GrMipmapped mipMapped = backendTexture.hasMipmaps() ? GrMipmapped::kYes : GrMipmapped::kNo;
Greg Danielaaf738c2020-07-10 09:30:33 -04001477
1478 const char* rawData = nullptr;
1479 size_t rawDataSize = 0;
1480 SkAutoMalloc am;
1481 if (data->type() == BackendTextureData::Type::kCompressed) {
1482 rawData = (const char*)data->compressedData();
1483 rawDataSize = data->compressedSize();
1484 } else {
1485 SkASSERT(data->type() == BackendTextureData::Type::kColor);
1486 SkASSERT(compression != SkImage::CompressionType::kNone);
1487
1488 rawDataSize = SkCompressedDataSize(compression, backendTexture.dimensions(), nullptr,
Brian Salomon40a40622020-07-21 10:32:07 -04001489 backendTexture.hasMipmaps());
Greg Danielaaf738c2020-07-10 09:30:33 -04001490
1491 am.reset(rawDataSize);
1492
1493 GrFillInCompressedData(compression, backendTexture.dimensions(), mipMapped, (char*)am.get(),
1494 data->color());
1495
1496 rawData = (const char*)am.get();
1497 }
1498
1499 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel95afafb2020-07-22 12:09:26 -04001500
1501 // If we have mips make sure the base level is set to 0 and the max level set to numMipLevels-1
1502 // so that the uploads go to the right levels.
1503 if (backendTexture.hasMipMaps() && this->glCaps().mipmapLevelAndLodControlSupport()) {
1504 auto params = backendTexture.getGLTextureParams();
1505 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
1506 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
1507 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
1508 nonsamplerState.fBaseMipMapLevel = 0;
1509 }
1510 int numMipLevels =
1511 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
1512 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
1513 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
1514 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
1515 }
1516 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
1517 }
1518
Greg Danielaaf738c2020-07-10 09:30:33 -04001519 bool result = this->uploadCompressedTexData(
1520 compression, glFormat, backendTexture.dimensions(), mipMapped, GR_GL_TEXTURE_2D,
1521 rawData, rawDataSize);
1522
1523 // Unbind this texture from the scratch texture unit.
1524 this->bindTextureToScratchUnit(info.fTarget, 0);
1525
1526 return result;
1527}
1528
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001529namespace {
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001530
egdaniel8dc7c3a2015-04-16 11:22:42 -07001531const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount;
bsalomon@google.comf7fa8062012-02-14 14:09:57 +00001532
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001533void inline get_stencil_rb_sizes(const GrGLInterface* gl,
egdaniel8dc7c3a2015-04-16 11:22:42 -07001534 GrGLStencilAttachment::Format* format) {
sugoi@google.com6ba0b0f2013-05-03 13:52:32 +00001535
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001536 // we shouldn't ever know one size and not the other
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001537 SkASSERT((kUnknownBitCount == format->fStencilBits) ==
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001538 (kUnknownBitCount == format->fTotalBits));
1539 if (kUnknownBitCount == format->fStencilBits) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001540 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001541 GR_GL_RENDERBUFFER_STENCIL_SIZE,
1542 (GrGLint*)&format->fStencilBits);
1543 if (format->fPacked) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001544 GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER,
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001545 GR_GL_RENDERBUFFER_DEPTH_SIZE,
1546 (GrGLint*)&format->fTotalBits);
1547 format->fTotalBits += format->fStencilBits;
1548 } else {
1549 format->fTotalBits = format->fStencilBits;
1550 }
1551 }
1552}
1553}
1554
Brian Salomon5043f1f2019-07-11 21:27:54 -04001555int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) {
bsalomon100b8f82015-10-28 08:37:44 -07001556 static const int kSize = 16;
Brian Salomonf6da1462019-07-11 14:21:59 -04001557 SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format));
1558
1559 if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) {
bsalomon30447372015-12-21 09:03:05 -08001560 // Default to unsupported, set this if we find a stencil format that works.
1561 int firstWorkingStencilFormatIndex = -1;
Brian Osman91f9a2c2017-09-05 15:02:46 -04001562
Brian Salomon0f396992020-06-19 19:51:21 -04001563 GrGLuint colorID = this->createTexture({kSize, kSize}, format, GR_GL_TEXTURE_2D,
1564 GrRenderable::kYes, nullptr, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001565 if (!colorID) {
Brian Salomonf6da1462019-07-11 14:21:59 -04001566 return -1;
bsalomon76148af2016-01-12 11:13:47 -08001567 }
egdanielff1d5472015-09-10 08:37:20 -07001568 // unbind the texture from the texture unit before binding it to the frame buffer
1569 GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
1570
1571 // Create Framebuffer
kkinnunen546eb5c2015-12-11 00:05:33 -08001572 GrGLuint fb = 0;
egdanielff1d5472015-09-10 08:37:20 -07001573 GL_CALL(GenFramebuffers(1, &fb));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001574 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb);
Robert Phillips294870f2016-11-11 12:38:40 -05001575 fHWBoundRenderTargetUniqueID.makeInvalid();
egdanielff1d5472015-09-10 08:37:20 -07001576 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER,
1577 GR_GL_COLOR_ATTACHMENT0,
1578 GR_GL_TEXTURE_2D,
1579 colorID,
1580 0));
bsalomon30447372015-12-21 09:03:05 -08001581 GrGLuint sbRBID = 0;
1582 GL_CALL(GenRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001583
1584 // look over formats till I find a compatible one
1585 int stencilFmtCnt = this->glCaps().stencilFormats().count();
bsalomon30447372015-12-21 09:03:05 -08001586 if (sbRBID) {
egdanielff1d5472015-09-10 08:37:20 -07001587 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001588 for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) {
1589 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i];
Brian Salomond8575452020-02-25 12:13:29 -05001590 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
1591 GR_GL_RENDERBUFFER, sFmt.fInternalFormat, kSize, kSize));
1592 if (error == GR_GL_NO_ERROR) {
egdanielff1d5472015-09-10 08:37:20 -07001593 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
bsalomon30447372015-12-21 09:03:05 -08001594 GR_GL_STENCIL_ATTACHMENT,
egdanielff1d5472015-09-10 08:37:20 -07001595 GR_GL_RENDERBUFFER, sbRBID));
bsalomon30447372015-12-21 09:03:05 -08001596 if (sFmt.fPacked) {
1597 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1598 GR_GL_DEPTH_ATTACHMENT,
1599 GR_GL_RENDERBUFFER, sbRBID));
1600 } else {
1601 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1602 GR_GL_DEPTH_ATTACHMENT,
1603 GR_GL_RENDERBUFFER, 0));
1604 }
1605 GrGLenum status;
1606 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
1607 if (status == GR_GL_FRAMEBUFFER_COMPLETE) {
1608 firstWorkingStencilFormatIndex = i;
1609 break;
1610 }
egdanielff1d5472015-09-10 08:37:20 -07001611 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1612 GR_GL_STENCIL_ATTACHMENT,
1613 GR_GL_RENDERBUFFER, 0));
1614 if (sFmt.fPacked) {
1615 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
1616 GR_GL_DEPTH_ATTACHMENT,
1617 GR_GL_RENDERBUFFER, 0));
1618 }
egdanielff1d5472015-09-10 08:37:20 -07001619 }
1620 }
bsalomon30447372015-12-21 09:03:05 -08001621 GL_CALL(DeleteRenderbuffers(1, &sbRBID));
egdanielff1d5472015-09-10 08:37:20 -07001622 }
1623 GL_CALL(DeleteTextures(1, &colorID));
Adrienne Walker4ee88512018-05-17 11:37:14 -07001624 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
1625 this->deleteFramebuffer(fb);
Brian Salomonf6da1462019-07-11 14:21:59 -04001626 fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex);
egdanielff1d5472015-09-10 08:37:20 -07001627 }
Brian Salomonf6da1462019-07-11 14:21:59 -04001628 return this->glCaps().getStencilFormatIndexForFormat(format);
egdanielff1d5472015-09-10 08:37:20 -07001629}
1630
Brian Salomonea4ad302019-08-07 13:04:55 -04001631GrGLuint GrGLGpu::createCompressedTexture2D(
Robert Phillips2716daf2020-01-23 12:53:42 -05001632 SkISize dimensions,
Greg Daniel01f278c2020-06-12 16:58:17 -04001633 SkImage::CompressionType compression,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001634 GrGLFormat format,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001635 GrMipmapped mipMapped,
Greg Danielaaf738c2020-07-10 09:30:33 -04001636 GrGLTextureParameters::SamplerOverriddenState* initialState) {
Greg Daniel7bfc9132019-08-14 14:23:53 -04001637 if (format == GrGLFormat::kUnknown) {
1638 return 0;
1639 }
Brian Salomonea4ad302019-08-07 13:04:55 -04001640 GrGLuint id = 0;
1641 GL_CALL(GenTextures(1, &id));
1642 if (!id) {
1643 return 0;
erikchen9a1ed5d2016-02-10 16:32:34 -08001644 }
1645
Brian Salomonea4ad302019-08-07 13:04:55 -04001646 this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id);
Robert Phillips8043f322019-05-31 08:11:36 -04001647
Brian Salomonea4ad302019-08-07 13:04:55 -04001648 *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D);
1649
Brian Salomonea4ad302019-08-07 13:04:55 -04001650 return id;
1651}
1652
Brian Salomon0f396992020-06-19 19:51:21 -04001653GrGLuint GrGLGpu::createTexture(SkISize dimensions,
1654 GrGLFormat format,
1655 GrGLenum target,
1656 GrRenderable renderable,
1657 GrGLTextureParameters::SamplerOverriddenState* initialState,
1658 int mipLevelCount) {
Brian Salomon81536f22019-08-08 16:30:49 -04001659 SkASSERT(format != GrGLFormat::kUnknown);
Brian Salomonea4ad302019-08-07 13:04:55 -04001660 SkASSERT(!GrGLFormatIsCompressed(format));
Brian Salomon81536f22019-08-08 16:30:49 -04001661
Brian Salomonea4ad302019-08-07 13:04:55 -04001662 GrGLuint id = 0;
1663 GL_CALL(GenTextures(1, &id));
1664
1665 if (!id) {
1666 return 0;
1667 }
1668
Brian Salomon0f396992020-06-19 19:51:21 -04001669 this->bindTextureToScratchUnit(target, id);
erikchen9a1ed5d2016-02-10 16:32:34 -08001670
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001671 if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) {
erikchen9a1ed5d2016-02-10 16:32:34 -08001672 // provides a hint about how this texture will be used
Brian Salomon0f396992020-06-19 19:51:21 -04001673 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT));
erikchen9a1ed5d2016-02-10 16:32:34 -08001674 }
1675
Brian Salomond2a8ae22019-09-10 16:03:59 -04001676 if (initialState) {
Brian Salomon0f396992020-06-19 19:51:21 -04001677 *initialState = set_initial_texture_params(this->glInterface(), target);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001678 } else {
Brian Salomon0f396992020-06-19 19:51:21 -04001679 set_initial_texture_params(this->glInterface(), target);
Brian Salomona7398242019-09-10 09:17:38 -04001680 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04001681
1682 GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format);
1683
1684 bool success = false;
1685 if (internalFormat) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04001686 if (this->glCaps().formatSupportsTexStorage(format)) {
Brian Salomond8575452020-02-25 12:13:29 -05001687 auto levelCount = std::max(mipLevelCount, 1);
Brian Salomon0f396992020-06-19 19:51:21 -04001688 GrGLenum error = GL_ALLOC_CALL(TexStorage2D(target, levelCount, internalFormat,
1689 dimensions.width(), dimensions.height()));
Brian Salomond8575452020-02-25 12:13:29 -05001690 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001691 } else {
1692 GrGLenum externalFormat, externalType;
1693 this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType);
1694 GrGLenum error = GR_GL_NO_ERROR;
1695 if (externalFormat && externalType) {
1696 for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) {
1697 const int twoToTheMipLevel = 1 << level;
Brian Osman788b9162020-02-07 10:36:46 -05001698 const int currentWidth = std::max(1, dimensions.width() / twoToTheMipLevel);
1699 const int currentHeight = std::max(1, dimensions.height() / twoToTheMipLevel);
Brian Salomon0f396992020-06-19 19:51:21 -04001700 error = GL_ALLOC_CALL(TexImage2D(target, level, internalFormat, currentWidth,
1701 currentHeight, 0, externalFormat, externalType,
1702 nullptr));
Brian Salomond2a8ae22019-09-10 16:03:59 -04001703 }
Brian Salomond8575452020-02-25 12:13:29 -05001704 success = (error == GR_GL_NO_ERROR);
Brian Salomond2a8ae22019-09-10 16:03:59 -04001705 }
1706 }
1707 }
1708 if (success) {
1709 return id;
1710 }
1711 GL_CALL(DeleteTextures(1, &id));
1712 return 0;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001713}
1714
Chris Daltoneffee202019-07-01 22:28:03 -06001715GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget(
1716 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001717 SkASSERT(width >= rt->width());
1718 SkASSERT(height >= rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001719
egdaniel8dc7c3a2015-04-16 11:22:42 -07001720 GrGLStencilAttachment::IDDesc sbDesc;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001721
Brian Salomond4764a12019-08-08 12:08:24 -04001722 int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat());
bsalomon62a627b2015-12-17 09:50:47 -08001723 if (sIdx < 0) {
egdanielec00d942015-09-14 12:56:10 -07001724 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00001725 }
egdanielff1d5472015-09-10 08:37:20 -07001726
1727 if (!sbDesc.fRenderbufferID) {
1728 GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID));
1729 }
1730 if (!sbDesc.fRenderbufferID) {
egdanielec00d942015-09-14 12:56:10 -07001731 return nullptr;
egdanielff1d5472015-09-10 08:37:20 -07001732 }
1733 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID));
1734 const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx];
egdanielff1d5472015-09-10 08:37:20 -07001735 // we do this "if" so that we don't call the multisample
1736 // version on a GL that doesn't have an MSAA extension.
Chris Daltoneffee202019-07-01 22:28:03 -06001737 if (numStencilSamples > 1) {
Brian Salomond8575452020-02-25 12:13:29 -05001738 if (!this->renderbufferStorageMSAA(*fGLContext, numStencilSamples, sFmt.fInternalFormat,
1739 width, height)) {
1740 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1741 return nullptr;
1742 }
egdanielff1d5472015-09-10 08:37:20 -07001743 } else {
Brian Salomond8575452020-02-25 12:13:29 -05001744 GrGLenum error = GL_ALLOC_CALL(
1745 RenderbufferStorage(GR_GL_RENDERBUFFER, sFmt.fInternalFormat, width, height));
1746 if (error != GR_GL_NO_ERROR) {
1747 GL_CALL(DeleteRenderbuffers(1, &sbDesc.fRenderbufferID));
1748 return nullptr;
1749 }
egdanielff1d5472015-09-10 08:37:20 -07001750 }
1751 fStats.incStencilAttachmentCreates();
1752 // After sized formats we attempt an unsized format and take
1753 // whatever sizes GL gives us. In that case we query for the size.
1754 GrGLStencilAttachment::Format format = sFmt;
1755 get_stencil_rb_sizes(this->glInterface(), &format);
egdanielec00d942015-09-14 12:56:10 -07001756 GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this,
1757 sbDesc,
1758 width,
1759 height,
Chris Daltoneffee202019-07-01 22:28:03 -06001760 numStencilSamples,
egdanielec00d942015-09-14 12:56:10 -07001761 format);
1762 return stencil;
reed@google.comac10a2d2010-12-22 21:39:39 +00001763}
1764
bsalomon@google.com71f341a2011-08-01 13:36:00 +00001765////////////////////////////////////////////////////////////////////////////////
1766
Brian Salomondbf70722019-02-07 11:31:24 -05001767sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType,
1768 GrAccessPattern accessPattern, const void* data) {
Brian Salomon12d22642019-01-29 14:38:50 -05001769 return GrGLBuffer::Make(this, size, intendedType, accessPattern, data);
jvanverth73063dc2015-12-03 09:15:47 -08001770}
1771
Chris Dalton2e7ed262020-02-21 15:17:59 -07001772void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
1773 if (GrScissorTest::kEnabled == scissorTest) {
Chris Daltondc2b15e2020-02-19 11:45:21 -07001774 if (kYes_TriState != fHWScissorSettings.fEnabled) {
1775 GL_CALL(Enable(GR_GL_SCISSOR_TEST));
1776 fHWScissorSettings.fEnabled = kYes_TriState;
1777 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001778 } else {
1779 if (kNo_TriState != fHWScissorSettings.fEnabled) {
1780 GL_CALL(Disable(GR_GL_SCISSOR_TEST));
1781 fHWScissorSettings.fEnabled = kNo_TriState;
1782 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07001784}
Brian Salomond818ebf2018-07-02 14:08:49 +00001785
Chris Dalton2e7ed262020-02-21 15:17:59 -07001786void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
1787 GrSurfaceOrigin rtOrigin) {
Chris Daltond42d2002020-02-28 12:05:04 -07001788 SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
Chris Dalton2e7ed262020-02-21 15:17:59 -07001789 auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
1790 if (fHWScissorSettings.fRect != nativeScissor) {
1791 GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
1792 nativeScissor.fHeight));
1793 fHWScissorSettings.fRect = nativeScissor;
1794 }
joshualitt77b13072014-10-27 14:51:01 -07001795}
1796
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001797void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001798 const GrGLRenderTarget* rt, GrSurfaceOrigin origin) {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001799#ifndef USE_NSIGHT
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001800 typedef GrWindowRectsState::Mode Mode;
1801 SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen.
1802 SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles());
csmartdalton28341fa2016-08-17 10:00:21 -07001803
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001804 if (!this->caps()->maxWindowRectangles() ||
Greg Danielacd66b42019-05-22 16:29:12 -04001805 fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) {
csmartdalton28341fa2016-08-17 10:00:21 -07001806 return;
csmartdalton28341fa2016-08-17 10:00:21 -07001807 }
1808
csmartdalton7535f412016-08-23 06:51:00 -07001809 // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above
1810 // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912
Brian Osman788b9162020-02-07 10:36:46 -05001811 int numWindows = std::min(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001812 SkASSERT(windowState.numWindows() == numWindows);
csmartdalton7535f412016-08-23 06:51:00 -07001813
Chris Daltond6cda8d2019-09-05 02:30:04 -06001814 GrNativeRect glwindows[GrWindowRectangles::kMaxWindows];
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001815 const SkIRect* skwindows = windowState.windows().data();
csmartdalton7535f412016-08-23 06:51:00 -07001816 for (int i = 0; i < numWindows; ++i) {
Chris Dalton76500e52019-09-05 02:13:05 -06001817 glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]);
csmartdalton28341fa2016-08-17 10:00:21 -07001818 }
1819
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001820 GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE;
csmartdalton7535f412016-08-23 06:51:00 -07001821 GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts()));
csmartdalton28341fa2016-08-17 10:00:21 -07001822
Greg Danielacd66b42019-05-22 16:29:12 -04001823 fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState);
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001824#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001825}
1826
1827void GrGLGpu::disableWindowRectangles() {
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001828#ifndef USE_NSIGHT
Jim Van Verth6a40abc2017-11-02 16:56:09 +00001829 if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) {
csmartdalton28341fa2016-08-17 10:00:21 -07001830 return;
1831 }
1832 GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr));
csmartdaltonbf4a8f92016-09-06 10:01:06 -07001833 fHWWindowRectsState.setDisabled();
Jim Van Verth32ac83e2016-11-28 15:23:57 -05001834#endif
csmartdalton28341fa2016-08-17 10:00:21 -07001835}
1836
Robert Phillipsdf546db2020-05-29 09:42:54 -04001837bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
Chris Dalton4386ad12020-02-19 16:42:06 -07001838 this->handleDirtyContext();
1839
Chris Daltond42d2002020-02-28 12:05:04 -07001840 sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
Chris Dalton20e7a532020-02-28 15:37:53 +00001841 if (!program) {
1842 GrCapsDebugf(this->caps(), "Failed to create program!\n");
1843 return false;
1844 }
1845
1846 this->flushProgram(std::move(program));
1847
Chris Daltond42d2002020-02-28 12:05:04 -07001848 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1849 this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1850 }
1851
Dongseong Hwang4e1f0222018-11-01 09:10:51 -07001852 // Swizzle the blend to match what the shader will output.
Robert Phillips901aff02019-10-08 12:32:56 -04001853 this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
Brian Salomon982f5462020-03-30 12:52:33 -04001854 programInfo.pipeline().writeSwizzle());
bsalomon1f78c0a2014-12-17 09:43:13 -08001855
Chris Dalton20e7a532020-02-28 15:37:53 +00001856 fHWProgram->updateUniforms(renderTarget, programInfo);
bsalomon1f78c0a2014-12-17 09:43:13 -08001857
Robert Phillipsd0fe8752019-01-31 14:13:59 -05001858 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001859 GrStencilSettings stencil;
1860 if (programInfo.pipeline().isStencilEnabled()) {
Brian Salomonf7f54332020-07-28 09:23:35 -04001861 SkASSERT(glRT->getStencilAttachment());
Robert Phillips6c2aa7a2019-10-17 19:06:39 +00001862 stencil.reset(*programInfo.pipeline().getUserStencil(),
1863 programInfo.pipeline().hasStencilClip(),
Brian Salomonf7f54332020-07-28 09:23:35 -04001864 glRT->numStencilBits());
csmartdaltonc633abb2016-11-01 08:55:55 -07001865 }
Robert Phillips901aff02019-10-08 12:32:56 -04001866 this->flushStencil(stencil, programInfo.origin());
Chris Dalton2e7ed262020-02-21 15:17:59 -07001867 this->flushScissorTest(GrScissorTest(programInfo.pipeline().isScissorTestEnabled()));
Robert Phillips901aff02019-10-08 12:32:56 -04001868 this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(),
1869 glRT, programInfo.origin());
1870 this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState());
Chris Daltonce425af2019-12-16 10:39:03 -07001871 this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster());
Chris Dalton1215cda2019-12-17 21:44:04 -07001872 this->flushWireframeState(programInfo.pipeline().isWireframe());
bsalomonbc3d0de2014-12-15 13:45:03 -08001873
1874 // This must come after textures are flushed because a texture may need
egdanield803f272015-03-18 13:01:52 -07001875 // to be msaa-resolved (which will modify bound FBO state).
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001876 this->flushRenderTarget(glRT);
bsalomonbc3d0de2014-12-15 13:45:03 -08001877
Chris Dalton20e7a532020-02-28 15:37:53 +00001878 return true;
1879}
1880
1881void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1882 if (!program) {
1883 fHWProgram.reset();
1884 fHWProgramID = 0;
1885 return;
1886 }
1887 SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1888 if (program == fHWProgram) {
1889 return;
1890 }
1891 auto id = program->programID();
1892 SkASSERT(id);
1893 GL_CALL(UseProgram(id));
1894 fHWProgram = std::move(program);
1895 fHWProgramID = id;
Brian Salomon802cb312018-06-08 18:05:20 -04001896}
1897
1898void GrGLGpu::flushProgram(GrGLuint id) {
1899 SkASSERT(id);
1900 if (fHWProgramID == id) {
Chris Dalton20e7a532020-02-28 15:37:53 +00001901 SkASSERT(!fHWProgram);
Brian Salomon802cb312018-06-08 18:05:20 -04001902 return;
1903 }
Chris Dalton20e7a532020-02-28 15:37:53 +00001904 fHWProgram.reset();
Brian Salomon802cb312018-06-08 18:05:20 -04001905 GL_CALL(UseProgram(id));
1906 fHWProgramID = id;
1907}
1908
Brian Salomonae64c192019-02-05 09:41:37 -05001909GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
joshualitt93316b92015-10-23 09:08:08 -07001910 this->handleDirtyContext();
cdaltondeacc972016-04-06 14:26:33 -07001911
cdaltone2e71c22016-04-07 18:13:29 -07001912 // Index buffer state is tied to the vertex array.
Brian Salomonae64c192019-02-05 09:41:37 -05001913 if (GrGpuBufferType::kIndex == type) {
cdaltone2e71c22016-04-07 18:13:29 -07001914 this->bindVertexArray(0);
cdaltondeacc972016-04-06 14:26:33 -07001915 }
cdaltone2e71c22016-04-07 18:13:29 -07001916
Brian Salomonae64c192019-02-05 09:41:37 -05001917 auto* bufferState = this->hwBufferState(type);
Brian Salomondbf70722019-02-07 11:31:24 -05001918 if (buffer->isCpuBuffer()) {
Brian Salomonae64c192019-02-05 09:41:37 -05001919 if (!bufferState->fBufferZeroKnownBound) {
1920 GL_CALL(BindBuffer(bufferState->fGLTarget, 0));
1921 bufferState->fBufferZeroKnownBound = true;
1922 bufferState->fBoundBufferUniqueID.makeInvalid();
cdaltone2e71c22016-04-07 18:13:29 -07001923 }
Brian Salomondbf70722019-02-07 11:31:24 -05001924 } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() !=
1925 bufferState->fBoundBufferUniqueID) {
Brian Salomonae64c192019-02-05 09:41:37 -05001926 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer);
1927 GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID()));
1928 bufferState->fBufferZeroKnownBound = false;
1929 bufferState->fBoundBufferUniqueID = glBuffer->uniqueID();
cdaltone2e71c22016-04-07 18:13:29 -07001930 }
1931
Brian Salomonae64c192019-02-05 09:41:37 -05001932 return bufferState->fGLTarget;
joshualitt93316b92015-10-23 09:08:08 -07001933}
Brian Salomond818ebf2018-07-02 14:08:49 +00001934
Michael Ludwig1e632792020-05-21 12:45:31 -04001935void GrGLGpu::clear(const GrScissorState& scissor, const SkPMColor4f& color,
Robert Phillips19e51dc2017-08-09 09:30:51 -04001936 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001937 // parent class should never let us get here with no RT
bsalomon49f085d2014-09-05 13:34:00 -07001938 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05001939 SkASSERT(!this->caps()->performColorClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04001940 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +00001941
Brian Salomon43f8bf02017-10-18 08:33:29 -04001942 this->handleDirtyContext();
bsalomon@google.com74b98712011-11-11 19:46:16 +00001943
Brian Salomon43f8bf02017-10-18 08:33:29 -04001944 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
1945
Michael Ludwig1e632792020-05-21 12:45:31 -04001946 if (scissor.enabled()) {
1947 this->flushRenderTarget(glRT, origin, scissor.rect());
Brian Salomon1fabd512018-02-09 09:54:25 -05001948 } else {
Chris Daltonc8ece3d2018-07-30 15:03:45 -06001949 this->flushRenderTarget(glRT);
Brian Salomon1fabd512018-02-09 09:54:25 -05001950 }
Michael Ludwig1e632792020-05-21 12:45:31 -04001951 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
1952 this->disableWindowRectangles();
Brian Salomon805cc7a2019-01-28 09:52:34 -05001953 this->flushColorWrite(true);
Chris Dalton674f77a2019-09-30 20:49:39 -06001954 this->flushClearColor(color);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001955 GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT));
reed@google.comac10a2d2010-12-22 21:39:39 +00001956}
1957
Chris Daltonb50cc812019-10-14 16:29:21 -06001958static bool use_tiled_rendering(const GrGLCaps& glCaps,
1959 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1960 // Only use the tiled rendering extension if we can explicitly clear and discard the stencil.
1961 // Otherwise it's faster to just not use it.
1962 return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp &&
1963 GrStoreOp::kDiscard == stencilLoadStore.fStoreOp;
1964}
1965
1966void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin,
Chris Dalton674f77a2019-09-30 20:49:39 -06001967 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
1968 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
1969 SkASSERT(!fIsExecutingCommandBuffer_DebugOnly);
1970
1971 this->handleDirtyContext();
1972
1973 auto glRT = static_cast<GrGLRenderTarget*>(rt);
1974 this->flushRenderTarget(glRT);
1975 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true);
1976
Chris Daltonb50cc812019-10-14 16:29:21 -06001977 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
1978 auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds);
1979 GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp)
1980 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
1981 SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering().
1982 GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth,
1983 nativeBounds.fHeight, preserveMask));
1984 }
1985
Chris Dalton674f77a2019-09-30 20:49:39 -06001986 GrGLbitfield clearMask = 0;
1987 if (GrLoadOp::kClear == colorLoadStore.fLoadOp) {
1988 SkASSERT(!this->caps()->performColorClearsAsDraws());
1989 this->flushClearColor(colorLoadStore.fClearColor);
1990 this->flushColorWrite(true);
1991 clearMask |= GR_GL_COLOR_BUFFER_BIT;
Robert Phillipscb2e2352017-08-30 16:44:40 -04001992 }
Chris Dalton674f77a2019-09-30 20:49:39 -06001993 if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) {
1994 SkASSERT(!this->caps()->performStencilClearsAsDraws());
1995 GL_CALL(StencilMask(0xffffffff));
1996 GL_CALL(ClearStencil(0));
1997 clearMask |= GR_GL_STENCIL_BUFFER_BIT;
1998 }
1999 if (clearMask) {
Chris Dalton2e7ed262020-02-21 15:17:59 -07002000 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Dalton674f77a2019-09-30 20:49:39 -06002001 this->disableWindowRectangles();
2002 GL_CALL(Clear(clearMask));
2003 }
2004}
2005
2006void GrGLGpu::endCommandBuffer(GrRenderTarget* rt,
2007 const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore,
2008 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) {
2009 SkASSERT(fIsExecutingCommandBuffer_DebugOnly);
2010
2011 this->handleDirtyContext();
2012
2013 if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) {
2014 // The framebuffer binding changed in the middle of a command buffer. We should have already
2015 // printed a warning during onFBOChanged.
2016 return;
2017 }
2018
2019 if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) {
2020 auto glRT = static_cast<GrGLRenderTarget*>(rt);
2021
2022 SkSTArray<2, GrGLenum> discardAttachments;
2023 if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) {
2024 discardAttachments.push_back(
2025 (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0);
2026 }
2027 if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) {
2028 discardAttachments.push_back(
2029 (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT);
2030 }
2031
2032 if (!discardAttachments.empty()) {
2033 if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) {
2034 GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2035 discardAttachments.begin()));
2036 } else {
2037 SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType());
2038 GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(),
2039 discardAttachments.begin()));
2040 }
2041 }
2042 }
2043
Chris Daltonb50cc812019-10-14 16:29:21 -06002044 if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) {
2045 GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp)
2046 ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE;
2047 // Handled by use_tiled_rendering().
2048 SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp);
2049 GL_CALL(EndTiling(preserveMask));
2050 }
2051
Chris Dalton674f77a2019-09-30 20:49:39 -06002052 SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false);
reed@google.comac10a2d2010-12-22 21:39:39 +00002053}
2054
Michael Ludwig1e632792020-05-21 12:45:31 -04002055void GrGLGpu::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask,
Robert Phillips19e51dc2017-08-09 09:30:51 -04002056 GrRenderTarget* target, GrSurfaceOrigin origin) {
bsalomon49f085d2014-09-05 13:34:00 -07002057 SkASSERT(target);
Michael Ludwigc39d0c82019-01-15 10:03:43 -05002058 SkASSERT(!this->caps()->performStencilClearsAsDraws());
Michael Ludwig1e632792020-05-21 12:45:31 -04002059 SkASSERT(!scissor.enabled() || !this->caps()->performPartialClearsAsDraws());
egdaniel9cb63402016-06-23 08:37:05 -07002060 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002061
Brian Salomonf7f54332020-07-28 09:23:35 -04002062 GrStencilAttachment* sb = target->getStencilAttachment();
Brian Salomon38c85d22020-01-29 13:04:22 -05002063 if (!sb) {
2064 // We should only get here if we marked a proxy as requiring a SB. However,
2065 // the SB creation could later fail. Likely clipping is going to go awry now.
2066 return;
2067 }
2068
bsalomon6bc1b5f2015-02-23 09:06:38 -08002069 GrGLint stencilBitCount = sb->bits();
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002070#if 0
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00002071 SkASSERT(stencilBitCount > 0);
twiz@google.com0f31ca72011-03-18 17:38:11 +00002072 GrGLint clipStencilMask = (1 << (stencilBitCount - 1));
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002073#else
2074 // we could just clear the clip bit but when we go through
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +00002075 // ANGLE a partial stencil mask will cause clears to be
Greg Danielf41b2bd2019-08-22 16:19:24 -04002076 // turned into draws. Our contract on GrOpsTask says that
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002077 // changing the clip between stencil passes may or may not
2078 // zero the client's clip bits. So we just clear the whole thing.
twiz@google.com0f31ca72011-03-18 17:38:11 +00002079 static const GrGLint clipStencilMask = ~0;
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +00002080#endif
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002081 GrGLint value;
csmartdalton29df7602016-08-31 11:55:52 -07002082 if (insideStencilMask) {
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002083 value = (1 << (stencilBitCount - 1));
2084 } else {
2085 value = 0;
2086 }
bsalomonb0bd4f62014-09-03 07:19:50 -07002087 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target);
Brian Salomon1fabd512018-02-09 09:54:25 -05002088 this->flushRenderTargetNoColorWrites(glRT);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002089
Michael Ludwig1e632792020-05-21 12:45:31 -04002090 this->flushScissor(scissor, glRT->width(), glRT->height(), origin);
2091 this->disableWindowRectangles();
bsalomon@google.coma3201942012-06-21 19:58:20 +00002092
caryclark@google.comcf6285b2012-06-06 12:09:01 +00002093 GL_CALL(StencilMask((uint32_t) clipStencilMask));
bsalomon@google.comab3dee52011-08-29 15:18:41 +00002094 GL_CALL(ClearStencil(value));
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002095 GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT));
bsalomon@google.com457b8a32012-05-21 21:19:58 +00002096 fHWStencilSettings.invalidate();
reed@google.comac10a2d2010-12-22 21:39:39 +00002097}
2098
Brian Salomone05ba5a2019-04-08 11:59:07 -04002099bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002100 GrColorType surfaceColorType, GrColorType dstColorType,
2101 void* offsetOrPtr, int rowWidthInPixels) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002102 SkASSERT(surface);
bsalomon39826022015-07-23 08:07:21 -07002103
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002104 auto format = surface->backendFormat().asGLFormat();
bsalomone9573312016-01-25 14:33:25 -08002105 GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002106 if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) {
bsalomon6cb3cbe2015-07-30 07:34:27 -07002107 return false;
2108 }
Greg Danielba88ab62019-07-26 09:14:01 -04002109 GrGLenum externalFormat = 0;
2110 GrGLenum externalType = 0;
Brian Salomond4764a12019-08-08 12:08:24 -04002111 this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(),
2112 surfaceColorType,
2113 dstColorType,
2114 &externalFormat,
2115 &externalType);
Greg Danielba88ab62019-07-26 09:14:01 -04002116 if (!externalFormat || !externalType) {
bsalomon76148af2016-01-12 11:13:47 -08002117 return false;
2118 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00002119
Brian Salomon71d9d842016-11-03 13:42:00 -04002120 if (renderTarget) {
Chris Daltonc139d082019-09-26 14:04:24 -06002121 if (renderTarget->numSamples() <= 1 ||
2122 renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0.
2123 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2124 this->flushRenderTargetNoColorWrites(renderTarget);
2125 } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) {
2126 SkASSERT(!renderTarget->requiresManualMSAAResolve());
2127 return false;
2128 } else {
2129 SkASSERT(renderTarget->requiresManualMSAAResolve());
2130 // we don't track the state of the READ FBO ID.
2131 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID());
Brian Salomon71d9d842016-11-03 13:42:00 -04002132 }
Brian Salomon71d9d842016-11-03 13:42:00 -04002133 } else {
2134 // Use a temporary FBO.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002135 this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
Robert Phillips294870f2016-11-11 12:38:40 -05002136 fHWBoundRenderTargetUniqueID.makeInvalid();
reed@google.comac10a2d2010-12-22 21:39:39 +00002137 }
2138
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00002139 // the read rect is viewport-relative
Chris Daltond6cda8d2019-09-05 02:30:04 -06002140 GrNativeRect readRect = {left, top, width, height};
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002141
Brian Salomona6948702018-06-01 15:33:20 -04002142 // determine if GL can read using the passed rowBytes or if we need a scratch buffer.
Brian Salomon26de56e2019-04-10 12:14:26 -04002143 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002144 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
Brian Salomon26de56e2019-04-10 12:14:26 -04002145 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels));
bsalomon@google.comc6980972011-11-02 19:57:21 +00002146 }
Brian Salomon8cbf6622019-07-30 11:03:14 -04002147 GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1));
bsalomonf46a1242015-12-15 12:37:38 -08002148
Brian Osman1348ed02018-09-12 09:08:39 -04002149 bool reattachStencil = false;
2150 if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() &&
2151 renderTarget &&
Brian Salomonf7f54332020-07-28 09:23:35 -04002152 renderTarget->getStencilAttachment() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -06002153 renderTarget->numSamples() > 1) {
Brian Osman1348ed02018-09-12 09:08:39 -04002154 // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached
2155 reattachStencil = true;
2156 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2157 GR_GL_RENDERBUFFER, 0));
2158 }
2159
Chris Dalton76500e52019-09-05 02:13:05 -06002160 GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight,
Brian Salomone05ba5a2019-04-08 11:59:07 -04002161 externalFormat, externalType, offsetOrPtr));
Brian Osman1348ed02018-09-12 09:08:39 -04002162
2163 if (reattachStencil) {
Brian Salomonf7f54332020-07-28 09:23:35 -04002164 auto* stencilAttachment =
2165 static_cast<GrGLStencilAttachment*>(renderTarget->getStencilAttachment());
Brian Osman1348ed02018-09-12 09:08:39 -04002166 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2167 GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID()));
2168 }
2169
Brian Salomon26de56e2019-04-10 12:14:26 -04002170 if (rowWidthInPixels != width) {
Brian Salomon1047a492019-07-02 12:25:21 -04002171 SkASSERT(this->glCaps().readPixelsRowBytesSupport());
bsalomon@google.comc6980972011-11-02 19:57:21 +00002172 GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0));
2173 }
reed@google.comac10a2d2010-12-22 21:39:39 +00002174
Brian Salomone05ba5a2019-04-08 11:59:07 -04002175 if (!renderTarget) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04002176 this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002177 }
2178 return true;
2179}
2180
2181bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002182 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2183 size_t rowBytes) {
Brian Salomone05ba5a2019-04-08 11:59:07 -04002184 SkASSERT(surface);
2185
Brian Salomonb28cb682019-07-26 12:48:47 -04002186 size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType);
Brian Salomone05ba5a2019-04-08 11:59:07 -04002187
Brian Salomon26de56e2019-04-10 12:14:26 -04002188 // GL_PACK_ROW_LENGTH is in terms of pixels not bytes.
2189 int rowPixelWidth;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002190
Brian Salomon1047a492019-07-02 12:25:21 -04002191 if (rowBytes == SkToSizeT(width * bytesPerPixel)) {
Brian Salomon26de56e2019-04-10 12:14:26 -04002192 rowPixelWidth = width;
2193 } else {
Brian Salomon1047a492019-07-02 12:25:21 -04002194 SkASSERT(!(rowBytes % bytesPerPixel));
2195 rowPixelWidth = rowBytes / bytesPerPixel;
Brian Salomone05ba5a2019-04-08 11:59:07 -04002196 }
Brian Salomonf77c1462019-08-01 15:19:29 -04002197 return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType,
2198 dstColorType, buffer, rowPixelWidth);
reed@google.comac10a2d2010-12-22 21:39:39 +00002199}
2200
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002201GrOpsRenderPass* GrGLGpu::getOpsRenderPass(
Robert Phillips96f22372020-05-20 12:31:18 -04002202 GrRenderTarget* rt, GrStencilAttachment*,
2203 GrSurfaceOrigin origin, const SkIRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002204 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -04002205 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -05002206 const SkTArray<GrSurfaceProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002207 if (!fCachedOpsRenderPass) {
John Stilesfbd050b2020-08-03 13:21:46 -04002208 fCachedOpsRenderPass = std::make_unique<GrGLOpsRenderPass>(this);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002209 }
2210
Chris Daltonb50cc812019-10-14 16:29:21 -06002211 fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo);
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002212 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -07002213}
2214
Brian Salomon1fabd512018-02-09 09:54:25 -05002215void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin,
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002216 const SkIRect& bounds) {
Brian Osman9aa30c62018-07-02 15:21:46 -04002217 this->flushRenderTargetNoColorWrites(target);
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002218 this->didWriteToSurface(target, origin, &bounds);
2219}
bsalomon6ba6fa12015-03-04 11:57:37 -08002220
Chris Daltonc8ece3d2018-07-30 15:03:45 -06002221void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) {
2222 this->flushRenderTargetNoColorWrites(target);
2223 this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr);
Brian Salomon1fabd512018-02-09 09:54:25 -05002224}
2225
Brian Osman9aa30c62018-07-02 15:21:46 -04002226void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) {
Brian Salomon1fabd512018-02-09 09:54:25 -05002227 SkASSERT(target);
Robert Phillips294870f2016-11-11 12:38:40 -05002228 GrGpuResource::UniqueID rtID = target->uniqueID();
egdanield803f272015-03-18 13:01:52 -07002229 if (fHWBoundRenderTargetUniqueID != rtID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002230 this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID());
egdanield803f272015-03-18 13:01:52 -07002231#ifdef SK_DEBUG
2232 // don't do this check in Chromium -- this is causing
2233 // lots of repeated command buffer flushes when the compositor is
2234 // rendering with Ganesh, which is really slow; even too slow for
2235 // Debug mode.
Brian Salomond8575452020-02-25 12:13:29 -05002236 if (!this->glCaps().skipErrorChecks()) {
egdanield803f272015-03-18 13:01:52 -07002237 GrGLenum status;
2238 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
2239 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
2240 SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status);
2241 }
bsalomon160f24c2015-03-17 15:55:42 -07002242 }
egdanield803f272015-03-18 13:01:52 -07002243#endif
2244 fHWBoundRenderTargetUniqueID = rtID;
Greg Danielacd66b42019-05-22 16:29:12 -04002245 this->flushViewport(target->width(), target->height());
brianosman64d094d2016-03-25 06:01:59 -07002246 }
2247
brianosman35b784d2016-05-05 11:52:53 -07002248 if (this->glCaps().srgbWriteControl()) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002249 this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat()));
bsalomon5cd020f2015-03-17 12:46:56 -07002250 }
Brian Salomon9b553272020-01-24 14:38:37 -05002251
2252 if (this->glCaps().shouldQueryImplementationReadSupport(target->format())) {
2253 GrGLint format;
2254 GrGLint type;
2255 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_FORMAT, &format);
2256 GR_GL_GetIntegerv(this->glInterface(), GR_GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2257 this->glCaps().didQueryImplementationReadSupport(target->format(), format, type);
2258 }
bsalomon083617b2016-02-12 12:10:14 -08002259}
bsalomona9909122016-01-23 10:41:40 -08002260
brianosman33f6b3f2016-06-02 05:49:21 -07002261void GrGLGpu::flushFramebufferSRGB(bool enable) {
2262 if (enable && kYes_TriState != fHWSRGBFramebuffer) {
2263 GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB));
2264 fHWSRGBFramebuffer = kYes_TriState;
2265 } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) {
2266 GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB));
2267 fHWSRGBFramebuffer = kNo_TriState;
2268 }
2269}
2270
Greg Danielacd66b42019-05-22 16:29:12 -04002271void GrGLGpu::flushViewport(int width, int height) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002272 GrNativeRect viewport = {0, 0, width, height};
bsalomon083617b2016-02-12 12:10:14 -08002273 if (fHWViewport != viewport) {
Chris Dalton76500e52019-09-05 02:13:05 -06002274 GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight));
bsalomon083617b2016-02-12 12:10:14 -08002275 fHWViewport = viewport;
2276 }
2277}
2278
Chris Dalton33db9fc2020-02-25 18:52:12 -07002279GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
Chris Daltond42d2002020-02-28 12:05:04 -07002280 fStats.incNumDraws();
2281
Chris Dalton2e7ed262020-02-21 15:17:59 -07002282 if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
2283 GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
2284 GL_CALL(Enable(GR_GL_CULL_FACE));
2285 GL_CALL(Disable(GR_GL_CULL_FACE));
bsalomon@google.com74749cd2013-01-30 16:12:41 +00002286 }
Chris Dalton2e7ed262020-02-21 15:17:59 -07002287 fLastPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +00002288
Chris Dalton3809bab2017-06-13 10:55:06 -06002289 switch (primitiveType) {
2290 case GrPrimitiveType::kTriangles:
2291 return GR_GL_TRIANGLES;
2292 case GrPrimitiveType::kTriangleStrip:
2293 return GR_GL_TRIANGLE_STRIP;
Chris Dalton3809bab2017-06-13 10:55:06 -06002294 case GrPrimitiveType::kPoints:
2295 return GR_GL_POINTS;
2296 case GrPrimitiveType::kLines:
2297 return GR_GL_LINES;
2298 case GrPrimitiveType::kLineStrip:
2299 return GR_GL_LINE_STRIP;
Chris Dalton5a2f9622019-12-27 14:56:38 -07002300 case GrPrimitiveType::kPatches:
2301 return GR_GL_PATCHES;
Robert Phillips571177f2019-10-04 14:41:49 -04002302 case GrPrimitiveType::kPath:
2303 SK_ABORT("non-mesh-based GrPrimitiveType");
2304 return 0;
Chris Dalton3809bab2017-06-13 10:55:06 -06002305 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002306 SK_ABORT("invalid GrPrimitiveType");
Chris Dalton3809bab2017-06-13 10:55:06 -06002307}
2308
Jim Van Verthbb61fe32020-07-07 16:39:04 -04002309void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) {
Chris Daltonc139d082019-09-26 14:04:24 -06002310 // Some extensions automatically resolves the texture when it is read.
2311 SkASSERT(this->glCaps().usesMSAARenderBuffers());
2312
bsalomon@google.com75f9f252012-01-31 13:35:56 +00002313 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target);
Chris Daltonc139d082019-09-26 14:04:24 -06002314 SkASSERT(rt->textureFBOID() != rt->renderFBOID());
2315 SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0);
2316 this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID());
2317 this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID());
Adrienne Walker4ee88512018-05-17 11:37:14 -07002318
Chris Daltonc139d082019-09-26 14:04:24 -06002319 // make sure we go through flushRenderTarget() since we've modified
2320 // the bound DRAW FBO ID.
2321 fHWBoundRenderTargetUniqueID.makeInvalid();
2322 if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) {
2323 // Apple's extension uses the scissor as the blit bounds.
Greg Daniel242536f2020-02-13 14:12:46 -05002324 // Passing in kTopLeft_GrSurfaceOrigin will make sure no transformation of the rect
2325 // happens inside flushScissor since resolveRect is already in native device coordinates.
Michael Ludwigd1d997e2020-06-04 15:52:44 -04002326 GrScissorState scissor(rt->dimensions());
2327 SkAssertResult(scissor.set(resolveRect));
2328 this->flushScissor(scissor, rt->width(), rt->height(), kTopLeft_GrSurfaceOrigin);
Chris Daltonc139d082019-09-26 14:04:24 -06002329 this->disableWindowRectangles();
2330 GL_CALL(ResolveMultisampleFramebuffer());
2331 } else {
2332 int l, b, r, t;
2333 if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag &
2334 this->glCaps().blitFramebufferSupportFlags()) {
2335 l = 0;
2336 b = 0;
2337 r = target->width();
2338 t = target->height();
2339 } else {
Greg Daniel242536f2020-02-13 14:12:46 -05002340 l = resolveRect.x();
2341 b = resolveRect.y();
2342 r = resolveRect.x() + resolveRect.width();
2343 t = resolveRect.y() + resolveRect.height();
reed@google.comac10a2d2010-12-22 21:39:39 +00002344 }
Chris Daltonc139d082019-09-26 14:04:24 -06002345
2346 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07002347 this->flushScissorTest(GrScissorTest::kDisabled);
Chris Daltonc139d082019-09-26 14:04:24 -06002348 this->disableWindowRectangles();
2349 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 +00002350 }
2351}
2352
bsalomon@google.com411dad02012-06-05 20:24:20 +00002353namespace {
bsalomon@google.comd302f142011-03-03 13:54:13 +00002354
bsalomon@google.com411dad02012-06-05 20:24:20 +00002355
2356GrGLenum gr_to_gl_stencil_op(GrStencilOp op) {
cdalton93a379b2016-05-11 13:58:08 -07002357 static const GrGLenum gTable[kGrStencilOpCount] = {
2358 GR_GL_KEEP, // kKeep
2359 GR_GL_ZERO, // kZero
2360 GR_GL_REPLACE, // kReplace
2361 GR_GL_INVERT, // kInvert
2362 GR_GL_INCR_WRAP, // kIncWrap
2363 GR_GL_DECR_WRAP, // kDecWrap
2364 GR_GL_INCR, // kIncClamp
2365 GR_GL_DECR, // kDecClamp
bsalomon@google.com411dad02012-06-05 20:24:20 +00002366 };
Brian Salomon4dea72a2019-12-18 10:43:10 -05002367 static_assert(0 == (int)GrStencilOp::kKeep);
2368 static_assert(1 == (int)GrStencilOp::kZero);
2369 static_assert(2 == (int)GrStencilOp::kReplace);
2370 static_assert(3 == (int)GrStencilOp::kInvert);
2371 static_assert(4 == (int)GrStencilOp::kIncWrap);
2372 static_assert(5 == (int)GrStencilOp::kDecWrap);
2373 static_assert(6 == (int)GrStencilOp::kIncClamp);
2374 static_assert(7 == (int)GrStencilOp::kDecClamp);
cdalton93a379b2016-05-11 13:58:08 -07002375 SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
2376 return gTable[(int)op];
bsalomon@google.com411dad02012-06-05 20:24:20 +00002377}
2378
2379void set_gl_stencil(const GrGLInterface* gl,
cdalton93a379b2016-05-11 13:58:08 -07002380 const GrStencilSettings::Face& face,
2381 GrGLenum glFace) {
2382 GrGLenum glFunc = GrToGLStencilFunc(face.fTest);
2383 GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp);
2384 GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp);
bsalomon@google.coma3201942012-06-21 19:58:20 +00002385
cdalton93a379b2016-05-11 13:58:08 -07002386 GrGLint ref = face.fRef;
2387 GrGLint mask = face.fTestMask;
2388 GrGLint writeMask = face.fWriteMask;
bsalomon@google.com411dad02012-06-05 20:24:20 +00002389
2390 if (GR_GL_FRONT_AND_BACK == glFace) {
2391 // we call the combined func just in case separate stencil is not
2392 // supported.
2393 GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask));
2394 GR_GL_CALL(gl, StencilMask(writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002395 GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002396 } else {
2397 GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask));
2398 GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask));
vbuzinovc5d58f02015-08-21 05:24:24 -07002399 GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp));
bsalomon@google.com411dad02012-06-05 20:24:20 +00002400 }
2401}
2402}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002403
Chris Dalton71713f62019-04-18 12:41:03 -06002404void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002405 if (stencilSettings.isDisabled()) {
2406 this->disableStencil();
Chris Dalton71713f62019-04-18 12:41:03 -06002407 } else if (fHWStencilSettings != stencilSettings ||
2408 (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) {
csmartdaltonc7d85332016-10-26 10:13:46 -07002409 if (kYes_TriState != fHWStencilTestEnabled) {
2410 GL_CALL(Enable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002411
csmartdaltonc7d85332016-10-26 10:13:46 -07002412 fHWStencilTestEnabled = kYes_TriState;
bsalomon@google.coma3201942012-06-21 19:58:20 +00002413 }
Chris Dalton67d43fe2019-11-05 23:01:21 -07002414 if (!stencilSettings.isTwoSided()) {
2415 set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(),
2416 GR_GL_FRONT_AND_BACK);
csmartdaltonc7d85332016-10-26 10:13:46 -07002417 } else {
Chris Dalton67d43fe2019-11-05 23:01:21 -07002418 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin),
2419 GR_GL_FRONT);
2420 set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin),
2421 GR_GL_BACK);
bsalomon@google.comd302f142011-03-03 13:54:13 +00002422 }
joshualitta58fe352014-10-27 08:39:00 -07002423 fHWStencilSettings = stencilSettings;
Chris Dalton71713f62019-04-18 12:41:03 -06002424 fHWStencilOrigin = origin;
reed@google.comac10a2d2010-12-22 21:39:39 +00002425 }
2426}
2427
csmartdaltonc7d85332016-10-26 10:13:46 -07002428void GrGLGpu::disableStencil() {
2429 if (kNo_TriState != fHWStencilTestEnabled) {
2430 GL_CALL(Disable(GR_GL_STENCIL_TEST));
Brian Salomonaf971de2017-06-08 16:11:33 -04002431
csmartdaltonc7d85332016-10-26 10:13:46 -07002432 fHWStencilTestEnabled = kNo_TriState;
2433 fHWStencilSettings.invalidate();
2434 }
2435}
2436
Chris Dalton4c56b032019-03-04 12:28:42 -07002437void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) {
bsalomon083617b2016-02-12 12:10:14 -08002438 // rt is only optional if useHWAA is false.
2439 SkASSERT(rt || !useHWAA);
Chris Daltoneffee202019-07-01 22:28:03 -06002440#ifdef SK_DEBUG
2441 if (useHWAA && rt->numSamples() <= 1) {
2442 SkASSERT(this->caps()->mixedSamplesSupport());
2443 SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID());
Brian Salomonf7f54332020-07-28 09:23:35 -04002444 SkASSERT(rt->getStencilAttachment());
Chris Daltoneffee202019-07-01 22:28:03 -06002445 }
2446#endif
bsalomon@google.com202d1392013-03-19 18:58:08 +00002447
csmartdalton2b5f2cb2016-06-10 14:06:32 -07002448 if (this->caps()->multisampleDisableSupport()) {
cdaltond0a840d2015-03-16 17:19:58 -07002449 if (useHWAA) {
2450 if (kYes_TriState != fMSAAEnabled) {
2451 GL_CALL(Enable(GR_GL_MULTISAMPLE));
2452 fMSAAEnabled = kYes_TriState;
2453 }
2454 } else {
2455 if (kNo_TriState != fMSAAEnabled) {
2456 GL_CALL(Disable(GR_GL_MULTISAMPLE));
2457 fMSAAEnabled = kNo_TriState;
bsalomon@google.comf954d8d2011-04-06 17:50:02 +00002458 }
2459 }
2460 }
2461}
2462
Chris Daltonce425af2019-12-16 10:39:03 -07002463void GrGLGpu::flushConservativeRasterState(bool enabled) {
2464 if (this->caps()->conservativeRasterSupport()) {
2465 if (enabled) {
2466 if (kYes_TriState != fHWConservativeRasterEnabled) {
2467 GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION));
2468 fHWConservativeRasterEnabled = kYes_TriState;
2469 }
2470 } else {
2471 if (kNo_TriState != fHWConservativeRasterEnabled) {
2472 GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION));
2473 fHWConservativeRasterEnabled = kNo_TriState;
2474 }
2475 }
2476 }
2477}
2478
Chris Dalton1215cda2019-12-17 21:44:04 -07002479void GrGLGpu::flushWireframeState(bool enabled) {
2480 if (this->caps()->wireframeSupport()) {
2481 if (this->caps()->wireframeMode() || enabled) {
2482 if (kYes_TriState != fHWWireframeEnabled) {
2483 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE));
2484 fHWWireframeEnabled = kYes_TriState;
2485 }
2486 } else {
2487 if (kNo_TriState != fHWWireframeEnabled) {
2488 GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL));
2489 fHWWireframeEnabled = kNo_TriState;
2490 }
2491 }
2492 }
2493}
2494
Chris Daltonbbb3f642019-07-24 12:25:08 -04002495void GrGLGpu::flushBlendAndColorWrite(
2496 const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) {
2497 if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) {
2498 // We need to work around a driver bug by using a blend state that preserves the dst color,
2499 // rather than disabling color writes.
2500 GrXferProcessor::BlendInfo preserveDstBlend;
2501 preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff;
2502 preserveDstBlend.fDstBlend = kOne_GrBlendCoeff;
2503 this->flushBlendAndColorWrite(preserveDstBlend, swizzle);
2504 return;
2505 }
bsalomonf7cc8772015-05-11 11:21:14 -07002506
cdalton8917d622015-05-06 13:40:21 -07002507 GrBlendEquation equation = blendInfo.fEquation;
egdanielc2304142014-12-11 13:15:13 -08002508 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
2509 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002510
2511 // Any optimization to disable blending should have already been applied and
2512 // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0).
Greg Daniel0a335512020-03-31 09:14:57 -04002513 bool blendOff = GrBlendShouldDisable(equation, srcCoeff, dstCoeff) ||
2514 !blendInfo.fWriteColor;
Chris Daltonbbb3f642019-07-24 12:25:08 -04002515
egdanielb414f252014-07-29 13:15:47 -07002516 if (blendOff) {
2517 if (kNo_TriState != fHWBlendState.fEnabled) {
2518 GL_CALL(Disable(GR_GL_BLEND));
joel.liang9764c402015-07-09 19:46:18 -07002519
Jim Van Verth1bef9792020-07-09 08:09:13 -04002520 // Workaround for the ARM KHR_blend_equation_advanced disable flags issue
joel.liang9764c402015-07-09 19:46:18 -07002521 // https://code.google.com/p/skia/issues/detail?id=3943
2522 if (kARM_GrGLVendor == this->ctxInfo().vendor() &&
2523 GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) {
2524 SkASSERT(this->caps()->advancedBlendEquationSupport());
2525 // Set to any basic blending equation.
2526 GrBlendEquation blend_equation = kAdd_GrBlendEquation;
2527 GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation]));
2528 fHWBlendState.fEquation = blend_equation;
2529 }
2530
egdanielb414f252014-07-29 13:15:47 -07002531 fHWBlendState.fEnabled = kNo_TriState;
2532 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002533 } else {
2534 if (kYes_TriState != fHWBlendState.fEnabled) {
2535 GL_CALL(Enable(GR_GL_BLEND));
cdalton8917d622015-05-06 13:40:21 -07002536
Chris Daltonbbb3f642019-07-24 12:25:08 -04002537 fHWBlendState.fEnabled = kYes_TriState;
2538 }
Brian Salomonaf971de2017-06-08 16:11:33 -04002539
Chris Daltonbbb3f642019-07-24 12:25:08 -04002540 if (fHWBlendState.fEquation != equation) {
2541 GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation]));
2542 fHWBlendState.fEquation = equation;
2543 }
cdalton8917d622015-05-06 13:40:21 -07002544
Chris Daltonbbb3f642019-07-24 12:25:08 -04002545 if (GrBlendEquationIsAdvanced(equation)) {
2546 SkASSERT(this->caps()->advancedBlendEquationSupport());
2547 // Advanced equations have no other blend state.
2548 return;
2549 }
cdalton8917d622015-05-06 13:40:21 -07002550
Chris Daltonbbb3f642019-07-24 12:25:08 -04002551 if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) {
2552 GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff],
2553 gXfermodeCoeff2Blend[dstCoeff]));
2554 fHWBlendState.fSrcCoeff = srcCoeff;
2555 fHWBlendState.fDstCoeff = dstCoeff;
2556 }
cdalton8917d622015-05-06 13:40:21 -07002557
Greg Daniel6e2af5c2020-03-30 15:07:05 -04002558 if ((GrBlendCoeffRefsConstant(srcCoeff) || GrBlendCoeffRefsConstant(dstCoeff))) {
Chris Daltonbbb3f642019-07-24 12:25:08 -04002559 SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
2560 if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) {
2561 GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA));
2562 fHWBlendState.fConstColor = blendConst;
2563 fHWBlendState.fConstColorValid = true;
2564 }
bsalomon7f9b2e42016-01-12 13:29:26 -08002565 }
bsalomon@google.com0650e812011-04-08 18:07:53 +00002566 }
Chris Daltonbbb3f642019-07-24 12:25:08 -04002567
2568 this->flushColorWrite(blendInfo.fWriteColor);
bsalomon@google.com0650e812011-04-08 18:07:53 +00002569}
bsalomon@google.com0a97be22011-11-08 19:20:57 +00002570
Brian Salomondc829942018-10-23 16:07:24 -04002571static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) {
Brian Salomon327955b2018-10-22 15:39:22 +00002572 for (int i = 0; i < 4; ++i) {
Brian Salomondc829942018-10-23 16:07:24 -04002573 switch (swizzle[i]) {
2574 case 'r': glValues[i] = GR_GL_RED; break;
2575 case 'g': glValues[i] = GR_GL_GREEN; break;
2576 case 'b': glValues[i] = GR_GL_BLUE; break;
2577 case 'a': glValues[i] = GR_GL_ALPHA; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002578 case '0': glValues[i] = GR_GL_ZERO; break;
Greg Daniel216a9d72019-02-20 10:12:29 -05002579 case '1': glValues[i] = GR_GL_ONE; break;
Brian Salomondc829942018-10-23 16:07:24 -04002580 default: SK_ABORT("Unsupported component");
2581 }
Brian Salomon327955b2018-10-22 15:39:22 +00002582 }
2583}
2584
Greg Daniel2c3398d2019-06-19 11:58:01 -04002585void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle,
2586 GrGLTexture* texture) {
bsalomon49f085d2014-09-05 13:34:00 -07002587 SkASSERT(texture);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00002588
reed856e9d92015-09-30 12:21:45 -07002589#ifdef SK_DEBUG
2590 if (!this->caps()->npotTextureTileSupport()) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -04002591 if (samplerState.isRepeated()) {
reed856e9d92015-09-30 12:21:45 -07002592 const int w = texture->width();
2593 const int h = texture->height();
2594 SkASSERT(SkIsPow2(w) && SkIsPow2(h));
2595 }
2596 }
2597#endif
2598
Robert Phillips294870f2016-11-11 12:38:40 -05002599 GrGpuResource::UniqueID textureID = texture->uniqueID();
bsalomon10528f12015-10-14 12:54:52 -07002600 GrGLenum target = texture->target();
Brian Salomond978b902019-02-07 15:09:18 -05002601 if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) {
bsalomon@google.com34cccde2013-01-04 18:34:30 +00002602 this->setTextureUnit(unitIdx);
bsalomon10528f12015-10-14 12:54:52 -07002603 GL_CALL(BindTexture(target, texture->textureID()));
Brian Salomond978b902019-02-07 15:09:18 -05002604 fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID);
bsalomon@google.com4c883782012-06-04 19:05:11 +00002605 }
2606
Brian Salomone69b9ef2020-07-22 11:18:06 -04002607 if (samplerState.mipmapped() == GrMipmapped::kYes) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002608 if (!this->caps()->mipmapSupport() || texture->mipmapped() == GrMipmapped::kNo) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002609 samplerState.setMipmapMode(GrSamplerState::MipmapMode::kNone);
2610 } else {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002611 SkASSERT(!texture->mipmapsAreDirty());
bsalomonefd7d452014-10-23 14:17:46 -07002612 }
commit-bot@chromium.org47442312013-12-19 16:18:01 +00002613 }
bsalomonefd7d452014-10-23 14:17:46 -07002614
Brian Salomone2826ab2019-06-04 15:58:31 -04002615 auto timestamp = texture->parameters()->resetTimestamp();
2616 bool setAll = timestamp < fResetTimestampForTextureParameters;
Brian Salomone2826ab2019-06-04 15:58:31 -04002617 const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr;
2618 GrGLTextureParameters::SamplerOverriddenState newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002619 if (fSamplerObjectCache) {
2620 fSamplerObjectCache->bindSampler(unitIdx, samplerState);
Greg Daniel95afafb2020-07-22 12:09:26 -04002621 if (this->glCaps().mustSetAnyTexParameterToEnableMipmapping()) {
Brian Salomone69b9ef2020-07-22 11:18:06 -04002622 if (samplerState.mipmapped() == GrMipmapped::kYes) {
2623 GrGLenum minFilter = filter_to_gl_min_filter(samplerState.filter(),
2624 samplerState.mipmapMode());
Brian Salomon1908bb82020-05-13 12:22:54 -04002625 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2626 texture->parameters()->samplerOverriddenState();
Greg Daniel95afafb2020-07-22 12:09:26 -04002627 this->setTextureUnit(unitIdx);
2628 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, minFilter));
2629 newSamplerState = oldSamplerState;
2630 newSamplerState.fMinFilter = minFilter;
2631 samplerStateToRecord = &newSamplerState;
Brian Salomon1908bb82020-05-13 12:22:54 -04002632 }
2633 }
Brian Salomondc829942018-10-23 16:07:24 -04002634 } else {
Brian Salomone2826ab2019-06-04 15:58:31 -04002635 const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState =
2636 texture->parameters()->samplerOverriddenState();
2637 samplerStateToRecord = &newSamplerState;
Brian Salomondc829942018-10-23 16:07:24 -04002638
Brian Salomone69b9ef2020-07-22 11:18:06 -04002639 newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter(),
2640 samplerState.mipmapMode());
Brian Salomone2826ab2019-06-04 15:58:31 -04002641 newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter());
Brian Salomondc829942018-10-23 16:07:24 -04002642
Brian Salomone2826ab2019-06-04 15:58:31 -04002643 newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps());
2644 newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps());
Brian Salomondc829942018-10-23 16:07:24 -04002645
2646 // These are the OpenGL default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04002647 newSamplerState.fMinLOD = -1000.f;
2648 newSamplerState.fMaxLOD = 1000.f;
Brian Salomondc829942018-10-23 16:07:24 -04002649
Brian Salomone2826ab2019-06-04 15:58:31 -04002650 if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002651 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002652 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002653 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002654 if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) {
Brian Salomondc829942018-10-23 16:07:24 -04002655 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002656 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter));
Brian Salomondc829942018-10-23 16:07:24 -04002657 }
Brian Salomon69100f02020-07-21 10:49:25 -04002658 if (this->glCaps().mipmapLevelAndLodControlSupport()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002659 if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) {
Mike Klein1bccae52018-10-19 11:38:44 +00002660 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002661 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002662 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002663 if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) {
Brian Salomondc829942018-10-23 16:07:24 -04002664 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002665 GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD));
Brian Salomondc829942018-10-23 16:07:24 -04002666 }
2667 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002668 if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) {
Brian Salomondc829942018-10-23 16:07:24 -04002669 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002670 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS));
Brian Salomondc829942018-10-23 16:07:24 -04002671 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002672 if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) {
Brian Salomondc829942018-10-23 16:07:24 -04002673 this->setTextureUnit(unitIdx);
Brian Salomone2826ab2019-06-04 15:58:31 -04002674 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT));
Brian Salomondc829942018-10-23 16:07:24 -04002675 }
Michael Ludwigf23a1522018-12-10 11:36:13 -05002676 if (this->glCaps().clampToBorderSupport()) {
2677 // Make sure the border color is transparent black (the default)
Brian Salomone2826ab2019-06-04 15:58:31 -04002678 if (setAll || oldSamplerState.fBorderColorInvalid) {
Michael Ludwigf23a1522018-12-10 11:36:13 -05002679 this->setTextureUnit(unitIdx);
Brian Salomon89f2ff12018-12-07 19:30:25 -05002680 static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f};
2681 GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack));
Michael Ludwigf23a1522018-12-10 11:36:13 -05002682 }
2683 }
Brian Salomondc829942018-10-23 16:07:24 -04002684 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002685 GrGLTextureParameters::NonsamplerState newNonsamplerState;
2686 newNonsamplerState.fBaseMipMapLevel = 0;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002687 newNonsamplerState.fMaxMipmapLevel = texture->maxMipmapLevel();
Brian Salomondc829942018-10-23 16:07:24 -04002688
Brian Salomone2826ab2019-06-04 15:58:31 -04002689 const GrGLTextureParameters::NonsamplerState& oldNonsamplerState =
2690 texture->parameters()->nonsamplerState();
Brian Salomon68ba1172019-06-05 11:15:08 -04002691 if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002692 newNonsamplerState.fSwizzleKey = swizzle.asKey();
2693 if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) {
Brian Salomondc829942018-10-23 16:07:24 -04002694 GrGLenum glValues[4];
2695 get_gl_swizzle_values(swizzle, glValues);
2696 this->setTextureUnit(unitIdx);
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002697 if (GR_IS_GR_GL(this->glStandard())) {
Brian Salomon4dea72a2019-12-18 10:43:10 -05002698 static_assert(sizeof(glValues[0]) == sizeof(GrGLint));
Kevin Lubick8aa203c2019-03-19 13:23:10 -04002699 GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA,
2700 reinterpret_cast<const GrGLint*>(glValues)));
2701 } else if (GR_IS_GR_GL_ES(this->glStandard())) {
Brian Salomondc829942018-10-23 16:07:24 -04002702 // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA.
2703 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0]));
2704 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1]));
2705 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2]));
2706 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3]));
Brian Salomonbfb3df42018-10-19 19:24:31 +00002707 }
Brian Salomonbfb3df42018-10-19 19:24:31 +00002708 }
2709 }
Brian Salomondc829942018-10-23 16:07:24 -04002710 // These are not supported in ES2 contexts
Brian Salomon69100f02020-07-21 10:49:25 -04002711 if (this->glCaps().mipmapLevelAndLodControlSupport() &&
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002712 (texture->textureType() != GrTextureType::kExternal ||
Brian Salomonf3841932018-11-29 09:13:37 -05002713 !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) {
Brian Salomone2826ab2019-06-04 15:58:31 -04002714 if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002715 this->setTextureUnit(unitIdx);
2716 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL,
Brian Salomone2826ab2019-06-04 15:58:31 -04002717 newNonsamplerState.fBaseMipMapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002718 }
Brian Salomon8c82a872020-07-21 12:09:58 -04002719 if (newNonsamplerState.fMaxMipmapLevel != oldNonsamplerState.fMaxMipmapLevel) {
Brian Salomondc829942018-10-23 16:07:24 -04002720 this->setTextureUnit(unitIdx);
2721 GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL,
Brian Salomon8c82a872020-07-21 12:09:58 -04002722 newNonsamplerState.fMaxMipmapLevel));
Brian Salomondc829942018-10-23 16:07:24 -04002723 }
Brian Salomon327955b2018-10-22 15:39:22 +00002724 }
Brian Salomone2826ab2019-06-04 15:58:31 -04002725 texture->parameters()->set(samplerStateToRecord, newNonsamplerState,
2726 fResetTimestampForTextureParameters);
cdalton74b8d322016-04-11 14:47:28 -07002727}
2728
Brian Salomon1f05d452019-02-08 12:33:08 -05002729void GrGLGpu::onResetTextureBindings() {
2730 static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE,
2731 GR_GL_TEXTURE_EXTERNAL};
2732 for (int i = 0; i < this->numTextureUnits(); ++i) {
2733 this->setTextureUnit(i);
2734 for (auto target : kTargets) {
2735 if (fHWTextureUnitBindings[i].hasBeenModified(target)) {
2736 GL_CALL(BindTexture(target, 0));
2737 }
2738 }
2739 fHWTextureUnitBindings[i].invalidateAllTargets(true);
2740 }
2741}
2742
Chris Dalton5a2f9622019-12-27 14:56:38 -07002743void GrGLGpu::flushPatchVertexCount(uint8_t count) {
2744 SkASSERT(this->caps()->shaderCaps()->tessellationSupport());
2745 if (fHWPatchVertexCount != count) {
2746 GL_CALL(PatchParameteri(GR_GL_PATCH_VERTICES, count));
2747 fHWPatchVertexCount = count;
2748 }
2749}
2750
egdaniel080e6732014-12-22 07:35:52 -08002751void GrGLGpu::flushColorWrite(bool writeColor) {
2752 if (!writeColor) {
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002753 if (kNo_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002754 GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE,
2755 GR_GL_FALSE, GR_GL_FALSE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002756 fHWWriteToColor = kNo_TriState;
bsalomon@google.comd302f142011-03-03 13:54:13 +00002757 }
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002758 } else {
2759 if (kYes_TriState != fHWWriteToColor) {
Brian Osmana9aaef02019-05-06 20:07:20 +00002760 GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE));
bsalomon@google.com978c8c62012-05-21 14:45:49 +00002761 fHWWriteToColor = kYes_TriState;
2762 }
bsalomon@google.comd302f142011-03-03 13:54:13 +00002763 }
bsalomon3e791242014-12-17 13:43:13 -08002764}
bsalomon@google.comd302f142011-03-03 13:54:13 +00002765
Chris Dalton674f77a2019-09-30 20:49:39 -06002766void GrGLGpu::flushClearColor(const SkPMColor4f& color) {
2767 GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA;
2768 if (this->glCaps().clearToBoundaryValuesIsBroken() &&
2769 (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) {
2770 static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f);
2771 static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f);
2772 a = (1 == a) ? safeAlpha1 : safeAlpha0;
2773 }
Brian Salomon805cc7a2019-01-28 09:52:34 -05002774 if (r != fHWClearColor[0] || g != fHWClearColor[1] ||
2775 b != fHWClearColor[2] || a != fHWClearColor[3]) {
2776 GL_CALL(ClearColor(r, g, b, a));
2777 fHWClearColor[0] = r;
2778 fHWClearColor[1] = g;
2779 fHWClearColor[2] = b;
2780 fHWClearColor[3] = a;
2781 }
2782}
2783
bsalomon861e1032014-12-16 07:33:49 -08002784void GrGLGpu::setTextureUnit(int unit) {
Brian Salomond978b902019-02-07 15:09:18 -05002785 SkASSERT(unit >= 0 && unit < this->numTextureUnits());
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002786 if (unit != fHWActiveTextureUnitIdx) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00002787 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit));
bsalomon@google.com49209392012-06-05 15:13:46 +00002788 fHWActiveTextureUnitIdx = unit;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002789 }
2790}
bsalomon@google.com316f99232011-01-13 21:28:12 +00002791
Brian Salomond978b902019-02-07 15:09:18 -05002792void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) {
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002793 // Bind the last texture unit since it is the least likely to be used by GrGLProgram.
Brian Salomond978b902019-02-07 15:09:18 -05002794 int lastUnitIdx = this->numTextureUnits() - 1;
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +00002795 if (lastUnitIdx != fHWActiveTextureUnitIdx) {
2796 GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx));
2797 fHWActiveTextureUnitIdx = lastUnitIdx;
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002798 }
Brian Salomond978b902019-02-07 15:09:18 -05002799 // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the
2800 // correct texture.
Brian Salomon1f05d452019-02-08 12:33:08 -05002801 fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target);
Brian Salomond978b902019-02-07 15:09:18 -05002802 GL_CALL(BindTexture(target, textureID));
bsalomon@google.com8531c1c2011-01-13 19:52:45 +00002803}
2804
Brian Salomone5e7eb12016-10-14 16:18:33 -04002805// Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface.
Greg Daniel46cfbc62019-06-07 11:43:30 -04002806static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst,
2807 const GrSurface* src,
2808 const SkIRect& srcRect,
2809 const SkIPoint& dstPoint,
2810 const GrGLCaps& caps) {
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002811 int dstSampleCnt = 0;
2812 int srcSampleCnt = 0;
2813 if (const GrRenderTarget* rt = dst->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002814 dstSampleCnt = rt->numSamples();
bsalomon@google.comeb851172013-04-15 13:51:00 +00002815 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002816 if (const GrRenderTarget* rt = src->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002817 srcSampleCnt = rt->numSamples();
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002818 }
2819 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget()));
2820 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget()));
2821
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002822 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2823 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2824
Brian Salomone5e7eb12016-10-14 16:18:33 -04002825 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
Mike Klein1d746202018-01-25 17:32:51 -05002826 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002827
Greg Daniel46cfbc62019-06-07 11:43:30 -04002828 GrTextureType dstTexType;
2829 GrTextureType* dstTexTypePtr = nullptr;
2830 GrTextureType srcTexType;
2831 GrTextureType* srcTexTypePtr = nullptr;
2832 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002833 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002834 dstTexTypePtr = &dstTexType;
2835 }
2836 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002837 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002838 srcTexTypePtr = &srcTexType;
2839 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002840
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002841 return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr,
2842 srcFormat, srcSampleCnt, srcTexTypePtr,
Greg Daniel46cfbc62019-06-07 11:43:30 -04002843 src->getBoundsRect(), true, srcRect, dstPoint);
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00002844}
bsalomon@google.comeb851172013-04-15 13:51:00 +00002845
Brian Osmana9c8a052018-01-19 10:31:56 -05002846static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) {
2847 // A RT has a separate MSAA renderbuffer if:
2848 // 1) It's multisampled
2849 // 2) We're using an extension with separate MSAA renderbuffers
2850 // 3) It's not FBO 0, which is special and always auto-resolves
Chris Dalton6ce447a2019-06-23 18:07:38 -06002851 return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0;
Brian Osmana9c8a052018-01-19 10:31:56 -05002852}
2853
Greg Daniel46cfbc62019-06-07 11:43:30 -04002854static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src,
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002855 const GrGLCaps& caps) {
2856
bsalomon@google.comeb851172013-04-15 13:51:00 +00002857 const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget());
bsalomon@google.coma2719852013-04-17 14:25:27 +00002858 const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget());
bsalomon7ea33f52015-11-22 14:51:00 -08002859 const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture());
bsalomon7ea33f52015-11-22 14:51:00 -08002860 const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture());
cblume61214052016-01-26 09:10:48 -08002861
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002862 bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false;
2863 bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false;
2864
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002865 GrGLFormat dstFormat = dst->backendFormat().asGLFormat();
2866 GrGLFormat srcFormat = src->backendFormat().asGLFormat();
2867
Greg Daniel46cfbc62019-06-07 11:43:30 -04002868 GrTextureType dstTexType;
2869 GrTextureType* dstTexTypePtr = nullptr;
2870 GrTextureType srcTexType;
2871 GrTextureType* srcTexTypePtr = nullptr;
2872 if (dstTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002873 dstTexType = dstTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002874 dstTexTypePtr = &dstTexType;
2875 }
2876 if (srcTex) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04002877 srcTexType = srcTex->textureType();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002878 srcTexTypePtr = &srcTexType;
2879 }
Greg Daniel26dbe3b2018-05-03 10:35:42 -04002880
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002881 return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr,
2882 srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr);
bsalomon@google.comeb851172013-04-15 13:51:00 +00002883}
2884
Greg Danielacd66b42019-05-22 16:29:12 -04002885// If a temporary FBO was created, its non-zero ID is returned.
Brian Salomond2a8ae22019-09-10 16:03:59 -04002886void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget,
Brian Salomon71d9d842016-11-03 13:42:00 -04002887 TempFBOTarget tempFBOTarget) {
bsalomon@google.comeb851172013-04-15 13:51:00 +00002888 GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
Brian Salomond2a8ae22019-09-10 16:03:59 -04002889 if (!rt || mipLevel > 0) {
bsalomon49f085d2014-09-05 13:34:00 -07002890 SkASSERT(surface->asTexture());
Brian Salomon9bada542017-06-12 12:09:30 -04002891 GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture());
2892 GrGLuint texID = texture->textureID();
2893 GrGLenum target = texture->target();
egdanield803f272015-03-18 13:01:52 -07002894 GrGLuint* tempFBOID;
2895 tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID;
egdaniel0f5f9672015-02-03 11:10:51 -08002896
egdanield803f272015-03-18 13:01:52 -07002897 if (0 == *tempFBOID) {
2898 GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID));
egdaniel0f5f9672015-02-03 11:10:51 -08002899 }
2900
Adrienne Walker4ee88512018-05-17 11:37:14 -07002901 this->bindFramebuffer(fboTarget, *tempFBOID);
Brian Salomond2a8ae22019-09-10 16:03:59 -04002902 GR_GL_CALL(
2903 this->glInterface(),
2904 FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel));
2905 if (mipLevel == 0) {
2906 texture->baseLevelWasBoundToFBO();
2907 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002908 } else {
Adrienne Walker4ee88512018-05-17 11:37:14 -07002909 this->bindFramebuffer(fboTarget, rt->renderFBOID());
bsalomon@google.comeb851172013-04-15 13:51:00 +00002910 }
egdaniel0f5f9672015-02-03 11:10:51 -08002911}
2912
Brian Salomond2a8ae22019-09-10 16:03:59 -04002913void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) {
Brian Salomon71d9d842016-11-03 13:42:00 -04002914 // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to
Brian Salomond2a8ae22019-09-10 16:03:59 -04002915 if (mipLevel > 0 || !surface->asRenderTarget()) {
bsalomon10528f12015-10-14 12:54:52 -07002916 SkASSERT(surface->asTexture());
2917 GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target();
2918 GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget,
2919 GR_GL_COLOR_ATTACHMENT0,
2920 textureTarget,
2921 0,
2922 0));
2923 }
bsalomon@google.comeb851172013-04-15 13:51:00 +00002924}
2925
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002926void GrGLGpu::onFBOChanged() {
2927 if (this->caps()->workarounds().flush_on_framebuffer_change ||
2928 this->caps()->workarounds().restore_scissor_on_fbo_change) {
Greg Daniel78be37f2020-04-14 16:40:35 -04002929 this->flush(FlushType::kForce);
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002930 }
Chris Dalton674f77a2019-09-30 20:49:39 -06002931#ifdef SK_DEBUG
2932 if (fIsExecutingCommandBuffer_DebugOnly) {
2933 SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. "
2934 "This will severely hurt performance.\n");
2935 }
2936#endif
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002937}
2938
Adrienne Walker4ee88512018-05-17 11:37:14 -07002939void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) {
2940 fStats.incRenderTargetBinds();
2941 GL_CALL(BindFramebuffer(target, fboid));
2942 if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) {
2943 fBoundDrawFramebuffer = fboid;
2944 }
2945
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002946 if (this->caps()->workarounds().restore_scissor_on_fbo_change) {
2947 // The driver forgets the correct scissor when modifying the FBO binding.
2948 if (!fHWScissorSettings.fRect.isInvalid()) {
Chris Daltond6cda8d2019-09-05 02:30:04 -06002949 const GrNativeRect& r = fHWScissorSettings.fRect;
Chris Dalton76500e52019-09-05 02:13:05 -06002950 GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002951 }
Adrienne Walker3ed33992018-05-15 11:44:34 -07002952 }
2953
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002954 this->onFBOChanged();
Adrienne Walker3ed33992018-05-15 11:44:34 -07002955}
2956
Adrienne Walker4ee88512018-05-17 11:37:14 -07002957void GrGLGpu::deleteFramebuffer(GrGLuint fboid) {
Brian Salomon2536b7f2020-02-27 17:09:29 -05002958 // We're relying on the GL state shadowing being correct in the workaround code below so we
2959 // need to handle a dirty context.
2960 this->handleDirtyContext();
Adrienne Walker4ee88512018-05-17 11:37:14 -07002961 if (fboid == fBoundDrawFramebuffer &&
2962 this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) {
2963 // This workaround only applies to deleting currently bound framebuffers
2964 // on Adreno 420. Because this is a somewhat rare case, instead of
2965 // tracking all the attachments of every framebuffer instead just always
2966 // unbind all attachments.
2967 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
2968 GR_GL_RENDERBUFFER, 0));
2969 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT,
2970 GR_GL_RENDERBUFFER, 0));
2971 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
2972 GR_GL_RENDERBUFFER, 0));
2973 }
2974
2975 GL_CALL(DeleteFramebuffers(1, &fboid));
Adrienne Walkerca0cdef2018-08-20 13:49:40 -07002976
2977 // Deleting the currently bound framebuffer rebinds to 0.
2978 if (fboid == fBoundDrawFramebuffer) {
2979 this->onFBOChanged();
2980 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07002981}
2982
Greg Daniel46cfbc62019-06-07 11:43:30 -04002983bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002984 const SkIPoint& dstPoint) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002985 // Don't prefer copying as a draw if the dst doesn't already have a FBO object.
2986 // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites().
2987 bool preferCopy = SkToBool(dst->asRenderTarget());
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002988 auto dstFormat = dst->backendFormat().asGLFormat();
2989 if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002990 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04002991 return true;
2992 }
2993 }
cblume61214052016-01-26 09:10:48 -08002994
Greg Daniel46cfbc62019-06-07 11:43:30 -04002995 if (can_copy_texsubimage(dst, src, this->glCaps())) {
2996 this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint);
bsalomon6df86402015-06-01 10:41:49 -07002997 return true;
2998 }
2999
Greg Daniel46cfbc62019-06-07 11:43:30 -04003000 if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) {
3001 return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003002 }
3003
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003004 if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003005 if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003006 return true;
3007 }
bsalomon083617b2016-02-12 12:10:14 -08003008 }
3009
bsalomon6df86402015-06-01 10:41:49 -07003010 return false;
3011}
3012
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003013bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
Brian Salomon5f394272019-07-02 14:07:49 -04003014 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003015
3016 int progIdx = TextureToCopyProgramIdx(srcTex);
3017 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003018 GrSLType samplerType = GrSLCombinedSamplerTypeForTextureType(srcTex->textureType());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003019
3020 if (!fCopyProgramArrayBuffer) {
3021 static const GrGLfloat vdata[] = {
3022 0, 0,
3023 0, 1,
3024 1, 0,
3025 1, 1
3026 };
3027 fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
3028 kStatic_GrAccessPattern, vdata);
3029 }
3030 if (!fCopyProgramArrayBuffer) {
3031 return false;
3032 }
3033
3034 SkASSERT(!fCopyPrograms[progIdx].fProgram);
3035 GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram());
3036 if (!fCopyPrograms[progIdx].fProgram) {
3037 return false;
3038 }
3039
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003040 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003041 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003042 GrShaderVar::TypeModifier::Uniform);
3043 GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::TypeModifier::Uniform);
3044 GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::TypeModifier::Uniform);
3045 GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out);
3046 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::TypeModifier::Out);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003047
Greg Daniel9e3169b2019-06-06 14:38:17 -04003048 SkString vshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003049 if (shaderCaps->noperspectiveInterpolationSupport()) {
3050 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3051 vshaderTxt.appendf("#extension %s : require\n", extension);
3052 }
3053 vTexCoord.addModifier("noperspective");
3054 }
3055
3056 aVertex.appendDecl(shaderCaps, &vshaderTxt);
3057 vshaderTxt.append(";");
3058 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
3059 vshaderTxt.append(";");
3060 uPosXform.appendDecl(shaderCaps, &vshaderTxt);
3061 vshaderTxt.append(";");
3062 vTexCoord.appendDecl(shaderCaps, &vshaderTxt);
3063 vshaderTxt.append(";");
3064
3065 vshaderTxt.append(
3066 "// Copy Program VS\n"
3067 "void main() {"
3068 " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);"
3069 " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;"
3070 " sk_Position.zw = half2(0, 1);"
3071 "}"
3072 );
3073
Greg Daniel9e3169b2019-06-06 14:38:17 -04003074 SkString fshaderTxt;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003075 if (shaderCaps->noperspectiveInterpolationSupport()) {
3076 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
3077 fshaderTxt.appendf("#extension %s : require\n", extension);
3078 }
3079 }
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003080 vTexCoord.setTypeModifier(GrShaderVar::TypeModifier::In);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003081 vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
3082 fshaderTxt.append(";");
3083 uTexture.appendDecl(shaderCaps, &fshaderTxt);
3084 fshaderTxt.append(";");
3085 fshaderTxt.appendf(
3086 "// Copy Program FS\n"
3087 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04003088 " sk_FragColor = sample(u_texture, v_texCoord);"
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003089 "}"
3090 );
3091
3092 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
3093 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
3094 SkSL::Program::Settings settings;
3095 settings.fCaps = shaderCaps;
3096 SkSL::String glsl;
3097 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
3098 sksl, settings, &glsl, errorHandler);
3099 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3100 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
3101 SkASSERT(program->fInputs.isEmpty());
3102
3103 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
3104 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3105 errorHandler);
3106 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
3107 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
3108 errorHandler);
3109 SkASSERT(program->fInputs.isEmpty());
3110
3111 GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
3112
3113 GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform,
3114 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture"));
3115 GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform,
3116 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform"));
3117 GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform,
3118 GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform"));
3119
3120 GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex"));
3121
3122 GL_CALL(DeleteShader(vshader));
3123 GL_CALL(DeleteShader(fshader));
3124
3125 return true;
3126}
3127
brianosman33f6b3f2016-06-02 05:49:21 -07003128bool GrGLGpu::createMipmapProgram(int progIdx) {
3129 const bool oddWidth = SkToBool(progIdx & 0x2);
3130 const bool oddHeight = SkToBool(progIdx & 0x1);
3131 const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1);
3132
Brian Salomon1edc5b92016-11-29 13:43:46 -05003133 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
brianosman33f6b3f2016-06-02 05:49:21 -07003134
3135 SkASSERT(!fMipmapPrograms[progIdx].fProgram);
3136 GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram());
3137 if (!fMipmapPrograms[progIdx].fProgram) {
3138 return false;
3139 }
3140
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003141 GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::TypeModifier::In);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003142 GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003143 GrShaderVar::TypeModifier::Uniform);
Brian Salomon99938a82016-11-21 13:41:08 -05003144 GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003145 GrShaderVar::TypeModifier::Uniform);
brianosman33f6b3f2016-06-02 05:49:21 -07003146 // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension):
Brian Salomon99938a82016-11-21 13:41:08 -05003147 GrShaderVar vTexCoords[] = {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003148 GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3149 GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3150 GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
3151 GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::TypeModifier::Out),
brianosman33f6b3f2016-06-02 05:49:21 -07003152 };
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003153 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::TypeModifier::Out);
brianosman33f6b3f2016-06-02 05:49:21 -07003154
Ethan Nicholasfc994162019-06-06 10:04:27 -04003155 SkString vshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003156 if (shaderCaps->noperspectiveInterpolationSupport()) {
3157 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003158 vshaderTxt.appendf("#extension %s : require\n", extension);
3159 }
3160 vTexCoords[0].addModifier("noperspective");
3161 vTexCoords[1].addModifier("noperspective");
3162 vTexCoords[2].addModifier("noperspective");
3163 vTexCoords[3].addModifier("noperspective");
3164 }
3165
Brian Salomon1edc5b92016-11-29 13:43:46 -05003166 aVertex.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003167 vshaderTxt.append(";");
Brian Salomon1edc5b92016-11-29 13:43:46 -05003168 uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003169 vshaderTxt.append(";");
3170 for (int i = 0; i < numTaps; ++i) {
Brian Salomon1edc5b92016-11-29 13:43:46 -05003171 vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003172 vshaderTxt.append(";");
3173 }
3174
3175 vshaderTxt.append(
3176 "// Mipmap Program VS\n"
3177 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04003178 " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);"
3179 " sk_Position.zw = half2(0, 1);"
brianosman33f6b3f2016-06-02 05:49:21 -07003180 );
3181
3182 // Insert texture coordinate computation:
3183 if (oddWidth && oddHeight) {
3184 vshaderTxt.append(
3185 " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003186 " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);"
3187 " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003188 " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;"
3189 );
3190 } else if (oddWidth) {
3191 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003192 " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);"
3193 " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003194 );
3195 } else if (oddHeight) {
3196 vshaderTxt.append(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04003197 " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);"
3198 " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);"
brianosman33f6b3f2016-06-02 05:49:21 -07003199 );
3200 } else {
3201 vshaderTxt.append(
3202 " v_texCoord0 = a_vertex.xy;"
3203 );
3204 }
3205
3206 vshaderTxt.append("}");
3207
Ethan Nicholasfc994162019-06-06 10:04:27 -04003208 SkString fshaderTxt;
Brian Salomon1edc5b92016-11-29 13:43:46 -05003209 if (shaderCaps->noperspectiveInterpolationSupport()) {
3210 if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003211 fshaderTxt.appendf("#extension %s : require\n", extension);
3212 }
3213 }
brianosman33f6b3f2016-06-02 05:49:21 -07003214 for (int i = 0; i < numTaps; ++i) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -04003215 vTexCoords[i].setTypeModifier(GrShaderVar::TypeModifier::In);
Brian Salomon1edc5b92016-11-29 13:43:46 -05003216 vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003217 fshaderTxt.append(";");
3218 }
Brian Salomon1edc5b92016-11-29 13:43:46 -05003219 uTexture.appendDecl(shaderCaps, &fshaderTxt);
brianosman33f6b3f2016-06-02 05:49:21 -07003220 fshaderTxt.append(";");
brianosman33f6b3f2016-06-02 05:49:21 -07003221 fshaderTxt.append(
3222 "// Mipmap Program FS\n"
3223 "void main() {"
3224 );
3225
3226 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) + "
3230 " sample(u_texture, v_texCoord2) + "
3231 " sample(u_texture, v_texCoord3)) * 0.25;"
brianosman33f6b3f2016-06-02 05:49:21 -07003232 );
3233 } else if (oddWidth || oddHeight) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003234 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003235 " sk_FragColor = (sample(u_texture, v_texCoord0) + "
3236 " sample(u_texture, v_texCoord1)) * 0.5;"
brianosman33f6b3f2016-06-02 05:49:21 -07003237 );
3238 } else {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05003239 fshaderTxt.append(
Ethan Nicholas13863662019-07-29 13:05:15 -04003240 " sk_FragColor = sample(u_texture, v_texCoord0);"
brianosman33f6b3f2016-06-02 05:49:21 -07003241 );
3242 }
3243
3244 fshaderTxt.append("}");
3245
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003246 auto errorHandler = this->getContext()->priv().getShaderErrorHandler();
Brian Osman6c431d52019-04-15 16:31:54 -04003247 SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003248 SkSL::Program::Settings settings;
3249 settings.fCaps = shaderCaps;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003250 SkSL::String glsl;
Brian Osmanac9be9d2019-05-01 10:29:34 -04003251 std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003252 sksl, settings, &glsl, errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003253 GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003254 GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003255 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003256
Brian Osman6c431d52019-04-15 16:31:54 -04003257 sksl.assign(fshaderTxt.c_str(), fshaderTxt.size());
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003258 program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl,
3259 errorHandler);
brianosman33f6b3f2016-06-02 05:49:21 -07003260 GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
Brian Osman8518f2e2019-05-01 14:13:41 -04003261 GR_GL_FRAGMENT_SHADER, glsl, &fStats,
Brian Osman5e7fbfd2019-05-03 13:13:35 -04003262 errorHandler);
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -04003263 SkASSERT(program->fInputs.isEmpty());
brianosman33f6b3f2016-06-02 05:49:21 -07003264
3265 GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
3266
3267 GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform,
3268 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture"));
3269 GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3270 GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform"));
3271
3272 GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex"));
3273
3274 GL_CALL(DeleteShader(vshader));
3275 GL_CALL(DeleteShader(fshader));
3276
3277 return true;
3278}
3279
Greg Daniel46cfbc62019-06-07 11:43:30 -04003280bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003281 const SkIPoint& dstPoint) {
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003282 auto* srcTex = static_cast<GrGLTexture*>(src->asTexture());
3283 auto* dstTex = static_cast<GrGLTexture*>(src->asTexture());
3284 auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget());
3285 if (!srcTex) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003286 return false;
3287 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003288 int progIdx = TextureToCopyProgramIdx(srcTex);
3289 if (!dstRT) {
3290 SkASSERT(dstTex);
3291 if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) {
3292 return false;
3293 }
3294 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003295 if (!fCopyPrograms[progIdx].fProgram) {
3296 if (!this->createCopyProgram(srcTex)) {
3297 SkDebugf("Failed to create copy program.\n");
3298 return false;
3299 }
3300 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003301 int w = srcRect.width();
3302 int h = srcRect.height();
Greg Daniel2c3398d2019-06-19 11:58:01 -04003303 // We don't swizzle at all in our copies.
Brian Salomonccb61422020-01-09 10:46:36 -05003304 this->bindTexture(0, GrSamplerState::Filter::kNearest, GrSwizzle::RGBA(), srcTex);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003305 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003306 this->flushViewport(dst->width(), dst->height());
3307 fHWBoundRenderTargetUniqueID.makeInvalid();
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003308 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003309 this->flushProgram(fCopyPrograms[progIdx].fProgram);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003310 fHWVertexArrayState.setVertexArrayID(this, 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003311 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
3312 attribs->enableVertexArrays(this, 1);
3313 attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
3314 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003315 // dst rect edges in NDC (-1 to 1)
3316 int dw = dst->width();
3317 int dh = dst->height();
3318 GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f;
3319 GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
3320 GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f;
3321 GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003322 GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft;
3323 GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w);
3324 GrGLfloat sy0 = (GrGLfloat)srcRect.fTop;
3325 GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h);
3326 int sw = src->width();
3327 int sh = src->height();
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003328 if (srcTex->textureType() != GrTextureType::kRectangle) {
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003329 // src rect edges in normalized texture space (0 to 1)
3330 sx0 /= sw;
3331 sx1 /= sw;
3332 sy0 /= sh;
3333 sy1 /= sh;
3334 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003335 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0));
3336 GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform,
3337 sx1 - sx0, sy1 - sy0, sx0, sy0));
3338 GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0));
Chris Daltonbbb3f642019-07-24 12:25:08 -04003339 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003340 this->flushHWAAState(nullptr, false);
Chris Daltonce425af2019-12-16 10:39:03 -07003341 this->flushConservativeRasterState(false);
Chris Dalton1215cda2019-12-17 21:44:04 -07003342 this->flushWireframeState(false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003343 this->flushScissorTest(GrScissorTest::kDisabled);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003344 this->disableWindowRectangles();
3345 this->disableStencil();
3346 if (this->glCaps().srgbWriteControl()) {
3347 this->flushFramebufferSRGB(true);
3348 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003349 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003350 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003351 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3352 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel4c6f9b72019-06-06 13:40:59 -04003353 return true;
3354}
3355
Greg Daniel46cfbc62019-06-07 11:43:30 -04003356void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003357 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003358 SkASSERT(can_copy_texsubimage(dst, src, this->glCaps()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003359 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon083617b2016-02-12 12:10:14 -08003360 GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture());
bsalomon6df86402015-06-01 10:41:49 -07003361 SkASSERT(dstTex);
3362 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003363 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003364
Brian Salomond978b902019-02-07 15:09:18 -05003365 this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID());
bsalomon10528f12015-10-14 12:54:52 -07003366 GL_CALL(CopyTexSubImage2D(dstTex->target(), 0,
Greg Daniel46cfbc62019-06-07 11:43:30 -04003367 dstPoint.fX, dstPoint.fY,
3368 srcRect.fLeft, srcRect.fTop,
3369 srcRect.width(), srcRect.height()));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003370 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER);
bsalomon083617b2016-02-12 12:10:14 -08003371 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3372 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04003373 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3374 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003375}
3376
Greg Daniel46cfbc62019-06-07 11:43:30 -04003377bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
bsalomon6df86402015-06-01 10:41:49 -07003378 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04003379 SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps()));
bsalomon6df86402015-06-01 10:41:49 -07003380 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
3381 srcRect.width(), srcRect.height());
3382 if (dst == src) {
Mike Reed3012cba2019-08-26 10:31:13 -04003383 if (SkIRect::Intersects(dstRect, srcRect)) {
bsalomon6df86402015-06-01 10:41:49 -07003384 return false;
mtklein404b3b22015-05-18 09:29:10 -07003385 }
bsalomon5df6fee2015-05-18 06:26:15 -07003386 }
bsalomon6df86402015-06-01 10:41:49 -07003387
Brian Salomond2a8ae22019-09-10 16:03:59 -04003388 this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget);
3389 this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget);
bsalomon6df86402015-06-01 10:41:49 -07003390 // We modified the bound FBO
Robert Phillips294870f2016-11-11 12:38:40 -05003391 fHWBoundRenderTargetUniqueID.makeInvalid();
bsalomon6df86402015-06-01 10:41:49 -07003392
3393 // BlitFrameBuffer respects the scissor, so disable it.
Chris Dalton2e7ed262020-02-21 15:17:59 -07003394 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003395 this->disableWindowRectangles();
bsalomon6df86402015-06-01 10:41:49 -07003396
Greg Daniel46cfbc62019-06-07 11:43:30 -04003397 GL_CALL(BlitFramebuffer(srcRect.fLeft,
3398 srcRect.fTop,
3399 srcRect.fRight,
3400 srcRect.fBottom,
3401 dstRect.fLeft,
3402 dstRect.fTop,
3403 dstRect.fRight,
3404 dstRect.fBottom,
bsalomon6df86402015-06-01 10:41:49 -07003405 GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST));
Brian Salomond2a8ae22019-09-10 16:03:59 -04003406 this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER);
3407 this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER);
Greg Daniel46cfbc62019-06-07 11:43:30 -04003408
3409 // The rect is already in device space so we pass in kTopLeft so no flip is done.
3410 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
bsalomon6df86402015-06-01 10:41:49 -07003411 return true;
commit-bot@chromium.org63150af2013-04-11 22:00:22 +00003412}
3413
Brian Salomon930f9392018-06-20 16:25:26 -04003414bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) {
3415 auto glTex = static_cast<GrGLTexture*>(texture);
brianosman33f6b3f2016-06-02 05:49:21 -07003416 // Mipmaps are only supported on 2D textures:
Brian Salomon930f9392018-06-20 16:25:26 -04003417 if (GR_GL_TEXTURE_2D != glTex->target()) {
brianosman33f6b3f2016-06-02 05:49:21 -07003418 return false;
3419 }
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003420 GrGLFormat format = glTex->format();
Brian Salomon930f9392018-06-20 16:25:26 -04003421 // Manual implementation of mipmap generation, to work around driver bugs w/sRGB.
3422 // Uses draw calls to do a series of downsample operations to successive mips.
3423
3424 // The manual approach requires the ability to limit which level we're sampling and that the
3425 // destination can be bound to a FBO:
Brian Salomon1c53a9f2019-08-12 14:10:12 -04003426 if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomon930f9392018-06-20 16:25:26 -04003427 GrGLenum target = glTex->target();
Brian Salomond978b902019-02-07 15:09:18 -05003428 this->bindTextureToScratchUnit(target, glTex->textureID());
Brian Salomon930f9392018-06-20 16:25:26 -04003429 GL_CALL(GenerateMipmap(glTex->target()));
3430 return true;
brianosman33f6b3f2016-06-02 05:49:21 -07003431 }
3432
brianosman33f6b3f2016-06-02 05:49:21 -07003433 int width = texture->width();
3434 int height = texture->height();
Mike Reed13711eb2020-07-14 17:16:32 -04003435 int levelCount = SkMipmap::ComputeLevelCount(width, height) + 1;
Brian Salomon4cfae3b2020-07-23 10:33:24 -04003436 SkASSERT(levelCount == texture->maxMipmapLevel() + 1);
brianosman33f6b3f2016-06-02 05:49:21 -07003437
3438 // Create (if necessary), then bind temporary FBO:
3439 if (0 == fTempDstFBOID) {
3440 GL_CALL(GenFramebuffers(1, &fTempDstFBOID));
3441 }
Adrienne Walker4ee88512018-05-17 11:37:14 -07003442 this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID);
Robert Phillips294870f2016-11-11 12:38:40 -05003443 fHWBoundRenderTargetUniqueID.makeInvalid();
brianosman33f6b3f2016-06-02 05:49:21 -07003444
3445 // Bind the texture, to get things configured for filtering.
3446 // We'll be changing our base level further below:
3447 this->setTextureUnit(0);
Greg Daniel2c3398d2019-06-19 11:58:01 -04003448 // The mipmap program does not do any swizzling.
Brian Salomona3b02f52020-07-15 16:02:01 -04003449 this->bindTexture(0, GrSamplerState::Filter::kLinear, GrSwizzle::RGBA(), glTex);
brianosman33f6b3f2016-06-02 05:49:21 -07003450
3451 // Vertex data:
3452 if (!fMipmapProgramArrayBuffer) {
3453 static const GrGLfloat vdata[] = {
3454 0, 0,
3455 0, 1,
3456 1, 0,
3457 1, 1
3458 };
Brian Salomonae64c192019-02-05 09:41:37 -05003459 fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex,
Brian Salomon12d22642019-01-29 14:38:50 -05003460 kStatic_GrAccessPattern, vdata);
brianosman33f6b3f2016-06-02 05:49:21 -07003461 }
3462 if (!fMipmapProgramArrayBuffer) {
3463 return false;
3464 }
3465
3466 fHWVertexArrayState.setVertexArrayID(this, 0);
3467
3468 GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this);
Chris Dalton8e45b4f2017-05-05 14:00:56 -04003469 attribs->enableVertexArrays(this, 1);
Brian Osmand4c29702018-09-14 16:16:55 -04003470 attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType,
Brian Osman4a3f5c82018-09-18 16:16:38 -04003471 kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0);
brianosman33f6b3f2016-06-02 05:49:21 -07003472
3473 // Set "simple" state once:
Chris Daltonbbb3f642019-07-24 12:25:08 -04003474 this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA());
Chris Dalton4c56b032019-03-04 12:28:42 -07003475 this->flushHWAAState(nullptr, false);
Chris Dalton2e7ed262020-02-21 15:17:59 -07003476 this->flushScissorTest(GrScissorTest::kDisabled);
csmartdalton28341fa2016-08-17 10:00:21 -07003477 this->disableWindowRectangles();
csmartdaltonc7d85332016-10-26 10:13:46 -07003478 this->disableStencil();
brianosman33f6b3f2016-06-02 05:49:21 -07003479
3480 // Do all the blits:
3481 width = texture->width();
3482 height = texture->height();
Brian Salomondc829942018-10-23 16:07:24 -04003483
brianosman33f6b3f2016-06-02 05:49:21 -07003484 for (GrGLint level = 1; level < levelCount; ++level) {
3485 // Get and bind the program for this particular downsample (filter shape can vary):
3486 int progIdx = TextureSizeToMipmapProgramIdx(width, height);
3487 if (!fMipmapPrograms[progIdx].fProgram) {
3488 if (!this->createMipmapProgram(progIdx)) {
3489 SkDebugf("Failed to create mipmap program.\n");
Brian Salomondc829942018-10-23 16:07:24 -04003490 // Invalidate all params to cover base level change in a previous iteration.
3491 glTex->textureParamsModified();
brianosman33f6b3f2016-06-02 05:49:21 -07003492 return false;
3493 }
3494 }
Brian Salomon802cb312018-06-08 18:05:20 -04003495 this->flushProgram(fMipmapPrograms[progIdx].fProgram);
brianosman33f6b3f2016-06-02 05:49:21 -07003496
3497 // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h)
3498 const float invWidth = 1.0f / width;
3499 const float invHeight = 1.0f / height;
3500 GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform,
3501 invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight));
3502 GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0));
3503
3504 // Only sample from previous mip
3505 GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1));
3506
Brian Salomon930f9392018-06-20 16:25:26 -04003507 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3508 glTex->textureID(), level));
brianosman33f6b3f2016-06-02 05:49:21 -07003509
Brian Osman788b9162020-02-07 10:36:46 -05003510 width = std::max(1, width / 2);
3511 height = std::max(1, height / 2);
Greg Danielacd66b42019-05-22 16:29:12 -04003512 this->flushViewport(width, height);
brianosman33f6b3f2016-06-02 05:49:21 -07003513
3514 GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4));
3515 }
3516
3517 // Unbind:
3518 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3519 GR_GL_TEXTURE_2D, 0, 0));
3520
Brian Salomon930f9392018-06-20 16:25:26 -04003521 // We modified the base level param.
Brian Salomone2826ab2019-06-04 15:58:31 -04003522 GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState();
3523 // We drew the 2nd to last level into the last level.
3524 nonsamplerState.fBaseMipMapLevel = levelCount - 2;
3525 glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
Brian Salomondc829942018-10-23 16:07:24 -04003526
brianosman33f6b3f2016-06-02 05:49:21 -07003527 return true;
3528}
3529
Chris Daltond7291ba2019-03-07 14:17:03 -07003530void GrGLGpu::querySampleLocations(
Chris Dalton8c4cafd2019-04-15 19:14:36 -06003531 GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) {
3532 this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget));
Chris Daltond7291ba2019-03-07 14:17:03 -07003533
3534 int effectiveSampleCnt;
3535 GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt);
Chris Dalton6ce447a2019-06-23 18:07:38 -06003536 SkASSERT(effectiveSampleCnt >= renderTarget->numSamples());
Chris Daltond7291ba2019-03-07 14:17:03 -07003537
3538 sampleLocations->reset(effectiveSampleCnt);
3539 for (int i = 0; i < effectiveSampleCnt; ++i) {
3540 GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX));
3541 }
3542}
3543
cdalton231c5fd2015-05-13 12:35:36 -07003544void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) {
bsalomoncb02b382015-08-12 11:14:50 -07003545 SkASSERT(type);
cdalton9954bc32015-04-29 14:17:00 -07003546 switch (type) {
cdalton231c5fd2015-05-13 12:35:36 -07003547 case kTexture_GrXferBarrierType: {
3548 GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt);
Brian Osmancfe83d12018-01-19 11:13:45 -05003549 SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0);
cdalton231c5fd2015-05-13 12:35:36 -07003550 if (glrt->textureFBOID() != glrt->renderFBOID()) {
3551 // The render target uses separate storage so no need for glTextureBarrier.
3552 // FIXME: The render target will resolve automatically when its texture is bound,
3553 // but we could resolve only the bounds that will be read if we do it here instead.
3554 return;
3555 }
cdalton9954bc32015-04-29 14:17:00 -07003556 SkASSERT(this->caps()->textureBarrierSupport());
3557 GL_CALL(TextureBarrier());
3558 return;
cdalton231c5fd2015-05-13 12:35:36 -07003559 }
cdalton8917d622015-05-06 13:40:21 -07003560 case kBlend_GrXferBarrierType:
bsalomon4b91f762015-05-19 09:29:46 -07003561 SkASSERT(GrCaps::kAdvanced_BlendEquationSupport ==
cdalton8917d622015-05-06 13:40:21 -07003562 this->caps()->blendEquationSupport());
3563 GL_CALL(BlendBarrier());
3564 return;
bsalomoncb02b382015-08-12 11:14:50 -07003565 default: break; // placate compiler warnings that kNone not handled
cdalton9954bc32015-04-29 14:17:00 -07003566 }
3567}
3568
Chris Daltone1196c52019-12-28 14:31:09 -07003569void GrGLGpu::insertManualFramebufferBarrier() {
3570 SkASSERT(this->caps()->requiresManualFBBarrierAfterTessellatedStencilDraw());
3571 GL_CALL(MemoryBarrier(GR_GL_FRAMEBUFFER_BARRIER_BIT));
3572}
3573
Brian Salomon85c3d682019-11-04 15:04:54 -05003574GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions,
Robert Phillips57ef6802019-09-23 10:12:47 -04003575 const GrBackendFormat& format,
Robert Phillips57ef6802019-09-23 10:12:47 -04003576 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -04003577 GrMipmapped mipMapped,
Greg Daniel16032b32020-05-06 15:31:10 -04003578 GrProtected isProtected) {
Robert Phillips42716d42019-12-16 12:19:54 -05003579 // We don't support protected textures in GL.
3580 if (isProtected == GrProtected::kYes) {
3581 return {};
3582 }
3583
Brian Salomon8a375832018-03-14 10:21:40 -04003584 this->handleDirtyContext();
Robert Phillips646f6372018-09-25 09:31:10 -04003585
Brian Salomond4764a12019-08-08 12:08:24 -04003586 GrGLFormat glFormat = format.asGLFormat();
Brian Salomon5043f1f2019-07-11 21:27:54 -04003587 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003588 return {};
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04003589 }
3590
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003591 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -04003592 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -04003593 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips0d7e2f12019-12-18 13:01:04 -05003594 }
3595
Robert Phillipsd34691b2019-09-24 13:38:43 -04003596 // Compressed formats go through onCreateCompressedBackendTexture
3597 SkASSERT(!GrGLFormatIsCompressed(glFormat));
3598
Greg Daniel15500642019-06-27 03:05:55 +00003599 GrGLTextureInfo info;
3600 GrGLTextureParameters::SamplerOverriddenState initialState;
3601
Greg Danield51fa2f2020-01-22 16:53:38 -05003602 if (glFormat == GrGLFormat::kUnknown) {
Robert Phillips42716d42019-12-16 12:19:54 -05003603 return {};
Brian Salomon85c3d682019-11-04 15:04:54 -05003604 }
Brian Salomon0f396992020-06-19 19:51:21 -04003605 switch (format.textureType()) {
3606 case GrTextureType::kNone:
3607 case GrTextureType::kExternal:
3608 return {};
3609 case GrTextureType::k2D:
3610 info.fTarget = GR_GL_TEXTURE_2D;
3611 break;
3612 case GrTextureType::kRectangle:
Brian Salomon7e67dca2020-07-21 09:27:25 -04003613 if (!this->glCaps().rectangleTextureSupport() || mipMapped == GrMipmapped::kYes) {
Brian Salomon0f396992020-06-19 19:51:21 -04003614 return {};
3615 }
3616 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
3617 break;
3618 }
Robert Phillipsd34691b2019-09-24 13:38:43 -04003619 info.fFormat = GrGLFormatToEnum(glFormat);
Brian Salomon0f396992020-06-19 19:51:21 -04003620 info.fID = this->createTexture(dimensions, glFormat, info.fTarget, renderable, &initialState,
3621 numMipLevels);
Robert Phillipsd34691b2019-09-24 13:38:43 -04003622 if (!info.fID) {
Brian Salomon85c3d682019-11-04 15:04:54 -05003623 return {};
Robert Phillipse3bd6732019-05-29 14:20:35 -04003624 }
Robert Phillipsb04b6942019-05-21 17:24:31 -04003625
Brian Salomon85c3d682019-11-04 15:04:54 -05003626 // Unbind this texture from the scratch texture unit.
Brian Salomon0f396992020-06-19 19:51:21 -04003627 this->bindTextureToScratchUnit(info.fTarget, 0);
Robert Phillipse8fabb22018-02-04 14:33:21 -05003628
Brian Salomone2826ab2019-06-04 15:58:31 -04003629 auto parameters = sk_make_sp<GrGLTextureParameters>();
Robert Phillips42716d42019-12-16 12:19:54 -05003630 // The non-sampler params are still at their default values.
Brian Salomone2826ab2019-06-04 15:58:31 -04003631 parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(),
3632 fResetTimestampForTextureParameters);
Robert Phillips62221e72019-07-24 15:07:38 -04003633
Brian Salomon85c3d682019-11-04 15:04:54 -05003634 return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info,
3635 std::move(parameters));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003636}
3637
Greg Daniel16032b32020-05-06 15:31:10 -04003638bool GrGLGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
3639 sk_sp<GrRefCntedCallback> finishedCallback,
3640 const BackendTextureData* data) {
3641 GrGLTextureInfo info;
3642 SkAssertResult(backendTexture.getGLTextureInfo(&info));
3643
3644 int numMipLevels = 1;
Brian Salomon40a40622020-07-21 10:32:07 -04003645 if (backendTexture.hasMipmaps()) {
Greg Daniel16032b32020-05-06 15:31:10 -04003646 numMipLevels =
Mike Reed13711eb2020-07-14 17:16:32 -04003647 SkMipmap::ComputeLevelCount(backendTexture.width(), backendTexture.height()) + 1;
Greg Daniel16032b32020-05-06 15:31:10 -04003648 }
3649
3650 GrGLFormat glFormat = GrGLFormatFromGLEnum(info.fFormat);
3651
Brian Salomon22558932020-05-15 14:46:34 -04003652 this->bindTextureToScratchUnit(info.fTarget, info.fID);
Greg Daniel16032b32020-05-06 15:31:10 -04003653
Brian Salomonc3f49432020-06-03 10:21:50 -04003654 // 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 -04003655 // so that the uploads go to the right levels.
Brian Salomon69100f02020-07-21 10:49:25 -04003656 if (numMipLevels && this->glCaps().mipmapLevelAndLodControlSupport()) {
Greg Danielb2365d82020-05-13 15:32:04 -04003657 auto params = backendTexture.getGLTextureParams();
3658 GrGLTextureParameters::NonsamplerState nonsamplerState = params->nonsamplerState();
3659 if (params->nonsamplerState().fBaseMipMapLevel != 0) {
Brian Salomon22558932020-05-15 14:46:34 -04003660 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_BASE_LEVEL, 0));
Greg Danielb2365d82020-05-13 15:32:04 -04003661 nonsamplerState.fBaseMipMapLevel = 0;
3662 }
Brian Salomon8c82a872020-07-21 12:09:58 -04003663 if (params->nonsamplerState().fMaxMipmapLevel != (numMipLevels - 1)) {
Brian Salomon22558932020-05-15 14:46:34 -04003664 GL_CALL(TexParameteri(info.fTarget, GR_GL_TEXTURE_MAX_LEVEL, numMipLevels - 1));
Greg Danielb2365d82020-05-13 15:32:04 -04003665 nonsamplerState.fBaseMipMapLevel = numMipLevels - 1;
3666 }
3667 params->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters);
3668 }
3669
Greg Daniel16032b32020-05-06 15:31:10 -04003670 SkASSERT(data->type() != BackendTextureData::Type::kCompressed);
Brian Salomon22558932020-05-15 14:46:34 -04003671 bool result = false;
Greg Daniel16032b32020-05-06 15:31:10 -04003672 if (data->type() == BackendTextureData::Type::kPixmaps) {
3673 SkTDArray<GrMipLevel> texels;
3674 GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType());
3675 texels.append(numMipLevels);
3676 for (int i = 0; i < numMipLevels; ++i) {
3677 texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()};
3678 }
Brian Salomon22558932020-05-15 14:46:34 -04003679 SkIRect dstRect = SkIRect::MakeSize(backendTexture.dimensions());
3680 result = this->uploadColorTypeTexData(glFormat, colorType, backendTexture.dimensions(),
3681 info.fTarget, dstRect, colorType, texels.begin(),
3682 texels.count());
3683 } else if (data->type() == BackendTextureData::Type::kColor) {
3684 uint32_t levelMask = (1 << numMipLevels) - 1;
3685 result = this->uploadColorToTex(glFormat, backendTexture.dimensions(), info.fTarget,
3686 data->color(), levelMask);
Greg Daniel16032b32020-05-06 15:31:10 -04003687 }
3688
3689 // Unbind this texture from the scratch texture unit.
Brian Salomon22558932020-05-15 14:46:34 -04003690 this->bindTextureToScratchUnit(info.fTarget, 0);
3691 return result;
Greg Daniel16032b32020-05-06 15:31:10 -04003692}
3693
Robert Phillipsf0313ee2019-05-21 13:51:11 -04003694void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04003695 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
3696
3697 GrGLTextureInfo info;
3698 if (tex.getGLTextureInfo(&info)) {
3699 GL_CALL(DeleteTextures(1, &info.fID));
3700 }
3701}
3702
Robert Phillips3bce1f72020-05-12 12:41:58 -04003703bool GrGLGpu::compile(const GrProgramDesc& desc, const GrProgramInfo& programInfo) {
3704 SkASSERT(!(GrProcessor::CustomFeatures::kSampleLocations & programInfo.requestedFeatures()));
3705
3706 Stats::ProgramCacheResult stat;
3707
3708 sk_sp<GrGLProgram> tmp = fProgramCache->findOrCreateProgram(desc, programInfo, &stat);
3709 if (!tmp) {
3710 return false;
3711 }
3712
3713 return stat != Stats::ProgramCacheResult::kHit;
3714}
3715
Robert Phillipsf0ced622019-05-16 09:06:25 -04003716#if GR_TEST_UTILS
3717
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003718bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003719 SkASSERT(GrBackendApi::kOpenGL == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003720
Greg Daniel52e16d92018-04-10 09:34:07 -04003721 GrGLTextureInfo info;
3722 if (!tex.getGLTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003723 return false;
3724 }
3725
3726 GrGLboolean result;
Greg Daniel52e16d92018-04-10 09:34:07 -04003727 GL_CALL_RET(result, IsTexture(info.fID));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05003728
3729 return (GR_GL_TRUE == result);
3730}
3731
Brian Salomonf865b052018-03-09 09:01:53 -05003732GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -04003733 GrColorType colorType) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04003734 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
3735 return GrBackendRenderTarget(); // invalid
3736 }
Brian Salomon8a375832018-03-14 10:21:40 -04003737 this->handleDirtyContext();
Greg Daniel0258c902019-08-01 13:08:33 -04003738 auto format = this->glCaps().getFormatFromColorType(colorType);
Greg Daniel900583a2019-08-06 12:05:31 -04003739 if (!this->glCaps().isFormatRenderable(format, 1)) {
Brian Salomonf865b052018-03-09 09:01:53 -05003740 return {};
3741 }
Brian Salomond2a8ae22019-09-10 16:03:59 -04003742 bool useTexture = format == GrGLFormat::kBGRA8;
Brian Salomonf6da1462019-07-11 14:21:59 -04003743 int sFormatIdx = this->getCompatibleStencilIndex(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003744 if (sFormatIdx < 0) {
3745 return {};
3746 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003747 GrGLuint colorID = 0;
3748 GrGLuint stencilID = 0;
3749 auto deleteIDs = [&] {
3750 if (colorID) {
3751 if (useTexture) {
3752 GL_CALL(DeleteTextures(1, &colorID));
3753 } else {
3754 GL_CALL(DeleteRenderbuffers(1, &colorID));
3755 }
Brian Salomonf865b052018-03-09 09:01:53 -05003756 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003757 if (stencilID) {
3758 GL_CALL(DeleteRenderbuffers(1, &stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003759 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003760 };
3761
3762 if (useTexture) {
3763 GL_CALL(GenTextures(1, &colorID));
3764 } else {
3765 GL_CALL(GenRenderbuffers(1, &colorID));
3766 }
3767 GL_CALL(GenRenderbuffers(1, &stencilID));
3768 if (!stencilID || !colorID) {
3769 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003770 return {};
3771 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003772
Brian Salomonf865b052018-03-09 09:01:53 -05003773 GrGLFramebufferInfo info;
3774 info.fFBOID = 0;
Brian Salomond2a8ae22019-09-10 16:03:59 -04003775 info.fFormat = GrGLFormatToEnum(format);
Brian Salomonf865b052018-03-09 09:01:53 -05003776 GL_CALL(GenFramebuffers(1, &info.fFBOID));
3777 if (!info.fFBOID) {
Brian Salomon93348dd2018-08-29 12:56:23 -04003778 deleteIDs();
3779 return {};
Brian Salomonf865b052018-03-09 09:01:53 -05003780 }
3781
3782 this->invalidateBoundRenderTarget();
3783
Adrienne Walker4ee88512018-05-17 11:37:14 -07003784 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomon93348dd2018-08-29 12:56:23 -04003785 if (useTexture) {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003786 GrGLTextureParameters::SamplerOverriddenState initialState;
Brian Salomon0f396992020-06-19 19:51:21 -04003787 colorID = this->createTexture({w, h}, format, GR_GL_TEXTURE_2D, GrRenderable::kYes,
3788 &initialState, 1);
Brian Salomond2a8ae22019-09-10 16:03:59 -04003789 if (!colorID) {
3790 deleteIDs();
3791 return {};
3792 }
Brian Salomon93348dd2018-08-29 12:56:23 -04003793 GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D,
3794 colorID, 0));
3795 } else {
Brian Salomond2a8ae22019-09-10 16:03:59 -04003796 GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format);
Brian Salomon93348dd2018-08-29 12:56:23 -04003797 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID));
Brian Salomond8575452020-02-25 12:13:29 -05003798 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h));
Brian Salomon93348dd2018-08-29 12:56:23 -04003799 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
3800 GR_GL_RENDERBUFFER, colorID));
3801 }
3802 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003803 auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat;
Brian Salomond8575452020-02-25 12:13:29 -05003804 GL_CALL(RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h));
Brian Salomonf865b052018-03-09 09:01:53 -05003805 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER,
Brian Salomon93348dd2018-08-29 12:56:23 -04003806 stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003807 if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) {
3808 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT,
Brian Salomon93348dd2018-08-29 12:56:23 -04003809 GR_GL_RENDERBUFFER, stencilID));
Brian Salomonf865b052018-03-09 09:01:53 -05003810 }
3811
Brian Salomon93348dd2018-08-29 12:56:23 -04003812 // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL
3813 // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO
3814 // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the
3815 // renderbuffers/texture.
Adrienne Walker4ee88512018-05-17 11:37:14 -07003816 this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0);
Brian Salomon93348dd2018-08-29 12:56:23 -04003817 deleteIDs();
Brian Salomonf865b052018-03-09 09:01:53 -05003818
Adrienne Walker4ee88512018-05-17 11:37:14 -07003819 this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003820 GrGLenum status;
3821 GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
3822 if (GR_GL_FRAMEBUFFER_COMPLETE != status) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003823 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003824 return {};
3825 }
3826 auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits);
Robert Phillips62221e72019-07-24 15:07:38 -04003827
Greg Daniel108bb232018-07-03 16:18:29 -04003828 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info);
Robert Phillips62221e72019-07-24 15:07:38 -04003829 SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat()));
Greg Daniel108bb232018-07-03 16:18:29 -04003830 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05003831}
3832
3833void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04003834 SkASSERT(GrBackendApi::kOpenGL == backendRT.backend());
Greg Daniel323fbcf2018-04-10 13:46:30 -04003835 GrGLFramebufferInfo info;
3836 if (backendRT.getGLFramebufferInfo(&info)) {
3837 if (info.fFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -07003838 this->deleteFramebuffer(info.fFBOID);
Brian Salomonf865b052018-03-09 09:01:53 -05003839 }
3840 }
joshualitt8fd844f2015-12-02 13:36:47 -08003841}
3842
Greg Daniel26b50a42018-03-08 09:49:58 -05003843void GrGLGpu::testingOnly_flushGpuAndSync() {
3844 GL_CALL(Finish());
3845}
Brian Salomonf865b052018-03-09 09:01:53 -05003846#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05003847
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003848///////////////////////////////////////////////////////////////////////////////
bsalomon6df86402015-06-01 10:41:49 -07003849
cdaltone2e71c22016-04-07 18:13:29 -07003850GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
csmartdalton485a1202016-07-13 10:16:32 -07003851 const GrBuffer* ibuf) {
Chris Daltond42d2002020-02-28 12:05:04 -07003852 SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
robertphillips@google.com4f65a272013-03-26 19:40:46 +00003853 GrGLAttribArrayState* attribState;
3854
cdaltone2e71c22016-04-07 18:13:29 -07003855 if (gpu->glCaps().isCoreProfile()) {
3856 if (!fCoreProfileVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +00003857 GrGLuint arrayID;
3858 GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID));
3859 int attrCount = gpu->glCaps().maxVertexAttributes();
cdaltone2e71c22016-04-07 18:13:29 -07003860 fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003861 }
cdaltone2e71c22016-04-07 18:13:29 -07003862 if (ibuf) {
3863 attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf);
bsalomon6df86402015-06-01 10:41:49 -07003864 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003865 attribState = fCoreProfileVertexArray->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -07003866 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003867 } else {
cdaltone2e71c22016-04-07 18:13:29 -07003868 if (ibuf) {
3869 // bindBuffer implicitly binds VAO 0 when binding an index buffer.
Brian Salomonae64c192019-02-05 09:41:37 -05003870 gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf);
bsalomon@google.com6918d482013-03-07 19:09:11 +00003871 } else {
3872 this->setVertexArrayID(gpu, 0);
3873 }
3874 int attrCount = gpu->glCaps().maxVertexAttributes();
3875 if (fDefaultVertexArrayAttribState.count() != attrCount) {
3876 fDefaultVertexArrayAttribState.resize(attrCount);
3877 }
3878 attribState = &fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +00003879 }
bsalomon@google.com6918d482013-03-07 19:09:11 +00003880 return attribState;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +00003881}
bsalomone179a912016-01-20 06:18:10 -08003882
Greg Danielfe159622020-04-10 17:43:51 +00003883void GrGLGpu::addFinishedProc(GrGpuFinishedProc finishedProc,
3884 GrGpuFinishedContext finishedContext) {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003885 fFinishCallbacks.add(finishedProc, finishedContext);
Greg Danielfe159622020-04-10 17:43:51 +00003886}
3887
Greg Daniel78be37f2020-04-14 16:40:35 -04003888void GrGLGpu::flush(FlushType flushType) {
3889 if (fNeedsGLFlush || flushType == FlushType::kForce) {
3890 GL_CALL(Flush());
3891 fNeedsGLFlush = false;
3892 }
3893}
3894
Greg Danielfe159622020-04-10 17:43:51 +00003895bool GrGLGpu::onSubmitToGpu(bool syncCpu) {
3896 if (syncCpu || (!fFinishCallbacks.empty() && !this->caps()->fenceSyncSupport())) {
Greg Danielbae71212019-03-01 15:24:35 -05003897 GL_CALL(Finish());
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003898 fFinishCallbacks.callAll(true);
Brian Salomonb0d8b762019-05-06 16:58:22 -04003899 } else {
Greg Daniel78be37f2020-04-14 16:40:35 -04003900 this->flush();
Brian Salomonb0d8b762019-05-06 16:58:22 -04003901 // See if any previously inserted finish procs are good to go.
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003902 fFinishCallbacks.check();
Greg Daniela3aa75a2019-04-12 14:24:55 -04003903 }
Brian Salomon24069eb2020-06-24 10:19:52 -04003904 if (!this->glCaps().skipErrorChecks()) {
3905 this->clearErrorsAndCheckForOOM();
3906 }
Greg Daniel30a35e82019-11-19 14:12:25 -05003907 return true;
Greg Daniel51316782017-08-02 15:10:09 +00003908}
3909
Greg Daniel2d41d0d2019-08-26 11:08:51 -04003910void GrGLGpu::submit(GrOpsRenderPass* renderPass) {
3911 // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here
3912 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
3913 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04003914}
3915
Greg Daniel6be35232017-03-01 17:01:09 -05003916GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003917 if (!this->caps()->fenceSyncSupport()) {
3918 return 0;
3919 }
Greg Daniel6be35232017-03-01 17:01:09 -05003920 GrGLsync sync;
Brian Salomon8675bcb2020-01-23 13:37:39 -05003921 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3922 static_assert(sizeof(GrGLsync) >= sizeof(GrGLuint));
3923 GrGLuint fence = 0;
3924 GL_CALL(GenFences(1, &fence));
3925 GL_CALL(SetFence(fence, GR_GL_ALL_COMPLETED));
3926 sync = reinterpret_cast<GrGLsync>(static_cast<intptr_t>(fence));
3927 } else {
3928 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3929 }
Greg Daniel78be37f2020-04-14 16:40:35 -04003930 this->setNeedsFlush();
Brian Salomon4dea72a2019-12-18 10:43:10 -05003931 static_assert(sizeof(GrFence) >= sizeof(GrGLsync));
Greg Daniel6be35232017-03-01 17:01:09 -05003932 return (GrFence)sync;
jvanverth84741b32016-09-30 08:39:02 -07003933}
3934
Brian Salomonb0d8b762019-05-06 16:58:22 -04003935bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) {
Brian Salomon8675bcb2020-01-23 13:37:39 -05003936 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
3937 GrGLuint nvFence = static_cast<GrGLuint>(reinterpret_cast<intptr_t>(sync));
3938 if (!timeout) {
3939 if (flush) {
Greg Daniel78be37f2020-04-14 16:40:35 -04003940 this->flush(FlushType::kForce);
Brian Salomon8675bcb2020-01-23 13:37:39 -05003941 }
3942 GrGLboolean result;
3943 GL_CALL_RET(result, TestFence(nvFence));
3944 return result == GR_GL_TRUE;
3945 }
3946 // Ignore non-zero timeouts. GL_NV_fence has no timeout functionality.
3947 // If this really becomes necessary we could poll TestFence().
3948 // FinishFence always flushes so no need to check flush param.
3949 GL_CALL(FinishFence(nvFence));
3950 return true;
3951 } else {
3952 GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0;
3953 GrGLenum result;
3954 GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout));
3955 return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result);
3956 }
Brian Salomonb0d8b762019-05-06 16:58:22 -04003957}
3958
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003959bool GrGLGpu::waitFence(GrFence fence) {
3960 if (!this->caps()->fenceSyncSupport()) {
3961 return true;
3962 }
3963 return this->waitSync(reinterpret_cast<GrGLsync>(fence), 0, false);
jvanverth84741b32016-09-30 08:39:02 -07003964}
3965
3966void GrGLGpu::deleteFence(GrFence fence) const {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04003967 if (this->caps()->fenceSyncSupport()) {
3968 this->deleteSync(reinterpret_cast<GrGLsync>(fence));
3969 }
Greg Daniel6be35232017-03-01 17:01:09 -05003970}
3971
Greg Daniel301015c2019-11-18 14:06:46 -05003972std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003973 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003974 return GrGLSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05003975}
3976
Greg Daniel301015c2019-11-18 14:06:46 -05003977std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore(
3978 const GrBackendSemaphore& semaphore,
3979 GrResourceProvider::SemaphoreWrapType wrapType,
3980 GrWrapOwnership ownership) {
Brian Salomon9ff5acb2019-05-08 09:04:47 -04003981 SkASSERT(this->caps()->semaphoreSupport());
Greg Daniela5cb7812017-06-16 09:45:32 -04003982 return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership);
3983}
3984
Greg Daniel301015c2019-11-18 14:06:46 -05003985void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003986 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003987 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003988
Greg Daniel48661b82018-01-22 16:11:35 -05003989 GrGLsync sync;
3990 GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
3991 glSem->setSync(sync);
Greg Daniel78be37f2020-04-14 16:40:35 -04003992 this->setNeedsFlush();
Greg Daniel6be35232017-03-01 17:01:09 -05003993}
3994
Greg Daniel301015c2019-11-18 14:06:46 -05003995void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) {
Greg Daniel0106fcc2020-07-01 17:40:12 -04003996 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05003997 GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore);
Greg Daniel6be35232017-03-01 17:01:09 -05003998
3999 GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED));
4000}
4001
Brian Salomonb0d8b762019-05-06 16:58:22 -04004002void GrGLGpu::checkFinishProcs() {
Stephen Whiteb353c9b2020-04-16 14:14:13 -04004003 fFinishCallbacks.check();
Brian Salomonb0d8b762019-05-06 16:58:22 -04004004}
4005
Brian Salomon24069eb2020-06-24 10:19:52 -04004006void GrGLGpu::clearErrorsAndCheckForOOM() {
4007 while (this->getErrorAndCheckForOOM() != GR_GL_NO_ERROR) {}
4008}
4009
4010GrGLenum GrGLGpu::getErrorAndCheckForOOM() {
4011#if GR_GL_CHECK_ERROR
4012 if (this->glInterface()->checkAndResetOOMed()) {
4013 this->setOOMed();
4014 }
4015#endif
4016 GrGLenum error = this->fGLContext->glInterface()->fFunctions.fGetError();
4017 if (error == GR_GL_OUT_OF_MEMORY) {
4018 this->setOOMed();
4019 }
4020 return error;
4021}
4022
Greg Daniel6be35232017-03-01 17:01:09 -05004023void GrGLGpu::deleteSync(GrGLsync sync) const {
Brian Salomon8675bcb2020-01-23 13:37:39 -05004024 if (this->glCaps().fenceType() == GrGLCaps::FenceType::kNVFence) {
4025 GrGLuint nvFence = SkToUInt(reinterpret_cast<intptr_t>(sync));
4026 GL_CALL(DeleteFences(1, &nvFence));
4027 } else {
4028 GL_CALL(DeleteSync(sync));
4029 }
jvanverth84741b32016-09-30 08:39:02 -07004030}
Brian Osman13dddce2017-05-09 13:19:50 -04004031
Greg Daniel301015c2019-11-18 14:06:46 -05004032std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
Brian Osman13dddce2017-05-09 13:19:50 -04004033 // Set up a semaphore to be signaled once the data is ready, and flush GL
Greg Daniel301015c2019-11-18 14:06:46 -05004034 std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true);
Greg Daniel30a35e82019-11-19 14:12:25 -05004035 SkASSERT(semaphore);
Greg Daniel301015c2019-11-18 14:06:46 -05004036 this->insertSemaphore(semaphore.get());
Greg Daniel858e12c2018-12-06 11:11:37 -05004037 // 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 -04004038 this->flush(FlushType::kForce);
Brian Osman13dddce2017-05-09 13:19:50 -04004039
4040 return semaphore;
4041}
Robert Phillips646e4292017-06-13 12:44:56 -04004042
4043int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -04004044 switch (GrSLCombinedSamplerTypeForTextureType(texture->textureType())) {
Robert Phillips646e4292017-06-13 12:44:56 -04004045 case kTexture2DSampler_GrSLType:
4046 return 0;
Robert Phillips646e4292017-06-13 12:44:56 -04004047 case kTexture2DRectSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004048 return 1;
Robert Phillips646e4292017-06-13 12:44:56 -04004049 case kTextureExternalSampler_GrSLType:
Brian Salomon57111332018-02-05 15:55:54 -05004050 return 2;
Robert Phillips646e4292017-06-13 12:44:56 -04004051 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04004052 SK_ABORT("Unexpected samper type");
Robert Phillips646e4292017-06-13 12:44:56 -04004053 }
4054}
Brian Osman71a18892017-08-10 10:23:25 -04004055
Kevin Lubickf4def342018-10-04 12:52:50 -04004056#ifdef SK_ENABLE_DUMP_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004057#include "src/utils/SkJSONWriter.h"
Brian Osman71a18892017-08-10 10:23:25 -04004058void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const {
4059 // We are called by the base class, which has already called beginObject(). We choose to nest
4060 // all of our caps information in a named sub-object.
4061 writer->beginObject("GL GPU");
4062
4063 const GrGLubyte* str;
4064 GL_CALL_RET(str, GetString(GR_GL_VERSION));
4065 writer->appendString("GL_VERSION", (const char*)(str));
4066 GL_CALL_RET(str, GetString(GR_GL_RENDERER));
4067 writer->appendString("GL_RENDERER", (const char*)(str));
4068 GL_CALL_RET(str, GetString(GR_GL_VENDOR));
4069 writer->appendString("GL_VENDOR", (const char*)(str));
4070 GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
4071 writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str));
4072
4073 writer->appendName("extensions");
4074 glInterface()->fExtensions.dumpJSON(writer);
4075
4076 writer->endObject();
4077}
Kevin Lubickf4def342018-10-04 12:52:50 -04004078#endif