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