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