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