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