reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkPixmap.h" |
| 9 | #include "include/core/SkStrokeRec.h" |
| 10 | #include "include/core/SkTypes.h" |
| 11 | #include "include/gpu/GrBackendSemaphore.h" |
| 12 | #include "include/gpu/GrBackendSurface.h" |
| 13 | #include "include/gpu/GrTypes.h" |
| 14 | #include "include/private/SkHalf.h" |
| 15 | #include "include/private/SkTemplates.h" |
| 16 | #include "include/private/SkTo.h" |
| 17 | #include "src/core/SkAutoMalloc.h" |
| 18 | #include "src/core/SkConvertPixels.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkMipMap.h" |
| 20 | #include "src/core/SkTraceEvent.h" |
Brian Osman | 8518f2e | 2019-05-01 14:13:41 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrCpuBuffer.h" |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrFixedClip.h" |
| 25 | #include "src/gpu/GrGpuResourcePriv.h" |
| 26 | #include "src/gpu/GrMesh.h" |
| 27 | #include "src/gpu/GrPipeline.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 28 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrRenderTargetPriv.h" |
| 30 | #include "src/gpu/GrShaderCaps.h" |
| 31 | #include "src/gpu/GrSurfaceProxyPriv.h" |
| 32 | #include "src/gpu/GrTexturePriv.h" |
| 33 | #include "src/gpu/gl/GrGLBuffer.h" |
| 34 | #include "src/gpu/gl/GrGLGpu.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 35 | #include "src/gpu/gl/GrGLOpsRenderPass.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 42 | #include <cmath> |
| 43 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 44 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 45 | #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->glInterface(), RET, X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 47 | #if GR_GL_CHECK_ALLOC_WITH_GET_ERROR |
| 48 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) GrGLClearErr(iface) |
| 49 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL_NOERRCHECK(iface, call) |
| 50 | #define CHECK_ALLOC_ERROR(iface) GR_GL_GET_ERROR(iface) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | #else |
bsalomon@google.com | 4f3c253 | 2012-01-19 16:16:52 +0000 | [diff] [blame] | 52 | #define CLEAR_ERROR_BEFORE_ALLOC(iface) |
| 53 | #define GL_ALLOC_CALL(iface, call) GR_GL_CALL(iface, call) |
| 54 | #define CHECK_ALLOC_ERROR(iface) GR_GL_NO_ERROR |
| 55 | #endif |
| 56 | |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 57 | //#define USE_NSIGHT |
| 58 | |
bsalomon@google.com | 4bcb0c6 | 2012-02-07 16:06:47 +0000 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 61 | static const GrGLenum gXfermodeEquation2Blend[] = { |
| 62 | // Basic OpenGL blend equations. |
| 63 | GR_GL_FUNC_ADD, |
| 64 | GR_GL_FUNC_SUBTRACT, |
| 65 | GR_GL_FUNC_REVERSE_SUBTRACT, |
| 66 | |
| 67 | // GL_KHR_blend_equation_advanced. |
| 68 | GR_GL_SCREEN, |
| 69 | GR_GL_OVERLAY, |
| 70 | GR_GL_DARKEN, |
| 71 | GR_GL_LIGHTEN, |
| 72 | GR_GL_COLORDODGE, |
| 73 | GR_GL_COLORBURN, |
| 74 | GR_GL_HARDLIGHT, |
| 75 | GR_GL_SOFTLIGHT, |
| 76 | GR_GL_DIFFERENCE, |
| 77 | GR_GL_EXCLUSION, |
| 78 | GR_GL_MULTIPLY, |
| 79 | GR_GL_HSL_HUE, |
| 80 | GR_GL_HSL_SATURATION, |
| 81 | GR_GL_HSL_COLOR, |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 82 | GR_GL_HSL_LUMINOSITY, |
| 83 | |
| 84 | // Illegal... needs to map to something. |
| 85 | GR_GL_FUNC_ADD, |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 86 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 87 | static_assert(0 == kAdd_GrBlendEquation); |
| 88 | static_assert(1 == kSubtract_GrBlendEquation); |
| 89 | static_assert(2 == kReverseSubtract_GrBlendEquation); |
| 90 | static_assert(3 == kScreen_GrBlendEquation); |
| 91 | static_assert(4 == kOverlay_GrBlendEquation); |
| 92 | static_assert(5 == kDarken_GrBlendEquation); |
| 93 | static_assert(6 == kLighten_GrBlendEquation); |
| 94 | static_assert(7 == kColorDodge_GrBlendEquation); |
| 95 | static_assert(8 == kColorBurn_GrBlendEquation); |
| 96 | static_assert(9 == kHardLight_GrBlendEquation); |
| 97 | static_assert(10 == kSoftLight_GrBlendEquation); |
| 98 | static_assert(11 == kDifference_GrBlendEquation); |
| 99 | static_assert(12 == kExclusion_GrBlendEquation); |
| 100 | static_assert(13 == kMultiply_GrBlendEquation); |
| 101 | static_assert(14 == kHSLHue_GrBlendEquation); |
| 102 | static_assert(15 == kHSLSaturation_GrBlendEquation); |
| 103 | static_assert(16 == kHSLColor_GrBlendEquation); |
| 104 | static_assert(17 == kHSLLuminosity_GrBlendEquation); |
| 105 | static_assert(SK_ARRAY_COUNT(gXfermodeEquation2Blend) == kGrBlendEquationCnt); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 106 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 107 | static const GrGLenum gXfermodeCoeff2Blend[] = { |
| 108 | GR_GL_ZERO, |
| 109 | GR_GL_ONE, |
| 110 | GR_GL_SRC_COLOR, |
| 111 | GR_GL_ONE_MINUS_SRC_COLOR, |
| 112 | GR_GL_DST_COLOR, |
| 113 | GR_GL_ONE_MINUS_DST_COLOR, |
| 114 | GR_GL_SRC_ALPHA, |
| 115 | GR_GL_ONE_MINUS_SRC_ALPHA, |
| 116 | GR_GL_DST_ALPHA, |
| 117 | GR_GL_ONE_MINUS_DST_ALPHA, |
| 118 | GR_GL_CONSTANT_COLOR, |
| 119 | GR_GL_ONE_MINUS_CONSTANT_COLOR, |
| 120 | GR_GL_CONSTANT_ALPHA, |
| 121 | GR_GL_ONE_MINUS_CONSTANT_ALPHA, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 122 | |
| 123 | // extended blend coeffs |
| 124 | GR_GL_SRC1_COLOR, |
| 125 | GR_GL_ONE_MINUS_SRC1_COLOR, |
| 126 | GR_GL_SRC1_ALPHA, |
| 127 | GR_GL_ONE_MINUS_SRC1_ALPHA, |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 128 | |
| 129 | // Illegal... needs to map to something. |
| 130 | GR_GL_ZERO, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 133 | bool GrGLGpu::BlendCoeffReferencesConstant(GrBlendCoeff coeff) { |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 134 | static const bool gCoeffReferencesBlendConst[] = { |
| 135 | false, |
| 136 | false, |
| 137 | false, |
| 138 | false, |
| 139 | false, |
| 140 | false, |
| 141 | false, |
| 142 | false, |
| 143 | false, |
| 144 | false, |
| 145 | true, |
| 146 | true, |
| 147 | true, |
| 148 | true, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 149 | |
| 150 | // extended blend coeffs |
| 151 | false, |
| 152 | false, |
| 153 | false, |
| 154 | false, |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 155 | |
| 156 | // Illegal. |
| 157 | false, |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 158 | }; |
| 159 | return gCoeffReferencesBlendConst[coeff]; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 160 | static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 161 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 162 | static_assert(0 == kZero_GrBlendCoeff); |
| 163 | static_assert(1 == kOne_GrBlendCoeff); |
| 164 | static_assert(2 == kSC_GrBlendCoeff); |
| 165 | static_assert(3 == kISC_GrBlendCoeff); |
| 166 | static_assert(4 == kDC_GrBlendCoeff); |
| 167 | static_assert(5 == kIDC_GrBlendCoeff); |
| 168 | static_assert(6 == kSA_GrBlendCoeff); |
| 169 | static_assert(7 == kISA_GrBlendCoeff); |
| 170 | static_assert(8 == kDA_GrBlendCoeff); |
| 171 | static_assert(9 == kIDA_GrBlendCoeff); |
| 172 | static_assert(10 == kConstC_GrBlendCoeff); |
| 173 | static_assert(11 == kIConstC_GrBlendCoeff); |
| 174 | static_assert(12 == kConstA_GrBlendCoeff); |
| 175 | static_assert(13 == kIConstA_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 176 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 177 | static_assert(14 == kS2C_GrBlendCoeff); |
| 178 | static_assert(15 == kIS2C_GrBlendCoeff); |
| 179 | static_assert(16 == kS2A_GrBlendCoeff); |
| 180 | static_assert(17 == kIS2A_GrBlendCoeff); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 181 | |
| 182 | // assertion for gXfermodeCoeff2Blend have to be in GrGpu scope |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 183 | static_assert(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gXfermodeCoeff2Blend)); |
bsalomon@google.com | 080773c | 2011-03-15 19:09:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 186 | ////////////////////////////////////////////////////////////////////////////// |
| 187 | |
| 188 | static int gl_target_to_binding_index(GrGLenum target) { |
| 189 | switch (target) { |
| 190 | case GR_GL_TEXTURE_2D: |
| 191 | return 0; |
| 192 | case GR_GL_TEXTURE_RECTANGLE: |
| 193 | return 1; |
| 194 | case GR_GL_TEXTURE_EXTERNAL: |
| 195 | return 2; |
| 196 | } |
| 197 | SK_ABORT("Unexpected GL texture target."); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | GrGpuResource::UniqueID GrGLGpu::TextureUnitBindings::boundID(GrGLenum target) const { |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 201 | return fTargetBindings[gl_target_to_binding_index(target)].fBoundResourceID; |
| 202 | } |
| 203 | |
| 204 | bool GrGLGpu::TextureUnitBindings::hasBeenModified(GrGLenum target) const { |
| 205 | return fTargetBindings[gl_target_to_binding_index(target)].fHasBeenModified; |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void GrGLGpu::TextureUnitBindings::setBoundID(GrGLenum target, GrGpuResource::UniqueID resourceID) { |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 209 | int targetIndex = gl_target_to_binding_index(target); |
| 210 | fTargetBindings[targetIndex].fBoundResourceID = resourceID; |
| 211 | fTargetBindings[targetIndex].fHasBeenModified = true; |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 214 | void GrGLGpu::TextureUnitBindings::invalidateForScratchUse(GrGLenum target) { |
| 215 | this->setBoundID(target, GrGpuResource::UniqueID()); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 218 | void GrGLGpu::TextureUnitBindings::invalidateAllTargets(bool markUnmodified) { |
| 219 | for (auto& targetBinding : fTargetBindings) { |
| 220 | targetBinding.fBoundResourceID.makeInvalid(); |
| 221 | if (markUnmodified) { |
| 222 | targetBinding.fHasBeenModified = false; |
| 223 | } |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | ////////////////////////////////////////////////////////////////////////////// |
| 228 | |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 229 | static GrGLenum filter_to_gl_mag_filter(GrSamplerState::Filter filter) { |
| 230 | switch (filter) { |
| 231 | case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST; |
| 232 | case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR; |
| 233 | case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR; |
| 234 | } |
| 235 | SK_ABORT("Unknown filter"); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static GrGLenum filter_to_gl_min_filter(GrSamplerState::Filter filter) { |
| 239 | switch (filter) { |
| 240 | case GrSamplerState::Filter::kNearest: return GR_GL_NEAREST; |
| 241 | case GrSamplerState::Filter::kBilerp: return GR_GL_LINEAR; |
| 242 | case GrSamplerState::Filter::kMipMap: return GR_GL_LINEAR_MIPMAP_LINEAR; |
| 243 | } |
| 244 | SK_ABORT("Unknown filter"); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 247 | static inline GrGLenum wrap_mode_to_gl_wrap(GrSamplerState::WrapMode wrapMode, |
| 248 | const GrCaps& caps) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 249 | switch (wrapMode) { |
| 250 | case GrSamplerState::WrapMode::kClamp: return GR_GL_CLAMP_TO_EDGE; |
| 251 | case GrSamplerState::WrapMode::kRepeat: return GR_GL_REPEAT; |
| 252 | case GrSamplerState::WrapMode::kMirrorRepeat: return GR_GL_MIRRORED_REPEAT; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 253 | case GrSamplerState::WrapMode::kClampToBorder: |
| 254 | // May not be supported but should have been caught earlier |
| 255 | SkASSERT(caps.clampToBorderSupport()); |
| 256 | return GR_GL_CLAMP_TO_BORDER; |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 257 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 258 | SK_ABORT("Unknown wrap mode"); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /////////////////////////////////////////////////////////////////////////////// |
| 262 | |
| 263 | class GrGLGpu::SamplerObjectCache { |
| 264 | public: |
| 265 | SamplerObjectCache(GrGLGpu* gpu) : fGpu(gpu) { |
| 266 | fNumTextureUnits = fGpu->glCaps().shaderCaps()->maxFragmentSamplers(); |
| 267 | fHWBoundSamplers.reset(new GrGLuint[fNumTextureUnits]); |
| 268 | std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0); |
| 269 | std::fill_n(fSamplers, kNumSamplers, 0); |
| 270 | } |
| 271 | |
| 272 | ~SamplerObjectCache() { |
| 273 | if (!fNumTextureUnits) { |
| 274 | // We've already been abandoned. |
| 275 | return; |
| 276 | } |
Chris Dalton | 703fe68 | 2019-05-15 12:58:12 -0600 | [diff] [blame] | 277 | for (GrGLuint sampler : fSamplers) { |
| 278 | // The spec states that "zero" values should be silently ignored, however they still |
| 279 | // trigger GL errors on some NVIDIA platforms. |
| 280 | if (sampler) { |
| 281 | GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(1, &sampler)); |
| 282 | } |
| 283 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void bindSampler(int unitIdx, const GrSamplerState& state) { |
| 287 | int index = StateToIndex(state); |
| 288 | if (!fSamplers[index]) { |
| 289 | GrGLuint s; |
| 290 | GR_GL_CALL(fGpu->glInterface(), GenSamplers(1, &s)); |
| 291 | if (!s) { |
| 292 | return; |
| 293 | } |
| 294 | fSamplers[index] = s; |
| 295 | auto minFilter = filter_to_gl_min_filter(state.filter()); |
| 296 | auto magFilter = filter_to_gl_mag_filter(state.filter()); |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 297 | auto wrapX = wrap_mode_to_gl_wrap(state.wrapModeX(), fGpu->glCaps()); |
| 298 | auto wrapY = wrap_mode_to_gl_wrap(state.wrapModeY(), fGpu->glCaps()); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 299 | GR_GL_CALL(fGpu->glInterface(), |
| 300 | SamplerParameteri(s, GR_GL_TEXTURE_MIN_FILTER, minFilter)); |
| 301 | GR_GL_CALL(fGpu->glInterface(), |
| 302 | SamplerParameteri(s, GR_GL_TEXTURE_MAG_FILTER, magFilter)); |
| 303 | GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_S, wrapX)); |
| 304 | GR_GL_CALL(fGpu->glInterface(), SamplerParameteri(s, GR_GL_TEXTURE_WRAP_T, wrapY)); |
| 305 | } |
| 306 | if (fHWBoundSamplers[unitIdx] != fSamplers[index]) { |
| 307 | GR_GL_CALL(fGpu->glInterface(), BindSampler(unitIdx, fSamplers[index])); |
| 308 | fHWBoundSamplers[unitIdx] = fSamplers[index]; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void invalidateBindings() { |
| 313 | // When we have sampler support we always use samplers. So setting these to zero will cause |
| 314 | // a rebind on next usage. |
| 315 | std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0); |
| 316 | } |
| 317 | |
| 318 | void abandon() { |
| 319 | fHWBoundSamplers.reset(); |
| 320 | fNumTextureUnits = 0; |
| 321 | } |
| 322 | |
| 323 | void release() { |
| 324 | if (!fNumTextureUnits) { |
| 325 | // We've already been abandoned. |
| 326 | return; |
| 327 | } |
| 328 | GR_GL_CALL(fGpu->glInterface(), DeleteSamplers(kNumSamplers, fSamplers)); |
| 329 | std::fill_n(fSamplers, kNumSamplers, 0); |
| 330 | // Deleting a bound sampler implicitly binds sampler 0. |
| 331 | std::fill_n(fHWBoundSamplers.get(), fNumTextureUnits, 0); |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | static int StateToIndex(const GrSamplerState& state) { |
| 336 | int filter = static_cast<int>(state.filter()); |
| 337 | SkASSERT(filter >= 0 && filter < 3); |
| 338 | int wrapX = static_cast<int>(state.wrapModeX()); |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 339 | SkASSERT(wrapX >= 0 && wrapX < 4); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 340 | int wrapY = static_cast<int>(state.wrapModeY()); |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 341 | SkASSERT(wrapY >= 0 && wrapY < 4); |
| 342 | int idx = 16 * filter + 4 * wrapX + wrapY; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 343 | SkASSERT(idx < kNumSamplers); |
| 344 | return idx; |
| 345 | } |
| 346 | |
| 347 | GrGLGpu* fGpu; |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 348 | static constexpr int kNumSamplers = 48; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 349 | std::unique_ptr<GrGLuint[]> fHWBoundSamplers; |
| 350 | GrGLuint fSamplers[kNumSamplers]; |
| 351 | int fNumTextureUnits; |
| 352 | }; |
| 353 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | /////////////////////////////////////////////////////////////////////////////// |
| 355 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 356 | sk_sp<GrGpu> GrGLGpu::Make(sk_sp<const GrGLInterface> interface, const GrContextOptions& options, |
| 357 | GrContext* context) { |
| 358 | if (!interface) { |
Brian Salomon | 3d6801e | 2017-12-11 10:06:31 -0500 | [diff] [blame] | 359 | interface = GrGLMakeNativeInterface(); |
| 360 | // For clients that have written their own GrGLCreateNativeInterface and haven't yet updated |
| 361 | // to GrGLMakeNativeInterface. |
| 362 | if (!interface) { |
| 363 | interface = sk_ref_sp(GrGLCreateNativeInterface()); |
| 364 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 365 | if (!interface) { |
| 366 | return nullptr; |
| 367 | } |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 368 | } |
Jim Van Verth | 7633477 | 2017-05-05 16:46:05 -0400 | [diff] [blame] | 369 | #ifdef USE_NSIGHT |
| 370 | const_cast<GrContextOptions&>(options).fSuppressPathRendering = true; |
| 371 | #endif |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 372 | auto glContext = GrGLContext::Make(std::move(interface), options); |
| 373 | if (!glContext) { |
| 374 | return nullptr; |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 375 | } |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 376 | return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context)); |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 379 | GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context) |
| 380 | : GrGpu(context) |
| 381 | , fGLContext(std::move(ctx)) |
| 382 | , fProgramCache(new ProgramCache(this)) |
| 383 | , fHWProgramID(0) |
| 384 | , fTempSrcFBOID(0) |
| 385 | , fTempDstFBOID(0) |
Brian Osman | a63593a | 2018-12-06 15:54:53 -0500 | [diff] [blame] | 386 | , fStencilClearFBOID(0) { |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 387 | SkASSERT(fGLContext); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 388 | GrGLClearErr(this->glInterface()); |
Brian Salomon | 8ab1cc4 | 2017-12-07 12:40:00 -0500 | [diff] [blame] | 389 | fCaps = sk_ref_sp(fGLContext->caps()); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 390 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 391 | fHWTextureUnitBindings.reset(this->numTextureUnits()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 392 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 393 | this->hwBufferState(GrGpuBufferType::kVertex)->fGLTarget = GR_GL_ARRAY_BUFFER; |
| 394 | this->hwBufferState(GrGpuBufferType::kIndex)->fGLTarget = GR_GL_ELEMENT_ARRAY_BUFFER; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 395 | if (GrGLCaps::kChromium_TransferBufferType == this->glCaps().transferBufferType()) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 396 | this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = |
| 397 | GR_GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM; |
| 398 | this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = |
| 399 | GR_GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 400 | } else { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 401 | this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->fGLTarget = GR_GL_PIXEL_UNPACK_BUFFER; |
| 402 | this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->fGLTarget = GR_GL_PIXEL_PACK_BUFFER; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 403 | } |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 404 | for (int i = 0; i < kGrGpuBufferTypeCount; ++i) { |
Rob Phillips | bc534f6 | 2017-09-05 19:56:19 -0400 | [diff] [blame] | 405 | fHWBufferState[i].invalidate(); |
| 406 | } |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 407 | static_assert(4 == SK_ARRAY_COUNT(fHWBufferState)); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 408 | |
| 409 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
| 410 | fPathRendering.reset(new GrGLPathRendering(this)); |
| 411 | } |
| 412 | |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 413 | if (this->glCaps().samplerObjectSupport()) { |
| 414 | fSamplerObjectCache.reset(new SamplerObjectCache(this)); |
| 415 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | } |
| 417 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 418 | GrGLGpu::~GrGLGpu() { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 419 | // Ensure any GrGpuResource objects get deleted first, since they may require a working GrGLGpu |
| 420 | // to release the resources held by the objects themselves. |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 421 | fPathRendering.reset(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 422 | fCopyProgramArrayBuffer.reset(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 423 | fMipmapProgramArrayBuffer.reset(); |
kkinnunen | 702501d | 2016-01-13 23:36:45 -0800 | [diff] [blame] | 424 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 425 | fHWProgram.reset(); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 426 | if (fHWProgramID) { |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 427 | // detach the current program so there is no confusion on OpenGL's part |
| 428 | // that we want it to be deleted |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 429 | GL_CALL(UseProgram(0)); |
| 430 | } |
| 431 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 432 | if (fTempSrcFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 433 | this->deleteFramebuffer(fTempSrcFBOID); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 434 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 435 | if (fTempDstFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 436 | this->deleteFramebuffer(fTempDstFBOID); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 437 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 438 | if (fStencilClearFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 439 | this->deleteFramebuffer(fStencilClearFBOID); |
bsalomon | dd3143b | 2015-02-23 09:27:45 -0800 | [diff] [blame] | 440 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 441 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 442 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 443 | if (0 != fCopyPrograms[i].fProgram) { |
| 444 | GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram)); |
| 445 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 446 | } |
bsalomon | 6dea83f | 2015-12-03 12:58:06 -0800 | [diff] [blame] | 447 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 448 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 449 | if (0 != fMipmapPrograms[i].fProgram) { |
| 450 | GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram)); |
| 451 | } |
| 452 | } |
| 453 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 454 | delete fProgramCache; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 455 | fSamplerObjectCache.reset(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 456 | } |
| 457 | |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 458 | void GrGLGpu::disconnect(DisconnectType type) { |
| 459 | INHERITED::disconnect(type); |
| 460 | if (DisconnectType::kCleanup == type) { |
| 461 | if (fHWProgramID) { |
| 462 | GL_CALL(UseProgram(0)); |
| 463 | } |
| 464 | if (fTempSrcFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 465 | this->deleteFramebuffer(fTempSrcFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 466 | } |
| 467 | if (fTempDstFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 468 | this->deleteFramebuffer(fTempDstFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 469 | } |
| 470 | if (fStencilClearFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 471 | this->deleteFramebuffer(fStencilClearFBOID); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 472 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 473 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 474 | if (fCopyPrograms[i].fProgram) { |
| 475 | GL_CALL(DeleteProgram(fCopyPrograms[i].fProgram)); |
| 476 | } |
| 477 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 478 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 479 | if (fMipmapPrograms[i].fProgram) { |
| 480 | GL_CALL(DeleteProgram(fMipmapPrograms[i].fProgram)); |
| 481 | } |
| 482 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 483 | |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 484 | if (fSamplerObjectCache) { |
| 485 | fSamplerObjectCache->release(); |
| 486 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 487 | } else { |
| 488 | if (fProgramCache) { |
| 489 | fProgramCache->abandon(); |
| 490 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 491 | if (fSamplerObjectCache) { |
| 492 | fSamplerObjectCache->abandon(); |
| 493 | } |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 496 | fHWProgram.reset(); |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 497 | delete fProgramCache; |
| 498 | fProgramCache = nullptr; |
| 499 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 500 | fHWProgramID = 0; |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 501 | fTempSrcFBOID = 0; |
| 502 | fTempDstFBOID = 0; |
| 503 | fStencilClearFBOID = 0; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 504 | fCopyProgramArrayBuffer.reset(); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 505 | for (size_t i = 0; i < SK_ARRAY_COUNT(fCopyPrograms); ++i) { |
| 506 | fCopyPrograms[i].fProgram = 0; |
| 507 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 508 | fMipmapProgramArrayBuffer.reset(); |
| 509 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMipmapPrograms); ++i) { |
| 510 | fMipmapPrograms[i].fProgram = 0; |
| 511 | } |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 512 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 513 | if (this->glCaps().shaderCaps()->pathRenderingSupport()) { |
bsalomon | 6e2aad4 | 2016-04-01 11:54:31 -0700 | [diff] [blame] | 514 | this->glPathRendering()->disconnect(type); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 515 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 516 | } |
| 517 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 518 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | a85449d | 2011-11-19 02:36:05 +0000 | [diff] [blame] | 519 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 520 | void GrGLGpu::onResetContext(uint32_t resetBits) { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 521 | if (resetBits & kMisc_GrGLBackendState) { |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 522 | // we don't use the zb at all |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 523 | GL_CALL(Disable(GR_GL_DEPTH_TEST)); |
| 524 | GL_CALL(DepthMask(GR_GL_FALSE)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 525 | |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 526 | // We don't use face culling. |
| 527 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 528 | // We do use separate stencil. Our algorithms don't care which face is front vs. back so |
| 529 | // just set this to the default for self-consistency. |
| 530 | GL_CALL(FrontFace(GR_GL_CCW)); |
Brian Salomon | f086167 | 2017-05-08 11:10:10 -0400 | [diff] [blame] | 531 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 532 | this->hwBufferState(GrGpuBufferType::kXferCpuToGpu)->invalidate(); |
| 533 | this->hwBufferState(GrGpuBufferType::kXferGpuToCpu)->invalidate(); |
cdalton | c161310 | 2016-03-16 07:48:20 -0700 | [diff] [blame] | 534 | |
Kevin Lubick | 8aa203c | 2019-03-19 13:23:10 -0400 | [diff] [blame] | 535 | if (GR_IS_GR_GL(this->glStandard())) { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 536 | #ifndef USE_NSIGHT |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 537 | // Desktop-only state that we never change |
| 538 | if (!this->glCaps().isCoreProfile()) { |
| 539 | GL_CALL(Disable(GR_GL_POINT_SMOOTH)); |
| 540 | GL_CALL(Disable(GR_GL_LINE_SMOOTH)); |
| 541 | GL_CALL(Disable(GR_GL_POLYGON_SMOOTH)); |
| 542 | GL_CALL(Disable(GR_GL_POLYGON_STIPPLE)); |
| 543 | GL_CALL(Disable(GR_GL_COLOR_LOGIC_OP)); |
| 544 | GL_CALL(Disable(GR_GL_INDEX_LOGIC_OP)); |
| 545 | } |
| 546 | // The windows NVIDIA driver has GL_ARB_imaging in the extension string when using a |
| 547 | // core profile. This seems like a bug since the core spec removes any mention of |
| 548 | // GL_ARB_imaging. |
| 549 | if (this->glCaps().imagingSupport() && !this->glCaps().isCoreProfile()) { |
| 550 | GL_CALL(Disable(GR_GL_COLOR_TABLE)); |
| 551 | } |
| 552 | GL_CALL(Disable(GR_GL_POLYGON_OFFSET_FILL)); |
Jim Van Verth | 609e7cc | 2017-03-30 14:28:08 -0400 | [diff] [blame] | 553 | |
Chris Dalton | 1215cda | 2019-12-17 21:44:04 -0700 | [diff] [blame] | 554 | fHWWireframeEnabled = kUnknown_TriState; |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 555 | #endif |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 556 | // Since ES doesn't support glPointSize at all we always use the VS to |
| 557 | // set the point size |
| 558 | GL_CALL(Enable(GR_GL_VERTEX_PROGRAM_POINT_SIZE)); |
| 559 | |
bsalomon@google.com | 2b1b8c0 | 2013-02-28 22:06:02 +0000 | [diff] [blame] | 560 | } |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 561 | |
Kevin Lubick | 8aa203c | 2019-03-19 13:23:10 -0400 | [diff] [blame] | 562 | if (GR_IS_GR_GL_ES(this->glStandard()) && |
Brian Salomon | 8bc352c | 2019-01-28 09:05:22 -0500 | [diff] [blame] | 563 | this->glCaps().fbFetchRequiresEnablePerSample()) { |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 564 | // The arm extension requires specifically enabling MSAA fetching per sample. |
| 565 | // On some devices this may have a perf hit. Also multiple render targets are disabled |
Brian Salomon | 8bc352c | 2019-01-28 09:05:22 -0500 | [diff] [blame] | 566 | GL_CALL(Enable(GR_GL_FETCH_PER_SAMPLE)); |
joshualitt | 5816233 | 2014-08-01 06:44:53 -0700 | [diff] [blame] | 567 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 568 | fHWWriteToColor = kUnknown_TriState; |
| 569 | // we only ever use lines in hairline mode |
| 570 | GL_CALL(LineWidth(1)); |
bsalomon | aca31fe | 2015-09-22 11:38:46 -0700 | [diff] [blame] | 571 | GL_CALL(Disable(GR_GL_DITHER)); |
Brian Salomon | 805cc7a | 2019-01-28 09:52:34 -0500 | [diff] [blame] | 572 | |
| 573 | fHWClearColor[0] = fHWClearColor[1] = fHWClearColor[2] = fHWClearColor[3] = SK_FloatNaN; |
bsalomon@google.com | cad107b | 2013-06-28 14:32:08 +0000 | [diff] [blame] | 574 | } |
edisonn@google.com | ba66999 | 2013-06-28 16:03:21 +0000 | [diff] [blame] | 575 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 576 | if (resetBits & kMSAAEnable_GrGLBackendState) { |
| 577 | fMSAAEnabled = kUnknown_TriState; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 578 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 579 | if (this->caps()->mixedSamplesSupport()) { |
cdalton | af8bc7d | 2016-02-05 09:35:20 -0800 | [diff] [blame] | 580 | // The skia blend modes all use premultiplied alpha and therefore expect RGBA coverage |
| 581 | // modulation. This state has no effect when not rendering to a mixed sampled target. |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 582 | GL_CALL(CoverageModulation(GR_GL_RGBA)); |
| 583 | } |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 584 | |
| 585 | fHWConservativeRasterEnabled = kUnknown_TriState; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 586 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 587 | |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 588 | fHWActiveTextureUnitIdx = -1; // invalid |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 589 | fLastPrimitiveType = static_cast<GrPrimitiveType>(-1); |
commit-bot@chromium.org | 46fbfe0 | 2013-08-30 15:52:12 +0000 | [diff] [blame] | 590 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 591 | if (resetBits & kTextureBinding_GrGLBackendState) { |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 592 | for (int s = 0; s < this->numTextureUnits(); ++s) { |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 593 | fHWTextureUnitBindings[s].invalidateAllTargets(false); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 594 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 595 | if (fSamplerObjectCache) { |
| 596 | fSamplerObjectCache->invalidateBindings(); |
| 597 | } |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 598 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 599 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 600 | if (resetBits & kBlend_GrGLBackendState) { |
| 601 | fHWBlendState.invalidate(); |
| 602 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 603 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 604 | if (resetBits & kView_GrGLBackendState) { |
| 605 | fHWScissorSettings.invalidate(); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 606 | fHWWindowRectsState.invalidate(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 607 | fHWViewport.invalidate(); |
| 608 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 609 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 610 | if (resetBits & kStencil_GrGLBackendState) { |
| 611 | fHWStencilSettings.invalidate(); |
| 612 | fHWStencilTestEnabled = kUnknown_TriState; |
| 613 | } |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 614 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 615 | // Vertex |
| 616 | if (resetBits & kVertex_GrGLBackendState) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 617 | fHWVertexArrayState.invalidate(); |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 618 | this->hwBufferState(GrGpuBufferType::kVertex)->invalidate(); |
| 619 | this->hwBufferState(GrGpuBufferType::kIndex)->invalidate(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 620 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 621 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 622 | if (resetBits & kRenderTarget_GrGLBackendState) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 623 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 16921ec | 2015-07-30 15:34:56 -0700 | [diff] [blame] | 624 | fHWSRGBFramebuffer = kUnknown_TriState; |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 625 | } |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 626 | |
commit-bot@chromium.org | 0a6fe71 | 2014-04-23 19:26:26 +0000 | [diff] [blame] | 627 | if (resetBits & kPathRendering_GrGLBackendState) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 628 | if (this->caps()->shaderCaps()->pathRenderingSupport()) { |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 629 | this->glPathRendering()->resetContext(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 630 | } |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 631 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 632 | |
bsalomon@google.com | 8ef3fd0 | 2011-11-21 15:53:13 +0000 | [diff] [blame] | 633 | // we assume these values |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 634 | if (resetBits & kPixelStore_GrGLBackendState) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 635 | if (this->caps()->writePixelsRowBytesSupport()) { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 636 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 637 | } |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 638 | if (this->glCaps().readPixelsRowBytesSupport()) { |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 639 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 640 | } |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 641 | if (this->glCaps().packFlipYSupport()) { |
| 642 | GL_CALL(PixelStorei(GR_GL_PACK_REVERSE_ROW_ORDER, GR_GL_FALSE)); |
| 643 | } |
bsalomon@google.com | 56d11e0 | 2011-11-30 19:59:08 +0000 | [diff] [blame] | 644 | } |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 645 | |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 646 | if (resetBits & kProgram_GrGLBackendState) { |
| 647 | fHWProgramID = 0; |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 648 | fHWProgram.reset(); |
bsalomon@google.com | 0a208a1 | 2013-06-28 18:57:35 +0000 | [diff] [blame] | 649 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 650 | ++fResetTimestampForTextureParameters; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 653 | static bool check_backend_texture(const GrBackendTexture& backendTex, const GrColorType colorType, |
| 654 | const GrGLCaps& caps, GrGLTexture::Desc* desc, |
| 655 | bool skipRectTexSupportCheck = false) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 656 | GrGLTextureInfo info; |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 657 | if (!backendTex.getGLTextureInfo(&info) || !info.fID || !info.fFormat) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 658 | return false; |
bsalomon | 091f60c | 2015-11-10 11:54:56 -0800 | [diff] [blame] | 659 | } |
robertphillips@google.com | b72e5d3 | 2012-10-30 15:18:10 +0000 | [diff] [blame] | 660 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 661 | desc->fSize = {backendTex.width(), backendTex.height()}; |
| 662 | desc->fTarget = info.fTarget; |
| 663 | desc->fID = info.fID; |
| 664 | desc->fFormat = GrGLFormatFromGLEnum(info.fFormat); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 665 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 666 | if (desc->fFormat == GrGLFormat::kUnknown) { |
| 667 | return false; |
| 668 | } |
| 669 | if (GR_GL_TEXTURE_EXTERNAL == desc->fTarget) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 670 | if (!caps.shaderCaps()->externalTextureSupport()) { |
| 671 | return false; |
| 672 | } |
Brian Salomon | f04fb44 | 2019-08-08 16:06:29 -0400 | [diff] [blame] | 673 | } else if (GR_GL_TEXTURE_RECTANGLE == desc->fTarget) { |
| 674 | if (!caps.rectangleTextureSupport() && !skipRectTexSupportCheck) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 675 | return false; |
| 676 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 677 | } else if (GR_GL_TEXTURE_2D != desc->fTarget) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 678 | return false; |
| 679 | } |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 680 | if (backendTex.isProtected()) { |
| 681 | // Not supported in GL backend at this time. |
| 682 | return false; |
| 683 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 684 | |
| 685 | desc->fConfig = caps.getConfigFromBackendFormat(backendTex.getBackendFormat(), colorType); |
| 686 | SkASSERT(desc->fConfig != kUnknown_GrPixelConfig); |
| 687 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 688 | return true; |
| 689 | } |
| 690 | |
| 691 | sk_sp<GrTexture> GrGLGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 692 | GrColorType colorType, GrWrapOwnership ownership, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 693 | GrWrapCacheable cacheable, GrIOType ioType) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 694 | GrGLTexture::Desc desc; |
| 695 | if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 696 | return nullptr; |
| 697 | } |
Robert Phillips | 1cd1ed8 | 2019-07-23 13:21:01 -0400 | [diff] [blame] | 698 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 699 | if (kBorrow_GrWrapOwnership == ownership) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 700 | desc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 701 | } else { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 702 | desc.fOwnership = GrBackendObjectOwnership::kOwned; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 703 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 704 | |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 705 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kValid |
| 706 | : GrMipMapsStatus::kNotAllocated; |
| 707 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 708 | auto texture = GrGLTexture::MakeWrapped(this, mipMapsStatus, desc, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 709 | backendTex.getGLTextureParams(), cacheable, ioType); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 710 | // We don't know what parameters are already set on wrapped textures. |
| 711 | texture->textureParamsModified(); |
Brian Salomon | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 712 | return texture; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 715 | sk_sp<GrTexture> GrGLGpu::onWrapCompressedBackendTexture(const GrBackendTexture& backendTex, |
| 716 | GrWrapOwnership ownership, |
| 717 | GrWrapCacheable cacheable) { |
| 718 | return nullptr; |
| 719 | } |
| 720 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 721 | sk_sp<GrTexture> GrGLGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 722 | int sampleCnt, |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 723 | GrColorType colorType, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 724 | GrWrapOwnership ownership, |
| 725 | GrWrapCacheable cacheable) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 726 | const GrGLCaps& caps = this->glCaps(); |
| 727 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 728 | GrGLTexture::Desc desc; |
| 729 | if (!check_backend_texture(backendTex, colorType, this->glCaps(), &desc)) { |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 730 | return nullptr; |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 731 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 732 | SkASSERT(caps.isFormatRenderable(desc.fFormat, sampleCnt)); |
| 733 | SkASSERT(caps.isFormatTexturable(desc.fFormat)); |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 734 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 735 | // We don't support rendering to a EXTERNAL texture. |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 736 | if (GR_GL_TEXTURE_EXTERNAL == desc.fTarget) { |
bsalomon | a98419b | 2015-11-23 07:09:50 -0800 | [diff] [blame] | 737 | return nullptr; |
| 738 | } |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 739 | |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 740 | if (kBorrow_GrWrapOwnership == ownership) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 741 | desc.fOwnership = GrBackendObjectOwnership::kBorrowed; |
Brian Osman | 766fcbb | 2017-03-13 09:33:09 -0400 | [diff] [blame] | 742 | } else { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 743 | desc.fOwnership = GrBackendObjectOwnership::kOwned; |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 744 | } |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 745 | |
Robert Phillips | 0902c98 | 2019-07-16 07:47:56 -0400 | [diff] [blame] | 746 | |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 747 | sampleCnt = caps.getRenderTargetSampleCount(sampleCnt, desc.fFormat); |
| 748 | SkASSERT(sampleCnt); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 749 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 750 | GrGLRenderTarget::IDs rtIDs; |
| 751 | if (!this->createRenderTargetObjects(desc, sampleCnt, &rtIDs)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 752 | return nullptr; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 753 | } |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 754 | |
| 755 | GrMipMapsStatus mipMapsStatus = backendTex.hasMipMaps() ? GrMipMapsStatus::kDirty |
| 756 | : GrMipMapsStatus::kNotAllocated; |
| 757 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 758 | sk_sp<GrGLTextureRenderTarget> texRT(GrGLTextureRenderTarget::MakeWrapped( |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 759 | this, sampleCnt, desc, backendTex.getGLTextureParams(), rtIDs, cacheable, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 760 | mipMapsStatus)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 761 | texRT->baseLevelWasBoundToFBO(); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 762 | // We don't know what parameters are already set on wrapped textures. |
| 763 | texRT->textureParamsModified(); |
Brian Salomon | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 764 | return texRT; |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 767 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT, |
| 768 | GrColorType grColorType) { |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 769 | GrGLFramebufferInfo info; |
| 770 | if (!backendRT.getGLFramebufferInfo(&info)) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 771 | return nullptr; |
| 772 | } |
| 773 | |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 774 | if (backendRT.isProtected()) { |
| 775 | // Not supported in GL at this time. |
| 776 | return nullptr; |
| 777 | } |
| 778 | |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 779 | const auto format = backendRT.getBackendFormat().asGLFormat(); |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 780 | if (!this->glCaps().isFormatRenderable(format, backendRT.sampleCnt())) { |
| 781 | return nullptr; |
| 782 | } |
| 783 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 784 | GrGLRenderTarget::IDs rtIDs; |
| 785 | rtIDs.fRTFBOID = info.fFBOID; |
| 786 | rtIDs.fMSColorRenderbufferID = 0; |
| 787 | rtIDs.fTexFBOID = GrGLRenderTarget::kUnresolvableFBOID; |
| 788 | rtIDs.fRTFBOOwnership = GrBackendObjectOwnership::kBorrowed; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 789 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 790 | GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(), |
| 791 | grColorType); |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 792 | SkASSERT(kUnknown_GrPixelConfig != config); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 793 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 794 | const auto size = SkISize::Make(backendRT.width(), backendRT.height()); |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 795 | int sampleCount = this->glCaps().getRenderTargetSampleCount(backendRT.sampleCnt(), format); |
bsalomon | b15b4c1 | 2014-10-29 12:41:57 -0700 | [diff] [blame] | 796 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 797 | return GrGLRenderTarget::MakeWrapped(this, size, format, config, sampleCount, rtIDs, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 798 | backendRT.stencilBits()); |
bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 801 | sk_sp<GrRenderTarget> GrGLGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 802 | int sampleCnt, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 803 | GrColorType colorType) { |
| 804 | GrGLTexture::Desc desc; |
| 805 | // We do not check whether texture rectangle is supported by Skia - if the caller provided us |
| 806 | // with a texture rectangle,we assume the necessary support exists. |
| 807 | if (!check_backend_texture(tex, colorType, this->glCaps(), &desc, true)) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 808 | return nullptr; |
| 809 | } |
Greg Daniel | 6fa62e2 | 2019-08-07 15:52:37 -0400 | [diff] [blame] | 810 | |
| 811 | if (!this->glCaps().isFormatRenderable(desc.fFormat, sampleCnt)) { |
| 812 | return nullptr; |
| 813 | } |
| 814 | |
| 815 | const int sampleCount = this->glCaps().getRenderTargetSampleCount(sampleCnt, desc.fFormat); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 816 | GrGLRenderTarget::IDs rtIDs; |
| 817 | if (!this->createRenderTargetObjects(desc, sampleCount, &rtIDs)) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 818 | return nullptr; |
| 819 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 820 | return GrGLRenderTarget::MakeWrapped(this, desc.fSize, desc.fFormat, desc.fConfig, sampleCount, |
| 821 | rtIDs, 0); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 824 | static bool check_write_and_transfer_input(GrGLTexture* glTex) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 825 | if (!glTex) { |
| 826 | return false; |
| 827 | } |
bsalomon@google.com | 280e99f | 2012-01-05 16:17:38 +0000 | [diff] [blame] | 828 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 829 | // Write or transfer of pixels is not implemented for TEXTURE_EXTERNAL textures |
| 830 | if (GR_GL_TEXTURE_EXTERNAL == glTex->target()) { |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 831 | return false; |
| 832 | } |
| 833 | |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 834 | return true; |
| 835 | } |
| 836 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 837 | bool GrGLGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 838 | GrColorType surfaceColorType, GrColorType srcColorType, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 839 | const GrMipLevel texels[], int mipLevelCount, |
| 840 | bool prepForTexSampling) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 841 | auto glTex = static_cast<GrGLTexture*>(surface->asTexture()); |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 842 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 843 | if (!check_write_and_transfer_input(glTex)) { |
jvanverth | 17aa047 | 2016-01-05 10:41:27 -0800 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 847 | this->bindTextureToScratchUnit(glTex->target(), glTex->textureID()); |
bsalomon@google.com | 9d6cfd8 | 2011-11-05 13:25:21 +0000 | [diff] [blame] | 848 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 849 | SkASSERT(!GrGLFormatIsCompressed(glTex->format())); |
Brian Salomon | 85769bf | 2019-08-01 15:52:34 -0400 | [diff] [blame] | 850 | return this->uploadTexData(glTex->format(), surfaceColorType, glTex->width(), glTex->height(), |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 851 | glTex->target(), left, top, width, height, srcColorType, texels, |
| 852 | mipLevelCount); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 855 | bool GrGLGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 856 | GrColorType textureColorType, GrColorType bufferColorType, |
| 857 | GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 858 | GrGLTexture* glTex = static_cast<GrGLTexture*>(texture); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 859 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 860 | // Can't transfer compressed data |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 861 | SkASSERT(!GrGLFormatIsCompressed(glTex->format())); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 862 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 863 | if (!check_write_and_transfer_input(glTex)) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | |
Hal Canary | c36f7e7 | 2018-06-18 15:50:09 -0400 | [diff] [blame] | 867 | static_assert(sizeof(int) == sizeof(int32_t), ""); |
| 868 | if (width <= 0 || height <= 0) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 869 | return false; |
| 870 | } |
| 871 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 872 | this->bindTextureToScratchUnit(glTex->target(), glTex->textureID()); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 873 | |
| 874 | SkASSERT(!transferBuffer->isMapped()); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 875 | SkASSERT(!transferBuffer->isCpuBuffer()); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 876 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(transferBuffer); |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 877 | this->bindBuffer(GrGpuBufferType::kXferCpuToGpu, glBuffer); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 878 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 879 | SkDEBUGCODE( |
| 880 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 881 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 882 | SkASSERT(bounds.contains(subRect)); |
| 883 | ) |
| 884 | |
Brian Salomon | b28cb68 | 2019-07-26 12:48:47 -0400 | [diff] [blame] | 885 | size_t bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 886 | const size_t trimRowBytes = width * bpp; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 887 | const void* pixels = (void*)offset; |
Bruce Dawson | 71479b7 | 2017-07-05 14:30:20 -0700 | [diff] [blame] | 888 | if (width < 0 || height < 0) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 889 | return false; |
| 890 | } |
| 891 | |
| 892 | bool restoreGLRowLength = false; |
| 893 | if (trimRowBytes != rowBytes) { |
| 894 | // we should have checked for this support already |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 895 | SkASSERT(this->glCaps().writePixelsRowBytesSupport()); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 896 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowBytes / bpp)); |
| 897 | restoreGLRowLength = true; |
| 898 | } |
| 899 | |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 900 | GrGLFormat textureFormat = glTex->format(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 901 | // External format and type come from the upload data. |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 902 | GrGLenum externalFormat = 0; |
| 903 | GrGLenum externalType = 0; |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 904 | this->glCaps().getTexSubImageExternalFormatAndType( |
| 905 | textureFormat, textureColorType, bufferColorType, &externalFormat, &externalType); |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 906 | if (!externalFormat || !externalType) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 907 | return false; |
| 908 | } |
| 909 | |
Brian Salomon | 8cbf662 | 2019-07-30 11:03:14 -0400 | [diff] [blame] | 910 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 911 | GL_CALL(TexSubImage2D(glTex->target(), |
| 912 | 0, |
| 913 | left, top, |
| 914 | width, |
| 915 | height, |
| 916 | externalFormat, externalType, |
| 917 | pixels)); |
| 918 | |
| 919 | if (restoreGLRowLength) { |
| 920 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 921 | } |
| 922 | |
| 923 | return true; |
| 924 | } |
| 925 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 926 | bool GrGLGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 927 | GrColorType surfaceColorType, GrColorType dstColorType, |
| 928 | GrGpuBuffer* transferBuffer, size_t offset) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 929 | auto* glBuffer = static_cast<GrGLBuffer*>(transferBuffer); |
| 930 | this->bindBuffer(GrGpuBufferType::kXferGpuToCpu, glBuffer); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 931 | auto offsetAsPtr = reinterpret_cast<void*>(offset); |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 932 | return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType, |
| 933 | dstColorType, offsetAsPtr, width); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 936 | void GrGLGpu::unbindCpuToGpuXferBuffer() { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 937 | auto* xferBufferState = this->hwBufferState(GrGpuBufferType::kXferCpuToGpu); |
| 938 | if (!xferBufferState->fBoundBufferUniqueID.isInvalid()) { |
| 939 | GL_CALL(BindBuffer(xferBufferState->fGLTarget, 0)); |
| 940 | xferBufferState->invalidate(); |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 941 | } |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 942 | } |
| 943 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 944 | bool GrGLGpu::uploadTexData(GrGLFormat textureFormat, GrColorType textureColorType, int texWidth, |
| 945 | int texHeight, GrGLenum target, int left, int top, int width, |
| 946 | int height, GrColorType srcColorType, const GrMipLevel texels[], |
| 947 | int mipLevelCount, GrMipMapsStatus* mipMapsStatus) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 948 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 949 | SkASSERT(!GrGLFormatIsCompressed(textureFormat)); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 950 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 951 | SkASSERT(this->glCaps().isFormatTexturable(textureFormat)); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 952 | SkDEBUGCODE( |
| 953 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 954 | SkIRect bounds = SkIRect::MakeWH(texWidth, texHeight); |
| 955 | SkASSERT(bounds.contains(subRect)); |
| 956 | ) |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 957 | SkASSERT(1 == mipLevelCount || |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 958 | (0 == left && 0 == top && width == texWidth && height == texHeight)); |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 959 | |
Brian Osman | 9b56024 | 2017-09-05 15:34:52 -0400 | [diff] [blame] | 960 | this->unbindCpuToGpuXferBuffer(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 961 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 962 | const GrGLInterface* interface = this->glInterface(); |
| 963 | const GrGLCaps& caps = this->glCaps(); |
| 964 | |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 965 | size_t bpp = GrColorTypeBytesPerPixel(srcColorType); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 966 | |
| 967 | if (width == 0 || height == 0) { |
bsalomon@google.com | 136f55b | 2011-11-28 18:34:44 +0000 | [diff] [blame] | 968 | return false; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 969 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 970 | |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 971 | // External format and type come from the upload data. |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 972 | GrGLenum externalFormat; |
| 973 | GrGLenum externalType; |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 974 | this->glCaps().getTexSubImageExternalFormatAndType( |
| 975 | textureFormat, textureColorType, srcColorType, &externalFormat, &externalType); |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 976 | if (!externalFormat || !externalType) { |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 977 | return false; |
| 978 | } |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 979 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 980 | /* |
bsalomon | 5b30c6f | 2015-12-17 14:17:34 -0800 | [diff] [blame] | 981 | * Check whether to allocate a temporary buffer for flipping y or |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 982 | * because our srcData has extra bytes past each row. If so, we need |
| 983 | * to trim those off here, since GL ES may not let us specify |
| 984 | * GL_UNPACK_ROW_LENGTH. |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 985 | */ |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 986 | bool restoreGLRowLength = false; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 987 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 988 | if (mipMapsStatus) { |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 989 | *mipMapsStatus = (mipLevelCount > 1) ? |
| 990 | GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 991 | } |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 992 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 993 | GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 994 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 995 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 996 | if (!texels[currentMipLevel].fPixels) { |
| 997 | if (mipMapsStatus) { |
| 998 | *mipMapsStatus = GrMipMapsStatus::kDirty; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 999 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1000 | continue; |
Brian Salomon | 8e63cab | 2019-09-10 19:02:29 +0000 | [diff] [blame] | 1001 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1002 | int twoToTheMipLevel = 1 << currentMipLevel; |
| 1003 | const int currentWidth = SkTMax(1, width / twoToTheMipLevel); |
| 1004 | const int currentHeight = SkTMax(1, height / twoToTheMipLevel); |
| 1005 | const size_t trimRowBytes = currentWidth * bpp; |
| 1006 | const size_t rowBytes = texels[currentMipLevel].fRowBytes; |
| 1007 | |
| 1008 | if (caps.writePixelsRowBytesSupport() && (rowBytes != trimRowBytes || restoreGLRowLength)) { |
| 1009 | GrGLint rowLength = static_cast<GrGLint>(rowBytes / bpp); |
| 1010 | GR_GL_CALL(interface, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, rowLength)); |
| 1011 | restoreGLRowLength = true; |
| 1012 | } |
| 1013 | |
| 1014 | GL_CALL(TexSubImage2D(target, currentMipLevel, left, top, currentWidth, currentHeight, |
| 1015 | externalFormat, externalType, texels[currentMipLevel].fPixels)); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1016 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1017 | if (restoreGLRowLength) { |
| 1018 | SkASSERT(caps.writePixelsRowBytesSupport()); |
| 1019 | GL_CALL(PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); |
| 1020 | } |
| 1021 | return true; |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1024 | bool GrGLGpu::uploadCompressedTexData(GrGLFormat format, |
| 1025 | SkImage::CompressionType compressionType, |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 1026 | SkISize dimensions, |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1027 | GrMipMapped mipMapped, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1028 | GrGLenum target, |
| 1029 | const void* data) { |
| 1030 | SkASSERT(format != GrGLFormat::kUnknown); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1031 | const GrGLCaps& caps = this->glCaps(); |
| 1032 | |
| 1033 | // We only need the internal format for compressed 2D textures. |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1034 | GrGLenum internalFormat = caps.getTexImageOrStorageInternalFormat(format); |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1035 | if (!internalFormat) { |
| 1036 | return 0; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1039 | bool useTexStorage = caps.formatSupportsTexStorage(format); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1040 | |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1041 | int numMipLevels = 1; |
| 1042 | if (mipMapped == GrMipMapped::kYes) { |
| 1043 | numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height())+1; |
| 1044 | } |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1045 | |
| 1046 | // Make sure that the width and height that we pass to OpenGL |
| 1047 | // is a multiple of the block size. |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1048 | size_t dataSize = GrCompressedDataSize(compressionType, dimensions, nullptr, mipMapped); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1049 | |
| 1050 | if (useTexStorage) { |
| 1051 | // We never resize or change formats of textures. |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1052 | GL_ALLOC_CALL(this->glInterface(), |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1053 | TexStorage2D(target, numMipLevels, internalFormat, dimensions.width(), |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1054 | dimensions.height())); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1055 | GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface()); |
| 1056 | if (error != GR_GL_NO_ERROR) { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1057 | return false; |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1058 | } |
| 1059 | GL_CALL(CompressedTexSubImage2D(target, |
| 1060 | 0, // level |
| 1061 | 0, // left |
| 1062 | 0, // top |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1063 | dimensions.width(), |
| 1064 | dimensions.height(), |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1065 | internalFormat, |
| 1066 | SkToInt(dataSize), |
| 1067 | data)); |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 1068 | |
| 1069 | error = CHECK_ALLOC_ERROR(this->glInterface()); |
| 1070 | if (error != GR_GL_NO_ERROR) { |
| 1071 | return false; |
| 1072 | } |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1073 | } else { |
| 1074 | GL_ALLOC_CALL(this->glInterface(), CompressedTexImage2D(target, |
| 1075 | 0, // level |
| 1076 | internalFormat, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1077 | dimensions.width(), |
| 1078 | dimensions.height(), |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1079 | 0, // border |
| 1080 | SkToInt(dataSize), |
| 1081 | data)); |
| 1082 | |
| 1083 | GrGLenum error = CHECK_ALLOC_ERROR(this->glInterface()); |
| 1084 | if (error != GR_GL_NO_ERROR) { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1085 | return false; |
Greg Kaiser | 73bfb89 | 2019-02-11 09:03:41 -0800 | [diff] [blame] | 1086 | } |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1087 | } |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1088 | return true; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
bsalomon | 424cc26 | 2015-05-22 10:37:30 -0700 | [diff] [blame] | 1091 | static bool renderbuffer_storage_msaa(const GrGLContext& ctx, |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1092 | int sampleCount, |
| 1093 | GrGLenum format, |
| 1094 | int width, int height) { |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 1095 | CLEAR_ERROR_BEFORE_ALLOC(ctx.interface()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1096 | SkASSERT(GrGLCaps::kNone_MSFBOType != ctx.caps()->msFBOType()); |
commit-bot@chromium.org | b1854a8 | 2014-01-16 18:39:04 +0000 | [diff] [blame] | 1097 | switch (ctx.caps()->msFBOType()) { |
Brian Salomon | 00731b4 | 2016-10-14 11:30:51 -0400 | [diff] [blame] | 1098 | case GrGLCaps::kStandard_MSFBOType: |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1099 | GL_ALLOC_CALL(ctx.interface(), |
| 1100 | RenderbufferStorageMultisample(GR_GL_RENDERBUFFER, |
| 1101 | sampleCount, |
| 1102 | format, |
| 1103 | width, height)); |
| 1104 | break; |
| 1105 | case GrGLCaps::kES_Apple_MSFBOType: |
| 1106 | GL_ALLOC_CALL(ctx.interface(), |
| 1107 | RenderbufferStorageMultisampleES2APPLE(GR_GL_RENDERBUFFER, |
| 1108 | sampleCount, |
| 1109 | format, |
| 1110 | width, height)); |
| 1111 | break; |
| 1112 | case GrGLCaps::kES_EXT_MsToTexture_MSFBOType: |
| 1113 | case GrGLCaps::kES_IMG_MsToTexture_MSFBOType: |
| 1114 | GL_ALLOC_CALL(ctx.interface(), |
| 1115 | RenderbufferStorageMultisampleES2EXT(GR_GL_RENDERBUFFER, |
| 1116 | sampleCount, |
| 1117 | format, |
| 1118 | width, height)); |
| 1119 | break; |
| 1120 | case GrGLCaps::kNone_MSFBOType: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 1121 | SK_ABORT("Shouldn't be here if we don't support multisampled renderbuffers."); |
commit-bot@chromium.org | 040fd8f | 2013-09-06 20:00:41 +0000 | [diff] [blame] | 1122 | break; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1123 | } |
Brian Salomon | 9251d4e | 2015-03-17 16:03:19 -0400 | [diff] [blame] | 1124 | return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface())); |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1127 | bool GrGLGpu::createRenderTargetObjects(const GrGLTexture::Desc& desc, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1128 | int sampleCount, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1129 | GrGLRenderTarget::IDs* rtIDs) { |
| 1130 | rtIDs->fMSColorRenderbufferID = 0; |
| 1131 | rtIDs->fRTFBOID = 0; |
| 1132 | rtIDs->fRTFBOOwnership = GrBackendObjectOwnership::kOwned; |
| 1133 | rtIDs->fTexFBOID = 0; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1134 | |
bsalomon | a11e5fc | 2015-12-18 07:59:41 -0800 | [diff] [blame] | 1135 | GrGLenum colorRenderbufferFormat = 0; // suppress warning |
bsalomon@google.com | ab15d61 | 2011-08-09 12:57:56 +0000 | [diff] [blame] | 1136 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1137 | if (desc.fFormat == GrGLFormat::kUnknown) { |
Greg Daniel | 9bb268b | 2019-07-17 10:40:13 -0400 | [diff] [blame] | 1138 | goto FAILED; |
| 1139 | } |
| 1140 | |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1141 | if (sampleCount > 1 && GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType()) { |
bsalomon@google.com | 347c382 | 2013-05-01 20:10:01 +0000 | [diff] [blame] | 1142 | goto FAILED; |
| 1143 | } |
| 1144 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1145 | GL_CALL(GenFramebuffers(1, &rtIDs->fTexFBOID)); |
| 1146 | if (!rtIDs->fTexFBOID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1147 | goto FAILED; |
| 1148 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1149 | |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1150 | // If we are using multisampling we will create two FBOS. We render to one and then resolve to |
| 1151 | // the texture bound to the other. The exception is the IMG multisample extension. With this |
| 1152 | // extension the texture is multisampled when rendered to and then auto-resolves it when it is |
| 1153 | // rendered from. |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1154 | if (sampleCount > 1 && this->glCaps().usesMSAARenderBuffers()) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1155 | GL_CALL(GenFramebuffers(1, &rtIDs->fRTFBOID)); |
| 1156 | GL_CALL(GenRenderbuffers(1, &rtIDs->fMSColorRenderbufferID)); |
| 1157 | if (!rtIDs->fRTFBOID || !rtIDs->fMSColorRenderbufferID) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1158 | goto FAILED; |
| 1159 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1160 | colorRenderbufferFormat = this->glCaps().getRenderbufferInternalFormat(desc.fFormat); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1161 | } else { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1162 | rtIDs->fRTFBOID = rtIDs->fTexFBOID; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1165 | // below here we may bind the FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1166 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1167 | if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) { |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1168 | SkASSERT(sampleCount > 1); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1169 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rtIDs->fMSColorRenderbufferID)); |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1170 | if (!renderbuffer_storage_msaa(*fGLContext, sampleCount, colorRenderbufferFormat, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1171 | desc.fSize.width(), desc.fSize.height())) { |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1172 | goto FAILED; |
| 1173 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1174 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fRTFBOID); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1175 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 1176 | GR_GL_COLOR_ATTACHMENT0, |
| 1177 | GR_GL_RENDERBUFFER, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1178 | rtIDs->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1179 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1180 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, rtIDs->fTexFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1181 | |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1182 | if (this->glCaps().usesImplicitMSAAResolve() && sampleCount > 1) { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1183 | GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, |
| 1184 | GR_GL_COLOR_ATTACHMENT0, |
| 1185 | desc.fTarget, |
| 1186 | desc.fID, |
| 1187 | 0, |
| 1188 | sampleCount)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1189 | } else { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1190 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1191 | GR_GL_COLOR_ATTACHMENT0, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1192 | desc.fTarget, |
| 1193 | desc.fID, |
| 1194 | 0)); |
bsalomon@google.com | f3a60c0 | 2013-03-19 19:06:09 +0000 | [diff] [blame] | 1195 | } |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1196 | |
| 1197 | return true; |
| 1198 | |
| 1199 | FAILED: |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1200 | if (rtIDs->fMSColorRenderbufferID) { |
| 1201 | GL_CALL(DeleteRenderbuffers(1, &rtIDs->fMSColorRenderbufferID)); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1202 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1203 | if (rtIDs->fRTFBOID != rtIDs->fTexFBOID) { |
| 1204 | this->deleteFramebuffer(rtIDs->fRTFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1205 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1206 | if (rtIDs->fTexFBOID) { |
| 1207 | this->deleteFramebuffer(rtIDs->fTexFBOID); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1208 | } |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1212 | // good to set a break-point here to know when createTexture fails |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1213 | static sk_sp<GrTexture> return_null_texture() { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 1214 | // SkDEBUGFAIL("null texture"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1215 | return nullptr; |
bsalomon@google.com | 3f3ffd6 | 2011-01-18 17:14:52 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 1218 | static GrGLTextureParameters::SamplerOverriddenState set_initial_texture_params( |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1219 | const GrGLInterface* interface, GrGLenum target) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1220 | // Some drivers like to know filter/wrap before seeing glTexImage2D. Some |
| 1221 | // drivers have a bug where an FBO won't be complete if it includes a |
| 1222 | // texture that is not mipmap complete (considering the filter in use). |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 1223 | GrGLTextureParameters::SamplerOverriddenState state; |
| 1224 | state.fMinFilter = GR_GL_NEAREST; |
| 1225 | state.fMagFilter = GR_GL_NEAREST; |
| 1226 | state.fWrapS = GR_GL_CLAMP_TO_EDGE; |
| 1227 | state.fWrapT = GR_GL_CLAMP_TO_EDGE; |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1228 | GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, state.fMagFilter)); |
| 1229 | GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, state.fMinFilter)); |
| 1230 | GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_S, state.fWrapS)); |
| 1231 | GR_GL_CALL(interface, TexParameteri(target, GR_GL_TEXTURE_WRAP_T, state.fWrapT)); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 1232 | return state; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1235 | sk_sp<GrTexture> GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc, |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 1236 | const GrBackendFormat& format, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 1237 | GrRenderable renderable, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1238 | int renderTargetSampleCnt, |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1239 | SkBudgeted budgeted, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 1240 | GrProtected isProtected, |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1241 | int mipLevelCount, |
| 1242 | uint32_t levelClearMask) { |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 1243 | // We don't support protected textures in GL. |
| 1244 | if (isProtected == GrProtected::kYes) { |
| 1245 | return nullptr; |
| 1246 | } |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 1247 | SkASSERT(GrGLCaps::kNone_MSFBOType != this->glCaps().msFBOType() || renderTargetSampleCnt == 1); |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1248 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1249 | SkASSERT(mipLevelCount > 0); |
| 1250 | GrMipMapsStatus mipMapsStatus = |
| 1251 | mipLevelCount > 1 ? GrMipMapsStatus::kDirty : GrMipMapsStatus::kNotAllocated; |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 1252 | GrGLTextureParameters::SamplerOverriddenState initialState; |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1253 | GrGLTexture::Desc texDesc; |
| 1254 | texDesc.fSize = {desc.fWidth, desc.fHeight}; |
| 1255 | texDesc.fTarget = GR_GL_TEXTURE_2D; |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 1256 | texDesc.fFormat = format.asGLFormat(); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1257 | texDesc.fConfig = desc.fConfig; |
| 1258 | texDesc.fOwnership = GrBackendObjectOwnership::kOwned; |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 1259 | SkASSERT(texDesc.fFormat != GrGLFormat::kUnknown); |
| 1260 | SkASSERT(!GrGLFormatIsCompressed(texDesc.fFormat)); |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 1261 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1262 | texDesc.fID = this->createTexture2D({desc.fWidth, desc.fHeight}, texDesc.fFormat, renderable, |
| 1263 | &initialState, mipLevelCount); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1264 | |
| 1265 | if (!texDesc.fID) { |
bsalomon@google.com | b1d14fd | 2011-12-09 18:41:34 +0000 | [diff] [blame] | 1266 | return return_null_texture(); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 1267 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1268 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1269 | sk_sp<GrGLTexture> tex; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 1270 | if (renderable == GrRenderable::kYes) { |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1271 | // unbind the texture from the texture unit before binding it to the frame buffer |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1272 | GL_CALL(BindTexture(texDesc.fTarget, 0)); |
| 1273 | GrGLRenderTarget::IDs rtIDDesc; |
robertphillips@google.com | ba0cc3e | 2012-03-26 17:58:35 +0000 | [diff] [blame] | 1274 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1275 | if (!this->createRenderTargetObjects(texDesc, renderTargetSampleCnt, &rtIDDesc)) { |
| 1276 | GL_CALL(DeleteTextures(1, &texDesc.fID)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1277 | return return_null_texture(); |
| 1278 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1279 | tex = sk_make_sp<GrGLTextureRenderTarget>( |
| 1280 | this, budgeted, renderTargetSampleCnt, texDesc, rtIDDesc, mipMapsStatus); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 1281 | tex->baseLevelWasBoundToFBO(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1282 | } else { |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1283 | tex = sk_make_sp<GrGLTexture>(this, budgeted, texDesc, mipMapsStatus); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1284 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 1285 | // The non-sampler params are still at their default values. |
| 1286 | tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(), |
| 1287 | fResetTimestampForTextureParameters); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1288 | if (levelClearMask) { |
| 1289 | GrGLenum externalFormat, externalType; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 1290 | GrColorType colorType; |
| 1291 | this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(texDesc.fFormat, &externalFormat, |
| 1292 | &externalType, &colorType); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1293 | if (this->glCaps().clearTextureSupport()) { |
| 1294 | for (int i = 0; i < mipLevelCount; ++i) { |
| 1295 | if (levelClearMask & (1U << i)) { |
| 1296 | GL_CALL(ClearTexImage(tex->textureID(), i, externalFormat, externalType, |
| 1297 | nullptr)); |
| 1298 | } |
| 1299 | } |
| 1300 | } else if (this->glCaps().canFormatBeFBOColorAttachment(format.asGLFormat()) && |
| 1301 | !this->glCaps().performColorClearsAsDraws()) { |
| 1302 | this->disableScissor(); |
| 1303 | this->disableWindowRectangles(); |
| 1304 | this->flushColorWrite(true); |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1305 | this->flushClearColor(SK_PMColor4fTRANSPARENT); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1306 | for (int i = 0; i < mipLevelCount; ++i) { |
| 1307 | if (levelClearMask & (1U << i)) { |
| 1308 | this->bindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER, |
| 1309 | kDst_TempFBOTarget); |
| 1310 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
| 1311 | this->unbindSurfaceFBOForPixelOps(tex.get(), i, GR_GL_FRAMEBUFFER); |
| 1312 | } |
| 1313 | } |
Brian Salomon | c224985 | 2019-09-16 11:08:50 -0400 | [diff] [blame] | 1314 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1315 | } else { |
| 1316 | std::unique_ptr<char[]> zeros; |
| 1317 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 1318 | for (int i = 0; i < mipLevelCount; ++i) { |
| 1319 | if (levelClearMask & (1U << i)) { |
| 1320 | int levelWidth = SkTMax(1, texDesc.fSize.width() >> i); |
| 1321 | int levelHeight = SkTMax(1, texDesc.fSize.height() >> i); |
| 1322 | // Levels only get smaller as we proceed. Once we create a zeros use it for all |
| 1323 | // smaller levels that need clearing. |
| 1324 | if (!zeros) { |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 1325 | size_t bpp = GrColorTypeBytesPerPixel(colorType); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1326 | size_t size = levelWidth * levelHeight * bpp; |
| 1327 | zeros.reset(new char[size]()); |
| 1328 | } |
| 1329 | this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, tex->textureID()); |
| 1330 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelWidth, levelHeight, |
| 1331 | externalFormat, externalType, zeros.get())); |
| 1332 | } |
Brian Salomon | a3e2996 | 2019-07-16 11:52:08 -0400 | [diff] [blame] | 1333 | } |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1334 | } |
| 1335 | } |
Brian Salomon | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 1336 | return tex; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1339 | sk_sp<GrTexture> GrGLGpu::onCreateCompressedTexture(int width, int height, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1340 | const GrBackendFormat& format, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1341 | SkImage::CompressionType compression, |
| 1342 | SkBudgeted budgeted, const void* data) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1343 | GrGLTextureParameters::SamplerOverriddenState initialState; |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1344 | GrGLTexture::Desc desc; |
| 1345 | desc.fSize = {width, height}; |
| 1346 | desc.fTarget = GR_GL_TEXTURE_2D; |
Robert Phillips | a27d625 | 2019-12-10 14:48:36 -0500 | [diff] [blame] | 1347 | desc.fConfig = GrCompressionTypeToPixelConfig(compression); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1348 | desc.fOwnership = GrBackendObjectOwnership::kOwned; |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1349 | desc.fFormat = format.asGLFormat(); |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1350 | desc.fID = this->createCompressedTexture2D(desc.fSize, desc.fFormat, compression, |
| 1351 | GrMipMapped::kNo, &initialState, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1352 | data); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1353 | if (!desc.fID) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1354 | return nullptr; |
| 1355 | } |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 1356 | |
Robert Phillips | ee94693 | 2019-12-18 11:16:17 -0500 | [diff] [blame] | 1357 | // Unbind this texture from the scratch texture unit. |
| 1358 | this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0); |
| 1359 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1360 | auto tex = sk_make_sp<GrGLTexture>(this, budgeted, desc, GrMipMapsStatus::kNotAllocated); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1361 | // The non-sampler params are still at their default values. |
| 1362 | tex->parameters()->set(&initialState, GrGLTextureParameters::NonsamplerState(), |
| 1363 | fResetTimestampForTextureParameters); |
Brian Salomon | 9c73e3d | 2019-08-15 10:55:49 -0400 | [diff] [blame] | 1364 | return tex; |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 1367 | GrBackendTexture GrGLGpu::onCreateCompressedBackendTexture(SkISize dimensions, |
| 1368 | const GrBackendFormat& format, |
| 1369 | const BackendTextureData* data, |
| 1370 | GrMipMapped mipMapped, |
| 1371 | GrProtected isProtected) { |
| 1372 | return {}; |
| 1373 | } |
| 1374 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1375 | namespace { |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1376 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1377 | const GrGLuint kUnknownBitCount = GrGLStencilAttachment::kUnknownBitCount; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1378 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1379 | void inline get_stencil_rb_sizes(const GrGLInterface* gl, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1380 | GrGLStencilAttachment::Format* format) { |
sugoi@google.com | 6ba0b0f | 2013-05-03 13:52:32 +0000 | [diff] [blame] | 1381 | |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1382 | // we shouldn't ever know one size and not the other |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1383 | SkASSERT((kUnknownBitCount == format->fStencilBits) == |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1384 | (kUnknownBitCount == format->fTotalBits)); |
| 1385 | if (kUnknownBitCount == format->fStencilBits) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1386 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1387 | GR_GL_RENDERBUFFER_STENCIL_SIZE, |
| 1388 | (GrGLint*)&format->fStencilBits); |
| 1389 | if (format->fPacked) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1390 | GR_GL_GetRenderbufferParameteriv(gl, GR_GL_RENDERBUFFER, |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1391 | GR_GL_RENDERBUFFER_DEPTH_SIZE, |
| 1392 | (GrGLint*)&format->fTotalBits); |
| 1393 | format->fTotalBits += format->fStencilBits; |
| 1394 | } else { |
| 1395 | format->fTotalBits = format->fStencilBits; |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 1401 | int GrGLGpu::getCompatibleStencilIndex(GrGLFormat format) { |
bsalomon | 100b8f8 | 2015-10-28 08:37:44 -0700 | [diff] [blame] | 1402 | static const int kSize = 16; |
Brian Salomon | f6da146 | 2019-07-11 14:21:59 -0400 | [diff] [blame] | 1403 | SkASSERT(this->glCaps().canFormatBeFBOColorAttachment(format)); |
| 1404 | |
| 1405 | if (!this->glCaps().hasStencilFormatBeenDeterminedForFormat(format)) { |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1406 | // Default to unsupported, set this if we find a stencil format that works. |
| 1407 | int firstWorkingStencilFormatIndex = -1; |
Brian Osman | 91f9a2c | 2017-09-05 15:02:46 -0400 | [diff] [blame] | 1408 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1409 | GrGLuint colorID = |
| 1410 | this->createTexture2D({kSize, kSize}, format, GrRenderable::kYes, nullptr, 1); |
| 1411 | if (!colorID) { |
Brian Salomon | f6da146 | 2019-07-11 14:21:59 -0400 | [diff] [blame] | 1412 | return -1; |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 1413 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1414 | // unbind the texture from the texture unit before binding it to the frame buffer |
| 1415 | GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 1416 | |
| 1417 | // Create Framebuffer |
kkinnunen | 546eb5c | 2015-12-11 00:05:33 -0800 | [diff] [blame] | 1418 | GrGLuint fb = 0; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1419 | GL_CALL(GenFramebuffers(1, &fb)); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1420 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, fb); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 1421 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1422 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 1423 | GR_GL_COLOR_ATTACHMENT0, |
| 1424 | GR_GL_TEXTURE_2D, |
| 1425 | colorID, |
| 1426 | 0)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1427 | GrGLuint sbRBID = 0; |
| 1428 | GL_CALL(GenRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1429 | |
| 1430 | // look over formats till I find a compatible one |
| 1431 | int stencilFmtCnt = this->glCaps().stencilFormats().count(); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1432 | if (sbRBID) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1433 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1434 | for (int i = 0; i < stencilFmtCnt && sbRBID; ++i) { |
| 1435 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[i]; |
| 1436 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1437 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1438 | sFmt.fInternalFormat, |
| 1439 | kSize, kSize)); |
| 1440 | if (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1441 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1442 | GR_GL_STENCIL_ATTACHMENT, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1443 | GR_GL_RENDERBUFFER, sbRBID)); |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1444 | if (sFmt.fPacked) { |
| 1445 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1446 | GR_GL_DEPTH_ATTACHMENT, |
| 1447 | GR_GL_RENDERBUFFER, sbRBID)); |
| 1448 | } else { |
| 1449 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1450 | GR_GL_DEPTH_ATTACHMENT, |
| 1451 | GR_GL_RENDERBUFFER, 0)); |
| 1452 | } |
| 1453 | GrGLenum status; |
| 1454 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 1455 | if (status == GR_GL_FRAMEBUFFER_COMPLETE) { |
| 1456 | firstWorkingStencilFormatIndex = i; |
| 1457 | break; |
| 1458 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1459 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1460 | GR_GL_STENCIL_ATTACHMENT, |
| 1461 | GR_GL_RENDERBUFFER, 0)); |
| 1462 | if (sFmt.fPacked) { |
| 1463 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 1464 | GR_GL_DEPTH_ATTACHMENT, |
| 1465 | GR_GL_RENDERBUFFER, 0)); |
| 1466 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1467 | } |
| 1468 | } |
bsalomon | 3044737 | 2015-12-21 09:03:05 -0800 | [diff] [blame] | 1469 | GL_CALL(DeleteRenderbuffers(1, &sbRBID)); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1470 | } |
| 1471 | GL_CALL(DeleteTextures(1, &colorID)); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 1472 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0); |
| 1473 | this->deleteFramebuffer(fb); |
Brian Salomon | f6da146 | 2019-07-11 14:21:59 -0400 | [diff] [blame] | 1474 | fGLContext->caps()->setStencilFormatIndexForFormat(format, firstWorkingStencilFormatIndex); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1475 | } |
Brian Salomon | f6da146 | 2019-07-11 14:21:59 -0400 | [diff] [blame] | 1476 | return this->glCaps().getStencilFormatIndexForFormat(format); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1479 | GrGLuint GrGLGpu::createCompressedTexture2D( |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1480 | const SkISize& dimensions, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1481 | GrGLFormat format, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1482 | SkImage::CompressionType compression, |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1483 | GrMipMapped mipMapped, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1484 | GrGLTextureParameters::SamplerOverriddenState* initialState, |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 1485 | const void* data) { |
| 1486 | if (format == GrGLFormat::kUnknown) { |
| 1487 | return 0; |
| 1488 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1489 | GrGLuint id = 0; |
| 1490 | GL_CALL(GenTextures(1, &id)); |
| 1491 | if (!id) { |
| 1492 | return 0; |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1495 | this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id); |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 1496 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1497 | *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D); |
| 1498 | |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 1499 | if (data) { |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 1500 | if (!this->uploadCompressedTexData(format, compression, dimensions, mipMapped, |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 1501 | GR_GL_TEXTURE_2D, data)) { |
| 1502 | GL_CALL(DeleteTextures(1, &id)); |
| 1503 | return 0; |
| 1504 | } |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1505 | } |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 1506 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1507 | return id; |
| 1508 | } |
| 1509 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1510 | GrGLuint GrGLGpu::createTexture2D(const SkISize& dimensions, |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1511 | GrGLFormat format, |
| 1512 | GrRenderable renderable, |
| 1513 | GrGLTextureParameters::SamplerOverriddenState* initialState, |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1514 | int mipLevelCount) { |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 1515 | SkASSERT(format != GrGLFormat::kUnknown); |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1516 | SkASSERT(!GrGLFormatIsCompressed(format)); |
Brian Salomon | 81536f2 | 2019-08-08 16:30:49 -0400 | [diff] [blame] | 1517 | |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1518 | GrGLuint id = 0; |
| 1519 | GL_CALL(GenTextures(1, &id)); |
| 1520 | |
| 1521 | if (!id) { |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, id); |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1526 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 1527 | if (GrRenderable::kYes == renderable && this->glCaps().textureUsageSupport()) { |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1528 | // provides a hint about how this texture will be used |
Brian Salomon | ea4ad30 | 2019-08-07 13:04:55 -0400 | [diff] [blame] | 1529 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_USAGE, GR_GL_FRAMEBUFFER_ATTACHMENT)); |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 1530 | } |
| 1531 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1532 | if (initialState) { |
| 1533 | *initialState = set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D); |
| 1534 | } else { |
| 1535 | set_initial_texture_params(this->glInterface(), GR_GL_TEXTURE_2D); |
Brian Salomon | a739824 | 2019-09-10 09:17:38 -0400 | [diff] [blame] | 1536 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1537 | |
| 1538 | GrGLenum internalFormat = this->glCaps().getTexImageOrStorageInternalFormat(format); |
| 1539 | |
| 1540 | bool success = false; |
| 1541 | if (internalFormat) { |
| 1542 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1543 | if (this->glCaps().formatSupportsTexStorage(format)) { |
| 1544 | GL_ALLOC_CALL(this->glInterface(), |
| 1545 | TexStorage2D(GR_GL_TEXTURE_2D, SkTMax(mipLevelCount, 1), internalFormat, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1546 | dimensions.width(), dimensions.height())); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1547 | success = (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())); |
| 1548 | } else { |
| 1549 | GrGLenum externalFormat, externalType; |
| 1550 | this->glCaps().getTexImageExternalFormatAndType(format, &externalFormat, &externalType); |
| 1551 | GrGLenum error = GR_GL_NO_ERROR; |
| 1552 | if (externalFormat && externalType) { |
| 1553 | for (int level = 0; level < mipLevelCount && error == GR_GL_NO_ERROR; level++) { |
| 1554 | const int twoToTheMipLevel = 1 << level; |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 1555 | const int currentWidth = SkTMax(1, dimensions.width() / twoToTheMipLevel); |
| 1556 | const int currentHeight = SkTMax(1, dimensions.height() / twoToTheMipLevel); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 1557 | GL_ALLOC_CALL( |
| 1558 | this->glInterface(), |
| 1559 | TexImage2D(GR_GL_TEXTURE_2D, level, internalFormat, currentWidth, |
| 1560 | currentHeight, 0, externalFormat, externalType, nullptr)); |
| 1561 | error = CHECK_ALLOC_ERROR(this->glInterface()); |
| 1562 | } |
| 1563 | success = (GR_GL_NO_ERROR == error); |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | if (success) { |
| 1568 | return id; |
| 1569 | } |
| 1570 | GL_CALL(DeleteTextures(1, &id)); |
| 1571 | return 0; |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 1574 | GrStencilAttachment* GrGLGpu::createStencilAttachmentForRenderTarget( |
| 1575 | const GrRenderTarget* rt, int width, int height, int numStencilSamples) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1576 | SkASSERT(width >= rt->width()); |
| 1577 | SkASSERT(height >= rt->height()); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1578 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1579 | GrGLStencilAttachment::IDDesc sbDesc; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1580 | |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 1581 | int sIdx = this->getCompatibleStencilIndex(rt->backendFormat().asGLFormat()); |
bsalomon | 62a627b | 2015-12-17 09:50:47 -0800 | [diff] [blame] | 1582 | if (sIdx < 0) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1583 | return nullptr; |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1584 | } |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1585 | |
| 1586 | if (!sbDesc.fRenderbufferID) { |
| 1587 | GL_CALL(GenRenderbuffers(1, &sbDesc.fRenderbufferID)); |
| 1588 | } |
| 1589 | if (!sbDesc.fRenderbufferID) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1590 | return nullptr; |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1591 | } |
| 1592 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, sbDesc.fRenderbufferID)); |
| 1593 | const GrGLCaps::StencilFormat& sFmt = this->glCaps().stencilFormats()[sIdx]; |
| 1594 | CLEAR_ERROR_BEFORE_ALLOC(this->glInterface()); |
| 1595 | // we do this "if" so that we don't call the multisample |
| 1596 | // version on a GL that doesn't have an MSAA extension. |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 1597 | if (numStencilSamples > 1) { |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1598 | SkAssertResult(renderbuffer_storage_msaa(*fGLContext, |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 1599 | numStencilSamples, |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1600 | sFmt.fInternalFormat, |
| 1601 | width, height)); |
| 1602 | } else { |
| 1603 | GL_ALLOC_CALL(this->glInterface(), RenderbufferStorage(GR_GL_RENDERBUFFER, |
| 1604 | sFmt.fInternalFormat, |
| 1605 | width, height)); |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1606 | SkASSERT(GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(this->glInterface())); |
egdaniel | ff1d547 | 2015-09-10 08:37:20 -0700 | [diff] [blame] | 1607 | } |
| 1608 | fStats.incStencilAttachmentCreates(); |
| 1609 | // After sized formats we attempt an unsized format and take |
| 1610 | // whatever sizes GL gives us. In that case we query for the size. |
| 1611 | GrGLStencilAttachment::Format format = sFmt; |
| 1612 | get_stencil_rb_sizes(this->glInterface(), &format); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1613 | GrGLStencilAttachment* stencil = new GrGLStencilAttachment(this, |
| 1614 | sbDesc, |
| 1615 | width, |
| 1616 | height, |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 1617 | numStencilSamples, |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 1618 | format); |
| 1619 | return stencil; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
bsalomon@google.com | 71f341a | 2011-08-01 13:36:00 +0000 | [diff] [blame] | 1622 | //////////////////////////////////////////////////////////////////////////////// |
| 1623 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1624 | sk_sp<GrGpuBuffer> GrGLGpu::onCreateBuffer(size_t size, GrGpuBufferType intendedType, |
| 1625 | GrAccessPattern accessPattern, const void* data) { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1626 | return GrGLBuffer::Make(this, size, intendedType, accessPattern, data); |
jvanverth | 73063dc | 2015-12-03 09:15:47 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 1629 | void GrGLGpu::flushScissor(const GrScissorState& scissorState, int rtWidth, int rtHeight, |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1630 | GrSurfaceOrigin rtOrigin) { |
| 1631 | if (scissorState.enabled()) { |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 1632 | auto scissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissorState.rect()); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1633 | // if the scissor fully contains the viewport then we fall through and |
| 1634 | // disable the scissor test. |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 1635 | if (!scissor.contains(rtWidth, rtHeight)) { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1636 | if (fHWScissorSettings.fRect != scissor) { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 1637 | GL_CALL(Scissor(scissor.fX, scissor.fY, scissor.fWidth, scissor.fHeight)); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1638 | fHWScissorSettings.fRect = scissor; |
| 1639 | } |
| 1640 | if (kYes_TriState != fHWScissorSettings.fEnabled) { |
| 1641 | GL_CALL(Enable(GR_GL_SCISSOR_TEST)); |
| 1642 | fHWScissorSettings.fEnabled = kYes_TriState; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1643 | } |
| 1644 | return; |
| 1645 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1646 | } |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1647 | |
| 1648 | // See fall through note above |
| 1649 | this->disableScissor(); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1652 | void GrGLGpu::flushWindowRectangles(const GrWindowRectsState& windowState, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1653 | const GrGLRenderTarget* rt, GrSurfaceOrigin origin) { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1654 | #ifndef USE_NSIGHT |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1655 | typedef GrWindowRectsState::Mode Mode; |
| 1656 | SkASSERT(!windowState.enabled() || rt->renderFBOID()); // Window rects can't be used on-screen. |
| 1657 | SkASSERT(windowState.numWindows() <= this->caps()->maxWindowRectangles()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1658 | |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1659 | if (!this->caps()->maxWindowRectangles() || |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 1660 | fHWWindowRectsState.knownEqualTo(origin, rt->width(), rt->height(), windowState)) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1661 | return; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1664 | // This is purely a workaround for a spurious warning generated by gcc. Otherwise the above |
| 1665 | // assert would be sufficient. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5912 |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1666 | int numWindows = SkTMin(windowState.numWindows(), int(GrWindowRectangles::kMaxWindows)); |
| 1667 | SkASSERT(windowState.numWindows() == numWindows); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1668 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 1669 | GrNativeRect glwindows[GrWindowRectangles::kMaxWindows]; |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1670 | const SkIRect* skwindows = windowState.windows().data(); |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1671 | for (int i = 0; i < numWindows; ++i) { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 1672 | glwindows[i].setRelativeTo(origin, rt->height(), skwindows[i]); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1675 | GrGLenum glmode = (Mode::kExclusive == windowState.mode()) ? GR_GL_EXCLUSIVE : GR_GL_INCLUSIVE; |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 1676 | GL_CALL(WindowRectangles(glmode, numWindows, glwindows->asInts())); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1677 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 1678 | fHWWindowRectsState.set(origin, rt->width(), rt->height(), windowState); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1679 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | void GrGLGpu::disableWindowRectangles() { |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1683 | #ifndef USE_NSIGHT |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 1684 | if (!this->caps()->maxWindowRectangles() || fHWWindowRectsState.knownDisabled()) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1685 | return; |
| 1686 | } |
| 1687 | GL_CALL(WindowRectangles(GR_GL_EXCLUSIVE, 0, nullptr)); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 1688 | fHWWindowRectsState.setDisabled(); |
Jim Van Verth | 32ac83e | 2016-11-28 15:23:57 -0500 | [diff] [blame] | 1689 | #endif |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 1692 | bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) { |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 1693 | |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 1694 | sk_sp<GrGLProgram> program(fProgramCache->refProgram(this, renderTarget, programInfo)); |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 1695 | if (!program) { |
| 1696 | GrCapsDebugf(this->caps(), "Failed to create program!\n"); |
| 1697 | return false; |
| 1698 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 1699 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 1700 | this->flushProgram(std::move(program)); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1701 | |
Dongseong Hwang | 4e1f022 | 2018-11-01 09:10:51 -0700 | [diff] [blame] | 1702 | // Swizzle the blend to match what the shader will output. |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 1703 | this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(), |
| 1704 | programInfo.pipeline().outputSwizzle()); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1705 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 1706 | fHWProgram->updateUniformsAndTextureBindings(renderTarget, programInfo); |
bsalomon | 1f78c0a | 2014-12-17 09:43:13 -0800 | [diff] [blame] | 1707 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 1708 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget); |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 1709 | GrStencilSettings stencil; |
| 1710 | if (programInfo.pipeline().isStencilEnabled()) { |
Robert Phillips | 6c2aa7a | 2019-10-17 19:06:39 +0000 | [diff] [blame] | 1711 | SkASSERT(glRT->renderTargetPriv().getStencilAttachment()); |
| 1712 | stencil.reset(*programInfo.pipeline().getUserStencil(), |
| 1713 | programInfo.pipeline().hasStencilClip(), |
| 1714 | glRT->renderTargetPriv().numStencilBits()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 1715 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 1716 | this->flushStencil(stencil, programInfo.origin()); |
| 1717 | if (programInfo.pipeline().isScissorEnabled()) { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1718 | static constexpr SkIRect kBogusScissor{0, 0, 1, 1}; |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 1719 | GrScissorState state(programInfo.fixedDynamicState() ? programInfo.fixedScissor() |
| 1720 | : kBogusScissor); |
| 1721 | this->flushScissor(state, glRT->width(), glRT->height(), programInfo.origin()); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1722 | } else { |
| 1723 | this->disableScissor(); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 1724 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 1725 | this->flushWindowRectangles(programInfo.pipeline().getWindowRectsState(), |
| 1726 | glRT, programInfo.origin()); |
| 1727 | this->flushHWAAState(glRT, programInfo.pipeline().isHWAntialiasState()); |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 1728 | this->flushConservativeRasterState(programInfo.pipeline().usesConservativeRaster()); |
Chris Dalton | 1215cda | 2019-12-17 21:44:04 -0700 | [diff] [blame] | 1729 | this->flushWireframeState(programInfo.pipeline().isWireframe()); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1730 | |
| 1731 | // This must come after textures are flushed because a texture may need |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 1732 | // to be msaa-resolved (which will modify bound FBO state). |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 1733 | this->flushRenderTarget(glRT); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1734 | |
| 1735 | return true; |
| 1736 | } |
| 1737 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 1738 | void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) { |
| 1739 | if (!program) { |
| 1740 | fHWProgram.reset(); |
| 1741 | fHWProgramID = 0; |
| 1742 | return; |
| 1743 | } |
| 1744 | SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID())); |
| 1745 | if (program == fHWProgram) { |
| 1746 | return; |
| 1747 | } |
| 1748 | auto id = program->programID(); |
| 1749 | SkASSERT(id); |
| 1750 | GL_CALL(UseProgram(id)); |
| 1751 | fHWProgram = std::move(program); |
| 1752 | fHWProgramID = id; |
| 1753 | } |
| 1754 | |
| 1755 | void GrGLGpu::flushProgram(GrGLuint id) { |
| 1756 | SkASSERT(id); |
| 1757 | if (fHWProgramID == id) { |
| 1758 | SkASSERT(!fHWProgram); |
| 1759 | return; |
| 1760 | } |
| 1761 | fHWProgram.reset(); |
| 1762 | GL_CALL(UseProgram(id)); |
| 1763 | fHWProgramID = id; |
| 1764 | } |
| 1765 | |
| 1766 | void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer, |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1767 | const GrBuffer* vertexBuffer, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1768 | int baseVertex, |
| 1769 | const GrBuffer* instanceBuffer, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 1770 | int baseInstance, |
| 1771 | GrPrimitiveRestart enablePrimitiveRestart) { |
| 1772 | SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 1773 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1774 | GrGLAttribArrayState* attribState; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1775 | if (indexBuffer) { |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1776 | SkASSERT(indexBuffer->isCpuBuffer() || |
| 1777 | !static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped()); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 1778 | attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1779 | } else { |
| 1780 | attribState = fHWVertexArrayState.bindInternalVertexArray(this); |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1781 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1782 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1783 | int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes(); |
| 1784 | attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 1785 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 1786 | if (int vertexStride = fHWProgram->vertexStride()) { |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1787 | SkASSERT(vertexBuffer); |
| 1788 | SkASSERT(vertexBuffer->isCpuBuffer() || |
| 1789 | !static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped()); |
| 1790 | size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1791 | for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) { |
| 1792 | const auto& attrib = fHWProgram->vertexAttribute(i); |
| 1793 | static constexpr int kDivisor = 0; |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 1794 | attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType, |
| 1795 | vertexStride, bufferOffset + attrib.fOffset, kDivisor); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1796 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 1797 | } |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 1798 | if (int instanceStride = fHWProgram->instanceStride()) { |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1799 | SkASSERT(instanceBuffer); |
| 1800 | SkASSERT(instanceBuffer->isCpuBuffer() || |
| 1801 | !static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped()); |
| 1802 | size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1803 | int attribIdx = fHWProgram->numVertexAttributes(); |
| 1804 | for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) { |
| 1805 | const auto& attrib = fHWProgram->instanceAttribute(i); |
| 1806 | static constexpr int kDivisor = 1; |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 1807 | attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType, |
| 1808 | attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset, |
| 1809 | kDivisor); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1810 | } |
bsalomon | bc3d0de | 2014-12-15 13:45:03 -0800 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1814 | GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) { |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1815 | this->handleDirtyContext(); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1816 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1817 | // Index buffer state is tied to the vertex array. |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1818 | if (GrGpuBufferType::kIndex == type) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1819 | this->bindVertexArray(0); |
cdalton | deacc97 | 2016-04-06 14:26:33 -0700 | [diff] [blame] | 1820 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1821 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1822 | auto* bufferState = this->hwBufferState(type); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1823 | if (buffer->isCpuBuffer()) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1824 | if (!bufferState->fBufferZeroKnownBound) { |
| 1825 | GL_CALL(BindBuffer(bufferState->fGLTarget, 0)); |
| 1826 | bufferState->fBufferZeroKnownBound = true; |
| 1827 | bufferState->fBoundBufferUniqueID.makeInvalid(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1828 | } |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 1829 | } else if (static_cast<const GrGpuBuffer*>(buffer)->uniqueID() != |
| 1830 | bufferState->fBoundBufferUniqueID) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1831 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(buffer); |
| 1832 | GL_CALL(BindBuffer(bufferState->fGLTarget, glBuffer->bufferID())); |
| 1833 | bufferState->fBufferZeroKnownBound = false; |
| 1834 | bufferState->fBoundBufferUniqueID = glBuffer->uniqueID(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 1837 | return bufferState->fGLTarget; |
joshualitt | 93316b9 | 2015-10-23 09:08:08 -0700 | [diff] [blame] | 1838 | } |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1839 | void GrGLGpu::disableScissor() { |
| 1840 | if (kNo_TriState != fHWScissorSettings.fEnabled) { |
| 1841 | GL_CALL(Disable(GR_GL_SCISSOR_TEST)); |
| 1842 | fHWScissorSettings.fEnabled = kNo_TriState; |
| 1843 | return; |
| 1844 | } |
| 1845 | } |
| 1846 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 1847 | void GrGLGpu::clear(const GrFixedClip& clip, const SkPMColor4f& color, |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 1848 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1849 | // parent class should never let us get here with no RT |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1850 | SkASSERT(target); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 1851 | SkASSERT(!this->caps()->performColorClearsAsDraws()); |
| 1852 | SkASSERT(!clip.scissorEnabled() || !this->caps()->performPartialClearsAsDraws()); |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 1853 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1854 | this->handleDirtyContext(); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1855 | |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1856 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
| 1857 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1858 | if (clip.scissorEnabled()) { |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 1859 | this->flushRenderTarget(glRT, origin, clip.scissorRect()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1860 | } else { |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 1861 | this->flushRenderTarget(glRT); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1862 | } |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 1863 | this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin); |
Brian Salomon | 43f8bf0 | 2017-10-18 08:33:29 -0400 | [diff] [blame] | 1864 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
Brian Salomon | 805cc7a | 2019-01-28 09:52:34 -0500 | [diff] [blame] | 1865 | this->flushColorWrite(true); |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1866 | this->flushClearColor(color); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1867 | GL_CALL(Clear(GR_GL_COLOR_BUFFER_BIT)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 1870 | void GrGLGpu::clearStencil(GrRenderTarget* target, int clearValue) { |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 1871 | SkASSERT(!this->caps()->performStencilClearsAsDraws()); |
| 1872 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 1873 | if (!target) { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 1874 | return; |
| 1875 | } |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 1876 | |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1877 | // This should only be called internally when we know we have a stencil buffer. |
| 1878 | SkASSERT(target->renderTargetPriv().getStencilAttachment()); |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 1879 | |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 1880 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1881 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 1882 | |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 1883 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1884 | this->disableWindowRectangles(); |
robertphillips@google.com | 730ebe5 | 2012-04-16 16:33:13 +0000 | [diff] [blame] | 1885 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1886 | GL_CALL(StencilMask(0xffffffff)); |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 1887 | GL_CALL(ClearStencil(clearValue)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1888 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 1889 | fHWStencilSettings.invalidate(); |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1890 | } |
| 1891 | |
Chris Dalton | b50cc81 | 2019-10-14 16:29:21 -0600 | [diff] [blame] | 1892 | static bool use_tiled_rendering(const GrGLCaps& glCaps, |
| 1893 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) { |
| 1894 | // Only use the tiled rendering extension if we can explicitly clear and discard the stencil. |
| 1895 | // Otherwise it's faster to just not use it. |
| 1896 | return glCaps.tiledRenderingSupport() && GrLoadOp::kClear == stencilLoadStore.fLoadOp && |
| 1897 | GrStoreOp::kDiscard == stencilLoadStore.fStoreOp; |
| 1898 | } |
| 1899 | |
| 1900 | void GrGLGpu::beginCommandBuffer(GrRenderTarget* rt, const SkIRect& bounds, GrSurfaceOrigin origin, |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1901 | const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore, |
| 1902 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) { |
| 1903 | SkASSERT(!fIsExecutingCommandBuffer_DebugOnly); |
| 1904 | |
| 1905 | this->handleDirtyContext(); |
| 1906 | |
| 1907 | auto glRT = static_cast<GrGLRenderTarget*>(rt); |
| 1908 | this->flushRenderTarget(glRT); |
| 1909 | SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = true); |
| 1910 | |
Chris Dalton | b50cc81 | 2019-10-14 16:29:21 -0600 | [diff] [blame] | 1911 | if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) { |
| 1912 | auto nativeBounds = GrNativeRect::MakeRelativeTo(origin, glRT->height(), bounds); |
| 1913 | GrGLbitfield preserveMask = (GrLoadOp::kLoad == colorLoadStore.fLoadOp) |
| 1914 | ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE; |
| 1915 | SkASSERT(GrLoadOp::kLoad != stencilLoadStore.fLoadOp); // Handled by use_tiled_rendering(). |
| 1916 | GL_CALL(StartTiling(nativeBounds.fX, nativeBounds.fY, nativeBounds.fWidth, |
| 1917 | nativeBounds.fHeight, preserveMask)); |
| 1918 | } |
| 1919 | |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1920 | GrGLbitfield clearMask = 0; |
| 1921 | if (GrLoadOp::kClear == colorLoadStore.fLoadOp) { |
| 1922 | SkASSERT(!this->caps()->performColorClearsAsDraws()); |
| 1923 | this->flushClearColor(colorLoadStore.fClearColor); |
| 1924 | this->flushColorWrite(true); |
| 1925 | clearMask |= GR_GL_COLOR_BUFFER_BIT; |
Robert Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 1926 | } |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1927 | if (GrLoadOp::kClear == stencilLoadStore.fLoadOp) { |
| 1928 | SkASSERT(!this->caps()->performStencilClearsAsDraws()); |
| 1929 | GL_CALL(StencilMask(0xffffffff)); |
| 1930 | GL_CALL(ClearStencil(0)); |
| 1931 | clearMask |= GR_GL_STENCIL_BUFFER_BIT; |
| 1932 | } |
| 1933 | if (clearMask) { |
| 1934 | this->disableScissor(); |
| 1935 | this->disableWindowRectangles(); |
| 1936 | GL_CALL(Clear(clearMask)); |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | void GrGLGpu::endCommandBuffer(GrRenderTarget* rt, |
| 1941 | const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore, |
| 1942 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore) { |
| 1943 | SkASSERT(fIsExecutingCommandBuffer_DebugOnly); |
| 1944 | |
| 1945 | this->handleDirtyContext(); |
| 1946 | |
| 1947 | if (rt->uniqueID() != fHWBoundRenderTargetUniqueID) { |
| 1948 | // The framebuffer binding changed in the middle of a command buffer. We should have already |
| 1949 | // printed a warning during onFBOChanged. |
| 1950 | return; |
| 1951 | } |
| 1952 | |
| 1953 | if (GrGLCaps::kNone_InvalidateFBType != this->glCaps().invalidateFBType()) { |
| 1954 | auto glRT = static_cast<GrGLRenderTarget*>(rt); |
| 1955 | |
| 1956 | SkSTArray<2, GrGLenum> discardAttachments; |
| 1957 | if (GrStoreOp::kDiscard == colorLoadStore.fStoreOp) { |
| 1958 | discardAttachments.push_back( |
| 1959 | (0 == glRT->renderFBOID()) ? GR_GL_COLOR : GR_GL_COLOR_ATTACHMENT0); |
| 1960 | } |
| 1961 | if (GrStoreOp::kDiscard == stencilLoadStore.fStoreOp) { |
| 1962 | discardAttachments.push_back( |
| 1963 | (0 == glRT->renderFBOID()) ? GR_GL_STENCIL : GR_GL_STENCIL_ATTACHMENT); |
| 1964 | } |
| 1965 | |
| 1966 | if (!discardAttachments.empty()) { |
| 1967 | if (GrGLCaps::kInvalidate_InvalidateFBType == this->glCaps().invalidateFBType()) { |
| 1968 | GL_CALL(InvalidateFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(), |
| 1969 | discardAttachments.begin())); |
| 1970 | } else { |
| 1971 | SkASSERT(GrGLCaps::kDiscard_InvalidateFBType == this->glCaps().invalidateFBType()); |
| 1972 | GL_CALL(DiscardFramebuffer(GR_GL_FRAMEBUFFER, discardAttachments.count(), |
| 1973 | discardAttachments.begin())); |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | |
Chris Dalton | b50cc81 | 2019-10-14 16:29:21 -0600 | [diff] [blame] | 1978 | if (use_tiled_rendering(this->glCaps(), stencilLoadStore)) { |
| 1979 | GrGLbitfield preserveMask = (GrStoreOp::kStore == colorLoadStore.fStoreOp) |
| 1980 | ? GR_GL_COLOR_BUFFER_BIT0 : GR_GL_NONE; |
| 1981 | // Handled by use_tiled_rendering(). |
| 1982 | SkASSERT(GrStoreOp::kStore != stencilLoadStore.fStoreOp); |
| 1983 | GL_CALL(EndTiling(preserveMask)); |
| 1984 | } |
| 1985 | |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 1986 | SkDEBUGCODE(fIsExecutingCommandBuffer_DebugOnly = false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 1989 | void GrGLGpu::clearStencilClip(const GrFixedClip& clip, |
| 1990 | bool insideStencilMask, |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 1991 | GrRenderTarget* target, GrSurfaceOrigin origin) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1992 | SkASSERT(target); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 1993 | SkASSERT(!this->caps()->performStencilClearsAsDraws()); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1994 | this->handleDirtyContext(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1995 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1996 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 1997 | // this should only be called internally when we know we have a |
| 1998 | // stencil buffer. |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 1999 | SkASSERT(sb); |
| 2000 | GrGLint stencilBitCount = sb->bits(); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2001 | #if 0 |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 2002 | SkASSERT(stencilBitCount > 0); |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2003 | GrGLint clipStencilMask = (1 << (stencilBitCount - 1)); |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2004 | #else |
| 2005 | // we could just clear the clip bit but when we go through |
bsalomon@google.com | 81c3f8d | 2011-08-03 15:18:33 +0000 | [diff] [blame] | 2006 | // ANGLE a partial stencil mask will cause clears to be |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 2007 | // turned into draws. Our contract on GrOpsTask says that |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2008 | // changing the clip between stencil passes may or may not |
| 2009 | // zero the client's clip bits. So we just clear the whole thing. |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 2010 | static const GrGLint clipStencilMask = ~0; |
bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 2011 | #endif |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2012 | GrGLint value; |
csmartdalton | 29df760 | 2016-08-31 11:55:52 -0700 | [diff] [blame] | 2013 | if (insideStencilMask) { |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2014 | value = (1 << (stencilBitCount - 1)); |
| 2015 | } else { |
| 2016 | value = 0; |
| 2017 | } |
bsalomon | b0bd4f6 | 2014-09-03 07:19:50 -0700 | [diff] [blame] | 2018 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(target); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2019 | this->flushRenderTargetNoColorWrites(glRT); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2020 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 2021 | this->flushScissor(clip.scissorState(), glRT->width(), glRT->height(), origin); |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2022 | this->flushWindowRectangles(clip.windowRectsState(), glRT, origin); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2023 | |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 2024 | GL_CALL(StencilMask((uint32_t) clipStencilMask)); |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 2025 | GL_CALL(ClearStencil(value)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2026 | GL_CALL(Clear(GR_GL_STENCIL_BUFFER_BIT)); |
bsalomon@google.com | 457b8a3 | 2012-05-21 21:19:58 +0000 | [diff] [blame] | 2027 | fHWStencilSettings.invalidate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2030 | bool GrGLGpu::readOrTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 2031 | GrColorType surfaceColorType, GrColorType dstColorType, |
| 2032 | void* offsetOrPtr, int rowWidthInPixels) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2033 | SkASSERT(surface); |
bsalomon | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame] | 2034 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2035 | auto format = surface->backendFormat().asGLFormat(); |
bsalomon | e957331 | 2016-01-25 14:33:25 -0800 | [diff] [blame] | 2036 | GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2037 | if (!renderTarget && !this->glCaps().isFormatRenderable(format, 1)) { |
bsalomon | 6cb3cbe | 2015-07-30 07:34:27 -0700 | [diff] [blame] | 2038 | return false; |
| 2039 | } |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 2040 | GrGLenum externalFormat = 0; |
| 2041 | GrGLenum externalType = 0; |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 2042 | this->glCaps().getReadPixelsFormat(surface->backendFormat().asGLFormat(), |
| 2043 | surfaceColorType, |
| 2044 | dstColorType, |
| 2045 | &externalFormat, |
| 2046 | &externalType); |
Greg Daniel | ba88ab6 | 2019-07-26 09:14:01 -0400 | [diff] [blame] | 2047 | if (!externalFormat || !externalType) { |
bsalomon | 76148af | 2016-01-12 11:13:47 -0800 | [diff] [blame] | 2048 | return false; |
| 2049 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 2050 | |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2051 | if (renderTarget) { |
Chris Dalton | c139d08 | 2019-09-26 14:04:24 -0600 | [diff] [blame] | 2052 | if (renderTarget->numSamples() <= 1 || |
| 2053 | renderTarget->renderFBOID() == renderTarget->textureFBOID()) { // Also catches FBO 0. |
| 2054 | SkASSERT(!renderTarget->requiresManualMSAAResolve()); |
| 2055 | this->flushRenderTargetNoColorWrites(renderTarget); |
| 2056 | } else if (GrGLRenderTarget::kUnresolvableFBOID == renderTarget->textureFBOID()) { |
| 2057 | SkASSERT(!renderTarget->requiresManualMSAAResolve()); |
| 2058 | return false; |
| 2059 | } else { |
| 2060 | SkASSERT(renderTarget->requiresManualMSAAResolve()); |
| 2061 | // we don't track the state of the READ FBO ID. |
| 2062 | this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()); |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2063 | } |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2064 | } else { |
| 2065 | // Use a temporary FBO. |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2066 | this->bindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2067 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 2070 | // the read rect is viewport-relative |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 2071 | GrNativeRect readRect = {left, top, width, height}; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2072 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 2073 | // determine if GL can read using the passed rowBytes or if we need a scratch buffer. |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 2074 | if (rowWidthInPixels != width) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 2075 | SkASSERT(this->glCaps().readPixelsRowBytesSupport()); |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 2076 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, rowWidthInPixels)); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2077 | } |
Brian Salomon | 8cbf662 | 2019-07-30 11:03:14 -0400 | [diff] [blame] | 2078 | GL_CALL(PixelStorei(GR_GL_PACK_ALIGNMENT, 1)); |
bsalomon | f46a124 | 2015-12-15 12:37:38 -0800 | [diff] [blame] | 2079 | |
Brian Osman | 1348ed0 | 2018-09-12 09:08:39 -0400 | [diff] [blame] | 2080 | bool reattachStencil = false; |
| 2081 | if (this->glCaps().detachStencilFromMSAABuffersBeforeReadPixels() && |
| 2082 | renderTarget && |
| 2083 | renderTarget->renderTargetPriv().getStencilAttachment() && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2084 | renderTarget->numSamples() > 1) { |
Brian Osman | 1348ed0 | 2018-09-12 09:08:39 -0400 | [diff] [blame] | 2085 | // Fix Adreno devices that won't read from MSAA framebuffers with stencil attached |
| 2086 | reattachStencil = true; |
| 2087 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, |
| 2088 | GR_GL_RENDERBUFFER, 0)); |
| 2089 | } |
| 2090 | |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 2091 | GL_CALL(ReadPixels(readRect.fX, readRect.fY, readRect.fWidth, readRect.fHeight, |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2092 | externalFormat, externalType, offsetOrPtr)); |
Brian Osman | 1348ed0 | 2018-09-12 09:08:39 -0400 | [diff] [blame] | 2093 | |
| 2094 | if (reattachStencil) { |
| 2095 | GrGLStencilAttachment* stencilAttachment = static_cast<GrGLStencilAttachment*>( |
| 2096 | renderTarget->renderTargetPriv().getStencilAttachment()); |
| 2097 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, |
| 2098 | GR_GL_RENDERBUFFER, stencilAttachment->renderbufferID())); |
| 2099 | } |
| 2100 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 2101 | if (rowWidthInPixels != width) { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 2102 | SkASSERT(this->glCaps().readPixelsRowBytesSupport()); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 2103 | GL_CALL(PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); |
| 2104 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2105 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2106 | if (!renderTarget) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2107 | this->unbindSurfaceFBOForPixelOps(surface, 0, GR_GL_FRAMEBUFFER); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2108 | } |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
| 2112 | bool GrGLGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height, |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 2113 | GrColorType surfaceColorType, GrColorType dstColorType, void* buffer, |
| 2114 | size_t rowBytes) { |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2115 | SkASSERT(surface); |
| 2116 | |
Brian Salomon | b28cb68 | 2019-07-26 12:48:47 -0400 | [diff] [blame] | 2117 | size_t bytesPerPixel = GrColorTypeBytesPerPixel(dstColorType); |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2118 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 2119 | // GL_PACK_ROW_LENGTH is in terms of pixels not bytes. |
| 2120 | int rowPixelWidth; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2121 | |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 2122 | if (rowBytes == SkToSizeT(width * bytesPerPixel)) { |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 2123 | rowPixelWidth = width; |
| 2124 | } else { |
Brian Salomon | 1047a49 | 2019-07-02 12:25:21 -0400 | [diff] [blame] | 2125 | SkASSERT(!(rowBytes % bytesPerPixel)); |
| 2126 | rowPixelWidth = rowBytes / bytesPerPixel; |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 2127 | } |
Brian Salomon | f77c146 | 2019-08-01 15:19:29 -0400 | [diff] [blame] | 2128 | return this->readOrTransferPixelsFrom(surface, left, top, width, height, surfaceColorType, |
| 2129 | dstColorType, buffer, rowPixelWidth); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 2132 | GrOpsRenderPass* GrGLGpu::getOpsRenderPass( |
Greg Daniel | 4a0d36d | 2019-09-30 12:24:36 -0400 | [diff] [blame] | 2133 | GrRenderTarget* rt, GrSurfaceOrigin origin, const SkIRect& bounds, |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 2134 | const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 2135 | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo, |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 2136 | const SkTArray<GrSurfaceProxy*, true>& sampledProxies) { |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 2137 | if (!fCachedOpsRenderPass) { |
| 2138 | fCachedOpsRenderPass.reset(new GrGLOpsRenderPass(this)); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 2139 | } |
| 2140 | |
Chris Dalton | b50cc81 | 2019-10-14 16:29:21 -0600 | [diff] [blame] | 2141 | fCachedOpsRenderPass->set(rt, bounds, origin, colorInfo, stencilInfo); |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 2142 | return fCachedOpsRenderPass.get(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2145 | void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target, GrSurfaceOrigin origin, |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 2146 | const SkIRect& bounds) { |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 2147 | this->flushRenderTargetNoColorWrites(target); |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 2148 | this->didWriteToSurface(target, origin, &bounds); |
| 2149 | } |
bsalomon | 6ba6fa1 | 2015-03-04 11:57:37 -0800 | [diff] [blame] | 2150 | |
Chris Dalton | c8ece3d | 2018-07-30 15:03:45 -0600 | [diff] [blame] | 2151 | void GrGLGpu::flushRenderTarget(GrGLRenderTarget* target) { |
| 2152 | this->flushRenderTargetNoColorWrites(target); |
| 2153 | this->didWriteToSurface(target, kTopLeft_GrSurfaceOrigin, nullptr); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2154 | } |
| 2155 | |
Brian Osman | 9aa30c6 | 2018-07-02 15:21:46 -0400 | [diff] [blame] | 2156 | void GrGLGpu::flushRenderTargetNoColorWrites(GrGLRenderTarget* target) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2157 | SkASSERT(target); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2158 | GrGpuResource::UniqueID rtID = target->uniqueID(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2159 | if (fHWBoundRenderTargetUniqueID != rtID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2160 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, target->renderFBOID()); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2161 | #ifdef SK_DEBUG |
| 2162 | // don't do this check in Chromium -- this is causing |
| 2163 | // lots of repeated command buffer flushes when the compositor is |
| 2164 | // rendering with Ganesh, which is really slow; even too slow for |
| 2165 | // Debug mode. |
cdalton | 1acea86 | 2015-06-02 13:05:52 -0700 | [diff] [blame] | 2166 | if (kChromium_GrGLDriver != this->glContext().driver()) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2167 | GrGLenum status; |
| 2168 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 2169 | if (status != GR_GL_FRAMEBUFFER_COMPLETE) { |
| 2170 | SkDebugf("GrGLGpu::flushRenderTarget glCheckFramebufferStatus %x\n", status); |
| 2171 | } |
bsalomon | 160f24c | 2015-03-17 15:55:42 -0700 | [diff] [blame] | 2172 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2173 | #endif |
| 2174 | fHWBoundRenderTargetUniqueID = rtID; |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 2175 | this->flushViewport(target->width(), target->height()); |
brianosman | 64d094d | 2016-03-25 06:01:59 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
brianosman | 35b784d | 2016-05-05 11:52:53 -0700 | [diff] [blame] | 2178 | if (this->glCaps().srgbWriteControl()) { |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2179 | this->flushFramebufferSRGB(this->caps()->isFormatSRGB(target->backendFormat())); |
bsalomon | 5cd020f | 2015-03-17 12:46:56 -0700 | [diff] [blame] | 2180 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2181 | } |
bsalomon | a990912 | 2016-01-23 10:41:40 -0800 | [diff] [blame] | 2182 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2183 | void GrGLGpu::flushFramebufferSRGB(bool enable) { |
| 2184 | if (enable && kYes_TriState != fHWSRGBFramebuffer) { |
| 2185 | GL_CALL(Enable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2186 | fHWSRGBFramebuffer = kYes_TriState; |
| 2187 | } else if (!enable && kNo_TriState != fHWSRGBFramebuffer) { |
| 2188 | GL_CALL(Disable(GR_GL_FRAMEBUFFER_SRGB)); |
| 2189 | fHWSRGBFramebuffer = kNo_TriState; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 2193 | void GrGLGpu::flushViewport(int width, int height) { |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 2194 | GrNativeRect viewport = {0, 0, width, height}; |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2195 | if (fHWViewport != viewport) { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 2196 | GL_CALL(Viewport(viewport.fX, viewport.fY, viewport.fWidth, viewport.fHeight)); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2197 | fHWViewport = viewport; |
| 2198 | } |
| 2199 | } |
| 2200 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2201 | #define SWAP_PER_DRAW 0 |
| 2202 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 2203 | #if SWAP_PER_DRAW |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2204 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2205 | #include <AGL/agl.h> |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2206 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | ce7357d | 2012-06-25 17:49:25 +0000 | [diff] [blame] | 2207 | #include <gl/GL.h> |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2208 | void SwapBuf() { |
| 2209 | DWORD procID = GetCurrentProcessId(); |
| 2210 | HWND hwnd = GetTopWindow(GetDesktopWindow()); |
| 2211 | while(hwnd) { |
| 2212 | DWORD wndProcID = 0; |
| 2213 | GetWindowThreadProcessId(hwnd, &wndProcID); |
| 2214 | if(wndProcID == procID) { |
| 2215 | SwapBuffers(GetDC(hwnd)); |
| 2216 | } |
| 2217 | hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); |
| 2218 | } |
| 2219 | } |
| 2220 | #endif |
| 2221 | #endif |
| 2222 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2223 | void GrGLGpu::draw(GrRenderTarget* renderTarget, |
| 2224 | const GrProgramInfo& programInfo, |
bsalomon | 2eda5b3 | 2016-09-21 10:53:24 -0700 | [diff] [blame] | 2225 | const GrMesh meshes[], |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2226 | int meshCount) { |
| 2227 | this->handleDirtyContext(); |
| 2228 | |
Robert Phillips | 2579de4 | 2019-10-09 09:51:59 -0400 | [diff] [blame] | 2229 | SkASSERT(meshCount); // guaranteed by GrOpsRenderPass::draw |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2230 | |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 2231 | if (!this->flushGLState(renderTarget, programInfo)) { |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 2232 | return; |
| 2233 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2234 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2235 | bool hasDynamicScissors = programInfo.hasDynamicScissors(); |
| 2236 | bool hasDynamicPrimProcTextures = programInfo.hasDynamicPrimProcTextures(); |
| 2237 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 2238 | for (int m = 0; m < meshCount; ++m) { |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 2239 | SkASSERT(meshes[m].primitiveType() == programInfo.primitiveType()); |
Robert Phillips | 6e54a29 | 2019-11-05 15:42:16 -0500 | [diff] [blame] | 2240 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2241 | if (auto barrierType = programInfo.pipeline().xferBarrierType(renderTarget->asTexture(), |
| 2242 | *this->caps())) { |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 2243 | this->xferBarrier(renderTarget, barrierType); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 2244 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2245 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2246 | if (hasDynamicScissors) { |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 2247 | GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2248 | this->flushScissor(GrScissorState(programInfo.dynamicScissor(m)), |
| 2249 | glRT->width(), glRT->height(), programInfo.origin()); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 2250 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 2251 | if (hasDynamicPrimProcTextures) { |
| 2252 | auto texProxyArray = programInfo.dynamicPrimProcTextures(m); |
| 2253 | fHWProgram->updatePrimitiveProcessorTextureBindings(programInfo.primProc(), |
| 2254 | texProxyArray); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 2255 | } |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2256 | if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() && |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 2257 | GrIsPrimTypeLines(meshes[m].primitiveType()) && |
Brian Salomon | 6d9c88b | 2017-06-12 10:24:42 -0400 | [diff] [blame] | 2258 | !GrIsPrimTypeLines(fLastPrimitiveType)) { |
| 2259 | GL_CALL(Enable(GR_GL_CULL_FACE)); |
| 2260 | GL_CALL(Disable(GR_GL_CULL_FACE)); |
| 2261 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 2262 | meshes[m].sendToGpu(this); |
| 2263 | fLastPrimitiveType = meshes[m].primitiveType(); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 2264 | } |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 2265 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2266 | #if SWAP_PER_DRAW |
| 2267 | glFlush(); |
commit-bot@chromium.org | 4382330 | 2013-09-25 20:57:51 +0000 | [diff] [blame] | 2268 | #if defined(SK_BUILD_FOR_MAC) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2269 | aglSwapBuffers(aglGetCurrentContext()); |
| 2270 | int set_a_break_pt_here = 9; |
| 2271 | aglSwapBuffers(aglGetCurrentContext()); |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 2272 | #elif defined(SK_BUILD_FOR_WIN) |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2273 | SwapBuf(); |
| 2274 | int set_a_break_pt_here = 9; |
| 2275 | SwapBuf(); |
| 2276 | #endif |
| 2277 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2280 | static GrGLenum gr_primitive_type_to_gl_mode(GrPrimitiveType primitiveType) { |
| 2281 | switch (primitiveType) { |
| 2282 | case GrPrimitiveType::kTriangles: |
| 2283 | return GR_GL_TRIANGLES; |
| 2284 | case GrPrimitiveType::kTriangleStrip: |
| 2285 | return GR_GL_TRIANGLE_STRIP; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2286 | case GrPrimitiveType::kPoints: |
| 2287 | return GR_GL_POINTS; |
| 2288 | case GrPrimitiveType::kLines: |
| 2289 | return GR_GL_LINES; |
| 2290 | case GrPrimitiveType::kLineStrip: |
| 2291 | return GR_GL_LINE_STRIP; |
Robert Phillips | 571177f | 2019-10-04 14:41:49 -0400 | [diff] [blame] | 2292 | case GrPrimitiveType::kPath: |
| 2293 | SK_ABORT("non-mesh-based GrPrimitiveType"); |
| 2294 | return 0; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2295 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2296 | SK_ABORT("invalid GrPrimitiveType"); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2297 | } |
| 2298 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2299 | void GrGLGpu::sendMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer, |
| 2300 | int vertexCount, int baseVertex) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2301 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2302 | if (this->glCaps().drawArraysBaseVertexIsBroken()) { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2303 | this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2304 | GL_CALL(DrawArrays(glPrimType, 0, vertexCount)); |
| 2305 | } else { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2306 | this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2307 | GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount)); |
| 2308 | } |
| 2309 | fStats.incNumDraws(); |
| 2310 | } |
| 2311 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2312 | static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) { |
| 2313 | size_t baseOffset = baseIndex * sizeof(uint16_t); |
| 2314 | if (indexBuffer->isCpuBuffer()) { |
| 2315 | return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset; |
| 2316 | } else { |
| 2317 | return reinterpret_cast<const GrGLvoid*>(baseOffset); |
| 2318 | } |
| 2319 | } |
| 2320 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2321 | void GrGLGpu::sendIndexedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2322 | int indexCount, int baseIndex, uint16_t minIndexValue, |
| 2323 | uint16_t maxIndexValue, const GrBuffer* vertexBuffer, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2324 | int baseVertex, GrPrimitiveRestart enablePrimitiveRestart) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2325 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2326 | const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2327 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2328 | this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, enablePrimitiveRestart); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2329 | |
| 2330 | if (this->glCaps().drawRangeElementsSupport()) { |
| 2331 | GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2332 | GR_GL_UNSIGNED_SHORT, elementPtr)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2333 | } else { |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2334 | GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 2335 | } |
| 2336 | fStats.incNumDraws(); |
| 2337 | } |
| 2338 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2339 | void GrGLGpu::sendInstancedMeshToGpu(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2340 | int vertexCount, int baseVertex, |
| 2341 | const GrBuffer* instanceBuffer, int instanceCount, |
| 2342 | int baseInstance) { |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 2343 | GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Chris Dalton | 1b4ad76 | 2018-10-04 11:58:09 -0600 | [diff] [blame] | 2344 | int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 2345 | for (int i = 0; i < instanceCount; i += maxInstances) { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2346 | this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i, |
| 2347 | GrPrimitiveRestart::kNo); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 2348 | GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, |
| 2349 | SkTMin(instanceCount - i, maxInstances))); |
| 2350 | fStats.incNumDraws(); |
| 2351 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2352 | } |
| 2353 | |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2354 | void GrGLGpu::sendIndexedInstancedMeshToGpu(GrPrimitiveType primitiveType, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2355 | const GrBuffer* indexBuffer, int indexCount, |
| 2356 | int baseIndex, const GrBuffer* vertexBuffer, |
| 2357 | int baseVertex, const GrBuffer* instanceBuffer, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 2358 | int instanceCount, int baseInstance, |
| 2359 | GrPrimitiveRestart enablePrimitiveRestart) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 2360 | const GrGLenum glPrimType = gr_primitive_type_to_gl_mode(primitiveType); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2361 | const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex); |
Chris Dalton | 1b4ad76 | 2018-10-04 11:58:09 -0600 | [diff] [blame] | 2362 | int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount); |
| 2363 | for (int i = 0; i < instanceCount; i += maxInstances) { |
| 2364 | this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, instanceBuffer, baseInstance + i, |
| 2365 | enablePrimitiveRestart); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 2366 | GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr, |
Chris Dalton | 1b4ad76 | 2018-10-04 11:58:09 -0600 | [diff] [blame] | 2367 | SkTMin(instanceCount - i, maxInstances))); |
| 2368 | fStats.incNumDraws(); |
| 2369 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 2370 | } |
| 2371 | |
Chris Dalton | 16a33c6 | 2019-09-24 22:19:17 -0600 | [diff] [blame] | 2372 | void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect, |
| 2373 | GrSurfaceOrigin resolveOrigin, ForExternalIO) { |
Chris Dalton | c139d08 | 2019-09-26 14:04:24 -0600 | [diff] [blame] | 2374 | // Some extensions automatically resolves the texture when it is read. |
| 2375 | SkASSERT(this->glCaps().usesMSAARenderBuffers()); |
| 2376 | |
bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 2377 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(target); |
Chris Dalton | c139d08 | 2019-09-26 14:04:24 -0600 | [diff] [blame] | 2378 | SkASSERT(rt->textureFBOID() != rt->renderFBOID()); |
| 2379 | SkASSERT(rt->textureFBOID() != 0 && rt->renderFBOID() != 0); |
| 2380 | this->bindFramebuffer(GR_GL_READ_FRAMEBUFFER, rt->renderFBOID()); |
| 2381 | this->bindFramebuffer(GR_GL_DRAW_FRAMEBUFFER, rt->textureFBOID()); |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2382 | |
Chris Dalton | c139d08 | 2019-09-26 14:04:24 -0600 | [diff] [blame] | 2383 | // make sure we go through flushRenderTarget() since we've modified |
| 2384 | // the bound DRAW FBO ID. |
| 2385 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
| 2386 | if (GrGLCaps::kES_Apple_MSFBOType == this->glCaps().msFBOType()) { |
| 2387 | // Apple's extension uses the scissor as the blit bounds. |
| 2388 | GrScissorState scissorState; |
| 2389 | scissorState.set(resolveRect); |
| 2390 | this->flushScissor(scissorState, rt->width(), rt->height(), resolveOrigin); |
| 2391 | this->disableWindowRectangles(); |
| 2392 | GL_CALL(ResolveMultisampleFramebuffer()); |
| 2393 | } else { |
| 2394 | int l, b, r, t; |
| 2395 | if (GrGLCaps::kResolveMustBeFull_BlitFrambufferFlag & |
| 2396 | this->glCaps().blitFramebufferSupportFlags()) { |
| 2397 | l = 0; |
| 2398 | b = 0; |
| 2399 | r = target->width(); |
| 2400 | t = target->height(); |
| 2401 | } else { |
| 2402 | auto rect = GrNativeRect::MakeRelativeTo( |
| 2403 | resolveOrigin, rt->height(), resolveRect); |
| 2404 | l = rect.fX; |
| 2405 | b = rect.fY; |
| 2406 | r = rect.fX + rect.fWidth; |
| 2407 | t = rect.fY + rect.fHeight; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2408 | } |
Chris Dalton | c139d08 | 2019-09-26 14:04:24 -0600 | [diff] [blame] | 2409 | |
| 2410 | // BlitFrameBuffer respects the scissor, so disable it. |
| 2411 | this->disableScissor(); |
| 2412 | this->disableWindowRectangles(); |
| 2413 | GL_CALL(BlitFramebuffer(l, b, r, t, l, b, r, t, GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2417 | namespace { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2418 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2419 | |
| 2420 | GrGLenum gr_to_gl_stencil_op(GrStencilOp op) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2421 | static const GrGLenum gTable[kGrStencilOpCount] = { |
| 2422 | GR_GL_KEEP, // kKeep |
| 2423 | GR_GL_ZERO, // kZero |
| 2424 | GR_GL_REPLACE, // kReplace |
| 2425 | GR_GL_INVERT, // kInvert |
| 2426 | GR_GL_INCR_WRAP, // kIncWrap |
| 2427 | GR_GL_DECR_WRAP, // kDecWrap |
| 2428 | GR_GL_INCR, // kIncClamp |
| 2429 | GR_GL_DECR, // kDecClamp |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2430 | }; |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 2431 | static_assert(0 == (int)GrStencilOp::kKeep); |
| 2432 | static_assert(1 == (int)GrStencilOp::kZero); |
| 2433 | static_assert(2 == (int)GrStencilOp::kReplace); |
| 2434 | static_assert(3 == (int)GrStencilOp::kInvert); |
| 2435 | static_assert(4 == (int)GrStencilOp::kIncWrap); |
| 2436 | static_assert(5 == (int)GrStencilOp::kDecWrap); |
| 2437 | static_assert(6 == (int)GrStencilOp::kIncClamp); |
| 2438 | static_assert(7 == (int)GrStencilOp::kDecClamp); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2439 | SkASSERT(op < (GrStencilOp)kGrStencilOpCount); |
| 2440 | return gTable[(int)op]; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | void set_gl_stencil(const GrGLInterface* gl, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2444 | const GrStencilSettings::Face& face, |
| 2445 | GrGLenum glFace) { |
| 2446 | GrGLenum glFunc = GrToGLStencilFunc(face.fTest); |
| 2447 | GrGLenum glFailOp = gr_to_gl_stencil_op(face.fFailOp); |
| 2448 | GrGLenum glPassOp = gr_to_gl_stencil_op(face.fPassOp); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2449 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 2450 | GrGLint ref = face.fRef; |
| 2451 | GrGLint mask = face.fTestMask; |
| 2452 | GrGLint writeMask = face.fWriteMask; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2453 | |
| 2454 | if (GR_GL_FRONT_AND_BACK == glFace) { |
| 2455 | // we call the combined func just in case separate stencil is not |
| 2456 | // supported. |
| 2457 | GR_GL_CALL(gl, StencilFunc(glFunc, ref, mask)); |
| 2458 | GR_GL_CALL(gl, StencilMask(writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2459 | GR_GL_CALL(gl, StencilOp(glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2460 | } else { |
| 2461 | GR_GL_CALL(gl, StencilFuncSeparate(glFace, glFunc, ref, mask)); |
| 2462 | GR_GL_CALL(gl, StencilMaskSeparate(glFace, writeMask)); |
vbuzinov | c5d58f0 | 2015-08-21 05:24:24 -0700 | [diff] [blame] | 2463 | GR_GL_CALL(gl, StencilOpSeparate(glFace, glFailOp, GR_GL_KEEP, glPassOp)); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2467 | |
Chris Dalton | 71713f6 | 2019-04-18 12:41:03 -0600 | [diff] [blame] | 2468 | void GrGLGpu::flushStencil(const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2469 | if (stencilSettings.isDisabled()) { |
| 2470 | this->disableStencil(); |
Chris Dalton | 71713f6 | 2019-04-18 12:41:03 -0600 | [diff] [blame] | 2471 | } else if (fHWStencilSettings != stencilSettings || |
| 2472 | (stencilSettings.isTwoSided() && fHWStencilOrigin != origin)) { |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2473 | if (kYes_TriState != fHWStencilTestEnabled) { |
| 2474 | GL_CALL(Enable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2475 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2476 | fHWStencilTestEnabled = kYes_TriState; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 2477 | } |
Chris Dalton | 67d43fe | 2019-11-05 23:01:21 -0700 | [diff] [blame] | 2478 | if (!stencilSettings.isTwoSided()) { |
| 2479 | set_gl_stencil(this->glInterface(), stencilSettings.singleSidedFace(), |
| 2480 | GR_GL_FRONT_AND_BACK); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2481 | } else { |
Chris Dalton | 67d43fe | 2019-11-05 23:01:21 -0700 | [diff] [blame] | 2482 | set_gl_stencil(this->glInterface(), stencilSettings.postOriginCWFace(origin), |
| 2483 | GR_GL_FRONT); |
| 2484 | set_gl_stencil(this->glInterface(), stencilSettings.postOriginCCWFace(origin), |
| 2485 | GR_GL_BACK); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2486 | } |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 2487 | fHWStencilSettings = stencilSettings; |
Chris Dalton | 71713f6 | 2019-04-18 12:41:03 -0600 | [diff] [blame] | 2488 | fHWStencilOrigin = origin; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2492 | void GrGLGpu::disableStencil() { |
| 2493 | if (kNo_TriState != fHWStencilTestEnabled) { |
| 2494 | GL_CALL(Disable(GR_GL_STENCIL_TEST)); |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2495 | |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 2496 | fHWStencilTestEnabled = kNo_TriState; |
| 2497 | fHWStencilSettings.invalidate(); |
| 2498 | } |
| 2499 | } |
| 2500 | |
Chris Dalton | 4c56b03 | 2019-03-04 12:28:42 -0700 | [diff] [blame] | 2501 | void GrGLGpu::flushHWAAState(GrRenderTarget* rt, bool useHWAA) { |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 2502 | // rt is only optional if useHWAA is false. |
| 2503 | SkASSERT(rt || !useHWAA); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 2504 | #ifdef SK_DEBUG |
| 2505 | if (useHWAA && rt->numSamples() <= 1) { |
| 2506 | SkASSERT(this->caps()->mixedSamplesSupport()); |
| 2507 | SkASSERT(0 != static_cast<GrGLRenderTarget*>(rt)->renderFBOID()); |
| 2508 | SkASSERT(rt->renderTargetPriv().getStencilAttachment()); |
| 2509 | } |
| 2510 | #endif |
bsalomon@google.com | 202d139 | 2013-03-19 18:58:08 +0000 | [diff] [blame] | 2511 | |
csmartdalton | 2b5f2cb | 2016-06-10 14:06:32 -0700 | [diff] [blame] | 2512 | if (this->caps()->multisampleDisableSupport()) { |
cdalton | d0a840d | 2015-03-16 17:19:58 -0700 | [diff] [blame] | 2513 | if (useHWAA) { |
| 2514 | if (kYes_TriState != fMSAAEnabled) { |
| 2515 | GL_CALL(Enable(GR_GL_MULTISAMPLE)); |
| 2516 | fMSAAEnabled = kYes_TriState; |
| 2517 | } |
| 2518 | } else { |
| 2519 | if (kNo_TriState != fMSAAEnabled) { |
| 2520 | GL_CALL(Disable(GR_GL_MULTISAMPLE)); |
| 2521 | fMSAAEnabled = kNo_TriState; |
bsalomon@google.com | f954d8d | 2011-04-06 17:50:02 +0000 | [diff] [blame] | 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 2527 | void GrGLGpu::flushConservativeRasterState(bool enabled) { |
| 2528 | if (this->caps()->conservativeRasterSupport()) { |
| 2529 | if (enabled) { |
| 2530 | if (kYes_TriState != fHWConservativeRasterEnabled) { |
| 2531 | GL_CALL(Enable(GR_GL_CONSERVATIVE_RASTERIZATION)); |
| 2532 | fHWConservativeRasterEnabled = kYes_TriState; |
| 2533 | } |
| 2534 | } else { |
| 2535 | if (kNo_TriState != fHWConservativeRasterEnabled) { |
| 2536 | GL_CALL(Disable(GR_GL_CONSERVATIVE_RASTERIZATION)); |
| 2537 | fHWConservativeRasterEnabled = kNo_TriState; |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | |
Chris Dalton | 1215cda | 2019-12-17 21:44:04 -0700 | [diff] [blame] | 2543 | void GrGLGpu::flushWireframeState(bool enabled) { |
| 2544 | if (this->caps()->wireframeSupport()) { |
| 2545 | if (this->caps()->wireframeMode() || enabled) { |
| 2546 | if (kYes_TriState != fHWWireframeEnabled) { |
| 2547 | GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_LINE)); |
| 2548 | fHWWireframeEnabled = kYes_TriState; |
| 2549 | } |
| 2550 | } else { |
| 2551 | if (kNo_TriState != fHWWireframeEnabled) { |
| 2552 | GL_CALL(PolygonMode(GR_GL_FRONT_AND_BACK, GR_GL_FILL)); |
| 2553 | fHWWireframeEnabled = kNo_TriState; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2559 | void GrGLGpu::flushBlendAndColorWrite( |
| 2560 | const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle& swizzle) { |
| 2561 | if (this->glCaps().neverDisableColorWrites() && !blendInfo.fWriteColor) { |
| 2562 | // We need to work around a driver bug by using a blend state that preserves the dst color, |
| 2563 | // rather than disabling color writes. |
| 2564 | GrXferProcessor::BlendInfo preserveDstBlend; |
| 2565 | preserveDstBlend.fSrcBlend = kZero_GrBlendCoeff; |
| 2566 | preserveDstBlend.fDstBlend = kOne_GrBlendCoeff; |
| 2567 | this->flushBlendAndColorWrite(preserveDstBlend, swizzle); |
| 2568 | return; |
| 2569 | } |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 2570 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2571 | GrBlendEquation equation = blendInfo.fEquation; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 2572 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 2573 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2574 | |
| 2575 | // Any optimization to disable blending should have already been applied and |
| 2576 | // tweaked the equation to "add" or "subtract", and the coeffs to (1, 0). |
Dongseong Hwang | 4e1f022 | 2018-11-01 09:10:51 -0700 | [diff] [blame] | 2577 | bool blendOff = |
| 2578 | ((kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) && |
| 2579 | kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff) || |
| 2580 | !blendInfo.fWriteColor; |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2581 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2582 | if (blendOff) { |
| 2583 | if (kNo_TriState != fHWBlendState.fEnabled) { |
| 2584 | GL_CALL(Disable(GR_GL_BLEND)); |
joel.liang | 9764c40 | 2015-07-09 19:46:18 -0700 | [diff] [blame] | 2585 | |
| 2586 | // Workaround for the ARM KHR_blend_equation_advanced blacklist issue |
| 2587 | // https://code.google.com/p/skia/issues/detail?id=3943 |
| 2588 | if (kARM_GrGLVendor == this->ctxInfo().vendor() && |
| 2589 | GrBlendEquationIsAdvanced(fHWBlendState.fEquation)) { |
| 2590 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2591 | // Set to any basic blending equation. |
| 2592 | GrBlendEquation blend_equation = kAdd_GrBlendEquation; |
| 2593 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[blend_equation])); |
| 2594 | fHWBlendState.fEquation = blend_equation; |
| 2595 | } |
| 2596 | |
egdaniel | b414f25 | 2014-07-29 13:15:47 -0700 | [diff] [blame] | 2597 | fHWBlendState.fEnabled = kNo_TriState; |
| 2598 | } |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2599 | } else { |
| 2600 | if (kYes_TriState != fHWBlendState.fEnabled) { |
| 2601 | GL_CALL(Enable(GR_GL_BLEND)); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2602 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2603 | fHWBlendState.fEnabled = kYes_TriState; |
| 2604 | } |
Brian Salomon | af971de | 2017-06-08 16:11:33 -0400 | [diff] [blame] | 2605 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2606 | if (fHWBlendState.fEquation != equation) { |
| 2607 | GL_CALL(BlendEquation(gXfermodeEquation2Blend[equation])); |
| 2608 | fHWBlendState.fEquation = equation; |
| 2609 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2610 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2611 | if (GrBlendEquationIsAdvanced(equation)) { |
| 2612 | SkASSERT(this->caps()->advancedBlendEquationSupport()); |
| 2613 | // Advanced equations have no other blend state. |
| 2614 | return; |
| 2615 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2616 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2617 | if (fHWBlendState.fSrcCoeff != srcCoeff || fHWBlendState.fDstCoeff != dstCoeff) { |
| 2618 | GL_CALL(BlendFunc(gXfermodeCoeff2Blend[srcCoeff], |
| 2619 | gXfermodeCoeff2Blend[dstCoeff])); |
| 2620 | fHWBlendState.fSrcCoeff = srcCoeff; |
| 2621 | fHWBlendState.fDstCoeff = dstCoeff; |
| 2622 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 2623 | |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2624 | if ((BlendCoeffReferencesConstant(srcCoeff) || BlendCoeffReferencesConstant(dstCoeff))) { |
| 2625 | SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant); |
| 2626 | if (!fHWBlendState.fConstColorValid || fHWBlendState.fConstColor != blendConst) { |
| 2627 | GL_CALL(BlendColor(blendConst.fR, blendConst.fG, blendConst.fB, blendConst.fA)); |
| 2628 | fHWBlendState.fConstColor = blendConst; |
| 2629 | fHWBlendState.fConstColorValid = true; |
| 2630 | } |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 2631 | } |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2632 | } |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 2633 | |
| 2634 | this->flushColorWrite(blendInfo.fWriteColor); |
bsalomon@google.com | 0650e81 | 2011-04-08 18:07:53 +0000 | [diff] [blame] | 2635 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 2636 | |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2637 | static void get_gl_swizzle_values(const GrSwizzle& swizzle, GrGLenum glValues[4]) { |
Brian Salomon | 327955b | 2018-10-22 15:39:22 +0000 | [diff] [blame] | 2638 | for (int i = 0; i < 4; ++i) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2639 | switch (swizzle[i]) { |
| 2640 | case 'r': glValues[i] = GR_GL_RED; break; |
| 2641 | case 'g': glValues[i] = GR_GL_GREEN; break; |
| 2642 | case 'b': glValues[i] = GR_GL_BLUE; break; |
| 2643 | case 'a': glValues[i] = GR_GL_ALPHA; break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 2644 | case '0': glValues[i] = GR_GL_ZERO; break; |
Greg Daniel | 216a9d7 | 2019-02-20 10:12:29 -0500 | [diff] [blame] | 2645 | case '1': glValues[i] = GR_GL_ONE; break; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2646 | default: SK_ABORT("Unsupported component"); |
| 2647 | } |
Brian Salomon | 327955b | 2018-10-22 15:39:22 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 2651 | void GrGLGpu::bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle& swizzle, |
| 2652 | GrGLTexture* texture) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2653 | SkASSERT(texture); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 2654 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 2655 | #ifdef SK_DEBUG |
| 2656 | if (!this->caps()->npotTextureTileSupport()) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 2657 | if (samplerState.isRepeated()) { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 2658 | const int w = texture->width(); |
| 2659 | const int h = texture->height(); |
| 2660 | SkASSERT(SkIsPow2(w) && SkIsPow2(h)); |
| 2661 | } |
| 2662 | } |
| 2663 | #endif |
| 2664 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 2665 | GrGpuResource::UniqueID textureID = texture->uniqueID(); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2666 | GrGLenum target = texture->target(); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2667 | if (fHWTextureUnitBindings[unitIdx].boundID(target) != textureID) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 2668 | this->setTextureUnit(unitIdx); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2669 | GL_CALL(BindTexture(target, texture->textureID())); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2670 | fHWTextureUnitBindings[unitIdx].setBoundID(target, textureID); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2673 | if (samplerState.filter() == GrSamplerState::Filter::kMipMap) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 2674 | if (!this->caps()->mipMapSupport() || |
| 2675 | texture->texturePriv().mipMapped() == GrMipMapped::kNo) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2676 | samplerState.setFilterMode(GrSamplerState::Filter::kBilerp); |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 2677 | } |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 2678 | } |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 2679 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2680 | #ifdef SK_DEBUG |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 2681 | // We were supposed to ensure MipMaps were up-to-date before getting here. |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2682 | if (samplerState.filter() == GrSamplerState::Filter::kMipMap) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2683 | SkASSERT(!texture->texturePriv().mipMapsAreDirty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 2684 | } |
| 2685 | #endif |
| 2686 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2687 | auto timestamp = texture->parameters()->resetTimestamp(); |
| 2688 | bool setAll = timestamp < fResetTimestampForTextureParameters; |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 2689 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2690 | const GrGLTextureParameters::SamplerOverriddenState* samplerStateToRecord = nullptr; |
| 2691 | GrGLTextureParameters::SamplerOverriddenState newSamplerState; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2692 | if (fSamplerObjectCache) { |
| 2693 | fSamplerObjectCache->bindSampler(unitIdx, samplerState); |
| 2694 | } else { |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2695 | const GrGLTextureParameters::SamplerOverriddenState& oldSamplerState = |
| 2696 | texture->parameters()->samplerOverriddenState(); |
| 2697 | samplerStateToRecord = &newSamplerState; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2698 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2699 | newSamplerState.fMinFilter = filter_to_gl_min_filter(samplerState.filter()); |
| 2700 | newSamplerState.fMagFilter = filter_to_gl_mag_filter(samplerState.filter()); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2701 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2702 | newSamplerState.fWrapS = wrap_mode_to_gl_wrap(samplerState.wrapModeX(), this->glCaps()); |
| 2703 | newSamplerState.fWrapT = wrap_mode_to_gl_wrap(samplerState.wrapModeY(), this->glCaps()); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2704 | |
| 2705 | // These are the OpenGL default values. |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2706 | newSamplerState.fMinLOD = -1000.f; |
| 2707 | newSamplerState.fMaxLOD = 1000.f; |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2708 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2709 | if (setAll || newSamplerState.fMagFilter != oldSamplerState.fMagFilter) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2710 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2711 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAG_FILTER, newSamplerState.fMagFilter)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2712 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2713 | if (setAll || newSamplerState.fMinFilter != oldSamplerState.fMinFilter) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2714 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2715 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MIN_FILTER, newSamplerState.fMinFilter)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2716 | } |
Mike Klein | 1bccae5 | 2018-10-19 11:38:44 +0000 | [diff] [blame] | 2717 | if (this->glCaps().mipMapLevelAndLodControlSupport()) { |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2718 | if (setAll || newSamplerState.fMinLOD != oldSamplerState.fMinLOD) { |
Mike Klein | 1bccae5 | 2018-10-19 11:38:44 +0000 | [diff] [blame] | 2719 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2720 | GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MIN_LOD, newSamplerState.fMinLOD)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2721 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2722 | if (setAll || newSamplerState.fMaxLOD != oldSamplerState.fMaxLOD) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2723 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2724 | GL_CALL(TexParameterf(target, GR_GL_TEXTURE_MAX_LOD, newSamplerState.fMaxLOD)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2725 | } |
| 2726 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2727 | if (setAll || newSamplerState.fWrapS != oldSamplerState.fWrapS) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2728 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2729 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_S, newSamplerState.fWrapS)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2730 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2731 | if (setAll || newSamplerState.fWrapT != oldSamplerState.fWrapT) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2732 | this->setTextureUnit(unitIdx); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2733 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_WRAP_T, newSamplerState.fWrapT)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2734 | } |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 2735 | if (this->glCaps().clampToBorderSupport()) { |
| 2736 | // Make sure the border color is transparent black (the default) |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2737 | if (setAll || oldSamplerState.fBorderColorInvalid) { |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 2738 | this->setTextureUnit(unitIdx); |
Brian Salomon | 89f2ff1 | 2018-12-07 19:30:25 -0500 | [diff] [blame] | 2739 | static const GrGLfloat kTransparentBlack[4] = {0.f, 0.f, 0.f, 0.f}; |
| 2740 | GL_CALL(TexParameterfv(target, GR_GL_TEXTURE_BORDER_COLOR, kTransparentBlack)); |
Michael Ludwig | f23a152 | 2018-12-10 11:36:13 -0500 | [diff] [blame] | 2741 | } |
| 2742 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2743 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2744 | GrGLTextureParameters::NonsamplerState newNonsamplerState; |
| 2745 | newNonsamplerState.fBaseMipMapLevel = 0; |
| 2746 | newNonsamplerState.fMaxMipMapLevel = texture->texturePriv().maxMipMapLevel(); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2747 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2748 | const GrGLTextureParameters::NonsamplerState& oldNonsamplerState = |
| 2749 | texture->parameters()->nonsamplerState(); |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 2750 | if (!this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) { |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2751 | newNonsamplerState.fSwizzleKey = swizzle.asKey(); |
| 2752 | if (setAll || swizzle.asKey() != oldNonsamplerState.fSwizzleKey) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2753 | GrGLenum glValues[4]; |
| 2754 | get_gl_swizzle_values(swizzle, glValues); |
| 2755 | this->setTextureUnit(unitIdx); |
Kevin Lubick | 8aa203c | 2019-03-19 13:23:10 -0400 | [diff] [blame] | 2756 | if (GR_IS_GR_GL(this->glStandard())) { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 2757 | static_assert(sizeof(glValues[0]) == sizeof(GrGLint)); |
Kevin Lubick | 8aa203c | 2019-03-19 13:23:10 -0400 | [diff] [blame] | 2758 | GL_CALL(TexParameteriv(target, GR_GL_TEXTURE_SWIZZLE_RGBA, |
| 2759 | reinterpret_cast<const GrGLint*>(glValues))); |
| 2760 | } else if (GR_IS_GR_GL_ES(this->glStandard())) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2761 | // ES3 added swizzle support but not GL_TEXTURE_SWIZZLE_RGBA. |
| 2762 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_R, glValues[0])); |
| 2763 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_G, glValues[1])); |
| 2764 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_B, glValues[2])); |
| 2765 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_SWIZZLE_A, glValues[3])); |
Brian Salomon | bfb3df4 | 2018-10-19 19:24:31 +0000 | [diff] [blame] | 2766 | } |
Brian Salomon | bfb3df4 | 2018-10-19 19:24:31 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2769 | // These are not supported in ES2 contexts |
Brian Salomon | f384193 | 2018-11-29 09:13:37 -0500 | [diff] [blame] | 2770 | if (this->glCaps().mipMapLevelAndLodControlSupport() && |
| 2771 | (texture->texturePriv().textureType() != GrTextureType::kExternal || |
| 2772 | !this->glCaps().dontSetBaseOrMaxLevelForExternalTextures())) { |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2773 | if (newNonsamplerState.fBaseMipMapLevel != oldNonsamplerState.fBaseMipMapLevel) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2774 | this->setTextureUnit(unitIdx); |
| 2775 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_BASE_LEVEL, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2776 | newNonsamplerState.fBaseMipMapLevel)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2777 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2778 | if (newNonsamplerState.fMaxMipMapLevel != oldNonsamplerState.fMaxMipMapLevel) { |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2779 | this->setTextureUnit(unitIdx); |
| 2780 | GL_CALL(TexParameteri(target, GR_GL_TEXTURE_MAX_LEVEL, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2781 | newNonsamplerState.fMaxMipMapLevel)); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 2782 | } |
Brian Salomon | 327955b | 2018-10-22 15:39:22 +0000 | [diff] [blame] | 2783 | } |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 2784 | texture->parameters()->set(samplerStateToRecord, newNonsamplerState, |
| 2785 | fResetTimestampForTextureParameters); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 2788 | void GrGLGpu::onResetTextureBindings() { |
| 2789 | static constexpr GrGLenum kTargets[] = {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE, |
| 2790 | GR_GL_TEXTURE_EXTERNAL}; |
| 2791 | for (int i = 0; i < this->numTextureUnits(); ++i) { |
| 2792 | this->setTextureUnit(i); |
| 2793 | for (auto target : kTargets) { |
| 2794 | if (fHWTextureUnitBindings[i].hasBeenModified(target)) { |
| 2795 | GL_CALL(BindTexture(target, 0)); |
| 2796 | } |
| 2797 | } |
| 2798 | fHWTextureUnitBindings[i].invalidateAllTargets(true); |
| 2799 | } |
| 2800 | } |
| 2801 | |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 2802 | void GrGLGpu::flushColorWrite(bool writeColor) { |
| 2803 | if (!writeColor) { |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2804 | if (kNo_TriState != fHWWriteToColor) { |
Brian Osman | a9aaef0 | 2019-05-06 20:07:20 +0000 | [diff] [blame] | 2805 | GL_CALL(ColorMask(GR_GL_FALSE, GR_GL_FALSE, |
| 2806 | GR_GL_FALSE, GR_GL_FALSE)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2807 | fHWWriteToColor = kNo_TriState; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2808 | } |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2809 | } else { |
| 2810 | if (kYes_TriState != fHWWriteToColor) { |
Brian Osman | a9aaef0 | 2019-05-06 20:07:20 +0000 | [diff] [blame] | 2811 | GL_CALL(ColorMask(GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE, GR_GL_TRUE)); |
bsalomon@google.com | 978c8c6 | 2012-05-21 14:45:49 +0000 | [diff] [blame] | 2812 | fHWWriteToColor = kYes_TriState; |
| 2813 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2814 | } |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 2815 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 2816 | |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 2817 | void GrGLGpu::flushClearColor(const SkPMColor4f& color) { |
| 2818 | GrGLfloat r = color.fR, g = color.fG, b = color.fB, a = color.fA; |
| 2819 | if (this->glCaps().clearToBoundaryValuesIsBroken() && |
| 2820 | (1 == r || 0 == r) && (1 == g || 0 == g) && (1 == b || 0 == b) && (1 == a || 0 == a)) { |
| 2821 | static const GrGLfloat safeAlpha1 = nextafter(1.f, 2.f); |
| 2822 | static const GrGLfloat safeAlpha0 = nextafter(0.f, -1.f); |
| 2823 | a = (1 == a) ? safeAlpha1 : safeAlpha0; |
| 2824 | } |
Brian Salomon | 805cc7a | 2019-01-28 09:52:34 -0500 | [diff] [blame] | 2825 | if (r != fHWClearColor[0] || g != fHWClearColor[1] || |
| 2826 | b != fHWClearColor[2] || a != fHWClearColor[3]) { |
| 2827 | GL_CALL(ClearColor(r, g, b, a)); |
| 2828 | fHWClearColor[0] = r; |
| 2829 | fHWClearColor[1] = g; |
| 2830 | fHWClearColor[2] = b; |
| 2831 | fHWClearColor[3] = a; |
| 2832 | } |
| 2833 | } |
| 2834 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 2835 | void GrGLGpu::setTextureUnit(int unit) { |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2836 | SkASSERT(unit >= 0 && unit < this->numTextureUnits()); |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2837 | if (unit != fHWActiveTextureUnitIdx) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 2838 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + unit)); |
bsalomon@google.com | 4920939 | 2012-06-05 15:13:46 +0000 | [diff] [blame] | 2839 | fHWActiveTextureUnitIdx = unit; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
bsalomon@google.com | 316f9923 | 2011-01-13 21:28:12 +0000 | [diff] [blame] | 2842 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2843 | void GrGLGpu::bindTextureToScratchUnit(GrGLenum target, GrGLint textureID) { |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2844 | // Bind the last texture unit since it is the least likely to be used by GrGLProgram. |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2845 | int lastUnitIdx = this->numTextureUnits() - 1; |
commit-bot@chromium.org | a15f7e5 | 2013-06-05 23:29:25 +0000 | [diff] [blame] | 2846 | if (lastUnitIdx != fHWActiveTextureUnitIdx) { |
| 2847 | GL_CALL(ActiveTexture(GR_GL_TEXTURE0 + lastUnitIdx)); |
| 2848 | fHWActiveTextureUnitIdx = lastUnitIdx; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2849 | } |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2850 | // Clear out the this field so that if a GrGLProgram does use this unit it will rebind the |
| 2851 | // correct texture. |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 2852 | fHWTextureUnitBindings[lastUnitIdx].invalidateForScratchUse(target); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 2853 | GL_CALL(BindTexture(target, textureID)); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2856 | // Determines whether glBlitFramebuffer could be used between src and dst by onCopySurface. |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2857 | static inline bool can_blit_framebuffer_for_copy_surface(const GrSurface* dst, |
| 2858 | const GrSurface* src, |
| 2859 | const SkIRect& srcRect, |
| 2860 | const SkIPoint& dstPoint, |
| 2861 | const GrGLCaps& caps) { |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2862 | int dstSampleCnt = 0; |
| 2863 | int srcSampleCnt = 0; |
| 2864 | if (const GrRenderTarget* rt = dst->asRenderTarget()) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2865 | dstSampleCnt = rt->numSamples(); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2866 | } |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2867 | if (const GrRenderTarget* rt = src->asRenderTarget()) { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2868 | srcSampleCnt = rt->numSamples(); |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2869 | } |
| 2870 | SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTarget())); |
| 2871 | SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTarget())); |
| 2872 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2873 | GrGLFormat dstFormat = dst->backendFormat().asGLFormat(); |
| 2874 | GrGLFormat srcFormat = src->backendFormat().asGLFormat(); |
| 2875 | |
Brian Salomon | e5e7eb1 | 2016-10-14 16:18:33 -0400 | [diff] [blame] | 2876 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
Mike Klein | 1d74620 | 2018-01-25 17:32:51 -0500 | [diff] [blame] | 2877 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2878 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2879 | GrTextureType dstTexType; |
| 2880 | GrTextureType* dstTexTypePtr = nullptr; |
| 2881 | GrTextureType srcTexType; |
| 2882 | GrTextureType* srcTexTypePtr = nullptr; |
| 2883 | if (dstTex) { |
| 2884 | dstTexType = dstTex->texturePriv().textureType(); |
| 2885 | dstTexTypePtr = &dstTexType; |
| 2886 | } |
| 2887 | if (srcTex) { |
| 2888 | srcTexType = srcTex->texturePriv().textureType(); |
| 2889 | srcTexTypePtr = &srcTexType; |
| 2890 | } |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2891 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2892 | return caps.canCopyAsBlit(dstFormat, dstSampleCnt, dstTexTypePtr, |
| 2893 | srcFormat, srcSampleCnt, srcTexTypePtr, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2894 | src->getBoundsRect(), true, srcRect, dstPoint); |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 2895 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2896 | |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 2897 | static bool rt_has_msaa_render_buffer(const GrGLRenderTarget* rt, const GrGLCaps& glCaps) { |
| 2898 | // A RT has a separate MSAA renderbuffer if: |
| 2899 | // 1) It's multisampled |
| 2900 | // 2) We're using an extension with separate MSAA renderbuffers |
| 2901 | // 3) It's not FBO 0, which is special and always auto-resolves |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 2902 | return rt->numSamples() > 1 && glCaps.usesMSAARenderBuffers() && rt->renderFBOID() != 0; |
Brian Osman | a9c8a05 | 2018-01-19 10:31:56 -0500 | [diff] [blame] | 2903 | } |
| 2904 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2905 | static inline bool can_copy_texsubimage(const GrSurface* dst, const GrSurface* src, |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2906 | const GrGLCaps& caps) { |
| 2907 | |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2908 | const GrGLRenderTarget* dstRT = static_cast<const GrGLRenderTarget*>(dst->asRenderTarget()); |
bsalomon@google.com | a271985 | 2013-04-17 14:25:27 +0000 | [diff] [blame] | 2909 | const GrGLRenderTarget* srcRT = static_cast<const GrGLRenderTarget*>(src->asRenderTarget()); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2910 | const GrGLTexture* dstTex = static_cast<const GrGLTexture*>(dst->asTexture()); |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 2911 | const GrGLTexture* srcTex = static_cast<const GrGLTexture*>(src->asTexture()); |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 2912 | |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2913 | bool dstHasMSAARenderBuffer = dstRT ? rt_has_msaa_render_buffer(dstRT, caps) : false; |
| 2914 | bool srcHasMSAARenderBuffer = srcRT ? rt_has_msaa_render_buffer(srcRT, caps) : false; |
| 2915 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2916 | GrGLFormat dstFormat = dst->backendFormat().asGLFormat(); |
| 2917 | GrGLFormat srcFormat = src->backendFormat().asGLFormat(); |
| 2918 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 2919 | GrTextureType dstTexType; |
| 2920 | GrTextureType* dstTexTypePtr = nullptr; |
| 2921 | GrTextureType srcTexType; |
| 2922 | GrTextureType* srcTexTypePtr = nullptr; |
| 2923 | if (dstTex) { |
| 2924 | dstTexType = dstTex->texturePriv().textureType(); |
| 2925 | dstTexTypePtr = &dstTexType; |
| 2926 | } |
| 2927 | if (srcTex) { |
| 2928 | srcTexType = srcTex->texturePriv().textureType(); |
| 2929 | srcTexTypePtr = &srcTexType; |
| 2930 | } |
Greg Daniel | 26dbe3b | 2018-05-03 10:35:42 -0400 | [diff] [blame] | 2931 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 2932 | return caps.canCopyTexSubImage(dstFormat, dstHasMSAARenderBuffer, dstTexTypePtr, |
| 2933 | srcFormat, srcHasMSAARenderBuffer, srcTexTypePtr); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 2936 | // If a temporary FBO was created, its non-zero ID is returned. |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2937 | void GrGLGpu::bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget, |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2938 | TempFBOTarget tempFBOTarget) { |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2939 | GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget()); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2940 | if (!rt || mipLevel > 0) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2941 | SkASSERT(surface->asTexture()); |
Brian Salomon | 9bada54 | 2017-06-12 12:09:30 -0400 | [diff] [blame] | 2942 | GrGLTexture* texture = static_cast<GrGLTexture*>(surface->asTexture()); |
| 2943 | GrGLuint texID = texture->textureID(); |
| 2944 | GrGLenum target = texture->target(); |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2945 | GrGLuint* tempFBOID; |
| 2946 | tempFBOID = kSrc_TempFBOTarget == tempFBOTarget ? &fTempSrcFBOID : &fTempDstFBOID; |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2947 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 2948 | if (0 == *tempFBOID) { |
| 2949 | GR_GL_CALL(this->glInterface(), GenFramebuffers(1, tempFBOID)); |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2952 | this->bindFramebuffer(fboTarget, *tempFBOID); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2953 | GR_GL_CALL( |
| 2954 | this->glInterface(), |
| 2955 | FramebufferTexture2D(fboTarget, GR_GL_COLOR_ATTACHMENT0, target, texID, mipLevel)); |
| 2956 | if (mipLevel == 0) { |
| 2957 | texture->baseLevelWasBoundToFBO(); |
| 2958 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2959 | } else { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2960 | this->bindFramebuffer(fboTarget, rt->renderFBOID()); |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2961 | } |
egdaniel | 0f5f967 | 2015-02-03 11:10:51 -0800 | [diff] [blame] | 2962 | } |
| 2963 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2964 | void GrGLGpu::unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget) { |
Brian Salomon | 71d9d84 | 2016-11-03 13:42:00 -0400 | [diff] [blame] | 2965 | // bindSurfaceFBOForPixelOps temporarily binds textures that are not render targets to |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 2966 | if (mipLevel > 0 || !surface->asRenderTarget()) { |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 2967 | SkASSERT(surface->asTexture()); |
| 2968 | GrGLenum textureTarget = static_cast<GrGLTexture*>(surface->asTexture())->target(); |
| 2969 | GR_GL_CALL(this->glInterface(), FramebufferTexture2D(fboTarget, |
| 2970 | GR_GL_COLOR_ATTACHMENT0, |
| 2971 | textureTarget, |
| 2972 | 0, |
| 2973 | 0)); |
| 2974 | } |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 2977 | void GrGLGpu::onFBOChanged() { |
| 2978 | if (this->caps()->workarounds().flush_on_framebuffer_change || |
| 2979 | this->caps()->workarounds().restore_scissor_on_fbo_change) { |
| 2980 | GL_CALL(Flush()); |
| 2981 | } |
Chris Dalton | 674f77a | 2019-09-30 20:49:39 -0600 | [diff] [blame] | 2982 | #ifdef SK_DEBUG |
| 2983 | if (fIsExecutingCommandBuffer_DebugOnly) { |
| 2984 | SkDebugf("WARNING: GL FBO binding changed while executing a command buffer. " |
| 2985 | "This will severely hurt performance.\n"); |
| 2986 | } |
| 2987 | #endif |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 2990 | void GrGLGpu::bindFramebuffer(GrGLenum target, GrGLuint fboid) { |
| 2991 | fStats.incRenderTargetBinds(); |
| 2992 | GL_CALL(BindFramebuffer(target, fboid)); |
| 2993 | if (target == GR_GL_FRAMEBUFFER || target == GR_GL_DRAW_FRAMEBUFFER) { |
| 2994 | fBoundDrawFramebuffer = fboid; |
| 2995 | } |
| 2996 | |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 2997 | if (this->caps()->workarounds().restore_scissor_on_fbo_change) { |
| 2998 | // The driver forgets the correct scissor when modifying the FBO binding. |
| 2999 | if (!fHWScissorSettings.fRect.isInvalid()) { |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 3000 | const GrNativeRect& r = fHWScissorSettings.fRect; |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 3001 | GL_CALL(Scissor(r.fX, r.fY, r.fWidth, r.fHeight)); |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 3002 | } |
Adrienne Walker | 3ed3399 | 2018-05-15 11:44:34 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 3005 | this->onFBOChanged(); |
Adrienne Walker | 3ed3399 | 2018-05-15 11:44:34 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3008 | void GrGLGpu::deleteFramebuffer(GrGLuint fboid) { |
| 3009 | if (fboid == fBoundDrawFramebuffer && |
| 3010 | this->caps()->workarounds().unbind_attachments_on_bound_render_fbo_delete) { |
| 3011 | // This workaround only applies to deleting currently bound framebuffers |
| 3012 | // on Adreno 420. Because this is a somewhat rare case, instead of |
| 3013 | // tracking all the attachments of every framebuffer instead just always |
| 3014 | // unbind all attachments. |
| 3015 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 3016 | GR_GL_RENDERBUFFER, 0)); |
| 3017 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, |
| 3018 | GR_GL_RENDERBUFFER, 0)); |
| 3019 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, |
| 3020 | GR_GL_RENDERBUFFER, 0)); |
| 3021 | } |
| 3022 | |
| 3023 | GL_CALL(DeleteFramebuffers(1, &fboid)); |
Adrienne Walker | ca0cdef | 2018-08-20 13:49:40 -0700 | [diff] [blame] | 3024 | |
| 3025 | // Deleting the currently bound framebuffer rebinds to 0. |
| 3026 | if (fboid == fBoundDrawFramebuffer) { |
| 3027 | this->onFBOChanged(); |
| 3028 | } |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3031 | bool GrGLGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
Greg Daniel | e227fe4 | 2019-08-21 13:52:24 -0400 | [diff] [blame] | 3032 | const SkIPoint& dstPoint) { |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3033 | // Don't prefer copying as a draw if the dst doesn't already have a FBO object. |
| 3034 | // This implicitly handles this->glCaps().useDrawInsteadOfAllRenderTargetWrites(). |
| 3035 | bool preferCopy = SkToBool(dst->asRenderTarget()); |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3036 | auto dstFormat = dst->backendFormat().asGLFormat(); |
| 3037 | if (preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3038 | if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3039 | return true; |
| 3040 | } |
| 3041 | } |
cblume | 6121405 | 2016-01-26 09:10:48 -0800 | [diff] [blame] | 3042 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3043 | if (can_copy_texsubimage(dst, src, this->glCaps())) { |
| 3044 | this->copySurfaceAsCopyTexSubImage(dst, src, srcRect, dstPoint); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3045 | return true; |
| 3046 | } |
| 3047 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3048 | if (can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())) { |
| 3049 | return this->copySurfaceAsBlitFramebuffer(dst, src, srcRect, dstPoint); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3050 | } |
| 3051 | |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3052 | if (!preferCopy && this->glCaps().canCopyAsDraw(dstFormat, SkToBool(src->asTexture()))) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3053 | if (this->copySurfaceAsDraw(dst, src, srcRect, dstPoint)) { |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3054 | return true; |
| 3055 | } |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3056 | } |
| 3057 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3058 | return false; |
| 3059 | } |
| 3060 | |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3061 | bool GrGLGpu::createCopyProgram(GrTexture* srcTex) { |
Brian Salomon | 5f39427 | 2019-07-02 14:07:49 -0400 | [diff] [blame] | 3062 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3063 | |
| 3064 | int progIdx = TextureToCopyProgramIdx(srcTex); |
| 3065 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
| 3066 | GrSLType samplerType = |
| 3067 | GrSLCombinedSamplerTypeForTextureType(srcTex->texturePriv().textureType()); |
| 3068 | |
| 3069 | if (!fCopyProgramArrayBuffer) { |
| 3070 | static const GrGLfloat vdata[] = { |
| 3071 | 0, 0, |
| 3072 | 0, 1, |
| 3073 | 1, 0, |
| 3074 | 1, 1 |
| 3075 | }; |
| 3076 | fCopyProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex, |
| 3077 | kStatic_GrAccessPattern, vdata); |
| 3078 | } |
| 3079 | if (!fCopyProgramArrayBuffer) { |
| 3080 | return false; |
| 3081 | } |
| 3082 | |
| 3083 | SkASSERT(!fCopyPrograms[progIdx].fProgram); |
| 3084 | GL_CALL_RET(fCopyPrograms[progIdx].fProgram, CreateProgram()); |
| 3085 | if (!fCopyPrograms[progIdx].fProgram) { |
| 3086 | return false; |
| 3087 | } |
| 3088 | |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3089 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3090 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
| 3091 | GrShaderVar::kUniform_TypeModifier); |
| 3092 | GrShaderVar uPosXform("u_posXform", kHalf4_GrSLType, GrShaderVar::kUniform_TypeModifier); |
| 3093 | GrShaderVar uTexture("u_texture", samplerType, GrShaderVar::kUniform_TypeModifier); |
| 3094 | GrShaderVar vTexCoord("v_texCoord", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier); |
| 3095 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier); |
| 3096 | |
Greg Daniel | 9e3169b | 2019-06-06 14:38:17 -0400 | [diff] [blame] | 3097 | SkString vshaderTxt; |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3098 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3099 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
| 3100 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3101 | } |
| 3102 | vTexCoord.addModifier("noperspective"); |
| 3103 | } |
| 3104 | |
| 3105 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
| 3106 | vshaderTxt.append(";"); |
| 3107 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
| 3108 | vshaderTxt.append(";"); |
| 3109 | uPosXform.appendDecl(shaderCaps, &vshaderTxt); |
| 3110 | vshaderTxt.append(";"); |
| 3111 | vTexCoord.appendDecl(shaderCaps, &vshaderTxt); |
| 3112 | vshaderTxt.append(";"); |
| 3113 | |
| 3114 | vshaderTxt.append( |
| 3115 | "// Copy Program VS\n" |
| 3116 | "void main() {" |
| 3117 | " v_texCoord = half2(a_vertex.xy * u_texCoordXform.xy + u_texCoordXform.zw);" |
| 3118 | " sk_Position.xy = a_vertex * u_posXform.xy + u_posXform.zw;" |
| 3119 | " sk_Position.zw = half2(0, 1);" |
| 3120 | "}" |
| 3121 | ); |
| 3122 | |
Greg Daniel | 9e3169b | 2019-06-06 14:38:17 -0400 | [diff] [blame] | 3123 | SkString fshaderTxt; |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3124 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3125 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
| 3126 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3127 | } |
| 3128 | } |
| 3129 | vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier); |
| 3130 | vTexCoord.appendDecl(shaderCaps, &fshaderTxt); |
| 3131 | fshaderTxt.append(";"); |
| 3132 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
| 3133 | fshaderTxt.append(";"); |
| 3134 | fshaderTxt.appendf( |
| 3135 | "// Copy Program FS\n" |
| 3136 | "void main() {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 3137 | " sk_FragColor = sample(u_texture, v_texCoord);" |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3138 | "}" |
| 3139 | ); |
| 3140 | |
| 3141 | auto errorHandler = this->getContext()->priv().getShaderErrorHandler(); |
| 3142 | SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size()); |
| 3143 | SkSL::Program::Settings settings; |
| 3144 | settings.fCaps = shaderCaps; |
| 3145 | SkSL::String glsl; |
| 3146 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind, |
| 3147 | sksl, settings, &glsl, errorHandler); |
| 3148 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
| 3149 | GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler); |
| 3150 | SkASSERT(program->fInputs.isEmpty()); |
| 3151 | |
| 3152 | sksl.assign(fshaderTxt.c_str(), fshaderTxt.size()); |
| 3153 | program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl, |
| 3154 | errorHandler); |
| 3155 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, |
| 3156 | GR_GL_FRAGMENT_SHADER, glsl, &fStats, |
| 3157 | errorHandler); |
| 3158 | SkASSERT(program->fInputs.isEmpty()); |
| 3159 | |
| 3160 | GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram)); |
| 3161 | |
| 3162 | GL_CALL_RET(fCopyPrograms[progIdx].fTextureUniform, |
| 3163 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texture")); |
| 3164 | GL_CALL_RET(fCopyPrograms[progIdx].fPosXformUniform, |
| 3165 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_posXform")); |
| 3166 | GL_CALL_RET(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 3167 | GetUniformLocation(fCopyPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3168 | |
| 3169 | GL_CALL(BindAttribLocation(fCopyPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3170 | |
| 3171 | GL_CALL(DeleteShader(vshader)); |
| 3172 | GL_CALL(DeleteShader(fshader)); |
| 3173 | |
| 3174 | return true; |
| 3175 | } |
| 3176 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3177 | bool GrGLGpu::createMipmapProgram(int progIdx) { |
| 3178 | const bool oddWidth = SkToBool(progIdx & 0x2); |
| 3179 | const bool oddHeight = SkToBool(progIdx & 0x1); |
| 3180 | const int numTaps = (oddWidth ? 2 : 1) * (oddHeight ? 2 : 1); |
| 3181 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3182 | const GrShaderCaps* shaderCaps = this->caps()->shaderCaps(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3183 | |
| 3184 | SkASSERT(!fMipmapPrograms[progIdx].fProgram); |
| 3185 | GL_CALL_RET(fMipmapPrograms[progIdx].fProgram, CreateProgram()); |
| 3186 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 3187 | return false; |
| 3188 | } |
| 3189 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3190 | GrShaderVar aVertex("a_vertex", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier); |
| 3191 | GrShaderVar uTexCoordXform("u_texCoordXform", kHalf4_GrSLType, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3192 | GrShaderVar::kUniform_TypeModifier); |
| 3193 | GrShaderVar uTexture("u_texture", kTexture2DSampler_GrSLType, |
| 3194 | GrShaderVar::kUniform_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3195 | // We need 1, 2, or 4 texture coordinates (depending on parity of each dimension): |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 3196 | GrShaderVar vTexCoords[] = { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3197 | GrShaderVar("v_texCoord0", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3198 | GrShaderVar("v_texCoord1", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3199 | GrShaderVar("v_texCoord2", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
| 3200 | GrShaderVar("v_texCoord3", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier), |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3201 | }; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3202 | GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType,GrShaderVar::kOut_TypeModifier); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3203 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3204 | SkString vshaderTxt; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3205 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3206 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3207 | vshaderTxt.appendf("#extension %s : require\n", extension); |
| 3208 | } |
| 3209 | vTexCoords[0].addModifier("noperspective"); |
| 3210 | vTexCoords[1].addModifier("noperspective"); |
| 3211 | vTexCoords[2].addModifier("noperspective"); |
| 3212 | vTexCoords[3].addModifier("noperspective"); |
| 3213 | } |
| 3214 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3215 | aVertex.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3216 | vshaderTxt.append(";"); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3217 | uTexCoordXform.appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3218 | vshaderTxt.append(";"); |
| 3219 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3220 | vTexCoords[i].appendDecl(shaderCaps, &vshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3221 | vshaderTxt.append(";"); |
| 3222 | } |
| 3223 | |
| 3224 | vshaderTxt.append( |
| 3225 | "// Mipmap Program VS\n" |
| 3226 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 3227 | " sk_Position.xy = a_vertex * half2(2, 2) - half2(1, 1);" |
| 3228 | " sk_Position.zw = half2(0, 1);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3229 | ); |
| 3230 | |
| 3231 | // Insert texture coordinate computation: |
| 3232 | if (oddWidth && oddHeight) { |
| 3233 | vshaderTxt.append( |
| 3234 | " v_texCoord0 = a_vertex.xy * u_texCoordXform.yw;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3235 | " v_texCoord1 = a_vertex.xy * u_texCoordXform.yw + half2(u_texCoordXform.x, 0);" |
| 3236 | " v_texCoord2 = a_vertex.xy * u_texCoordXform.yw + half2(0, u_texCoordXform.z);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3237 | " v_texCoord3 = a_vertex.xy * u_texCoordXform.yw + u_texCoordXform.xz;" |
| 3238 | ); |
| 3239 | } else if (oddWidth) { |
| 3240 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3241 | " v_texCoord0 = a_vertex.xy * half2(u_texCoordXform.y, 1);" |
| 3242 | " v_texCoord1 = a_vertex.xy * half2(u_texCoordXform.y, 1) + half2(u_texCoordXform.x, 0);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3243 | ); |
| 3244 | } else if (oddHeight) { |
| 3245 | vshaderTxt.append( |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 3246 | " v_texCoord0 = a_vertex.xy * half2(1, u_texCoordXform.w);" |
| 3247 | " v_texCoord1 = a_vertex.xy * half2(1, u_texCoordXform.w) + half2(0, u_texCoordXform.z);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3248 | ); |
| 3249 | } else { |
| 3250 | vshaderTxt.append( |
| 3251 | " v_texCoord0 = a_vertex.xy;" |
| 3252 | ); |
| 3253 | } |
| 3254 | |
| 3255 | vshaderTxt.append("}"); |
| 3256 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 3257 | SkString fshaderTxt; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3258 | if (shaderCaps->noperspectiveInterpolationSupport()) { |
| 3259 | if (const char* extension = shaderCaps->noperspectiveInterpolationExtensionString()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3260 | fshaderTxt.appendf("#extension %s : require\n", extension); |
| 3261 | } |
| 3262 | } |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3263 | for (int i = 0; i < numTaps; ++i) { |
Brian Salomon | f31ae49 | 2016-11-18 15:35:33 -0500 | [diff] [blame] | 3264 | vTexCoords[i].setTypeModifier(GrShaderVar::kIn_TypeModifier); |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3265 | vTexCoords[i].appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3266 | fshaderTxt.append(";"); |
| 3267 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 3268 | uTexture.appendDecl(shaderCaps, &fshaderTxt); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3269 | fshaderTxt.append(";"); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3270 | fshaderTxt.append( |
| 3271 | "// Mipmap Program FS\n" |
| 3272 | "void main() {" |
| 3273 | ); |
| 3274 | |
| 3275 | if (oddWidth && oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3276 | fshaderTxt.append( |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 3277 | " sk_FragColor = (sample(u_texture, v_texCoord0) + " |
| 3278 | " sample(u_texture, v_texCoord1) + " |
| 3279 | " sample(u_texture, v_texCoord2) + " |
| 3280 | " sample(u_texture, v_texCoord3)) * 0.25;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3281 | ); |
| 3282 | } else if (oddWidth || oddHeight) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3283 | fshaderTxt.append( |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 3284 | " sk_FragColor = (sample(u_texture, v_texCoord0) + " |
| 3285 | " sample(u_texture, v_texCoord1)) * 0.5;" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3286 | ); |
| 3287 | } else { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 3288 | fshaderTxt.append( |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 3289 | " sk_FragColor = sample(u_texture, v_texCoord0);" |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3290 | ); |
| 3291 | } |
| 3292 | |
| 3293 | fshaderTxt.append("}"); |
| 3294 | |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 3295 | auto errorHandler = this->getContext()->priv().getShaderErrorHandler(); |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 3296 | SkSL::String sksl(vshaderTxt.c_str(), vshaderTxt.size()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3297 | SkSL::Program::Settings settings; |
| 3298 | settings.fCaps = shaderCaps; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3299 | SkSL::String glsl; |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 3300 | std::unique_ptr<SkSL::Program> program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kVertex_Kind, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 3301 | sksl, settings, &glsl, errorHandler); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3302 | GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 3303 | GR_GL_VERTEX_SHADER, glsl, &fStats, errorHandler); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3304 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3305 | |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 3306 | sksl.assign(fshaderTxt.c_str(), fshaderTxt.size()); |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 3307 | program = GrSkSLtoGLSL(*fGLContext, SkSL::Program::kFragment_Kind, sksl, settings, &glsl, |
| 3308 | errorHandler); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3309 | GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, |
Brian Osman | 8518f2e | 2019-05-01 14:13:41 -0400 | [diff] [blame] | 3310 | GR_GL_FRAGMENT_SHADER, glsl, &fStats, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 3311 | errorHandler); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 3312 | SkASSERT(program->fInputs.isEmpty()); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3313 | |
| 3314 | GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram)); |
| 3315 | |
| 3316 | GL_CALL_RET(fMipmapPrograms[progIdx].fTextureUniform, |
| 3317 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texture")); |
| 3318 | GL_CALL_RET(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 3319 | GetUniformLocation(fMipmapPrograms[progIdx].fProgram, "u_texCoordXform")); |
| 3320 | |
| 3321 | GL_CALL(BindAttribLocation(fMipmapPrograms[progIdx].fProgram, 0, "a_vertex")); |
| 3322 | |
| 3323 | GL_CALL(DeleteShader(vshader)); |
| 3324 | GL_CALL(DeleteShader(fshader)); |
| 3325 | |
| 3326 | return true; |
| 3327 | } |
| 3328 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3329 | bool GrGLGpu::copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3330 | const SkIPoint& dstPoint) { |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3331 | auto* srcTex = static_cast<GrGLTexture*>(src->asTexture()); |
| 3332 | auto* dstTex = static_cast<GrGLTexture*>(src->asTexture()); |
| 3333 | auto* dstRT = static_cast<GrGLRenderTarget*>(src->asRenderTarget()); |
| 3334 | if (!srcTex) { |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3335 | return false; |
| 3336 | } |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3337 | int progIdx = TextureToCopyProgramIdx(srcTex); |
| 3338 | if (!dstRT) { |
| 3339 | SkASSERT(dstTex); |
| 3340 | if (!this->glCaps().isFormatRenderable(dstTex->format(), 1)) { |
| 3341 | return false; |
| 3342 | } |
| 3343 | } |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3344 | if (!fCopyPrograms[progIdx].fProgram) { |
| 3345 | if (!this->createCopyProgram(srcTex)) { |
| 3346 | SkDebugf("Failed to create copy program.\n"); |
| 3347 | return false; |
| 3348 | } |
| 3349 | } |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3350 | int w = srcRect.width(); |
| 3351 | int h = srcRect.height(); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 3352 | // We don't swizzle at all in our copies. |
| 3353 | this->bindTexture(0, GrSamplerState::ClampNearest(), GrSwizzle::RGBA(), srcTex); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3354 | this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER, kDst_TempFBOTarget); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3355 | this->flushViewport(dst->width(), dst->height()); |
| 3356 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3357 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, w, h); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3358 | this->flushProgram(fCopyPrograms[progIdx].fProgram); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3359 | fHWVertexArrayState.setVertexArrayID(this, 0); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3360 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
| 3361 | attribs->enableVertexArrays(this, 1); |
| 3362 | attribs->set(this, 0, fCopyProgramArrayBuffer.get(), kFloat2_GrVertexAttribType, |
| 3363 | kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3364 | // dst rect edges in NDC (-1 to 1) |
| 3365 | int dw = dst->width(); |
| 3366 | int dh = dst->height(); |
| 3367 | GrGLfloat dx0 = 2.f * dstPoint.fX / dw - 1.f; |
| 3368 | GrGLfloat dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f; |
| 3369 | GrGLfloat dy0 = 2.f * dstPoint.fY / dh - 1.f; |
| 3370 | GrGLfloat dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f; |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3371 | GrGLfloat sx0 = (GrGLfloat)srcRect.fLeft; |
| 3372 | GrGLfloat sx1 = (GrGLfloat)(srcRect.fLeft + w); |
| 3373 | GrGLfloat sy0 = (GrGLfloat)srcRect.fTop; |
| 3374 | GrGLfloat sy1 = (GrGLfloat)(srcRect.fTop + h); |
| 3375 | int sw = src->width(); |
| 3376 | int sh = src->height(); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3377 | if (srcTex->texturePriv().textureType() != GrTextureType::kRectangle) { |
| 3378 | // src rect edges in normalized texture space (0 to 1) |
| 3379 | sx0 /= sw; |
| 3380 | sx1 /= sw; |
| 3381 | sy0 /= sh; |
| 3382 | sy1 /= sh; |
| 3383 | } |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3384 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fPosXformUniform, dx1 - dx0, dy1 - dy0, dx0, dy0)); |
| 3385 | GL_CALL(Uniform4f(fCopyPrograms[progIdx].fTexCoordXformUniform, |
| 3386 | sx1 - sx0, sy1 - sy0, sx0, sy0)); |
| 3387 | GL_CALL(Uniform1i(fCopyPrograms[progIdx].fTextureUniform, 0)); |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 3388 | this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA()); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3389 | this->flushHWAAState(nullptr, false); |
Chris Dalton | ce425af | 2019-12-16 10:39:03 -0700 | [diff] [blame] | 3390 | this->flushConservativeRasterState(false); |
Chris Dalton | 1215cda | 2019-12-17 21:44:04 -0700 | [diff] [blame] | 3391 | this->flushWireframeState(false); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3392 | this->disableScissor(); |
| 3393 | this->disableWindowRectangles(); |
| 3394 | this->disableStencil(); |
| 3395 | if (this->glCaps().srgbWriteControl()) { |
| 3396 | this->flushFramebufferSRGB(true); |
| 3397 | } |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3398 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3399 | this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_FRAMEBUFFER); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3400 | // The rect is already in device space so we pass in kTopLeft so no flip is done. |
| 3401 | this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 3402 | return true; |
| 3403 | } |
| 3404 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3405 | void GrGLGpu::copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3406 | const SkIPoint& dstPoint) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3407 | SkASSERT(can_copy_texsubimage(dst, src, this->glCaps())); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3408 | this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER, kSrc_TempFBOTarget); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3409 | GrGLTexture* dstTex = static_cast<GrGLTexture *>(dst->asTexture()); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3410 | SkASSERT(dstTex); |
| 3411 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3412 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3413 | |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 3414 | this->bindTextureToScratchUnit(dstTex->target(), dstTex->textureID()); |
bsalomon | 10528f1 | 2015-10-14 12:54:52 -0700 | [diff] [blame] | 3415 | GL_CALL(CopyTexSubImage2D(dstTex->target(), 0, |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3416 | dstPoint.fX, dstPoint.fY, |
| 3417 | srcRect.fLeft, srcRect.fTop, |
| 3418 | srcRect.width(), srcRect.height())); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3419 | this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_FRAMEBUFFER); |
bsalomon | 083617b | 2016-02-12 12:10:14 -0800 | [diff] [blame] | 3420 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 3421 | srcRect.width(), srcRect.height()); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3422 | // The rect is already in device space so we pass in kTopLeft so no flip is done. |
| 3423 | this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3424 | } |
| 3425 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3426 | bool GrGLGpu::copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3427 | const SkIPoint& dstPoint) { |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3428 | SkASSERT(can_blit_framebuffer_for_copy_surface(dst, src, srcRect, dstPoint, this->glCaps())); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3429 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 3430 | srcRect.width(), srcRect.height()); |
| 3431 | if (dst == src) { |
Mike Reed | 3012cba | 2019-08-26 10:31:13 -0400 | [diff] [blame] | 3432 | if (SkIRect::Intersects(dstRect, srcRect)) { |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3433 | return false; |
mtklein | 404b3b2 | 2015-05-18 09:29:10 -0700 | [diff] [blame] | 3434 | } |
bsalomon | 5df6fee | 2015-05-18 06:26:15 -0700 | [diff] [blame] | 3435 | } |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3436 | |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3437 | this->bindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER, kDst_TempFBOTarget); |
| 3438 | this->bindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER, kSrc_TempFBOTarget); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3439 | // We modified the bound FBO |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3440 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3441 | |
| 3442 | // BlitFrameBuffer respects the scissor, so disable it. |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 3443 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 3444 | this->disableWindowRectangles(); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3445 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3446 | GL_CALL(BlitFramebuffer(srcRect.fLeft, |
| 3447 | srcRect.fTop, |
| 3448 | srcRect.fRight, |
| 3449 | srcRect.fBottom, |
| 3450 | dstRect.fLeft, |
| 3451 | dstRect.fTop, |
| 3452 | dstRect.fRight, |
| 3453 | dstRect.fBottom, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3454 | GR_GL_COLOR_BUFFER_BIT, GR_GL_NEAREST)); |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3455 | this->unbindSurfaceFBOForPixelOps(dst, 0, GR_GL_DRAW_FRAMEBUFFER); |
| 3456 | this->unbindSurfaceFBOForPixelOps(src, 0, GR_GL_READ_FRAMEBUFFER); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 3457 | |
| 3458 | // The rect is already in device space so we pass in kTopLeft so no flip is done. |
| 3459 | this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3460 | return true; |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3463 | bool GrGLGpu::onRegenerateMipMapLevels(GrTexture* texture) { |
| 3464 | auto glTex = static_cast<GrGLTexture*>(texture); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3465 | // Mipmaps are only supported on 2D textures: |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3466 | if (GR_GL_TEXTURE_2D != glTex->target()) { |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3467 | return false; |
| 3468 | } |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3469 | GrGLFormat format = glTex->format(); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3470 | // Manual implementation of mipmap generation, to work around driver bugs w/sRGB. |
| 3471 | // Uses draw calls to do a series of downsample operations to successive mips. |
| 3472 | |
| 3473 | // The manual approach requires the ability to limit which level we're sampling and that the |
| 3474 | // destination can be bound to a FBO: |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 3475 | if (!this->glCaps().doManualMipmapping() || !this->glCaps().isFormatRenderable(format, 1)) { |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3476 | GrGLenum target = glTex->target(); |
Brian Salomon | d978b90 | 2019-02-07 15:09:18 -0500 | [diff] [blame] | 3477 | this->bindTextureToScratchUnit(target, glTex->textureID()); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3478 | GL_CALL(GenerateMipmap(glTex->target())); |
| 3479 | return true; |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3480 | } |
| 3481 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3482 | int width = texture->width(); |
| 3483 | int height = texture->height(); |
| 3484 | int levelCount = SkMipMap::ComputeLevelCount(width, height) + 1; |
Greg Daniel | da86e28 | 2018-06-13 09:41:19 -0400 | [diff] [blame] | 3485 | SkASSERT(levelCount == texture->texturePriv().maxMipMapLevel() + 1); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3486 | |
| 3487 | // Create (if necessary), then bind temporary FBO: |
| 3488 | if (0 == fTempDstFBOID) { |
| 3489 | GL_CALL(GenFramebuffers(1, &fTempDstFBOID)); |
| 3490 | } |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3491 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, fTempDstFBOID); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 3492 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3493 | |
| 3494 | // Bind the texture, to get things configured for filtering. |
| 3495 | // We'll be changing our base level further below: |
| 3496 | this->setTextureUnit(0); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 3497 | // The mipmap program does not do any swizzling. |
| 3498 | this->bindTexture(0, GrSamplerState::ClampBilerp(), GrSwizzle::RGBA(), glTex); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3499 | |
| 3500 | // Vertex data: |
| 3501 | if (!fMipmapProgramArrayBuffer) { |
| 3502 | static const GrGLfloat vdata[] = { |
| 3503 | 0, 0, |
| 3504 | 0, 1, |
| 3505 | 1, 0, |
| 3506 | 1, 1 |
| 3507 | }; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 3508 | fMipmapProgramArrayBuffer = GrGLBuffer::Make(this, sizeof(vdata), GrGpuBufferType::kVertex, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 3509 | kStatic_GrAccessPattern, vdata); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3510 | } |
| 3511 | if (!fMipmapProgramArrayBuffer) { |
| 3512 | return false; |
| 3513 | } |
| 3514 | |
| 3515 | fHWVertexArrayState.setVertexArrayID(this, 0); |
| 3516 | |
| 3517 | GrGLAttribArrayState* attribs = fHWVertexArrayState.bindInternalVertexArray(this); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 3518 | attribs->enableVertexArrays(this, 1); |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 3519 | attribs->set(this, 0, fMipmapProgramArrayBuffer.get(), kFloat2_GrVertexAttribType, |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 3520 | kFloat2_GrSLType, 2 * sizeof(GrGLfloat), 0); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3521 | |
| 3522 | // Set "simple" state once: |
Chris Dalton | bbb3f64 | 2019-07-24 12:25:08 -0400 | [diff] [blame] | 3523 | this->flushBlendAndColorWrite(GrXferProcessor::BlendInfo(), GrSwizzle::RGBA()); |
Chris Dalton | 4c56b03 | 2019-03-04 12:28:42 -0700 | [diff] [blame] | 3524 | this->flushHWAAState(nullptr, false); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 3525 | this->disableScissor(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 3526 | this->disableWindowRectangles(); |
csmartdalton | c7d8533 | 2016-10-26 10:13:46 -0700 | [diff] [blame] | 3527 | this->disableStencil(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3528 | |
| 3529 | // Do all the blits: |
| 3530 | width = texture->width(); |
| 3531 | height = texture->height(); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 3532 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3533 | for (GrGLint level = 1; level < levelCount; ++level) { |
| 3534 | // Get and bind the program for this particular downsample (filter shape can vary): |
| 3535 | int progIdx = TextureSizeToMipmapProgramIdx(width, height); |
| 3536 | if (!fMipmapPrograms[progIdx].fProgram) { |
| 3537 | if (!this->createMipmapProgram(progIdx)) { |
| 3538 | SkDebugf("Failed to create mipmap program.\n"); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 3539 | // Invalidate all params to cover base level change in a previous iteration. |
| 3540 | glTex->textureParamsModified(); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3541 | return false; |
| 3542 | } |
| 3543 | } |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 3544 | this->flushProgram(fMipmapPrograms[progIdx].fProgram); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3545 | |
| 3546 | // Texcoord uniform is expected to contain (1/w, (w-1)/w, 1/h, (h-1)/h) |
| 3547 | const float invWidth = 1.0f / width; |
| 3548 | const float invHeight = 1.0f / height; |
| 3549 | GL_CALL(Uniform4f(fMipmapPrograms[progIdx].fTexCoordXformUniform, |
| 3550 | invWidth, (width - 1) * invWidth, invHeight, (height - 1) * invHeight)); |
| 3551 | GL_CALL(Uniform1i(fMipmapPrograms[progIdx].fTextureUniform, 0)); |
| 3552 | |
| 3553 | // Only sample from previous mip |
| 3554 | GL_CALL(TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BASE_LEVEL, level - 1)); |
| 3555 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3556 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D, |
| 3557 | glTex->textureID(), level)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3558 | |
| 3559 | width = SkTMax(1, width / 2); |
| 3560 | height = SkTMax(1, height / 2); |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 3561 | this->flushViewport(width, height); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3562 | |
| 3563 | GL_CALL(DrawArrays(GR_GL_TRIANGLE_STRIP, 0, 4)); |
| 3564 | } |
| 3565 | |
| 3566 | // Unbind: |
| 3567 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 3568 | GR_GL_TEXTURE_2D, 0, 0)); |
| 3569 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 3570 | // We modified the base level param. |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 3571 | GrGLTextureParameters::NonsamplerState nonsamplerState = glTex->parameters()->nonsamplerState(); |
| 3572 | // We drew the 2nd to last level into the last level. |
| 3573 | nonsamplerState.fBaseMipMapLevel = levelCount - 2; |
| 3574 | glTex->parameters()->set(nullptr, nonsamplerState, fResetTimestampForTextureParameters); |
Brian Salomon | dc82994 | 2018-10-23 16:07:24 -0400 | [diff] [blame] | 3575 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 3576 | return true; |
| 3577 | } |
| 3578 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 3579 | void GrGLGpu::querySampleLocations( |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 3580 | GrRenderTarget* renderTarget, SkTArray<SkPoint>* sampleLocations) { |
| 3581 | this->flushRenderTargetNoColorWrites(static_cast<GrGLRenderTarget*>(renderTarget)); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 3582 | |
| 3583 | int effectiveSampleCnt; |
| 3584 | GR_GL_GetIntegerv(this->glInterface(), GR_GL_SAMPLES, &effectiveSampleCnt); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 3585 | SkASSERT(effectiveSampleCnt >= renderTarget->numSamples()); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 3586 | |
| 3587 | sampleLocations->reset(effectiveSampleCnt); |
| 3588 | for (int i = 0; i < effectiveSampleCnt; ++i) { |
| 3589 | GL_CALL(GetMultisamplefv(GR_GL_SAMPLE_POSITION, i, &(*sampleLocations)[i].fX)); |
| 3590 | } |
| 3591 | } |
| 3592 | |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3593 | void GrGLGpu::xferBarrier(GrRenderTarget* rt, GrXferBarrierType type) { |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 3594 | SkASSERT(type); |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3595 | switch (type) { |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3596 | case kTexture_GrXferBarrierType: { |
| 3597 | GrGLRenderTarget* glrt = static_cast<GrGLRenderTarget*>(rt); |
Brian Osman | cfe83d1 | 2018-01-19 11:13:45 -0500 | [diff] [blame] | 3598 | SkASSERT(glrt->textureFBOID() != 0 && glrt->renderFBOID() != 0); |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3599 | if (glrt->textureFBOID() != glrt->renderFBOID()) { |
| 3600 | // The render target uses separate storage so no need for glTextureBarrier. |
| 3601 | // FIXME: The render target will resolve automatically when its texture is bound, |
| 3602 | // but we could resolve only the bounds that will be read if we do it here instead. |
| 3603 | return; |
| 3604 | } |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3605 | SkASSERT(this->caps()->textureBarrierSupport()); |
| 3606 | GL_CALL(TextureBarrier()); |
| 3607 | return; |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 3608 | } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 3609 | case kBlend_GrXferBarrierType: |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 3610 | SkASSERT(GrCaps::kAdvanced_BlendEquationSupport == |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 3611 | this->caps()->blendEquationSupport()); |
| 3612 | GL_CALL(BlendBarrier()); |
| 3613 | return; |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 3614 | default: break; // placate compiler warnings that kNone not handled |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 3615 | } |
| 3616 | } |
| 3617 | |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3618 | static GrPixelConfig gl_format_to_pixel_config(GrGLFormat format) { |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 3619 | switch (format) { |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3620 | case GrGLFormat::kRGBA8: return kRGBA_8888_GrPixelConfig; |
| 3621 | case GrGLFormat::kRGB8: return kRGB_888_GrPixelConfig; |
| 3622 | case GrGLFormat::kRG8: return kRG_88_GrPixelConfig; |
| 3623 | case GrGLFormat::kBGRA8: return kBGRA_8888_GrPixelConfig; |
| 3624 | case GrGLFormat::kLUMINANCE8: return kGray_8_GrPixelConfig; |
| 3625 | case GrGLFormat::kSRGB8_ALPHA8: return kSRGBA_8888_GrPixelConfig; |
| 3626 | case GrGLFormat::kRGB10_A2: return kRGBA_1010102_GrPixelConfig; |
| 3627 | case GrGLFormat::kRGB565: return kRGB_565_GrPixelConfig; |
| 3628 | case GrGLFormat::kRGBA4: return kRGBA_4444_GrPixelConfig; |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3629 | case GrGLFormat::kRGBA16F: return kRGBA_half_GrPixelConfig; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 3630 | case GrGLFormat::kR16: return kAlpha_16_GrPixelConfig; |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3631 | case GrGLFormat::kRG16: return kRG_1616_GrPixelConfig; |
| 3632 | case GrGLFormat::kRGBA16: return kRGBA_16161616_GrPixelConfig; |
| 3633 | case GrGLFormat::kRG16F: return kRG_half_GrPixelConfig; |
| 3634 | case GrGLFormat::kUnknown: return kUnknown_GrPixelConfig; |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 3635 | |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3636 | // Configs with multiple equivalent formats. |
| 3637 | |
Robert Phillips | ebab03f | 2019-07-22 08:48:18 -0400 | [diff] [blame] | 3638 | case GrGLFormat::kR16F: return kAlpha_half_GrPixelConfig; |
| 3639 | case GrGLFormat::kLUMINANCE16F: return kAlpha_half_GrPixelConfig; |
| 3640 | |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3641 | case GrGLFormat::kALPHA8: return kAlpha_8_GrPixelConfig; |
| 3642 | case GrGLFormat::kR8: return kAlpha_8_GrPixelConfig; |
| 3643 | |
| 3644 | case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_ETC1_GrPixelConfig; |
| 3645 | case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_ETC1_GrPixelConfig; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 3646 | } |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3647 | SkUNREACHABLE; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 3648 | } |
| 3649 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3650 | GrBackendTexture GrGLGpu::onCreateBackendTexture(SkISize dimensions, |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 3651 | const GrBackendFormat& format, |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 3652 | GrRenderable renderable, |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3653 | const BackendTextureData* data, |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 3654 | GrMipMapped mipMapped, |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 3655 | GrProtected isProtected) { |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 3656 | // We don't support protected textures in GL. |
| 3657 | if (isProtected == GrProtected::kYes) { |
| 3658 | return {}; |
| 3659 | } |
| 3660 | |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 3661 | this->handleDirtyContext(); |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 3662 | |
Brian Salomon | d4764a1 | 2019-08-08 12:08:24 -0400 | [diff] [blame] | 3663 | GrGLFormat glFormat = format.asGLFormat(); |
Brian Salomon | 5043f1f | 2019-07-11 21:27:54 -0400 | [diff] [blame] | 3664 | if (glFormat == GrGLFormat::kUnknown) { |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 3665 | return {}; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 3666 | } |
| 3667 | |
Robert Phillips | 0d7e2f1 | 2019-12-18 13:01:04 -0500 | [diff] [blame^] | 3668 | int numMipLevels = 1; |
| 3669 | if (mipMapped == GrMipMapped::kYes) { |
| 3670 | numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
| 3671 | } |
| 3672 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 3673 | // Compressed formats go through onCreateCompressedBackendTexture |
| 3674 | SkASSERT(!GrGLFormatIsCompressed(glFormat)); |
| 3675 | |
Greg Daniel | 1550064 | 2019-06-27 03:05:55 +0000 | [diff] [blame] | 3676 | GrGLTextureInfo info; |
| 3677 | GrGLTextureParameters::SamplerOverriddenState initialState; |
| 3678 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 3679 | GrSurfaceDesc desc; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3680 | desc.fWidth = dimensions.width(); |
| 3681 | desc.fHeight = dimensions.height(); |
| 3682 | desc.fConfig = gl_format_to_pixel_config(glFormat); |
| 3683 | if (desc.fConfig == kUnknown_GrPixelConfig) { |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 3684 | return {}; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3685 | } |
Robert Phillips | 57ef680 | 2019-09-23 10:12:47 -0400 | [diff] [blame] | 3686 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 3687 | info.fTarget = GR_GL_TEXTURE_2D; |
| 3688 | info.fFormat = GrGLFormatToEnum(glFormat); |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3689 | info.fID = this->createTexture2D(dimensions, glFormat, renderable, &initialState, numMipLevels); |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 3690 | if (!info.fID) { |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3691 | return {}; |
Robert Phillips | e3bd673 | 2019-05-29 14:20:35 -0400 | [diff] [blame] | 3692 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 3693 | |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 3694 | SkASSERT(!data || data->type() != BackendTextureData::Type::kCompressed); |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3695 | if (data && data->type() == BackendTextureData::Type::kPixmaps) { |
| 3696 | SkTDArray<GrMipLevel> texels; |
| 3697 | GrColorType colorType = SkColorTypeToGrColorType(data->pixmap(0).colorType()); |
| 3698 | // Incorporate the color type into the config to make it "specific" if applicable. |
| 3699 | desc.fConfig = this->caps()->getConfigFromBackendFormat(format, colorType); |
| 3700 | SkASSERT(desc.fConfig != kUnknown_GrPixelConfig); |
| 3701 | texels.append(numMipLevels); |
| 3702 | for (int i = 0; i < numMipLevels; ++i) { |
| 3703 | texels[i] = {data->pixmap(i).addr(), data->pixmap(i).rowBytes()}; |
| 3704 | } |
| 3705 | if (!this->uploadTexData(glFormat, colorType, desc.fWidth, desc.fHeight, GR_GL_TEXTURE_2D, |
| 3706 | 0, 0, desc.fWidth, desc.fHeight, colorType, texels.begin(), |
| 3707 | texels.count())) { |
| 3708 | GL_CALL(DeleteTextures(1, &info.fID)); |
| 3709 | return {}; |
| 3710 | } |
| 3711 | } else if (data && data->type() == BackendTextureData::Type::kColor) { |
| 3712 | // TODO: Unify this with the clear texture code in onCreateTexture(). |
| 3713 | GrColorType colorType; |
| 3714 | GrGLenum externalFormat, externalType; |
| 3715 | this->glCaps().getTexSubImageDefaultFormatTypeAndColorType(glFormat, &externalFormat, |
| 3716 | &externalType, &colorType); |
| 3717 | if (colorType == GrColorType::kUnknown) { |
| 3718 | GL_CALL(DeleteTextures(1, &info.fID)); |
| 3719 | return {}; |
| 3720 | } |
| 3721 | |
| 3722 | // Make one tight image at the base size and reuse it for smaller levels. |
| 3723 | GrImageInfo ii(colorType, kUnpremul_SkAlphaType, nullptr, dimensions); |
| 3724 | auto rb = ii.minRowBytes(); |
| 3725 | std::unique_ptr<char[]> pixelStorage(new char[rb * dimensions.height()]); |
| 3726 | if (!GrClearImage(ii, pixelStorage.get(), rb, data->color())) { |
| 3727 | GL_CALL(DeleteTextures(1, &info.fID)); |
| 3728 | return {}; |
| 3729 | } |
| 3730 | |
| 3731 | GL_CALL(PixelStorei(GR_GL_UNPACK_ALIGNMENT, 1)); |
| 3732 | SkISize levelDimensions = dimensions; |
| 3733 | for (int i = 0; i < numMipLevels; ++i) { |
| 3734 | GL_CALL(TexSubImage2D(GR_GL_TEXTURE_2D, i, 0, 0, levelDimensions.width(), |
| 3735 | levelDimensions.height(), externalFormat, externalType, |
| 3736 | pixelStorage.get())); |
| 3737 | levelDimensions = {SkTMax(1, levelDimensions.width() /2), |
| 3738 | SkTMax(1, levelDimensions.height()/2)}; |
| 3739 | } |
| 3740 | } |
| 3741 | // Unbind this texture from the scratch texture unit. |
| 3742 | this->bindTextureToScratchUnit(GR_GL_TEXTURE_2D, 0); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 3743 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 3744 | auto parameters = sk_make_sp<GrGLTextureParameters>(); |
Robert Phillips | 42716d4 | 2019-12-16 12:19:54 -0500 | [diff] [blame] | 3745 | // The non-sampler params are still at their default values. |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 3746 | parameters->set(&initialState, GrGLTextureParameters::NonsamplerState(), |
| 3747 | fResetTimestampForTextureParameters); |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 3748 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 3749 | return GrBackendTexture(dimensions.width(), dimensions.height(), mipMapped, info, |
| 3750 | std::move(parameters)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 3751 | } |
| 3752 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 3753 | void GrGLGpu::deleteBackendTexture(const GrBackendTexture& tex) { |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 3754 | SkASSERT(GrBackendApi::kOpenGL == tex.backend()); |
| 3755 | |
| 3756 | GrGLTextureInfo info; |
| 3757 | if (tex.getGLTextureInfo(&info)) { |
| 3758 | GL_CALL(DeleteTextures(1, &info.fID)); |
| 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | #if GR_TEST_UTILS |
| 3763 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 3764 | bool GrGLGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 3765 | SkASSERT(GrBackendApi::kOpenGL == tex.backend()); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 3766 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 3767 | GrGLTextureInfo info; |
| 3768 | if (!tex.getGLTextureInfo(&info)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 3769 | return false; |
| 3770 | } |
| 3771 | |
| 3772 | GrGLboolean result; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 3773 | GL_CALL_RET(result, IsTexture(info.fID)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 3774 | |
| 3775 | return (GR_GL_TRUE == result); |
| 3776 | } |
| 3777 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3778 | GrBackendRenderTarget GrGLGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 3779 | GrColorType colorType) { |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 3780 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
| 3781 | return GrBackendRenderTarget(); // invalid |
| 3782 | } |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 3783 | this->handleDirtyContext(); |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 3784 | auto format = this->glCaps().getFormatFromColorType(colorType); |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 3785 | if (!this->glCaps().isFormatRenderable(format, 1)) { |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3786 | return {}; |
| 3787 | } |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3788 | bool useTexture = format == GrGLFormat::kBGRA8; |
Brian Salomon | f6da146 | 2019-07-11 14:21:59 -0400 | [diff] [blame] | 3789 | int sFormatIdx = this->getCompatibleStencilIndex(format); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3790 | if (sFormatIdx < 0) { |
| 3791 | return {}; |
| 3792 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3793 | GrGLuint colorID = 0; |
| 3794 | GrGLuint stencilID = 0; |
| 3795 | auto deleteIDs = [&] { |
| 3796 | if (colorID) { |
| 3797 | if (useTexture) { |
| 3798 | GL_CALL(DeleteTextures(1, &colorID)); |
| 3799 | } else { |
| 3800 | GL_CALL(DeleteRenderbuffers(1, &colorID)); |
| 3801 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3802 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3803 | if (stencilID) { |
| 3804 | GL_CALL(DeleteRenderbuffers(1, &stencilID)); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3805 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3806 | }; |
| 3807 | |
| 3808 | if (useTexture) { |
| 3809 | GL_CALL(GenTextures(1, &colorID)); |
| 3810 | } else { |
| 3811 | GL_CALL(GenRenderbuffers(1, &colorID)); |
| 3812 | } |
| 3813 | GL_CALL(GenRenderbuffers(1, &stencilID)); |
| 3814 | if (!stencilID || !colorID) { |
| 3815 | deleteIDs(); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3816 | return {}; |
| 3817 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3818 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3819 | GrGLFramebufferInfo info; |
| 3820 | info.fFBOID = 0; |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3821 | info.fFormat = GrGLFormatToEnum(format); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3822 | GL_CALL(GenFramebuffers(1, &info.fFBOID)); |
| 3823 | if (!info.fFBOID) { |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3824 | deleteIDs(); |
| 3825 | return {}; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | this->invalidateBoundRenderTarget(); |
| 3829 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3830 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID); |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3831 | if (useTexture) { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3832 | GrGLTextureParameters::SamplerOverriddenState initialState; |
| 3833 | colorID = this->createTexture2D({w, h}, format, GrRenderable::kYes, &initialState, 1); |
| 3834 | if (!colorID) { |
| 3835 | deleteIDs(); |
| 3836 | return {}; |
| 3837 | } |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3838 | GL_CALL(FramebufferTexture2D(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_TEXTURE_2D, |
| 3839 | colorID, 0)); |
| 3840 | } else { |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3841 | GrGLenum renderBufferFormat = this->glCaps().getRenderbufferInternalFormat(format); |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3842 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, colorID)); |
| 3843 | GL_ALLOC_CALL(this->glInterface(), |
Brian Salomon | d2a8ae2 | 2019-09-10 16:03:59 -0400 | [diff] [blame] | 3844 | RenderbufferStorage(GR_GL_RENDERBUFFER, renderBufferFormat, w, h)); |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3845 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, |
| 3846 | GR_GL_RENDERBUFFER, colorID)); |
| 3847 | } |
| 3848 | GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, stencilID)); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3849 | auto stencilBufferFormat = this->glCaps().stencilFormats()[sFormatIdx].fInternalFormat; |
| 3850 | GL_ALLOC_CALL(this->glInterface(), |
| 3851 | RenderbufferStorage(GR_GL_RENDERBUFFER, stencilBufferFormat, w, h)); |
| 3852 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_STENCIL_ATTACHMENT, GR_GL_RENDERBUFFER, |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3853 | stencilID)); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3854 | if (this->glCaps().stencilFormats()[sFormatIdx].fPacked) { |
| 3855 | GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_DEPTH_ATTACHMENT, |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3856 | GR_GL_RENDERBUFFER, stencilID)); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3857 | } |
| 3858 | |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3859 | // We don't want to have to recover the renderbuffer/texture IDs later to delete them. OpenGL |
| 3860 | // has this rule that if a renderbuffer/texture is deleted and a FBO other than the current FBO |
| 3861 | // has the RB attached then deletion is delayed. So we unbind the FBO here and delete the |
| 3862 | // renderbuffers/texture. |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3863 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, 0); |
Brian Salomon | 93348dd | 2018-08-29 12:56:23 -0400 | [diff] [blame] | 3864 | deleteIDs(); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3865 | |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3866 | this->bindFramebuffer(GR_GL_FRAMEBUFFER, info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3867 | GrGLenum status; |
| 3868 | GL_CALL_RET(status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 3869 | if (GR_GL_FRAMEBUFFER_COMPLETE != status) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3870 | this->deleteFramebuffer(info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3871 | return {}; |
| 3872 | } |
| 3873 | auto stencilBits = SkToInt(this->glCaps().stencilFormats()[sFormatIdx].fStencilBits); |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 3874 | |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 3875 | GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, stencilBits, info); |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 3876 | SkASSERT(this->caps()->areColorTypeAndFormatCompatible(colorType, beRT.getBackendFormat())); |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 3877 | return beRT; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | void GrGLGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 3881 | SkASSERT(GrBackendApi::kOpenGL == backendRT.backend()); |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 3882 | GrGLFramebufferInfo info; |
| 3883 | if (backendRT.getGLFramebufferInfo(&info)) { |
| 3884 | if (info.fFBOID) { |
Adrienne Walker | 4ee8851 | 2018-05-17 11:37:14 -0700 | [diff] [blame] | 3885 | this->deleteFramebuffer(info.fFBOID); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3886 | } |
| 3887 | } |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 3890 | void GrGLGpu::testingOnly_flushGpuAndSync() { |
| 3891 | GL_CALL(Finish()); |
| 3892 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 3893 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 3894 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3895 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3896 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3897 | GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 3898 | const GrBuffer* ibuf) { |
robertphillips@google.com | 4f65a27 | 2013-03-26 19:40:46 +0000 | [diff] [blame] | 3899 | GrGLAttribArrayState* attribState; |
| 3900 | |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3901 | if (gpu->glCaps().isCoreProfile()) { |
| 3902 | if (!fCoreProfileVertexArray) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3903 | GrGLuint arrayID; |
| 3904 | GR_GL_CALL(gpu->glInterface(), GenVertexArrays(1, &arrayID)); |
| 3905 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3906 | fCoreProfileVertexArray = new GrGLVertexArray(arrayID, attrCount); |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3907 | } |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3908 | if (ibuf) { |
| 3909 | attribState = fCoreProfileVertexArray->bindWithIndexBuffer(gpu, ibuf); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3910 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3911 | attribState = fCoreProfileVertexArray->bind(gpu); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 3912 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3913 | } else { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 3914 | if (ibuf) { |
| 3915 | // bindBuffer implicitly binds VAO 0 when binding an index buffer. |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 3916 | gpu->bindBuffer(GrGpuBufferType::kIndex, ibuf); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3917 | } else { |
| 3918 | this->setVertexArrayID(gpu, 0); |
| 3919 | } |
| 3920 | int attrCount = gpu->glCaps().maxVertexAttributes(); |
| 3921 | if (fDefaultVertexArrayAttribState.count() != attrCount) { |
| 3922 | fDefaultVertexArrayAttribState.resize(attrCount); |
| 3923 | } |
| 3924 | attribState = &fDefaultVertexArrayAttribState; |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 3925 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 3926 | return attribState; |
bsalomon@google.com | 7acdb8e | 2011-02-11 14:07:02 +0000 | [diff] [blame] | 3927 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame] | 3928 | |
Greg Daniel | 30a35e8 | 2019-11-19 14:12:25 -0500 | [diff] [blame] | 3929 | bool GrGLGpu::onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access, |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 3930 | const GrFlushInfo& info, const GrPrepareForExternalIORequests&) { |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 3931 | // If we inserted semaphores during the flush, we need to call GLFlush. |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 3932 | bool insertedSemaphore = info.fNumSemaphores > 0 && this->caps()->semaphoreSupport(); |
Brian Salomon | b0d8b76 | 2019-05-06 16:58:22 -0400 | [diff] [blame] | 3933 | // We call finish if the client told us to sync or if we have a finished proc but don't support |
| 3934 | // GLsync objects. |
| 3935 | bool finish = (info.fFlags & kSyncCpu_GrFlushFlag) || |
| 3936 | (info.fFinishedProc && !this->caps()->fenceSyncSupport()); |
| 3937 | if (finish) { |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 3938 | GL_CALL(Finish()); |
Brian Salomon | b0d8b76 | 2019-05-06 16:58:22 -0400 | [diff] [blame] | 3939 | // After a finish everything previously sent to GL is done. |
| 3940 | for (const auto& cb : fFinishCallbacks) { |
| 3941 | cb.fCallback(cb.fContext); |
| 3942 | this->deleteSync(cb.fSync); |
| 3943 | } |
| 3944 | fFinishCallbacks.clear(); |
| 3945 | if (info.fFinishedProc) { |
| 3946 | info.fFinishedProc(info.fFinishedContext); |
| 3947 | } |
| 3948 | } else { |
| 3949 | if (info.fFinishedProc) { |
| 3950 | FinishCallback callback; |
| 3951 | callback.fCallback = info.fFinishedProc; |
| 3952 | callback.fContext = info.fFinishedContext; |
| 3953 | callback.fSync = (GrGLsync)this->insertFence(); |
| 3954 | fFinishCallbacks.push_back(callback); |
| 3955 | GL_CALL(Flush()); |
| 3956 | } else if (insertedSemaphore) { |
| 3957 | // Must call flush after semaphores in case they are waited on another GL context. |
| 3958 | GL_CALL(Flush()); |
| 3959 | } |
| 3960 | // See if any previously inserted finish procs are good to go. |
| 3961 | this->checkFinishProcs(); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 3962 | } |
Greg Daniel | 30a35e8 | 2019-11-19 14:12:25 -0500 | [diff] [blame] | 3963 | return true; |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 3966 | void GrGLGpu::submit(GrOpsRenderPass* renderPass) { |
| 3967 | // The GrGLOpsRenderPass doesn't buffer ops so there is nothing to do here |
| 3968 | SkASSERT(fCachedOpsRenderPass.get() == renderPass); |
| 3969 | fCachedOpsRenderPass->reset(); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 3970 | } |
| 3971 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 3972 | GrFence SK_WARN_UNUSED_RESULT GrGLGpu::insertFence() { |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 3973 | SkASSERT(this->caps()->fenceSyncSupport()); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 3974 | GrGLsync sync; |
| 3975 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 3976 | static_assert(sizeof(GrFence) >= sizeof(GrGLsync)); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 3977 | return (GrFence)sync; |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 3978 | } |
| 3979 | |
Brian Salomon | b0d8b76 | 2019-05-06 16:58:22 -0400 | [diff] [blame] | 3980 | bool GrGLGpu::waitSync(GrGLsync sync, uint64_t timeout, bool flush) { |
| 3981 | GrGLbitfield flags = flush ? GR_GL_SYNC_FLUSH_COMMANDS_BIT : 0; |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 3982 | GrGLenum result; |
Brian Salomon | b0d8b76 | 2019-05-06 16:58:22 -0400 | [diff] [blame] | 3983 | GL_CALL_RET(result, ClientWaitSync(sync, flags, timeout)); |
| 3984 | return (GR_GL_CONDITION_SATISFIED == result || GR_GL_ALREADY_SIGNALED == result); |
| 3985 | } |
| 3986 | |
| 3987 | bool GrGLGpu::waitFence(GrFence fence, uint64_t timeout) { |
| 3988 | return this->waitSync((GrGLsync)fence, timeout, /* flush = */ true); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | void GrGLGpu::deleteFence(GrFence fence) const { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 3992 | this->deleteSync((GrGLsync)fence); |
| 3993 | } |
| 3994 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 3995 | std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT GrGLGpu::makeSemaphore(bool isOwned) { |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 3996 | SkASSERT(this->caps()->semaphoreSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 3997 | return GrGLSemaphore::Make(this, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 3998 | } |
| 3999 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4000 | std::unique_ptr<GrSemaphore> GrGLGpu::wrapBackendSemaphore( |
| 4001 | const GrBackendSemaphore& semaphore, |
| 4002 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 4003 | GrWrapOwnership ownership) { |
Brian Salomon | 9ff5acb | 2019-05-08 09:04:47 -0400 | [diff] [blame] | 4004 | SkASSERT(this->caps()->semaphoreSupport()); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 4005 | return GrGLSemaphore::MakeWrapped(this, semaphore.glSync(), ownership); |
| 4006 | } |
| 4007 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4008 | void GrGLGpu::insertSemaphore(GrSemaphore* semaphore) { |
| 4009 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4010 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 4011 | GrGLsync sync; |
| 4012 | GL_CALL_RET(sync, FenceSync(GR_GL_SYNC_GPU_COMMANDS_COMPLETE, 0)); |
| 4013 | glSem->setSync(sync); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4014 | } |
| 4015 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4016 | void GrGLGpu::waitSemaphore(GrSemaphore* semaphore) { |
| 4017 | GrGLSemaphore* glSem = static_cast<GrGLSemaphore*>(semaphore); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4018 | |
| 4019 | GL_CALL(WaitSync(glSem->sync(), 0, GR_GL_TIMEOUT_IGNORED)); |
| 4020 | } |
| 4021 | |
Brian Salomon | b0d8b76 | 2019-05-06 16:58:22 -0400 | [diff] [blame] | 4022 | void GrGLGpu::checkFinishProcs() { |
| 4023 | // Bail after the first unfinished sync since we expect they signal in the order inserted. |
| 4024 | while (!fFinishCallbacks.empty() && this->waitSync(fFinishCallbacks.front().fSync, |
| 4025 | /* timeout = */ 0, /* flush = */ false)) { |
| 4026 | fFinishCallbacks.front().fCallback(fFinishCallbacks.front().fContext); |
| 4027 | this->deleteSync(fFinishCallbacks.front().fSync); |
| 4028 | fFinishCallbacks.pop_front(); |
| 4029 | } |
| 4030 | } |
| 4031 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 4032 | void GrGLGpu::deleteSync(GrGLsync sync) const { |
| 4033 | GL_CALL(DeleteSync(sync)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 4034 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4035 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4036 | std::unique_ptr<GrSemaphore> GrGLGpu::prepareTextureForCrossContextUsage(GrTexture* texture) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4037 | // Set up a semaphore to be signaled once the data is ready, and flush GL |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4038 | std::unique_ptr<GrSemaphore> semaphore = this->makeSemaphore(true); |
Greg Daniel | 30a35e8 | 2019-11-19 14:12:25 -0500 | [diff] [blame] | 4039 | SkASSERT(semaphore); |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 4040 | this->insertSemaphore(semaphore.get()); |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 4041 | // We must call flush here to make sure the GrGLSync object gets created and sent to the gpu. |
| 4042 | GL_CALL(Flush()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 4043 | |
| 4044 | return semaphore; |
| 4045 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4046 | |
| 4047 | int GrGLGpu::TextureToCopyProgramIdx(GrTexture* texture) { |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 4048 | switch (GrSLCombinedSamplerTypeForTextureType(texture->texturePriv().textureType())) { |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4049 | case kTexture2DSampler_GrSLType: |
| 4050 | return 0; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4051 | case kTexture2DRectSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4052 | return 1; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4053 | case kTextureExternalSampler_GrSLType: |
Brian Salomon | 5711133 | 2018-02-05 15:55:54 -0500 | [diff] [blame] | 4054 | return 2; |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4055 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 4056 | SK_ABORT("Unexpected samper type"); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 4057 | } |
| 4058 | } |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 4059 | |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 4060 | #ifdef SK_ENABLE_DUMP_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 4061 | #include "src/utils/SkJSONWriter.h" |
Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 4062 | void GrGLGpu::onDumpJSON(SkJSONWriter* writer) const { |
| 4063 | // We are called by the base class, which has already called beginObject(). We choose to nest |
| 4064 | // all of our caps information in a named sub-object. |
| 4065 | writer->beginObject("GL GPU"); |
| 4066 | |
| 4067 | const GrGLubyte* str; |
| 4068 | GL_CALL_RET(str, GetString(GR_GL_VERSION)); |
| 4069 | writer->appendString("GL_VERSION", (const char*)(str)); |
| 4070 | GL_CALL_RET(str, GetString(GR_GL_RENDERER)); |
| 4071 | writer->appendString("GL_RENDERER", (const char*)(str)); |
| 4072 | GL_CALL_RET(str, GetString(GR_GL_VENDOR)); |
| 4073 | writer->appendString("GL_VENDOR", (const char*)(str)); |
| 4074 | GL_CALL_RET(str, GetString(GR_GL_SHADING_LANGUAGE_VERSION)); |
| 4075 | writer->appendString("GL_SHADING_LANGUAGE_VERSION", (const char*)(str)); |
| 4076 | |
| 4077 | writer->appendName("extensions"); |
| 4078 | glInterface()->fExtensions.dumpJSON(writer); |
| 4079 | |
| 4080 | writer->endObject(); |
| 4081 | } |
Kevin Lubick | f4def34 | 2018-10-04 12:52:50 -0400 | [diff] [blame] | 4082 | #endif |