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