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